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 /* CPUs usually cannot use float constants, so we store them
1221 generically in data segment */
1222 size
= type_size(&vtop
->type
, &align
);
1223 vpush_ref(&vtop
->type
, data_section
, data_section
->data_offset
, size
);
1225 init_putv(&vtop
->type
, data_section
, data_section
->data_offset
);
1228 #ifdef CONFIG_TCC_BCHECK
1229 if (vtop
->r
& VT_MUSTBOUND
)
1233 r
= vtop
->r
& VT_VALMASK
;
1234 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1235 #ifndef TCC_TARGET_ARM64
1238 #ifdef TCC_TARGET_X86_64
1239 else if (rc
== RC_FRET
)
1243 /* need to reload if:
1245 - lvalue (need to dereference pointer)
1246 - already a register, but not in the right class */
1248 || (vtop
->r
& VT_LVAL
)
1249 || !(reg_classes
[r
] & rc
)
1251 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1252 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1254 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1260 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1261 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1263 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1264 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1265 unsigned long long ll
;
1267 int r2
, original_type
;
1268 original_type
= vtop
->type
.t
;
1269 /* two register type load : expand to two words
1272 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1275 vtop
->c
.i
= ll
; /* first word */
1277 vtop
->r
= r
; /* save register value */
1278 vpushi(ll
>> 32); /* second word */
1281 if (vtop
->r
& VT_LVAL
) {
1282 /* We do not want to modifier the long long
1283 pointer here, so the safest (and less
1284 efficient) is to save all the other registers
1285 in the stack. XXX: totally inefficient. */
1289 /* lvalue_save: save only if used further down the stack */
1290 save_reg_upstack(vtop
->r
, 1);
1292 /* load from memory */
1293 vtop
->type
.t
= load_type
;
1296 vtop
[-1].r
= r
; /* save register value */
1297 /* increment pointer to get second word */
1298 vtop
->type
.t
= addr_type
;
1303 vtop
->type
.t
= load_type
;
1305 /* move registers */
1308 vtop
[-1].r
= r
; /* save register value */
1309 vtop
->r
= vtop
[-1].r2
;
1311 /* Allocate second register. Here we rely on the fact that
1312 get_reg() tries first to free r2 of an SValue. */
1316 /* write second register */
1318 vtop
->type
.t
= original_type
;
1319 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1321 /* lvalue of scalar type : need to use lvalue type
1322 because of possible cast */
1325 /* compute memory access type */
1326 if (vtop
->r
& VT_LVAL_BYTE
)
1328 else if (vtop
->r
& VT_LVAL_SHORT
)
1330 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1334 /* restore wanted type */
1337 /* one register type load */
1342 #ifdef TCC_TARGET_C67
1343 /* uses register pairs for doubles */
1344 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1351 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1352 ST_FUNC
void gv2(int rc1
, int rc2
)
1356 /* generate more generic register first. But VT_JMP or VT_CMP
1357 values must be generated first in all cases to avoid possible
1359 v
= vtop
[0].r
& VT_VALMASK
;
1360 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1365 /* test if reload is needed for first register */
1366 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1376 /* test if reload is needed for first register */
1377 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1383 #ifndef TCC_TARGET_ARM64
1384 /* wrapper around RC_FRET to return a register by type */
1385 static int rc_fret(int t
)
1387 #ifdef TCC_TARGET_X86_64
1388 if (t
== VT_LDOUBLE
) {
1396 /* wrapper around REG_FRET to return a register by type */
1397 static int reg_fret(int t
)
1399 #ifdef TCC_TARGET_X86_64
1400 if (t
== VT_LDOUBLE
) {
1408 /* expand 64bit on stack in two ints */
1409 static void lexpand(void)
1412 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1413 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1414 if (v
== VT_CONST
) {
1417 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1423 vtop
[0].r
= vtop
[-1].r2
;
1424 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1426 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1430 #ifdef TCC_TARGET_ARM
1431 /* expand long long on stack */
1432 ST_FUNC
void lexpand_nr(void)
1436 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1438 vtop
->r2
= VT_CONST
;
1439 vtop
->type
.t
= VT_INT
| u
;
1440 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1441 if (v
== VT_CONST
) {
1442 vtop
[-1].c
.i
= vtop
->c
.i
;
1443 vtop
->c
.i
= vtop
->c
.i
>> 32;
1445 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1447 vtop
->r
= vtop
[-1].r
;
1448 } else if (v
> VT_CONST
) {
1452 vtop
->r
= vtop
[-1].r2
;
1453 vtop
[-1].r2
= VT_CONST
;
1454 vtop
[-1].type
.t
= VT_INT
| u
;
1459 /* build a long long from two ints */
1460 static void lbuild(int t
)
1462 gv2(RC_INT
, RC_INT
);
1463 vtop
[-1].r2
= vtop
[0].r
;
1464 vtop
[-1].type
.t
= t
;
1469 /* convert stack entry to register and duplicate its value in another
1471 static void gv_dup(void)
1478 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1479 if (t
& VT_BITFIELD
) {
1489 /* stack: H L L1 H1 */
1499 /* duplicate value */
1504 #ifdef TCC_TARGET_X86_64
1505 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1515 load(r1
, &sv
); /* move r to r1 */
1517 /* duplicates value */
1523 /* Generate value test
1525 * Generate a test for any value (jump, comparison and integers) */
1526 ST_FUNC
int gvtst(int inv
, int t
)
1528 int v
= vtop
->r
& VT_VALMASK
;
1529 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1533 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1534 /* constant jmp optimization */
1535 if ((vtop
->c
.i
!= 0) != inv
)
1540 return gtst(inv
, t
);
1544 /* generate CPU independent (unsigned) long long operations */
1545 static void gen_opl(int op
)
1547 int t
, a
, b
, op1
, c
, i
;
1549 unsigned short reg_iret
= REG_IRET
;
1550 unsigned short reg_lret
= REG_LRET
;
1556 func
= TOK___divdi3
;
1559 func
= TOK___udivdi3
;
1562 func
= TOK___moddi3
;
1565 func
= TOK___umoddi3
;
1572 /* call generic long long function */
1573 vpush_global_sym(&func_old_type
, func
);
1578 vtop
->r2
= reg_lret
;
1586 //pv("gen_opl A",0,2);
1592 /* stack: L1 H1 L2 H2 */
1597 vtop
[-2] = vtop
[-3];
1600 /* stack: H1 H2 L1 L2 */
1601 //pv("gen_opl B",0,4);
1607 /* stack: H1 H2 L1 L2 ML MH */
1610 /* stack: ML MH H1 H2 L1 L2 */
1614 /* stack: ML MH H1 L2 H2 L1 */
1619 /* stack: ML MH M1 M2 */
1622 } else if (op
== '+' || op
== '-') {
1623 /* XXX: add non carry method too (for MIPS or alpha) */
1629 /* stack: H1 H2 (L1 op L2) */
1632 gen_op(op1
+ 1); /* TOK_xxxC2 */
1635 /* stack: H1 H2 (L1 op L2) */
1638 /* stack: (L1 op L2) H1 H2 */
1640 /* stack: (L1 op L2) (H1 op H2) */
1648 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1649 t
= vtop
[-1].type
.t
;
1653 /* stack: L H shift */
1655 /* constant: simpler */
1656 /* NOTE: all comments are for SHL. the other cases are
1657 done by swapping words */
1668 if (op
!= TOK_SAR
) {
1701 /* XXX: should provide a faster fallback on x86 ? */
1704 func
= TOK___ashrdi3
;
1707 func
= TOK___lshrdi3
;
1710 func
= TOK___ashldi3
;
1716 /* compare operations */
1722 /* stack: L1 H1 L2 H2 */
1724 vtop
[-1] = vtop
[-2];
1726 /* stack: L1 L2 H1 H2 */
1729 /* when values are equal, we need to compare low words. since
1730 the jump is inverted, we invert the test too. */
1733 else if (op1
== TOK_GT
)
1735 else if (op1
== TOK_ULT
)
1737 else if (op1
== TOK_UGT
)
1747 /* generate non equal test */
1753 /* compare low. Always unsigned */
1757 else if (op1
== TOK_LE
)
1759 else if (op1
== TOK_GT
)
1761 else if (op1
== TOK_GE
)
1772 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1774 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1775 return (a
^ b
) >> 63 ? -x
: x
;
1778 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1780 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1783 /* handle integer constant optimizations and various machine
1785 static void gen_opic(int op
)
1787 SValue
*v1
= vtop
- 1;
1789 int t1
= v1
->type
.t
& VT_BTYPE
;
1790 int t2
= v2
->type
.t
& VT_BTYPE
;
1791 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1792 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1793 uint64_t l1
= c1
? v1
->c
.i
: 0;
1794 uint64_t l2
= c2
? v2
->c
.i
: 0;
1795 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1797 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1798 l1
= ((uint32_t)l1
|
1799 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1800 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1801 l2
= ((uint32_t)l2
|
1802 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1806 case '+': l1
+= l2
; break;
1807 case '-': l1
-= l2
; break;
1808 case '&': l1
&= l2
; break;
1809 case '^': l1
^= l2
; break;
1810 case '|': l1
|= l2
; break;
1811 case '*': l1
*= l2
; break;
1818 /* if division by zero, generate explicit division */
1821 tcc_error("division by zero in constant");
1825 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1826 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1827 case TOK_UDIV
: l1
= l1
/ l2
; break;
1828 case TOK_UMOD
: l1
= l1
% l2
; break;
1831 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1832 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1834 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1837 case TOK_ULT
: l1
= l1
< l2
; break;
1838 case TOK_UGE
: l1
= l1
>= l2
; break;
1839 case TOK_EQ
: l1
= l1
== l2
; break;
1840 case TOK_NE
: l1
= l1
!= l2
; break;
1841 case TOK_ULE
: l1
= l1
<= l2
; break;
1842 case TOK_UGT
: l1
= l1
> l2
; break;
1843 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1844 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1845 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1846 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1848 case TOK_LAND
: l1
= l1
&& l2
; break;
1849 case TOK_LOR
: l1
= l1
|| l2
; break;
1853 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1854 l1
= ((uint32_t)l1
|
1855 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1859 /* if commutative ops, put c2 as constant */
1860 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1861 op
== '|' || op
== '*')) {
1863 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1864 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1866 if (!const_wanted
&&
1868 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1869 (l1
== -1 && op
== TOK_SAR
))) {
1870 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1872 } else if (!const_wanted
&&
1873 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1875 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
1876 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1877 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1882 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1885 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1886 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1889 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
1890 /* filter out NOP operations like x*1, x-0, x&-1... */
1892 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1893 /* try to use shifts instead of muls or divs */
1894 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1903 else if (op
== TOK_PDIV
)
1909 } else if (c2
&& (op
== '+' || op
== '-') &&
1910 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1911 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1912 /* symbol + constant case */
1916 /* The backends can't always deal with addends to symbols
1917 larger than +-1<<31. Don't construct such. */
1924 /* call low level op generator */
1925 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1926 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1934 /* generate a floating point operation with constant propagation */
1935 static void gen_opif(int op
)
1939 #if defined _MSC_VER && defined _AMD64_
1940 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
1947 /* currently, we cannot do computations with forward symbols */
1948 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1949 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1951 if (v1
->type
.t
== VT_FLOAT
) {
1954 } else if (v1
->type
.t
== VT_DOUBLE
) {
1962 /* NOTE: we only do constant propagation if finite number (not
1963 NaN or infinity) (ANSI spec) */
1964 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1968 case '+': f1
+= f2
; break;
1969 case '-': f1
-= f2
; break;
1970 case '*': f1
*= f2
; break;
1974 tcc_error("division by zero in constant");
1979 /* XXX: also handles tests ? */
1983 /* XXX: overflow test ? */
1984 if (v1
->type
.t
== VT_FLOAT
) {
1986 } else if (v1
->type
.t
== VT_DOUBLE
) {
1998 static int pointed_size(CType
*type
)
2001 return type_size(pointed_type(type
), &align
);
2004 static void vla_runtime_pointed_size(CType
*type
)
2007 vla_runtime_type_size(pointed_type(type
), &align
);
2010 static inline int is_null_pointer(SValue
*p
)
2012 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2014 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2015 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2016 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2017 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
2020 static inline int is_integer_btype(int bt
)
2022 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2023 bt
== VT_INT
|| bt
== VT_LLONG
);
2026 /* check types for comparison or subtraction of pointers */
2027 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2029 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2032 /* null pointers are accepted for all comparisons as gcc */
2033 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2037 bt1
= type1
->t
& VT_BTYPE
;
2038 bt2
= type2
->t
& VT_BTYPE
;
2039 /* accept comparison between pointer and integer with a warning */
2040 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2041 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2042 tcc_warning("comparison between pointer and integer");
2046 /* both must be pointers or implicit function pointers */
2047 if (bt1
== VT_PTR
) {
2048 type1
= pointed_type(type1
);
2049 } else if (bt1
!= VT_FUNC
)
2050 goto invalid_operands
;
2052 if (bt2
== VT_PTR
) {
2053 type2
= pointed_type(type2
);
2054 } else if (bt2
!= VT_FUNC
) {
2056 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2058 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2059 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2063 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2064 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2065 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2066 /* gcc-like error if '-' is used */
2068 goto invalid_operands
;
2070 tcc_warning("comparison of distinct pointer types lacks a cast");
2074 /* generic gen_op: handles types problems */
2075 ST_FUNC
void gen_op(int op
)
2077 int u
, t1
, t2
, bt1
, bt2
, t
;
2081 t1
= vtop
[-1].type
.t
;
2082 t2
= vtop
[0].type
.t
;
2083 bt1
= t1
& VT_BTYPE
;
2084 bt2
= t2
& VT_BTYPE
;
2086 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2087 tcc_error("operation on a struct");
2088 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2089 if (bt2
== VT_FUNC
) {
2090 mk_pointer(&vtop
->type
);
2093 if (bt1
== VT_FUNC
) {
2095 mk_pointer(&vtop
->type
);
2100 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2101 /* at least one operand is a pointer */
2102 /* relational op: must be both pointers */
2103 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2104 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2105 /* pointers are handled are unsigned */
2107 t
= VT_LLONG
| VT_UNSIGNED
;
2109 t
= VT_INT
| VT_UNSIGNED
;
2113 /* if both pointers, then it must be the '-' op */
2114 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2116 tcc_error("cannot use pointers here");
2117 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2118 /* XXX: check that types are compatible */
2119 if (vtop
[-1].type
.t
& VT_VLA
) {
2120 vla_runtime_pointed_size(&vtop
[-1].type
);
2122 vpushi(pointed_size(&vtop
[-1].type
));
2126 /* set to integer type */
2128 vtop
->type
.t
= VT_LLONG
;
2130 vtop
->type
.t
= VT_INT
;
2135 /* exactly one pointer : must be '+' or '-'. */
2136 if (op
!= '-' && op
!= '+')
2137 tcc_error("cannot use pointers here");
2138 /* Put pointer as first operand */
2139 if (bt2
== VT_PTR
) {
2141 t
= t1
, t1
= t2
, t2
= t
;
2144 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2145 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2148 type1
= vtop
[-1].type
;
2149 type1
.t
&= ~VT_ARRAY
;
2150 if (vtop
[-1].type
.t
& VT_VLA
)
2151 vla_runtime_pointed_size(&vtop
[-1].type
);
2153 u
= pointed_size(&vtop
[-1].type
);
2155 tcc_error("unknown array element size");
2159 /* XXX: cast to int ? (long long case) */
2165 /* #ifdef CONFIG_TCC_BCHECK
2166 The main reason to removing this code:
2173 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2174 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2176 When this code is on. then the output looks like
2178 v+(i-j) = 0xbff84000
2180 /* if evaluating constant expression, no code should be
2181 generated, so no bound check */
2182 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2183 /* if bounded pointers, we generate a special code to
2190 gen_bounded_ptr_add();
2196 /* put again type if gen_opic() swaped operands */
2199 } else if (is_float(bt1
) || is_float(bt2
)) {
2200 /* compute bigger type and do implicit casts */
2201 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2203 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2208 /* floats can only be used for a few operations */
2209 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2210 (op
< TOK_ULT
|| op
> TOK_GT
))
2211 tcc_error("invalid operands for binary operation");
2213 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2214 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2215 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2218 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2219 /* cast to biggest op */
2221 /* convert to unsigned if it does not fit in a long long */
2222 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2223 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2227 /* integer operations */
2229 /* convert to unsigned if it does not fit in an integer */
2230 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2231 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2234 /* XXX: currently, some unsigned operations are explicit, so
2235 we modify them here */
2236 if (t
& VT_UNSIGNED
) {
2243 else if (op
== TOK_LT
)
2245 else if (op
== TOK_GT
)
2247 else if (op
== TOK_LE
)
2249 else if (op
== TOK_GE
)
2257 /* special case for shifts and long long: we keep the shift as
2259 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2266 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2267 /* relational op: the result is an int */
2268 vtop
->type
.t
= VT_INT
;
2273 // Make sure that we have converted to an rvalue:
2274 if (vtop
->r
& VT_LVAL
)
2275 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2278 #ifndef TCC_TARGET_ARM
2279 /* generic itof for unsigned long long case */
2280 static void gen_cvt_itof1(int t
)
2282 #ifdef TCC_TARGET_ARM64
2285 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2286 (VT_LLONG
| VT_UNSIGNED
)) {
2289 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2290 #if LDOUBLE_SIZE != 8
2291 else if (t
== VT_LDOUBLE
)
2292 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2295 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2299 vtop
->r
= reg_fret(t
);
2307 /* generic ftoi for unsigned long long case */
2308 static void gen_cvt_ftoi1(int t
)
2310 #ifdef TCC_TARGET_ARM64
2315 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2316 /* not handled natively */
2317 st
= vtop
->type
.t
& VT_BTYPE
;
2319 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2320 #if LDOUBLE_SIZE != 8
2321 else if (st
== VT_LDOUBLE
)
2322 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2325 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2330 vtop
->r2
= REG_LRET
;
2337 /* force char or short cast */
2338 static void force_charshort_cast(int t
)
2342 /* cannot cast static initializers */
2343 if (STATIC_DATA_WANTED
)
2347 /* XXX: add optimization if lvalue : just change type and offset */
2352 if (t
& VT_UNSIGNED
) {
2353 vpushi((1 << bits
) - 1);
2356 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2362 /* result must be signed or the SAR is converted to an SHL
2363 This was not the case when "t" was a signed short
2364 and the last value on the stack was an unsigned int */
2365 vtop
->type
.t
&= ~VT_UNSIGNED
;
2371 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2372 static void gen_cast_s(int t
)
2380 static void gen_cast(CType
*type
)
2382 int sbt
, dbt
, sf
, df
, c
, p
;
2384 /* special delayed cast for char/short */
2385 /* XXX: in some cases (multiple cascaded casts), it may still
2387 if (vtop
->r
& VT_MUSTCAST
) {
2388 vtop
->r
&= ~VT_MUSTCAST
;
2389 force_charshort_cast(vtop
->type
.t
);
2392 /* bitfields first get cast to ints */
2393 if (vtop
->type
.t
& VT_BITFIELD
) {
2397 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2398 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2403 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2404 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2406 /* constant case: we can do it now */
2407 /* XXX: in ISOC, cannot do it if error in convert */
2408 if (sbt
== VT_FLOAT
)
2409 vtop
->c
.ld
= vtop
->c
.f
;
2410 else if (sbt
== VT_DOUBLE
)
2411 vtop
->c
.ld
= vtop
->c
.d
;
2414 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2415 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2416 vtop
->c
.ld
= vtop
->c
.i
;
2418 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2420 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2421 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2423 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2426 if (dbt
== VT_FLOAT
)
2427 vtop
->c
.f
= (float)vtop
->c
.ld
;
2428 else if (dbt
== VT_DOUBLE
)
2429 vtop
->c
.d
= (double)vtop
->c
.ld
;
2430 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2431 vtop
->c
.i
= vtop
->c
.ld
;
2432 } else if (sf
&& dbt
== VT_BOOL
) {
2433 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2436 vtop
->c
.i
= vtop
->c
.ld
;
2437 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2439 else if (sbt
& VT_UNSIGNED
)
2440 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2442 else if (sbt
== VT_PTR
)
2445 else if (sbt
!= VT_LLONG
)
2446 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2447 -(vtop
->c
.i
& 0x80000000));
2449 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2451 else if (dbt
== VT_BOOL
)
2452 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2454 else if (dbt
== VT_PTR
)
2457 else if (dbt
!= VT_LLONG
) {
2458 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2459 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2462 if (!(dbt
& VT_UNSIGNED
))
2463 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2466 } else if (p
&& dbt
== VT_BOOL
) {
2470 /* non constant case: generate code */
2472 /* convert from fp to fp */
2475 /* convert int to fp */
2478 /* convert fp to int */
2479 if (dbt
== VT_BOOL
) {
2483 /* we handle char/short/etc... with generic code */
2484 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2485 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2489 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2490 /* additional cast for char/short... */
2496 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2497 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2498 /* scalar to long long */
2499 /* machine independent conversion */
2501 /* generate high word */
2502 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2506 if (sbt
== VT_PTR
) {
2507 /* cast from pointer to int before we apply
2508 shift operation, which pointers don't support*/
2515 /* patch second register */
2516 vtop
[-1].r2
= vtop
->r
;
2520 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2521 (dbt
& VT_BTYPE
) == VT_PTR
||
2522 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2523 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2524 (sbt
& VT_BTYPE
) != VT_PTR
&&
2525 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2526 /* need to convert from 32bit to 64bit */
2528 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2529 #if defined(TCC_TARGET_ARM64)
2531 #elif defined(TCC_TARGET_X86_64)
2533 /* x86_64 specific: movslq */
2535 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2542 } else if (dbt
== VT_BOOL
) {
2543 /* scalar to bool */
2546 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2547 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2548 if (sbt
== VT_PTR
) {
2549 vtop
->type
.t
= VT_INT
;
2550 tcc_warning("nonportable conversion from pointer to char/short");
2552 force_charshort_cast(dbt
);
2554 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2556 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2557 /* from long long: just take low order word */
2561 /* if lvalue and single word type, nothing to do because
2562 the lvalue already contains the real type size (see
2563 VT_LVAL_xxx constants) */
2567 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2568 /* if we are casting between pointer types,
2569 we must update the VT_LVAL_xxx size */
2570 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2571 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2576 /* return type size as known at compile time. Put alignment at 'a' */
2577 ST_FUNC
int type_size(CType
*type
, int *a
)
2582 bt
= type
->t
& VT_BTYPE
;
2583 if (bt
== VT_STRUCT
) {
2588 } else if (bt
== VT_PTR
) {
2589 if (type
->t
& VT_ARRAY
) {
2593 ts
= type_size(&s
->type
, a
);
2595 if (ts
< 0 && s
->c
< 0)
2603 } else if (IS_ENUM(type
->t
) && type
->ref
->c
== -1) {
2604 return -1; /* incomplete enum */
2605 } else if (bt
== VT_LDOUBLE
) {
2607 return LDOUBLE_SIZE
;
2608 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2609 #ifdef TCC_TARGET_I386
2610 #ifdef TCC_TARGET_PE
2615 #elif defined(TCC_TARGET_ARM)
2625 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2628 } else if (bt
== VT_SHORT
) {
2631 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2635 /* char, void, function, _Bool */
2641 /* push type size as known at runtime time on top of value stack. Put
2643 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2645 if (type
->t
& VT_VLA
) {
2646 type_size(&type
->ref
->type
, a
);
2647 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2649 vpushi(type_size(type
, a
));
2653 static void vla_sp_restore(void) {
2654 if (vlas_in_scope
) {
2655 gen_vla_sp_restore(vla_sp_loc
);
2659 static void vla_sp_restore_root(void) {
2660 if (vlas_in_scope
) {
2661 gen_vla_sp_restore(vla_sp_root_loc
);
2665 /* return the pointed type of t */
2666 static inline CType
*pointed_type(CType
*type
)
2668 return &type
->ref
->type
;
2671 /* modify type so that its it is a pointer to type. */
2672 ST_FUNC
void mk_pointer(CType
*type
)
2675 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2676 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2680 /* compare function types. OLD functions match any new functions */
2681 static int is_compatible_func(CType
*type1
, CType
*type2
)
2687 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2689 /* check func_call */
2690 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2692 /* XXX: not complete */
2693 if (s1
->f
.func_type
== FUNC_OLD
|| s2
->f
.func_type
== FUNC_OLD
)
2695 if (s1
->f
.func_type
!= s2
->f
.func_type
)
2697 while (s1
!= NULL
) {
2700 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2710 /* return true if type1 and type2 are the same. If unqualified is
2711 true, qualifiers on the types are ignored.
2713 - enums are not checked as gcc __builtin_types_compatible_p ()
2715 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2719 t1
= type1
->t
& VT_TYPE
;
2720 t2
= type2
->t
& VT_TYPE
;
2722 /* strip qualifiers before comparing */
2723 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2724 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2726 /* Default Vs explicit signedness only matters for char */
2727 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2732 /* XXX: bitfields ? */
2735 /* test more complicated cases */
2736 bt1
= t1
& VT_BTYPE
;
2737 if (bt1
== VT_PTR
) {
2738 type1
= pointed_type(type1
);
2739 type2
= pointed_type(type2
);
2740 return is_compatible_types(type1
, type2
);
2741 } else if (bt1
== VT_STRUCT
) {
2742 return (type1
->ref
== type2
->ref
);
2743 } else if (bt1
== VT_FUNC
) {
2744 return is_compatible_func(type1
, type2
);
2750 /* return true if type1 and type2 are exactly the same (including
2753 static int is_compatible_types(CType
*type1
, CType
*type2
)
2755 return compare_types(type1
,type2
,0);
2758 /* return true if type1 and type2 are the same (ignoring qualifiers).
2760 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
2762 return compare_types(type1
,type2
,1);
2765 /* print a type. If 'varstr' is not NULL, then the variable is also
2766 printed in the type */
2768 /* XXX: add array and function pointers */
2769 static void type_to_str(char *buf
, int buf_size
,
2770 CType
*type
, const char *varstr
)
2780 if (t
& VT_CONSTANT
)
2781 pstrcat(buf
, buf_size
, "const ");
2782 if (t
& VT_VOLATILE
)
2783 pstrcat(buf
, buf_size
, "volatile ");
2784 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2785 pstrcat(buf
, buf_size
, "unsigned ");
2786 else if (t
& VT_DEFSIGN
)
2787 pstrcat(buf
, buf_size
, "signed ");
2789 pstrcat(buf
, buf_size
, "extern ");
2791 pstrcat(buf
, buf_size
, "static ");
2793 pstrcat(buf
, buf_size
, "typedef ");
2795 pstrcat(buf
, buf_size
, "inline ");
2796 buf_size
-= strlen(buf
);
2831 tstr
= "long double";
2833 pstrcat(buf
, buf_size
, tstr
);
2840 pstrcat(buf
, buf_size
, tstr
);
2841 v
= type
->ref
->v
& ~SYM_STRUCT
;
2842 if (v
>= SYM_FIRST_ANOM
)
2843 pstrcat(buf
, buf_size
, "<anonymous>");
2845 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2849 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2850 pstrcat(buf
, buf_size
, "(");
2852 while (sa
!= NULL
) {
2853 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2854 pstrcat(buf
, buf_size
, buf1
);
2857 pstrcat(buf
, buf_size
, ", ");
2859 pstrcat(buf
, buf_size
, ")");
2864 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
2865 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2868 pstrcpy(buf1
, sizeof(buf1
), "*");
2869 if (t
& VT_CONSTANT
)
2870 pstrcat(buf1
, buf_size
, "const ");
2871 if (t
& VT_VOLATILE
)
2872 pstrcat(buf1
, buf_size
, "volatile ");
2874 pstrcat(buf1
, sizeof(buf1
), varstr
);
2875 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2879 pstrcat(buf
, buf_size
, " ");
2880 pstrcat(buf
, buf_size
, varstr
);
2885 /* verify type compatibility to store vtop in 'dt' type, and generate
2887 static void gen_assign_cast(CType
*dt
)
2889 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2890 char buf1
[256], buf2
[256];
2893 st
= &vtop
->type
; /* source type */
2894 dbt
= dt
->t
& VT_BTYPE
;
2895 sbt
= st
->t
& VT_BTYPE
;
2896 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2897 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2899 It is Ok if both are void
2905 gcc accepts this program
2908 tcc_error("cannot cast from/to void");
2910 if (dt
->t
& VT_CONSTANT
)
2911 tcc_warning("assignment of read-only location");
2914 /* special cases for pointers */
2915 /* '0' can also be a pointer */
2916 if (is_null_pointer(vtop
))
2918 /* accept implicit pointer to integer cast with warning */
2919 if (is_integer_btype(sbt
)) {
2920 tcc_warning("assignment makes pointer from integer without a cast");
2923 type1
= pointed_type(dt
);
2924 /* a function is implicitly a function pointer */
2925 if (sbt
== VT_FUNC
) {
2926 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2927 !is_compatible_types(pointed_type(dt
), st
))
2928 tcc_warning("assignment from incompatible pointer type");
2933 type2
= pointed_type(st
);
2934 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2935 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2936 /* void * can match anything */
2938 //printf("types %08x %08x\n", type1->t, type2->t);
2939 /* exact type match, except for qualifiers */
2940 if (!is_compatible_unqualified_types(type1
, type2
)) {
2941 /* Like GCC don't warn by default for merely changes
2942 in pointer target signedness. Do warn for different
2943 base types, though, in particular for unsigned enums
2944 and signed int targets. */
2945 if ((type1
->t
& VT_BTYPE
) != (type2
->t
& VT_BTYPE
)
2946 || IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)
2948 tcc_warning("assignment from incompatible pointer type");
2951 /* check const and volatile */
2952 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2953 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2954 tcc_warning("assignment discards qualifiers from pointer target type");
2960 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2961 tcc_warning("assignment makes integer from pointer without a cast");
2962 } else if (sbt
== VT_STRUCT
) {
2963 goto case_VT_STRUCT
;
2965 /* XXX: more tests */
2971 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2972 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2973 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2975 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2976 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2977 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2985 /* store vtop in lvalue pushed on stack */
2986 ST_FUNC
void vstore(void)
2988 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2990 ft
= vtop
[-1].type
.t
;
2991 sbt
= vtop
->type
.t
& VT_BTYPE
;
2992 dbt
= ft
& VT_BTYPE
;
2993 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2994 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2995 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2996 /* optimize char/short casts */
2997 delayed_cast
= VT_MUSTCAST
;
2998 vtop
->type
.t
= ft
& VT_TYPE
;
2999 /* XXX: factorize */
3000 if (ft
& VT_CONSTANT
)
3001 tcc_warning("assignment of read-only location");
3004 if (!(ft
& VT_BITFIELD
))
3005 gen_assign_cast(&vtop
[-1].type
);
3008 if (sbt
== VT_STRUCT
) {
3009 /* if structure, only generate pointer */
3010 /* structure assignment : generate memcpy */
3011 /* XXX: optimize if small size */
3012 size
= type_size(&vtop
->type
, &align
);
3016 vtop
->type
.t
= VT_PTR
;
3019 /* address of memcpy() */
3022 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3023 else if(!(align
& 3))
3024 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3027 /* Use memmove, rather than memcpy, as dest and src may be same: */
3028 vpush_global_sym(&func_old_type
, TOK_memmove
);
3033 vtop
->type
.t
= VT_PTR
;
3039 /* leave source on stack */
3040 } else if (ft
& VT_BITFIELD
) {
3041 /* bitfield store handling */
3043 /* save lvalue as expression result (example: s.b = s.a = n;) */
3044 vdup(), vtop
[-1] = vtop
[-2];
3046 bit_pos
= BIT_POS(ft
);
3047 bit_size
= BIT_SIZE(ft
);
3048 /* remove bit field info to avoid loops */
3049 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3051 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3052 gen_cast(&vtop
[-1].type
);
3053 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3056 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3057 if (r
== VT_STRUCT
) {
3058 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG
? VT_LLONG
: VT_INT
);
3059 store_packed_bf(bit_pos
, bit_size
);
3061 unsigned long long mask
= (1ULL << bit_size
) - 1;
3062 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3064 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3067 vpushi((unsigned)mask
);
3074 /* duplicate destination */
3077 /* load destination, mask and or with source */
3078 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3079 vpushll(~(mask
<< bit_pos
));
3081 vpushi(~((unsigned)mask
<< bit_pos
));
3086 /* ... and discard */
3090 #ifdef CONFIG_TCC_BCHECK
3091 /* bound check case */
3092 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3101 #ifdef TCC_TARGET_X86_64
3102 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3104 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3109 r
= gv(rc
); /* generate value */
3110 /* if lvalue was saved on stack, must read it */
3111 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3113 t
= get_reg(RC_INT
);
3119 sv
.r
= VT_LOCAL
| VT_LVAL
;
3120 sv
.c
.i
= vtop
[-1].c
.i
;
3122 vtop
[-1].r
= t
| VT_LVAL
;
3124 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3126 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3127 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3129 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3130 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3132 vtop
[-1].type
.t
= load_type
;
3135 /* convert to int to increment easily */
3136 vtop
->type
.t
= addr_type
;
3142 vtop
[-1].type
.t
= load_type
;
3143 /* XXX: it works because r2 is spilled last ! */
3144 store(vtop
->r2
, vtop
- 1);
3150 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3151 vtop
->r
|= delayed_cast
;
3155 /* post defines POST/PRE add. c is the token ++ or -- */
3156 ST_FUNC
void inc(int post
, int c
)
3159 vdup(); /* save lvalue */
3161 gv_dup(); /* duplicate value */
3166 vpushi(c
- TOK_MID
);
3168 vstore(); /* store value */
3170 vpop(); /* if post op, return saved value */
3173 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3175 /* read the string */
3179 while (tok
== TOK_STR
) {
3180 /* XXX: add \0 handling too ? */
3181 cstr_cat(astr
, tokc
.str
.data
, -1);
3184 cstr_ccat(astr
, '\0');
3187 /* If I is >= 1 and a power of two, returns log2(i)+1.
3188 If I is 0 returns 0. */
3189 static int exact_log2p1(int i
)
3194 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3205 /* Parse __attribute__((...)) GNUC extension. */
3206 static void parse_attribute(AttributeDef
*ad
)
3212 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3217 while (tok
!= ')') {
3218 if (tok
< TOK_IDENT
)
3219 expect("attribute name");
3226 parse_mult_str(&astr
, "section name");
3227 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3234 parse_mult_str(&astr
, "alias(\"target\")");
3235 ad
->alias_target
= /* save string as token, for later */
3236 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3240 case TOK_VISIBILITY1
:
3241 case TOK_VISIBILITY2
:
3243 parse_mult_str(&astr
,
3244 "visibility(\"default|hidden|internal|protected\")");
3245 if (!strcmp (astr
.data
, "default"))
3246 ad
->a
.visibility
= STV_DEFAULT
;
3247 else if (!strcmp (astr
.data
, "hidden"))
3248 ad
->a
.visibility
= STV_HIDDEN
;
3249 else if (!strcmp (astr
.data
, "internal"))
3250 ad
->a
.visibility
= STV_INTERNAL
;
3251 else if (!strcmp (astr
.data
, "protected"))
3252 ad
->a
.visibility
= STV_PROTECTED
;
3254 expect("visibility(\"default|hidden|internal|protected\")");
3263 if (n
<= 0 || (n
& (n
- 1)) != 0)
3264 tcc_error("alignment must be a positive power of two");
3269 ad
->a
.aligned
= exact_log2p1(n
);
3270 if (n
!= 1 << (ad
->a
.aligned
- 1))
3271 tcc_error("alignment of %d is larger than implemented", n
);
3283 /* currently, no need to handle it because tcc does not
3284 track unused objects */
3288 /* currently, no need to handle it because tcc does not
3289 track unused objects */
3294 ad
->f
.func_call
= FUNC_CDECL
;
3299 ad
->f
.func_call
= FUNC_STDCALL
;
3301 #ifdef TCC_TARGET_I386
3311 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3317 ad
->f
.func_call
= FUNC_FASTCALLW
;
3324 ad
->attr_mode
= VT_LLONG
+ 1;
3327 ad
->attr_mode
= VT_BYTE
+ 1;
3330 ad
->attr_mode
= VT_SHORT
+ 1;
3334 ad
->attr_mode
= VT_INT
+ 1;
3337 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3344 ad
->a
.dllexport
= 1;
3347 ad
->a
.dllimport
= 1;
3350 if (tcc_state
->warn_unsupported
)
3351 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3352 /* skip parameters */
3354 int parenthesis
= 0;
3358 else if (tok
== ')')
3361 } while (parenthesis
&& tok
!= -1);
3374 static Sym
* find_field (CType
*type
, int v
)
3378 while ((s
= s
->next
) != NULL
) {
3379 if ((s
->v
& SYM_FIELD
) &&
3380 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3381 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3382 Sym
*ret
= find_field (&s
->type
, v
);
3392 static void struct_add_offset (Sym
*s
, int offset
)
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 struct_add_offset(s
->type
.ref
, offset
);
3404 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3406 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3407 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3408 int pcc
= !tcc_state
->ms_bitfields
;
3409 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3416 prevbt
= VT_STRUCT
; /* make it never match */
3421 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3422 if (f
->type
.t
& VT_BITFIELD
)
3423 bit_size
= BIT_SIZE(f
->type
.t
);
3426 size
= type_size(&f
->type
, &align
);
3427 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3430 if (pcc
&& bit_size
== 0) {
3431 /* in pcc mode, packing does not affect zero-width bitfields */
3434 /* in pcc mode, attribute packed overrides if set. */
3435 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3438 /* pragma pack overrides align if lesser and packs bitfields always */
3441 if (pragma_pack
< align
)
3442 align
= pragma_pack
;
3443 /* in pcc mode pragma pack also overrides individual align */
3444 if (pcc
&& pragma_pack
< a
)
3448 /* some individual align was specified */
3452 if (type
->ref
->type
.t
== VT_UNION
) {
3453 if (pcc
&& bit_size
>= 0)
3454 size
= (bit_size
+ 7) >> 3;
3459 } else if (bit_size
< 0) {
3461 c
+= (bit_pos
+ 7) >> 3;
3462 c
= (c
+ align
- 1) & -align
;
3471 /* A bit-field. Layout is more complicated. There are two
3472 options: PCC (GCC) compatible and MS compatible */
3474 /* In PCC layout a bit-field is placed adjacent to the
3475 preceding bit-fields, except if:
3477 - an individual alignment was given
3478 - it would overflow its base type container and
3479 there is no packing */
3480 if (bit_size
== 0) {
3482 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3484 } else if (f
->a
.aligned
) {
3486 } else if (!packed
) {
3488 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3489 if (ofs
> size
/ align
)
3493 /* in pcc mode, long long bitfields have type int if they fit */
3494 if (size
== 8 && bit_size
<= 32)
3495 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3497 while (bit_pos
>= align
* 8)
3498 c
+= align
, bit_pos
-= align
* 8;
3501 /* In PCC layout named bit-fields influence the alignment
3502 of the containing struct using the base types alignment,
3503 except for packed fields (which here have correct align). */
3504 if (f
->v
& SYM_FIRST_ANOM
)
3507 bt
= f
->type
.t
& VT_BTYPE
;
3508 if ((bit_pos
+ bit_size
> size
* 8)
3509 || (bit_size
> 0) == (bt
!= prevbt
)
3511 c
= (c
+ align
- 1) & -align
;
3514 /* In MS bitfield mode a bit-field run always uses
3515 at least as many bits as the underlying type.
3516 To start a new run it's also required that this
3517 or the last bit-field had non-zero width. */
3518 if (bit_size
|| prev_bit_size
)
3521 /* In MS layout the records alignment is normally
3522 influenced by the field, except for a zero-width
3523 field at the start of a run (but by further zero-width
3524 fields it is again). */
3525 if (bit_size
== 0 && prevbt
!= bt
)
3528 prev_bit_size
= bit_size
;
3531 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3532 | (bit_pos
<< VT_STRUCT_SHIFT
);
3533 bit_pos
+= bit_size
;
3535 if (align
> maxalign
)
3539 printf("set field %s offset %-2d size %-2d align %-2d",
3540 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3541 if (f
->type
.t
& VT_BITFIELD
) {
3542 printf(" pos %-2d bits %-2d",
3550 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3552 /* An anonymous struct/union. Adjust member offsets
3553 to reflect the real offset of our containing struct.
3554 Also set the offset of this anon member inside
3555 the outer struct to be zero. Via this it
3556 works when accessing the field offset directly
3557 (from base object), as well as when recursing
3558 members in initializer handling. */
3559 int v2
= f
->type
.ref
->v
;
3560 if (!(v2
& SYM_FIELD
) &&
3561 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3563 /* This happens only with MS extensions. The
3564 anon member has a named struct type, so it
3565 potentially is shared with other references.
3566 We need to unshare members so we can modify
3569 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3570 &f
->type
.ref
->type
, 0,
3572 pps
= &f
->type
.ref
->next
;
3573 while ((ass
= ass
->next
) != NULL
) {
3574 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3575 pps
= &((*pps
)->next
);
3579 struct_add_offset(f
->type
.ref
, offset
);
3589 c
+= (bit_pos
+ 7) >> 3;
3591 /* store size and alignment */
3592 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3596 if (pragma_pack
&& pragma_pack
< maxalign
) {
3597 /* can happen if individual align for some member was given. In
3598 this case MSVC ignores maxalign when aligning the size */
3603 c
= (c
+ a
- 1) & -a
;
3607 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3610 /* check whether we can access bitfields by their type */
3611 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3615 if (0 == (f
->type
.t
& VT_BITFIELD
))
3619 bit_size
= BIT_SIZE(f
->type
.t
);
3622 bit_pos
= BIT_POS(f
->type
.t
);
3623 size
= type_size(&f
->type
, &align
);
3624 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3627 /* try to access the field using a differnt type */
3628 c0
= -1, s
= align
= 1;
3630 px
= f
->c
* 8 + bit_pos
;
3631 cx
= (px
>> 3) & -align
;
3632 px
= px
- (cx
<< 3);
3635 s
= (px
+ bit_size
+ 7) >> 3;
3645 s
= type_size(&t
, &align
);
3649 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3650 /* update offset and bit position */
3653 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3654 | (bit_pos
<< VT_STRUCT_SHIFT
);
3658 printf("FIX field %s offset %-2d size %-2d align %-2d "
3659 "pos %-2d bits %-2d\n",
3660 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3661 cx
, s
, align
, px
, bit_size
);
3664 /* fall back to load/store single-byte wise */
3665 f
->auxtype
= VT_STRUCT
;
3667 printf("FIX field %s : load byte-wise\n",
3668 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3674 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3675 static void struct_decl(CType
*type
, int u
)
3677 int v
, c
, size
, align
, flexible
;
3678 int bit_size
, bsize
, bt
;
3680 AttributeDef ad
, ad1
;
3683 memset(&ad
, 0, sizeof ad
);
3685 parse_attribute(&ad
);
3689 /* struct already defined ? return it */
3691 expect("struct/union/enum name");
3693 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3696 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3698 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3703 /* Record the original enum/struct/union token. */
3704 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3706 /* we put an undefined size for struct/union */
3707 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3708 s
->r
= 0; /* default alignment is zero as gcc */
3710 type
->t
= s
->type
.t
;
3716 tcc_error("struct/union/enum already defined");
3717 /* cannot be empty */
3718 /* non empty enums are not allowed */
3721 long long ll
= 0, pl
= 0, nl
= 0;
3724 /* enum symbols have static storage */
3725 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3729 expect("identifier");
3731 if (ss
&& !local_stack
)
3732 tcc_error("redefinition of enumerator '%s'",
3733 get_tok_str(v
, NULL
));
3737 ll
= expr_const64();
3739 ss
= sym_push(v
, &t
, VT_CONST
, 0);
3741 *ps
= ss
, ps
= &ss
->next
;
3750 /* NOTE: we accept a trailing comma */
3755 /* set integral type of the enum */
3758 if (pl
!= (unsigned)pl
)
3761 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
3763 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
3765 /* set type for enum members */
3766 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
3768 if (ll
== (int)ll
) /* default is int if it fits */
3770 if (t
.t
& VT_UNSIGNED
) {
3771 ss
->type
.t
|= VT_UNSIGNED
;
3772 if (ll
== (unsigned)ll
)
3775 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
) | VT_LLONG
;
3780 while (tok
!= '}') {
3781 if (!parse_btype(&btype
, &ad1
)) {
3787 tcc_error("flexible array member '%s' not at the end of struct",
3788 get_tok_str(v
, NULL
));
3794 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
3796 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3797 expect("identifier");
3799 int v
= btype
.ref
->v
;
3800 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3801 if (tcc_state
->ms_extensions
== 0)
3802 expect("identifier");
3806 if (type_size(&type1
, &align
) < 0) {
3807 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3810 tcc_error("field '%s' has incomplete type",
3811 get_tok_str(v
, NULL
));
3813 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3814 (type1
.t
& VT_STORAGE
))
3815 tcc_error("invalid type for '%s'",
3816 get_tok_str(v
, NULL
));
3820 bit_size
= expr_const();
3821 /* XXX: handle v = 0 case for messages */
3823 tcc_error("negative width in bit-field '%s'",
3824 get_tok_str(v
, NULL
));
3825 if (v
&& bit_size
== 0)
3826 tcc_error("zero width for bit-field '%s'",
3827 get_tok_str(v
, NULL
));
3828 parse_attribute(&ad1
);
3830 size
= type_size(&type1
, &align
);
3831 if (bit_size
>= 0) {
3832 bt
= type1
.t
& VT_BTYPE
;
3838 tcc_error("bitfields must have scalar type");
3840 if (bit_size
> bsize
) {
3841 tcc_error("width of '%s' exceeds its type",
3842 get_tok_str(v
, NULL
));
3843 } else if (bit_size
== bsize
3844 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
3845 /* no need for bit fields */
3847 } else if (bit_size
== 64) {
3848 tcc_error("field width 64 not implemented");
3850 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
3852 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3855 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3856 /* Remember we've seen a real field to check
3857 for placement of flexible array member. */
3860 /* If member is a struct or bit-field, enforce
3861 placing into the struct (as anonymous). */
3863 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3868 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
3873 if (tok
== ';' || tok
== TOK_EOF
)
3880 parse_attribute(&ad
);
3881 struct_layout(type
, &ad
);
3886 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
3888 if (s
->a
.aligned
&& 0 == ad
->a
.aligned
)
3889 ad
->a
.aligned
= s
->a
.aligned
;
3890 if (s
->f
.func_call
&& 0 == ad
->f
.func_call
)
3891 ad
->f
.func_call
= s
->f
.func_call
;
3892 if (s
->f
.func_type
&& 0 == ad
->f
.func_type
)
3893 ad
->f
.func_type
= s
->f
.func_type
;
3898 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3899 are added to the element type, copied because it could be a typedef. */
3900 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3902 while (type
->t
& VT_ARRAY
) {
3903 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3904 type
= &type
->ref
->type
;
3906 type
->t
|= qualifiers
;
3909 /* return 0 if no type declaration. otherwise, return the basic type
3912 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3914 int t
, u
, bt
, st
, type_found
, typespec_found
, g
;
3918 memset(ad
, 0, sizeof(AttributeDef
));
3928 /* currently, we really ignore extension */
3938 if (u
== VT_SHORT
|| u
== VT_LONG
) {
3939 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
3940 tmbt
: tcc_error("too many basic types");
3943 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
3948 t
= (t
& ~VT_BTYPE
) | u
;
3961 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3962 #ifndef TCC_TARGET_PE
3963 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3965 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3966 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3973 #ifdef TCC_TARGET_ARM64
3975 /* GCC's __uint128_t appears in some Linux header files. Make it a
3976 synonym for long double to get the size and alignment right. */
3987 if ((t
& VT_BTYPE
) == VT_LONG
) {
3988 #ifdef TCC_TARGET_PE
3989 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3991 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
4000 struct_decl(&type1
, VT_ENUM
);
4003 type
->ref
= type1
.ref
;
4006 struct_decl(&type1
, VT_STRUCT
);
4009 struct_decl(&type1
, VT_UNION
);
4012 /* type modifiers */
4017 parse_btype_qualify(type
, VT_CONSTANT
);
4025 parse_btype_qualify(type
, VT_VOLATILE
);
4032 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4033 tcc_error("signed and unsigned modifier");
4046 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4047 tcc_error("signed and unsigned modifier");
4048 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4064 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4065 tcc_error("multiple storage classes");
4076 /* GNUC attribute */
4077 case TOK_ATTRIBUTE1
:
4078 case TOK_ATTRIBUTE2
:
4079 parse_attribute(ad
);
4080 if (ad
->attr_mode
) {
4081 u
= ad
->attr_mode
-1;
4082 t
= (t
& ~VT_BTYPE
) | u
;
4090 parse_expr_type(&type1
);
4091 /* remove all storage modifiers except typedef */
4092 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4094 sym_to_attr(ad
, type1
.ref
);
4100 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4103 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4104 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4105 type
->ref
= s
->type
.ref
;
4107 parse_btype_qualify(type
, t
);
4109 /* get attributes from typedef */
4119 if (tcc_state
->char_is_unsigned
) {
4120 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4124 /* long is never used as type */
4125 if ((t
& VT_BTYPE
) == VT_LONG
)
4126 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
4127 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
4129 t
= (t
& ~VT_BTYPE
) | VT_INT
;
4135 /* convert a function parameter type (array to pointer and function to
4136 function pointer) */
4137 static inline void convert_parameter_type(CType
*pt
)
4139 /* remove const and volatile qualifiers (XXX: const could be used
4140 to indicate a const function parameter */
4141 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4142 /* array must be transformed to pointer according to ANSI C */
4144 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4149 ST_FUNC
void parse_asm_str(CString
*astr
)
4152 parse_mult_str(astr
, "string constant");
4155 /* Parse an asm label and return the token */
4156 static int asm_label_instr(void)
4162 parse_asm_str(&astr
);
4165 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4167 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4172 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4174 int n
, l
, t1
, arg_size
, align
;
4175 Sym
**plast
, *s
, *first
;
4180 /* function type, or recursive declarator (return if so) */
4182 if (td
&& !(td
& TYPE_ABSTRACT
))
4186 else if (parse_btype(&pt
, &ad1
))
4197 /* read param name and compute offset */
4198 if (l
!= FUNC_OLD
) {
4199 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4201 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4202 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4203 tcc_error("parameter declared as void");
4204 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4208 expect("identifier");
4209 pt
.t
= VT_VOID
; /* invalid type */
4212 convert_parameter_type(&pt
);
4213 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4219 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4224 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4225 tcc_error("invalid type");
4228 /* if no parameters, then old type prototype */
4231 /* NOTE: const is ignored in returned type as it has a special
4232 meaning in gcc / C++ */
4233 type
->t
&= ~VT_CONSTANT
;
4234 /* some ancient pre-K&R C allows a function to return an array
4235 and the array brackets to be put after the arguments, such
4236 that "int c()[]" means something like "int[] c()" */
4239 skip(']'); /* only handle simple "[]" */
4242 /* we push a anonymous symbol which will contain the function prototype */
4243 ad
->f
.func_args
= arg_size
;
4244 ad
->f
.func_type
= l
;
4245 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4251 } else if (tok
== '[') {
4252 int saved_nocode_wanted
= nocode_wanted
;
4253 /* array definition */
4255 if (tok
== TOK_RESTRICT1
)
4260 if (!local_stack
|| (storage
& VT_STATIC
))
4261 vpushi(expr_const());
4263 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4264 length must always be evaluated, even under nocode_wanted,
4265 so that its size slot is initialized (e.g. under sizeof
4270 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4273 tcc_error("invalid array size");
4275 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4276 tcc_error("size of variable length array should be an integer");
4281 /* parse next post type */
4282 post_type(type
, ad
, storage
, 0);
4283 if (type
->t
== VT_FUNC
)
4284 tcc_error("declaration of an array of functions");
4285 t1
|= type
->t
& VT_VLA
;
4288 loc
-= type_size(&int_type
, &align
);
4292 vla_runtime_type_size(type
, &align
);
4294 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4300 nocode_wanted
= saved_nocode_wanted
;
4302 /* we push an anonymous symbol which will contain the array
4304 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4305 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4311 /* Parse a type declarator (except basic type), and return the type
4312 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4313 expected. 'type' should contain the basic type. 'ad' is the
4314 attribute definition of the basic type. It can be modified by
4315 type_decl(). If this (possibly abstract) declarator is a pointer chain
4316 it returns the innermost pointed to type (equals *type, but is a different
4317 pointer), otherwise returns type itself, that's used for recursive calls. */
4318 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4321 int qualifiers
, storage
;
4323 /* recursive type, remove storage bits first, apply them later again */
4324 storage
= type
->t
& VT_STORAGE
;
4325 type
->t
&= ~VT_STORAGE
;
4328 while (tok
== '*') {
4336 qualifiers
|= VT_CONSTANT
;
4341 qualifiers
|= VT_VOLATILE
;
4347 /* XXX: clarify attribute handling */
4348 case TOK_ATTRIBUTE1
:
4349 case TOK_ATTRIBUTE2
:
4350 parse_attribute(ad
);
4354 type
->t
|= qualifiers
;
4356 /* innermost pointed to type is the one for the first derivation */
4357 ret
= pointed_type(type
);
4361 /* This is possibly a parameter type list for abstract declarators
4362 ('int ()'), use post_type for testing this. */
4363 if (!post_type(type
, ad
, 0, td
)) {
4364 /* It's not, so it's a nested declarator, and the post operations
4365 apply to the innermost pointed to type (if any). */
4366 /* XXX: this is not correct to modify 'ad' at this point, but
4367 the syntax is not clear */
4368 parse_attribute(ad
);
4369 post
= type_decl(type
, ad
, v
, td
);
4372 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4373 /* type identifier */
4377 if (!(td
& TYPE_ABSTRACT
))
4378 expect("identifier");
4381 post_type(post
, ad
, storage
, 0);
4382 parse_attribute(ad
);
4387 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4388 ST_FUNC
int lvalue_type(int t
)
4393 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4395 else if (bt
== VT_SHORT
)
4399 if (t
& VT_UNSIGNED
)
4400 r
|= VT_LVAL_UNSIGNED
;
4404 /* indirection with full error checking and bound check */
4405 ST_FUNC
void indir(void)
4407 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4408 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4412 if (vtop
->r
& VT_LVAL
)
4414 vtop
->type
= *pointed_type(&vtop
->type
);
4415 /* Arrays and functions are never lvalues */
4416 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4417 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4418 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4419 /* if bound checking, the referenced pointer must be checked */
4420 #ifdef CONFIG_TCC_BCHECK
4421 if (tcc_state
->do_bounds_check
)
4422 vtop
->r
|= VT_MUSTBOUND
;
4427 /* pass a parameter to a function and do type checking and casting */
4428 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4433 func_type
= func
->f
.func_type
;
4434 if (func_type
== FUNC_OLD
||
4435 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4436 /* default casting : only need to convert float to double */
4437 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4438 gen_cast_s(VT_DOUBLE
);
4439 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4440 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4441 type
.ref
= vtop
->type
.ref
;
4444 } else if (arg
== NULL
) {
4445 tcc_error("too many arguments to function");
4448 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4449 gen_assign_cast(&type
);
4453 /* parse an expression and return its type without any side effect. */
4454 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4463 /* parse an expression of the form '(type)' or '(expr)' and return its
4465 static void parse_expr_type(CType
*type
)
4471 if (parse_btype(type
, &ad
)) {
4472 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4474 expr_type(type
, gexpr
);
4479 static void parse_type(CType
*type
)
4484 if (!parse_btype(type
, &ad
)) {
4487 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4490 static void parse_builtin_params(int nc
, const char *args
)
4497 while ((c
= *args
++)) {
4501 case 'e': expr_eq(); continue;
4502 case 't': parse_type(&t
); vpush(&t
); continue;
4503 default: tcc_error("internal error"); break;
4511 ST_FUNC
void unary(void)
4513 int n
, t
, align
, size
, r
, sizeof_caller
;
4518 sizeof_caller
= in_sizeof
;
4521 /* XXX: GCC 2.95.3 does not generate a table although it should be
4534 vsetc(&type
, VT_CONST
, &tokc
);
4538 t
= VT_INT
| VT_UNSIGNED
;
4544 t
= VT_LLONG
| VT_UNSIGNED
;
4556 case TOK___FUNCTION__
:
4558 goto tok_identifier
;
4564 /* special function name identifier */
4565 len
= strlen(funcname
) + 1;
4566 /* generate char[len] type */
4571 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4572 if (!NODATA_WANTED
) {
4573 ptr
= section_ptr_add(data_section
, len
);
4574 memcpy(ptr
, funcname
, len
);
4580 #ifdef TCC_TARGET_PE
4581 t
= VT_SHORT
| VT_UNSIGNED
;
4587 /* string parsing */
4589 if (tcc_state
->char_is_unsigned
)
4590 t
= VT_BYTE
| VT_UNSIGNED
;
4592 if (tcc_state
->warn_write_strings
)
4597 memset(&ad
, 0, sizeof(AttributeDef
));
4598 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4603 if (parse_btype(&type
, &ad
)) {
4604 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4606 /* check ISOC99 compound literal */
4608 /* data is allocated locally by default */
4613 /* all except arrays are lvalues */
4614 if (!(type
.t
& VT_ARRAY
))
4615 r
|= lvalue_type(type
.t
);
4616 memset(&ad
, 0, sizeof(AttributeDef
));
4617 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4619 if (sizeof_caller
) {
4626 } else if (tok
== '{') {
4627 int saved_nocode_wanted
= nocode_wanted
;
4629 tcc_error("expected constant");
4630 /* save all registers */
4632 /* statement expression : we do not accept break/continue
4633 inside as GCC does. We do retain the nocode_wanted state,
4634 as statement expressions can't ever be entered from the
4635 outside, so any reactivation of code emission (from labels
4636 or loop heads) can be disabled again after the end of it. */
4637 block(NULL
, NULL
, 1);
4638 nocode_wanted
= saved_nocode_wanted
;
4653 /* functions names must be treated as function pointers,
4654 except for unary '&' and sizeof. Since we consider that
4655 functions are not lvalues, we only have to handle it
4656 there and in function calls. */
4657 /* arrays can also be used although they are not lvalues */
4658 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4659 !(vtop
->type
.t
& VT_ARRAY
))
4661 mk_pointer(&vtop
->type
);
4667 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4668 gen_cast_s(VT_BOOL
);
4669 vtop
->c
.i
= !vtop
->c
.i
;
4670 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4674 vseti(VT_JMP
, gvtst(1, 0));
4686 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4687 tcc_error("pointer not accepted for unary plus");
4688 /* In order to force cast, we add zero, except for floating point
4689 where we really need an noop (otherwise -0.0 will be transformed
4691 if (!is_float(vtop
->type
.t
)) {
4702 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
4703 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
4704 size
= type_size(&type
, &align
);
4705 if (s
&& s
->a
.aligned
)
4706 align
= 1 << (s
->a
.aligned
- 1);
4707 if (t
== TOK_SIZEOF
) {
4708 if (!(type
.t
& VT_VLA
)) {
4710 tcc_error("sizeof applied to an incomplete type");
4713 vla_runtime_type_size(&type
, &align
);
4718 vtop
->type
.t
|= VT_UNSIGNED
;
4721 case TOK_builtin_expect
:
4722 /* __builtin_expect is a no-op for now */
4723 parse_builtin_params(0, "ee");
4726 case TOK_builtin_types_compatible_p
:
4727 parse_builtin_params(0, "tt");
4728 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4729 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4730 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
4734 case TOK_builtin_choose_expr
:
4761 case TOK_builtin_constant_p
:
4762 parse_builtin_params(1, "e");
4763 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4767 case TOK_builtin_frame_address
:
4768 case TOK_builtin_return_address
:
4774 if (tok
!= TOK_CINT
) {
4775 tcc_error("%s only takes positive integers",
4776 tok1
== TOK_builtin_return_address
?
4777 "__builtin_return_address" :
4778 "__builtin_frame_address");
4780 level
= (uint32_t)tokc
.i
;
4785 vset(&type
, VT_LOCAL
, 0); /* local frame */
4787 mk_pointer(&vtop
->type
);
4788 indir(); /* -> parent frame */
4790 if (tok1
== TOK_builtin_return_address
) {
4791 // assume return address is just above frame pointer on stack
4794 mk_pointer(&vtop
->type
);
4799 #ifdef TCC_TARGET_X86_64
4800 #ifdef TCC_TARGET_PE
4801 case TOK_builtin_va_start
:
4802 parse_builtin_params(0, "ee");
4803 r
= vtop
->r
& VT_VALMASK
;
4807 tcc_error("__builtin_va_start expects a local variable");
4809 vtop
->type
= char_pointer_type
;
4814 case TOK_builtin_va_arg_types
:
4815 parse_builtin_params(0, "t");
4816 vpushi(classify_x86_64_va_arg(&vtop
->type
));
4823 #ifdef TCC_TARGET_ARM64
4824 case TOK___va_start
: {
4825 parse_builtin_params(0, "ee");
4829 vtop
->type
.t
= VT_VOID
;
4832 case TOK___va_arg
: {
4833 parse_builtin_params(0, "et");
4841 case TOK___arm64_clear_cache
: {
4842 parse_builtin_params(0, "ee");
4845 vtop
->type
.t
= VT_VOID
;
4849 /* pre operations */
4860 t
= vtop
->type
.t
& VT_BTYPE
;
4862 /* In IEEE negate(x) isn't subtract(0,x), but rather
4866 vtop
->c
.f
= -1.0 * 0.0;
4867 else if (t
== VT_DOUBLE
)
4868 vtop
->c
.d
= -1.0 * 0.0;
4870 vtop
->c
.ld
= -1.0 * 0.0;
4878 goto tok_identifier
;
4880 /* allow to take the address of a label */
4881 if (tok
< TOK_UIDENT
)
4882 expect("label identifier");
4883 s
= label_find(tok
);
4885 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4887 if (s
->r
== LABEL_DECLARED
)
4888 s
->r
= LABEL_FORWARD
;
4891 s
->type
.t
= VT_VOID
;
4892 mk_pointer(&s
->type
);
4893 s
->type
.t
|= VT_STATIC
;
4895 vpushsym(&s
->type
, s
);
4901 CType controlling_type
;
4902 int has_default
= 0;
4905 TokenString
*str
= NULL
;
4909 expr_type(&controlling_type
, expr_eq
);
4910 controlling_type
.t
&= ~(VT_CONSTANT
|VT_VOLATILE
|VT_ARRAY
);
4914 if (tok
== TOK_DEFAULT
) {
4916 tcc_error("too many 'default'");
4922 AttributeDef ad_tmp
;
4925 parse_btype(&cur_type
, &ad_tmp
);
4926 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
4927 if (compare_types(&controlling_type
, &cur_type
, 0)) {
4929 // tcc_error("type match twice");
4939 skip_or_save_block(&str
);
4941 skip_or_save_block(NULL
);
4948 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
4949 tcc_error("type '%s' does not match any association", buf
);
4951 begin_macro(str
, 1);
4960 // special qnan , snan and infinity values
4962 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4966 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4970 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4979 expect("identifier");
4982 const char *name
= get_tok_str(t
, NULL
);
4984 tcc_error("'%s' undeclared", name
);
4985 /* for simple function calls, we tolerate undeclared
4986 external reference to int() function */
4987 if (tcc_state
->warn_implicit_function_declaration
4988 #ifdef TCC_TARGET_PE
4989 /* people must be warned about using undeclared WINAPI functions
4990 (which usually start with uppercase letter) */
4991 || (name
[0] >= 'A' && name
[0] <= 'Z')
4994 tcc_warning("implicit declaration of function '%s'", name
);
4995 s
= external_global_sym(t
, &func_old_type
, 0);
4999 /* A symbol that has a register is a local register variable,
5000 which starts out as VT_LOCAL value. */
5001 if ((r
& VT_VALMASK
) < VT_CONST
)
5002 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5004 vset(&s
->type
, r
, s
->c
);
5005 /* Point to s as backpointer (even without r&VT_SYM).
5006 Will be used by at least the x86 inline asm parser for
5012 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5013 vtop
->c
.i
= s
->enum_val
;
5018 /* post operations */
5020 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5023 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5026 if (tok
== TOK_ARROW
)
5028 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5031 /* expect pointer on structure */
5032 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5033 expect("struct or union");
5034 if (tok
== TOK_CDOUBLE
)
5035 expect("field name");
5037 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5038 expect("field name");
5039 s
= find_field(&vtop
->type
, tok
);
5041 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5042 /* add field offset to pointer */
5043 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5046 /* change type to field type, and set to lvalue */
5047 vtop
->type
= s
->type
;
5048 vtop
->type
.t
|= qualifiers
;
5049 /* an array is never an lvalue */
5050 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5051 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5052 #ifdef CONFIG_TCC_BCHECK
5053 /* if bound checking, the referenced pointer must be checked */
5054 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5055 vtop
->r
|= VT_MUSTBOUND
;
5059 } else if (tok
== '[') {
5065 } else if (tok
== '(') {
5068 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5071 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5072 /* pointer test (no array accepted) */
5073 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5074 vtop
->type
= *pointed_type(&vtop
->type
);
5075 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5079 expect("function pointer");
5082 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5084 /* get return type */
5087 sa
= s
->next
; /* first parameter */
5088 nb_args
= regsize
= 0;
5090 /* compute first implicit argument if a structure is returned */
5091 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5092 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5093 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5094 &ret_align
, ®size
);
5096 /* get some space for the returned structure */
5097 size
= type_size(&s
->type
, &align
);
5098 #ifdef TCC_TARGET_ARM64
5099 /* On arm64, a small struct is return in registers.
5100 It is much easier to write it to memory if we know
5101 that we are allowed to write some extra bytes, so
5102 round the allocated space up to a power of 2: */
5104 while (size
& (size
- 1))
5105 size
= (size
| (size
- 1)) + 1;
5107 loc
= (loc
- size
) & -align
;
5109 ret
.r
= VT_LOCAL
| VT_LVAL
;
5110 /* pass it as 'int' to avoid structure arg passing
5112 vseti(VT_LOCAL
, loc
);
5122 /* return in register */
5123 if (is_float(ret
.type
.t
)) {
5124 ret
.r
= reg_fret(ret
.type
.t
);
5125 #ifdef TCC_TARGET_X86_64
5126 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5130 #ifndef TCC_TARGET_ARM64
5131 #ifdef TCC_TARGET_X86_64
5132 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5134 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5145 gfunc_param_typed(s
, sa
);
5155 tcc_error("too few arguments to function");
5157 gfunc_call(nb_args
);
5160 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5161 vsetc(&ret
.type
, r
, &ret
.c
);
5162 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5165 /* handle packed struct return */
5166 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5169 size
= type_size(&s
->type
, &align
);
5170 /* We're writing whole regs often, make sure there's enough
5171 space. Assume register size is power of 2. */
5172 if (regsize
> align
)
5174 loc
= (loc
- size
) & -align
;
5178 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5182 if (--ret_nregs
== 0)
5186 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5194 ST_FUNC
void expr_prod(void)
5199 while (tok
== '*' || tok
== '/' || tok
== '%') {
5207 ST_FUNC
void expr_sum(void)
5212 while (tok
== '+' || tok
== '-') {
5220 static void expr_shift(void)
5225 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5233 static void expr_cmp(void)
5238 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5239 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5247 static void expr_cmpeq(void)
5252 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5260 static void expr_and(void)
5263 while (tok
== '&') {
5270 static void expr_xor(void)
5273 while (tok
== '^') {
5280 static void expr_or(void)
5283 while (tok
== '|') {
5290 static void expr_land(void)
5293 if (tok
== TOK_LAND
) {
5296 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5297 gen_cast_s(VT_BOOL
);
5302 while (tok
== TOK_LAND
) {
5318 if (tok
!= TOK_LAND
) {
5331 static void expr_lor(void)
5334 if (tok
== TOK_LOR
) {
5337 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5338 gen_cast_s(VT_BOOL
);
5343 while (tok
== TOK_LOR
) {
5359 if (tok
!= TOK_LOR
) {
5372 /* Assuming vtop is a value used in a conditional context
5373 (i.e. compared with zero) return 0 if it's false, 1 if
5374 true and -1 if it can't be statically determined. */
5375 static int condition_3way(void)
5378 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5379 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5381 gen_cast_s(VT_BOOL
);
5388 static void expr_cond(void)
5390 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5392 CType type
, type1
, type2
;
5397 c
= condition_3way();
5398 g
= (tok
== ':' && gnu_ext
);
5400 /* needed to avoid having different registers saved in
5402 if (is_float(vtop
->type
.t
)) {
5404 #ifdef TCC_TARGET_X86_64
5405 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5430 sv
= *vtop
; /* save value to handle it later */
5431 vtop
--; /* no vpop so that FP stack is not flushed */
5449 bt1
= t1
& VT_BTYPE
;
5451 bt2
= t2
& VT_BTYPE
;
5454 /* cast operands to correct type according to ISOC rules */
5455 if (is_float(bt1
) || is_float(bt2
)) {
5456 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5457 type
.t
= VT_LDOUBLE
;
5459 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5464 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5465 /* cast to biggest op */
5467 /* convert to unsigned if it does not fit in a long long */
5468 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5469 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5470 type
.t
|= VT_UNSIGNED
;
5471 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5472 /* If one is a null ptr constant the result type
5474 if (is_null_pointer (vtop
))
5476 else if (is_null_pointer (&sv
))
5478 /* XXX: test pointer compatibility, C99 has more elaborate
5482 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5483 /* XXX: test function pointer compatibility */
5484 type
= bt1
== VT_FUNC
? type1
: type2
;
5485 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5486 /* XXX: test structure compatibility */
5487 type
= bt1
== VT_STRUCT
? type1
: type2
;
5488 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5489 /* NOTE: as an extension, we accept void on only one side */
5492 /* integer operations */
5494 /* convert to unsigned if it does not fit in an integer */
5495 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5496 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5497 type
.t
|= VT_UNSIGNED
;
5499 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5500 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5501 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5504 /* now we convert second operand */
5508 mk_pointer(&vtop
->type
);
5510 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5515 if (is_float(type
.t
)) {
5517 #ifdef TCC_TARGET_X86_64
5518 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5522 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5523 /* for long longs, we use fixed registers to avoid having
5524 to handle a complicated move */
5535 /* this is horrible, but we must also convert first
5541 mk_pointer(&vtop
->type
);
5543 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5549 move_reg(r2
, r1
, type
.t
);
5559 static void expr_eq(void)
5565 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5566 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5567 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5582 ST_FUNC
void gexpr(void)
5593 /* parse a constant expression and return value in vtop. */
5594 static void expr_const1(void)
5603 /* parse an integer constant and return its value. */
5604 static inline int64_t expr_const64(void)
5608 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5609 expect("constant expression");
5615 /* parse an integer constant and return its value.
5616 Complain if it doesn't fit 32bit (signed or unsigned). */
5617 ST_FUNC
int expr_const(void)
5620 int64_t wc
= expr_const64();
5622 if (c
!= wc
&& (unsigned)c
!= wc
)
5623 tcc_error("constant exceeds 32 bit");
5627 /* return the label token if current token is a label, otherwise
5629 static int is_label(void)
5633 /* fast test first */
5634 if (tok
< TOK_UIDENT
)
5636 /* no need to save tokc because tok is an identifier */
5642 unget_tok(last_tok
);
5647 #ifndef TCC_TARGET_ARM64
5648 static void gfunc_return(CType
*func_type
)
5650 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5651 CType type
, ret_type
;
5652 int ret_align
, ret_nregs
, regsize
;
5653 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5654 &ret_align
, ®size
);
5655 if (0 == ret_nregs
) {
5656 /* if returning structure, must copy it to implicit
5657 first pointer arg location */
5660 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5663 /* copy structure value to pointer */
5666 /* returning structure packed into registers */
5667 int r
, size
, addr
, align
;
5668 size
= type_size(func_type
,&align
);
5669 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5670 (vtop
->c
.i
& (ret_align
-1)))
5671 && (align
& (ret_align
-1))) {
5672 loc
= (loc
- size
) & -ret_align
;
5675 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5679 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5681 vtop
->type
= ret_type
;
5682 if (is_float(ret_type
.t
))
5683 r
= rc_fret(ret_type
.t
);
5694 if (--ret_nregs
== 0)
5696 /* We assume that when a structure is returned in multiple
5697 registers, their classes are consecutive values of the
5700 vtop
->c
.i
+= regsize
;
5704 } else if (is_float(func_type
->t
)) {
5705 gv(rc_fret(func_type
->t
));
5709 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5713 static int case_cmp(const void *pa
, const void *pb
)
5715 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5716 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5717 return a
< b
? -1 : a
> b
;
5720 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5724 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5742 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5744 gcase(base
, len
/2, bsym
);
5745 if (cur_switch
->def_sym
)
5746 gjmp_addr(cur_switch
->def_sym
);
5748 *bsym
= gjmp(*bsym
);
5752 base
+= e
; len
-= e
;
5762 if (p
->v1
== p
->v2
) {
5764 gtst_addr(0, p
->sym
);
5774 gtst_addr(0, p
->sym
);
5780 static void block(int *bsym
, int *csym
, int is_expr
)
5782 int a
, b
, c
, d
, cond
;
5785 /* generate line number info */
5786 if (tcc_state
->do_debug
)
5787 tcc_debug_line(tcc_state
);
5790 /* default return value is (void) */
5792 vtop
->type
.t
= VT_VOID
;
5795 if (tok
== TOK_IF
) {
5797 int saved_nocode_wanted
= nocode_wanted
;
5802 cond
= condition_3way();
5808 nocode_wanted
|= 0x20000000;
5809 block(bsym
, csym
, 0);
5811 nocode_wanted
= saved_nocode_wanted
;
5813 if (c
== TOK_ELSE
) {
5818 nocode_wanted
|= 0x20000000;
5819 block(bsym
, csym
, 0);
5820 gsym(d
); /* patch else jmp */
5822 nocode_wanted
= saved_nocode_wanted
;
5825 } else if (tok
== TOK_WHILE
) {
5826 int saved_nocode_wanted
;
5827 nocode_wanted
&= ~0x20000000;
5837 saved_nocode_wanted
= nocode_wanted
;
5839 nocode_wanted
= saved_nocode_wanted
;
5844 } else if (tok
== '{') {
5846 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5849 /* record local declaration stack position */
5851 llabel
= local_label_stack
;
5854 /* handle local labels declarations */
5855 if (tok
== TOK_LABEL
) {
5858 if (tok
< TOK_UIDENT
)
5859 expect("label identifier");
5860 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5870 while (tok
!= '}') {
5871 if ((a
= is_label()))
5878 block(bsym
, csym
, is_expr
);
5881 /* pop locally defined labels */
5882 label_pop(&local_label_stack
, llabel
, is_expr
);
5883 /* pop locally defined symbols */
5885 /* In the is_expr case (a statement expression is finished here),
5886 vtop might refer to symbols on the local_stack. Either via the
5887 type or via vtop->sym. We can't pop those nor any that in turn
5888 might be referred to. To make it easier we don't roll back
5889 any symbols in that case; some upper level call to block() will
5890 do that. We do have to remove such symbols from the lookup
5891 tables, though. sym_pop will do that. */
5892 sym_pop(&local_stack
, s
, is_expr
);
5894 /* Pop VLA frames and restore stack pointer if required */
5895 if (vlas_in_scope
> saved_vlas_in_scope
) {
5896 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5899 vlas_in_scope
= saved_vlas_in_scope
;
5902 } else if (tok
== TOK_RETURN
) {
5906 gen_assign_cast(&func_vt
);
5907 gfunc_return(&func_vt
);
5910 /* jump unless last stmt in top-level block */
5911 if (tok
!= '}' || local_scope
!= 1)
5913 nocode_wanted
|= 0x20000000;
5914 } else if (tok
== TOK_BREAK
) {
5917 tcc_error("cannot break");
5918 *bsym
= gjmp(*bsym
);
5921 nocode_wanted
|= 0x20000000;
5922 } else if (tok
== TOK_CONTINUE
) {
5925 tcc_error("cannot continue");
5926 vla_sp_restore_root();
5927 *csym
= gjmp(*csym
);
5930 } else if (tok
== TOK_FOR
) {
5932 int saved_nocode_wanted
;
5933 nocode_wanted
&= ~0x20000000;
5939 /* c99 for-loop init decl? */
5940 if (!decl0(VT_LOCAL
, 1, NULL
)) {
5941 /* no, regular for-loop init expr */
5967 saved_nocode_wanted
= nocode_wanted
;
5969 nocode_wanted
= saved_nocode_wanted
;
5974 sym_pop(&local_stack
, s
, 0);
5977 if (tok
== TOK_DO
) {
5978 int saved_nocode_wanted
;
5979 nocode_wanted
&= ~0x20000000;
5985 saved_nocode_wanted
= nocode_wanted
;
5993 nocode_wanted
= saved_nocode_wanted
;
5998 if (tok
== TOK_SWITCH
) {
5999 struct switch_t
*saved
, sw
;
6000 int saved_nocode_wanted
= nocode_wanted
;
6006 switchval
= *vtop
--;
6008 b
= gjmp(0); /* jump to first case */
6009 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
6013 nocode_wanted
= saved_nocode_wanted
;
6014 a
= gjmp(a
); /* add implicit break */
6017 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6018 for (b
= 1; b
< sw
.n
; b
++)
6019 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6020 tcc_error("duplicate case value");
6021 /* Our switch table sorting is signed, so the compared
6022 value needs to be as well when it's 64bit. */
6023 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6024 switchval
.type
.t
&= ~VT_UNSIGNED
;
6026 gcase(sw
.p
, sw
.n
, &a
);
6029 gjmp_addr(sw
.def_sym
);
6030 dynarray_reset(&sw
.p
, &sw
.n
);
6035 if (tok
== TOK_CASE
) {
6036 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6039 nocode_wanted
&= ~0x20000000;
6041 cr
->v1
= cr
->v2
= expr_const64();
6042 if (gnu_ext
&& tok
== TOK_DOTS
) {
6044 cr
->v2
= expr_const64();
6045 if (cr
->v2
< cr
->v1
)
6046 tcc_warning("empty case range");
6049 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6052 goto block_after_label
;
6054 if (tok
== TOK_DEFAULT
) {
6059 if (cur_switch
->def_sym
)
6060 tcc_error("too many 'default'");
6061 cur_switch
->def_sym
= ind
;
6063 goto block_after_label
;
6065 if (tok
== TOK_GOTO
) {
6067 if (tok
== '*' && gnu_ext
) {
6071 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6074 } else if (tok
>= TOK_UIDENT
) {
6075 s
= label_find(tok
);
6076 /* put forward definition if needed */
6078 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6080 if (s
->r
== LABEL_DECLARED
)
6081 s
->r
= LABEL_FORWARD
;
6083 vla_sp_restore_root();
6084 if (s
->r
& LABEL_FORWARD
)
6085 s
->jnext
= gjmp(s
->jnext
);
6087 gjmp_addr(s
->jnext
);
6090 expect("label identifier");
6093 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
6102 if (s
->r
== LABEL_DEFINED
)
6103 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6105 s
->r
= LABEL_DEFINED
;
6107 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
6111 /* we accept this, but it is a mistake */
6113 nocode_wanted
&= ~0x20000000;
6115 tcc_warning("deprecated use of label at end of compound statement");
6119 block(bsym
, csym
, is_expr
);
6122 /* expression case */
6137 /* This skips over a stream of tokens containing balanced {} and ()
6138 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6139 with a '{'). If STR then allocates and stores the skipped tokens
6140 in *STR. This doesn't check if () and {} are nested correctly,
6141 i.e. "({)}" is accepted. */
6142 static void skip_or_save_block(TokenString
**str
)
6144 int braces
= tok
== '{';
6147 *str
= tok_str_alloc();
6149 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6151 if (tok
== TOK_EOF
) {
6152 if (str
|| level
> 0)
6153 tcc_error("unexpected end of file");
6158 tok_str_add_tok(*str
);
6161 if (t
== '{' || t
== '(') {
6163 } else if (t
== '}' || t
== ')') {
6165 if (level
== 0 && braces
&& t
== '}')
6170 tok_str_add(*str
, -1);
6171 tok_str_add(*str
, 0);
6175 #define EXPR_CONST 1
6178 static void parse_init_elem(int expr_type
)
6180 int saved_global_expr
;
6183 /* compound literals must be allocated globally in this case */
6184 saved_global_expr
= global_expr
;
6187 global_expr
= saved_global_expr
;
6188 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6189 (compound literals). */
6190 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6191 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6192 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6193 #ifdef TCC_TARGET_PE
6194 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6197 tcc_error("initializer element is not constant");
6205 /* put zeros for variable based init */
6206 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6209 /* nothing to do because globals are already set to zero */
6211 vpush_global_sym(&func_old_type
, TOK_memset
);
6213 #ifdef TCC_TARGET_ARM
6224 /* t is the array or struct type. c is the array or struct
6225 address. cur_field is the pointer to the current
6226 field, for arrays the 'c' member contains the current start
6227 index. 'size_only' is true if only size info is needed (only used
6228 in arrays). al contains the already initialized length of the
6229 current container (starting at c). This returns the new length of that. */
6230 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6231 Sym
**cur_field
, int size_only
, int al
)
6234 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6235 unsigned long corig
= c
;
6239 if (gnu_ext
&& (l
= is_label()) != 0)
6241 /* NOTE: we only support ranges for last designator */
6242 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6244 if (!(type
->t
& VT_ARRAY
))
6245 expect("array type");
6247 index
= index_last
= expr_const();
6248 if (tok
== TOK_DOTS
&& gnu_ext
) {
6250 index_last
= expr_const();
6254 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6256 tcc_error("invalid index");
6258 (*cur_field
)->c
= index_last
;
6259 type
= pointed_type(type
);
6260 elem_size
= type_size(type
, &align
);
6261 c
+= index
* elem_size
;
6262 nb_elems
= index_last
- index
+ 1;
6268 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6269 expect("struct/union type");
6270 f
= find_field(type
, l
);
6283 } else if (!gnu_ext
) {
6287 if (type
->t
& VT_ARRAY
) {
6288 index
= (*cur_field
)->c
;
6289 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6290 tcc_error("index too large");
6291 type
= pointed_type(type
);
6292 c
+= index
* type_size(type
, &align
);
6295 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6296 *cur_field
= f
= f
->next
;
6298 tcc_error("too many field init");
6303 /* must put zero in holes (note that doing it that way
6304 ensures that it even works with designators) */
6305 if (!size_only
&& c
- corig
> al
)
6306 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6307 decl_initializer(type
, sec
, c
, 0, size_only
);
6309 /* XXX: make it more general */
6310 if (!size_only
&& nb_elems
> 1) {
6311 unsigned long c_end
;
6316 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6317 for (i
= 1; i
< nb_elems
; i
++) {
6318 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6323 } else if (!NODATA_WANTED
) {
6324 c_end
= c
+ nb_elems
* elem_size
;
6325 if (c_end
> sec
->data_allocated
)
6326 section_realloc(sec
, c_end
);
6327 src
= sec
->data
+ c
;
6329 for(i
= 1; i
< nb_elems
; i
++) {
6331 memcpy(dst
, src
, elem_size
);
6335 c
+= nb_elems
* type_size(type
, &align
);
6341 /* store a value or an expression directly in global data or in local array */
6342 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6349 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6353 /* XXX: not portable */
6354 /* XXX: generate error if incorrect relocation */
6355 gen_assign_cast(&dtype
);
6356 bt
= type
->t
& VT_BTYPE
;
6358 if ((vtop
->r
& VT_SYM
)
6361 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6362 || (type
->t
& VT_BITFIELD
))
6363 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6365 tcc_error("initializer element is not computable at load time");
6367 if (NODATA_WANTED
) {
6372 size
= type_size(type
, &align
);
6373 section_reserve(sec
, c
+ size
);
6374 ptr
= sec
->data
+ c
;
6376 /* XXX: make code faster ? */
6377 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6378 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6379 /* XXX This rejects compound literals like
6380 '(void *){ptr}'. The problem is that '&sym' is
6381 represented the same way, which would be ruled out
6382 by the SYM_FIRST_ANOM check above, but also '"string"'
6383 in 'char *p = "string"' is represented the same
6384 with the type being VT_PTR and the symbol being an
6385 anonymous one. That is, there's no difference in vtop
6386 between '(void *){x}' and '&(void *){x}'. Ignore
6387 pointer typed entities here. Hopefully no real code
6388 will every use compound literals with scalar type. */
6389 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6390 /* These come from compound literals, memcpy stuff over. */
6394 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6395 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6396 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6398 /* We need to copy over all memory contents, and that
6399 includes relocations. Use the fact that relocs are
6400 created it order, so look from the end of relocs
6401 until we hit one before the copied region. */
6402 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6403 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6404 while (num_relocs
--) {
6406 if (rel
->r_offset
>= esym
->st_value
+ size
)
6408 if (rel
->r_offset
< esym
->st_value
)
6410 /* Note: if the same fields are initialized multiple
6411 times (possible with designators) then we possibly
6412 add multiple relocations for the same offset here.
6413 That would lead to wrong code, the last reloc needs
6414 to win. We clean this up later after the whole
6415 initializer is parsed. */
6416 put_elf_reloca(symtab_section
, sec
,
6417 c
+ rel
->r_offset
- esym
->st_value
,
6418 ELFW(R_TYPE
)(rel
->r_info
),
6419 ELFW(R_SYM
)(rel
->r_info
),
6429 if (type
->t
& VT_BITFIELD
) {
6430 int bit_pos
, bit_size
, bits
, n
;
6431 unsigned char *p
, v
, m
;
6432 bit_pos
= BIT_POS(vtop
->type
.t
);
6433 bit_size
= BIT_SIZE(vtop
->type
.t
);
6434 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6435 bit_pos
&= 7, bits
= 0;
6440 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6441 m
= ((1 << n
) - 1) << bit_pos
;
6442 *p
= (*p
& ~m
) | (v
& m
);
6443 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6447 /* XXX: when cross-compiling we assume that each type has the
6448 same representation on host and target, which is likely to
6449 be wrong in the case of long double */
6451 vtop
->c
.i
= vtop
->c
.i
!= 0;
6453 *(char *)ptr
|= vtop
->c
.i
;
6456 *(short *)ptr
|= vtop
->c
.i
;
6459 *(float*)ptr
= vtop
->c
.f
;
6462 *(double *)ptr
= vtop
->c
.d
;
6465 if (sizeof(long double) == LDOUBLE_SIZE
)
6466 *(long double *)ptr
= vtop
->c
.ld
;
6467 else if (sizeof(double) == LDOUBLE_SIZE
)
6468 *(double *)ptr
= (double)vtop
->c
.ld
;
6469 #if (defined __i386__ || defined __x86_64__) && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
6470 else if (sizeof (long double) >= 10)
6471 memcpy(memset(ptr
, 0, LDOUBLE_SIZE
), &vtop
->c
.ld
, 10);
6473 else if (sizeof (long double) == sizeof (double))
6474 __asm__("fldl %1\nfstpt %0\n" : "=m"
6475 (memset(ptr
, 0, LDOUBLE_SIZE
), ptr
) : "m" (vtop
->c
.ld
));
6479 tcc_error("can't cross compile long double constants");
6483 *(long long *)ptr
|= vtop
->c
.i
;
6490 addr_t val
= vtop
->c
.i
;
6492 if (vtop
->r
& VT_SYM
)
6493 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6495 *(addr_t
*)ptr
|= val
;
6497 if (vtop
->r
& VT_SYM
)
6498 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6499 *(addr_t
*)ptr
|= val
;
6505 int val
= vtop
->c
.i
;
6507 if (vtop
->r
& VT_SYM
)
6508 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6512 if (vtop
->r
& VT_SYM
)
6513 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6522 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6529 /* 't' contains the type and storage info. 'c' is the offset of the
6530 object in section 'sec'. If 'sec' is NULL, it means stack based
6531 allocation. 'first' is true if array '{' must be read (multi
6532 dimension implicit array init handling). 'size_only' is true if
6533 size only evaluation is wanted (only for arrays). */
6534 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6535 int first
, int size_only
)
6537 int len
, n
, no_oblock
, nb
, i
;
6544 /* If we currently are at an '}' or ',' we have read an initializer
6545 element in one of our callers, and not yet consumed it. */
6546 have_elem
= tok
== '}' || tok
== ',';
6547 if (!have_elem
&& tok
!= '{' &&
6548 /* In case of strings we have special handling for arrays, so
6549 don't consume them as initializer value (which would commit them
6550 to some anonymous symbol). */
6551 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6553 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6558 !(type
->t
& VT_ARRAY
) &&
6559 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6560 The source type might have VT_CONSTANT set, which is
6561 of course assignable to non-const elements. */
6562 is_compatible_unqualified_types(type
, &vtop
->type
)) {
6563 init_putv(type
, sec
, c
);
6564 } else if (type
->t
& VT_ARRAY
) {
6567 t1
= pointed_type(type
);
6568 size1
= type_size(t1
, &align1
);
6571 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6574 tcc_error("character array initializer must be a literal,"
6575 " optionally enclosed in braces");
6580 /* only parse strings here if correct type (otherwise: handle
6581 them as ((w)char *) expressions */
6582 if ((tok
== TOK_LSTR
&&
6583 #ifdef TCC_TARGET_PE
6584 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6586 (t1
->t
& VT_BTYPE
) == VT_INT
6588 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6590 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6593 /* compute maximum number of chars wanted */
6595 cstr_len
= tokc
.str
.size
;
6597 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6600 if (n
>= 0 && nb
> (n
- len
))
6604 tcc_warning("initializer-string for array is too long");
6605 /* in order to go faster for common case (char
6606 string in global variable, we handle it
6608 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6610 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
6614 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6616 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6618 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
6625 /* only add trailing zero if enough storage (no
6626 warning in this case since it is standard) */
6627 if (n
< 0 || len
< n
) {
6630 init_putv(t1
, sec
, c
+ (len
* size1
));
6641 while (tok
!= '}' || have_elem
) {
6642 len
= decl_designator(type
, sec
, c
, &f
, size_only
, len
);
6644 if (type
->t
& VT_ARRAY
) {
6646 /* special test for multi dimensional arrays (may not
6647 be strictly correct if designators are used at the
6649 if (no_oblock
&& len
>= n
*size1
)
6652 if (s
->type
.t
== VT_UNION
)
6656 if (no_oblock
&& f
== NULL
)
6665 /* put zeros at the end */
6666 if (!size_only
&& len
< n
*size1
)
6667 init_putz(sec
, c
+ len
, n
*size1
- len
);
6670 /* patch type size if needed, which happens only for array types */
6672 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
6673 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6676 if (first
|| tok
== '{') {
6684 } else if (tok
== '{') {
6686 decl_initializer(type
, sec
, c
, first
, size_only
);
6688 } else if (size_only
) {
6689 /* If we supported only ISO C we wouldn't have to accept calling
6690 this on anything than an array size_only==1 (and even then
6691 only on the outermost level, so no recursion would be needed),
6692 because initializing a flex array member isn't supported.
6693 But GNU C supports it, so we need to recurse even into
6694 subfields of structs and arrays when size_only is set. */
6695 /* just skip expression */
6696 skip_or_save_block(NULL
);
6699 /* This should happen only when we haven't parsed
6700 the init element above for fear of committing a
6701 string constant to memory too early. */
6702 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6703 expect("string constant");
6704 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6706 init_putv(type
, sec
, c
);
6710 /* parse an initializer for type 't' if 'has_init' is non zero, and
6711 allocate space in local or global data space ('r' is either
6712 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6713 variable 'v' of scope 'scope' is declared before initializers
6714 are parsed. If 'v' is zero, then a reference to the new object
6715 is put in the value stack. If 'has_init' is 2, a special parsing
6716 is done to handle string constants. */
6717 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6718 int has_init
, int v
, int scope
)
6720 int size
, align
, addr
;
6721 TokenString
*init_str
= NULL
;
6724 Sym
*flexible_array
;
6726 int saved_nocode_wanted
= nocode_wanted
;
6727 #ifdef CONFIG_TCC_BCHECK
6728 int bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
6731 if (type
->t
& VT_STATIC
)
6732 nocode_wanted
|= NODATA_WANTED
? 0x40000000 : 0x80000000;
6734 flexible_array
= NULL
;
6735 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6736 Sym
*field
= type
->ref
->next
;
6739 field
= field
->next
;
6740 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6741 flexible_array
= field
;
6745 size
= type_size(type
, &align
);
6746 /* If unknown size, we must evaluate it before
6747 evaluating initializers because
6748 initializers can generate global data too
6749 (e.g. string pointers or ISOC99 compound
6750 literals). It also simplifies local
6751 initializers handling */
6752 if (size
< 0 || (flexible_array
&& has_init
)) {
6754 tcc_error("unknown type size");
6755 /* get all init string */
6756 if (has_init
== 2) {
6757 init_str
= tok_str_alloc();
6758 /* only get strings */
6759 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6760 tok_str_add_tok(init_str
);
6763 tok_str_add(init_str
, -1);
6764 tok_str_add(init_str
, 0);
6766 skip_or_save_block(&init_str
);
6771 begin_macro(init_str
, 1);
6773 decl_initializer(type
, NULL
, 0, 1, 1);
6774 /* prepare second initializer parsing */
6775 macro_ptr
= init_str
->str
;
6778 /* if still unknown size, error */
6779 size
= type_size(type
, &align
);
6781 tcc_error("unknown type size");
6783 /* If there's a flex member and it was used in the initializer
6785 if (flexible_array
&&
6786 flexible_array
->type
.ref
->c
> 0)
6787 size
+= flexible_array
->type
.ref
->c
6788 * pointed_size(&flexible_array
->type
);
6789 /* take into account specified alignment if bigger */
6790 if (ad
->a
.aligned
) {
6791 int speca
= 1 << (ad
->a
.aligned
- 1);
6794 } else if (ad
->a
.packed
) {
6799 size
= 0, align
= 1;
6801 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6803 #ifdef CONFIG_TCC_BCHECK
6804 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
6808 loc
= (loc
- size
) & -align
;
6810 #ifdef CONFIG_TCC_BCHECK
6811 /* handles bounds */
6812 /* XXX: currently, since we do only one pass, we cannot track
6813 '&' operators, so we add only arrays */
6814 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
6816 /* add padding between regions */
6818 /* then add local bound info */
6819 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6820 bounds_ptr
[0] = addr
;
6821 bounds_ptr
[1] = size
;
6825 /* local variable */
6826 #ifdef CONFIG_TCC_ASM
6827 if (ad
->asm_label
) {
6828 int reg
= asm_parse_regvar(ad
->asm_label
);
6830 r
= (r
& ~VT_VALMASK
) | reg
;
6833 sym
= sym_push(v
, type
, r
, addr
);
6836 /* push local reference */
6837 vset(type
, r
, addr
);
6840 if (v
&& scope
== VT_CONST
) {
6841 /* see if the symbol was already defined */
6844 patch_storage(sym
, ad
, type
);
6845 if (sym
->type
.t
& VT_EXTERN
) {
6846 /* if the variable is extern, it was not allocated */
6847 sym
->type
.t
&= ~VT_EXTERN
;
6848 /* set array size if it was omitted in extern
6850 if ((sym
->type
.t
& VT_ARRAY
) &&
6851 sym
->type
.ref
->c
< 0 &&
6853 sym
->type
.ref
->c
= type
->ref
->c
;
6854 } else if (!has_init
) {
6855 /* we accept several definitions of the same
6856 global variable. this is tricky, because we
6857 must play with the SHN_COMMON type of the symbol */
6858 /* no init data, we won't add more to the symbol */
6860 } else if (sym
->c
) {
6862 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6863 if (esym
->st_shndx
== data_section
->sh_num
)
6864 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6869 /* allocate symbol in corresponding section */
6874 else if (tcc_state
->nocommon
)
6879 addr
= section_add(sec
, size
, align
);
6880 #ifdef CONFIG_TCC_BCHECK
6881 /* add padding if bound check */
6883 section_add(sec
, 1, 1);
6886 addr
= align
; /* SHN_COMMON is special, symbol value is align */
6887 sec
= common_section
;
6892 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6893 patch_storage(sym
, ad
, NULL
);
6895 /* Local statics have a scope until now (for
6896 warnings), remove it here. */
6898 /* update symbol definition */
6899 put_extern_sym(sym
, sec
, addr
, size
);
6901 /* push global reference */
6902 sym
= get_sym_ref(type
, sec
, addr
, size
);
6903 vpushsym(type
, sym
);
6907 #ifdef CONFIG_TCC_BCHECK
6908 /* handles bounds now because the symbol must be defined
6909 before for the relocation */
6913 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
6914 /* then add global bound info */
6915 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6916 bounds_ptr
[0] = 0; /* relocated */
6917 bounds_ptr
[1] = size
;
6922 if (type
->t
& VT_VLA
) {
6928 /* save current stack pointer */
6929 if (vlas_in_scope
== 0) {
6930 if (vla_sp_root_loc
== -1)
6931 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6932 gen_vla_sp_save(vla_sp_root_loc
);
6935 vla_runtime_type_size(type
, &a
);
6936 gen_vla_alloc(type
, a
);
6937 gen_vla_sp_save(addr
);
6941 } else if (has_init
) {
6942 size_t oldreloc_offset
= 0;
6943 if (sec
&& sec
->reloc
)
6944 oldreloc_offset
= sec
->reloc
->data_offset
;
6945 decl_initializer(type
, sec
, addr
, 1, 0);
6946 if (sec
&& sec
->reloc
)
6947 squeeze_multi_relocs(sec
, oldreloc_offset
);
6948 /* patch flexible array member size back to -1, */
6949 /* for possible subsequent similar declarations */
6951 flexible_array
->type
.ref
->c
= -1;
6955 /* restore parse state if needed */
6961 nocode_wanted
= saved_nocode_wanted
;
6964 /* parse a function defined by symbol 'sym' and generate its code in
6965 'cur_text_section' */
6966 static void gen_function(Sym
*sym
)
6969 ind
= cur_text_section
->data_offset
;
6970 /* NOTE: we patch the symbol size later */
6971 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6972 funcname
= get_tok_str(sym
->v
, NULL
);
6974 /* Initialize VLA state */
6976 vla_sp_root_loc
= -1;
6977 /* put debug symbol */
6978 tcc_debug_funcstart(tcc_state
, sym
);
6979 /* push a dummy symbol to enable local sym storage */
6980 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6981 local_scope
= 1; /* for function parameters */
6982 gfunc_prolog(&sym
->type
);
6985 block(NULL
, NULL
, 0);
6989 cur_text_section
->data_offset
= ind
;
6990 label_pop(&global_label_stack
, NULL
, 0);
6991 /* reset local stack */
6993 sym_pop(&local_stack
, NULL
, 0);
6994 /* end of function */
6995 /* patch symbol size */
6996 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6998 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
6999 /* It's better to crash than to generate wrong code */
7000 cur_text_section
= NULL
;
7001 funcname
= ""; /* for safety */
7002 func_vt
.t
= VT_VOID
; /* for safety */
7003 func_var
= 0; /* for safety */
7004 ind
= 0; /* for safety */
7005 nocode_wanted
= 0x80000000;
7009 static void gen_inline_functions(TCCState
*s
)
7012 int inline_generated
, i
, ln
;
7013 struct InlineFunc
*fn
;
7015 ln
= file
->line_num
;
7016 /* iterate while inline function are referenced */
7018 inline_generated
= 0;
7019 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7020 fn
= s
->inline_fns
[i
];
7022 if (sym
&& sym
->c
) {
7023 /* the function was used: generate its code and
7024 convert it to a normal function */
7027 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7028 sym
->type
.t
&= ~VT_INLINE
;
7030 begin_macro(fn
->func_str
, 1);
7032 cur_text_section
= text_section
;
7036 inline_generated
= 1;
7039 } while (inline_generated
);
7040 file
->line_num
= ln
;
7043 ST_FUNC
void free_inline_functions(TCCState
*s
)
7046 /* free tokens of unused inline functions */
7047 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7048 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7050 tok_str_free(fn
->func_str
);
7052 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7055 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7056 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7057 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7065 if (!parse_btype(&btype
, &ad
)) {
7066 if (is_for_loop_init
)
7068 /* skip redundant ';' if not in old parameter decl scope */
7069 if (tok
== ';' && l
!= VT_CMP
) {
7073 if (l
== VT_CONST
&&
7074 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7075 /* global asm block */
7079 /* special test for old K&R protos without explicit int
7080 type. Only accepted when defining global data */
7081 if (l
!= VT_CONST
|| tok
< TOK_UIDENT
)
7086 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7087 int v
= btype
.ref
->v
;
7088 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7089 tcc_warning("unnamed struct/union that defines no instances");
7093 if (IS_ENUM(btype
.t
)) {
7098 while (1) { /* iterate thru each declaration */
7100 /* If the base type itself was an array type of unspecified
7101 size (like in 'typedef int arr[]; arr x = {1};') then
7102 we will overwrite the unknown size by the real one for
7103 this decl. We need to unshare the ref symbol holding
7105 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7106 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7108 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7112 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7113 printf("type = '%s'\n", buf
);
7116 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7117 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7118 tcc_error("function without file scope cannot be static");
7120 /* if old style function prototype, we accept a
7123 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7124 decl0(VT_CMP
, 0, sym
);
7127 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7128 ad
.asm_label
= asm_label_instr();
7129 /* parse one last attribute list, after asm label */
7130 parse_attribute(&ad
);
7135 #ifdef TCC_TARGET_PE
7136 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7137 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7138 tcc_error("cannot have dll linkage with static or typedef");
7139 if (ad
.a
.dllimport
) {
7140 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7143 type
.t
|= VT_EXTERN
;
7149 tcc_error("cannot use local functions");
7150 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7151 expect("function definition");
7153 /* reject abstract declarators in function definition
7154 make old style params without decl have int type */
7156 while ((sym
= sym
->next
) != NULL
) {
7157 if (!(sym
->v
& ~SYM_FIELD
))
7158 expect("identifier");
7159 if (sym
->type
.t
== VT_VOID
)
7160 sym
->type
= int_type
;
7163 /* XXX: cannot do better now: convert extern line to static inline */
7164 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7165 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7170 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7173 ref
= sym
->type
.ref
;
7175 /* use func_call from prototype if not defined */
7176 if (ref
->f
.func_call
!= FUNC_CDECL
7177 && type
.ref
->f
.func_call
== FUNC_CDECL
)
7178 type
.ref
->f
.func_call
= ref
->f
.func_call
;
7180 /* use static from prototype */
7181 if (sym
->type
.t
& VT_STATIC
)
7182 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7184 /* If the definition has no visibility use the
7185 one from prototype. */
7186 if (!type
.ref
->a
.visibility
)
7187 type
.ref
->a
.visibility
= ref
->a
.visibility
;
7188 /* apply other storage attributes from prototype */
7189 type
.ref
->a
.dllexport
|= ref
->a
.dllexport
;
7190 type
.ref
->a
.weak
|= ref
->a
.weak
;
7192 if (!is_compatible_types(&sym
->type
, &type
)) {
7194 tcc_error("incompatible types for redefinition of '%s'",
7195 get_tok_str(v
, NULL
));
7197 if (ref
->f
.func_body
)
7198 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7199 /* if symbol is already defined, then put complete type */
7203 /* put function symbol */
7204 sym
= global_identifier_push(v
, type
.t
, 0);
7205 sym
->type
.ref
= type
.ref
;
7208 sym
->type
.ref
->f
.func_body
= 1;
7209 sym
->r
= VT_SYM
| VT_CONST
;
7210 patch_storage(sym
, &ad
, NULL
);
7212 /* static inline functions are just recorded as a kind
7213 of macro. Their code will be emitted at the end of
7214 the compilation unit only if they are used */
7215 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7216 (VT_INLINE
| VT_STATIC
)) {
7217 struct InlineFunc
*fn
;
7218 const char *filename
;
7220 filename
= file
? file
->filename
: "";
7221 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7222 strcpy(fn
->filename
, filename
);
7224 skip_or_save_block(&fn
->func_str
);
7225 dynarray_add(&tcc_state
->inline_fns
,
7226 &tcc_state
->nb_inline_fns
, fn
);
7228 /* compute text section */
7229 cur_text_section
= ad
.section
;
7230 if (!cur_text_section
)
7231 cur_text_section
= text_section
;
7237 /* find parameter in function parameter list */
7238 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7239 if ((sym
->v
& ~SYM_FIELD
) == v
)
7241 tcc_error("declaration for parameter '%s' but no such parameter",
7242 get_tok_str(v
, NULL
));
7244 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7245 tcc_error("storage class specified for '%s'",
7246 get_tok_str(v
, NULL
));
7247 if (sym
->type
.t
!= VT_VOID
)
7248 tcc_error("redefinition of parameter '%s'",
7249 get_tok_str(v
, NULL
));
7250 convert_parameter_type(&type
);
7252 } else if (type
.t
& VT_TYPEDEF
) {
7253 /* save typedefed type */
7254 /* XXX: test storage specifiers ? */
7256 if (sym
&& sym
->sym_scope
== local_scope
) {
7257 if (!is_compatible_types(&sym
->type
, &type
)
7258 || !(sym
->type
.t
& VT_TYPEDEF
))
7259 tcc_error("incompatible redefinition of '%s'",
7260 get_tok_str(v
, NULL
));
7263 sym
= sym_push(v
, &type
, 0, 0);
7269 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7270 /* external function definition */
7271 /* specific case for func_call attribute */
7273 } else if (!(type
.t
& VT_ARRAY
)) {
7274 /* not lvalue if array */
7275 r
|= lvalue_type(type
.t
);
7277 has_init
= (tok
== '=');
7278 if (has_init
&& (type
.t
& VT_VLA
))
7279 tcc_error("variable length array cannot be initialized");
7280 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
)) ||
7281 ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7282 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7283 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7284 /* external variable or function */
7285 /* NOTE: as GCC, uninitialized global static
7286 arrays of null size are considered as
7288 sym
= external_sym(v
, &type
, r
, &ad
);
7289 if (ad
.alias_target
) {
7293 alias_target
= sym_find(ad
.alias_target
);
7294 if (!alias_target
|| !alias_target
->c
)
7295 tcc_error("unsupported forward __alias__ attribute");
7296 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7297 tsec
.sh_num
= esym
->st_shndx
;
7298 /* Local statics have a scope until now (for
7299 warnings), remove it here. */
7301 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7304 if (type
.t
& VT_STATIC
)
7310 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7314 if (is_for_loop_init
)
7327 static void decl(int l
)
7332 /* ------------------------------------------------------------------------- */