2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Sym
*sym_free_first
;
34 ST_DATA
void **sym_pools
;
35 ST_DATA
int nb_sym_pools
;
37 ST_DATA Sym
*global_stack
;
38 ST_DATA Sym
*local_stack
;
39 ST_DATA Sym
*define_stack
;
40 ST_DATA Sym
*global_label_stack
;
41 ST_DATA Sym
*local_label_stack
;
42 static int local_scope
;
44 static int section_sym
;
46 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
47 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
52 ST_DATA
int const_wanted
; /* true if constant wanted */
53 ST_DATA
int nocode_wanted
; /* no code generation wanted */
54 #define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
55 #define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
56 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
57 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
58 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
60 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
61 ST_DATA
const char *funcname
;
64 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
66 ST_DATA
struct switch_t
{
70 } **p
; int n
; /* list of case ranges */
71 int def_sym
; /* default symbol */
72 } *cur_switch
; /* current switch */
74 /* ------------------------------------------------------------------------- */
76 static void gen_cast(CType
*type
);
77 static void gen_cast_s(int t
);
78 static inline CType
*pointed_type(CType
*type
);
79 static int is_compatible_types(CType
*type1
, CType
*type2
);
80 static int parse_btype(CType
*type
, AttributeDef
*ad
);
81 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
82 static void parse_expr_type(CType
*type
);
83 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
);
84 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
85 static void block(int *bsym
, int *csym
, int is_expr
);
86 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
87 static void decl(int l
);
88 static int decl0(int l
, int is_for_loop_init
, Sym
*);
89 static void expr_eq(void);
90 static void vla_runtime_type_size(CType
*type
, int *a
);
91 static void vla_sp_restore(void);
92 static void vla_sp_restore_root(void);
93 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
);
94 static inline int64_t expr_const64(void);
95 static void vpush64(int ty
, unsigned long long v
);
96 static void vpush(CType
*type
);
97 static int gvtst(int inv
, int t
);
98 static void gen_inline_functions(TCCState
*s
);
99 static void skip_or_save_block(TokenString
**str
);
100 static void gv_dup(void);
102 ST_INLN
int is_float(int t
)
106 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
109 /* we use our own 'finite' function to avoid potential problems with
110 non standard math libs */
111 /* XXX: endianness dependent */
112 ST_FUNC
int ieee_finite(double d
)
115 memcpy(p
, &d
, sizeof(double));
116 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
119 ST_FUNC
void test_lvalue(void)
121 if (!(vtop
->r
& VT_LVAL
))
125 ST_FUNC
void check_vstack(void)
128 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
131 /* ------------------------------------------------------------------------- */
132 /* vstack debugging aid */
135 void pv (const char *lbl
, int a
, int b
)
138 for (i
= a
; i
< a
+ b
; ++i
) {
139 SValue
*p
= &vtop
[-i
];
140 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
141 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
146 /* ------------------------------------------------------------------------- */
147 /* start of translation unit info */
148 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
153 /* file info: full path + filename */
154 section_sym
= put_elf_sym(symtab_section
, 0, 0,
155 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
156 text_section
->sh_num
, NULL
);
157 getcwd(buf
, sizeof(buf
));
159 normalize_slashes(buf
);
161 pstrcat(buf
, sizeof(buf
), "/");
162 put_stabs_r(buf
, N_SO
, 0, 0,
163 text_section
->data_offset
, text_section
, section_sym
);
164 put_stabs_r(file
->filename
, N_SO
, 0, 0,
165 text_section
->data_offset
, text_section
, section_sym
);
170 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
171 symbols can be safely used */
172 put_elf_sym(symtab_section
, 0, 0,
173 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
174 SHN_ABS
, file
->filename
);
177 /* put end of translation unit info */
178 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
182 put_stabs_r(NULL
, N_SO
, 0, 0,
183 text_section
->data_offset
, text_section
, section_sym
);
187 /* generate line number info */
188 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
192 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
193 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
195 last_line_num
= file
->line_num
;
199 /* put function symbol */
200 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
208 /* XXX: we put here a dummy type */
209 snprintf(buf
, sizeof(buf
), "%s:%c1",
210 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
211 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
212 cur_text_section
, sym
->c
);
213 /* //gr gdb wants a line at the function */
214 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
220 /* put function size */
221 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
225 put_stabn(N_FUN
, 0, 0, size
);
228 /* ------------------------------------------------------------------------- */
229 ST_FUNC
int tccgen_compile(TCCState
*s1
)
231 cur_text_section
= NULL
;
233 anon_sym
= SYM_FIRST_ANOM
;
236 nocode_wanted
= 0x80000000;
238 /* define some often used types */
240 char_pointer_type
.t
= VT_BYTE
;
241 mk_pointer(&char_pointer_type
);
243 size_type
.t
= VT_INT
;
245 size_type
.t
= VT_LLONG
;
247 func_old_type
.t
= VT_FUNC
;
248 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, 0, 0);
249 func_old_type
.ref
->f
.func_call
= FUNC_CDECL
;
250 func_old_type
.ref
->f
.func_type
= FUNC_OLD
;
254 #ifdef TCC_TARGET_ARM
259 printf("%s: **** new file\n", file
->filename
);
262 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
266 expect("declaration");
268 gen_inline_functions(s1
);
270 /* end of translation unit info */
275 /* ------------------------------------------------------------------------- */
276 /* apply storage attributes to Elf symbol */
278 static void update_storage(Sym
*sym
)
283 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
284 if (sym
->a
.visibility
)
285 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
288 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(esym
->st_info
));
290 if (sym
->a
.dllimport
)
291 esym
->st_other
|= ST_PE_IMPORT
;
292 if (sym
->a
.dllexport
)
293 esym
->st_other
|= ST_PE_EXPORT
;
296 printf("storage %s: vis=%d weak=%d exp=%d imp=%d\n",
297 get_tok_str(sym
->v
, NULL
),
306 /* ------------------------------------------------------------------------- */
307 /* update sym->c so that it points to an external symbol in section
308 'section' with value 'value' */
310 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
311 addr_t value
, unsigned long size
,
312 int can_add_underscore
)
314 int sym_type
, sym_bind
, sh_num
, info
, other
, t
;
318 #ifdef CONFIG_TCC_BCHECK
324 else if (section
== SECTION_ABS
)
327 sh_num
= section
->sh_num
;
330 name
= get_tok_str(sym
->v
, NULL
);
331 #ifdef CONFIG_TCC_BCHECK
332 if (tcc_state
->do_bounds_check
) {
333 /* XXX: avoid doing that for statics ? */
334 /* if bound checking is activated, we change some function
335 names by adding the "__bound" prefix */
338 /* XXX: we rely only on malloc hooks */
351 strcpy(buf
, "__bound_");
359 if ((t
& VT_BTYPE
) == VT_FUNC
) {
361 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
362 sym_type
= STT_NOTYPE
;
364 sym_type
= STT_OBJECT
;
367 sym_bind
= STB_LOCAL
;
369 sym_bind
= STB_GLOBAL
;
372 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
373 Sym
*ref
= sym
->type
.ref
;
374 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
375 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
377 other
|= ST_PE_STDCALL
;
378 can_add_underscore
= 0;
382 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
384 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
388 name
= get_tok_str(sym
->asm_label
, NULL
);
389 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
390 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
392 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
393 esym
->st_value
= value
;
394 esym
->st_size
= size
;
395 esym
->st_shndx
= sh_num
;
400 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
401 addr_t value
, unsigned long size
)
403 put_extern_sym2(sym
, section
, value
, size
, 1);
406 /* add a new relocation entry to symbol 'sym' in section 's' */
407 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
412 if (nocode_wanted
&& s
== cur_text_section
)
417 put_extern_sym(sym
, NULL
, 0, 0);
421 /* now we can add ELF relocation info */
422 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
426 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
428 greloca(s
, sym
, offset
, type
, 0);
432 /* ------------------------------------------------------------------------- */
433 /* symbol allocator */
434 static Sym
*__sym_malloc(void)
436 Sym
*sym_pool
, *sym
, *last_sym
;
439 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
440 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
442 last_sym
= sym_free_first
;
444 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
445 sym
->next
= last_sym
;
449 sym_free_first
= last_sym
;
453 static inline Sym
*sym_malloc(void)
457 sym
= sym_free_first
;
459 sym
= __sym_malloc();
460 sym_free_first
= sym
->next
;
463 sym
= tcc_malloc(sizeof(Sym
));
468 ST_INLN
void sym_free(Sym
*sym
)
471 sym
->next
= sym_free_first
;
472 sym_free_first
= sym
;
478 /* push, without hashing */
479 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
484 memset(s
, 0, sizeof *s
);
494 /* find a symbol and return its associated structure. 's' is the top
495 of the symbol stack */
496 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
508 /* structure lookup */
509 ST_INLN Sym
*struct_find(int v
)
512 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
514 return table_ident
[v
]->sym_struct
;
517 /* find an identifier */
518 ST_INLN Sym
*sym_find(int v
)
521 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
523 return table_ident
[v
]->sym_identifier
;
526 /* push a given symbol on the symbol stack */
527 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
536 s
= sym_push2(ps
, v
, type
->t
, c
);
537 s
->type
.ref
= type
->ref
;
539 /* don't record fields or anonymous symbols */
541 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
542 /* record symbol in token array */
543 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
545 ps
= &ts
->sym_struct
;
547 ps
= &ts
->sym_identifier
;
550 s
->sym_scope
= local_scope
;
551 if (s
->prev_tok
&& s
->prev_tok
->sym_scope
== s
->sym_scope
)
552 tcc_error("redeclaration of '%s'",
553 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
558 /* push a global identifier */
559 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
562 s
= sym_push2(&global_stack
, v
, t
, c
);
563 /* don't record anonymous symbol */
564 if (v
< SYM_FIRST_ANOM
) {
565 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
566 /* modify the top most local identifier, so that
567 sym_identifier will point to 's' when popped */
569 ps
= &(*ps
)->prev_tok
;
576 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
577 pop them yet from the list, but do remove them from the token array. */
578 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
588 /* remove symbol in token array */
590 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
591 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
593 ps
= &ts
->sym_struct
;
595 ps
= &ts
->sym_identifier
;
606 /* ------------------------------------------------------------------------- */
608 static void vsetc(CType
*type
, int r
, CValue
*vc
)
612 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
613 tcc_error("memory full (vstack)");
614 /* cannot let cpu flags if other instruction are generated. Also
615 avoid leaving VT_JMP anywhere except on the top of the stack
616 because it would complicate the code generator.
618 Don't do this when nocode_wanted. vtop might come from
619 !nocode_wanted regions (see 88_codeopt.c) and transforming
620 it to a register without actually generating code is wrong
621 as their value might still be used for real. All values
622 we push under nocode_wanted will eventually be popped
623 again, so that the VT_CMP/VT_JMP value will be in vtop
624 when code is unsuppressed again.
626 Same logic below in vswap(); */
627 if (vtop
>= vstack
&& !nocode_wanted
) {
628 v
= vtop
->r
& VT_VALMASK
;
629 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
641 ST_FUNC
void vswap(void)
644 /* cannot vswap cpu flags. See comment at vsetc() above */
645 if (vtop
>= vstack
&& !nocode_wanted
) {
646 int v
= vtop
->r
& VT_VALMASK
;
647 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
655 /* pop stack value */
656 ST_FUNC
void vpop(void)
659 v
= vtop
->r
& VT_VALMASK
;
660 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
661 /* for x86, we need to pop the FP stack */
663 o(0xd8dd); /* fstp %st(0) */
666 if (v
== VT_JMP
|| v
== VT_JMPI
) {
667 /* need to put correct jump if && or || without test */
673 /* push constant of type "type" with useless value */
674 ST_FUNC
void vpush(CType
*type
)
676 vset(type
, VT_CONST
, 0);
679 /* push integer constant */
680 ST_FUNC
void vpushi(int v
)
684 vsetc(&int_type
, VT_CONST
, &cval
);
687 /* push a pointer sized constant */
688 static void vpushs(addr_t v
)
692 vsetc(&size_type
, VT_CONST
, &cval
);
695 /* push arbitrary 64bit constant */
696 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
703 vsetc(&ctype
, VT_CONST
, &cval
);
706 /* push long long constant */
707 static inline void vpushll(long long v
)
709 vpush64(VT_LLONG
, v
);
712 ST_FUNC
void vset(CType
*type
, int r
, int v
)
717 vsetc(type
, r
, &cval
);
720 static void vseti(int r
, int v
)
728 ST_FUNC
void vpushv(SValue
*v
)
730 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
731 tcc_error("memory full (vstack)");
736 static void vdup(void)
741 /* rotate n first stack elements to the bottom
742 I1 ... In -> I2 ... In I1 [top is right]
744 ST_FUNC
void vrotb(int n
)
755 /* rotate the n elements before entry e towards the top
756 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
758 ST_FUNC
void vrote(SValue
*e
, int n
)
764 for(i
= 0;i
< n
- 1; i
++)
769 /* rotate n first stack elements to the top
770 I1 ... In -> In I1 ... I(n-1) [top is right]
772 ST_FUNC
void vrott(int n
)
777 /* push a symbol value of TYPE */
778 static inline void vpushsym(CType
*type
, Sym
*sym
)
782 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
786 /* Return a static symbol pointing to a section */
787 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
793 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
794 sym
->type
.ref
= type
->ref
;
795 sym
->r
= VT_CONST
| VT_SYM
;
796 put_extern_sym(sym
, sec
, offset
, size
);
800 /* push a reference to a section offset by adding a dummy symbol */
801 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
803 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
806 /* define a new external reference to a symbol 'v' of type 'u' */
807 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
813 /* push forward reference */
814 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
815 s
->type
.ref
= type
->ref
;
816 s
->r
= r
| VT_CONST
| VT_SYM
;
821 /* Merge some storage attributes. */
822 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
824 if (type
&& !is_compatible_types(&sym
->type
, type
))
825 tcc_error("incompatible types for redefinition of '%s'",
826 get_tok_str(sym
->v
, NULL
));
828 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
829 tcc_error("incompatible dll linkage for redefinition of '%s'",
830 get_tok_str(sym
->v
, NULL
));
832 sym
->a
.dllexport
|= ad
->a
.dllexport
;
833 sym
->a
.weak
|= ad
->a
.weak
;
834 if (ad
->a
.visibility
) {
835 int vis
= sym
->a
.visibility
;
836 int vis2
= ad
->a
.visibility
;
837 if (vis
== STV_DEFAULT
)
839 else if (vis2
!= STV_DEFAULT
)
840 vis
= (vis
< vis2
) ? vis
: vis2
;
841 sym
->a
.visibility
= vis
;
844 sym
->a
.aligned
= ad
->a
.aligned
;
846 sym
->asm_label
= ad
->asm_label
;
850 /* define a new external reference to a symbol 'v' */
851 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
856 /* push forward reference */
857 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
858 s
->type
.t
|= VT_EXTERN
;
862 if (s
->type
.ref
== func_old_type
.ref
) {
863 s
->type
.ref
= type
->ref
;
864 s
->r
= r
| VT_CONST
| VT_SYM
;
865 s
->type
.t
|= VT_EXTERN
;
867 patch_storage(s
, ad
, type
);
872 /* push a reference to global symbol v */
873 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
875 vpushsym(type
, external_global_sym(v
, type
, 0));
878 /* save registers up to (vtop - n) stack entry */
879 ST_FUNC
void save_regs(int n
)
882 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
886 /* save r to the memory stack, and mark it as being free */
887 ST_FUNC
void save_reg(int r
)
889 save_reg_upstack(r
, 0);
892 /* save r to the memory stack, and mark it as being free,
893 if seen up to (vtop - n) stack entry */
894 ST_FUNC
void save_reg_upstack(int r
, int n
)
896 int l
, saved
, size
, align
;
900 if ((r
&= VT_VALMASK
) >= VT_CONST
)
905 /* modify all stack values */
908 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
909 if ((p
->r
& VT_VALMASK
) == r
||
910 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
911 /* must save value on stack if not already done */
913 /* NOTE: must reload 'r' because r might be equal to r2 */
914 r
= p
->r
& VT_VALMASK
;
915 /* store register in the stack */
917 if ((p
->r
& VT_LVAL
) ||
918 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
920 type
= &char_pointer_type
;
924 size
= type_size(type
, &align
);
925 loc
= (loc
- size
) & -align
;
927 sv
.r
= VT_LOCAL
| VT_LVAL
;
930 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
931 /* x86 specific: need to pop fp register ST0 if saved */
933 o(0xd8dd); /* fstp %st(0) */
937 /* special long long case */
938 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
946 /* mark that stack entry as being saved on the stack */
947 if (p
->r
& VT_LVAL
) {
948 /* also clear the bounded flag because the
949 relocation address of the function was stored in
951 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
953 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
961 #ifdef TCC_TARGET_ARM
962 /* find a register of class 'rc2' with at most one reference on stack.
963 * If none, call get_reg(rc) */
964 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
969 for(r
=0;r
<NB_REGS
;r
++) {
970 if (reg_classes
[r
] & rc2
) {
973 for(p
= vstack
; p
<= vtop
; p
++) {
974 if ((p
->r
& VT_VALMASK
) == r
||
975 (p
->r2
& VT_VALMASK
) == r
)
986 /* find a free register of class 'rc'. If none, save one register */
987 ST_FUNC
int get_reg(int rc
)
992 /* find a free register */
993 for(r
=0;r
<NB_REGS
;r
++) {
994 if (reg_classes
[r
] & rc
) {
997 for(p
=vstack
;p
<=vtop
;p
++) {
998 if ((p
->r
& VT_VALMASK
) == r
||
999 (p
->r2
& VT_VALMASK
) == r
)
1007 /* no register left : free the first one on the stack (VERY
1008 IMPORTANT to start from the bottom to ensure that we don't
1009 spill registers used in gen_opi()) */
1010 for(p
=vstack
;p
<=vtop
;p
++) {
1011 /* look at second register (if long long) */
1012 r
= p
->r2
& VT_VALMASK
;
1013 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1015 r
= p
->r
& VT_VALMASK
;
1016 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1022 /* Should never comes here */
1026 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1028 static void move_reg(int r
, int s
, int t
)
1042 /* get address of vtop (vtop MUST BE an lvalue) */
1043 ST_FUNC
void gaddrof(void)
1045 vtop
->r
&= ~VT_LVAL
;
1046 /* tricky: if saved lvalue, then we can go back to lvalue */
1047 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1048 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1053 #ifdef CONFIG_TCC_BCHECK
1054 /* generate lvalue bound code */
1055 static void gbound(void)
1060 vtop
->r
&= ~VT_MUSTBOUND
;
1061 /* if lvalue, then use checking code before dereferencing */
1062 if (vtop
->r
& VT_LVAL
) {
1063 /* if not VT_BOUNDED value, then make one */
1064 if (!(vtop
->r
& VT_BOUNDED
)) {
1065 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1066 /* must save type because we must set it to int to get pointer */
1068 vtop
->type
.t
= VT_PTR
;
1071 gen_bounded_ptr_add();
1072 vtop
->r
|= lval_type
;
1075 /* then check for dereferencing */
1076 gen_bounded_ptr_deref();
1081 static void incr_bf_adr(int o
)
1083 vtop
->type
= char_pointer_type
;
1087 vtop
->type
.t
= (vtop
->type
.t
& ~(VT_BTYPE
|VT_DEFSIGN
))
1088 | (VT_BYTE
|VT_UNSIGNED
);
1089 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1090 | (VT_LVAL_BYTE
|VT_LVAL_UNSIGNED
|VT_LVAL
);
1093 /* single-byte load mode for packed or otherwise unaligned bitfields */
1094 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1097 save_reg_upstack(vtop
->r
, 1);
1098 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1099 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1108 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1110 vpushi((1 << n
) - 1), gen_op('&');
1113 vpushi(bits
), gen_op(TOK_SHL
);
1116 bits
+= n
, bit_size
-= n
, o
= 1;
1119 if (!(type
->t
& VT_UNSIGNED
)) {
1120 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG
? 64 : 32) - bits
;
1121 vpushi(n
), gen_op(TOK_SHL
);
1122 vpushi(n
), gen_op(TOK_SAR
);
1126 /* single-byte store mode for packed or otherwise unaligned bitfields */
1127 static void store_packed_bf(int bit_pos
, int bit_size
)
1129 int bits
, n
, o
, m
, c
;
1131 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1133 save_reg_upstack(vtop
->r
, 1);
1134 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1136 incr_bf_adr(o
); // X B
1138 c
? vdup() : gv_dup(); // B V X
1141 vpushi(bits
), gen_op(TOK_SHR
);
1143 vpushi(bit_pos
), gen_op(TOK_SHL
);
1148 m
= ((1 << n
) - 1) << bit_pos
;
1149 vpushi(m
), gen_op('&'); // X B V1
1150 vpushv(vtop
-1); // X B V1 B
1151 vpushi(m
& 0x80 ? ~m
& 0x7f : ~m
);
1152 gen_op('&'); // X B V1 B1
1153 gen_op('|'); // X B V2
1155 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1156 vstore(), vpop(); // X B
1157 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1162 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1165 if (0 == sv
->type
.ref
)
1167 t
= sv
->type
.ref
->auxtype
;
1168 if (t
!= -1 && t
!= VT_STRUCT
) {
1169 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1170 sv
->r
= (sv
->r
& ~VT_LVAL_TYPE
) | lvalue_type(sv
->type
.t
);
1175 /* store vtop a register belonging to class 'rc'. lvalues are
1176 converted to values. Cannot be used if cannot be converted to
1177 register value (such as structures). */
1178 ST_FUNC
int gv(int rc
)
1180 int r
, bit_pos
, bit_size
, size
, align
, rc2
;
1182 /* NOTE: get_reg can modify vstack[] */
1183 if (vtop
->type
.t
& VT_BITFIELD
) {
1186 bit_pos
= BIT_POS(vtop
->type
.t
);
1187 bit_size
= BIT_SIZE(vtop
->type
.t
);
1188 /* remove bit field info to avoid loops */
1189 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1192 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1193 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1194 type
.t
|= VT_UNSIGNED
;
1196 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1198 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1203 if (r
== VT_STRUCT
) {
1204 load_packed_bf(&type
, bit_pos
, bit_size
);
1206 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG
? 64 : 32;
1207 /* cast to int to propagate signedness in following ops */
1209 /* generate shifts */
1210 vpushi(bits
- (bit_pos
+ bit_size
));
1212 vpushi(bits
- bit_size
);
1213 /* NOTE: transformed to SHR if unsigned */
1218 if (is_float(vtop
->type
.t
) &&
1219 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1220 unsigned long offset
;
1221 /* CPUs usually cannot use float constants, so we store them
1222 generically in data segment */
1223 size
= type_size(&vtop
->type
, &align
);
1225 size
= 0, align
= 1;
1226 offset
= section_add(data_section
, size
, align
);
1227 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1229 init_putv(&vtop
->type
, data_section
, offset
);
1232 #ifdef CONFIG_TCC_BCHECK
1233 if (vtop
->r
& VT_MUSTBOUND
)
1237 r
= vtop
->r
& VT_VALMASK
;
1238 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1239 #ifndef TCC_TARGET_ARM64
1242 #ifdef TCC_TARGET_X86_64
1243 else if (rc
== RC_FRET
)
1247 /* need to reload if:
1249 - lvalue (need to dereference pointer)
1250 - already a register, but not in the right class */
1252 || (vtop
->r
& VT_LVAL
)
1253 || !(reg_classes
[r
] & rc
)
1255 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1256 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1258 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1264 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1265 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1267 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1268 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1269 unsigned long long ll
;
1271 int r2
, original_type
;
1272 original_type
= vtop
->type
.t
;
1273 /* two register type load : expand to two words
1276 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1279 vtop
->c
.i
= ll
; /* first word */
1281 vtop
->r
= r
; /* save register value */
1282 vpushi(ll
>> 32); /* second word */
1285 if (vtop
->r
& VT_LVAL
) {
1286 /* We do not want to modifier the long long
1287 pointer here, so the safest (and less
1288 efficient) is to save all the other registers
1289 in the stack. XXX: totally inefficient. */
1293 /* lvalue_save: save only if used further down the stack */
1294 save_reg_upstack(vtop
->r
, 1);
1296 /* load from memory */
1297 vtop
->type
.t
= load_type
;
1300 vtop
[-1].r
= r
; /* save register value */
1301 /* increment pointer to get second word */
1302 vtop
->type
.t
= addr_type
;
1307 vtop
->type
.t
= load_type
;
1309 /* move registers */
1312 vtop
[-1].r
= r
; /* save register value */
1313 vtop
->r
= vtop
[-1].r2
;
1315 /* Allocate second register. Here we rely on the fact that
1316 get_reg() tries first to free r2 of an SValue. */
1320 /* write second register */
1322 vtop
->type
.t
= original_type
;
1323 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1325 /* lvalue of scalar type : need to use lvalue type
1326 because of possible cast */
1329 /* compute memory access type */
1330 if (vtop
->r
& VT_LVAL_BYTE
)
1332 else if (vtop
->r
& VT_LVAL_SHORT
)
1334 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1338 /* restore wanted type */
1341 /* one register type load */
1346 #ifdef TCC_TARGET_C67
1347 /* uses register pairs for doubles */
1348 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1355 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1356 ST_FUNC
void gv2(int rc1
, int rc2
)
1360 /* generate more generic register first. But VT_JMP or VT_CMP
1361 values must be generated first in all cases to avoid possible
1363 v
= vtop
[0].r
& VT_VALMASK
;
1364 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1369 /* test if reload is needed for first register */
1370 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1380 /* test if reload is needed for first register */
1381 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1387 #ifndef TCC_TARGET_ARM64
1388 /* wrapper around RC_FRET to return a register by type */
1389 static int rc_fret(int t
)
1391 #ifdef TCC_TARGET_X86_64
1392 if (t
== VT_LDOUBLE
) {
1400 /* wrapper around REG_FRET to return a register by type */
1401 static int reg_fret(int t
)
1403 #ifdef TCC_TARGET_X86_64
1404 if (t
== VT_LDOUBLE
) {
1412 /* expand 64bit on stack in two ints */
1413 static void lexpand(void)
1416 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1417 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1418 if (v
== VT_CONST
) {
1421 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1427 vtop
[0].r
= vtop
[-1].r2
;
1428 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1430 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1434 #ifdef TCC_TARGET_ARM
1435 /* expand long long on stack */
1436 ST_FUNC
void lexpand_nr(void)
1440 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1442 vtop
->r2
= VT_CONST
;
1443 vtop
->type
.t
= VT_INT
| u
;
1444 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1445 if (v
== VT_CONST
) {
1446 vtop
[-1].c
.i
= vtop
->c
.i
;
1447 vtop
->c
.i
= vtop
->c
.i
>> 32;
1449 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1451 vtop
->r
= vtop
[-1].r
;
1452 } else if (v
> VT_CONST
) {
1456 vtop
->r
= vtop
[-1].r2
;
1457 vtop
[-1].r2
= VT_CONST
;
1458 vtop
[-1].type
.t
= VT_INT
| u
;
1463 /* build a long long from two ints */
1464 static void lbuild(int t
)
1466 gv2(RC_INT
, RC_INT
);
1467 vtop
[-1].r2
= vtop
[0].r
;
1468 vtop
[-1].type
.t
= t
;
1473 /* convert stack entry to register and duplicate its value in another
1475 static void gv_dup(void)
1482 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1483 if (t
& VT_BITFIELD
) {
1493 /* stack: H L L1 H1 */
1503 /* duplicate value */
1508 #ifdef TCC_TARGET_X86_64
1509 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1519 load(r1
, &sv
); /* move r to r1 */
1521 /* duplicates value */
1527 /* Generate value test
1529 * Generate a test for any value (jump, comparison and integers) */
1530 ST_FUNC
int gvtst(int inv
, int t
)
1532 int v
= vtop
->r
& VT_VALMASK
;
1533 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1537 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1538 /* constant jmp optimization */
1539 if ((vtop
->c
.i
!= 0) != inv
)
1544 return gtst(inv
, t
);
1548 /* generate CPU independent (unsigned) long long operations */
1549 static void gen_opl(int op
)
1551 int t
, a
, b
, op1
, c
, i
;
1553 unsigned short reg_iret
= REG_IRET
;
1554 unsigned short reg_lret
= REG_LRET
;
1560 func
= TOK___divdi3
;
1563 func
= TOK___udivdi3
;
1566 func
= TOK___moddi3
;
1569 func
= TOK___umoddi3
;
1576 /* call generic long long function */
1577 vpush_global_sym(&func_old_type
, func
);
1582 vtop
->r2
= reg_lret
;
1590 //pv("gen_opl A",0,2);
1596 /* stack: L1 H1 L2 H2 */
1601 vtop
[-2] = vtop
[-3];
1604 /* stack: H1 H2 L1 L2 */
1605 //pv("gen_opl B",0,4);
1611 /* stack: H1 H2 L1 L2 ML MH */
1614 /* stack: ML MH H1 H2 L1 L2 */
1618 /* stack: ML MH H1 L2 H2 L1 */
1623 /* stack: ML MH M1 M2 */
1626 } else if (op
== '+' || op
== '-') {
1627 /* XXX: add non carry method too (for MIPS or alpha) */
1633 /* stack: H1 H2 (L1 op L2) */
1636 gen_op(op1
+ 1); /* TOK_xxxC2 */
1639 /* stack: H1 H2 (L1 op L2) */
1642 /* stack: (L1 op L2) H1 H2 */
1644 /* stack: (L1 op L2) (H1 op H2) */
1652 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1653 t
= vtop
[-1].type
.t
;
1657 /* stack: L H shift */
1659 /* constant: simpler */
1660 /* NOTE: all comments are for SHL. the other cases are
1661 done by swapping words */
1672 if (op
!= TOK_SAR
) {
1705 /* XXX: should provide a faster fallback on x86 ? */
1708 func
= TOK___ashrdi3
;
1711 func
= TOK___lshrdi3
;
1714 func
= TOK___ashldi3
;
1720 /* compare operations */
1726 /* stack: L1 H1 L2 H2 */
1728 vtop
[-1] = vtop
[-2];
1730 /* stack: L1 L2 H1 H2 */
1733 /* when values are equal, we need to compare low words. since
1734 the jump is inverted, we invert the test too. */
1737 else if (op1
== TOK_GT
)
1739 else if (op1
== TOK_ULT
)
1741 else if (op1
== TOK_UGT
)
1751 /* generate non equal test */
1757 /* compare low. Always unsigned */
1761 else if (op1
== TOK_LE
)
1763 else if (op1
== TOK_GT
)
1765 else if (op1
== TOK_GE
)
1776 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1778 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1779 return (a
^ b
) >> 63 ? -x
: x
;
1782 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1784 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1787 /* handle integer constant optimizations and various machine
1789 static void gen_opic(int op
)
1791 SValue
*v1
= vtop
- 1;
1793 int t1
= v1
->type
.t
& VT_BTYPE
;
1794 int t2
= v2
->type
.t
& VT_BTYPE
;
1795 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1796 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1797 uint64_t l1
= c1
? v1
->c
.i
: 0;
1798 uint64_t l2
= c2
? v2
->c
.i
: 0;
1799 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1801 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1802 l1
= ((uint32_t)l1
|
1803 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1804 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1805 l2
= ((uint32_t)l2
|
1806 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1810 case '+': l1
+= l2
; break;
1811 case '-': l1
-= l2
; break;
1812 case '&': l1
&= l2
; break;
1813 case '^': l1
^= l2
; break;
1814 case '|': l1
|= l2
; break;
1815 case '*': l1
*= l2
; break;
1822 /* if division by zero, generate explicit division */
1825 tcc_error("division by zero in constant");
1829 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1830 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1831 case TOK_UDIV
: l1
= l1
/ l2
; break;
1832 case TOK_UMOD
: l1
= l1
% l2
; break;
1835 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1836 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1838 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1841 case TOK_ULT
: l1
= l1
< l2
; break;
1842 case TOK_UGE
: l1
= l1
>= l2
; break;
1843 case TOK_EQ
: l1
= l1
== l2
; break;
1844 case TOK_NE
: l1
= l1
!= l2
; break;
1845 case TOK_ULE
: l1
= l1
<= l2
; break;
1846 case TOK_UGT
: l1
= l1
> l2
; break;
1847 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1848 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1849 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1850 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1852 case TOK_LAND
: l1
= l1
&& l2
; break;
1853 case TOK_LOR
: l1
= l1
|| l2
; break;
1857 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1858 l1
= ((uint32_t)l1
|
1859 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1863 /* if commutative ops, put c2 as constant */
1864 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1865 op
== '|' || op
== '*')) {
1867 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1868 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1870 if (!const_wanted
&&
1872 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1873 (l1
== -1 && op
== TOK_SAR
))) {
1874 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1876 } else if (!const_wanted
&&
1877 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1879 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
1880 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1881 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1886 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1889 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1890 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1893 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
1894 /* filter out NOP operations like x*1, x-0, x&-1... */
1896 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1897 /* try to use shifts instead of muls or divs */
1898 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1907 else if (op
== TOK_PDIV
)
1913 } else if (c2
&& (op
== '+' || op
== '-') &&
1914 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1915 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1916 /* symbol + constant case */
1920 /* The backends can't always deal with addends to symbols
1921 larger than +-1<<31. Don't construct such. */
1928 /* call low level op generator */
1929 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1930 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1938 /* generate a floating point operation with constant propagation */
1939 static void gen_opif(int op
)
1943 #if defined _MSC_VER && defined _AMD64_
1944 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
1951 /* currently, we cannot do computations with forward symbols */
1952 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1953 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1955 if (v1
->type
.t
== VT_FLOAT
) {
1958 } else if (v1
->type
.t
== VT_DOUBLE
) {
1966 /* NOTE: we only do constant propagation if finite number (not
1967 NaN or infinity) (ANSI spec) */
1968 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1972 case '+': f1
+= f2
; break;
1973 case '-': f1
-= f2
; break;
1974 case '*': f1
*= f2
; break;
1978 tcc_error("division by zero in constant");
1983 /* XXX: also handles tests ? */
1987 /* XXX: overflow test ? */
1988 if (v1
->type
.t
== VT_FLOAT
) {
1990 } else if (v1
->type
.t
== VT_DOUBLE
) {
2002 static int pointed_size(CType
*type
)
2005 return type_size(pointed_type(type
), &align
);
2008 static void vla_runtime_pointed_size(CType
*type
)
2011 vla_runtime_type_size(pointed_type(type
), &align
);
2014 static inline int is_null_pointer(SValue
*p
)
2016 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2018 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2019 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2020 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2021 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
2024 static inline int is_integer_btype(int bt
)
2026 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2027 bt
== VT_INT
|| bt
== VT_LLONG
);
2030 /* check types for comparison or subtraction of pointers */
2031 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2033 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2036 /* null pointers are accepted for all comparisons as gcc */
2037 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2041 bt1
= type1
->t
& VT_BTYPE
;
2042 bt2
= type2
->t
& VT_BTYPE
;
2043 /* accept comparison between pointer and integer with a warning */
2044 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2045 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2046 tcc_warning("comparison between pointer and integer");
2050 /* both must be pointers or implicit function pointers */
2051 if (bt1
== VT_PTR
) {
2052 type1
= pointed_type(type1
);
2053 } else if (bt1
!= VT_FUNC
)
2054 goto invalid_operands
;
2056 if (bt2
== VT_PTR
) {
2057 type2
= pointed_type(type2
);
2058 } else if (bt2
!= VT_FUNC
) {
2060 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2062 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2063 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2067 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2068 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2069 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2070 /* gcc-like error if '-' is used */
2072 goto invalid_operands
;
2074 tcc_warning("comparison of distinct pointer types lacks a cast");
2078 /* generic gen_op: handles types problems */
2079 ST_FUNC
void gen_op(int op
)
2081 int u
, t1
, t2
, bt1
, bt2
, t
;
2085 t1
= vtop
[-1].type
.t
;
2086 t2
= vtop
[0].type
.t
;
2087 bt1
= t1
& VT_BTYPE
;
2088 bt2
= t2
& VT_BTYPE
;
2090 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2091 tcc_error("operation on a struct");
2092 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2093 if (bt2
== VT_FUNC
) {
2094 mk_pointer(&vtop
->type
);
2097 if (bt1
== VT_FUNC
) {
2099 mk_pointer(&vtop
->type
);
2104 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2105 /* at least one operand is a pointer */
2106 /* relational op: must be both pointers */
2107 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2108 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2109 /* pointers are handled are unsigned */
2111 t
= VT_LLONG
| VT_UNSIGNED
;
2113 t
= VT_INT
| VT_UNSIGNED
;
2117 /* if both pointers, then it must be the '-' op */
2118 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2120 tcc_error("cannot use pointers here");
2121 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2122 /* XXX: check that types are compatible */
2123 if (vtop
[-1].type
.t
& VT_VLA
) {
2124 vla_runtime_pointed_size(&vtop
[-1].type
);
2126 vpushi(pointed_size(&vtop
[-1].type
));
2130 /* set to integer type */
2132 vtop
->type
.t
= VT_LLONG
;
2134 vtop
->type
.t
= VT_INT
;
2139 /* exactly one pointer : must be '+' or '-'. */
2140 if (op
!= '-' && op
!= '+')
2141 tcc_error("cannot use pointers here");
2142 /* Put pointer as first operand */
2143 if (bt2
== VT_PTR
) {
2145 t
= t1
, t1
= t2
, t2
= t
;
2148 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2149 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2152 type1
= vtop
[-1].type
;
2153 type1
.t
&= ~VT_ARRAY
;
2154 if (vtop
[-1].type
.t
& VT_VLA
)
2155 vla_runtime_pointed_size(&vtop
[-1].type
);
2157 u
= pointed_size(&vtop
[-1].type
);
2159 tcc_error("unknown array element size");
2163 /* XXX: cast to int ? (long long case) */
2169 /* #ifdef CONFIG_TCC_BCHECK
2170 The main reason to removing this code:
2177 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2178 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2180 When this code is on. then the output looks like
2182 v+(i-j) = 0xbff84000
2184 /* if evaluating constant expression, no code should be
2185 generated, so no bound check */
2186 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2187 /* if bounded pointers, we generate a special code to
2194 gen_bounded_ptr_add();
2200 /* put again type if gen_opic() swaped operands */
2203 } else if (is_float(bt1
) || is_float(bt2
)) {
2204 /* compute bigger type and do implicit casts */
2205 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2207 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2212 /* floats can only be used for a few operations */
2213 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2214 (op
< TOK_ULT
|| op
> TOK_GT
))
2215 tcc_error("invalid operands for binary operation");
2217 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2218 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2219 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2222 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2223 /* cast to biggest op */
2225 /* check if we need to keep type as long or as long long */
2226 if ((t1
& VT_LONG
&& (t2
& (VT_BTYPE
| VT_LONG
)) != VT_LLONG
) ||
2227 (t2
& VT_LONG
&& (t1
& (VT_BTYPE
| VT_LONG
)) != VT_LLONG
))
2229 /* convert to unsigned if it does not fit in a long long */
2230 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2231 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2235 /* integer operations */
2238 if ((t1
& VT_LONG
) || (t2
& VT_LONG
))
2241 /* convert to unsigned if it does not fit in an integer */
2242 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2243 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2246 /* XXX: currently, some unsigned operations are explicit, so
2247 we modify them here */
2248 if (t
& VT_UNSIGNED
) {
2255 else if (op
== TOK_LT
)
2257 else if (op
== TOK_GT
)
2259 else if (op
== TOK_LE
)
2261 else if (op
== TOK_GE
)
2269 /* special case for shifts and long long: we keep the shift as
2271 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2278 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2279 /* relational op: the result is an int */
2280 vtop
->type
.t
= VT_INT
;
2285 // Make sure that we have converted to an rvalue:
2286 if (vtop
->r
& VT_LVAL
)
2287 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2290 #ifndef TCC_TARGET_ARM
2291 /* generic itof for unsigned long long case */
2292 static void gen_cvt_itof1(int t
)
2294 #ifdef TCC_TARGET_ARM64
2297 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2298 (VT_LLONG
| VT_UNSIGNED
)) {
2301 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2302 #if LDOUBLE_SIZE != 8
2303 else if (t
== VT_LDOUBLE
)
2304 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2307 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2311 vtop
->r
= reg_fret(t
);
2319 /* generic ftoi for unsigned long long case */
2320 static void gen_cvt_ftoi1(int t
)
2322 #ifdef TCC_TARGET_ARM64
2327 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2328 /* not handled natively */
2329 st
= vtop
->type
.t
& VT_BTYPE
;
2331 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2332 #if LDOUBLE_SIZE != 8
2333 else if (st
== VT_LDOUBLE
)
2334 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2337 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2342 vtop
->r2
= REG_LRET
;
2349 /* force char or short cast */
2350 static void force_charshort_cast(int t
)
2354 /* cannot cast static initializers */
2355 if (STATIC_DATA_WANTED
)
2359 /* XXX: add optimization if lvalue : just change type and offset */
2364 if (t
& VT_UNSIGNED
) {
2365 vpushi((1 << bits
) - 1);
2368 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2374 /* result must be signed or the SAR is converted to an SHL
2375 This was not the case when "t" was a signed short
2376 and the last value on the stack was an unsigned int */
2377 vtop
->type
.t
&= ~VT_UNSIGNED
;
2383 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2384 static void gen_cast_s(int t
)
2392 static void gen_cast(CType
*type
)
2394 int sbt
, dbt
, sf
, df
, c
, p
;
2396 /* special delayed cast for char/short */
2397 /* XXX: in some cases (multiple cascaded casts), it may still
2399 if (vtop
->r
& VT_MUSTCAST
) {
2400 vtop
->r
&= ~VT_MUSTCAST
;
2401 force_charshort_cast(vtop
->type
.t
);
2404 /* bitfields first get cast to ints */
2405 if (vtop
->type
.t
& VT_BITFIELD
) {
2409 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2410 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2415 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2416 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2418 /* constant case: we can do it now */
2419 /* XXX: in ISOC, cannot do it if error in convert */
2420 if (sbt
== VT_FLOAT
)
2421 vtop
->c
.ld
= vtop
->c
.f
;
2422 else if (sbt
== VT_DOUBLE
)
2423 vtop
->c
.ld
= vtop
->c
.d
;
2426 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2427 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2428 vtop
->c
.ld
= vtop
->c
.i
;
2430 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2432 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2433 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2435 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2438 if (dbt
== VT_FLOAT
)
2439 vtop
->c
.f
= (float)vtop
->c
.ld
;
2440 else if (dbt
== VT_DOUBLE
)
2441 vtop
->c
.d
= (double)vtop
->c
.ld
;
2442 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2443 vtop
->c
.i
= vtop
->c
.ld
;
2444 } else if (sf
&& dbt
== VT_BOOL
) {
2445 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2448 vtop
->c
.i
= vtop
->c
.ld
;
2449 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2451 else if (sbt
& VT_UNSIGNED
)
2452 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2454 else if (sbt
== VT_PTR
)
2457 else if (sbt
!= VT_LLONG
)
2458 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2459 -(vtop
->c
.i
& 0x80000000));
2461 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2463 else if (dbt
== VT_BOOL
)
2464 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2466 else if (dbt
== VT_PTR
)
2469 else if (dbt
!= VT_LLONG
) {
2470 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2471 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2474 if (!(dbt
& VT_UNSIGNED
))
2475 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2478 } else if (p
&& dbt
== VT_BOOL
) {
2482 /* non constant case: generate code */
2484 /* convert from fp to fp */
2487 /* convert int to fp */
2490 /* convert fp to int */
2491 if (dbt
== VT_BOOL
) {
2495 /* we handle char/short/etc... with generic code */
2496 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2497 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2501 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2502 /* additional cast for char/short... */
2508 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2509 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2510 /* scalar to long long */
2511 /* machine independent conversion */
2513 /* generate high word */
2514 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2518 if (sbt
== VT_PTR
) {
2519 /* cast from pointer to int before we apply
2520 shift operation, which pointers don't support*/
2527 /* patch second register */
2528 vtop
[-1].r2
= vtop
->r
;
2532 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2533 (dbt
& VT_BTYPE
) == VT_PTR
||
2534 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2535 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2536 (sbt
& VT_BTYPE
) != VT_PTR
&&
2537 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2538 /* need to convert from 32bit to 64bit */
2540 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2541 #if defined(TCC_TARGET_ARM64)
2543 #elif defined(TCC_TARGET_X86_64)
2545 /* x86_64 specific: movslq */
2547 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2554 } else if (dbt
== VT_BOOL
) {
2555 /* scalar to bool */
2558 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2559 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2560 if (sbt
== VT_PTR
) {
2561 vtop
->type
.t
= VT_INT
;
2562 tcc_warning("nonportable conversion from pointer to char/short");
2564 force_charshort_cast(dbt
);
2566 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2568 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2569 /* from long long: just take low order word */
2573 /* if lvalue and single word type, nothing to do because
2574 the lvalue already contains the real type size (see
2575 VT_LVAL_xxx constants) */
2579 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2580 /* if we are casting between pointer types,
2581 we must update the VT_LVAL_xxx size */
2582 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2583 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2588 /* return type size as known at compile time. Put alignment at 'a' */
2589 ST_FUNC
int type_size(CType
*type
, int *a
)
2594 bt
= type
->t
& VT_BTYPE
;
2595 if (bt
== VT_STRUCT
) {
2600 } else if (bt
== VT_PTR
) {
2601 if (type
->t
& VT_ARRAY
) {
2605 ts
= type_size(&s
->type
, a
);
2607 if (ts
< 0 && s
->c
< 0)
2615 } else if (IS_ENUM(type
->t
) && type
->ref
->c
== -1) {
2616 return -1; /* incomplete enum */
2617 } else if (bt
== VT_LDOUBLE
) {
2619 return LDOUBLE_SIZE
;
2620 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2621 #ifdef TCC_TARGET_I386
2622 #ifdef TCC_TARGET_PE
2627 #elif defined(TCC_TARGET_ARM)
2637 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2640 } else if (bt
== VT_SHORT
) {
2643 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2647 /* char, void, function, _Bool */
2653 /* push type size as known at runtime time on top of value stack. Put
2655 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2657 if (type
->t
& VT_VLA
) {
2658 type_size(&type
->ref
->type
, a
);
2659 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2661 vpushi(type_size(type
, a
));
2665 static void vla_sp_restore(void) {
2666 if (vlas_in_scope
) {
2667 gen_vla_sp_restore(vla_sp_loc
);
2671 static void vla_sp_restore_root(void) {
2672 if (vlas_in_scope
) {
2673 gen_vla_sp_restore(vla_sp_root_loc
);
2677 /* return the pointed type of t */
2678 static inline CType
*pointed_type(CType
*type
)
2680 return &type
->ref
->type
;
2683 /* modify type so that its it is a pointer to type. */
2684 ST_FUNC
void mk_pointer(CType
*type
)
2687 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2688 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2692 /* compare function types. OLD functions match any new functions */
2693 static int is_compatible_func(CType
*type1
, CType
*type2
)
2699 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2701 /* check func_call */
2702 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2704 /* XXX: not complete */
2705 if (s1
->f
.func_type
== FUNC_OLD
|| s2
->f
.func_type
== FUNC_OLD
)
2707 if (s1
->f
.func_type
!= s2
->f
.func_type
)
2709 while (s1
!= NULL
) {
2712 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2722 /* return true if type1 and type2 are the same. If unqualified is
2723 true, qualifiers on the types are ignored.
2725 - enums are not checked as gcc __builtin_types_compatible_p ()
2727 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2731 t1
= type1
->t
& VT_TYPE
;
2732 t2
= type2
->t
& VT_TYPE
;
2734 /* strip qualifiers before comparing */
2735 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2736 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2739 /* Default Vs explicit signedness only matters for char */
2740 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2745 /* XXX: bitfields ? */
2748 /* test more complicated cases */
2749 bt1
= t1
& VT_BTYPE
;
2750 if (bt1
== VT_PTR
) {
2751 type1
= pointed_type(type1
);
2752 type2
= pointed_type(type2
);
2753 return is_compatible_types(type1
, type2
);
2754 } else if (bt1
== VT_STRUCT
) {
2755 return (type1
->ref
== type2
->ref
);
2756 } else if (bt1
== VT_FUNC
) {
2757 return is_compatible_func(type1
, type2
);
2763 /* return true if type1 and type2 are exactly the same (including
2766 static int is_compatible_types(CType
*type1
, CType
*type2
)
2768 return compare_types(type1
,type2
,0);
2771 /* return true if type1 and type2 are the same (ignoring qualifiers).
2773 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
2775 return compare_types(type1
,type2
,1);
2778 /* print a type. If 'varstr' is not NULL, then the variable is also
2779 printed in the type */
2781 /* XXX: add array and function pointers */
2782 static void type_to_str(char *buf
, int buf_size
,
2783 CType
*type
, const char *varstr
)
2793 if (t
& VT_CONSTANT
)
2794 pstrcat(buf
, buf_size
, "const ");
2795 if (t
& VT_VOLATILE
)
2796 pstrcat(buf
, buf_size
, "volatile ");
2797 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2798 pstrcat(buf
, buf_size
, "unsigned ");
2799 else if (t
& VT_DEFSIGN
)
2800 pstrcat(buf
, buf_size
, "signed ");
2802 pstrcat(buf
, buf_size
, "extern ");
2804 pstrcat(buf
, buf_size
, "static ");
2806 pstrcat(buf
, buf_size
, "typedef ");
2808 pstrcat(buf
, buf_size
, "inline ");
2809 buf_size
-= strlen(buf
);
2816 if (!bt
&& VT_LONG
& t
) {
2847 tstr
= "long double";
2849 pstrcat(buf
, buf_size
, tstr
);
2856 pstrcat(buf
, buf_size
, tstr
);
2857 v
= type
->ref
->v
& ~SYM_STRUCT
;
2858 if (v
>= SYM_FIRST_ANOM
)
2859 pstrcat(buf
, buf_size
, "<anonymous>");
2861 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2865 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2866 pstrcat(buf
, buf_size
, "(");
2868 while (sa
!= NULL
) {
2869 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2870 pstrcat(buf
, buf_size
, buf1
);
2873 pstrcat(buf
, buf_size
, ", ");
2875 pstrcat(buf
, buf_size
, ")");
2880 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
2881 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2884 pstrcpy(buf1
, sizeof(buf1
), "*");
2885 if (t
& VT_CONSTANT
)
2886 pstrcat(buf1
, buf_size
, "const ");
2887 if (t
& VT_VOLATILE
)
2888 pstrcat(buf1
, buf_size
, "volatile ");
2890 pstrcat(buf1
, sizeof(buf1
), varstr
);
2891 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2895 pstrcat(buf
, buf_size
, " ");
2896 pstrcat(buf
, buf_size
, varstr
);
2901 /* verify type compatibility to store vtop in 'dt' type, and generate
2903 static void gen_assign_cast(CType
*dt
)
2905 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2906 char buf1
[256], buf2
[256];
2909 st
= &vtop
->type
; /* source type */
2910 dbt
= dt
->t
& VT_BTYPE
;
2911 sbt
= st
->t
& VT_BTYPE
;
2912 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2913 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2915 It is Ok if both are void
2921 gcc accepts this program
2924 tcc_error("cannot cast from/to void");
2926 if (dt
->t
& VT_CONSTANT
)
2927 tcc_warning("assignment of read-only location");
2930 /* special cases for pointers */
2931 /* '0' can also be a pointer */
2932 if (is_null_pointer(vtop
))
2934 /* accept implicit pointer to integer cast with warning */
2935 if (is_integer_btype(sbt
)) {
2936 tcc_warning("assignment makes pointer from integer without a cast");
2939 type1
= pointed_type(dt
);
2940 /* a function is implicitly a function pointer */
2941 if (sbt
== VT_FUNC
) {
2942 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2943 !is_compatible_types(pointed_type(dt
), st
))
2944 tcc_warning("assignment from incompatible pointer type");
2949 type2
= pointed_type(st
);
2950 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2951 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2952 /* void * can match anything */
2954 //printf("types %08x %08x\n", type1->t, type2->t);
2955 /* exact type match, except for qualifiers */
2956 if (!is_compatible_unqualified_types(type1
, type2
)) {
2957 /* Like GCC don't warn by default for merely changes
2958 in pointer target signedness. Do warn for different
2959 base types, though, in particular for unsigned enums
2960 and signed int targets. */
2961 if ((type1
->t
& VT_BTYPE
) != (type2
->t
& VT_BTYPE
)
2962 || IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)
2964 tcc_warning("assignment from incompatible pointer type");
2967 /* check const and volatile */
2968 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2969 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2970 tcc_warning("assignment discards qualifiers from pointer target type");
2976 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2977 tcc_warning("assignment makes integer from pointer without a cast");
2978 } else if (sbt
== VT_STRUCT
) {
2979 goto case_VT_STRUCT
;
2981 /* XXX: more tests */
2987 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2988 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2989 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2991 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2992 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2993 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
3001 /* store vtop in lvalue pushed on stack */
3002 ST_FUNC
void vstore(void)
3004 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
3006 ft
= vtop
[-1].type
.t
;
3007 sbt
= vtop
->type
.t
& VT_BTYPE
;
3008 dbt
= ft
& VT_BTYPE
;
3009 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
3010 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
3011 && !(vtop
->type
.t
& VT_BITFIELD
)) {
3012 /* optimize char/short casts */
3013 delayed_cast
= VT_MUSTCAST
;
3014 vtop
->type
.t
= ft
& VT_TYPE
;
3015 /* XXX: factorize */
3016 if (ft
& VT_CONSTANT
)
3017 tcc_warning("assignment of read-only location");
3020 if (!(ft
& VT_BITFIELD
))
3021 gen_assign_cast(&vtop
[-1].type
);
3024 if (sbt
== VT_STRUCT
) {
3025 /* if structure, only generate pointer */
3026 /* structure assignment : generate memcpy */
3027 /* XXX: optimize if small size */
3028 size
= type_size(&vtop
->type
, &align
);
3032 vtop
->type
.t
= VT_PTR
;
3035 /* address of memcpy() */
3038 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3039 else if(!(align
& 3))
3040 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3043 /* Use memmove, rather than memcpy, as dest and src may be same: */
3044 vpush_global_sym(&func_old_type
, TOK_memmove
);
3049 vtop
->type
.t
= VT_PTR
;
3055 /* leave source on stack */
3056 } else if (ft
& VT_BITFIELD
) {
3057 /* bitfield store handling */
3059 /* save lvalue as expression result (example: s.b = s.a = n;) */
3060 vdup(), vtop
[-1] = vtop
[-2];
3062 bit_pos
= BIT_POS(ft
);
3063 bit_size
= BIT_SIZE(ft
);
3064 /* remove bit field info to avoid loops */
3065 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3067 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3068 gen_cast(&vtop
[-1].type
);
3069 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3072 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3073 if (r
== VT_STRUCT
) {
3074 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG
? VT_LLONG
: VT_INT
);
3075 store_packed_bf(bit_pos
, bit_size
);
3077 unsigned long long mask
= (1ULL << bit_size
) - 1;
3078 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3080 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3083 vpushi((unsigned)mask
);
3090 /* duplicate destination */
3093 /* load destination, mask and or with source */
3094 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3095 vpushll(~(mask
<< bit_pos
));
3097 vpushi(~((unsigned)mask
<< bit_pos
));
3102 /* ... and discard */
3106 #ifdef CONFIG_TCC_BCHECK
3107 /* bound check case */
3108 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3117 #ifdef TCC_TARGET_X86_64
3118 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3120 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3125 r
= gv(rc
); /* generate value */
3126 /* if lvalue was saved on stack, must read it */
3127 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3129 t
= get_reg(RC_INT
);
3135 sv
.r
= VT_LOCAL
| VT_LVAL
;
3136 sv
.c
.i
= vtop
[-1].c
.i
;
3138 vtop
[-1].r
= t
| VT_LVAL
;
3140 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3142 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3143 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3145 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3146 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3148 vtop
[-1].type
.t
= load_type
;
3151 /* convert to int to increment easily */
3152 vtop
->type
.t
= addr_type
;
3158 vtop
[-1].type
.t
= load_type
;
3159 /* XXX: it works because r2 is spilled last ! */
3160 store(vtop
->r2
, vtop
- 1);
3166 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3167 vtop
->r
|= delayed_cast
;
3171 /* post defines POST/PRE add. c is the token ++ or -- */
3172 ST_FUNC
void inc(int post
, int c
)
3175 vdup(); /* save lvalue */
3177 gv_dup(); /* duplicate value */
3182 vpushi(c
- TOK_MID
);
3184 vstore(); /* store value */
3186 vpop(); /* if post op, return saved value */
3189 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3191 /* read the string */
3195 while (tok
== TOK_STR
) {
3196 /* XXX: add \0 handling too ? */
3197 cstr_cat(astr
, tokc
.str
.data
, -1);
3200 cstr_ccat(astr
, '\0');
3203 /* If I is >= 1 and a power of two, returns log2(i)+1.
3204 If I is 0 returns 0. */
3205 static int exact_log2p1(int i
)
3210 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3221 /* Parse __attribute__((...)) GNUC extension. */
3222 static void parse_attribute(AttributeDef
*ad
)
3228 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3233 while (tok
!= ')') {
3234 if (tok
< TOK_IDENT
)
3235 expect("attribute name");
3242 parse_mult_str(&astr
, "section name");
3243 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3250 parse_mult_str(&astr
, "alias(\"target\")");
3251 ad
->alias_target
= /* save string as token, for later */
3252 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3256 case TOK_VISIBILITY1
:
3257 case TOK_VISIBILITY2
:
3259 parse_mult_str(&astr
,
3260 "visibility(\"default|hidden|internal|protected\")");
3261 if (!strcmp (astr
.data
, "default"))
3262 ad
->a
.visibility
= STV_DEFAULT
;
3263 else if (!strcmp (astr
.data
, "hidden"))
3264 ad
->a
.visibility
= STV_HIDDEN
;
3265 else if (!strcmp (astr
.data
, "internal"))
3266 ad
->a
.visibility
= STV_INTERNAL
;
3267 else if (!strcmp (astr
.data
, "protected"))
3268 ad
->a
.visibility
= STV_PROTECTED
;
3270 expect("visibility(\"default|hidden|internal|protected\")");
3279 if (n
<= 0 || (n
& (n
- 1)) != 0)
3280 tcc_error("alignment must be a positive power of two");
3285 ad
->a
.aligned
= exact_log2p1(n
);
3286 if (n
!= 1 << (ad
->a
.aligned
- 1))
3287 tcc_error("alignment of %d is larger than implemented", n
);
3299 /* currently, no need to handle it because tcc does not
3300 track unused objects */
3304 /* currently, no need to handle it because tcc does not
3305 track unused objects */
3310 ad
->f
.func_call
= FUNC_CDECL
;
3315 ad
->f
.func_call
= FUNC_STDCALL
;
3317 #ifdef TCC_TARGET_I386
3327 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3333 ad
->f
.func_call
= FUNC_FASTCALLW
;
3340 ad
->attr_mode
= VT_LLONG
+ 1;
3343 ad
->attr_mode
= VT_BYTE
+ 1;
3346 ad
->attr_mode
= VT_SHORT
+ 1;
3350 ad
->attr_mode
= VT_INT
+ 1;
3353 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3360 ad
->a
.dllexport
= 1;
3363 ad
->a
.dllimport
= 1;
3366 if (tcc_state
->warn_unsupported
)
3367 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3368 /* skip parameters */
3370 int parenthesis
= 0;
3374 else if (tok
== ')')
3377 } while (parenthesis
&& tok
!= -1);
3390 static Sym
* find_field (CType
*type
, int v
)
3394 while ((s
= s
->next
) != NULL
) {
3395 if ((s
->v
& SYM_FIELD
) &&
3396 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3397 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3398 Sym
*ret
= find_field (&s
->type
, v
);
3408 static void struct_add_offset (Sym
*s
, int offset
)
3410 while ((s
= s
->next
) != NULL
) {
3411 if ((s
->v
& SYM_FIELD
) &&
3412 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3413 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3414 struct_add_offset(s
->type
.ref
, offset
);
3420 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3422 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3423 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3424 int pcc
= !tcc_state
->ms_bitfields
;
3425 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3432 prevbt
= VT_STRUCT
; /* make it never match */
3437 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3438 if (f
->type
.t
& VT_BITFIELD
)
3439 bit_size
= BIT_SIZE(f
->type
.t
);
3442 size
= type_size(&f
->type
, &align
);
3443 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3446 if (pcc
&& bit_size
== 0) {
3447 /* in pcc mode, packing does not affect zero-width bitfields */
3450 /* in pcc mode, attribute packed overrides if set. */
3451 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3454 /* pragma pack overrides align if lesser and packs bitfields always */
3457 if (pragma_pack
< align
)
3458 align
= pragma_pack
;
3459 /* in pcc mode pragma pack also overrides individual align */
3460 if (pcc
&& pragma_pack
< a
)
3464 /* some individual align was specified */
3468 if (type
->ref
->type
.t
== VT_UNION
) {
3469 if (pcc
&& bit_size
>= 0)
3470 size
= (bit_size
+ 7) >> 3;
3475 } else if (bit_size
< 0) {
3477 c
+= (bit_pos
+ 7) >> 3;
3478 c
= (c
+ align
- 1) & -align
;
3487 /* A bit-field. Layout is more complicated. There are two
3488 options: PCC (GCC) compatible and MS compatible */
3490 /* In PCC layout a bit-field is placed adjacent to the
3491 preceding bit-fields, except if:
3493 - an individual alignment was given
3494 - it would overflow its base type container and
3495 there is no packing */
3496 if (bit_size
== 0) {
3498 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3500 } else if (f
->a
.aligned
) {
3502 } else if (!packed
) {
3504 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3505 if (ofs
> size
/ align
)
3509 /* in pcc mode, long long bitfields have type int if they fit */
3510 if (size
== 8 && bit_size
<= 32)
3511 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3513 while (bit_pos
>= align
* 8)
3514 c
+= align
, bit_pos
-= align
* 8;
3517 /* In PCC layout named bit-fields influence the alignment
3518 of the containing struct using the base types alignment,
3519 except for packed fields (which here have correct align). */
3520 if (f
->v
& SYM_FIRST_ANOM
3521 // && bit_size // ??? gcc on ARM/rpi does that
3526 bt
= f
->type
.t
& VT_BTYPE
;
3527 if ((bit_pos
+ bit_size
> size
* 8)
3528 || (bit_size
> 0) == (bt
!= prevbt
)
3530 c
= (c
+ align
- 1) & -align
;
3533 /* In MS bitfield mode a bit-field run always uses
3534 at least as many bits as the underlying type.
3535 To start a new run it's also required that this
3536 or the last bit-field had non-zero width. */
3537 if (bit_size
|| prev_bit_size
)
3540 /* In MS layout the records alignment is normally
3541 influenced by the field, except for a zero-width
3542 field at the start of a run (but by further zero-width
3543 fields it is again). */
3544 if (bit_size
== 0 && prevbt
!= bt
)
3547 prev_bit_size
= bit_size
;
3550 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3551 | (bit_pos
<< VT_STRUCT_SHIFT
);
3552 bit_pos
+= bit_size
;
3554 if (align
> maxalign
)
3558 printf("set field %s offset %-2d size %-2d align %-2d",
3559 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3560 if (f
->type
.t
& VT_BITFIELD
) {
3561 printf(" pos %-2d bits %-2d",
3569 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3571 /* An anonymous struct/union. Adjust member offsets
3572 to reflect the real offset of our containing struct.
3573 Also set the offset of this anon member inside
3574 the outer struct to be zero. Via this it
3575 works when accessing the field offset directly
3576 (from base object), as well as when recursing
3577 members in initializer handling. */
3578 int v2
= f
->type
.ref
->v
;
3579 if (!(v2
& SYM_FIELD
) &&
3580 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3582 /* This happens only with MS extensions. The
3583 anon member has a named struct type, so it
3584 potentially is shared with other references.
3585 We need to unshare members so we can modify
3588 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3589 &f
->type
.ref
->type
, 0,
3591 pps
= &f
->type
.ref
->next
;
3592 while ((ass
= ass
->next
) != NULL
) {
3593 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3594 pps
= &((*pps
)->next
);
3598 struct_add_offset(f
->type
.ref
, offset
);
3608 c
+= (bit_pos
+ 7) >> 3;
3610 /* store size and alignment */
3611 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3615 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
3616 /* can happen if individual align for some member was given. In
3617 this case MSVC ignores maxalign when aligning the size */
3622 c
= (c
+ a
- 1) & -a
;
3626 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3629 /* check whether we can access bitfields by their type */
3630 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3634 if (0 == (f
->type
.t
& VT_BITFIELD
))
3638 bit_size
= BIT_SIZE(f
->type
.t
);
3641 bit_pos
= BIT_POS(f
->type
.t
);
3642 size
= type_size(&f
->type
, &align
);
3643 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3646 /* try to access the field using a differnt type */
3647 c0
= -1, s
= align
= 1;
3649 px
= f
->c
* 8 + bit_pos
;
3650 cx
= (px
>> 3) & -align
;
3651 px
= px
- (cx
<< 3);
3654 s
= (px
+ bit_size
+ 7) >> 3;
3664 s
= type_size(&t
, &align
);
3668 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3669 /* update offset and bit position */
3672 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3673 | (bit_pos
<< VT_STRUCT_SHIFT
);
3677 printf("FIX field %s offset %-2d size %-2d align %-2d "
3678 "pos %-2d bits %-2d\n",
3679 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3680 cx
, s
, align
, px
, bit_size
);
3683 /* fall back to load/store single-byte wise */
3684 f
->auxtype
= VT_STRUCT
;
3686 printf("FIX field %s : load byte-wise\n",
3687 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3693 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3694 static void struct_decl(CType
*type
, int u
)
3696 int v
, c
, size
, align
, flexible
;
3697 int bit_size
, bsize
, bt
;
3699 AttributeDef ad
, ad1
;
3702 memset(&ad
, 0, sizeof ad
);
3704 parse_attribute(&ad
);
3708 /* struct already defined ? return it */
3710 expect("struct/union/enum name");
3712 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3715 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3717 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3722 /* Record the original enum/struct/union token. */
3723 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3725 /* we put an undefined size for struct/union */
3726 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3727 s
->r
= 0; /* default alignment is zero as gcc */
3729 type
->t
= s
->type
.t
;
3735 tcc_error("struct/union/enum already defined");
3736 /* cannot be empty */
3737 /* non empty enums are not allowed */
3740 long long ll
= 0, pl
= 0, nl
= 0;
3743 /* enum symbols have static storage */
3744 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3748 expect("identifier");
3750 if (ss
&& !local_stack
)
3751 tcc_error("redefinition of enumerator '%s'",
3752 get_tok_str(v
, NULL
));
3756 ll
= expr_const64();
3758 ss
= sym_push(v
, &t
, VT_CONST
, 0);
3760 *ps
= ss
, ps
= &ss
->next
;
3769 /* NOTE: we accept a trailing comma */
3774 /* set integral type of the enum */
3777 if (pl
!= (unsigned)pl
)
3780 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
3782 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
3784 /* set type for enum members */
3785 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
3787 if (ll
== (int)ll
) /* default is int if it fits */
3789 if (t
.t
& VT_UNSIGNED
) {
3790 ss
->type
.t
|= VT_UNSIGNED
;
3791 if (ll
== (unsigned)ll
)
3794 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
) | VT_LLONG
;
3799 while (tok
!= '}') {
3800 if (!parse_btype(&btype
, &ad1
)) {
3806 tcc_error("flexible array member '%s' not at the end of struct",
3807 get_tok_str(v
, NULL
));
3813 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
3815 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3816 expect("identifier");
3818 int v
= btype
.ref
->v
;
3819 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3820 if (tcc_state
->ms_extensions
== 0)
3821 expect("identifier");
3825 if (type_size(&type1
, &align
) < 0) {
3826 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3829 tcc_error("field '%s' has incomplete type",
3830 get_tok_str(v
, NULL
));
3832 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3833 (type1
.t
& VT_STORAGE
))
3834 tcc_error("invalid type for '%s'",
3835 get_tok_str(v
, NULL
));
3839 bit_size
= expr_const();
3840 /* XXX: handle v = 0 case for messages */
3842 tcc_error("negative width in bit-field '%s'",
3843 get_tok_str(v
, NULL
));
3844 if (v
&& bit_size
== 0)
3845 tcc_error("zero width for bit-field '%s'",
3846 get_tok_str(v
, NULL
));
3847 parse_attribute(&ad1
);
3849 size
= type_size(&type1
, &align
);
3850 if (bit_size
>= 0) {
3851 bt
= type1
.t
& VT_BTYPE
;
3857 tcc_error("bitfields must have scalar type");
3859 if (bit_size
> bsize
) {
3860 tcc_error("width of '%s' exceeds its type",
3861 get_tok_str(v
, NULL
));
3862 } else if (bit_size
== bsize
3863 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
3864 /* no need for bit fields */
3866 } else if (bit_size
== 64) {
3867 tcc_error("field width 64 not implemented");
3869 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
3871 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3874 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3875 /* Remember we've seen a real field to check
3876 for placement of flexible array member. */
3879 /* If member is a struct or bit-field, enforce
3880 placing into the struct (as anonymous). */
3882 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3887 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
3892 if (tok
== ';' || tok
== TOK_EOF
)
3899 parse_attribute(&ad
);
3900 struct_layout(type
, &ad
);
3905 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
3907 if (s
->a
.aligned
&& 0 == ad
->a
.aligned
)
3908 ad
->a
.aligned
= s
->a
.aligned
;
3909 if (s
->f
.func_call
&& 0 == ad
->f
.func_call
)
3910 ad
->f
.func_call
= s
->f
.func_call
;
3911 if (s
->f
.func_type
&& 0 == ad
->f
.func_type
)
3912 ad
->f
.func_type
= s
->f
.func_type
;
3917 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3918 are added to the element type, copied because it could be a typedef. */
3919 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3921 while (type
->t
& VT_ARRAY
) {
3922 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3923 type
= &type
->ref
->type
;
3925 type
->t
|= qualifiers
;
3928 /* return 0 if no type declaration. otherwise, return the basic type
3931 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3933 int t
, u
, bt
, st
, type_found
, typespec_found
, g
;
3937 memset(ad
, 0, sizeof(AttributeDef
));
3947 /* currently, we really ignore extension */
3957 if (u
== VT_SHORT
) {
3958 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
3959 tmbt
: tcc_error("too many basic types");
3962 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
3967 t
= (t
& ~VT_BTYPE
) | u
;
3980 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3981 #ifndef TCC_TARGET_PE
3982 t
= (t
& ~(VT_LONG
| VT_BTYPE
)) | VT_LDOUBLE
;
3984 } else if (t
& VT_LONG
) {
3985 t
= (t
& ~(VT_LONG
| VT_BTYPE
)) | VT_LLONG
;
3992 #ifdef TCC_TARGET_ARM64
3994 /* GCC's __uint128_t appears in some Linux header files. Make it a
3995 synonym for long double to get the size and alignment right. */
4007 #ifdef TCC_TARGET_PE
4008 t
= (t
& ~(VT_LONG
| VT_BTYPE
)) | VT_DOUBLE
;
4010 t
= (t
& ~(VT_LONG
| VT_BTYPE
)) | VT_LDOUBLE
;
4019 struct_decl(&type1
, VT_ENUM
);
4022 type
->ref
= type1
.ref
;
4025 struct_decl(&type1
, VT_STRUCT
);
4028 struct_decl(&type1
, VT_UNION
);
4031 /* type modifiers */
4036 parse_btype_qualify(type
, VT_CONSTANT
);
4044 parse_btype_qualify(type
, VT_VOLATILE
);
4051 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4052 tcc_error("signed and unsigned modifier");
4065 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4066 tcc_error("signed and unsigned modifier");
4067 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4083 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4084 tcc_error("multiple storage classes");
4095 /* GNUC attribute */
4096 case TOK_ATTRIBUTE1
:
4097 case TOK_ATTRIBUTE2
:
4098 parse_attribute(ad
);
4099 if (ad
->attr_mode
) {
4100 u
= ad
->attr_mode
-1;
4101 t
= (t
& ~VT_BTYPE
) | u
;
4109 parse_expr_type(&type1
);
4110 /* remove all storage modifiers except typedef */
4111 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4113 sym_to_attr(ad
, type1
.ref
);
4119 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4122 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4123 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4124 type
->ref
= s
->type
.ref
;
4126 parse_btype_qualify(type
, t
);
4128 /* get attributes from typedef */
4138 if (tcc_state
->char_is_unsigned
) {
4139 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4143 /* long is never used as type */
4145 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
4146 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
4148 t
= (t
& ~VT_BTYPE
) | VT_INT
;
4154 /* convert a function parameter type (array to pointer and function to
4155 function pointer) */
4156 static inline void convert_parameter_type(CType
*pt
)
4158 /* remove const and volatile qualifiers (XXX: const could be used
4159 to indicate a const function parameter */
4160 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4161 /* array must be transformed to pointer according to ANSI C */
4163 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4168 ST_FUNC
void parse_asm_str(CString
*astr
)
4171 parse_mult_str(astr
, "string constant");
4174 /* Parse an asm label and return the token */
4175 static int asm_label_instr(void)
4181 parse_asm_str(&astr
);
4184 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4186 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4191 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4193 int n
, l
, t1
, arg_size
, align
;
4194 Sym
**plast
, *s
, *first
;
4199 /* function type, or recursive declarator (return if so) */
4201 if (td
&& !(td
& TYPE_ABSTRACT
))
4205 else if (parse_btype(&pt
, &ad1
))
4216 /* read param name and compute offset */
4217 if (l
!= FUNC_OLD
) {
4218 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4220 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4221 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4222 tcc_error("parameter declared as void");
4223 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4227 expect("identifier");
4228 pt
.t
= VT_VOID
; /* invalid type */
4231 convert_parameter_type(&pt
);
4232 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4238 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4243 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4244 tcc_error("invalid type");
4247 /* if no parameters, then old type prototype */
4250 /* NOTE: const is ignored in returned type as it has a special
4251 meaning in gcc / C++ */
4252 type
->t
&= ~VT_CONSTANT
;
4253 /* some ancient pre-K&R C allows a function to return an array
4254 and the array brackets to be put after the arguments, such
4255 that "int c()[]" means something like "int[] c()" */
4258 skip(']'); /* only handle simple "[]" */
4261 /* we push a anonymous symbol which will contain the function prototype */
4262 ad
->f
.func_args
= arg_size
;
4263 ad
->f
.func_type
= l
;
4264 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4270 } else if (tok
== '[') {
4271 int saved_nocode_wanted
= nocode_wanted
;
4272 /* array definition */
4274 if (tok
== TOK_RESTRICT1
)
4279 if (!local_stack
|| (storage
& VT_STATIC
))
4280 vpushi(expr_const());
4282 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4283 length must always be evaluated, even under nocode_wanted,
4284 so that its size slot is initialized (e.g. under sizeof
4289 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4292 tcc_error("invalid array size");
4294 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4295 tcc_error("size of variable length array should be an integer");
4300 /* parse next post type */
4301 post_type(type
, ad
, storage
, 0);
4302 if (type
->t
== VT_FUNC
)
4303 tcc_error("declaration of an array of functions");
4304 t1
|= type
->t
& VT_VLA
;
4307 loc
-= type_size(&int_type
, &align
);
4311 vla_runtime_type_size(type
, &align
);
4313 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4319 nocode_wanted
= saved_nocode_wanted
;
4321 /* we push an anonymous symbol which will contain the array
4323 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4324 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4330 /* Parse a type declarator (except basic type), and return the type
4331 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4332 expected. 'type' should contain the basic type. 'ad' is the
4333 attribute definition of the basic type. It can be modified by
4334 type_decl(). If this (possibly abstract) declarator is a pointer chain
4335 it returns the innermost pointed to type (equals *type, but is a different
4336 pointer), otherwise returns type itself, that's used for recursive calls. */
4337 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4340 int qualifiers
, storage
;
4342 /* recursive type, remove storage bits first, apply them later again */
4343 storage
= type
->t
& VT_STORAGE
;
4344 type
->t
&= ~VT_STORAGE
;
4347 while (tok
== '*') {
4355 qualifiers
|= VT_CONSTANT
;
4360 qualifiers
|= VT_VOLATILE
;
4366 /* XXX: clarify attribute handling */
4367 case TOK_ATTRIBUTE1
:
4368 case TOK_ATTRIBUTE2
:
4369 parse_attribute(ad
);
4373 type
->t
|= qualifiers
;
4375 /* innermost pointed to type is the one for the first derivation */
4376 ret
= pointed_type(type
);
4380 /* This is possibly a parameter type list for abstract declarators
4381 ('int ()'), use post_type for testing this. */
4382 if (!post_type(type
, ad
, 0, td
)) {
4383 /* It's not, so it's a nested declarator, and the post operations
4384 apply to the innermost pointed to type (if any). */
4385 /* XXX: this is not correct to modify 'ad' at this point, but
4386 the syntax is not clear */
4387 parse_attribute(ad
);
4388 post
= type_decl(type
, ad
, v
, td
);
4391 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4392 /* type identifier */
4396 if (!(td
& TYPE_ABSTRACT
))
4397 expect("identifier");
4400 post_type(post
, ad
, storage
, 0);
4401 parse_attribute(ad
);
4406 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4407 ST_FUNC
int lvalue_type(int t
)
4412 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4414 else if (bt
== VT_SHORT
)
4418 if (t
& VT_UNSIGNED
)
4419 r
|= VT_LVAL_UNSIGNED
;
4423 /* indirection with full error checking and bound check */
4424 ST_FUNC
void indir(void)
4426 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4427 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4431 if (vtop
->r
& VT_LVAL
)
4433 vtop
->type
= *pointed_type(&vtop
->type
);
4434 /* Arrays and functions are never lvalues */
4435 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4436 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4437 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4438 /* if bound checking, the referenced pointer must be checked */
4439 #ifdef CONFIG_TCC_BCHECK
4440 if (tcc_state
->do_bounds_check
)
4441 vtop
->r
|= VT_MUSTBOUND
;
4446 /* pass a parameter to a function and do type checking and casting */
4447 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4452 func_type
= func
->f
.func_type
;
4453 if (func_type
== FUNC_OLD
||
4454 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4455 /* default casting : only need to convert float to double */
4456 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4457 gen_cast_s(VT_DOUBLE
);
4458 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4459 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4460 type
.ref
= vtop
->type
.ref
;
4463 } else if (arg
== NULL
) {
4464 tcc_error("too many arguments to function");
4467 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4468 gen_assign_cast(&type
);
4472 /* parse an expression and return its type without any side effect. */
4473 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4482 /* parse an expression of the form '(type)' or '(expr)' and return its
4484 static void parse_expr_type(CType
*type
)
4490 if (parse_btype(type
, &ad
)) {
4491 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4493 expr_type(type
, gexpr
);
4498 static void parse_type(CType
*type
)
4503 if (!parse_btype(type
, &ad
)) {
4506 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4509 static void parse_builtin_params(int nc
, const char *args
)
4516 while ((c
= *args
++)) {
4520 case 'e': expr_eq(); continue;
4521 case 't': parse_type(&t
); vpush(&t
); continue;
4522 default: tcc_error("internal error"); break;
4530 ST_FUNC
void unary(void)
4532 int n
, t
, align
, size
, r
, sizeof_caller
;
4537 sizeof_caller
= in_sizeof
;
4540 /* XXX: GCC 2.95.3 does not generate a table although it should be
4553 vsetc(&type
, VT_CONST
, &tokc
);
4557 t
= VT_INT
| VT_UNSIGNED
;
4563 t
= VT_LLONG
| VT_UNSIGNED
;
4576 #ifdef TCC_LONG_ARE_64_BIT
4577 t
= VT_LLONG
| VT_LONG
;
4579 t
= VT_INT
| VT_LONG
;
4581 if (tok
== TOK_CULONG
)
4584 case TOK___FUNCTION__
:
4586 goto tok_identifier
;
4592 /* special function name identifier */
4593 len
= strlen(funcname
) + 1;
4594 /* generate char[len] type */
4599 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4600 if (!NODATA_WANTED
) {
4601 ptr
= section_ptr_add(data_section
, len
);
4602 memcpy(ptr
, funcname
, len
);
4608 #ifdef TCC_TARGET_PE
4609 t
= VT_SHORT
| VT_UNSIGNED
;
4615 /* string parsing */
4617 if (tcc_state
->char_is_unsigned
)
4618 t
= VT_BYTE
| VT_UNSIGNED
;
4620 if (tcc_state
->warn_write_strings
)
4625 memset(&ad
, 0, sizeof(AttributeDef
));
4626 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4631 if (parse_btype(&type
, &ad
)) {
4632 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4634 /* check ISOC99 compound literal */
4636 /* data is allocated locally by default */
4641 /* all except arrays are lvalues */
4642 if (!(type
.t
& VT_ARRAY
))
4643 r
|= lvalue_type(type
.t
);
4644 memset(&ad
, 0, sizeof(AttributeDef
));
4645 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4647 if (sizeof_caller
) {
4654 } else if (tok
== '{') {
4655 int saved_nocode_wanted
= nocode_wanted
;
4657 tcc_error("expected constant");
4658 /* save all registers */
4660 /* statement expression : we do not accept break/continue
4661 inside as GCC does. We do retain the nocode_wanted state,
4662 as statement expressions can't ever be entered from the
4663 outside, so any reactivation of code emission (from labels
4664 or loop heads) can be disabled again after the end of it. */
4665 block(NULL
, NULL
, 1);
4666 nocode_wanted
= saved_nocode_wanted
;
4681 /* functions names must be treated as function pointers,
4682 except for unary '&' and sizeof. Since we consider that
4683 functions are not lvalues, we only have to handle it
4684 there and in function calls. */
4685 /* arrays can also be used although they are not lvalues */
4686 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4687 !(vtop
->type
.t
& VT_ARRAY
))
4689 mk_pointer(&vtop
->type
);
4695 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4696 gen_cast_s(VT_BOOL
);
4697 vtop
->c
.i
= !vtop
->c
.i
;
4698 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4702 vseti(VT_JMP
, gvtst(1, 0));
4714 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4715 tcc_error("pointer not accepted for unary plus");
4716 /* In order to force cast, we add zero, except for floating point
4717 where we really need an noop (otherwise -0.0 will be transformed
4719 if (!is_float(vtop
->type
.t
)) {
4730 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
4731 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
4732 size
= type_size(&type
, &align
);
4733 if (s
&& s
->a
.aligned
)
4734 align
= 1 << (s
->a
.aligned
- 1);
4735 if (t
== TOK_SIZEOF
) {
4736 if (!(type
.t
& VT_VLA
)) {
4738 tcc_error("sizeof applied to an incomplete type");
4741 vla_runtime_type_size(&type
, &align
);
4746 vtop
->type
.t
|= VT_UNSIGNED
;
4749 case TOK_builtin_expect
:
4750 /* __builtin_expect is a no-op for now */
4751 parse_builtin_params(0, "ee");
4754 case TOK_builtin_types_compatible_p
:
4755 parse_builtin_params(0, "tt");
4756 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4757 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4758 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
4762 case TOK_builtin_choose_expr
:
4789 case TOK_builtin_constant_p
:
4790 parse_builtin_params(1, "e");
4791 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4795 case TOK_builtin_frame_address
:
4796 case TOK_builtin_return_address
:
4802 if (tok
!= TOK_CINT
) {
4803 tcc_error("%s only takes positive integers",
4804 tok1
== TOK_builtin_return_address
?
4805 "__builtin_return_address" :
4806 "__builtin_frame_address");
4808 level
= (uint32_t)tokc
.i
;
4813 vset(&type
, VT_LOCAL
, 0); /* local frame */
4815 mk_pointer(&vtop
->type
);
4816 indir(); /* -> parent frame */
4818 if (tok1
== TOK_builtin_return_address
) {
4819 // assume return address is just above frame pointer on stack
4822 mk_pointer(&vtop
->type
);
4827 #ifdef TCC_TARGET_X86_64
4828 #ifdef TCC_TARGET_PE
4829 case TOK_builtin_va_start
:
4830 parse_builtin_params(0, "ee");
4831 r
= vtop
->r
& VT_VALMASK
;
4835 tcc_error("__builtin_va_start expects a local variable");
4837 vtop
->type
= char_pointer_type
;
4842 case TOK_builtin_va_arg_types
:
4843 parse_builtin_params(0, "t");
4844 vpushi(classify_x86_64_va_arg(&vtop
->type
));
4851 #ifdef TCC_TARGET_ARM64
4852 case TOK___va_start
: {
4853 parse_builtin_params(0, "ee");
4857 vtop
->type
.t
= VT_VOID
;
4860 case TOK___va_arg
: {
4861 parse_builtin_params(0, "et");
4869 case TOK___arm64_clear_cache
: {
4870 parse_builtin_params(0, "ee");
4873 vtop
->type
.t
= VT_VOID
;
4877 /* pre operations */
4888 t
= vtop
->type
.t
& VT_BTYPE
;
4890 /* In IEEE negate(x) isn't subtract(0,x), but rather
4894 vtop
->c
.f
= -1.0 * 0.0;
4895 else if (t
== VT_DOUBLE
)
4896 vtop
->c
.d
= -1.0 * 0.0;
4898 vtop
->c
.ld
= -1.0 * 0.0;
4906 goto tok_identifier
;
4908 /* allow to take the address of a label */
4909 if (tok
< TOK_UIDENT
)
4910 expect("label identifier");
4911 s
= label_find(tok
);
4913 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4915 if (s
->r
== LABEL_DECLARED
)
4916 s
->r
= LABEL_FORWARD
;
4919 s
->type
.t
= VT_VOID
;
4920 mk_pointer(&s
->type
);
4921 s
->type
.t
|= VT_STATIC
;
4923 vpushsym(&s
->type
, s
);
4929 CType controlling_type
;
4930 int has_default
= 0;
4933 TokenString
*str
= NULL
;
4937 expr_type(&controlling_type
, expr_eq
);
4938 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
4942 if (tok
== TOK_DEFAULT
) {
4944 tcc_error("too many 'default'");
4950 AttributeDef ad_tmp
;
4953 parse_btype(&cur_type
, &ad_tmp
);
4954 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
4955 if (compare_types(&controlling_type
, &cur_type
, 0)) {
4957 tcc_error("type match twice");
4967 skip_or_save_block(&str
);
4969 skip_or_save_block(NULL
);
4976 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
4977 tcc_error("type '%s' does not match any association", buf
);
4979 begin_macro(str
, 1);
4988 // special qnan , snan and infinity values
4990 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4994 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4998 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
5007 expect("identifier");
5010 const char *name
= get_tok_str(t
, NULL
);
5012 tcc_error("'%s' undeclared", name
);
5013 /* for simple function calls, we tolerate undeclared
5014 external reference to int() function */
5015 if (tcc_state
->warn_implicit_function_declaration
5016 #ifdef TCC_TARGET_PE
5017 /* people must be warned about using undeclared WINAPI functions
5018 (which usually start with uppercase letter) */
5019 || (name
[0] >= 'A' && name
[0] <= 'Z')
5022 tcc_warning("implicit declaration of function '%s'", name
);
5023 s
= external_global_sym(t
, &func_old_type
, 0);
5027 /* A symbol that has a register is a local register variable,
5028 which starts out as VT_LOCAL value. */
5029 if ((r
& VT_VALMASK
) < VT_CONST
)
5030 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5032 vset(&s
->type
, r
, s
->c
);
5033 /* Point to s as backpointer (even without r&VT_SYM).
5034 Will be used by at least the x86 inline asm parser for
5040 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5041 vtop
->c
.i
= s
->enum_val
;
5046 /* post operations */
5048 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5051 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5054 if (tok
== TOK_ARROW
)
5056 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5059 /* expect pointer on structure */
5060 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5061 expect("struct or union");
5062 if (tok
== TOK_CDOUBLE
)
5063 expect("field name");
5065 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5066 expect("field name");
5067 s
= find_field(&vtop
->type
, tok
);
5069 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5070 /* add field offset to pointer */
5071 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5074 /* change type to field type, and set to lvalue */
5075 vtop
->type
= s
->type
;
5076 vtop
->type
.t
|= qualifiers
;
5077 /* an array is never an lvalue */
5078 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5079 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5080 #ifdef CONFIG_TCC_BCHECK
5081 /* if bound checking, the referenced pointer must be checked */
5082 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5083 vtop
->r
|= VT_MUSTBOUND
;
5087 } else if (tok
== '[') {
5093 } else if (tok
== '(') {
5096 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5099 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5100 /* pointer test (no array accepted) */
5101 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5102 vtop
->type
= *pointed_type(&vtop
->type
);
5103 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5107 expect("function pointer");
5110 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5112 /* get return type */
5115 sa
= s
->next
; /* first parameter */
5116 nb_args
= regsize
= 0;
5118 /* compute first implicit argument if a structure is returned */
5119 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5120 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5121 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5122 &ret_align
, ®size
);
5124 /* get some space for the returned structure */
5125 size
= type_size(&s
->type
, &align
);
5126 #ifdef TCC_TARGET_ARM64
5127 /* On arm64, a small struct is return in registers.
5128 It is much easier to write it to memory if we know
5129 that we are allowed to write some extra bytes, so
5130 round the allocated space up to a power of 2: */
5132 while (size
& (size
- 1))
5133 size
= (size
| (size
- 1)) + 1;
5135 loc
= (loc
- size
) & -align
;
5137 ret
.r
= VT_LOCAL
| VT_LVAL
;
5138 /* pass it as 'int' to avoid structure arg passing
5140 vseti(VT_LOCAL
, loc
);
5150 /* return in register */
5151 if (is_float(ret
.type
.t
)) {
5152 ret
.r
= reg_fret(ret
.type
.t
);
5153 #ifdef TCC_TARGET_X86_64
5154 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5158 #ifndef TCC_TARGET_ARM64
5159 #ifdef TCC_TARGET_X86_64
5160 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5162 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5173 gfunc_param_typed(s
, sa
);
5183 tcc_error("too few arguments to function");
5185 gfunc_call(nb_args
);
5188 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5189 vsetc(&ret
.type
, r
, &ret
.c
);
5190 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5193 /* handle packed struct return */
5194 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5197 size
= type_size(&s
->type
, &align
);
5198 /* We're writing whole regs often, make sure there's enough
5199 space. Assume register size is power of 2. */
5200 if (regsize
> align
)
5202 loc
= (loc
- size
) & -align
;
5206 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5210 if (--ret_nregs
== 0)
5214 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5222 ST_FUNC
void expr_prod(void)
5227 while (tok
== '*' || tok
== '/' || tok
== '%') {
5235 ST_FUNC
void expr_sum(void)
5240 while (tok
== '+' || tok
== '-') {
5248 static void expr_shift(void)
5253 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5261 static void expr_cmp(void)
5266 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5267 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5275 static void expr_cmpeq(void)
5280 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5288 static void expr_and(void)
5291 while (tok
== '&') {
5298 static void expr_xor(void)
5301 while (tok
== '^') {
5308 static void expr_or(void)
5311 while (tok
== '|') {
5318 static void expr_land(void)
5321 if (tok
== TOK_LAND
) {
5324 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5325 gen_cast_s(VT_BOOL
);
5330 while (tok
== TOK_LAND
) {
5346 if (tok
!= TOK_LAND
) {
5359 static void expr_lor(void)
5362 if (tok
== TOK_LOR
) {
5365 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5366 gen_cast_s(VT_BOOL
);
5371 while (tok
== TOK_LOR
) {
5387 if (tok
!= TOK_LOR
) {
5400 /* Assuming vtop is a value used in a conditional context
5401 (i.e. compared with zero) return 0 if it's false, 1 if
5402 true and -1 if it can't be statically determined. */
5403 static int condition_3way(void)
5406 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5407 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5409 gen_cast_s(VT_BOOL
);
5416 static void expr_cond(void)
5418 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5420 CType type
, type1
, type2
;
5425 c
= condition_3way();
5426 g
= (tok
== ':' && gnu_ext
);
5428 /* needed to avoid having different registers saved in
5430 if (is_float(vtop
->type
.t
)) {
5432 #ifdef TCC_TARGET_X86_64
5433 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5458 sv
= *vtop
; /* save value to handle it later */
5459 vtop
--; /* no vpop so that FP stack is not flushed */
5477 bt1
= t1
& VT_BTYPE
;
5479 bt2
= t2
& VT_BTYPE
;
5482 /* cast operands to correct type according to ISOC rules */
5483 if (is_float(bt1
) || is_float(bt2
)) {
5484 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5485 type
.t
= VT_LDOUBLE
;
5487 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5492 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5493 /* cast to biggest op */
5495 /* convert to unsigned if it does not fit in a long long */
5496 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5497 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5498 type
.t
|= VT_UNSIGNED
;
5499 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5500 /* If one is a null ptr constant the result type
5502 if (is_null_pointer (vtop
))
5504 else if (is_null_pointer (&sv
))
5506 /* XXX: test pointer compatibility, C99 has more elaborate
5510 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5511 /* XXX: test function pointer compatibility */
5512 type
= bt1
== VT_FUNC
? type1
: type2
;
5513 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5514 /* XXX: test structure compatibility */
5515 type
= bt1
== VT_STRUCT
? type1
: type2
;
5516 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5517 /* NOTE: as an extension, we accept void on only one side */
5520 /* integer operations */
5522 /* convert to unsigned if it does not fit in an integer */
5523 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5524 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5525 type
.t
|= VT_UNSIGNED
;
5527 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5528 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5529 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5532 /* now we convert second operand */
5536 mk_pointer(&vtop
->type
);
5538 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5543 if (is_float(type
.t
)) {
5545 #ifdef TCC_TARGET_X86_64
5546 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5550 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5551 /* for long longs, we use fixed registers to avoid having
5552 to handle a complicated move */
5563 /* this is horrible, but we must also convert first
5569 mk_pointer(&vtop
->type
);
5571 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5577 move_reg(r2
, r1
, type
.t
);
5587 static void expr_eq(void)
5593 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5594 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5595 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5610 ST_FUNC
void gexpr(void)
5621 /* parse a constant expression and return value in vtop. */
5622 static void expr_const1(void)
5631 /* parse an integer constant and return its value. */
5632 static inline int64_t expr_const64(void)
5636 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5637 expect("constant expression");
5643 /* parse an integer constant and return its value.
5644 Complain if it doesn't fit 32bit (signed or unsigned). */
5645 ST_FUNC
int expr_const(void)
5648 int64_t wc
= expr_const64();
5650 if (c
!= wc
&& (unsigned)c
!= wc
)
5651 tcc_error("constant exceeds 32 bit");
5655 /* return the label token if current token is a label, otherwise
5657 static int is_label(void)
5661 /* fast test first */
5662 if (tok
< TOK_UIDENT
)
5664 /* no need to save tokc because tok is an identifier */
5670 unget_tok(last_tok
);
5675 #ifndef TCC_TARGET_ARM64
5676 static void gfunc_return(CType
*func_type
)
5678 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5679 CType type
, ret_type
;
5680 int ret_align
, ret_nregs
, regsize
;
5681 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5682 &ret_align
, ®size
);
5683 if (0 == ret_nregs
) {
5684 /* if returning structure, must copy it to implicit
5685 first pointer arg location */
5688 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5691 /* copy structure value to pointer */
5694 /* returning structure packed into registers */
5695 int r
, size
, addr
, align
;
5696 size
= type_size(func_type
,&align
);
5697 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5698 (vtop
->c
.i
& (ret_align
-1)))
5699 && (align
& (ret_align
-1))) {
5700 loc
= (loc
- size
) & -ret_align
;
5703 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5707 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5709 vtop
->type
= ret_type
;
5710 if (is_float(ret_type
.t
))
5711 r
= rc_fret(ret_type
.t
);
5722 if (--ret_nregs
== 0)
5724 /* We assume that when a structure is returned in multiple
5725 registers, their classes are consecutive values of the
5728 vtop
->c
.i
+= regsize
;
5732 } else if (is_float(func_type
->t
)) {
5733 gv(rc_fret(func_type
->t
));
5737 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5741 static int case_cmp(const void *pa
, const void *pb
)
5743 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5744 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5745 return a
< b
? -1 : a
> b
;
5748 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5752 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5770 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5772 gcase(base
, len
/2, bsym
);
5773 if (cur_switch
->def_sym
)
5774 gjmp_addr(cur_switch
->def_sym
);
5776 *bsym
= gjmp(*bsym
);
5780 base
+= e
; len
-= e
;
5790 if (p
->v1
== p
->v2
) {
5792 gtst_addr(0, p
->sym
);
5802 gtst_addr(0, p
->sym
);
5808 static void block(int *bsym
, int *csym
, int is_expr
)
5810 int a
, b
, c
, d
, cond
;
5813 /* generate line number info */
5814 if (tcc_state
->do_debug
)
5815 tcc_debug_line(tcc_state
);
5818 /* default return value is (void) */
5820 vtop
->type
.t
= VT_VOID
;
5823 if (tok
== TOK_IF
) {
5825 int saved_nocode_wanted
= nocode_wanted
;
5830 cond
= condition_3way();
5836 nocode_wanted
|= 0x20000000;
5837 block(bsym
, csym
, 0);
5839 nocode_wanted
= saved_nocode_wanted
;
5841 if (c
== TOK_ELSE
) {
5846 nocode_wanted
|= 0x20000000;
5847 block(bsym
, csym
, 0);
5848 gsym(d
); /* patch else jmp */
5850 nocode_wanted
= saved_nocode_wanted
;
5853 } else if (tok
== TOK_WHILE
) {
5854 int saved_nocode_wanted
;
5855 nocode_wanted
&= ~0x20000000;
5865 saved_nocode_wanted
= nocode_wanted
;
5867 nocode_wanted
= saved_nocode_wanted
;
5872 } else if (tok
== '{') {
5874 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5877 /* record local declaration stack position */
5879 llabel
= local_label_stack
;
5882 /* handle local labels declarations */
5883 if (tok
== TOK_LABEL
) {
5886 if (tok
< TOK_UIDENT
)
5887 expect("label identifier");
5888 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5898 while (tok
!= '}') {
5899 if ((a
= is_label()))
5906 block(bsym
, csym
, is_expr
);
5909 /* pop locally defined labels */
5910 label_pop(&local_label_stack
, llabel
, is_expr
);
5911 /* pop locally defined symbols */
5913 /* In the is_expr case (a statement expression is finished here),
5914 vtop might refer to symbols on the local_stack. Either via the
5915 type or via vtop->sym. We can't pop those nor any that in turn
5916 might be referred to. To make it easier we don't roll back
5917 any symbols in that case; some upper level call to block() will
5918 do that. We do have to remove such symbols from the lookup
5919 tables, though. sym_pop will do that. */
5920 sym_pop(&local_stack
, s
, is_expr
);
5922 /* Pop VLA frames and restore stack pointer if required */
5923 if (vlas_in_scope
> saved_vlas_in_scope
) {
5924 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5927 vlas_in_scope
= saved_vlas_in_scope
;
5930 } else if (tok
== TOK_RETURN
) {
5934 gen_assign_cast(&func_vt
);
5935 gfunc_return(&func_vt
);
5938 /* jump unless last stmt in top-level block */
5939 if (tok
!= '}' || local_scope
!= 1)
5941 nocode_wanted
|= 0x20000000;
5942 } else if (tok
== TOK_BREAK
) {
5945 tcc_error("cannot break");
5946 *bsym
= gjmp(*bsym
);
5949 nocode_wanted
|= 0x20000000;
5950 } else if (tok
== TOK_CONTINUE
) {
5953 tcc_error("cannot continue");
5954 vla_sp_restore_root();
5955 *csym
= gjmp(*csym
);
5958 } else if (tok
== TOK_FOR
) {
5960 int saved_nocode_wanted
;
5961 nocode_wanted
&= ~0x20000000;
5967 /* c99 for-loop init decl? */
5968 if (!decl0(VT_LOCAL
, 1, NULL
)) {
5969 /* no, regular for-loop init expr */
5995 saved_nocode_wanted
= nocode_wanted
;
5997 nocode_wanted
= saved_nocode_wanted
;
6002 sym_pop(&local_stack
, s
, 0);
6005 if (tok
== TOK_DO
) {
6006 int saved_nocode_wanted
;
6007 nocode_wanted
&= ~0x20000000;
6013 saved_nocode_wanted
= nocode_wanted
;
6021 nocode_wanted
= saved_nocode_wanted
;
6026 if (tok
== TOK_SWITCH
) {
6027 struct switch_t
*saved
, sw
;
6028 int saved_nocode_wanted
= nocode_wanted
;
6034 switchval
= *vtop
--;
6036 b
= gjmp(0); /* jump to first case */
6037 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
6041 nocode_wanted
= saved_nocode_wanted
;
6042 a
= gjmp(a
); /* add implicit break */
6045 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6046 for (b
= 1; b
< sw
.n
; b
++)
6047 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6048 tcc_error("duplicate case value");
6049 /* Our switch table sorting is signed, so the compared
6050 value needs to be as well when it's 64bit. */
6051 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6052 switchval
.type
.t
&= ~VT_UNSIGNED
;
6054 gcase(sw
.p
, sw
.n
, &a
);
6057 gjmp_addr(sw
.def_sym
);
6058 dynarray_reset(&sw
.p
, &sw
.n
);
6063 if (tok
== TOK_CASE
) {
6064 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6067 nocode_wanted
&= ~0x20000000;
6069 cr
->v1
= cr
->v2
= expr_const64();
6070 if (gnu_ext
&& tok
== TOK_DOTS
) {
6072 cr
->v2
= expr_const64();
6073 if (cr
->v2
< cr
->v1
)
6074 tcc_warning("empty case range");
6077 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6080 goto block_after_label
;
6082 if (tok
== TOK_DEFAULT
) {
6087 if (cur_switch
->def_sym
)
6088 tcc_error("too many 'default'");
6089 cur_switch
->def_sym
= ind
;
6091 goto block_after_label
;
6093 if (tok
== TOK_GOTO
) {
6095 if (tok
== '*' && gnu_ext
) {
6099 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6102 } else if (tok
>= TOK_UIDENT
) {
6103 s
= label_find(tok
);
6104 /* put forward definition if needed */
6106 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6108 if (s
->r
== LABEL_DECLARED
)
6109 s
->r
= LABEL_FORWARD
;
6111 vla_sp_restore_root();
6112 if (s
->r
& LABEL_FORWARD
)
6113 s
->jnext
= gjmp(s
->jnext
);
6115 gjmp_addr(s
->jnext
);
6118 expect("label identifier");
6121 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
6130 if (s
->r
== LABEL_DEFINED
)
6131 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6133 s
->r
= LABEL_DEFINED
;
6135 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
6139 /* we accept this, but it is a mistake */
6141 nocode_wanted
&= ~0x20000000;
6143 tcc_warning("deprecated use of label at end of compound statement");
6147 block(bsym
, csym
, is_expr
);
6150 /* expression case */
6165 /* This skips over a stream of tokens containing balanced {} and ()
6166 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6167 with a '{'). If STR then allocates and stores the skipped tokens
6168 in *STR. This doesn't check if () and {} are nested correctly,
6169 i.e. "({)}" is accepted. */
6170 static void skip_or_save_block(TokenString
**str
)
6172 int braces
= tok
== '{';
6175 *str
= tok_str_alloc();
6177 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6179 if (tok
== TOK_EOF
) {
6180 if (str
|| level
> 0)
6181 tcc_error("unexpected end of file");
6186 tok_str_add_tok(*str
);
6189 if (t
== '{' || t
== '(') {
6191 } else if (t
== '}' || t
== ')') {
6193 if (level
== 0 && braces
&& t
== '}')
6198 tok_str_add(*str
, -1);
6199 tok_str_add(*str
, 0);
6203 #define EXPR_CONST 1
6206 static void parse_init_elem(int expr_type
)
6208 int saved_global_expr
;
6211 /* compound literals must be allocated globally in this case */
6212 saved_global_expr
= global_expr
;
6215 global_expr
= saved_global_expr
;
6216 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6217 (compound literals). */
6218 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6219 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6220 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6221 #ifdef TCC_TARGET_PE
6222 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6225 tcc_error("initializer element is not constant");
6233 /* put zeros for variable based init */
6234 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6237 /* nothing to do because globals are already set to zero */
6239 vpush_global_sym(&func_old_type
, TOK_memset
);
6241 #ifdef TCC_TARGET_ARM
6252 /* t is the array or struct type. c is the array or struct
6253 address. cur_field is the pointer to the current
6254 field, for arrays the 'c' member contains the current start
6255 index. 'size_only' is true if only size info is needed (only used
6256 in arrays). al contains the already initialized length of the
6257 current container (starting at c). This returns the new length of that. */
6258 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6259 Sym
**cur_field
, int size_only
, int al
)
6262 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6263 unsigned long corig
= c
;
6267 if (gnu_ext
&& (l
= is_label()) != 0)
6269 /* NOTE: we only support ranges for last designator */
6270 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6272 if (!(type
->t
& VT_ARRAY
))
6273 expect("array type");
6275 index
= index_last
= expr_const();
6276 if (tok
== TOK_DOTS
&& gnu_ext
) {
6278 index_last
= expr_const();
6282 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6284 tcc_error("invalid index");
6286 (*cur_field
)->c
= index_last
;
6287 type
= pointed_type(type
);
6288 elem_size
= type_size(type
, &align
);
6289 c
+= index
* elem_size
;
6290 nb_elems
= index_last
- index
+ 1;
6296 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6297 expect("struct/union type");
6298 f
= find_field(type
, l
);
6311 } else if (!gnu_ext
) {
6315 if (type
->t
& VT_ARRAY
) {
6316 index
= (*cur_field
)->c
;
6317 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6318 tcc_error("index too large");
6319 type
= pointed_type(type
);
6320 c
+= index
* type_size(type
, &align
);
6323 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6324 *cur_field
= f
= f
->next
;
6326 tcc_error("too many field init");
6331 /* must put zero in holes (note that doing it that way
6332 ensures that it even works with designators) */
6333 if (!size_only
&& c
- corig
> al
)
6334 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6335 decl_initializer(type
, sec
, c
, 0, size_only
);
6337 /* XXX: make it more general */
6338 if (!size_only
&& nb_elems
> 1) {
6339 unsigned long c_end
;
6344 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6345 for (i
= 1; i
< nb_elems
; i
++) {
6346 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6351 } else if (!NODATA_WANTED
) {
6352 c_end
= c
+ nb_elems
* elem_size
;
6353 if (c_end
> sec
->data_allocated
)
6354 section_realloc(sec
, c_end
);
6355 src
= sec
->data
+ c
;
6357 for(i
= 1; i
< nb_elems
; i
++) {
6359 memcpy(dst
, src
, elem_size
);
6363 c
+= nb_elems
* type_size(type
, &align
);
6369 /* store a value or an expression directly in global data or in local array */
6370 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6377 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6381 /* XXX: not portable */
6382 /* XXX: generate error if incorrect relocation */
6383 gen_assign_cast(&dtype
);
6384 bt
= type
->t
& VT_BTYPE
;
6386 if ((vtop
->r
& VT_SYM
)
6389 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6390 || (type
->t
& VT_BITFIELD
))
6391 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6393 tcc_error("initializer element is not computable at load time");
6395 if (NODATA_WANTED
) {
6400 size
= type_size(type
, &align
);
6401 section_reserve(sec
, c
+ size
);
6402 ptr
= sec
->data
+ c
;
6404 /* XXX: make code faster ? */
6405 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6406 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6407 /* XXX This rejects compound literals like
6408 '(void *){ptr}'. The problem is that '&sym' is
6409 represented the same way, which would be ruled out
6410 by the SYM_FIRST_ANOM check above, but also '"string"'
6411 in 'char *p = "string"' is represented the same
6412 with the type being VT_PTR and the symbol being an
6413 anonymous one. That is, there's no difference in vtop
6414 between '(void *){x}' and '&(void *){x}'. Ignore
6415 pointer typed entities here. Hopefully no real code
6416 will every use compound literals with scalar type. */
6417 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6418 /* These come from compound literals, memcpy stuff over. */
6422 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6423 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6424 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6426 /* We need to copy over all memory contents, and that
6427 includes relocations. Use the fact that relocs are
6428 created it order, so look from the end of relocs
6429 until we hit one before the copied region. */
6430 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6431 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6432 while (num_relocs
--) {
6434 if (rel
->r_offset
>= esym
->st_value
+ size
)
6436 if (rel
->r_offset
< esym
->st_value
)
6438 /* Note: if the same fields are initialized multiple
6439 times (possible with designators) then we possibly
6440 add multiple relocations for the same offset here.
6441 That would lead to wrong code, the last reloc needs
6442 to win. We clean this up later after the whole
6443 initializer is parsed. */
6444 put_elf_reloca(symtab_section
, sec
,
6445 c
+ rel
->r_offset
- esym
->st_value
,
6446 ELFW(R_TYPE
)(rel
->r_info
),
6447 ELFW(R_SYM
)(rel
->r_info
),
6457 if (type
->t
& VT_BITFIELD
) {
6458 int bit_pos
, bit_size
, bits
, n
;
6459 unsigned char *p
, v
, m
;
6460 bit_pos
= BIT_POS(vtop
->type
.t
);
6461 bit_size
= BIT_SIZE(vtop
->type
.t
);
6462 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6463 bit_pos
&= 7, bits
= 0;
6468 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6469 m
= ((1 << n
) - 1) << bit_pos
;
6470 *p
= (*p
& ~m
) | (v
& m
);
6471 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6475 /* XXX: when cross-compiling we assume that each type has the
6476 same representation on host and target, which is likely to
6477 be wrong in the case of long double */
6479 vtop
->c
.i
= vtop
->c
.i
!= 0;
6481 *(char *)ptr
|= vtop
->c
.i
;
6484 *(short *)ptr
|= vtop
->c
.i
;
6487 *(float*)ptr
= vtop
->c
.f
;
6490 *(double *)ptr
= vtop
->c
.d
;
6493 if (sizeof(long double) == LDOUBLE_SIZE
)
6494 *(long double *)ptr
= vtop
->c
.ld
;
6495 else if (sizeof(double) == LDOUBLE_SIZE
)
6496 *(double *)ptr
= (double)vtop
->c
.ld
;
6497 #if (defined __i386__ || defined __x86_64__) && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
6498 else if (sizeof (long double) >= 10)
6499 memcpy(memset(ptr
, 0, LDOUBLE_SIZE
), &vtop
->c
.ld
, 10);
6501 else if (sizeof (long double) == sizeof (double))
6502 __asm__("fldl %1\nfstpt %0\n" : "=m"
6503 (memset(ptr
, 0, LDOUBLE_SIZE
), ptr
) : "m" (vtop
->c
.ld
));
6507 tcc_error("can't cross compile long double constants");
6511 *(long long *)ptr
|= vtop
->c
.i
;
6518 addr_t val
= vtop
->c
.i
;
6520 if (vtop
->r
& VT_SYM
)
6521 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6523 *(addr_t
*)ptr
|= val
;
6525 if (vtop
->r
& VT_SYM
)
6526 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6527 *(addr_t
*)ptr
|= val
;
6533 int val
= vtop
->c
.i
;
6535 if (vtop
->r
& VT_SYM
)
6536 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6540 if (vtop
->r
& VT_SYM
)
6541 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6550 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6557 /* 't' contains the type and storage info. 'c' is the offset of the
6558 object in section 'sec'. If 'sec' is NULL, it means stack based
6559 allocation. 'first' is true if array '{' must be read (multi
6560 dimension implicit array init handling). 'size_only' is true if
6561 size only evaluation is wanted (only for arrays). */
6562 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6563 int first
, int size_only
)
6565 int len
, n
, no_oblock
, nb
, i
;
6572 /* If we currently are at an '}' or ',' we have read an initializer
6573 element in one of our callers, and not yet consumed it. */
6574 have_elem
= tok
== '}' || tok
== ',';
6575 if (!have_elem
&& tok
!= '{' &&
6576 /* In case of strings we have special handling for arrays, so
6577 don't consume them as initializer value (which would commit them
6578 to some anonymous symbol). */
6579 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6581 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6586 !(type
->t
& VT_ARRAY
) &&
6587 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6588 The source type might have VT_CONSTANT set, which is
6589 of course assignable to non-const elements. */
6590 is_compatible_unqualified_types(type
, &vtop
->type
)) {
6591 init_putv(type
, sec
, c
);
6592 } else if (type
->t
& VT_ARRAY
) {
6595 t1
= pointed_type(type
);
6596 size1
= type_size(t1
, &align1
);
6599 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6602 tcc_error("character array initializer must be a literal,"
6603 " optionally enclosed in braces");
6608 /* only parse strings here if correct type (otherwise: handle
6609 them as ((w)char *) expressions */
6610 if ((tok
== TOK_LSTR
&&
6611 #ifdef TCC_TARGET_PE
6612 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6614 (t1
->t
& VT_BTYPE
) == VT_INT
6616 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6618 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6621 /* compute maximum number of chars wanted */
6623 cstr_len
= tokc
.str
.size
;
6625 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6628 if (n
>= 0 && nb
> (n
- len
))
6632 tcc_warning("initializer-string for array is too long");
6633 /* in order to go faster for common case (char
6634 string in global variable, we handle it
6636 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6638 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
6642 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6644 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6646 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
6653 /* only add trailing zero if enough storage (no
6654 warning in this case since it is standard) */
6655 if (n
< 0 || len
< n
) {
6658 init_putv(t1
, sec
, c
+ (len
* size1
));
6669 while (tok
!= '}' || have_elem
) {
6670 len
= decl_designator(type
, sec
, c
, &f
, size_only
, len
);
6672 if (type
->t
& VT_ARRAY
) {
6674 /* special test for multi dimensional arrays (may not
6675 be strictly correct if designators are used at the
6677 if (no_oblock
&& len
>= n
*size1
)
6680 if (s
->type
.t
== VT_UNION
)
6684 if (no_oblock
&& f
== NULL
)
6693 /* put zeros at the end */
6694 if (!size_only
&& len
< n
*size1
)
6695 init_putz(sec
, c
+ len
, n
*size1
- len
);
6698 /* patch type size if needed, which happens only for array types */
6700 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
6701 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6704 if (first
|| tok
== '{') {
6712 } else if (tok
== '{') {
6714 decl_initializer(type
, sec
, c
, first
, size_only
);
6716 } else if (size_only
) {
6717 /* If we supported only ISO C we wouldn't have to accept calling
6718 this on anything than an array size_only==1 (and even then
6719 only on the outermost level, so no recursion would be needed),
6720 because initializing a flex array member isn't supported.
6721 But GNU C supports it, so we need to recurse even into
6722 subfields of structs and arrays when size_only is set. */
6723 /* just skip expression */
6724 skip_or_save_block(NULL
);
6727 /* This should happen only when we haven't parsed
6728 the init element above for fear of committing a
6729 string constant to memory too early. */
6730 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6731 expect("string constant");
6732 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6734 init_putv(type
, sec
, c
);
6738 /* parse an initializer for type 't' if 'has_init' is non zero, and
6739 allocate space in local or global data space ('r' is either
6740 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6741 variable 'v' of scope 'scope' is declared before initializers
6742 are parsed. If 'v' is zero, then a reference to the new object
6743 is put in the value stack. If 'has_init' is 2, a special parsing
6744 is done to handle string constants. */
6745 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6746 int has_init
, int v
, int scope
)
6748 int size
, align
, addr
;
6749 TokenString
*init_str
= NULL
;
6752 Sym
*flexible_array
;
6754 int saved_nocode_wanted
= nocode_wanted
;
6755 #ifdef CONFIG_TCC_BCHECK
6756 int bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
6759 if (type
->t
& VT_STATIC
)
6760 nocode_wanted
|= NODATA_WANTED
? 0x40000000 : 0x80000000;
6762 flexible_array
= NULL
;
6763 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6764 Sym
*field
= type
->ref
->next
;
6767 field
= field
->next
;
6768 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6769 flexible_array
= field
;
6773 size
= type_size(type
, &align
);
6774 /* If unknown size, we must evaluate it before
6775 evaluating initializers because
6776 initializers can generate global data too
6777 (e.g. string pointers or ISOC99 compound
6778 literals). It also simplifies local
6779 initializers handling */
6780 if (size
< 0 || (flexible_array
&& has_init
)) {
6782 tcc_error("unknown type size");
6783 /* get all init string */
6784 if (has_init
== 2) {
6785 init_str
= tok_str_alloc();
6786 /* only get strings */
6787 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6788 tok_str_add_tok(init_str
);
6791 tok_str_add(init_str
, -1);
6792 tok_str_add(init_str
, 0);
6794 skip_or_save_block(&init_str
);
6799 begin_macro(init_str
, 1);
6801 decl_initializer(type
, NULL
, 0, 1, 1);
6802 /* prepare second initializer parsing */
6803 macro_ptr
= init_str
->str
;
6806 /* if still unknown size, error */
6807 size
= type_size(type
, &align
);
6809 tcc_error("unknown type size");
6811 /* If there's a flex member and it was used in the initializer
6813 if (flexible_array
&&
6814 flexible_array
->type
.ref
->c
> 0)
6815 size
+= flexible_array
->type
.ref
->c
6816 * pointed_size(&flexible_array
->type
);
6817 /* take into account specified alignment if bigger */
6818 if (ad
->a
.aligned
) {
6819 int speca
= 1 << (ad
->a
.aligned
- 1);
6822 } else if (ad
->a
.packed
) {
6827 size
= 0, align
= 1;
6829 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6831 #ifdef CONFIG_TCC_BCHECK
6832 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
6836 loc
= (loc
- size
) & -align
;
6838 #ifdef CONFIG_TCC_BCHECK
6839 /* handles bounds */
6840 /* XXX: currently, since we do only one pass, we cannot track
6841 '&' operators, so we add only arrays */
6842 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
6844 /* add padding between regions */
6846 /* then add local bound info */
6847 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6848 bounds_ptr
[0] = addr
;
6849 bounds_ptr
[1] = size
;
6853 /* local variable */
6854 #ifdef CONFIG_TCC_ASM
6855 if (ad
->asm_label
) {
6856 int reg
= asm_parse_regvar(ad
->asm_label
);
6858 r
= (r
& ~VT_VALMASK
) | reg
;
6861 sym
= sym_push(v
, type
, r
, addr
);
6864 /* push local reference */
6865 vset(type
, r
, addr
);
6868 if (v
&& scope
== VT_CONST
) {
6869 /* see if the symbol was already defined */
6872 patch_storage(sym
, ad
, type
);
6873 if (sym
->type
.t
& VT_EXTERN
) {
6874 /* if the variable is extern, it was not allocated */
6875 sym
->type
.t
&= ~VT_EXTERN
;
6876 /* set array size if it was omitted in extern
6878 if ((sym
->type
.t
& VT_ARRAY
) &&
6879 sym
->type
.ref
->c
< 0 &&
6881 sym
->type
.ref
->c
= type
->ref
->c
;
6882 } else if (!has_init
) {
6883 /* we accept several definitions of the same
6884 global variable. this is tricky, because we
6885 must play with the SHN_COMMON type of the symbol */
6886 /* no init data, we won't add more to the symbol */
6888 } else if (sym
->c
) {
6890 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6891 if (esym
->st_shndx
== data_section
->sh_num
)
6892 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6897 /* allocate symbol in corresponding section */
6902 else if (tcc_state
->nocommon
)
6907 addr
= section_add(sec
, size
, align
);
6908 #ifdef CONFIG_TCC_BCHECK
6909 /* add padding if bound check */
6911 section_add(sec
, 1, 1);
6914 addr
= align
; /* SHN_COMMON is special, symbol value is align */
6915 sec
= common_section
;
6920 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6921 patch_storage(sym
, ad
, NULL
);
6923 /* Local statics have a scope until now (for
6924 warnings), remove it here. */
6926 /* update symbol definition */
6927 put_extern_sym(sym
, sec
, addr
, size
);
6929 /* push global reference */
6930 sym
= get_sym_ref(type
, sec
, addr
, size
);
6931 vpushsym(type
, sym
);
6935 #ifdef CONFIG_TCC_BCHECK
6936 /* handles bounds now because the symbol must be defined
6937 before for the relocation */
6941 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
6942 /* then add global bound info */
6943 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6944 bounds_ptr
[0] = 0; /* relocated */
6945 bounds_ptr
[1] = size
;
6950 if (type
->t
& VT_VLA
) {
6956 /* save current stack pointer */
6957 if (vlas_in_scope
== 0) {
6958 if (vla_sp_root_loc
== -1)
6959 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6960 gen_vla_sp_save(vla_sp_root_loc
);
6963 vla_runtime_type_size(type
, &a
);
6964 gen_vla_alloc(type
, a
);
6965 gen_vla_sp_save(addr
);
6969 } else if (has_init
) {
6970 size_t oldreloc_offset
= 0;
6971 if (sec
&& sec
->reloc
)
6972 oldreloc_offset
= sec
->reloc
->data_offset
;
6973 decl_initializer(type
, sec
, addr
, 1, 0);
6974 if (sec
&& sec
->reloc
)
6975 squeeze_multi_relocs(sec
, oldreloc_offset
);
6976 /* patch flexible array member size back to -1, */
6977 /* for possible subsequent similar declarations */
6979 flexible_array
->type
.ref
->c
= -1;
6983 /* restore parse state if needed */
6989 nocode_wanted
= saved_nocode_wanted
;
6992 /* parse a function defined by symbol 'sym' and generate its code in
6993 'cur_text_section' */
6994 static void gen_function(Sym
*sym
)
6997 ind
= cur_text_section
->data_offset
;
6998 /* NOTE: we patch the symbol size later */
6999 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7000 funcname
= get_tok_str(sym
->v
, NULL
);
7002 /* Initialize VLA state */
7004 vla_sp_root_loc
= -1;
7005 /* put debug symbol */
7006 tcc_debug_funcstart(tcc_state
, sym
);
7007 /* push a dummy symbol to enable local sym storage */
7008 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7009 local_scope
= 1; /* for function parameters */
7010 gfunc_prolog(&sym
->type
);
7013 block(NULL
, NULL
, 0);
7017 cur_text_section
->data_offset
= ind
;
7018 label_pop(&global_label_stack
, NULL
, 0);
7019 /* reset local stack */
7021 sym_pop(&local_stack
, NULL
, 0);
7022 /* end of function */
7023 /* patch symbol size */
7024 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
7026 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7027 /* It's better to crash than to generate wrong code */
7028 cur_text_section
= NULL
;
7029 funcname
= ""; /* for safety */
7030 func_vt
.t
= VT_VOID
; /* for safety */
7031 func_var
= 0; /* for safety */
7032 ind
= 0; /* for safety */
7033 nocode_wanted
= 0x80000000;
7037 static void gen_inline_functions(TCCState
*s
)
7040 int inline_generated
, i
, ln
;
7041 struct InlineFunc
*fn
;
7043 ln
= file
->line_num
;
7044 /* iterate while inline function are referenced */
7046 inline_generated
= 0;
7047 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7048 fn
= s
->inline_fns
[i
];
7050 if (sym
&& sym
->c
) {
7051 /* the function was used: generate its code and
7052 convert it to a normal function */
7055 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7056 sym
->type
.t
&= ~VT_INLINE
;
7058 begin_macro(fn
->func_str
, 1);
7060 cur_text_section
= text_section
;
7064 inline_generated
= 1;
7067 } while (inline_generated
);
7068 file
->line_num
= ln
;
7071 ST_FUNC
void free_inline_functions(TCCState
*s
)
7074 /* free tokens of unused inline functions */
7075 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7076 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7078 tok_str_free(fn
->func_str
);
7080 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7083 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7084 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7085 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7093 if (!parse_btype(&btype
, &ad
)) {
7094 if (is_for_loop_init
)
7096 /* skip redundant ';' if not in old parameter decl scope */
7097 if (tok
== ';' && l
!= VT_CMP
) {
7101 if (l
== VT_CONST
&&
7102 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7103 /* global asm block */
7107 /* special test for old K&R protos without explicit int
7108 type. Only accepted when defining global data */
7109 if (l
!= VT_CONST
|| tok
< TOK_UIDENT
)
7114 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7115 int v
= btype
.ref
->v
;
7116 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7117 tcc_warning("unnamed struct/union that defines no instances");
7121 if (IS_ENUM(btype
.t
)) {
7126 while (1) { /* iterate thru each declaration */
7128 /* If the base type itself was an array type of unspecified
7129 size (like in 'typedef int arr[]; arr x = {1};') then
7130 we will overwrite the unknown size by the real one for
7131 this decl. We need to unshare the ref symbol holding
7133 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7134 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7136 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7140 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7141 printf("type = '%s'\n", buf
);
7144 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7145 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7146 tcc_error("function without file scope cannot be static");
7148 /* if old style function prototype, we accept a
7151 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7152 decl0(VT_CMP
, 0, sym
);
7155 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7156 ad
.asm_label
= asm_label_instr();
7157 /* parse one last attribute list, after asm label */
7158 parse_attribute(&ad
);
7163 #ifdef TCC_TARGET_PE
7164 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7165 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7166 tcc_error("cannot have dll linkage with static or typedef");
7167 if (ad
.a
.dllimport
) {
7168 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7171 type
.t
|= VT_EXTERN
;
7177 tcc_error("cannot use local functions");
7178 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7179 expect("function definition");
7181 /* reject abstract declarators in function definition
7182 make old style params without decl have int type */
7184 while ((sym
= sym
->next
) != NULL
) {
7185 if (!(sym
->v
& ~SYM_FIELD
))
7186 expect("identifier");
7187 if (sym
->type
.t
== VT_VOID
)
7188 sym
->type
= int_type
;
7191 /* XXX: cannot do better now: convert extern line to static inline */
7192 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7193 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7198 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7201 ref
= sym
->type
.ref
;
7203 /* use func_call from prototype if not defined */
7204 if (ref
->f
.func_call
!= FUNC_CDECL
7205 && type
.ref
->f
.func_call
== FUNC_CDECL
)
7206 type
.ref
->f
.func_call
= ref
->f
.func_call
;
7208 /* use static from prototype */
7209 if (sym
->type
.t
& VT_STATIC
)
7210 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7212 /* If the definition has no visibility use the
7213 one from prototype. */
7214 if (!type
.ref
->a
.visibility
)
7215 type
.ref
->a
.visibility
= ref
->a
.visibility
;
7216 /* apply other storage attributes from prototype */
7217 type
.ref
->a
.dllexport
|= ref
->a
.dllexport
;
7218 type
.ref
->a
.weak
|= ref
->a
.weak
;
7220 if (!is_compatible_types(&sym
->type
, &type
)) {
7222 tcc_error("incompatible types for redefinition of '%s'",
7223 get_tok_str(v
, NULL
));
7225 if (ref
->f
.func_body
)
7226 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7227 /* if symbol is already defined, then put complete type */
7231 /* put function symbol */
7232 sym
= global_identifier_push(v
, type
.t
, 0);
7233 sym
->type
.ref
= type
.ref
;
7236 sym
->type
.ref
->f
.func_body
= 1;
7237 sym
->r
= VT_SYM
| VT_CONST
;
7238 patch_storage(sym
, &ad
, NULL
);
7240 /* static inline functions are just recorded as a kind
7241 of macro. Their code will be emitted at the end of
7242 the compilation unit only if they are used */
7243 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7244 (VT_INLINE
| VT_STATIC
)) {
7245 struct InlineFunc
*fn
;
7246 const char *filename
;
7248 filename
= file
? file
->filename
: "";
7249 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7250 strcpy(fn
->filename
, filename
);
7252 skip_or_save_block(&fn
->func_str
);
7253 dynarray_add(&tcc_state
->inline_fns
,
7254 &tcc_state
->nb_inline_fns
, fn
);
7256 /* compute text section */
7257 cur_text_section
= ad
.section
;
7258 if (!cur_text_section
)
7259 cur_text_section
= text_section
;
7265 /* find parameter in function parameter list */
7266 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7267 if ((sym
->v
& ~SYM_FIELD
) == v
)
7269 tcc_error("declaration for parameter '%s' but no such parameter",
7270 get_tok_str(v
, NULL
));
7272 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7273 tcc_error("storage class specified for '%s'",
7274 get_tok_str(v
, NULL
));
7275 if (sym
->type
.t
!= VT_VOID
)
7276 tcc_error("redefinition of parameter '%s'",
7277 get_tok_str(v
, NULL
));
7278 convert_parameter_type(&type
);
7280 } else if (type
.t
& VT_TYPEDEF
) {
7281 /* save typedefed type */
7282 /* XXX: test storage specifiers ? */
7284 if (sym
&& sym
->sym_scope
== local_scope
) {
7285 if (!is_compatible_types(&sym
->type
, &type
)
7286 || !(sym
->type
.t
& VT_TYPEDEF
))
7287 tcc_error("incompatible redefinition of '%s'",
7288 get_tok_str(v
, NULL
));
7291 sym
= sym_push(v
, &type
, 0, 0);
7297 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7298 /* external function definition */
7299 /* specific case for func_call attribute */
7301 } else if (!(type
.t
& VT_ARRAY
)) {
7302 /* not lvalue if array */
7303 r
|= lvalue_type(type
.t
);
7305 has_init
= (tok
== '=');
7306 if (has_init
&& (type
.t
& VT_VLA
))
7307 tcc_error("variable length array cannot be initialized");
7308 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
)) ||
7309 ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7310 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7311 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7312 /* external variable or function */
7313 /* NOTE: as GCC, uninitialized global static
7314 arrays of null size are considered as
7316 sym
= external_sym(v
, &type
, r
, &ad
);
7317 if (ad
.alias_target
) {
7321 alias_target
= sym_find(ad
.alias_target
);
7322 if (!alias_target
|| !alias_target
->c
)
7323 tcc_error("unsupported forward __alias__ attribute");
7324 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7325 tsec
.sh_num
= esym
->st_shndx
;
7326 /* Local statics have a scope until now (for
7327 warnings), remove it here. */
7329 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7332 if (type
.t
& VT_STATIC
)
7338 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7342 if (is_for_loop_init
)
7355 static void decl(int l
)
7360 /* ------------------------------------------------------------------------- */