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 first
, int size_only
);
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();
116 static void incr_local_scope(void)
121 static void decr_local_scope(void)
126 static void reset_local_scope(void)
128 if (current_cleanups
)
129 tcc_error("ICE current_cleanups");
130 sym_pop(&all_cleanups
, NULL
, 0);
134 ST_INLN
int is_float(int t
)
138 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
141 /* we use our own 'finite' function to avoid potential problems with
142 non standard math libs */
143 /* XXX: endianness dependent */
144 ST_FUNC
int ieee_finite(double d
)
147 memcpy(p
, &d
, sizeof(double));
148 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
151 /* compiling intel long double natively */
152 #if (defined __i386__ || defined __x86_64__) \
153 && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
154 # define TCC_IS_NATIVE_387
157 ST_FUNC
void test_lvalue(void)
159 if (!(vtop
->r
& VT_LVAL
))
163 ST_FUNC
void check_vstack(void)
166 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
169 /* ------------------------------------------------------------------------- */
170 /* vstack debugging aid */
173 void pv (const char *lbl
, int a
, int b
)
176 for (i
= a
; i
< a
+ b
; ++i
) {
177 SValue
*p
= &vtop
[-i
];
178 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
179 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
184 /* ------------------------------------------------------------------------- */
185 /* start of translation unit info */
186 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
191 /* file info: full path + filename */
192 section_sym
= put_elf_sym(symtab_section
, 0, 0,
193 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
194 text_section
->sh_num
, NULL
);
195 getcwd(buf
, sizeof(buf
));
197 normalize_slashes(buf
);
199 pstrcat(buf
, sizeof(buf
), "/");
200 put_stabs_r(buf
, N_SO
, 0, 0,
201 text_section
->data_offset
, text_section
, section_sym
);
202 put_stabs_r(file
->filename
, N_SO
, 0, 0,
203 text_section
->data_offset
, text_section
, section_sym
);
208 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
209 symbols can be safely used */
210 put_elf_sym(symtab_section
, 0, 0,
211 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
212 SHN_ABS
, file
->filename
);
215 /* put end of translation unit info */
216 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
220 put_stabs_r(NULL
, N_SO
, 0, 0,
221 text_section
->data_offset
, text_section
, section_sym
);
225 /* generate line number info */
226 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
230 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
231 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
233 last_line_num
= file
->line_num
;
237 /* put function symbol */
238 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
246 /* XXX: we put here a dummy type */
247 snprintf(buf
, sizeof(buf
), "%s:%c1",
248 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
249 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
250 cur_text_section
, sym
->c
);
251 /* //gr gdb wants a line at the function */
252 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
258 /* put function size */
259 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
263 put_stabn(N_FUN
, 0, 0, size
);
266 /* ------------------------------------------------------------------------- */
267 ST_FUNC
int tccgen_compile(TCCState
*s1
)
269 cur_text_section
= NULL
;
271 anon_sym
= SYM_FIRST_ANOM
;
274 nocode_wanted
= 0x80000000;
276 /* define some often used types */
278 char_pointer_type
.t
= VT_BYTE
;
279 mk_pointer(&char_pointer_type
);
281 size_type
.t
= VT_INT
| VT_UNSIGNED
;
282 ptrdiff_type
.t
= VT_INT
;
284 size_type
.t
= VT_LLONG
| VT_UNSIGNED
;
285 ptrdiff_type
.t
= VT_LLONG
;
287 size_type
.t
= VT_LONG
| VT_LLONG
| VT_UNSIGNED
;
288 ptrdiff_type
.t
= VT_LONG
| VT_LLONG
;
290 func_old_type
.t
= VT_FUNC
;
291 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, 0, 0);
292 func_old_type
.ref
->f
.func_call
= FUNC_CDECL
;
293 func_old_type
.ref
->f
.func_type
= FUNC_OLD
;
297 #ifdef TCC_TARGET_ARM
302 printf("%s: **** new file\n", file
->filename
);
305 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
308 gen_inline_functions(s1
);
310 /* end of translation unit info */
315 /* ------------------------------------------------------------------------- */
316 ST_FUNC ElfSym
*elfsym(Sym
*s
)
320 return &((ElfSym
*)symtab_section
->data
)[s
->c
];
323 /* apply storage attributes to Elf symbol */
324 ST_FUNC
void update_storage(Sym
*sym
)
327 int sym_bind
, old_sym_bind
;
333 if (sym
->a
.visibility
)
334 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
337 if (sym
->type
.t
& VT_STATIC
)
338 sym_bind
= STB_LOCAL
;
339 else if (sym
->a
.weak
)
342 sym_bind
= STB_GLOBAL
;
343 old_sym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
344 if (sym_bind
!= old_sym_bind
) {
345 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, ELFW(ST_TYPE
)(esym
->st_info
));
349 if (sym
->a
.dllimport
)
350 esym
->st_other
|= ST_PE_IMPORT
;
351 if (sym
->a
.dllexport
)
352 esym
->st_other
|= ST_PE_EXPORT
;
356 printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
357 get_tok_str(sym
->v
, NULL
),
358 sym_bind
== STB_WEAK
? 'w' : sym_bind
== STB_LOCAL
? 'l' : 'g',
366 /* ------------------------------------------------------------------------- */
367 /* update sym->c so that it points to an external symbol in section
368 'section' with value 'value' */
370 ST_FUNC
void put_extern_sym2(Sym
*sym
, int sh_num
,
371 addr_t value
, unsigned long size
,
372 int can_add_underscore
)
374 int sym_type
, sym_bind
, info
, other
, t
;
378 #ifdef CONFIG_TCC_BCHECK
383 name
= get_tok_str(sym
->v
, NULL
);
384 #ifdef CONFIG_TCC_BCHECK
385 if (tcc_state
->do_bounds_check
) {
386 /* XXX: avoid doing that for statics ? */
387 /* if bound checking is activated, we change some function
388 names by adding the "__bound" prefix */
391 /* XXX: we rely only on malloc hooks */
404 strcpy(buf
, "__bound_");
412 if ((t
& VT_BTYPE
) == VT_FUNC
) {
414 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
415 sym_type
= STT_NOTYPE
;
417 sym_type
= STT_OBJECT
;
420 sym_bind
= STB_LOCAL
;
422 sym_bind
= STB_GLOBAL
;
425 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
426 Sym
*ref
= sym
->type
.ref
;
427 if (ref
->a
.nodecorate
) {
428 can_add_underscore
= 0;
430 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
431 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
433 other
|= ST_PE_STDCALL
;
434 can_add_underscore
= 0;
438 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
440 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
444 name
= get_tok_str(sym
->asm_label
, NULL
);
445 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
446 sym
->c
= put_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
449 esym
->st_value
= value
;
450 esym
->st_size
= size
;
451 esym
->st_shndx
= sh_num
;
456 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
457 addr_t value
, unsigned long size
)
459 int sh_num
= section
? section
->sh_num
: SHN_UNDEF
;
460 put_extern_sym2(sym
, sh_num
, value
, size
, 1);
463 /* add a new relocation entry to symbol 'sym' in section 's' */
464 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
469 if (nocode_wanted
&& s
== cur_text_section
)
474 put_extern_sym(sym
, NULL
, 0, 0);
478 /* now we can add ELF relocation info */
479 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
483 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
485 greloca(s
, sym
, offset
, type
, 0);
489 /* ------------------------------------------------------------------------- */
490 /* symbol allocator */
491 static Sym
*__sym_malloc(void)
493 Sym
*sym_pool
, *sym
, *last_sym
;
496 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
497 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
499 last_sym
= sym_free_first
;
501 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
502 sym
->next
= last_sym
;
506 sym_free_first
= last_sym
;
510 static inline Sym
*sym_malloc(void)
514 sym
= sym_free_first
;
516 sym
= __sym_malloc();
517 sym_free_first
= sym
->next
;
520 sym
= tcc_malloc(sizeof(Sym
));
525 ST_INLN
void sym_free(Sym
*sym
)
528 sym
->next
= sym_free_first
;
529 sym_free_first
= sym
;
535 /* push, without hashing */
536 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
541 memset(s
, 0, sizeof *s
);
551 /* find a symbol and return its associated structure. 's' is the top
552 of the symbol stack */
553 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
565 /* structure lookup */
566 ST_INLN Sym
*struct_find(int v
)
569 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
571 return table_ident
[v
]->sym_struct
;
574 /* find an identifier */
575 ST_INLN Sym
*sym_find(int v
)
578 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
580 return table_ident
[v
]->sym_identifier
;
583 /* push a given symbol on the symbol stack */
584 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
593 s
= sym_push2(ps
, v
, type
->t
, c
);
594 s
->type
.ref
= type
->ref
;
596 /* don't record fields or anonymous symbols */
598 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
599 /* record symbol in token array */
600 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
602 ps
= &ts
->sym_struct
;
604 ps
= &ts
->sym_identifier
;
607 s
->sym_scope
= local_scope
;
608 if (s
->prev_tok
&& s
->prev_tok
->sym_scope
== s
->sym_scope
)
609 tcc_error("redeclaration of '%s'",
610 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
615 /* push a global identifier */
616 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
619 s
= sym_push2(&global_stack
, v
, t
, c
);
620 /* 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
624 sym_identifier will point to 's' when popped */
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
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
851 sym
->type
.ref
= type
->ref
;
852 sym
->r
= VT_CONST
| VT_SYM
;
853 put_extern_sym(sym
, sec
, offset
, size
);
857 /* push a reference to a section offset by adding a dummy symbol */
858 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
860 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
863 /* define a new external reference to a symbol 'v' of type 'u' */
864 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
870 /* push forward reference */
871 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
872 s
->type
.ref
= type
->ref
;
873 s
->r
= r
| VT_CONST
| VT_SYM
;
874 } else if (IS_ASM_SYM(s
)) {
875 s
->type
.t
= type
->t
| (s
->type
.t
& VT_EXTERN
);
876 s
->type
.ref
= type
->ref
;
882 /* Merge symbol attributes. */
883 static void merge_symattr(struct SymAttr
*sa
, struct SymAttr
*sa1
)
885 if (sa1
->aligned
&& !sa
->aligned
)
886 sa
->aligned
= sa1
->aligned
;
887 sa
->packed
|= sa1
->packed
;
888 sa
->weak
|= sa1
->weak
;
889 if (sa1
->visibility
!= STV_DEFAULT
) {
890 int vis
= sa
->visibility
;
891 if (vis
== STV_DEFAULT
892 || vis
> sa1
->visibility
)
893 vis
= sa1
->visibility
;
894 sa
->visibility
= vis
;
896 sa
->dllexport
|= sa1
->dllexport
;
897 sa
->nodecorate
|= sa1
->nodecorate
;
898 sa
->dllimport
|= sa1
->dllimport
;
901 /* Merge function attributes. */
902 static void merge_funcattr(struct FuncAttr
*fa
, struct FuncAttr
*fa1
)
904 if (fa1
->func_call
&& !fa
->func_call
)
905 fa
->func_call
= fa1
->func_call
;
906 if (fa1
->func_type
&& !fa
->func_type
)
907 fa
->func_type
= fa1
->func_type
;
908 if (fa1
->func_args
&& !fa
->func_args
)
909 fa
->func_args
= fa1
->func_args
;
912 /* Merge attributes. */
913 static void merge_attr(AttributeDef
*ad
, AttributeDef
*ad1
)
915 merge_symattr(&ad
->a
, &ad1
->a
);
916 merge_funcattr(&ad
->f
, &ad1
->f
);
919 ad
->section
= ad1
->section
;
920 if (ad1
->alias_target
)
921 ad
->alias_target
= ad1
->alias_target
;
923 ad
->asm_label
= ad1
->asm_label
;
925 ad
->attr_mode
= ad1
->attr_mode
;
928 /* Merge some type attributes. */
929 static void patch_type(Sym
*sym
, CType
*type
)
931 if (!(type
->t
& VT_EXTERN
)) {
932 if (!(sym
->type
.t
& VT_EXTERN
))
933 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
934 sym
->type
.t
&= ~VT_EXTERN
;
937 if (IS_ASM_SYM(sym
)) {
938 /* stay static if both are static */
939 sym
->type
.t
= type
->t
& (sym
->type
.t
| ~VT_STATIC
);
940 sym
->type
.ref
= type
->ref
;
943 if (!is_compatible_types(&sym
->type
, type
)) {
944 tcc_error("incompatible types for redefinition of '%s'",
945 get_tok_str(sym
->v
, NULL
));
947 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
948 int static_proto
= sym
->type
.t
& VT_STATIC
;
949 /* warn if static follows non-static function declaration */
950 if ((type
->t
& VT_STATIC
) && !static_proto
&& !(type
->t
& VT_INLINE
))
951 tcc_warning("static storage ignored for redefinition of '%s'",
952 get_tok_str(sym
->v
, NULL
));
954 if (0 == (type
->t
& VT_EXTERN
)) {
955 /* put complete type, use static from prototype */
956 sym
->type
.t
= (type
->t
& ~VT_STATIC
) | static_proto
;
957 if (type
->t
& VT_INLINE
)
958 sym
->type
.t
= type
->t
;
959 sym
->type
.ref
= type
->ref
;
963 if ((sym
->type
.t
& VT_ARRAY
) && type
->ref
->c
>= 0) {
964 /* set array size if it was omitted in extern declaration */
965 if (sym
->type
.ref
->c
< 0)
966 sym
->type
.ref
->c
= type
->ref
->c
;
967 else if (sym
->type
.ref
->c
!= type
->ref
->c
)
968 tcc_error("conflicting type for '%s'", get_tok_str(sym
->v
, NULL
));
970 if ((type
->t
^ sym
->type
.t
) & VT_STATIC
)
971 tcc_warning("storage mismatch for redefinition of '%s'",
972 get_tok_str(sym
->v
, NULL
));
977 /* Merge some storage attributes. */
978 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
981 patch_type(sym
, type
);
984 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
985 tcc_error("incompatible dll linkage for redefinition of '%s'",
986 get_tok_str(sym
->v
, NULL
));
988 merge_symattr(&sym
->a
, &ad
->a
);
990 sym
->asm_label
= ad
->asm_label
;
994 /* define a new external reference to a symbol 'v' */
995 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
1000 /* push forward reference */
1001 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
1002 s
->type
.t
|= VT_EXTERN
;
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
, 0));
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
== -1) {
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
, 0);
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
)
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
);
3607 static void struct_add_offset (Sym
*s
, int offset
)
3609 while ((s
= s
->next
) != NULL
) {
3610 if ((s
->v
& SYM_FIELD
) &&
3611 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3612 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3613 struct_add_offset(s
->type
.ref
, offset
);
3619 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3621 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3622 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3623 int pcc
= !tcc_state
->ms_bitfields
;
3624 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3631 prevbt
= VT_STRUCT
; /* make it never match */
3636 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3637 if (f
->type
.t
& VT_BITFIELD
)
3638 bit_size
= BIT_SIZE(f
->type
.t
);
3641 size
= type_size(&f
->type
, &align
);
3642 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3645 if (pcc
&& bit_size
== 0) {
3646 /* in pcc mode, packing does not affect zero-width bitfields */
3649 /* in pcc mode, attribute packed overrides if set. */
3650 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3653 /* pragma pack overrides align if lesser and packs bitfields always */
3656 if (pragma_pack
< align
)
3657 align
= pragma_pack
;
3658 /* in pcc mode pragma pack also overrides individual align */
3659 if (pcc
&& pragma_pack
< a
)
3663 /* some individual align was specified */
3667 if (type
->ref
->type
.t
== VT_UNION
) {
3668 if (pcc
&& bit_size
>= 0)
3669 size
= (bit_size
+ 7) >> 3;
3674 } else if (bit_size
< 0) {
3676 c
+= (bit_pos
+ 7) >> 3;
3677 c
= (c
+ align
- 1) & -align
;
3686 /* A bit-field. Layout is more complicated. There are two
3687 options: PCC (GCC) compatible and MS compatible */
3689 /* In PCC layout a bit-field is placed adjacent to the
3690 preceding bit-fields, except if:
3692 - an individual alignment was given
3693 - it would overflow its base type container and
3694 there is no packing */
3695 if (bit_size
== 0) {
3697 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3699 } else if (f
->a
.aligned
) {
3701 } else if (!packed
) {
3703 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3704 if (ofs
> size
/ align
)
3708 /* in pcc mode, long long bitfields have type int if they fit */
3709 if (size
== 8 && bit_size
<= 32)
3710 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3712 while (bit_pos
>= align
* 8)
3713 c
+= align
, bit_pos
-= align
* 8;
3716 /* In PCC layout named bit-fields influence the alignment
3717 of the containing struct using the base types alignment,
3718 except for packed fields (which here have correct align). */
3719 if (f
->v
& SYM_FIRST_ANOM
3720 // && bit_size // ??? gcc on ARM/rpi does that
3725 bt
= f
->type
.t
& VT_BTYPE
;
3726 if ((bit_pos
+ bit_size
> size
* 8)
3727 || (bit_size
> 0) == (bt
!= prevbt
)
3729 c
= (c
+ align
- 1) & -align
;
3732 /* In MS bitfield mode a bit-field run always uses
3733 at least as many bits as the underlying type.
3734 To start a new run it's also required that this
3735 or the last bit-field had non-zero width. */
3736 if (bit_size
|| prev_bit_size
)
3739 /* In MS layout the records alignment is normally
3740 influenced by the field, except for a zero-width
3741 field at the start of a run (but by further zero-width
3742 fields it is again). */
3743 if (bit_size
== 0 && prevbt
!= bt
)
3746 prev_bit_size
= bit_size
;
3749 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3750 | (bit_pos
<< VT_STRUCT_SHIFT
);
3751 bit_pos
+= bit_size
;
3753 if (align
> maxalign
)
3757 printf("set field %s offset %-2d size %-2d align %-2d",
3758 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3759 if (f
->type
.t
& VT_BITFIELD
) {
3760 printf(" pos %-2d bits %-2d",
3768 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3770 /* An anonymous struct/union. Adjust member offsets
3771 to reflect the real offset of our containing struct.
3772 Also set the offset of this anon member inside
3773 the outer struct to be zero. Via this it
3774 works when accessing the field offset directly
3775 (from base object), as well as when recursing
3776 members in initializer handling. */
3777 int v2
= f
->type
.ref
->v
;
3778 if (!(v2
& SYM_FIELD
) &&
3779 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3781 /* This happens only with MS extensions. The
3782 anon member has a named struct type, so it
3783 potentially is shared with other references.
3784 We need to unshare members so we can modify
3787 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3788 &f
->type
.ref
->type
, 0,
3790 pps
= &f
->type
.ref
->next
;
3791 while ((ass
= ass
->next
) != NULL
) {
3792 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3793 pps
= &((*pps
)->next
);
3797 struct_add_offset(f
->type
.ref
, offset
);
3807 c
+= (bit_pos
+ 7) >> 3;
3809 /* store size and alignment */
3810 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3814 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
3815 /* can happen if individual align for some member was given. In
3816 this case MSVC ignores maxalign when aligning the size */
3821 c
= (c
+ a
- 1) & -a
;
3825 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3828 /* check whether we can access bitfields by their type */
3829 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3833 if (0 == (f
->type
.t
& VT_BITFIELD
))
3837 bit_size
= BIT_SIZE(f
->type
.t
);
3840 bit_pos
= BIT_POS(f
->type
.t
);
3841 size
= type_size(&f
->type
, &align
);
3842 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3845 /* try to access the field using a different type */
3846 c0
= -1, s
= align
= 1;
3848 px
= f
->c
* 8 + bit_pos
;
3849 cx
= (px
>> 3) & -align
;
3850 px
= px
- (cx
<< 3);
3853 s
= (px
+ bit_size
+ 7) >> 3;
3863 s
= type_size(&t
, &align
);
3867 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3868 /* update offset and bit position */
3871 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3872 | (bit_pos
<< VT_STRUCT_SHIFT
);
3876 printf("FIX field %s offset %-2d size %-2d align %-2d "
3877 "pos %-2d bits %-2d\n",
3878 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3879 cx
, s
, align
, px
, bit_size
);
3882 /* fall back to load/store single-byte wise */
3883 f
->auxtype
= VT_STRUCT
;
3885 printf("FIX field %s : load byte-wise\n",
3886 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3892 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3893 static void struct_decl(CType
*type
, int u
)
3895 int v
, c
, size
, align
, flexible
;
3896 int bit_size
, bsize
, bt
;
3898 AttributeDef ad
, ad1
;
3901 memset(&ad
, 0, sizeof ad
);
3903 parse_attribute(&ad
);
3907 /* struct already defined ? return it */
3909 expect("struct/union/enum name");
3911 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3914 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3916 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3921 /* Record the original enum/struct/union token. */
3922 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3924 /* we put an undefined size for struct/union */
3925 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3926 s
->r
= 0; /* default alignment is zero as gcc */
3928 type
->t
= s
->type
.t
;
3934 tcc_error("struct/union/enum already defined");
3935 /* cannot be empty */
3936 /* non empty enums are not allowed */
3939 long long ll
= 0, pl
= 0, nl
= 0;
3942 /* enum symbols have static storage */
3943 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3947 expect("identifier");
3949 if (ss
&& !local_stack
)
3950 tcc_error("redefinition of enumerator '%s'",
3951 get_tok_str(v
, NULL
));
3955 ll
= expr_const64();
3957 ss
= sym_push(v
, &t
, VT_CONST
, 0);
3959 *ps
= ss
, ps
= &ss
->next
;
3968 /* NOTE: we accept a trailing comma */
3973 /* set integral type of the enum */
3976 if (pl
!= (unsigned)pl
)
3977 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3979 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
3980 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3981 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
3983 /* set type for enum members */
3984 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
3986 if (ll
== (int)ll
) /* default is int if it fits */
3988 if (t
.t
& VT_UNSIGNED
) {
3989 ss
->type
.t
|= VT_UNSIGNED
;
3990 if (ll
== (unsigned)ll
)
3993 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
)
3994 | (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3999 while (tok
!= '}') {
4000 if (!parse_btype(&btype
, &ad1
)) {
4006 tcc_error("flexible array member '%s' not at the end of struct",
4007 get_tok_str(v
, NULL
));
4013 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
4015 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
4016 expect("identifier");
4018 int v
= btype
.ref
->v
;
4019 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
4020 if (tcc_state
->ms_extensions
== 0)
4021 expect("identifier");
4025 if (type_size(&type1
, &align
) < 0) {
4026 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
4029 tcc_error("field '%s' has incomplete type",
4030 get_tok_str(v
, NULL
));
4032 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
4033 (type1
.t
& VT_BTYPE
) == VT_VOID
||
4034 (type1
.t
& VT_STORAGE
))
4035 tcc_error("invalid type for '%s'",
4036 get_tok_str(v
, NULL
));
4040 bit_size
= expr_const();
4041 /* XXX: handle v = 0 case for messages */
4043 tcc_error("negative width in bit-field '%s'",
4044 get_tok_str(v
, NULL
));
4045 if (v
&& bit_size
== 0)
4046 tcc_error("zero width for bit-field '%s'",
4047 get_tok_str(v
, NULL
));
4048 parse_attribute(&ad1
);
4050 size
= type_size(&type1
, &align
);
4051 if (bit_size
>= 0) {
4052 bt
= type1
.t
& VT_BTYPE
;
4058 tcc_error("bitfields must have scalar type");
4060 if (bit_size
> bsize
) {
4061 tcc_error("width of '%s' exceeds its type",
4062 get_tok_str(v
, NULL
));
4063 } else if (bit_size
== bsize
4064 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
4065 /* no need for bit fields */
4067 } else if (bit_size
== 64) {
4068 tcc_error("field width 64 not implemented");
4070 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
4072 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4075 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
4076 /* Remember we've seen a real field to check
4077 for placement of flexible array member. */
4080 /* If member is a struct or bit-field, enforce
4081 placing into the struct (as anonymous). */
4083 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
4088 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
4093 if (tok
== ';' || tok
== TOK_EOF
)
4100 parse_attribute(&ad
);
4101 struct_layout(type
, &ad
);
4106 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
4108 merge_symattr(&ad
->a
, &s
->a
);
4109 merge_funcattr(&ad
->f
, &s
->f
);
4112 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4113 are added to the element type, copied because it could be a typedef. */
4114 static void parse_btype_qualify(CType
*type
, int qualifiers
)
4116 while (type
->t
& VT_ARRAY
) {
4117 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
4118 type
= &type
->ref
->type
;
4120 type
->t
|= qualifiers
;
4123 /* return 0 if no type declaration. otherwise, return the basic type
4126 static int parse_btype(CType
*type
, AttributeDef
*ad
)
4128 int t
, u
, bt
, st
, type_found
, typespec_found
, g
;
4132 memset(ad
, 0, sizeof(AttributeDef
));
4142 /* currently, we really ignore extension */
4152 if (u
== VT_SHORT
|| u
== VT_LONG
) {
4153 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
4154 tmbt
: tcc_error("too many basic types");
4157 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
4162 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4175 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4176 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4177 } else if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4178 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LLONG
;
4185 #ifdef TCC_TARGET_ARM64
4187 /* GCC's __uint128_t appears in some Linux header files. Make it a
4188 synonym for long double to get the size and alignment right. */
4199 if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4200 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4208 struct_decl(&type1
, VT_ENUM
);
4211 type
->ref
= type1
.ref
;
4214 struct_decl(&type1
, VT_STRUCT
);
4217 struct_decl(&type1
, VT_UNION
);
4220 /* type modifiers */
4225 parse_btype_qualify(type
, VT_CONSTANT
);
4233 parse_btype_qualify(type
, VT_VOLATILE
);
4240 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4241 tcc_error("signed and unsigned modifier");
4254 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4255 tcc_error("signed and unsigned modifier");
4256 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4272 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4273 tcc_error("multiple storage classes");
4284 /* GNUC attribute */
4285 case TOK_ATTRIBUTE1
:
4286 case TOK_ATTRIBUTE2
:
4287 parse_attribute(ad
);
4288 if (ad
->attr_mode
) {
4289 u
= ad
->attr_mode
-1;
4290 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4298 parse_expr_type(&type1
);
4299 /* remove all storage modifiers except typedef */
4300 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4302 sym_to_attr(ad
, type1
.ref
);
4308 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4310 t
&= ~(VT_BTYPE
|VT_LONG
);
4311 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4312 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4313 type
->ref
= s
->type
.ref
;
4315 parse_btype_qualify(type
, t
);
4317 /* get attributes from typedef */
4327 if (tcc_state
->char_is_unsigned
) {
4328 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4331 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4332 bt
= t
& (VT_BTYPE
|VT_LONG
);
4334 t
|= LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
;
4335 #ifdef TCC_TARGET_PE
4336 if (bt
== VT_LDOUBLE
)
4337 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_DOUBLE
;
4343 /* convert a function parameter type (array to pointer and function to
4344 function pointer) */
4345 static inline void convert_parameter_type(CType
*pt
)
4347 /* remove const and volatile qualifiers (XXX: const could be used
4348 to indicate a const function parameter */
4349 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4350 /* array must be transformed to pointer according to ANSI C */
4352 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4357 ST_FUNC
void parse_asm_str(CString
*astr
)
4360 parse_mult_str(astr
, "string constant");
4363 /* Parse an asm label and return the token */
4364 static int asm_label_instr(void)
4370 parse_asm_str(&astr
);
4373 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4375 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4380 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4382 int n
, l
, t1
, arg_size
, align
;
4383 Sym
**plast
, *s
, *first
;
4388 /* function type, or recursive declarator (return if so) */
4390 if (td
&& !(td
& TYPE_ABSTRACT
))
4394 else if (parse_btype(&pt
, &ad1
))
4397 merge_attr (ad
, &ad1
);
4406 /* read param name and compute offset */
4407 if (l
!= FUNC_OLD
) {
4408 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4410 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4411 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4412 tcc_error("parameter declared as void");
4416 expect("identifier");
4417 pt
.t
= VT_VOID
; /* invalid type */
4420 convert_parameter_type(&pt
);
4421 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4422 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4428 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4433 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4434 tcc_error("invalid type");
4437 /* if no parameters, then old type prototype */
4440 /* NOTE: const is ignored in returned type as it has a special
4441 meaning in gcc / C++ */
4442 type
->t
&= ~VT_CONSTANT
;
4443 /* some ancient pre-K&R C allows a function to return an array
4444 and the array brackets to be put after the arguments, such
4445 that "int c()[]" means something like "int[] c()" */
4448 skip(']'); /* only handle simple "[]" */
4451 /* we push a anonymous symbol which will contain the function prototype */
4452 ad
->f
.func_args
= arg_size
;
4453 ad
->f
.func_type
= l
;
4454 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4460 } else if (tok
== '[') {
4461 int saved_nocode_wanted
= nocode_wanted
;
4462 /* array definition */
4465 /* XXX The optional type-quals and static should only be accepted
4466 in parameter decls. The '*' as well, and then even only
4467 in prototypes (not function defs). */
4469 case TOK_RESTRICT1
: case TOK_RESTRICT2
: case TOK_RESTRICT3
:
4484 if (!local_stack
|| (storage
& VT_STATIC
))
4485 vpushi(expr_const());
4487 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4488 length must always be evaluated, even under nocode_wanted,
4489 so that its size slot is initialized (e.g. under sizeof
4494 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4497 tcc_error("invalid array size");
4499 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4500 tcc_error("size of variable length array should be an integer");
4505 /* parse next post type */
4506 post_type(type
, ad
, storage
, 0);
4507 if (type
->t
== VT_FUNC
)
4508 tcc_error("declaration of an array of functions");
4509 t1
|= type
->t
& VT_VLA
;
4512 loc
-= type_size(&int_type
, &align
);
4516 vla_runtime_type_size(type
, &align
);
4518 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4524 nocode_wanted
= saved_nocode_wanted
;
4526 /* we push an anonymous symbol which will contain the array
4528 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4529 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4535 /* Parse a type declarator (except basic type), and return the type
4536 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4537 expected. 'type' should contain the basic type. 'ad' is the
4538 attribute definition of the basic type. It can be modified by
4539 type_decl(). If this (possibly abstract) declarator is a pointer chain
4540 it returns the innermost pointed to type (equals *type, but is a different
4541 pointer), otherwise returns type itself, that's used for recursive calls. */
4542 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4545 int qualifiers
, storage
;
4547 /* recursive type, remove storage bits first, apply them later again */
4548 storage
= type
->t
& VT_STORAGE
;
4549 type
->t
&= ~VT_STORAGE
;
4552 while (tok
== '*') {
4560 qualifiers
|= VT_CONSTANT
;
4565 qualifiers
|= VT_VOLATILE
;
4571 /* XXX: clarify attribute handling */
4572 case TOK_ATTRIBUTE1
:
4573 case TOK_ATTRIBUTE2
:
4574 parse_attribute(ad
);
4578 type
->t
|= qualifiers
;
4580 /* innermost pointed to type is the one for the first derivation */
4581 ret
= pointed_type(type
);
4585 /* This is possibly a parameter type list for abstract declarators
4586 ('int ()'), use post_type for testing this. */
4587 if (!post_type(type
, ad
, 0, td
)) {
4588 /* It's not, so it's a nested declarator, and the post operations
4589 apply to the innermost pointed to type (if any). */
4590 /* XXX: this is not correct to modify 'ad' at this point, but
4591 the syntax is not clear */
4592 parse_attribute(ad
);
4593 post
= type_decl(type
, ad
, v
, td
);
4596 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4597 /* type identifier */
4601 if (!(td
& TYPE_ABSTRACT
))
4602 expect("identifier");
4605 post_type(post
, ad
, storage
, 0);
4606 parse_attribute(ad
);
4611 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4612 ST_FUNC
int lvalue_type(int t
)
4617 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4619 else if (bt
== VT_SHORT
)
4623 if (t
& VT_UNSIGNED
)
4624 r
|= VT_LVAL_UNSIGNED
;
4628 /* indirection with full error checking and bound check */
4629 ST_FUNC
void indir(void)
4631 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4632 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4636 if (vtop
->r
& VT_LVAL
)
4638 vtop
->type
= *pointed_type(&vtop
->type
);
4639 /* Arrays and functions are never lvalues */
4640 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4641 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4642 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4643 /* if bound checking, the referenced pointer must be checked */
4644 #ifdef CONFIG_TCC_BCHECK
4645 if (tcc_state
->do_bounds_check
)
4646 vtop
->r
|= VT_MUSTBOUND
;
4651 /* pass a parameter to a function and do type checking and casting */
4652 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4657 func_type
= func
->f
.func_type
;
4658 if (func_type
== FUNC_OLD
||
4659 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4660 /* default casting : only need to convert float to double */
4661 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4662 gen_cast_s(VT_DOUBLE
);
4663 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4664 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4665 type
.ref
= vtop
->type
.ref
;
4668 } else if (arg
== NULL
) {
4669 tcc_error("too many arguments to function");
4672 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4673 gen_assign_cast(&type
);
4677 /* parse an expression and return its type without any side effect. */
4678 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4687 /* parse an expression of the form '(type)' or '(expr)' and return its
4689 static void parse_expr_type(CType
*type
)
4695 if (parse_btype(type
, &ad
)) {
4696 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4698 expr_type(type
, gexpr
);
4703 static void parse_type(CType
*type
)
4708 if (!parse_btype(type
, &ad
)) {
4711 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4714 static void parse_builtin_params(int nc
, const char *args
)
4721 while ((c
= *args
++)) {
4725 case 'e': expr_eq(); continue;
4726 case 't': parse_type(&t
); vpush(&t
); continue;
4727 default: tcc_error("internal error"); break;
4735 static void try_call_scope_cleanup(Sym
*stop
)
4737 Sym
*cls
= current_cleanups
;
4739 for (; cls
!= stop
; cls
= cls
->ncl
) {
4740 Sym
*fs
= cls
->next
;
4741 Sym
*vs
= cls
->prev_tok
;
4743 vpushsym(&fs
->type
, fs
);
4744 vset(&vs
->type
, vs
->r
, vs
->c
);
4746 mk_pointer(&vtop
->type
);
4752 static void try_call_cleanup_goto(Sym
*cleanupstate
)
4757 if (!current_cleanups
)
4760 /* search NCA of both cleanup chains given parents and initial depth */
4761 ocd
= cleanupstate
? cleanupstate
->v
& ~SYM_FIELD
: 0;
4762 for (ccd
= ncleanups
, oc
= cleanupstate
; ocd
> ccd
; --ocd
, oc
= oc
->ncl
)
4764 for (cc
= current_cleanups
; ccd
> ocd
; --ccd
, cc
= cc
->ncl
)
4766 for (; cc
!= oc
; cc
= cc
->ncl
, oc
= oc
->ncl
, --ccd
)
4769 try_call_scope_cleanup(cc
);
4772 ST_FUNC
void unary(void)
4774 int n
, t
, align
, size
, r
, sizeof_caller
;
4779 sizeof_caller
= in_sizeof
;
4782 /* XXX: GCC 2.95.3 does not generate a table although it should be
4790 #ifdef TCC_TARGET_PE
4791 t
= VT_SHORT
|VT_UNSIGNED
;
4799 vsetc(&type
, VT_CONST
, &tokc
);
4803 t
= VT_INT
| VT_UNSIGNED
;
4809 t
= VT_LLONG
| VT_UNSIGNED
;
4821 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
;
4824 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
| VT_UNSIGNED
;
4826 case TOK___FUNCTION__
:
4828 goto tok_identifier
;
4834 /* special function name identifier */
4835 len
= strlen(funcname
) + 1;
4836 /* generate char[len] type */
4841 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4842 if (!NODATA_WANTED
) {
4843 ptr
= section_ptr_add(data_section
, len
);
4844 memcpy(ptr
, funcname
, len
);
4850 #ifdef TCC_TARGET_PE
4851 t
= VT_SHORT
| VT_UNSIGNED
;
4857 /* string parsing */
4859 if (tcc_state
->char_is_unsigned
)
4860 t
= VT_BYTE
| VT_UNSIGNED
;
4862 if (tcc_state
->warn_write_strings
)
4867 memset(&ad
, 0, sizeof(AttributeDef
));
4868 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4873 if (parse_btype(&type
, &ad
)) {
4874 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4876 /* check ISOC99 compound literal */
4878 /* data is allocated locally by default */
4883 /* all except arrays are lvalues */
4884 if (!(type
.t
& VT_ARRAY
))
4885 r
|= lvalue_type(type
.t
);
4886 memset(&ad
, 0, sizeof(AttributeDef
));
4887 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4889 if (sizeof_caller
) {
4896 } else if (tok
== '{') {
4897 int saved_nocode_wanted
= nocode_wanted
;
4899 tcc_error("expected constant");
4900 /* save all registers */
4902 /* statement expression : we do not accept break/continue
4903 inside as GCC does. We do retain the nocode_wanted state,
4904 as statement expressions can't ever be entered from the
4905 outside, so any reactivation of code emission (from labels
4906 or loop heads) can be disabled again after the end of it. */
4907 block(NULL
, NULL
, 1);
4908 nocode_wanted
= saved_nocode_wanted
;
4923 /* functions names must be treated as function pointers,
4924 except for unary '&' and sizeof. Since we consider that
4925 functions are not lvalues, we only have to handle it
4926 there and in function calls. */
4927 /* arrays can also be used although they are not lvalues */
4928 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4929 !(vtop
->type
.t
& VT_ARRAY
))
4931 mk_pointer(&vtop
->type
);
4937 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4938 gen_cast_s(VT_BOOL
);
4939 vtop
->c
.i
= !vtop
->c
.i
;
4940 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4944 vseti(VT_JMP
, gvtst(1, 0));
4956 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4957 tcc_error("pointer not accepted for unary plus");
4958 /* In order to force cast, we add zero, except for floating point
4959 where we really need an noop (otherwise -0.0 will be transformed
4961 if (!is_float(vtop
->type
.t
)) {
4973 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
4974 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
4975 size
= type_size(&type
, &align
);
4976 if (s
&& s
->a
.aligned
)
4977 align
= 1 << (s
->a
.aligned
- 1);
4978 if (t
== TOK_SIZEOF
) {
4979 if (!(type
.t
& VT_VLA
)) {
4981 tcc_error("sizeof applied to an incomplete type");
4984 vla_runtime_type_size(&type
, &align
);
4989 vtop
->type
.t
|= VT_UNSIGNED
;
4992 case TOK_builtin_expect
:
4993 /* __builtin_expect is a no-op for now */
4994 parse_builtin_params(0, "ee");
4997 case TOK_builtin_types_compatible_p
:
4998 parse_builtin_params(0, "tt");
4999 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5000 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5001 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
5005 case TOK_builtin_choose_expr
:
5032 case TOK_builtin_constant_p
:
5033 parse_builtin_params(1, "e");
5034 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5038 case TOK_builtin_frame_address
:
5039 case TOK_builtin_return_address
:
5045 if (tok
!= TOK_CINT
) {
5046 tcc_error("%s only takes positive integers",
5047 tok1
== TOK_builtin_return_address
?
5048 "__builtin_return_address" :
5049 "__builtin_frame_address");
5051 level
= (uint32_t)tokc
.i
;
5056 vset(&type
, VT_LOCAL
, 0); /* local frame */
5058 mk_pointer(&vtop
->type
);
5059 indir(); /* -> parent frame */
5061 if (tok1
== TOK_builtin_return_address
) {
5062 // assume return address is just above frame pointer on stack
5065 mk_pointer(&vtop
->type
);
5070 #ifdef TCC_TARGET_X86_64
5071 #ifdef TCC_TARGET_PE
5072 case TOK_builtin_va_start
:
5073 parse_builtin_params(0, "ee");
5074 r
= vtop
->r
& VT_VALMASK
;
5078 tcc_error("__builtin_va_start expects a local variable");
5080 vtop
->type
= char_pointer_type
;
5085 case TOK_builtin_va_arg_types
:
5086 parse_builtin_params(0, "t");
5087 vpushi(classify_x86_64_va_arg(&vtop
->type
));
5094 #ifdef TCC_TARGET_ARM64
5095 case TOK___va_start
: {
5096 parse_builtin_params(0, "ee");
5100 vtop
->type
.t
= VT_VOID
;
5103 case TOK___va_arg
: {
5104 parse_builtin_params(0, "et");
5112 case TOK___arm64_clear_cache
: {
5113 parse_builtin_params(0, "ee");
5116 vtop
->type
.t
= VT_VOID
;
5120 /* pre operations */
5131 t
= vtop
->type
.t
& VT_BTYPE
;
5133 /* In IEEE negate(x) isn't subtract(0,x), but rather
5137 vtop
->c
.f
= -1.0 * 0.0;
5138 else if (t
== VT_DOUBLE
)
5139 vtop
->c
.d
= -1.0 * 0.0;
5141 vtop
->c
.ld
= -1.0 * 0.0;
5149 goto tok_identifier
;
5151 /* allow to take the address of a label */
5152 if (tok
< TOK_UIDENT
)
5153 expect("label identifier");
5154 s
= label_find(tok
);
5156 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5158 if (s
->r
== LABEL_DECLARED
)
5159 s
->r
= LABEL_FORWARD
;
5162 s
->type
.t
= VT_VOID
;
5163 mk_pointer(&s
->type
);
5164 s
->type
.t
|= VT_STATIC
;
5166 vpushsym(&s
->type
, s
);
5172 CType controlling_type
;
5173 int has_default
= 0;
5176 TokenString
*str
= NULL
;
5177 int saved_const_wanted
= const_wanted
;
5182 expr_type(&controlling_type
, expr_eq
);
5183 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
5184 if ((controlling_type
.t
& VT_BTYPE
) == VT_FUNC
)
5185 mk_pointer(&controlling_type
);
5186 const_wanted
= saved_const_wanted
;
5190 if (tok
== TOK_DEFAULT
) {
5192 tcc_error("too many 'default'");
5198 AttributeDef ad_tmp
;
5201 parse_btype(&cur_type
, &ad_tmp
);
5202 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
5203 if (compare_types(&controlling_type
, &cur_type
, 0)) {
5205 tcc_error("type match twice");
5215 skip_or_save_block(&str
);
5217 skip_or_save_block(NULL
);
5224 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
5225 tcc_error("type '%s' does not match any association", buf
);
5227 begin_macro(str
, 1);
5236 // special qnan , snan and infinity values
5241 vtop
->type
.t
= VT_FLOAT
;
5246 goto special_math_val
;
5249 goto special_math_val
;
5256 expect("identifier");
5258 if (!s
|| IS_ASM_SYM(s
)) {
5259 const char *name
= get_tok_str(t
, NULL
);
5261 tcc_error("'%s' undeclared", name
);
5262 /* for simple function calls, we tolerate undeclared
5263 external reference to int() function */
5264 if (tcc_state
->warn_implicit_function_declaration
5265 #ifdef TCC_TARGET_PE
5266 /* people must be warned about using undeclared WINAPI functions
5267 (which usually start with uppercase letter) */
5268 || (name
[0] >= 'A' && name
[0] <= 'Z')
5271 tcc_warning("implicit declaration of function '%s'", name
);
5272 s
= external_global_sym(t
, &func_old_type
, 0);
5276 /* A symbol that has a register is a local register variable,
5277 which starts out as VT_LOCAL value. */
5278 if ((r
& VT_VALMASK
) < VT_CONST
)
5279 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5281 vset(&s
->type
, r
, s
->c
);
5282 /* Point to s as backpointer (even without r&VT_SYM).
5283 Will be used by at least the x86 inline asm parser for
5289 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5290 vtop
->c
.i
= s
->enum_val
;
5295 /* post operations */
5297 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5300 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5303 if (tok
== TOK_ARROW
)
5305 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5308 /* expect pointer on structure */
5309 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5310 expect("struct or union");
5311 if (tok
== TOK_CDOUBLE
)
5312 expect("field name");
5314 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5315 expect("field name");
5316 s
= find_field(&vtop
->type
, tok
);
5318 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5319 /* add field offset to pointer */
5320 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5323 /* change type to field type, and set to lvalue */
5324 vtop
->type
= s
->type
;
5325 vtop
->type
.t
|= qualifiers
;
5326 /* an array is never an lvalue */
5327 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5328 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5329 #ifdef CONFIG_TCC_BCHECK
5330 /* if bound checking, the referenced pointer must be checked */
5331 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5332 vtop
->r
|= VT_MUSTBOUND
;
5336 } else if (tok
== '[') {
5342 } else if (tok
== '(') {
5345 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5348 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5349 /* pointer test (no array accepted) */
5350 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5351 vtop
->type
= *pointed_type(&vtop
->type
);
5352 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5356 expect("function pointer");
5359 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5361 /* get return type */
5364 sa
= s
->next
; /* first parameter */
5365 nb_args
= regsize
= 0;
5367 /* compute first implicit argument if a structure is returned */
5368 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5369 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5370 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5371 &ret_align
, ®size
);
5373 /* get some space for the returned structure */
5374 size
= type_size(&s
->type
, &align
);
5375 #ifdef TCC_TARGET_ARM64
5376 /* On arm64, a small struct is return in registers.
5377 It is much easier to write it to memory if we know
5378 that we are allowed to write some extra bytes, so
5379 round the allocated space up to a power of 2: */
5381 while (size
& (size
- 1))
5382 size
= (size
| (size
- 1)) + 1;
5384 loc
= (loc
- size
) & -align
;
5386 ret
.r
= VT_LOCAL
| VT_LVAL
;
5387 /* pass it as 'int' to avoid structure arg passing
5389 vseti(VT_LOCAL
, loc
);
5399 /* return in register */
5400 if (is_float(ret
.type
.t
)) {
5401 ret
.r
= reg_fret(ret
.type
.t
);
5402 #ifdef TCC_TARGET_X86_64
5403 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5407 #ifndef TCC_TARGET_ARM64
5408 #ifdef TCC_TARGET_X86_64
5409 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5411 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5422 gfunc_param_typed(s
, sa
);
5432 tcc_error("too few arguments to function");
5434 gfunc_call(nb_args
);
5437 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5438 vsetc(&ret
.type
, r
, &ret
.c
);
5439 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5442 /* handle packed struct return */
5443 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5446 size
= type_size(&s
->type
, &align
);
5447 /* We're writing whole regs often, make sure there's enough
5448 space. Assume register size is power of 2. */
5449 if (regsize
> align
)
5451 loc
= (loc
- size
) & -align
;
5455 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5459 if (--ret_nregs
== 0)
5463 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5471 ST_FUNC
void expr_prod(void)
5476 while (tok
== '*' || tok
== '/' || tok
== '%') {
5484 ST_FUNC
void expr_sum(void)
5489 while (tok
== '+' || tok
== '-') {
5497 static void expr_shift(void)
5502 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5510 static void expr_cmp(void)
5515 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5516 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5524 static void expr_cmpeq(void)
5529 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5537 static void expr_and(void)
5540 while (tok
== '&') {
5547 static void expr_xor(void)
5550 while (tok
== '^') {
5557 static void expr_or(void)
5560 while (tok
== '|') {
5567 static void expr_land(void)
5570 if (tok
== TOK_LAND
) {
5573 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5574 gen_cast_s(VT_BOOL
);
5579 while (tok
== TOK_LAND
) {
5595 if (tok
!= TOK_LAND
) {
5608 static void expr_lor(void)
5611 if (tok
== TOK_LOR
) {
5614 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5615 gen_cast_s(VT_BOOL
);
5620 while (tok
== TOK_LOR
) {
5636 if (tok
!= TOK_LOR
) {
5649 /* Assuming vtop is a value used in a conditional context
5650 (i.e. compared with zero) return 0 if it's false, 1 if
5651 true and -1 if it can't be statically determined. */
5652 static int condition_3way(void)
5655 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5656 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5658 gen_cast_s(VT_BOOL
);
5665 static void expr_cond(void)
5667 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5669 CType type
, type1
, type2
;
5674 c
= condition_3way();
5675 g
= (tok
== ':' && gnu_ext
);
5677 /* needed to avoid having different registers saved in
5679 if (is_float(vtop
->type
.t
)) {
5681 #ifdef TCC_TARGET_X86_64
5682 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5706 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5707 mk_pointer(&vtop
->type
);
5709 sv
= *vtop
; /* save value to handle it later */
5710 vtop
--; /* no vpop so that FP stack is not flushed */
5726 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5727 mk_pointer(&vtop
->type
);
5730 bt1
= t1
& VT_BTYPE
;
5732 bt2
= t2
& VT_BTYPE
;
5736 /* cast operands to correct type according to ISOC rules */
5737 if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5738 type
.t
= VT_VOID
; /* NOTE: as an extension, we accept void on only one side */
5739 } else if (is_float(bt1
) || is_float(bt2
)) {
5740 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5741 type
.t
= VT_LDOUBLE
;
5743 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5748 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5749 /* cast to biggest op */
5750 type
.t
= VT_LLONG
| VT_LONG
;
5751 if (bt1
== VT_LLONG
)
5753 if (bt2
== VT_LLONG
)
5755 /* convert to unsigned if it does not fit in a long long */
5756 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5757 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5758 type
.t
|= VT_UNSIGNED
;
5759 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5760 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
5761 /* If one is a null ptr constant the result type
5763 if (is_null_pointer (vtop
)) type
= type1
;
5764 else if (is_null_pointer (&sv
)) type
= type2
;
5765 else if (bt1
!= bt2
)
5766 tcc_error("incompatible types in conditional expressions");
5768 CType
*pt1
= pointed_type(&type1
);
5769 CType
*pt2
= pointed_type(&type2
);
5770 int pbt1
= pt1
->t
& VT_BTYPE
;
5771 int pbt2
= pt2
->t
& VT_BTYPE
;
5772 int newquals
, copied
= 0;
5773 /* pointers to void get preferred, otherwise the
5774 pointed to types minus qualifs should be compatible */
5775 type
= (pbt1
== VT_VOID
) ? type1
: type2
;
5776 if (pbt1
!= VT_VOID
&& pbt2
!= VT_VOID
) {
5777 if(!compare_types(pt1
, pt2
, 1/*unqualif*/))
5778 tcc_warning("pointer type mismatch in conditional expression\n");
5780 /* combine qualifs */
5781 newquals
= ((pt1
->t
| pt2
->t
) & (VT_CONSTANT
| VT_VOLATILE
));
5782 if ((~pointed_type(&type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
5785 /* copy the pointer target symbol */
5786 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5789 pointed_type(&type
)->t
|= newquals
;
5791 /* pointers to incomplete arrays get converted to
5792 pointers to completed ones if possible */
5793 if (pt1
->t
& VT_ARRAY
5794 && pt2
->t
& VT_ARRAY
5795 && pointed_type(&type
)->ref
->c
< 0
5796 && (pt1
->ref
->c
> 0 || pt2
->ref
->c
> 0))
5799 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5801 pointed_type(&type
)->ref
=
5802 sym_push(SYM_FIELD
, &pointed_type(&type
)->ref
->type
,
5803 0, pointed_type(&type
)->ref
->c
);
5804 pointed_type(&type
)->ref
->c
=
5805 0 < pt1
->ref
->c
? pt1
->ref
->c
: pt2
->ref
->c
;
5808 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5809 /* XXX: test structure compatibility */
5810 type
= bt1
== VT_STRUCT
? type1
: type2
;
5812 /* integer operations */
5813 type
.t
= VT_INT
| (VT_LONG
& (t1
| t2
));
5814 /* convert to unsigned if it does not fit in an integer */
5815 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5816 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5817 type
.t
|= VT_UNSIGNED
;
5819 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5820 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5821 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5823 /* now we convert second operand */
5827 mk_pointer(&vtop
->type
);
5829 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5834 if (is_float(type
.t
)) {
5836 #ifdef TCC_TARGET_X86_64
5837 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5841 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5842 /* for long longs, we use fixed registers to avoid having
5843 to handle a complicated move */
5854 /* this is horrible, but we must also convert first
5860 mk_pointer(&vtop
->type
);
5862 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5866 if (c
< 0 || islv
) {
5868 move_reg(r2
, r1
, type
.t
);
5878 static void expr_eq(void)
5884 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5885 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5886 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5901 ST_FUNC
void gexpr(void)
5912 /* parse a constant expression and return value in vtop. */
5913 static void expr_const1(void)
5922 /* parse an integer constant and return its value. */
5923 static inline int64_t expr_const64(void)
5927 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5928 expect("constant expression");
5934 /* parse an integer constant and return its value.
5935 Complain if it doesn't fit 32bit (signed or unsigned). */
5936 ST_FUNC
int expr_const(void)
5939 int64_t wc
= expr_const64();
5941 if (c
!= wc
&& (unsigned)c
!= wc
)
5942 tcc_error("constant exceeds 32 bit");
5946 /* return the label token if current token is a label, otherwise
5948 static int is_label(void)
5952 /* fast test first */
5953 if (tok
< TOK_UIDENT
)
5955 /* no need to save tokc because tok is an identifier */
5961 unget_tok(last_tok
);
5966 #ifndef TCC_TARGET_ARM64
5967 static void gfunc_return(CType
*func_type
)
5969 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5970 CType type
, ret_type
;
5971 int ret_align
, ret_nregs
, regsize
;
5972 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5973 &ret_align
, ®size
);
5974 if (0 == ret_nregs
) {
5975 /* if returning structure, must copy it to implicit
5976 first pointer arg location */
5979 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5982 /* copy structure value to pointer */
5985 /* returning structure packed into registers */
5986 int r
, size
, addr
, align
;
5987 size
= type_size(func_type
,&align
);
5988 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5989 (vtop
->c
.i
& (ret_align
-1)))
5990 && (align
& (ret_align
-1))) {
5991 loc
= (loc
- size
) & -ret_align
;
5994 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5998 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
6000 vtop
->type
= ret_type
;
6001 if (is_float(ret_type
.t
))
6002 r
= rc_fret(ret_type
.t
);
6013 if (--ret_nregs
== 0)
6015 /* We assume that when a structure is returned in multiple
6016 registers, their classes are consecutive values of the
6019 vtop
->c
.i
+= regsize
;
6023 } else if (is_float(func_type
->t
)) {
6024 gv(rc_fret(func_type
->t
));
6028 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6032 static int case_cmp(const void *pa
, const void *pb
)
6034 int64_t a
= (*(struct case_t
**) pa
)->v1
;
6035 int64_t b
= (*(struct case_t
**) pb
)->v1
;
6036 return a
< b
? -1 : a
> b
;
6039 static void gcase(struct case_t
**base
, int len
, int *bsym
)
6043 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
6061 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
6063 gcase(base
, len
/2, bsym
);
6064 if (cur_switch
->def_sym
)
6065 gjmp_addr(cur_switch
->def_sym
);
6067 *bsym
= gjmp(*bsym
);
6071 base
+= e
; len
-= e
;
6081 if (p
->v1
== p
->v2
) {
6083 gtst_addr(0, p
->sym
);
6093 gtst_addr(0, p
->sym
);
6099 static void block(int *bsym
, int *csym
, int is_expr
)
6101 int a
, b
, c
, d
, cond
;
6104 /* generate line number info */
6105 if (tcc_state
->do_debug
)
6106 tcc_debug_line(tcc_state
);
6109 /* default return value is (void) */
6111 vtop
->type
.t
= VT_VOID
;
6114 if (tok
== TOK_IF
) {
6116 int saved_nocode_wanted
= nocode_wanted
;
6121 cond
= condition_3way();
6127 nocode_wanted
|= 0x20000000;
6128 block(bsym
, csym
, 0);
6130 nocode_wanted
= saved_nocode_wanted
;
6132 if (c
== TOK_ELSE
) {
6137 nocode_wanted
|= 0x20000000;
6138 block(bsym
, csym
, 0);
6139 gsym(d
); /* patch else jmp */
6141 nocode_wanted
= saved_nocode_wanted
;
6144 } else if (tok
== TOK_WHILE
) {
6145 int saved_nocode_wanted
;
6146 nocode_wanted
&= ~0x20000000;
6156 saved_nocode_wanted
= nocode_wanted
;
6158 nocode_wanted
= saved_nocode_wanted
;
6163 } else if (tok
== '{') {
6164 Sym
*llabel
, *lcleanup
;
6165 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
6166 int lncleanups
= ncleanups
;
6169 /* record local declaration stack position */
6171 llabel
= local_label_stack
;
6172 lcleanup
= current_cleanups
;
6175 /* handle local labels declarations */
6176 while (tok
== TOK_LABEL
) {
6179 if (tok
< TOK_UIDENT
)
6180 expect("label identifier");
6181 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
6191 while (tok
!= '}') {
6192 if ((a
= is_label()))
6199 block(bsym
, csym
, is_expr
);
6203 if (current_cleanups
!= lcleanup
) {
6207 for (pg
= &pending_gotos
; (g
= *pg
) && g
->c
> lncleanups
;)
6208 if (g
->prev_tok
->r
& LABEL_FORWARD
) {
6213 try_call_scope_cleanup(lcleanup
);
6214 pcl
->jnext
= gjmp(0);
6216 goto remove_pending
;
6225 if (!nocode_wanted
) {
6226 try_call_scope_cleanup(lcleanup
);
6230 current_cleanups
= lcleanup
;
6231 ncleanups
= lncleanups
;
6232 /* pop locally defined labels */
6233 label_pop(&local_label_stack
, llabel
, is_expr
);
6234 /* pop locally defined symbols */
6236 /* In the is_expr case (a statement expression is finished here),
6237 vtop might refer to symbols on the local_stack. Either via the
6238 type or via vtop->sym. We can't pop those nor any that in turn
6239 might be referred to. To make it easier we don't roll back
6240 any symbols in that case; some upper level call to block() will
6241 do that. We do have to remove such symbols from the lookup
6242 tables, though. sym_pop will do that. */
6243 sym_pop(&local_stack
, s
, is_expr
);
6245 /* Pop VLA frames and restore stack pointer if required */
6246 if (vlas_in_scope
> saved_vlas_in_scope
) {
6247 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
6250 vlas_in_scope
= saved_vlas_in_scope
;
6253 } else if (tok
== TOK_RETURN
) {
6257 gen_assign_cast(&func_vt
);
6258 try_call_scope_cleanup(NULL
);
6259 if ((func_vt
.t
& VT_BTYPE
) == VT_VOID
)
6262 gfunc_return(&func_vt
);
6264 try_call_scope_cleanup(NULL
);
6267 /* jump unless last stmt in top-level block */
6268 if (tok
!= '}' || local_scope
!= 1)
6270 nocode_wanted
|= 0x20000000;
6271 } else if (tok
== TOK_BREAK
) {
6274 tcc_error("cannot break");
6275 *bsym
= gjmp(*bsym
);
6278 nocode_wanted
|= 0x20000000;
6279 } else if (tok
== TOK_CONTINUE
) {
6282 tcc_error("cannot continue");
6283 vla_sp_restore_root();
6284 *csym
= gjmp(*csym
);
6287 } else if (tok
== TOK_FOR
) {
6289 int saved_nocode_wanted
;
6290 nocode_wanted
&= ~0x20000000;
6296 /* c99 for-loop init decl? */
6297 if (!decl0(VT_LOCAL
, 1, NULL
)) {
6298 /* no, regular for-loop init expr */
6324 saved_nocode_wanted
= nocode_wanted
;
6326 nocode_wanted
= saved_nocode_wanted
;
6331 sym_pop(&local_stack
, s
, 0);
6334 if (tok
== TOK_DO
) {
6335 int saved_nocode_wanted
;
6336 nocode_wanted
&= ~0x20000000;
6342 saved_nocode_wanted
= nocode_wanted
;
6348 nocode_wanted
= saved_nocode_wanted
;
6352 nocode_wanted
= saved_nocode_wanted
;
6357 if (tok
== TOK_SWITCH
) {
6358 struct switch_t
*saved
, sw
;
6359 int saved_nocode_wanted
= nocode_wanted
;
6365 switchval
= *vtop
--;
6367 b
= gjmp(0); /* jump to first case */
6368 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
6372 nocode_wanted
= saved_nocode_wanted
;
6373 a
= gjmp(a
); /* add implicit break */
6376 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6377 for (b
= 1; b
< sw
.n
; b
++)
6378 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6379 tcc_error("duplicate case value");
6380 /* Our switch table sorting is signed, so the compared
6381 value needs to be as well when it's 64bit. */
6382 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6383 switchval
.type
.t
&= ~VT_UNSIGNED
;
6385 gcase(sw
.p
, sw
.n
, &a
);
6388 gjmp_addr(sw
.def_sym
);
6389 dynarray_reset(&sw
.p
, &sw
.n
);
6394 if (tok
== TOK_CASE
) {
6395 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6398 nocode_wanted
&= ~0x20000000;
6400 cr
->v1
= cr
->v2
= expr_const64();
6401 if (gnu_ext
&& tok
== TOK_DOTS
) {
6403 cr
->v2
= expr_const64();
6404 if (cr
->v2
< cr
->v1
)
6405 tcc_warning("empty case range");
6408 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6411 goto block_after_label
;
6413 if (tok
== TOK_DEFAULT
) {
6418 if (cur_switch
->def_sym
)
6419 tcc_error("too many 'default'");
6420 cur_switch
->def_sym
= ind
;
6422 goto block_after_label
;
6424 if (tok
== TOK_GOTO
) {
6426 if (tok
== '*' && gnu_ext
) {
6430 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6433 } else if (tok
>= TOK_UIDENT
) {
6434 s
= label_find(tok
);
6435 /* put forward definition if needed */
6437 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6438 else if (s
->r
== LABEL_DECLARED
)
6439 s
->r
= LABEL_FORWARD
;
6441 vla_sp_restore_root();
6442 if (s
->r
& LABEL_FORWARD
) {
6443 /* start new goto chain for cleanups, linked via label->next */
6444 if (current_cleanups
) {
6445 sym_push2(&pending_gotos
, SYM_FIELD
, 0, ncleanups
);
6446 pending_gotos
->prev_tok
= s
;
6447 s
= sym_push2(&s
->next
, SYM_FIELD
, 0, 0);
6448 pending_gotos
->next
= s
;
6450 s
->jnext
= gjmp(s
->jnext
);
6452 try_call_cleanup_goto(s
->cleanupstate
);
6453 gjmp_addr(s
->jnext
);
6457 expect("label identifier");
6460 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
6469 if (s
->r
== LABEL_DEFINED
)
6470 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6471 s
->r
= LABEL_DEFINED
;
6473 Sym
*pcl
; /* pending cleanup goto */
6474 for (pcl
= s
->next
; pcl
; pcl
= pcl
->prev
)
6476 sym_pop(&s
->next
, NULL
, 0);
6480 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
6483 s
->cleanupstate
= current_cleanups
;
6485 /* we accept this, but it is a mistake */
6487 nocode_wanted
&= ~0x20000000;
6489 tcc_warning("deprecated use of label at end of compound statement");
6493 block(bsym
, csym
, is_expr
);
6496 /* expression case */
6511 /* This skips over a stream of tokens containing balanced {} and ()
6512 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6513 with a '{'). If STR then allocates and stores the skipped tokens
6514 in *STR. This doesn't check if () and {} are nested correctly,
6515 i.e. "({)}" is accepted. */
6516 static void skip_or_save_block(TokenString
**str
)
6518 int braces
= tok
== '{';
6521 *str
= tok_str_alloc();
6523 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6525 if (tok
== TOK_EOF
) {
6526 if (str
|| level
> 0)
6527 tcc_error("unexpected end of file");
6532 tok_str_add_tok(*str
);
6535 if (t
== '{' || t
== '(') {
6537 } else if (t
== '}' || t
== ')') {
6539 if (level
== 0 && braces
&& t
== '}')
6544 tok_str_add(*str
, -1);
6545 tok_str_add(*str
, 0);
6549 #define EXPR_CONST 1
6552 static void parse_init_elem(int expr_type
)
6554 int saved_global_expr
;
6557 /* compound literals must be allocated globally in this case */
6558 saved_global_expr
= global_expr
;
6561 global_expr
= saved_global_expr
;
6562 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6563 (compound literals). */
6564 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6565 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6566 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6567 #ifdef TCC_TARGET_PE
6568 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6571 tcc_error("initializer element is not constant");
6579 /* put zeros for variable based init */
6580 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6583 /* nothing to do because globals are already set to zero */
6585 vpush_global_sym(&func_old_type
, TOK_memset
);
6587 #ifdef TCC_TARGET_ARM
6598 /* t is the array or struct type. c is the array or struct
6599 address. cur_field is the pointer to the current
6600 field, for arrays the 'c' member contains the current start
6601 index. 'size_only' is true if only size info is needed (only used
6602 in arrays). al contains the already initialized length of the
6603 current container (starting at c). This returns the new length of that. */
6604 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6605 Sym
**cur_field
, int size_only
, int al
)
6608 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6609 unsigned long corig
= c
;
6613 if (gnu_ext
&& (l
= is_label()) != 0)
6615 /* NOTE: we only support ranges for last designator */
6616 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6618 if (!(type
->t
& VT_ARRAY
))
6619 expect("array type");
6621 index
= index_last
= expr_const();
6622 if (tok
== TOK_DOTS
&& gnu_ext
) {
6624 index_last
= expr_const();
6628 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6630 tcc_error("invalid index");
6632 (*cur_field
)->c
= index_last
;
6633 type
= pointed_type(type
);
6634 elem_size
= type_size(type
, &align
);
6635 c
+= index
* elem_size
;
6636 nb_elems
= index_last
- index
+ 1;
6642 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6643 expect("struct/union type");
6644 f
= find_field(type
, l
);
6657 } else if (!gnu_ext
) {
6661 if (type
->t
& VT_ARRAY
) {
6662 index
= (*cur_field
)->c
;
6663 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6664 tcc_error("index too large");
6665 type
= pointed_type(type
);
6666 c
+= index
* type_size(type
, &align
);
6669 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6670 *cur_field
= f
= f
->next
;
6672 tcc_error("too many field init");
6677 /* must put zero in holes (note that doing it that way
6678 ensures that it even works with designators) */
6679 if (!size_only
&& c
- corig
> al
)
6680 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6681 decl_initializer(type
, sec
, c
, 0, size_only
);
6683 /* XXX: make it more general */
6684 if (!size_only
&& nb_elems
> 1) {
6685 unsigned long c_end
;
6690 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6691 for (i
= 1; i
< nb_elems
; i
++) {
6692 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6697 } else if (!NODATA_WANTED
) {
6698 c_end
= c
+ nb_elems
* elem_size
;
6699 if (c_end
> sec
->data_allocated
)
6700 section_realloc(sec
, c_end
);
6701 src
= sec
->data
+ c
;
6703 for(i
= 1; i
< nb_elems
; i
++) {
6705 memcpy(dst
, src
, elem_size
);
6709 c
+= nb_elems
* type_size(type
, &align
);
6715 /* store a value or an expression directly in global data or in local array */
6716 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6723 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6727 /* XXX: not portable */
6728 /* XXX: generate error if incorrect relocation */
6729 gen_assign_cast(&dtype
);
6730 bt
= type
->t
& VT_BTYPE
;
6732 if ((vtop
->r
& VT_SYM
)
6735 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6736 || (type
->t
& VT_BITFIELD
))
6737 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6739 tcc_error("initializer element is not computable at load time");
6741 if (NODATA_WANTED
) {
6746 size
= type_size(type
, &align
);
6747 section_reserve(sec
, c
+ size
);
6748 ptr
= sec
->data
+ c
;
6750 /* XXX: make code faster ? */
6751 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6752 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6753 /* XXX This rejects compound literals like
6754 '(void *){ptr}'. The problem is that '&sym' is
6755 represented the same way, which would be ruled out
6756 by the SYM_FIRST_ANOM check above, but also '"string"'
6757 in 'char *p = "string"' is represented the same
6758 with the type being VT_PTR and the symbol being an
6759 anonymous one. That is, there's no difference in vtop
6760 between '(void *){x}' and '&(void *){x}'. Ignore
6761 pointer typed entities here. Hopefully no real code
6762 will every use compound literals with scalar type. */
6763 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6764 /* These come from compound literals, memcpy stuff over. */
6768 esym
= elfsym(vtop
->sym
);
6769 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6770 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6772 /* We need to copy over all memory contents, and that
6773 includes relocations. Use the fact that relocs are
6774 created it order, so look from the end of relocs
6775 until we hit one before the copied region. */
6776 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6777 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6778 while (num_relocs
--) {
6780 if (rel
->r_offset
>= esym
->st_value
+ size
)
6782 if (rel
->r_offset
< esym
->st_value
)
6784 /* Note: if the same fields are initialized multiple
6785 times (possible with designators) then we possibly
6786 add multiple relocations for the same offset here.
6787 That would lead to wrong code, the last reloc needs
6788 to win. We clean this up later after the whole
6789 initializer is parsed. */
6790 put_elf_reloca(symtab_section
, sec
,
6791 c
+ rel
->r_offset
- esym
->st_value
,
6792 ELFW(R_TYPE
)(rel
->r_info
),
6793 ELFW(R_SYM
)(rel
->r_info
),
6803 if (type
->t
& VT_BITFIELD
) {
6804 int bit_pos
, bit_size
, bits
, n
;
6805 unsigned char *p
, v
, m
;
6806 bit_pos
= BIT_POS(vtop
->type
.t
);
6807 bit_size
= BIT_SIZE(vtop
->type
.t
);
6808 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6809 bit_pos
&= 7, bits
= 0;
6814 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6815 m
= ((1 << n
) - 1) << bit_pos
;
6816 *p
= (*p
& ~m
) | (v
& m
);
6817 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6821 /* XXX: when cross-compiling we assume that each type has the
6822 same representation on host and target, which is likely to
6823 be wrong in the case of long double */
6825 vtop
->c
.i
= vtop
->c
.i
!= 0;
6827 *(char *)ptr
|= vtop
->c
.i
;
6830 *(short *)ptr
|= vtop
->c
.i
;
6833 *(float*)ptr
= vtop
->c
.f
;
6836 *(double *)ptr
= vtop
->c
.d
;
6839 #if defined TCC_IS_NATIVE_387
6840 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
6841 memcpy(ptr
, &vtop
->c
.ld
, 10);
6843 else if (sizeof (long double) == sizeof (double))
6844 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr
) : "m" (vtop
->c
.ld
));
6846 else if (vtop
->c
.ld
== 0.0)
6850 if (sizeof(long double) == LDOUBLE_SIZE
)
6851 *(long double*)ptr
= vtop
->c
.ld
;
6852 else if (sizeof(double) == LDOUBLE_SIZE
)
6853 *(double *)ptr
= (double)vtop
->c
.ld
;
6855 tcc_error("can't cross compile long double constants");
6859 *(long long *)ptr
|= vtop
->c
.i
;
6866 addr_t val
= vtop
->c
.i
;
6868 if (vtop
->r
& VT_SYM
)
6869 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6871 *(addr_t
*)ptr
|= val
;
6873 if (vtop
->r
& VT_SYM
)
6874 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6875 *(addr_t
*)ptr
|= val
;
6881 int val
= vtop
->c
.i
;
6883 if (vtop
->r
& VT_SYM
)
6884 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6888 if (vtop
->r
& VT_SYM
)
6889 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6898 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6905 /* 't' contains the type and storage info. 'c' is the offset of the
6906 object in section 'sec'. If 'sec' is NULL, it means stack based
6907 allocation. 'first' is true if array '{' must be read (multi
6908 dimension implicit array init handling). 'size_only' is true if
6909 size only evaluation is wanted (only for arrays). */
6910 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6911 int first
, int size_only
)
6913 int len
, n
, no_oblock
, nb
, i
;
6920 /* If we currently are at an '}' or ',' we have read an initializer
6921 element in one of our callers, and not yet consumed it. */
6922 have_elem
= tok
== '}' || tok
== ',';
6923 if (!have_elem
&& tok
!= '{' &&
6924 /* In case of strings we have special handling for arrays, so
6925 don't consume them as initializer value (which would commit them
6926 to some anonymous symbol). */
6927 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6929 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6934 !(type
->t
& VT_ARRAY
) &&
6935 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6936 The source type might have VT_CONSTANT set, which is
6937 of course assignable to non-const elements. */
6938 is_compatible_unqualified_types(type
, &vtop
->type
)) {
6939 init_putv(type
, sec
, c
);
6940 } else if (type
->t
& VT_ARRAY
) {
6943 t1
= pointed_type(type
);
6944 size1
= type_size(t1
, &align1
);
6947 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6950 tcc_error("character array initializer must be a literal,"
6951 " optionally enclosed in braces");
6956 /* only parse strings here if correct type (otherwise: handle
6957 them as ((w)char *) expressions */
6958 if ((tok
== TOK_LSTR
&&
6959 #ifdef TCC_TARGET_PE
6960 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6962 (t1
->t
& VT_BTYPE
) == VT_INT
6964 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6966 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6969 /* compute maximum number of chars wanted */
6971 cstr_len
= tokc
.str
.size
;
6973 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6976 if (n
>= 0 && nb
> (n
- len
))
6980 tcc_warning("initializer-string for array is too long");
6981 /* in order to go faster for common case (char
6982 string in global variable, we handle it
6984 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6986 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
6990 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6992 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6994 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
7001 /* only add trailing zero if enough storage (no
7002 warning in this case since it is standard) */
7003 if (n
< 0 || len
< n
) {
7006 init_putv(t1
, sec
, c
+ (len
* size1
));
7017 while (tok
!= '}' || have_elem
) {
7018 len
= decl_designator(type
, sec
, c
, &f
, size_only
, len
);
7020 if (type
->t
& VT_ARRAY
) {
7022 /* special test for multi dimensional arrays (may not
7023 be strictly correct if designators are used at the
7025 if (no_oblock
&& len
>= n
*size1
)
7028 if (s
->type
.t
== VT_UNION
)
7032 if (no_oblock
&& f
== NULL
)
7041 /* put zeros at the end */
7042 if (!size_only
&& len
< n
*size1
)
7043 init_putz(sec
, c
+ len
, n
*size1
- len
);
7046 /* patch type size if needed, which happens only for array types */
7048 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
7049 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7052 if (first
|| tok
== '{') {
7060 } else if (tok
== '{') {
7062 decl_initializer(type
, sec
, c
, first
, size_only
);
7064 } else if (size_only
) {
7065 /* If we supported only ISO C we wouldn't have to accept calling
7066 this on anything than an array size_only==1 (and even then
7067 only on the outermost level, so no recursion would be needed),
7068 because initializing a flex array member isn't supported.
7069 But GNU C supports it, so we need to recurse even into
7070 subfields of structs and arrays when size_only is set. */
7071 /* just skip expression */
7072 skip_or_save_block(NULL
);
7075 /* This should happen only when we haven't parsed
7076 the init element above for fear of committing a
7077 string constant to memory too early. */
7078 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
7079 expect("string constant");
7080 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7082 init_putv(type
, sec
, c
);
7086 /* parse an initializer for type 't' if 'has_init' is non zero, and
7087 allocate space in local or global data space ('r' is either
7088 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7089 variable 'v' of scope 'scope' is declared before initializers
7090 are parsed. If 'v' is zero, then a reference to the new object
7091 is put in the value stack. If 'has_init' is 2, a special parsing
7092 is done to handle string constants. */
7093 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7094 int has_init
, int v
, int scope
)
7096 int size
, align
, addr
;
7097 TokenString
*init_str
= NULL
;
7100 Sym
*flexible_array
;
7102 int saved_nocode_wanted
= nocode_wanted
;
7103 #ifdef CONFIG_TCC_BCHECK
7107 /* Always allocate static or global variables */
7108 if (v
&& (r
& VT_VALMASK
) == VT_CONST
)
7109 nocode_wanted
|= 0x80000000;
7111 #ifdef CONFIG_TCC_BCHECK
7112 bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
7115 flexible_array
= NULL
;
7116 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7117 Sym
*field
= type
->ref
->next
;
7120 field
= field
->next
;
7121 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
7122 flexible_array
= field
;
7126 size
= type_size(type
, &align
);
7127 /* If unknown size, we must evaluate it before
7128 evaluating initializers because
7129 initializers can generate global data too
7130 (e.g. string pointers or ISOC99 compound
7131 literals). It also simplifies local
7132 initializers handling */
7133 if (size
< 0 || (flexible_array
&& has_init
)) {
7135 tcc_error("unknown type size");
7136 /* get all init string */
7137 if (has_init
== 2) {
7138 init_str
= tok_str_alloc();
7139 /* only get strings */
7140 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7141 tok_str_add_tok(init_str
);
7144 tok_str_add(init_str
, -1);
7145 tok_str_add(init_str
, 0);
7147 skip_or_save_block(&init_str
);
7152 begin_macro(init_str
, 1);
7154 decl_initializer(type
, NULL
, 0, 1, 1);
7155 /* prepare second initializer parsing */
7156 macro_ptr
= init_str
->str
;
7159 /* if still unknown size, error */
7160 size
= type_size(type
, &align
);
7162 tcc_error("unknown type size");
7164 /* If there's a flex member and it was used in the initializer
7166 if (flexible_array
&&
7167 flexible_array
->type
.ref
->c
> 0)
7168 size
+= flexible_array
->type
.ref
->c
7169 * pointed_size(&flexible_array
->type
);
7170 /* take into account specified alignment if bigger */
7171 if (ad
->a
.aligned
) {
7172 int speca
= 1 << (ad
->a
.aligned
- 1);
7175 } else if (ad
->a
.packed
) {
7179 if (!v
&& NODATA_WANTED
)
7180 size
= 0, align
= 1;
7182 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7184 #ifdef CONFIG_TCC_BCHECK
7185 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7189 loc
= (loc
- size
) & -align
;
7191 #ifdef CONFIG_TCC_BCHECK
7192 /* handles bounds */
7193 /* XXX: currently, since we do only one pass, we cannot track
7194 '&' operators, so we add only arrays */
7195 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7197 /* add padding between regions */
7199 /* then add local bound info */
7200 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
7201 bounds_ptr
[0] = addr
;
7202 bounds_ptr
[1] = size
;
7206 /* local variable */
7207 #ifdef CONFIG_TCC_ASM
7208 if (ad
->asm_label
) {
7209 int reg
= asm_parse_regvar(ad
->asm_label
);
7211 r
= (r
& ~VT_VALMASK
) | reg
;
7214 sym
= sym_push(v
, type
, r
, addr
);
7215 if (ad
->cleanup_func
) {
7216 Sym
*cls
= sym_push2(&all_cleanups
, SYM_FIELD
| ++ncleanups
, 0, 0);
7217 cls
->prev_tok
= sym
;
7218 cls
->next
= ad
->cleanup_func
;
7219 cls
->ncl
= current_cleanups
;
7220 current_cleanups
= cls
;
7225 /* push local reference */
7226 vset(type
, r
, addr
);
7229 if (v
&& scope
== VT_CONST
) {
7230 /* see if the symbol was already defined */
7233 patch_storage(sym
, ad
, type
);
7234 /* we accept several definitions of the same global variable. */
7235 if (!has_init
&& sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
)
7240 /* allocate symbol in corresponding section */
7245 else if (tcc_state
->nocommon
)
7250 addr
= section_add(sec
, size
, align
);
7251 #ifdef CONFIG_TCC_BCHECK
7252 /* add padding if bound check */
7254 section_add(sec
, 1, 1);
7257 addr
= align
; /* SHN_COMMON is special, symbol value is align */
7258 sec
= common_section
;
7263 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7264 patch_storage(sym
, ad
, NULL
);
7266 /* Local statics have a scope until now (for
7267 warnings), remove it here. */
7269 /* update symbol definition */
7270 put_extern_sym(sym
, sec
, addr
, size
);
7272 /* push global reference */
7273 sym
= get_sym_ref(type
, sec
, addr
, size
);
7274 vpushsym(type
, sym
);
7278 #ifdef CONFIG_TCC_BCHECK
7279 /* handles bounds now because the symbol must be defined
7280 before for the relocation */
7284 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
7285 /* then add global bound info */
7286 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
7287 bounds_ptr
[0] = 0; /* relocated */
7288 bounds_ptr
[1] = size
;
7293 if (type
->t
& VT_VLA
) {
7299 /* save current stack pointer */
7300 if (vlas_in_scope
== 0) {
7301 if (vla_sp_root_loc
== -1)
7302 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
7303 gen_vla_sp_save(vla_sp_root_loc
);
7306 vla_runtime_type_size(type
, &a
);
7307 gen_vla_alloc(type
, a
);
7308 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7309 /* on _WIN64, because of the function args scratch area, the
7310 result of alloca differs from RSP and is returned in RAX. */
7311 gen_vla_result(addr
), addr
= (loc
-= PTR_SIZE
);
7313 gen_vla_sp_save(addr
);
7317 } else if (has_init
) {
7318 size_t oldreloc_offset
= 0;
7319 if (sec
&& sec
->reloc
)
7320 oldreloc_offset
= sec
->reloc
->data_offset
;
7321 decl_initializer(type
, sec
, addr
, 1, 0);
7322 if (sec
&& sec
->reloc
)
7323 squeeze_multi_relocs(sec
, oldreloc_offset
);
7324 /* patch flexible array member size back to -1, */
7325 /* for possible subsequent similar declarations */
7327 flexible_array
->type
.ref
->c
= -1;
7331 /* restore parse state if needed */
7337 nocode_wanted
= saved_nocode_wanted
;
7340 /* parse a function defined by symbol 'sym' and generate its code in
7341 'cur_text_section' */
7342 static void gen_function(Sym
*sym
)
7345 ind
= cur_text_section
->data_offset
;
7346 if (sym
->a
.aligned
) {
7347 size_t newoff
= section_add(cur_text_section
, 0,
7348 1 << (sym
->a
.aligned
- 1));
7349 gen_fill_nops(newoff
- ind
);
7351 /* NOTE: we patch the symbol size later */
7352 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7353 funcname
= get_tok_str(sym
->v
, NULL
);
7355 /* Initialize VLA state */
7357 vla_sp_root_loc
= -1;
7358 /* put debug symbol */
7359 tcc_debug_funcstart(tcc_state
, sym
);
7360 /* push a dummy symbol to enable local sym storage */
7361 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7362 local_scope
= 1; /* for function parameters */
7363 gfunc_prolog(&sym
->type
);
7364 reset_local_scope();
7366 clear_temp_local_var_list();
7367 block(NULL
, NULL
, 0);
7368 if (!(nocode_wanted
& 0x20000000)
7369 && ((func_vt
.t
& VT_BTYPE
) == VT_INT
)
7370 && !strcmp (funcname
, "main"))
7374 gen_assign_cast(&func_vt
);
7375 gfunc_return(&func_vt
);
7380 cur_text_section
->data_offset
= ind
;
7381 label_pop(&global_label_stack
, NULL
, 0);
7382 /* reset local stack */
7383 reset_local_scope();
7384 sym_pop(&local_stack
, NULL
, 0);
7385 /* end of function */
7386 /* patch symbol size */
7387 elfsym(sym
)->st_size
= ind
- func_ind
;
7388 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7389 /* It's better to crash than to generate wrong code */
7390 cur_text_section
= NULL
;
7391 funcname
= ""; /* for safety */
7392 func_vt
.t
= VT_VOID
; /* for safety */
7393 func_var
= 0; /* for safety */
7394 ind
= 0; /* for safety */
7395 nocode_wanted
= 0x80000000;
7399 static void gen_inline_functions(TCCState
*s
)
7402 int inline_generated
, i
, ln
;
7403 struct InlineFunc
*fn
;
7405 ln
= file
->line_num
;
7406 /* iterate while inline function are referenced */
7408 inline_generated
= 0;
7409 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7410 fn
= s
->inline_fns
[i
];
7412 if (sym
&& sym
->c
) {
7413 /* the function was used: generate its code and
7414 convert it to a normal function */
7417 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7418 sym
->type
.t
&= ~VT_INLINE
;
7420 begin_macro(fn
->func_str
, 1);
7422 cur_text_section
= text_section
;
7426 inline_generated
= 1;
7429 } while (inline_generated
);
7430 file
->line_num
= ln
;
7433 ST_FUNC
void free_inline_functions(TCCState
*s
)
7436 /* free tokens of unused inline functions */
7437 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7438 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7440 tok_str_free(fn
->func_str
);
7442 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7445 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7446 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7447 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7452 AttributeDef ad
, adbase
;
7455 if (!parse_btype(&btype
, &adbase
)) {
7456 if (is_for_loop_init
)
7458 /* skip redundant ';' if not in old parameter decl scope */
7459 if (tok
== ';' && l
!= VT_CMP
) {
7465 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7466 /* global asm block */
7470 if (tok
>= TOK_UIDENT
) {
7471 /* special test for old K&R protos without explicit int
7472 type. Only accepted when defining global data */
7476 expect("declaration");
7481 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7482 int v
= btype
.ref
->v
;
7483 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7484 tcc_warning("unnamed struct/union that defines no instances");
7488 if (IS_ENUM(btype
.t
)) {
7493 while (1) { /* iterate thru each declaration */
7495 /* If the base type itself was an array type of unspecified
7496 size (like in 'typedef int arr[]; arr x = {1};') then
7497 we will overwrite the unknown size by the real one for
7498 this decl. We need to unshare the ref symbol holding
7500 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7501 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7504 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7508 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7509 printf("type = '%s'\n", buf
);
7512 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7513 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7514 tcc_error("function without file scope cannot be static");
7516 /* if old style function prototype, we accept a
7519 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7520 decl0(VT_CMP
, 0, sym
);
7523 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7524 ad
.asm_label
= asm_label_instr();
7525 /* parse one last attribute list, after asm label */
7526 parse_attribute(&ad
);
7531 #ifdef TCC_TARGET_PE
7532 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7533 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7534 tcc_error("cannot have dll linkage with static or typedef");
7535 if (ad
.a
.dllimport
) {
7536 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7539 type
.t
|= VT_EXTERN
;
7545 tcc_error("cannot use local functions");
7546 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7547 expect("function definition");
7549 /* reject abstract declarators in function definition
7550 make old style params without decl have int type */
7552 while ((sym
= sym
->next
) != NULL
) {
7553 if (!(sym
->v
& ~SYM_FIELD
))
7554 expect("identifier");
7555 if (sym
->type
.t
== VT_VOID
)
7556 sym
->type
= int_type
;
7559 /* XXX: cannot do better now: convert extern line to static inline */
7560 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7561 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7563 /* put function symbol */
7564 sym
= external_global_sym(v
, &type
, 0);
7565 type
.t
&= ~VT_EXTERN
;
7566 patch_storage(sym
, &ad
, &type
);
7568 /* static inline functions are just recorded as a kind
7569 of macro. Their code will be emitted at the end of
7570 the compilation unit only if they are used */
7571 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7572 (VT_INLINE
| VT_STATIC
)) {
7573 struct InlineFunc
*fn
;
7574 const char *filename
;
7576 filename
= file
? file
->filename
: "";
7577 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7578 strcpy(fn
->filename
, filename
);
7580 skip_or_save_block(&fn
->func_str
);
7581 dynarray_add(&tcc_state
->inline_fns
,
7582 &tcc_state
->nb_inline_fns
, fn
);
7584 /* compute text section */
7585 cur_text_section
= ad
.section
;
7586 if (!cur_text_section
)
7587 cur_text_section
= text_section
;
7593 /* find parameter in function parameter list */
7594 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7595 if ((sym
->v
& ~SYM_FIELD
) == v
)
7597 tcc_error("declaration for parameter '%s' but no such parameter",
7598 get_tok_str(v
, NULL
));
7600 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7601 tcc_error("storage class specified for '%s'",
7602 get_tok_str(v
, NULL
));
7603 if (sym
->type
.t
!= VT_VOID
)
7604 tcc_error("redefinition of parameter '%s'",
7605 get_tok_str(v
, NULL
));
7606 convert_parameter_type(&type
);
7608 } else if (type
.t
& VT_TYPEDEF
) {
7609 /* save typedefed type */
7610 /* XXX: test storage specifiers ? */
7612 if (sym
&& sym
->sym_scope
== local_scope
) {
7613 if (!is_compatible_types(&sym
->type
, &type
)
7614 || !(sym
->type
.t
& VT_TYPEDEF
))
7615 tcc_error("incompatible redefinition of '%s'",
7616 get_tok_str(v
, NULL
));
7619 sym
= sym_push(v
, &type
, 0, 0);
7623 } else if ((type
.t
& VT_BTYPE
) == VT_VOID
7624 && !(type
.t
& VT_EXTERN
)) {
7625 tcc_error("declaration of void object");
7628 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7629 /* external function definition */
7630 /* specific case for func_call attribute */
7632 } else if (!(type
.t
& VT_ARRAY
)) {
7633 /* not lvalue if array */
7634 r
|= lvalue_type(type
.t
);
7636 has_init
= (tok
== '=');
7637 if (has_init
&& (type
.t
& VT_VLA
))
7638 tcc_error("variable length array cannot be initialized");
7639 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
)) ||
7640 ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7641 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7642 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7643 /* external variable or function */
7644 /* NOTE: as GCC, uninitialized global static
7645 arrays of null size are considered as
7647 type
.t
|= VT_EXTERN
;
7648 sym
= external_sym(v
, &type
, r
, &ad
);
7649 if (ad
.alias_target
) {
7652 alias_target
= sym_find(ad
.alias_target
);
7653 esym
= elfsym(alias_target
);
7655 tcc_error("unsupported forward __alias__ attribute");
7656 /* Local statics have a scope until now (for
7657 warnings), remove it here. */
7659 put_extern_sym2(sym
, esym
->st_shndx
, esym
->st_value
, esym
->st_size
, 0);
7662 if (type
.t
& VT_STATIC
)
7668 else if (l
== VT_CONST
)
7669 /* uninitialized global variables may be overridden */
7670 type
.t
|= VT_EXTERN
;
7671 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7675 if (is_for_loop_init
)
7688 static void decl(int l
)
7693 /* ------------------------------------------------------------------------- */