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
; /* true if no code generation wanted for an expression */
54 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
56 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
58 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
59 ST_DATA
const char *funcname
;
61 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
63 ST_DATA
struct switch_t
{
66 } **p
; int n
; /* list of case ranges */
67 int def_sym
; /* default symbol */
68 } *cur_switch
; /* current switch */
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void expr_lor_const(void);
83 static void unary_type(CType
*type
);
84 static void vla_runtime_type_size(CType
*type
, int *a
);
85 static void vla_sp_restore(void);
86 static void vla_sp_restore_root(void);
87 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
88 static void expr_type(CType
*type
);
89 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
90 ST_FUNC
void vpush(CType
*type
);
91 ST_FUNC
int gvtst(int inv
, int t
);
92 ST_FUNC
int is_btype_size(int bt
);
93 static void gen_inline_functions(TCCState
*s
);
95 ST_INLN
int is_float(int t
)
99 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
102 /* we use our own 'finite' function to avoid potential problems with
103 non standard math libs */
104 /* XXX: endianness dependent */
105 ST_FUNC
int ieee_finite(double d
)
108 memcpy(p
, &d
, sizeof(double));
109 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
112 ST_FUNC
void test_lvalue(void)
114 if (!(vtop
->r
& VT_LVAL
))
118 ST_FUNC
void check_vstack(void)
121 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
124 /* ------------------------------------------------------------------------- */
125 /* vstack debugging aid */
128 void pv (const char *lbl
, int a
, int b
)
131 for (i
= a
; i
< a
+ b
; ++i
) {
132 SValue
*p
= &vtop
[-i
];
133 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
134 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
139 /* ------------------------------------------------------------------------- */
140 ST_FUNC
void tccgen_start(TCCState
*s1
)
142 cur_text_section
= NULL
;
144 anon_sym
= SYM_FIRST_ANOM
;
148 /* define some often used types */
150 char_pointer_type
.t
= VT_BYTE
;
151 mk_pointer(&char_pointer_type
);
153 size_type
.t
= VT_INT
;
155 size_type
.t
= VT_LLONG
;
157 func_old_type
.t
= VT_FUNC
;
158 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
163 /* file info: full path + filename */
164 section_sym
= put_elf_sym(symtab_section
, 0, 0,
165 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
166 text_section
->sh_num
, NULL
);
167 getcwd(buf
, sizeof(buf
));
169 normalize_slashes(buf
);
171 pstrcat(buf
, sizeof(buf
), "/");
172 put_stabs_r(buf
, N_SO
, 0, 0,
173 text_section
->data_offset
, text_section
, section_sym
);
174 put_stabs_r(file
->filename
, N_SO
, 0, 0,
175 text_section
->data_offset
, text_section
, section_sym
);
177 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
178 symbols can be safely used */
179 put_elf_sym(symtab_section
, 0, 0,
180 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
181 SHN_ABS
, file
->filename
);
183 #ifdef TCC_TARGET_ARM
188 ST_FUNC
void tccgen_end(TCCState
*s1
)
190 gen_inline_functions(s1
);
192 /* end of translation unit info */
194 put_stabs_r(NULL
, N_SO
, 0, 0,
195 text_section
->data_offset
, text_section
, section_sym
);
199 /* ------------------------------------------------------------------------- */
200 /* update sym->c so that it points to an external symbol in section
201 'section' with value 'value' */
203 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
204 addr_t value
, unsigned long size
,
205 int can_add_underscore
)
207 int sym_type
, sym_bind
, sh_num
, info
, other
;
212 #ifdef CONFIG_TCC_BCHECK
218 else if (section
== SECTION_ABS
)
221 sh_num
= section
->sh_num
;
223 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
225 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
226 sym_type
= STT_NOTYPE
;
228 sym_type
= STT_OBJECT
;
231 if (sym
->type
.t
& VT_STATIC
)
232 sym_bind
= STB_LOCAL
;
234 if (sym
->type
.t
& VT_WEAK
)
237 sym_bind
= STB_GLOBAL
;
241 name
= get_tok_str(sym
->v
, NULL
);
242 #ifdef CONFIG_TCC_BCHECK
243 if (tcc_state
->do_bounds_check
) {
244 /* XXX: avoid doing that for statics ? */
245 /* if bound checking is activated, we change some function
246 names by adding the "__bound" prefix */
249 /* XXX: we rely only on malloc hooks */
262 strcpy(buf
, "__bound_");
272 if (sym
->type
.t
& VT_EXPORT
)
273 other
|= ST_PE_EXPORT
;
274 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
275 Sym
*ref
= sym
->type
.ref
;
276 if (ref
->a
.func_export
)
277 other
|= ST_PE_EXPORT
;
278 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
279 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
281 other
|= ST_PE_STDCALL
;
282 can_add_underscore
= 0;
285 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
286 other
|= ST_PE_IMPORT
;
287 if (sym
->type
.t
& VT_IMPORT
)
288 other
|= ST_PE_IMPORT
;
291 if (! (sym
->type
.t
& VT_STATIC
))
292 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
294 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
296 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
299 if (sym
->asm_label
) {
300 name
= get_tok_str(sym
->asm_label
, NULL
);
302 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
303 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
305 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
306 esym
->st_value
= value
;
307 esym
->st_size
= size
;
308 esym
->st_shndx
= sh_num
;
312 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
313 addr_t value
, unsigned long size
)
315 put_extern_sym2(sym
, section
, value
, size
, 1);
318 /* add a new relocation entry to symbol 'sym' in section 's' */
319 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
325 put_extern_sym(sym
, NULL
, 0, 0);
328 /* now we can add ELF relocation info */
329 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
332 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
334 greloca(s
, sym
, offset
, type
, 0);
337 /* ------------------------------------------------------------------------- */
338 /* symbol allocator */
339 static Sym
*__sym_malloc(void)
341 Sym
*sym_pool
, *sym
, *last_sym
;
344 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
345 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
347 last_sym
= sym_free_first
;
349 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
350 sym
->next
= last_sym
;
354 sym_free_first
= last_sym
;
358 static inline Sym
*sym_malloc(void)
361 sym
= sym_free_first
;
363 sym
= __sym_malloc();
364 sym_free_first
= sym
->next
;
368 ST_INLN
void sym_free(Sym
*sym
)
370 sym
->next
= sym_free_first
;
371 sym_free_first
= sym
;
374 /* push, without hashing */
375 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
395 /* find a symbol and return its associated structure. 's' is the top
396 of the symbol stack */
397 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
409 /* structure lookup */
410 ST_INLN Sym
*struct_find(int v
)
413 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
415 return table_ident
[v
]->sym_struct
;
418 /* find an identifier */
419 ST_INLN Sym
*sym_find(int v
)
422 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
424 return table_ident
[v
]->sym_identifier
;
427 /* push a given symbol on the symbol stack */
428 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
437 s
= sym_push2(ps
, v
, type
->t
, c
);
438 s
->type
.ref
= type
->ref
;
440 /* don't record fields or anonymous symbols */
442 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
443 /* record symbol in token array */
444 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
446 ps
= &ts
->sym_struct
;
448 ps
= &ts
->sym_identifier
;
451 s
->scope
= local_scope
;
452 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
453 tcc_error("redeclaration of '%s'",
454 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
459 /* push a global identifier */
460 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
463 s
= sym_push2(&global_stack
, v
, t
, c
);
464 /* don't record anonymous symbol */
465 if (v
< SYM_FIRST_ANOM
) {
466 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
467 /* modify the top most local identifier, so that
468 sym_identifier will point to 's' when popped */
470 ps
= &(*ps
)->prev_tok
;
477 /* pop symbols until top reaches 'b' */
478 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
488 /* remove symbol in token array */
490 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
491 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
493 ps
= &ts
->sym_struct
;
495 ps
= &ts
->sym_identifier
;
504 static void weaken_symbol(Sym
*sym
)
506 sym
->type
.t
|= VT_WEAK
;
511 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
512 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
513 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
517 static void apply_visibility(Sym
*sym
, CType
*type
)
519 int vis
= sym
->type
.t
& VT_VIS_MASK
;
520 int vis2
= type
->t
& VT_VIS_MASK
;
521 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
523 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
526 vis
= (vis
< vis2
) ? vis
: vis2
;
527 sym
->type
.t
&= ~VT_VIS_MASK
;
533 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
534 vis
>>= VT_VIS_SHIFT
;
535 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
539 /* ------------------------------------------------------------------------- */
541 ST_FUNC
void swap(int *p
, int *q
)
549 static void vsetc(CType
*type
, int r
, CValue
*vc
)
553 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
554 tcc_error("memory full (vstack)");
555 /* cannot let cpu flags if other instruction are generated. Also
556 avoid leaving VT_JMP anywhere except on the top of the stack
557 because it would complicate the code generator. */
558 if (vtop
>= vstack
) {
559 v
= vtop
->r
& VT_VALMASK
;
560 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
570 /* push constant of type "type" with useless value */
571 ST_FUNC
void vpush(CType
*type
)
574 vsetc(type
, VT_CONST
, &cval
);
577 /* push integer constant */
578 ST_FUNC
void vpushi(int v
)
582 vsetc(&int_type
, VT_CONST
, &cval
);
585 /* push a pointer sized constant */
586 static void vpushs(addr_t v
)
590 vsetc(&size_type
, VT_CONST
, &cval
);
593 /* push arbitrary 64bit constant */
594 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
601 vsetc(&ctype
, VT_CONST
, &cval
);
604 /* push long long constant */
605 static inline void vpushll(long long v
)
607 vpush64(VT_LLONG
, v
);
610 /* push a symbol value of TYPE */
611 static inline void vpushsym(CType
*type
, Sym
*sym
)
615 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
619 /* Return a static symbol pointing to a section */
620 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
626 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
627 sym
->type
.ref
= type
->ref
;
628 sym
->r
= VT_CONST
| VT_SYM
;
629 put_extern_sym(sym
, sec
, offset
, size
);
633 /* push a reference to a section offset by adding a dummy symbol */
634 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
636 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
639 /* define a new external reference to a symbol 'v' of type 'u' */
640 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
646 /* push forward reference */
647 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
648 s
->type
.ref
= type
->ref
;
649 s
->r
= r
| VT_CONST
| VT_SYM
;
654 /* define a new external reference to a symbol 'v' */
655 static Sym
*external_sym(int v
, CType
*type
, int r
)
661 /* push forward reference */
662 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
663 s
->type
.t
|= VT_EXTERN
;
664 } else if (s
->type
.ref
== func_old_type
.ref
) {
665 s
->type
.ref
= type
->ref
;
666 s
->r
= r
| VT_CONST
| VT_SYM
;
667 s
->type
.t
|= VT_EXTERN
;
668 } else if (!is_compatible_types(&s
->type
, type
)) {
669 tcc_error("incompatible types for redefinition of '%s'",
670 get_tok_str(v
, NULL
));
672 /* Merge some storage attributes. */
673 if (type
->t
& VT_WEAK
)
676 if (type
->t
& VT_VIS_MASK
)
677 apply_visibility(s
, type
);
682 /* push a reference to global symbol v */
683 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
685 vpushsym(type
, external_global_sym(v
, type
, 0));
688 ST_FUNC
void vset(CType
*type
, int r
, int v
)
693 vsetc(type
, r
, &cval
);
696 static void vseti(int r
, int v
)
704 ST_FUNC
void vswap(void)
707 /* cannot let cpu flags if other instruction are generated. Also
708 avoid leaving VT_JMP anywhere except on the top of the stack
709 because it would complicate the code generator. */
710 if (vtop
>= vstack
) {
711 int v
= vtop
->r
& VT_VALMASK
;
712 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
719 /* XXX: +2% overall speed possible with optimized memswap
721 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
725 ST_FUNC
void vpushv(SValue
*v
)
727 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
728 tcc_error("memory full (vstack)");
733 static void vdup(void)
738 /* save registers up to (vtop - n) stack entry */
739 ST_FUNC
void save_regs(int n
)
742 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
746 /* save r to the memory stack, and mark it as being free */
747 ST_FUNC
void save_reg(int r
)
749 save_reg_upstack(r
, 0);
752 /* save r to the memory stack, and mark it as being free,
753 if seen up to (vtop - n) stack entry */
754 ST_FUNC
void save_reg_upstack(int r
, int n
)
756 int l
, saved
, size
, align
;
760 if ((r
&= VT_VALMASK
) >= VT_CONST
)
763 /* modify all stack values */
766 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
767 if ((p
->r
& VT_VALMASK
) == r
||
768 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
769 /* must save value on stack if not already done */
771 /* NOTE: must reload 'r' because r might be equal to r2 */
772 r
= p
->r
& VT_VALMASK
;
773 /* store register in the stack */
775 if ((p
->r
& VT_LVAL
) ||
776 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
777 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
778 type
= &char_pointer_type
;
782 size
= type_size(type
, &align
);
783 loc
= (loc
- size
) & -align
;
785 sv
.r
= VT_LOCAL
| VT_LVAL
;
788 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
789 /* x86 specific: need to pop fp register ST0 if saved */
791 o(0xd8dd); /* fstp %st(0) */
794 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
795 /* special long long case */
796 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
804 /* mark that stack entry as being saved on the stack */
805 if (p
->r
& VT_LVAL
) {
806 /* also clear the bounded flag because the
807 relocation address of the function was stored in
809 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
811 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
819 #ifdef TCC_TARGET_ARM
820 /* find a register of class 'rc2' with at most one reference on stack.
821 * If none, call get_reg(rc) */
822 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
827 for(r
=0;r
<NB_REGS
;r
++) {
828 if (reg_classes
[r
] & rc2
) {
831 for(p
= vstack
; p
<= vtop
; p
++) {
832 if ((p
->r
& VT_VALMASK
) == r
||
833 (p
->r2
& VT_VALMASK
) == r
)
844 /* find a free register of class 'rc'. If none, save one register */
845 ST_FUNC
int get_reg(int rc
)
850 /* find a free register */
851 for(r
=0;r
<NB_REGS
;r
++) {
852 if (reg_classes
[r
] & rc
) {
853 for(p
=vstack
;p
<=vtop
;p
++) {
854 if ((p
->r
& VT_VALMASK
) == r
||
855 (p
->r2
& VT_VALMASK
) == r
)
863 /* no register left : free the first one on the stack (VERY
864 IMPORTANT to start from the bottom to ensure that we don't
865 spill registers used in gen_opi()) */
866 for(p
=vstack
;p
<=vtop
;p
++) {
867 /* look at second register (if long long) */
868 r
= p
->r2
& VT_VALMASK
;
869 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
871 r
= p
->r
& VT_VALMASK
;
872 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
878 /* Should never comes here */
882 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
884 static void move_reg(int r
, int s
, int t
)
898 /* get address of vtop (vtop MUST BE an lvalue) */
899 ST_FUNC
void gaddrof(void)
901 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
904 /* tricky: if saved lvalue, then we can go back to lvalue */
905 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
906 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
911 #ifdef CONFIG_TCC_BCHECK
912 /* generate lvalue bound code */
913 static void gbound(void)
918 vtop
->r
&= ~VT_MUSTBOUND
;
919 /* if lvalue, then use checking code before dereferencing */
920 if (vtop
->r
& VT_LVAL
) {
921 /* if not VT_BOUNDED value, then make one */
922 if (!(vtop
->r
& VT_BOUNDED
)) {
923 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
924 /* must save type because we must set it to int to get pointer */
926 vtop
->type
.t
= VT_PTR
;
929 gen_bounded_ptr_add();
930 vtop
->r
|= lval_type
;
933 /* then check for dereferencing */
934 gen_bounded_ptr_deref();
939 /* store vtop a register belonging to class 'rc'. lvalues are
940 converted to values. Cannot be used if cannot be converted to
941 register value (such as structures). */
942 ST_FUNC
int gv(int rc
)
944 int r
, bit_pos
, bit_size
, size
, align
, i
;
947 /* NOTE: get_reg can modify vstack[] */
948 if (vtop
->type
.t
& VT_BITFIELD
) {
951 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
952 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
953 /* remove bit field info to avoid loops */
954 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
955 /* cast to int to propagate signedness in following ops */
956 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
961 if((vtop
->type
.t
& VT_UNSIGNED
) ||
962 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
963 type
.t
|= VT_UNSIGNED
;
965 /* generate shifts */
966 vpushi(bits
- (bit_pos
+ bit_size
));
968 vpushi(bits
- bit_size
);
969 /* NOTE: transformed to SHR if unsigned */
973 if (is_float(vtop
->type
.t
) &&
974 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
977 unsigned long offset
;
978 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
982 /* XXX: unify with initializers handling ? */
983 /* CPUs usually cannot use float constants, so we store them
984 generically in data segment */
985 size
= type_size(&vtop
->type
, &align
);
986 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
987 data_section
->data_offset
= offset
;
988 /* XXX: not portable yet */
989 #if defined(__i386__) || defined(__x86_64__)
990 /* Zero pad x87 tenbyte long doubles */
991 if (size
== LDOUBLE_SIZE
) {
992 vtop
->c
.tab
[2] &= 0xffff;
993 #if LDOUBLE_SIZE == 16
998 ptr
= section_ptr_add(data_section
, size
);
1000 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1004 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1008 ptr
[i
] = vtop
->c
.tab
[i
];
1009 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1010 vtop
->r
|= VT_LVAL
| VT_SYM
;
1014 #ifdef CONFIG_TCC_BCHECK
1015 if (vtop
->r
& VT_MUSTBOUND
)
1019 r
= vtop
->r
& VT_VALMASK
;
1020 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1021 #ifndef TCC_TARGET_ARM64
1024 #ifdef TCC_TARGET_X86_64
1025 else if (rc
== RC_FRET
)
1030 /* need to reload if:
1032 - lvalue (need to dereference pointer)
1033 - already a register, but not in the right class */
1035 || (vtop
->r
& VT_LVAL
)
1036 || !(reg_classes
[r
] & rc
)
1037 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1038 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1039 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1041 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1046 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1047 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1048 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1050 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1051 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1052 unsigned long long ll
;
1054 int r2
, original_type
;
1055 original_type
= vtop
->type
.t
;
1056 /* two register type load : expand to two words
1058 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1059 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1062 vtop
->c
.i
= ll
; /* first word */
1064 vtop
->r
= r
; /* save register value */
1065 vpushi(ll
>> 32); /* second word */
1068 if (vtop
->r
& VT_LVAL
) {
1069 /* We do not want to modifier the long long
1070 pointer here, so the safest (and less
1071 efficient) is to save all the other registers
1072 in the stack. XXX: totally inefficient. */
1076 /* lvalue_save: save only if used further down the stack */
1077 save_reg_upstack(vtop
->r
, 1);
1079 /* load from memory */
1080 vtop
->type
.t
= load_type
;
1083 vtop
[-1].r
= r
; /* save register value */
1084 /* increment pointer to get second word */
1085 vtop
->type
.t
= addr_type
;
1090 vtop
->type
.t
= load_type
;
1092 /* move registers */
1095 vtop
[-1].r
= r
; /* save register value */
1096 vtop
->r
= vtop
[-1].r2
;
1098 /* Allocate second register. Here we rely on the fact that
1099 get_reg() tries first to free r2 of an SValue. */
1103 /* write second register */
1105 vtop
->type
.t
= original_type
;
1106 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1108 /* lvalue of scalar type : need to use lvalue type
1109 because of possible cast */
1112 /* compute memory access type */
1113 if (vtop
->r
& VT_REF
)
1114 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1119 else if (vtop
->r
& VT_LVAL_BYTE
)
1121 else if (vtop
->r
& VT_LVAL_SHORT
)
1123 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1127 /* restore wanted type */
1130 /* one register type load */
1135 #ifdef TCC_TARGET_C67
1136 /* uses register pairs for doubles */
1137 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1144 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1145 ST_FUNC
void gv2(int rc1
, int rc2
)
1149 /* generate more generic register first. But VT_JMP or VT_CMP
1150 values must be generated first in all cases to avoid possible
1152 v
= vtop
[0].r
& VT_VALMASK
;
1153 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1158 /* test if reload is needed for first register */
1159 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1169 /* test if reload is needed for first register */
1170 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1176 #ifndef TCC_TARGET_ARM64
1177 /* wrapper around RC_FRET to return a register by type */
1178 static int rc_fret(int t
)
1180 #ifdef TCC_TARGET_X86_64
1181 if (t
== VT_LDOUBLE
) {
1189 /* wrapper around REG_FRET to return a register by type */
1190 static int reg_fret(int t
)
1192 #ifdef TCC_TARGET_X86_64
1193 if (t
== VT_LDOUBLE
) {
1200 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1201 /* expand 64bit on stack in two ints */
1202 static void lexpand(void)
1205 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1206 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1207 if (v
== VT_CONST
) {
1210 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1216 vtop
[0].r
= vtop
[-1].r2
;
1217 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1219 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1223 #ifdef TCC_TARGET_ARM
1224 /* expand long long on stack */
1225 ST_FUNC
void lexpand_nr(void)
1229 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1231 vtop
->r2
= VT_CONST
;
1232 vtop
->type
.t
= VT_INT
| u
;
1233 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1234 if (v
== VT_CONST
) {
1235 vtop
[-1].c
.i
= vtop
->c
.i
;
1236 vtop
->c
.i
= vtop
->c
.i
>> 32;
1238 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1240 vtop
->r
= vtop
[-1].r
;
1241 } else if (v
> VT_CONST
) {
1245 vtop
->r
= vtop
[-1].r2
;
1246 vtop
[-1].r2
= VT_CONST
;
1247 vtop
[-1].type
.t
= VT_INT
| u
;
1251 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1252 /* build a long long from two ints */
1253 static void lbuild(int t
)
1255 gv2(RC_INT
, RC_INT
);
1256 vtop
[-1].r2
= vtop
[0].r
;
1257 vtop
[-1].type
.t
= t
;
1262 /* rotate n first stack elements to the bottom
1263 I1 ... In -> I2 ... In I1 [top is right]
1265 ST_FUNC
void vrotb(int n
)
1271 for(i
=-n
+1;i
!=0;i
++)
1272 vtop
[i
] = vtop
[i
+1];
1276 /* rotate the n elements before entry e towards the top
1277 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1279 ST_FUNC
void vrote(SValue
*e
, int n
)
1285 for(i
= 0;i
< n
- 1; i
++)
1290 /* rotate n first stack elements to the top
1291 I1 ... In -> In I1 ... I(n-1) [top is right]
1293 ST_FUNC
void vrott(int n
)
1298 /* pop stack value */
1299 ST_FUNC
void vpop(void)
1302 v
= vtop
->r
& VT_VALMASK
;
1303 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1304 /* for x86, we need to pop the FP stack */
1305 if (v
== TREG_ST0
&& !nocode_wanted
) {
1306 o(0xd8dd); /* fstp %st(0) */
1309 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1310 /* need to put correct jump if && or || without test */
1316 /* convert stack entry to register and duplicate its value in another
1318 static void gv_dup(void)
1324 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1325 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1332 /* stack: H L L1 H1 */
1342 /* duplicate value */
1347 #ifdef TCC_TARGET_X86_64
1348 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1358 load(r1
, &sv
); /* move r to r1 */
1360 /* duplicates value */
1366 /* Generate value test
1368 * Generate a test for any value (jump, comparison and integers) */
1369 ST_FUNC
int gvtst(int inv
, int t
)
1371 int v
= vtop
->r
& VT_VALMASK
;
1372 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1376 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1377 /* constant jmp optimization */
1378 if ((vtop
->c
.i
!= 0) != inv
)
1383 return gtst(inv
, t
);
1386 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1387 /* generate CPU independent (unsigned) long long operations */
1388 static void gen_opl(int op
)
1390 int t
, a
, b
, op1
, c
, i
;
1392 unsigned short reg_iret
= REG_IRET
;
1393 unsigned short reg_lret
= REG_LRET
;
1399 func
= TOK___divdi3
;
1402 func
= TOK___udivdi3
;
1405 func
= TOK___moddi3
;
1408 func
= TOK___umoddi3
;
1415 /* call generic long long function */
1416 vpush_global_sym(&func_old_type
, func
);
1421 vtop
->r2
= reg_lret
;
1429 //pv("gen_opl A",0,2);
1435 /* stack: L1 H1 L2 H2 */
1440 vtop
[-2] = vtop
[-3];
1443 /* stack: H1 H2 L1 L2 */
1444 //pv("gen_opl B",0,4);
1450 /* stack: H1 H2 L1 L2 ML MH */
1453 /* stack: ML MH H1 H2 L1 L2 */
1457 /* stack: ML MH H1 L2 H2 L1 */
1462 /* stack: ML MH M1 M2 */
1465 } else if (op
== '+' || op
== '-') {
1466 /* XXX: add non carry method too (for MIPS or alpha) */
1472 /* stack: H1 H2 (L1 op L2) */
1475 gen_op(op1
+ 1); /* TOK_xxxC2 */
1478 /* stack: H1 H2 (L1 op L2) */
1481 /* stack: (L1 op L2) H1 H2 */
1483 /* stack: (L1 op L2) (H1 op H2) */
1491 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1492 t
= vtop
[-1].type
.t
;
1496 /* stack: L H shift */
1498 /* constant: simpler */
1499 /* NOTE: all comments are for SHL. the other cases are
1500 done by swaping words */
1511 if (op
!= TOK_SAR
) {
1544 /* XXX: should provide a faster fallback on x86 ? */
1547 func
= TOK___ashrdi3
;
1550 func
= TOK___lshrdi3
;
1553 func
= TOK___ashldi3
;
1559 /* compare operations */
1565 /* stack: L1 H1 L2 H2 */
1567 vtop
[-1] = vtop
[-2];
1569 /* stack: L1 L2 H1 H2 */
1572 /* when values are equal, we need to compare low words. since
1573 the jump is inverted, we invert the test too. */
1576 else if (op1
== TOK_GT
)
1578 else if (op1
== TOK_ULT
)
1580 else if (op1
== TOK_UGT
)
1585 if (op1
!= TOK_NE
) {
1589 /* generate non equal test */
1590 /* XXX: NOT PORTABLE yet */
1594 #if defined(TCC_TARGET_I386)
1595 b
= psym(0x850f, 0);
1596 #elif defined(TCC_TARGET_ARM)
1598 o(0x1A000000 | encbranch(ind
, 0, 1));
1599 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1600 tcc_error("not implemented");
1602 #error not supported
1606 /* compare low. Always unsigned */
1610 else if (op1
== TOK_LE
)
1612 else if (op1
== TOK_GT
)
1614 else if (op1
== TOK_GE
)
1625 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1627 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1628 return (a
^ b
) >> 63 ? -x
: x
;
1631 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1633 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1636 /* handle integer constant optimizations and various machine
1638 static void gen_opic(int op
)
1640 SValue
*v1
= vtop
- 1;
1642 int t1
= v1
->type
.t
& VT_BTYPE
;
1643 int t2
= v2
->type
.t
& VT_BTYPE
;
1644 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1645 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1646 uint64_t l1
= c1
? v1
->c
.i
: 0;
1647 uint64_t l2
= c2
? v2
->c
.i
: 0;
1648 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1651 l1
= ((uint32_t)l1
|
1652 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1654 l2
= ((uint32_t)l2
|
1655 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1659 case '+': l1
+= l2
; break;
1660 case '-': l1
-= l2
; break;
1661 case '&': l1
&= l2
; break;
1662 case '^': l1
^= l2
; break;
1663 case '|': l1
|= l2
; break;
1664 case '*': l1
*= l2
; break;
1671 /* if division by zero, generate explicit division */
1674 tcc_error("division by zero in constant");
1678 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1679 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1680 case TOK_UDIV
: l1
= l1
/ l2
; break;
1681 case TOK_UMOD
: l1
= l1
% l2
; break;
1684 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1685 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1687 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1690 case TOK_ULT
: l1
= l1
< l2
; break;
1691 case TOK_UGE
: l1
= l1
>= l2
; break;
1692 case TOK_EQ
: l1
= l1
== l2
; break;
1693 case TOK_NE
: l1
= l1
!= l2
; break;
1694 case TOK_ULE
: l1
= l1
<= l2
; break;
1695 case TOK_UGT
: l1
= l1
> l2
; break;
1696 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1697 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1698 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1699 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1701 case TOK_LAND
: l1
= l1
&& l2
; break;
1702 case TOK_LOR
: l1
= l1
|| l2
; break;
1709 /* if commutative ops, put c2 as constant */
1710 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1711 op
== '|' || op
== '*')) {
1713 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1714 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1716 if (!const_wanted
&&
1718 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1719 (l1
== -1 && op
== TOK_SAR
))) {
1720 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1722 } else if (!const_wanted
&&
1723 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1724 (l2
== -1 && op
== '|') ||
1725 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1726 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1727 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1732 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1735 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1736 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1740 /* filter out NOP operations like x*1, x-0, x&-1... */
1742 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1743 /* try to use shifts instead of muls or divs */
1744 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1753 else if (op
== TOK_PDIV
)
1759 } else if (c2
&& (op
== '+' || op
== '-') &&
1760 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1761 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1762 /* symbol + constant case */
1769 if (!nocode_wanted
) {
1770 /* call low level op generator */
1771 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1772 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1783 /* generate a floating point operation with constant propagation */
1784 static void gen_opif(int op
)
1792 /* currently, we cannot do computations with forward symbols */
1793 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1794 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1796 if (v1
->type
.t
== VT_FLOAT
) {
1799 } else if (v1
->type
.t
== VT_DOUBLE
) {
1807 /* NOTE: we only do constant propagation if finite number (not
1808 NaN or infinity) (ANSI spec) */
1809 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1813 case '+': f1
+= f2
; break;
1814 case '-': f1
-= f2
; break;
1815 case '*': f1
*= f2
; break;
1819 tcc_error("division by zero in constant");
1824 /* XXX: also handles tests ? */
1828 /* XXX: overflow test ? */
1829 if (v1
->type
.t
== VT_FLOAT
) {
1831 } else if (v1
->type
.t
== VT_DOUBLE
) {
1839 if (!nocode_wanted
) {
1847 static int pointed_size(CType
*type
)
1850 return type_size(pointed_type(type
), &align
);
1853 static void vla_runtime_pointed_size(CType
*type
)
1856 vla_runtime_type_size(pointed_type(type
), &align
);
1859 static inline int is_null_pointer(SValue
*p
)
1861 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1863 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1864 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1865 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1866 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1869 static inline int is_integer_btype(int bt
)
1871 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1872 bt
== VT_INT
|| bt
== VT_LLONG
);
1875 /* check types for comparison or subtraction of pointers */
1876 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1878 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1881 /* null pointers are accepted for all comparisons as gcc */
1882 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1886 bt1
= type1
->t
& VT_BTYPE
;
1887 bt2
= type2
->t
& VT_BTYPE
;
1888 /* accept comparison between pointer and integer with a warning */
1889 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1890 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1891 tcc_warning("comparison between pointer and integer");
1895 /* both must be pointers or implicit function pointers */
1896 if (bt1
== VT_PTR
) {
1897 type1
= pointed_type(type1
);
1898 } else if (bt1
!= VT_FUNC
)
1899 goto invalid_operands
;
1901 if (bt2
== VT_PTR
) {
1902 type2
= pointed_type(type2
);
1903 } else if (bt2
!= VT_FUNC
) {
1905 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1907 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1908 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1912 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1913 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1914 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1915 /* gcc-like error if '-' is used */
1917 goto invalid_operands
;
1919 tcc_warning("comparison of distinct pointer types lacks a cast");
1923 /* generic gen_op: handles types problems */
1924 ST_FUNC
void gen_op(int op
)
1926 int u
, t1
, t2
, bt1
, bt2
, t
;
1929 t1
= vtop
[-1].type
.t
;
1930 t2
= vtop
[0].type
.t
;
1931 bt1
= t1
& VT_BTYPE
;
1932 bt2
= t2
& VT_BTYPE
;
1934 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1935 tcc_error("operation on a struct");
1936 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1937 /* at least one operand is a pointer */
1938 /* relationnal op: must be both pointers */
1939 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1940 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1941 /* pointers are handled are unsigned */
1942 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1943 t
= VT_LLONG
| VT_UNSIGNED
;
1945 t
= VT_INT
| VT_UNSIGNED
;
1949 /* if both pointers, then it must be the '-' op */
1950 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1952 tcc_error("cannot use pointers here");
1953 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1954 /* XXX: check that types are compatible */
1955 if (vtop
[-1].type
.t
& VT_VLA
) {
1956 vla_runtime_pointed_size(&vtop
[-1].type
);
1958 vpushi(pointed_size(&vtop
[-1].type
));
1962 /* set to integer type */
1963 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1964 vtop
->type
.t
= VT_LLONG
;
1966 vtop
->type
.t
= VT_INT
;
1971 /* exactly one pointer : must be '+' or '-'. */
1972 if (op
!= '-' && op
!= '+')
1973 tcc_error("cannot use pointers here");
1974 /* Put pointer as first operand */
1975 if (bt2
== VT_PTR
) {
1980 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
1981 /* XXX: truncate here because gen_opl can't handle ptr + long long */
1982 gen_cast(&int_type
);
1984 type1
= vtop
[-1].type
;
1985 type1
.t
&= ~VT_ARRAY
;
1986 if (vtop
[-1].type
.t
& VT_VLA
)
1987 vla_runtime_pointed_size(&vtop
[-1].type
);
1989 u
= pointed_size(&vtop
[-1].type
);
1991 tcc_error("unknown array element size");
1992 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1995 /* XXX: cast to int ? (long long case) */
2001 /* #ifdef CONFIG_TCC_BCHECK
2002 The main reason to removing this code:
2009 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2010 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2012 When this code is on. then the output looks like
2014 v+(i-j) = 0xbff84000
2016 /* if evaluating constant expression, no code should be
2017 generated, so no bound check */
2018 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2019 /* if bounded pointers, we generate a special code to
2026 gen_bounded_ptr_add();
2032 /* put again type if gen_opic() swaped operands */
2035 } else if (is_float(bt1
) || is_float(bt2
)) {
2036 /* compute bigger type and do implicit casts */
2037 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2039 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2044 /* floats can only be used for a few operations */
2045 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2046 (op
< TOK_ULT
|| op
> TOK_GT
))
2047 tcc_error("invalid operands for binary operation");
2049 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2050 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2051 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2054 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2055 /* cast to biggest op */
2057 /* convert to unsigned if it does not fit in a long long */
2058 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2059 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2063 /* integer operations */
2065 /* convert to unsigned if it does not fit in an integer */
2066 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2067 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2070 /* XXX: currently, some unsigned operations are explicit, so
2071 we modify them here */
2072 if (t
& VT_UNSIGNED
) {
2079 else if (op
== TOK_LT
)
2081 else if (op
== TOK_GT
)
2083 else if (op
== TOK_LE
)
2085 else if (op
== TOK_GE
)
2092 /* special case for shifts and long long: we keep the shift as
2094 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2101 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2102 /* relationnal op: the result is an int */
2103 vtop
->type
.t
= VT_INT
;
2108 // Make sure that we have converted to an rvalue:
2109 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
2110 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2113 #ifndef TCC_TARGET_ARM
2114 /* generic itof for unsigned long long case */
2115 static void gen_cvt_itof1(int t
)
2117 #ifdef TCC_TARGET_ARM64
2120 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2121 (VT_LLONG
| VT_UNSIGNED
)) {
2124 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2125 #if LDOUBLE_SIZE != 8
2126 else if (t
== VT_LDOUBLE
)
2127 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2130 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2134 vtop
->r
= reg_fret(t
);
2142 /* generic ftoi for unsigned long long case */
2143 static void gen_cvt_ftoi1(int t
)
2145 #ifdef TCC_TARGET_ARM64
2150 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2151 /* not handled natively */
2152 st
= vtop
->type
.t
& VT_BTYPE
;
2154 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2155 #if LDOUBLE_SIZE != 8
2156 else if (st
== VT_LDOUBLE
)
2157 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2160 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2165 vtop
->r2
= REG_LRET
;
2172 /* force char or short cast */
2173 static void force_charshort_cast(int t
)
2177 /* XXX: add optimization if lvalue : just change type and offset */
2182 if (t
& VT_UNSIGNED
) {
2183 vpushi((1 << bits
) - 1);
2186 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2192 /* result must be signed or the SAR is converted to an SHL
2193 This was not the case when "t" was a signed short
2194 and the last value on the stack was an unsigned int */
2195 vtop
->type
.t
&= ~VT_UNSIGNED
;
2201 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2202 static void gen_cast(CType
*type
)
2204 int sbt
, dbt
, sf
, df
, c
, p
;
2206 /* special delayed cast for char/short */
2207 /* XXX: in some cases (multiple cascaded casts), it may still
2209 if (vtop
->r
& VT_MUSTCAST
) {
2210 vtop
->r
&= ~VT_MUSTCAST
;
2211 force_charshort_cast(vtop
->type
.t
);
2214 /* bitfields first get cast to ints */
2215 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
2219 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2220 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2225 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2226 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2228 /* constant case: we can do it now */
2229 /* XXX: in ISOC, cannot do it if error in convert */
2230 if (sbt
== VT_FLOAT
)
2231 vtop
->c
.ld
= vtop
->c
.f
;
2232 else if (sbt
== VT_DOUBLE
)
2233 vtop
->c
.ld
= vtop
->c
.d
;
2236 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2237 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2238 vtop
->c
.ld
= vtop
->c
.i
;
2240 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2242 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2243 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2245 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2248 if (dbt
== VT_FLOAT
)
2249 vtop
->c
.f
= (float)vtop
->c
.ld
;
2250 else if (dbt
== VT_DOUBLE
)
2251 vtop
->c
.d
= (double)vtop
->c
.ld
;
2252 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2253 vtop
->c
.i
= vtop
->c
.ld
;
2254 } else if (sf
&& dbt
== VT_BOOL
) {
2255 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2258 vtop
->c
.i
= vtop
->c
.ld
;
2259 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2261 else if (sbt
& VT_UNSIGNED
)
2262 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2263 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2264 else if (sbt
== VT_PTR
)
2267 else if (sbt
!= VT_LLONG
)
2268 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2269 -(vtop
->c
.i
& 0x80000000));
2271 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2273 else if (dbt
== VT_BOOL
)
2274 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2275 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2276 else if (dbt
== VT_PTR
)
2279 else if (dbt
!= VT_LLONG
) {
2280 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2281 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2284 if (!(dbt
& VT_UNSIGNED
))
2285 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2288 } else if (p
&& dbt
== VT_BOOL
) {
2291 } else if (!nocode_wanted
) {
2292 /* non constant case: generate code */
2294 /* convert from fp to fp */
2297 /* convert int to fp */
2300 /* convert fp to int */
2301 if (dbt
== VT_BOOL
) {
2305 /* we handle char/short/etc... with generic code */
2306 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2307 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2311 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2312 /* additional cast for char/short... */
2317 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2318 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2319 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2320 /* scalar to long long */
2321 /* machine independent conversion */
2323 /* generate high word */
2324 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2328 if (sbt
== VT_PTR
) {
2329 /* cast from pointer to int before we apply
2330 shift operation, which pointers don't support*/
2331 gen_cast(&int_type
);
2337 /* patch second register */
2338 vtop
[-1].r2
= vtop
->r
;
2342 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2343 (dbt
& VT_BTYPE
) == VT_PTR
||
2344 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2345 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2346 (sbt
& VT_BTYPE
) != VT_PTR
&&
2347 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2348 /* need to convert from 32bit to 64bit */
2350 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2351 #if defined(TCC_TARGET_ARM64)
2353 #elif defined(TCC_TARGET_X86_64)
2355 /* x86_64 specific: movslq */
2357 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2364 } else if (dbt
== VT_BOOL
) {
2365 /* scalar to bool */
2368 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2369 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2370 if (sbt
== VT_PTR
) {
2371 vtop
->type
.t
= VT_INT
;
2372 tcc_warning("nonportable conversion from pointer to char/short");
2374 force_charshort_cast(dbt
);
2375 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2376 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2378 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2379 /* from long long: just take low order word */
2383 /* if lvalue and single word type, nothing to do because
2384 the lvalue already contains the real type size (see
2385 VT_LVAL_xxx constants) */
2389 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2390 /* if we are casting between pointer types,
2391 we must update the VT_LVAL_xxx size */
2392 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2393 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2398 /* return type size as known at compile time. Put alignment at 'a' */
2399 ST_FUNC
int type_size(CType
*type
, int *a
)
2404 bt
= type
->t
& VT_BTYPE
;
2405 if (bt
== VT_STRUCT
) {
2410 } else if (bt
== VT_PTR
) {
2411 if (type
->t
& VT_ARRAY
) {
2415 ts
= type_size(&s
->type
, a
);
2417 if (ts
< 0 && s
->c
< 0)
2425 } else if (bt
== VT_LDOUBLE
) {
2427 return LDOUBLE_SIZE
;
2428 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2429 #ifdef TCC_TARGET_I386
2430 #ifdef TCC_TARGET_PE
2435 #elif defined(TCC_TARGET_ARM)
2445 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2448 } else if (bt
== VT_SHORT
) {
2451 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2454 } else if (bt
== VT_ENUM
) {
2456 /* Enums might be incomplete, so don't just return '4' here. */
2457 return type
->ref
->c
;
2459 /* char, void, function, _Bool */
2465 /* push type size as known at runtime time on top of value stack. Put
2467 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2469 if (type
->t
& VT_VLA
) {
2470 type_size(&type
->ref
->type
, a
);
2471 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2473 vpushi(type_size(type
, a
));
2477 static void vla_sp_restore(void) {
2478 if (vlas_in_scope
) {
2479 gen_vla_sp_restore(vla_sp_loc
);
2483 static void vla_sp_restore_root(void) {
2484 if (vlas_in_scope
) {
2485 gen_vla_sp_restore(vla_sp_root_loc
);
2489 /* return the pointed type of t */
2490 static inline CType
*pointed_type(CType
*type
)
2492 return &type
->ref
->type
;
2495 /* modify type so that its it is a pointer to type. */
2496 ST_FUNC
void mk_pointer(CType
*type
)
2499 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2500 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2504 /* compare function types. OLD functions match any new functions */
2505 static int is_compatible_func(CType
*type1
, CType
*type2
)
2511 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2513 /* check func_call */
2514 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2516 /* XXX: not complete */
2517 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2521 while (s1
!= NULL
) {
2524 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2534 /* return true if type1 and type2 are the same. If unqualified is
2535 true, qualifiers on the types are ignored.
2537 - enums are not checked as gcc __builtin_types_compatible_p ()
2539 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2543 t1
= type1
->t
& VT_TYPE
;
2544 t2
= type2
->t
& VT_TYPE
;
2546 /* strip qualifiers before comparing */
2547 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2548 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2550 /* Default Vs explicit signedness only matters for char */
2551 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2555 /* XXX: bitfields ? */
2558 /* test more complicated cases */
2559 bt1
= t1
& VT_BTYPE
;
2560 if (bt1
== VT_PTR
) {
2561 type1
= pointed_type(type1
);
2562 type2
= pointed_type(type2
);
2563 return is_compatible_types(type1
, type2
);
2564 } else if (bt1
== VT_STRUCT
) {
2565 return (type1
->ref
== type2
->ref
);
2566 } else if (bt1
== VT_FUNC
) {
2567 return is_compatible_func(type1
, type2
);
2573 /* return true if type1 and type2 are exactly the same (including
2576 static int is_compatible_types(CType
*type1
, CType
*type2
)
2578 return compare_types(type1
,type2
,0);
2581 /* return true if type1 and type2 are the same (ignoring qualifiers).
2583 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2585 return compare_types(type1
,type2
,1);
2588 /* print a type. If 'varstr' is not NULL, then the variable is also
2589 printed in the type */
2591 /* XXX: add array and function pointers */
2592 static void type_to_str(char *buf
, int buf_size
,
2593 CType
*type
, const char *varstr
)
2600 t
= type
->t
& VT_TYPE
;
2603 if (t
& VT_CONSTANT
)
2604 pstrcat(buf
, buf_size
, "const ");
2605 if (t
& VT_VOLATILE
)
2606 pstrcat(buf
, buf_size
, "volatile ");
2607 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2608 pstrcat(buf
, buf_size
, "unsigned ");
2609 else if (t
& VT_DEFSIGN
)
2610 pstrcat(buf
, buf_size
, "signed ");
2640 tstr
= "long double";
2642 pstrcat(buf
, buf_size
, tstr
);
2646 if (bt
== VT_STRUCT
)
2650 pstrcat(buf
, buf_size
, tstr
);
2651 v
= type
->ref
->v
& ~SYM_STRUCT
;
2652 if (v
>= SYM_FIRST_ANOM
)
2653 pstrcat(buf
, buf_size
, "<anonymous>");
2655 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2659 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2660 pstrcat(buf
, buf_size
, "(");
2662 while (sa
!= NULL
) {
2663 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2664 pstrcat(buf
, buf_size
, buf1
);
2667 pstrcat(buf
, buf_size
, ", ");
2669 pstrcat(buf
, buf_size
, ")");
2674 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2675 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2678 pstrcpy(buf1
, sizeof(buf1
), "*");
2679 if (t
& VT_CONSTANT
)
2680 pstrcat(buf1
, buf_size
, "const ");
2681 if (t
& VT_VOLATILE
)
2682 pstrcat(buf1
, buf_size
, "volatile ");
2684 pstrcat(buf1
, sizeof(buf1
), varstr
);
2685 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2689 pstrcat(buf
, buf_size
, " ");
2690 pstrcat(buf
, buf_size
, varstr
);
2695 /* verify type compatibility to store vtop in 'dt' type, and generate
2697 static void gen_assign_cast(CType
*dt
)
2699 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2700 char buf1
[256], buf2
[256];
2703 st
= &vtop
->type
; /* source type */
2704 dbt
= dt
->t
& VT_BTYPE
;
2705 sbt
= st
->t
& VT_BTYPE
;
2706 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2707 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2709 It is Ok if both are void
2715 gcc accepts this program
2718 tcc_error("cannot cast from/to void");
2720 if (dt
->t
& VT_CONSTANT
)
2721 tcc_warning("assignment of read-only location");
2724 /* special cases for pointers */
2725 /* '0' can also be a pointer */
2726 if (is_null_pointer(vtop
))
2728 /* accept implicit pointer to integer cast with warning */
2729 if (is_integer_btype(sbt
)) {
2730 tcc_warning("assignment makes pointer from integer without a cast");
2733 type1
= pointed_type(dt
);
2734 /* a function is implicitely a function pointer */
2735 if (sbt
== VT_FUNC
) {
2736 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2737 !is_compatible_types(pointed_type(dt
), st
))
2738 tcc_warning("assignment from incompatible pointer type");
2743 type2
= pointed_type(st
);
2744 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2745 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2746 /* void * can match anything */
2748 /* exact type match, except for unsigned */
2751 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2753 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2755 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2756 tcc_warning("assignment from incompatible pointer type");
2758 /* check const and volatile */
2759 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2760 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2761 tcc_warning("assignment discards qualifiers from pointer target type");
2767 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2768 tcc_warning("assignment makes integer from pointer without a cast");
2769 } else if (sbt
== VT_STRUCT
) {
2770 goto case_VT_STRUCT
;
2772 /* XXX: more tests */
2778 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2779 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2780 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2782 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2783 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2784 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2792 /* store vtop in lvalue pushed on stack */
2793 ST_FUNC
void vstore(void)
2795 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2797 ft
= vtop
[-1].type
.t
;
2798 sbt
= vtop
->type
.t
& VT_BTYPE
;
2799 dbt
= ft
& VT_BTYPE
;
2800 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2801 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2802 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2803 /* optimize char/short casts */
2804 delayed_cast
= VT_MUSTCAST
;
2805 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2806 ((1 << VT_STRUCT_SHIFT
) - 1));
2807 /* XXX: factorize */
2808 if (ft
& VT_CONSTANT
)
2809 tcc_warning("assignment of read-only location");
2812 if (!(ft
& VT_BITFIELD
))
2813 gen_assign_cast(&vtop
[-1].type
);
2816 if (sbt
== VT_STRUCT
) {
2817 /* if structure, only generate pointer */
2818 /* structure assignment : generate memcpy */
2819 /* XXX: optimize if small size */
2820 if (!nocode_wanted
) {
2821 size
= type_size(&vtop
->type
, &align
);
2825 vtop
->type
.t
= VT_PTR
;
2828 /* address of memcpy() */
2831 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2832 else if(!(align
& 3))
2833 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2836 /* Use memmove, rather than memcpy, as dest and src may be same: */
2837 vpush_global_sym(&func_old_type
, TOK_memmove
);
2842 vtop
->type
.t
= VT_PTR
;
2851 /* leave source on stack */
2852 } else if (ft
& VT_BITFIELD
) {
2853 /* bitfield store handling */
2855 /* save lvalue as expression result (example: s.b = s.a = n;) */
2856 vdup(), vtop
[-1] = vtop
[-2];
2858 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2859 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2860 /* remove bit field info to avoid loops */
2861 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2863 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2864 gen_cast(&vtop
[-1].type
);
2865 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2868 /* duplicate destination */
2870 vtop
[-1] = vtop
[-2];
2872 /* mask and shift source */
2873 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2874 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2875 vpushll((1ULL << bit_size
) - 1ULL);
2877 vpushi((1 << bit_size
) - 1);
2883 /* load destination, mask and or with source */
2885 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2886 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2888 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2894 /* ... and discard */
2898 if (!nocode_wanted
) {
2899 #ifdef CONFIG_TCC_BCHECK
2900 /* bound check case */
2901 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2910 #ifdef TCC_TARGET_X86_64
2911 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2913 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2918 r
= gv(rc
); /* generate value */
2919 /* if lvalue was saved on stack, must read it */
2920 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2922 t
= get_reg(RC_INT
);
2923 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2928 sv
.r
= VT_LOCAL
| VT_LVAL
;
2929 sv
.c
.i
= vtop
[-1].c
.i
;
2931 vtop
[-1].r
= t
| VT_LVAL
;
2933 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2934 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2935 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2936 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2938 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2939 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2941 vtop
[-1].type
.t
= load_type
;
2944 /* convert to int to increment easily */
2945 vtop
->type
.t
= addr_type
;
2951 vtop
[-1].type
.t
= load_type
;
2952 /* XXX: it works because r2 is spilled last ! */
2953 store(vtop
->r2
, vtop
- 1);
2959 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2960 vtop
->r
|= delayed_cast
;
2964 /* post defines POST/PRE add. c is the token ++ or -- */
2965 ST_FUNC
void inc(int post
, int c
)
2968 vdup(); /* save lvalue */
2971 gv_dup(); /* duplicate value */
2973 vdup(); /* duplicate value */
2978 vpushi(c
- TOK_MID
);
2980 vstore(); /* store value */
2982 vpop(); /* if post op, return saved value */
2985 static void parse_mult_str (CString
*astr
, const char *msg
)
2987 /* read the string */
2991 while (tok
== TOK_STR
) {
2992 /* XXX: add \0 handling too ? */
2993 cstr_cat(astr
, tokc
.str
.data
, -1);
2996 cstr_ccat(astr
, '\0');
2999 /* Parse GNUC __attribute__ extension. Currently, the following
3000 extensions are recognized:
3001 - aligned(n) : set data/function alignment.
3002 - packed : force data alignment to 1
3003 - section(x) : generate data/code in this section.
3004 - unused : currently ignored, but may be used someday.
3005 - regparm(n) : pass function parameters in registers (i386 only)
3007 static void parse_attribute(AttributeDef
*ad
)
3012 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3016 while (tok
!= ')') {
3017 if (tok
< TOK_IDENT
)
3018 expect("attribute name");
3025 parse_mult_str(&astr
, "section name");
3026 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3033 parse_mult_str(&astr
, "alias(\"target\")");
3034 ad
->alias_target
= /* save string as token, for later */
3035 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3039 case TOK_VISIBILITY1
:
3040 case TOK_VISIBILITY2
:
3042 parse_mult_str(&astr
,
3043 "visibility(\"default|hidden|internal|protected\")");
3044 if (!strcmp (astr
.data
, "default"))
3045 ad
->a
.visibility
= STV_DEFAULT
;
3046 else if (!strcmp (astr
.data
, "hidden"))
3047 ad
->a
.visibility
= STV_HIDDEN
;
3048 else if (!strcmp (astr
.data
, "internal"))
3049 ad
->a
.visibility
= STV_INTERNAL
;
3050 else if (!strcmp (astr
.data
, "protected"))
3051 ad
->a
.visibility
= STV_PROTECTED
;
3053 expect("visibility(\"default|hidden|internal|protected\")");
3062 if (n
<= 0 || (n
& (n
- 1)) != 0)
3063 tcc_error("alignment must be a positive power of two");
3080 /* currently, no need to handle it because tcc does not
3081 track unused objects */
3085 /* currently, no need to handle it because tcc does not
3086 track unused objects */
3091 ad
->a
.func_call
= FUNC_CDECL
;
3096 ad
->a
.func_call
= FUNC_STDCALL
;
3098 #ifdef TCC_TARGET_I386
3108 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3114 ad
->a
.func_call
= FUNC_FASTCALLW
;
3121 ad
->a
.mode
= VT_LLONG
+ 1;
3124 ad
->a
.mode
= VT_BYTE
+ 1;
3127 ad
->a
.mode
= VT_SHORT
+ 1;
3131 ad
->a
.mode
= VT_INT
+ 1;
3134 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3141 ad
->a
.func_export
= 1;
3144 ad
->a
.func_import
= 1;
3147 if (tcc_state
->warn_unsupported
)
3148 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3149 /* skip parameters */
3151 int parenthesis
= 0;
3155 else if (tok
== ')')
3158 } while (parenthesis
&& tok
!= -1);
3171 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3172 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3174 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
, extra_bytes
;
3175 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
3176 Sym
*s
, *ss
, *ass
, **ps
;
3180 a
= tok
; /* save decl type */
3182 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3183 parse_attribute(ad
);
3189 /* struct already defined ? return it */
3191 expect("struct/union/enum name");
3193 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3195 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3203 /* we put an undefined size for struct/union */
3204 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3205 s
->r
= 0; /* default alignment is zero as gcc */
3206 /* put struct/union/enum name in type */
3214 tcc_error("struct/union/enum already defined");
3215 /* cannot be empty */
3217 /* non empty enums are not allowed */
3218 if (a
== TOK_ENUM
) {
3222 expect("identifier");
3224 if (ss
&& !local_stack
)
3225 tcc_error("redefinition of enumerator '%s'",
3226 get_tok_str(v
, NULL
));
3232 /* enum symbols have static storage */
3233 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
3234 ss
->type
.t
|= VT_STATIC
;
3239 /* NOTE: we accept a trailing comma */
3243 s
->c
= type_size(&int_type
, &align
);
3252 while (tok
!= '}') {
3253 parse_btype(&btype
, &ad1
);
3257 tcc_error("flexible array member '%s' not at the end of struct",
3258 get_tok_str(v
, NULL
));
3263 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3265 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3266 expect("identifier");
3268 int v
= btype
.ref
->v
;
3269 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3270 if (tcc_state
->ms_extensions
== 0)
3271 expect("identifier");
3275 if (type_size(&type1
, &align
) < 0) {
3276 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3279 tcc_error("field '%s' has incomplete type",
3280 get_tok_str(v
, NULL
));
3282 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3283 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3284 tcc_error("invalid type for '%s'",
3285 get_tok_str(v
, NULL
));
3289 bit_size
= expr_const();
3290 /* XXX: handle v = 0 case for messages */
3292 tcc_error("negative width in bit-field '%s'",
3293 get_tok_str(v
, NULL
));
3294 if (v
&& bit_size
== 0)
3295 tcc_error("zero width for bit-field '%s'",
3296 get_tok_str(v
, NULL
));
3298 size
= type_size(&type1
, &align
);
3299 if (ad1
.a
.aligned
) {
3300 if (align
< ad1
.a
.aligned
)
3301 align
= ad1
.a
.aligned
;
3302 } else if (ad1
.a
.packed
) {
3304 } else if (*tcc_state
->pack_stack_ptr
) {
3305 if (align
> *tcc_state
->pack_stack_ptr
)
3306 align
= *tcc_state
->pack_stack_ptr
;
3309 if (bit_size
>= 0) {
3310 bt
= type1
.t
& VT_BTYPE
;
3317 tcc_error("bitfields must have scalar type");
3319 if (bit_size
> bsize
) {
3320 tcc_error("width of '%s' exceeds its type",
3321 get_tok_str(v
, NULL
));
3322 } else if (bit_size
== bsize
) {
3323 /* no need for bit fields */
3325 } else if (bit_size
== 0) {
3326 /* XXX: what to do if only padding in a
3328 /* zero size: means to pad */
3331 /* if type change, union, or will overrun
3332 * allignment slot, start at a newly
3334 if ((bit_pos
+ bit_size
) > bsize
||
3335 bt
!= prevbt
|| a
== TOK_UNION
)
3338 /* XXX: handle LSB first */
3339 type1
.t
|= VT_BITFIELD
|
3340 (bit_pos
<< VT_STRUCT_SHIFT
) |
3341 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3342 bit_pos
+= bit_size
;
3343 /* without ms-bitfields, allocate the
3344 * minimum number of bytes necessary,
3345 * adding single bytes as needed */
3346 if (!tcc_state
->ms_bitfields
) {
3348 /* minimum bytes for new bitfield */
3349 size
= (bit_size
+ 7) / 8;
3351 /* enough spare bits already allocated? */
3352 bit_size
= (lbit_pos
- 1) % 8 + 1 + bit_size
;
3353 if (bit_size
> 8) /* doesn't fit */
3354 extra_bytes
= (bit_size
- 1) / 8;
3362 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3363 /* add new memory data only if starting bit
3364 field or adding bytes to existing bit field */
3365 if (extra_bytes
) c
+= extra_bytes
;
3366 else if (lbit_pos
== 0) {
3367 if (a
== TOK_STRUCT
) {
3368 c
= (c
+ align
- 1) & -align
;
3377 if (align
> maxalign
)
3381 printf("add field %s offset=%d",
3382 get_tok_str(v
, NULL
), offset
);
3383 if (type1
.t
& VT_BITFIELD
) {
3384 printf(" pos=%d size=%d",
3385 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3386 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3391 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3393 while ((ass
= ass
->next
) != NULL
) {
3394 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3399 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3403 if (tok
== ';' || tok
== TOK_EOF
)
3410 /* store size and alignment */
3411 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3417 /* return 1 if basic type is a type size (short, long, long long) */
3418 ST_FUNC
int is_btype_size(int bt
)
3420 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3423 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3424 are added to the element type, copied because it could be a typedef. */
3425 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3427 while (type
->t
& VT_ARRAY
) {
3428 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3429 type
= &type
->ref
->type
;
3431 type
->t
|= qualifiers
;
3434 /* return 0 if no type declaration. otherwise, return the basic type
3437 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3439 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3443 memset(ad
, 0, sizeof(AttributeDef
));
3451 /* currently, we really ignore extension */
3462 tcc_error("too many basic types");
3464 bt_size
= is_btype_size (u
& VT_BTYPE
);
3465 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3480 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3481 #ifndef TCC_TARGET_PE
3482 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3484 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3485 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3491 #ifdef TCC_TARGET_ARM64
3493 /* GCC's __uint128_t appears in some Linux header files. Make it a
3494 synonym for long double to get the size and alignment right. */
3506 if ((t
& VT_BTYPE
) == VT_LONG
) {
3507 #ifdef TCC_TARGET_PE
3508 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3510 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3518 struct_decl(&type1
, ad
, VT_ENUM
);
3521 type
->ref
= type1
.ref
;
3525 struct_decl(&type1
, ad
, VT_STRUCT
);
3528 /* type modifiers */
3533 parse_btype_qualify(type
, VT_CONSTANT
);
3541 parse_btype_qualify(type
, VT_VOLATILE
);
3548 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3549 tcc_error("signed and unsigned modifier");
3562 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3563 tcc_error("signed and unsigned modifier");
3564 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3589 /* GNUC attribute */
3590 case TOK_ATTRIBUTE1
:
3591 case TOK_ATTRIBUTE2
:
3592 parse_attribute(ad
);
3595 t
= (t
& ~VT_BTYPE
) | u
;
3603 parse_expr_type(&type1
);
3604 /* remove all storage modifiers except typedef */
3605 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3611 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3614 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3615 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3616 type
->ref
= s
->type
.ref
;
3617 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3618 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3622 /* get attributes from typedef */
3623 if (0 == ad
->a
.aligned
)
3624 ad
->a
.aligned
= s
->a
.aligned
;
3625 if (0 == ad
->a
.func_call
)
3626 ad
->a
.func_call
= s
->a
.func_call
;
3627 ad
->a
.packed
|= s
->a
.packed
;
3636 if (tcc_state
->char_is_unsigned
) {
3637 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3641 /* long is never used as type */
3642 if ((t
& VT_BTYPE
) == VT_LONG
)
3643 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3644 defined TCC_TARGET_PE
3645 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3647 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3653 /* convert a function parameter type (array to pointer and function to
3654 function pointer) */
3655 static inline void convert_parameter_type(CType
*pt
)
3657 /* remove const and volatile qualifiers (XXX: const could be used
3658 to indicate a const function parameter */
3659 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3660 /* array must be transformed to pointer according to ANSI C */
3662 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3667 ST_FUNC
void parse_asm_str(CString
*astr
)
3670 parse_mult_str(astr
, "string constant");
3673 /* Parse an asm label and return the token */
3674 static int asm_label_instr(void)
3680 parse_asm_str(&astr
);
3683 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3685 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3690 static void post_type(CType
*type
, AttributeDef
*ad
)
3692 int n
, l
, t1
, arg_size
, align
;
3693 Sym
**plast
, *s
, *first
;
3698 /* function declaration */
3706 /* read param name and compute offset */
3707 if (l
!= FUNC_OLD
) {
3708 if (!parse_btype(&pt
, &ad1
)) {
3710 tcc_error("invalid type");
3717 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3719 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3720 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3721 tcc_error("parameter declared as void");
3722 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3727 expect("identifier");
3731 convert_parameter_type(&pt
);
3732 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3738 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3745 /* if no parameters, then old type prototype */
3749 /* NOTE: const is ignored in returned type as it has a special
3750 meaning in gcc / C++ */
3751 type
->t
&= ~VT_CONSTANT
;
3752 /* some ancient pre-K&R C allows a function to return an array
3753 and the array brackets to be put after the arguments, such
3754 that "int c()[]" means something like "int[] c()" */
3757 skip(']'); /* only handle simple "[]" */
3760 /* we push a anonymous symbol which will contain the function prototype */
3761 ad
->a
.func_args
= arg_size
;
3762 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3767 } else if (tok
== '[') {
3768 /* array definition */
3770 if (tok
== TOK_RESTRICT1
)
3775 if (!local_stack
|| nocode_wanted
)
3776 vpushi(expr_const());
3778 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3781 tcc_error("invalid array size");
3783 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3784 tcc_error("size of variable length array should be an integer");
3789 /* parse next post type */
3790 post_type(type
, ad
);
3791 if (type
->t
== VT_FUNC
)
3792 tcc_error("declaration of an array of functions");
3793 t1
|= type
->t
& VT_VLA
;
3796 loc
-= type_size(&int_type
, &align
);
3800 vla_runtime_type_size(type
, &align
);
3802 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3809 /* we push an anonymous symbol which will contain the array
3811 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3812 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3817 /* Parse a type declaration (except basic type), and return the type
3818 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3819 expected. 'type' should contain the basic type. 'ad' is the
3820 attribute definition of the basic type. It can be modified by
3823 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3826 CType type1
, *type2
;
3827 int qualifiers
, storage
;
3829 while (tok
== '*') {
3837 qualifiers
|= VT_CONSTANT
;
3842 qualifiers
|= VT_VOLATILE
;
3850 type
->t
|= qualifiers
;
3853 /* XXX: clarify attribute handling */
3854 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3855 parse_attribute(ad
);
3857 /* recursive type */
3858 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3859 type1
.t
= 0; /* XXX: same as int */
3862 /* XXX: this is not correct to modify 'ad' at this point, but
3863 the syntax is not clear */
3864 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3865 parse_attribute(ad
);
3866 type_decl(&type1
, ad
, v
, td
);
3869 /* type identifier */
3870 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3874 if (!(td
& TYPE_ABSTRACT
))
3875 expect("identifier");
3879 storage
= type
->t
& VT_STORAGE
;
3880 type
->t
&= ~VT_STORAGE
;
3881 if (storage
& VT_STATIC
) {
3882 int saved_nocode_wanted
= nocode_wanted
;
3884 post_type(type
, ad
);
3885 nocode_wanted
= saved_nocode_wanted
;
3887 post_type(type
, ad
);
3889 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3890 parse_attribute(ad
);
3894 /* append type at the end of type1 */
3907 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3908 ST_FUNC
int lvalue_type(int t
)
3913 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3915 else if (bt
== VT_SHORT
)
3919 if (t
& VT_UNSIGNED
)
3920 r
|= VT_LVAL_UNSIGNED
;
3924 /* indirection with full error checking and bound check */
3925 ST_FUNC
void indir(void)
3927 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3928 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3932 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3934 vtop
->type
= *pointed_type(&vtop
->type
);
3935 /* Arrays and functions are never lvalues */
3936 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3937 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3938 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3939 /* if bound checking, the referenced pointer must be checked */
3940 #ifdef CONFIG_TCC_BCHECK
3941 if (tcc_state
->do_bounds_check
)
3942 vtop
->r
|= VT_MUSTBOUND
;
3947 /* pass a parameter to a function and do type checking and casting */
3948 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3953 func_type
= func
->c
;
3954 if (func_type
== FUNC_OLD
||
3955 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3956 /* default casting : only need to convert float to double */
3957 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3960 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3961 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3964 } else if (arg
== NULL
) {
3965 tcc_error("too many arguments to function");
3968 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3969 gen_assign_cast(&type
);
3973 /* parse an expression of the form '(type)' or '(expr)' and return its
3975 static void parse_expr_type(CType
*type
)
3981 if (parse_btype(type
, &ad
)) {
3982 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3989 static void parse_type(CType
*type
)
3994 if (!parse_btype(type
, &ad
)) {
3997 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4000 static void vpush_tokc(int t
)
4005 vsetc(&type
, VT_CONST
, &tokc
);
4008 ST_FUNC
void unary(void)
4010 int n
, t
, align
, size
, r
, sizeof_caller
;
4015 sizeof_caller
= in_sizeof
;
4017 /* XXX: GCC 2.95.3 does not generate a table although it should be
4031 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4035 vpush_tokc(VT_LLONG
);
4039 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4043 vpush_tokc(VT_FLOAT
);
4047 vpush_tokc(VT_DOUBLE
);
4051 vpush_tokc(VT_LDOUBLE
);
4054 case TOK___FUNCTION__
:
4056 goto tok_identifier
;
4062 /* special function name identifier */
4063 len
= strlen(funcname
) + 1;
4064 /* generate char[len] type */
4069 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4070 ptr
= section_ptr_add(data_section
, len
);
4071 memcpy(ptr
, funcname
, len
);
4076 #ifdef TCC_TARGET_PE
4077 t
= VT_SHORT
| VT_UNSIGNED
;
4083 /* string parsing */
4086 if (tcc_state
->warn_write_strings
)
4091 memset(&ad
, 0, sizeof(AttributeDef
));
4092 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4097 if (parse_btype(&type
, &ad
)) {
4098 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4100 /* check ISOC99 compound literal */
4102 /* data is allocated locally by default */
4107 /* all except arrays are lvalues */
4108 if (!(type
.t
& VT_ARRAY
))
4109 r
|= lvalue_type(type
.t
);
4110 memset(&ad
, 0, sizeof(AttributeDef
));
4111 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4113 if (sizeof_caller
) {
4120 } else if (tok
== '{') {
4122 tcc_error("expected constant");
4123 /* save all registers */
4126 /* statement expression : we do not accept break/continue
4127 inside as GCC does */
4128 block(NULL
, NULL
, 1);
4143 /* functions names must be treated as function pointers,
4144 except for unary '&' and sizeof. Since we consider that
4145 functions are not lvalues, we only have to handle it
4146 there and in function calls. */
4147 /* arrays can also be used although they are not lvalues */
4148 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4149 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4151 mk_pointer(&vtop
->type
);
4157 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4159 boolean
.t
= VT_BOOL
;
4161 vtop
->c
.i
= !vtop
->c
.i
;
4162 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4166 vseti(VT_JMP
, gvtst(1, 0));
4178 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4179 tcc_error("pointer not accepted for unary plus");
4180 /* In order to force cast, we add zero, except for floating point
4181 where we really need an noop (otherwise -0.0 will be transformed
4183 if (!is_float(vtop
->type
.t
)) {
4194 unary_type(&type
); // Perform a in_sizeof = 0;
4195 size
= type_size(&type
, &align
);
4196 if (t
== TOK_SIZEOF
) {
4197 if (!(type
.t
& VT_VLA
)) {
4199 tcc_error("sizeof applied to an incomplete type");
4202 vla_runtime_type_size(&type
, &align
);
4207 vtop
->type
.t
|= VT_UNSIGNED
;
4210 case TOK_builtin_expect
:
4212 /* __builtin_expect is a no-op for now */
4213 int saved_nocode_wanted
;
4218 saved_nocode_wanted
= nocode_wanted
;
4222 nocode_wanted
= saved_nocode_wanted
;
4226 case TOK_builtin_types_compatible_p
:
4235 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4236 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4237 vpushi(is_compatible_types(&type1
, &type2
));
4240 case TOK_builtin_constant_p
:
4242 int saved_nocode_wanted
, res
;
4245 saved_nocode_wanted
= nocode_wanted
;
4248 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4250 nocode_wanted
= saved_nocode_wanted
;
4255 case TOK_builtin_frame_address
:
4256 case TOK_builtin_return_address
:
4263 if (tok
!= TOK_CINT
) {
4264 tcc_error("%s only takes positive integers",
4265 tok1
== TOK_builtin_return_address
?
4266 "__builtin_return_address" :
4267 "__builtin_frame_address");
4269 level
= (uint32_t)tokc
.i
;
4274 vset(&type
, VT_LOCAL
, 0); /* local frame */
4276 mk_pointer(&vtop
->type
);
4277 indir(); /* -> parent frame */
4279 if (tok1
== TOK_builtin_return_address
) {
4280 // assume return address is just above frame pointer on stack
4283 mk_pointer(&vtop
->type
);
4288 #ifdef TCC_TARGET_X86_64
4289 #ifdef TCC_TARGET_PE
4290 case TOK_builtin_va_start
:
4298 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4299 tcc_error("__builtin_va_start expects a local variable");
4300 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4301 vtop
->type
= char_pointer_type
;
4307 case TOK_builtin_va_arg_types
:
4314 vpushi(classify_x86_64_va_arg(&type
));
4320 #ifdef TCC_TARGET_ARM64
4321 case TOK___va_start
: {
4323 tcc_error("statement in global scope");
4333 vtop
->type
.t
= VT_VOID
;
4336 case TOK___va_arg
: {
4339 tcc_error("statement in global scope");
4351 case TOK___arm64_clear_cache
: {
4360 vtop
->type
.t
= VT_VOID
;
4364 /* pre operations */
4375 t
= vtop
->type
.t
& VT_BTYPE
;
4377 /* In IEEE negate(x) isn't subtract(0,x), but rather
4382 else if (t
== VT_DOUBLE
)
4393 goto tok_identifier
;
4395 /* allow to take the address of a label */
4396 if (tok
< TOK_UIDENT
)
4397 expect("label identifier");
4398 s
= label_find(tok
);
4400 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4402 if (s
->r
== LABEL_DECLARED
)
4403 s
->r
= LABEL_FORWARD
;
4406 s
->type
.t
= VT_VOID
;
4407 mk_pointer(&s
->type
);
4408 s
->type
.t
|= VT_STATIC
;
4410 vpushsym(&s
->type
, s
);
4414 // special qnan , snan and infinity values
4416 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4420 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4424 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4433 expect("identifier");
4436 const char *name
= get_tok_str(t
, NULL
);
4438 tcc_error("'%s' undeclared", name
);
4439 /* for simple function calls, we tolerate undeclared
4440 external reference to int() function */
4441 if (tcc_state
->warn_implicit_function_declaration
4442 #ifdef TCC_TARGET_PE
4443 /* people must be warned about using undeclared WINAPI functions
4444 (which usually start with uppercase letter) */
4445 || (name
[0] >= 'A' && name
[0] <= 'Z')
4448 tcc_warning("implicit declaration of function '%s'", name
);
4449 s
= external_global_sym(t
, &func_old_type
, 0);
4451 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4452 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4453 /* if referencing an inline function, then we generate a
4454 symbol to it if not already done. It will have the
4455 effect to generate code for it at the end of the
4456 compilation unit. Inline function as always
4457 generated in the text section. */
4459 put_extern_sym(s
, text_section
, 0, 0);
4460 r
= VT_SYM
| VT_CONST
;
4464 vset(&s
->type
, r
, s
->c
);
4465 /* if forward reference, we must point to s */
4466 if (vtop
->r
& VT_SYM
) {
4473 /* post operations */
4475 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4478 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4481 if (tok
== TOK_ARROW
)
4483 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4486 /* expect pointer on structure */
4487 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4488 expect("struct or union");
4489 if (tok
== TOK_CDOUBLE
)
4490 expect("field name");
4492 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4493 expect("field name");
4497 while ((s
= s
->next
) != NULL
) {
4502 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4503 /* add field offset to pointer */
4504 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4507 /* change type to field type, and set to lvalue */
4508 vtop
->type
= s
->type
;
4509 vtop
->type
.t
|= qualifiers
;
4510 /* an array is never an lvalue */
4511 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4512 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4513 #ifdef CONFIG_TCC_BCHECK
4514 /* if bound checking, the referenced pointer must be checked */
4515 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4516 vtop
->r
|= VT_MUSTBOUND
;
4520 } else if (tok
== '[') {
4526 } else if (tok
== '(') {
4529 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4532 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4533 /* pointer test (no array accepted) */
4534 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4535 vtop
->type
= *pointed_type(&vtop
->type
);
4536 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4540 expect("function pointer");
4543 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4545 /* get return type */
4548 sa
= s
->next
; /* first parameter */
4551 /* compute first implicit argument if a structure is returned */
4552 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4553 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4554 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4555 &ret_align
, ®size
);
4557 /* get some space for the returned structure */
4558 size
= type_size(&s
->type
, &align
);
4559 #ifdef TCC_TARGET_ARM64
4560 /* On arm64, a small struct is return in registers.
4561 It is much easier to write it to memory if we know
4562 that we are allowed to write some extra bytes, so
4563 round the allocated space up to a power of 2: */
4565 while (size
& (size
- 1))
4566 size
= (size
| (size
- 1)) + 1;
4568 loc
= (loc
- size
) & -align
;
4570 ret
.r
= VT_LOCAL
| VT_LVAL
;
4571 /* pass it as 'int' to avoid structure arg passing
4573 vseti(VT_LOCAL
, loc
);
4583 /* return in register */
4584 if (is_float(ret
.type
.t
)) {
4585 ret
.r
= reg_fret(ret
.type
.t
);
4586 #ifdef TCC_TARGET_X86_64
4587 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4591 #ifndef TCC_TARGET_ARM64
4592 #ifdef TCC_TARGET_X86_64
4593 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4595 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4606 gfunc_param_typed(s
, sa
);
4616 tcc_error("too few arguments to function");
4618 if (!nocode_wanted
) {
4619 gfunc_call(nb_args
);
4621 vtop
-= (nb_args
+ 1);
4625 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4626 vsetc(&ret
.type
, r
, &ret
.c
);
4627 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4630 /* handle packed struct return */
4631 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4634 size
= type_size(&s
->type
, &align
);
4635 /* We're writing whole regs often, make sure there's enough
4636 space. Assume register size is power of 2. */
4637 if (regsize
> align
)
4639 loc
= (loc
- size
) & -align
;
4643 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4647 if (--ret_nregs
== 0)
4651 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4659 ST_FUNC
void expr_prod(void)
4664 while (tok
== '*' || tok
== '/' || tok
== '%') {
4672 ST_FUNC
void expr_sum(void)
4677 while (tok
== '+' || tok
== '-') {
4685 static void expr_shift(void)
4690 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4698 static void expr_cmp(void)
4703 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4704 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4712 static void expr_cmpeq(void)
4717 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4725 static void expr_and(void)
4728 while (tok
== '&') {
4735 static void expr_xor(void)
4738 while (tok
== '^') {
4745 static void expr_or(void)
4748 while (tok
== '|') {
4755 /* XXX: fix this mess */
4756 static void expr_land_const(void)
4759 while (tok
== TOK_LAND
) {
4765 static void expr_lor_const(void)
4768 while (tok
== TOK_LOR
) {
4775 static void expr_land(void)
4778 if (tok
== TOK_LAND
) {
4779 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4790 int saved_nocode_wanted
= nocode_wanted
;
4794 nocode_wanted
= saved_nocode_wanted
;
4802 if (tok
!= TOK_LAND
) {
4813 static void expr_lor(void)
4816 if (tok
== TOK_LOR
) {
4817 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4824 int saved_nocode_wanted
= nocode_wanted
;
4828 nocode_wanted
= saved_nocode_wanted
;
4840 if (tok
!= TOK_LOR
) {
4851 static void expr_cond(void)
4853 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4855 CType type
, type1
, type2
;
4860 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4861 int saved_nocode_wanted
= nocode_wanted
;
4864 boolean
.t
= VT_BOOL
;
4870 if (tok
!= ':' || !gnu_ext
) {
4878 nocode_wanted
= saved_nocode_wanted
;
4881 if (tok
!= ':' || !gnu_ext
) {
4885 nocode_wanted
= saved_nocode_wanted
;
4892 if (vtop
!= vstack
) {
4893 /* needed to avoid having different registers saved in
4895 if (is_float(vtop
->type
.t
)) {
4897 #ifdef TCC_TARGET_X86_64
4898 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4908 if (tok
== ':' && gnu_ext
) {
4916 sv
= *vtop
; /* save value to handle it later */
4917 vtop
--; /* no vpop so that FP stack is not flushed */
4925 bt1
= t1
& VT_BTYPE
;
4927 bt2
= t2
& VT_BTYPE
;
4928 /* cast operands to correct type according to ISOC rules */
4929 if (is_float(bt1
) || is_float(bt2
)) {
4930 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4931 type
.t
= VT_LDOUBLE
;
4932 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4937 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4938 /* cast to biggest op */
4940 /* convert to unsigned if it does not fit in a long long */
4941 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4942 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4943 type
.t
|= VT_UNSIGNED
;
4944 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4945 /* If one is a null ptr constant the result type
4947 if (is_null_pointer (vtop
))
4949 else if (is_null_pointer (&sv
))
4951 /* XXX: test pointer compatibility, C99 has more elaborate
4955 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4956 /* XXX: test function pointer compatibility */
4957 type
= bt1
== VT_FUNC
? type1
: type2
;
4958 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4959 /* XXX: test structure compatibility */
4960 type
= bt1
== VT_STRUCT
? type1
: type2
;
4961 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4962 /* NOTE: as an extension, we accept void on only one side */
4965 /* integer operations */
4967 /* convert to unsigned if it does not fit in an integer */
4968 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4969 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4970 type
.t
|= VT_UNSIGNED
;
4972 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
4973 that `(expr ? a : b).mem` does not error with "lvalue expected" */
4974 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
4976 /* now we convert second operand */
4979 mk_pointer(&vtop
->type
);
4982 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4985 if (is_float(type
.t
)) {
4987 #ifdef TCC_TARGET_X86_64
4988 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4992 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4993 /* for long longs, we use fixed registers to avoid having
4994 to handle a complicated move */
4999 /* this is horrible, but we must also convert first
5003 /* put again first value and cast it */
5007 mk_pointer(&vtop
->type
);
5010 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5013 move_reg(r2
, r1
, type
.t
);
5022 static void expr_eq(void)
5028 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5029 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5030 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5045 ST_FUNC
void gexpr(void)
5056 /* parse an expression and return its type without any side effect. */
5057 static void expr_type(CType
*type
)
5059 int saved_nocode_wanted
;
5061 saved_nocode_wanted
= nocode_wanted
;
5066 nocode_wanted
= saved_nocode_wanted
;
5069 /* parse a unary expression and return its type without any side
5071 static void unary_type(CType
*type
)
5083 /* parse a constant expression and return value in vtop. */
5084 static void expr_const1(void)
5093 /* parse an integer constant and return its value. */
5094 ST_FUNC
int expr_const(void)
5098 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5099 expect("constant expression");
5105 /* return the label token if current token is a label, otherwise
5107 static int is_label(void)
5111 /* fast test first */
5112 if (tok
< TOK_UIDENT
)
5114 /* no need to save tokc because tok is an identifier */
5121 unget_tok(last_tok
);
5126 static void label_or_decl(int l
)
5130 /* fast test first */
5131 if (tok
>= TOK_UIDENT
)
5133 /* no need to save tokc because tok is an identifier */
5137 unget_tok(last_tok
);
5140 unget_tok(last_tok
);
5145 static int case_cmp(const void *pa
, const void *pb
)
5147 int a
= (*(struct case_t
**) pa
)->v1
;
5148 int b
= (*(struct case_t
**) pb
)->v1
;
5149 return a
< b
? -1 : a
> b
;
5152 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
5164 case_reg
= gv(RC_INT
);
5170 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5171 case_reg
= gv(RC_INT
);
5174 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
5175 if (cur_switch
->def_sym
)
5176 gjmp_addr(cur_switch
->def_sym
);
5178 *bsym
= gjmp(*bsym
);
5182 base
+= e
; len
-= e
;
5190 if (p
->v1
== p
->v2
) {
5192 gtst_addr(0, p
->sym
);
5196 case_reg
= gv(RC_INT
);
5202 gtst_addr(0, p
->sym
);
5205 case_reg
= gv(RC_INT
);
5211 static void block(int *bsym
, int *csym
, int is_expr
)
5216 /* generate line number info */
5217 if (tcc_state
->do_debug
&&
5218 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5219 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5221 last_line_num
= file
->line_num
;
5225 /* default return value is (void) */
5227 vtop
->type
.t
= VT_VOID
;
5230 if (tok
== TOK_IF
) {
5237 block(bsym
, csym
, 0);
5239 if (c
== TOK_ELSE
) {
5243 block(bsym
, csym
, 0);
5244 gsym(d
); /* patch else jmp */
5247 } else if (tok
== TOK_WHILE
) {
5263 } else if (tok
== '{') {
5265 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5268 /* record local declaration stack position */
5270 llabel
= local_label_stack
;
5273 /* handle local labels declarations */
5274 if (tok
== TOK_LABEL
) {
5277 if (tok
< TOK_UIDENT
)
5278 expect("label identifier");
5279 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5289 while (tok
!= '}') {
5290 label_or_decl(VT_LOCAL
);
5294 block(bsym
, csym
, is_expr
);
5297 /* pop locally defined labels */
5298 label_pop(&local_label_stack
, llabel
);
5300 /* XXX: this solution makes only valgrind happy...
5301 triggered by gcc.c-torture/execute/20000917-1.c */
5303 switch(vtop
->type
.t
& VT_BTYPE
) {
5305 /* this breaks a compilation of the linux kernel v2.4.26 */
5306 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
5307 /* Look a commit a80acab: Display error on statement expressions with complex return type */
5308 /* A pointer is not a complex return type */
5312 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
5314 tcc_error("unsupported expression type");
5317 /* pop locally defined symbols */
5319 sym_pop(&local_stack
, s
);
5321 /* Pop VLA frames and restore stack pointer if required */
5322 if (vlas_in_scope
> saved_vlas_in_scope
) {
5323 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5326 vlas_in_scope
= saved_vlas_in_scope
;
5329 } else if (tok
== TOK_RETURN
) {
5333 gen_assign_cast(&func_vt
);
5334 #ifdef TCC_TARGET_ARM64
5335 // Perhaps it would be better to use this for all backends:
5338 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5339 CType type
, ret_type
;
5340 int ret_align
, ret_nregs
, regsize
;
5341 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5342 &ret_align
, ®size
);
5343 if (0 == ret_nregs
) {
5344 /* if returning structure, must copy it to implicit
5345 first pointer arg location */
5348 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5351 /* copy structure value to pointer */
5354 /* returning structure packed into registers */
5355 int r
, size
, addr
, align
;
5356 size
= type_size(&func_vt
,&align
);
5357 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5358 (vtop
->c
.i
& (ret_align
-1)))
5359 && (align
& (ret_align
-1))) {
5360 loc
= (loc
- size
) & -ret_align
;
5363 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5367 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5369 vtop
->type
= ret_type
;
5370 if (is_float(ret_type
.t
))
5371 r
= rc_fret(ret_type
.t
);
5382 if (--ret_nregs
== 0)
5384 /* We assume that when a structure is returned in multiple
5385 registers, their classes are consecutive values of the
5388 vtop
->c
.i
+= regsize
;
5392 } else if (is_float(func_vt
.t
)) {
5393 gv(rc_fret(func_vt
.t
));
5398 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5401 /* jump unless last stmt in top-level block */
5402 if (tok
!= '}' || local_scope
!= 1)
5404 } else if (tok
== TOK_BREAK
) {
5407 tcc_error("cannot break");
5408 *bsym
= gjmp(*bsym
);
5411 } else if (tok
== TOK_CONTINUE
) {
5414 tcc_error("cannot continue");
5415 vla_sp_restore_root();
5416 *csym
= gjmp(*csym
);
5419 } else if (tok
== TOK_FOR
) {
5426 /* c99 for-loop init decl? */
5427 if (!decl0(VT_LOCAL
, 1)) {
5428 /* no, regular for-loop init expr */
5460 sym_pop(&local_stack
, s
);
5463 if (tok
== TOK_DO
) {
5480 if (tok
== TOK_SWITCH
) {
5481 struct switch_t
*saved
, sw
;
5485 /* XXX: other types than integer */
5490 b
= gjmp(0); /* jump to first case */
5491 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5495 a
= gjmp(a
); /* add implicit break */
5498 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5499 for (b
= 1; b
< sw
.n
; b
++)
5500 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5501 tcc_error("duplicate case value");
5502 gcase(sw
.p
, sw
.n
, c
, &a
);
5504 gjmp_addr(sw
.def_sym
);
5505 dynarray_reset(&sw
.p
, &sw
.n
);
5510 if (tok
== TOK_CASE
) {
5511 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5515 cr
->v1
= cr
->v2
= expr_const();
5516 if (gnu_ext
&& tok
== TOK_DOTS
) {
5518 cr
->v2
= expr_const();
5519 if (cr
->v2
< cr
->v1
)
5520 tcc_warning("empty case range");
5523 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5526 goto block_after_label
;
5528 if (tok
== TOK_DEFAULT
) {
5533 if (cur_switch
->def_sym
)
5534 tcc_error("too many 'default'");
5535 cur_switch
->def_sym
= ind
;
5537 goto block_after_label
;
5539 if (tok
== TOK_GOTO
) {
5541 if (tok
== '*' && gnu_ext
) {
5545 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5548 } else if (tok
>= TOK_UIDENT
) {
5549 s
= label_find(tok
);
5550 /* put forward definition if needed */
5552 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5554 if (s
->r
== LABEL_DECLARED
)
5555 s
->r
= LABEL_FORWARD
;
5557 vla_sp_restore_root();
5558 if (s
->r
& LABEL_FORWARD
)
5559 s
->jnext
= gjmp(s
->jnext
);
5561 gjmp_addr(s
->jnext
);
5564 expect("label identifier");
5567 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5575 if (s
->r
== LABEL_DEFINED
)
5576 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5578 s
->r
= LABEL_DEFINED
;
5580 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5584 /* we accept this, but it is a mistake */
5587 tcc_warning("deprecated use of label at end of compound statement");
5591 block(bsym
, csym
, is_expr
);
5594 /* expression case */
5609 /* t is the array or struct type. c is the array or struct
5610 address. cur_index/cur_field is the pointer to the current
5611 value. 'size_only' is true if only size info is needed (only used
5613 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5614 int *cur_index
, Sym
**cur_field
,
5618 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5624 if (gnu_ext
&& (l
= is_label()) != 0)
5626 while (tok
== '[' || tok
== '.') {
5628 if (!(type
->t
& VT_ARRAY
))
5629 expect("array type");
5632 index
= expr_const();
5633 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5634 expect("invalid index");
5635 if (tok
== TOK_DOTS
&& gnu_ext
) {
5637 index_last
= expr_const();
5638 if (index_last
< 0 ||
5639 (s
->c
>= 0 && index_last
>= s
->c
) ||
5641 expect("invalid index");
5647 *cur_index
= index_last
;
5648 type
= pointed_type(type
);
5649 elem_size
= type_size(type
, &align
);
5650 c
+= index
* elem_size
;
5651 /* NOTE: we only support ranges for last designator */
5652 nb_elems
= index_last
- index
+ 1;
5653 if (nb_elems
!= 1) {
5662 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5663 expect("struct/union type");
5676 /* XXX: fix this mess by using explicit storage field */
5678 type1
.t
|= (type
->t
& ~VT_TYPE
);
5692 if (type
->t
& VT_ARRAY
) {
5694 type
= pointed_type(type
);
5695 c
+= index
* type_size(type
, &align
);
5699 tcc_error("too many field init");
5700 /* XXX: fix this mess by using explicit storage field */
5702 type1
.t
|= (type
->t
& ~VT_TYPE
);
5707 decl_initializer(type
, sec
, c
, 0, size_only
);
5709 /* XXX: make it more general */
5710 if (!size_only
&& nb_elems
> 1) {
5711 unsigned long c_end
;
5716 tcc_error("range init not supported yet for dynamic storage");
5717 c_end
= c
+ nb_elems
* elem_size
;
5718 if (c_end
> sec
->data_allocated
)
5719 section_realloc(sec
, c_end
);
5720 src
= sec
->data
+ c
;
5722 for(i
= 1; i
< nb_elems
; i
++) {
5724 memcpy(dst
, src
, elem_size
);
5730 #define EXPR_CONST 1
5733 /* store a value or an expression directly in global data or in local array */
5734 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5735 int v
, int expr_type
)
5737 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5739 unsigned long long bit_mask
;
5747 /* compound literals must be allocated globally in this case */
5748 saved_global_expr
= global_expr
;
5751 global_expr
= saved_global_expr
;
5752 /* NOTE: symbols are accepted */
5753 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5754 tcc_error("initializer element is not constant");
5762 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5765 /* XXX: not portable */
5766 /* XXX: generate error if incorrect relocation */
5767 gen_assign_cast(&dtype
);
5768 bt
= type
->t
& VT_BTYPE
;
5769 /* we'll write at most 16 bytes */
5770 if (c
+ 16 > sec
->data_allocated
) {
5771 section_realloc(sec
, c
+ 16);
5773 ptr
= sec
->data
+ c
;
5774 /* XXX: make code faster ? */
5775 if (!(type
->t
& VT_BITFIELD
)) {
5780 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5781 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5782 bit_mask
= (1LL << bit_size
) - 1;
5784 if ((vtop
->r
& VT_SYM
) &&
5790 (bt
== VT_INT
&& bit_size
!= 32)))
5791 tcc_error("initializer element is not computable at load time");
5793 /* XXX: when cross-compiling we assume that each type has the
5794 same representation on host and target, which is likely to
5795 be wrong in the case of long double */
5797 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5799 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5802 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5805 *(double *)ptr
= vtop
->c
.d
;
5808 if (sizeof(long double) == LDOUBLE_SIZE
)
5809 *(long double *)ptr
= vtop
->c
.ld
;
5810 else if (sizeof(double) == LDOUBLE_SIZE
)
5811 *(double *)ptr
= vtop
->c
.ld
;
5813 tcc_error("can't cross compile long double constants");
5816 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5819 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5820 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5821 if (vtop
->r
& VT_SYM
)
5822 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5824 *(addr_t
*)ptr
|= val
;
5826 if (vtop
->r
& VT_SYM
)
5827 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5828 *(addr_t
*)ptr
|= val
;
5833 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5834 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5835 if (vtop
->r
& VT_SYM
)
5836 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5840 if (vtop
->r
& VT_SYM
)
5841 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5849 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5856 /* put zeros for variable based init */
5857 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5860 /* nothing to do because globals are already set to zero */
5862 vpush_global_sym(&func_old_type
, TOK_memset
);
5864 #ifdef TCC_TARGET_ARM
5875 /* 't' contains the type and storage info. 'c' is the offset of the
5876 object in section 'sec'. If 'sec' is NULL, it means stack based
5877 allocation. 'first' is true if array '{' must be read (multi
5878 dimension implicit array init handling). 'size_only' is true if
5879 size only evaluation is wanted (only for arrays). */
5880 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5881 int first
, int size_only
)
5883 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5884 int size1
, align1
, expr_type
;
5888 if (type
->t
& VT_VLA
) {
5891 /* save current stack pointer */
5892 if (vlas_in_scope
== 0) {
5893 if (vla_sp_root_loc
== -1)
5894 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5895 gen_vla_sp_save(vla_sp_root_loc
);
5898 vla_runtime_type_size(type
, &a
);
5899 gen_vla_alloc(type
, a
);
5903 } else if (type
->t
& VT_ARRAY
) {
5907 t1
= pointed_type(type
);
5908 size1
= type_size(t1
, &align1
);
5911 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5914 tcc_error("character array initializer must be a literal,"
5915 " optionally enclosed in braces");
5920 /* only parse strings here if correct type (otherwise: handle
5921 them as ((w)char *) expressions */
5922 if ((tok
== TOK_LSTR
&&
5923 #ifdef TCC_TARGET_PE
5924 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5926 (t1
->t
& VT_BTYPE
) == VT_INT
5928 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5929 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5932 /* compute maximum number of chars wanted */
5934 cstr_len
= tokc
.str
.size
;
5936 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
5939 if (n
>= 0 && nb
> (n
- array_length
))
5940 nb
= n
- array_length
;
5943 tcc_warning("initializer-string for array is too long");
5944 /* in order to go faster for common case (char
5945 string in global variable, we handle it
5947 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5948 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
5952 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
5954 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
5955 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5963 /* only add trailing zero if enough storage (no
5964 warning in this case since it is standard) */
5965 if (n
< 0 || array_length
< n
) {
5967 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5973 while (tok
!= '}') {
5974 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5975 if (n
>= 0 && index
>= n
)
5976 tcc_error("index too large");
5977 /* must put zero in holes (note that doing it that way
5978 ensures that it even works with designators) */
5979 if (!size_only
&& array_length
< index
) {
5980 init_putz(t1
, sec
, c
+ array_length
* size1
,
5981 (index
- array_length
) * size1
);
5984 if (index
> array_length
)
5985 array_length
= index
;
5986 /* special test for multi dimensional arrays (may not
5987 be strictly correct if designators are used at the
5989 if (index
>= n
&& no_oblock
)
5998 /* put zeros at the end */
5999 if (!size_only
&& n
>= 0 && array_length
< n
) {
6000 init_putz(t1
, sec
, c
+ array_length
* size1
,
6001 (n
- array_length
) * size1
);
6003 /* patch type size if needed */
6005 s
->c
= array_length
;
6006 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
6007 (sec
|| !first
|| tok
== '{')) {
6009 /* NOTE: the previous test is a specific case for automatic
6010 struct/union init */
6011 /* XXX: union needs only one init */
6018 if (tcc_state
->old_struct_init_code
) {
6019 /* an old version of struct initialization.
6020 It have a problems. But with a new version
6021 linux 2.4.26 can't load ramdisk.
6023 while (tok
== '(') {
6027 if (!parse_btype(&type1
, &ad1
))
6029 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
6031 if (!is_assignable_types(type
, &type1
))
6032 tcc_error("invalid type for cast");
6039 if (!parse_btype(&type1
, &ad1
))
6041 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
6043 if (!is_assignable_types(type
, &type1
))
6044 tcc_error("invalid type for cast");
6053 if (first
|| tok
== '{') {
6062 while (tok
!= '}') {
6063 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
6065 if (!size_only
&& array_length
< index
) {
6066 init_putz(type
, sec
, c
+ array_length
,
6067 index
- array_length
);
6069 index
= index
+ type_size(&f
->type
, &align1
);
6070 if (index
> array_length
)
6071 array_length
= index
;
6073 /* gr: skip fields from same union - ugly. */
6076 int f_size
= type_size(&f
->type
, &align
);
6077 int f_type
= (f
->type
.t
& VT_BTYPE
);
6079 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
6080 /* test for same offset */
6081 if (f
->next
->c
!= f
->c
)
6083 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
6085 Lets assume a structure of size 0 can't be a member of the union.
6086 This allow to compile the following code from a linux kernel v2.4.26
6087 typedef struct { } rwlock_t;
6093 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
6094 tcc-0.9.23 can succesfully compile this version of the kernel.
6095 gcc don't have problems with this code too.
6099 /* if yes, test for bitfield shift */
6100 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
6101 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6102 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6103 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
6104 if (bit_pos_1
!= bit_pos_2
)
6111 if (no_oblock
&& f
== NULL
)
6117 /* put zeros at the end */
6118 if (!size_only
&& array_length
< n
) {
6119 init_putz(type
, sec
, c
+ array_length
,
6128 } else if (tok
== '{') {
6130 decl_initializer(type
, sec
, c
, first
, size_only
);
6132 } else if (size_only
) {
6133 /* just skip expression */
6134 parlevel
= parlevel1
= 0;
6135 while ((parlevel
> 0 || parlevel1
> 0 ||
6136 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6139 else if (tok
== ')') {
6140 if (parlevel
== 0 && parlevel1
== 0)
6144 else if (tok
== '{')
6146 else if (tok
== '}') {
6147 if (parlevel
== 0 && parlevel1
== 0)
6154 /* currently, we always use constant expression for globals
6155 (may change for scripting case) */
6156 expr_type
= EXPR_CONST
;
6158 expr_type
= EXPR_ANY
;
6159 init_putv(type
, sec
, c
, 0, expr_type
);
6163 /* parse an initializer for type 't' if 'has_init' is non zero, and
6164 allocate space in local or global data space ('r' is either
6165 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6166 variable 'v' of scope 'scope' is declared before initializers
6167 are parsed. If 'v' is zero, then a reference to the new object
6168 is put in the value stack. If 'has_init' is 2, a special parsing
6169 is done to handle string constants. */
6170 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6171 int has_init
, int v
, int scope
)
6173 int size
, align
, addr
, data_offset
;
6175 ParseState saved_parse_state
= {0};
6176 TokenString
*init_str
= NULL
;
6178 Sym
*flexible_array
;
6180 flexible_array
= NULL
;
6181 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6182 Sym
*field
= type
->ref
->next
;
6185 field
= field
->next
;
6186 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6187 flexible_array
= field
;
6191 size
= type_size(type
, &align
);
6192 /* If unknown size, we must evaluate it before
6193 evaluating initializers because
6194 initializers can generate global data too
6195 (e.g. string pointers or ISOC99 compound
6196 literals). It also simplifies local
6197 initializers handling */
6198 if (size
< 0 || (flexible_array
&& has_init
)) {
6200 tcc_error("unknown type size");
6201 /* get all init string */
6202 init_str
= tok_str_alloc();
6203 if (has_init
== 2) {
6204 /* only get strings */
6205 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6206 tok_str_add_tok(init_str
);
6211 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6213 tcc_error("unexpected end of file in initializer");
6214 tok_str_add_tok(init_str
);
6217 else if (tok
== '}') {
6227 tok_str_add(init_str
, -1);
6228 tok_str_add(init_str
, 0);
6231 save_parse_state(&saved_parse_state
);
6233 begin_macro(init_str
, 1);
6235 decl_initializer(type
, NULL
, 0, 1, 1);
6236 /* prepare second initializer parsing */
6237 macro_ptr
= init_str
->str
;
6240 /* if still unknown size, error */
6241 size
= type_size(type
, &align
);
6243 tcc_error("unknown type size");
6245 /* If there's a flex member and it was used in the initializer
6247 if (flexible_array
&&
6248 flexible_array
->type
.ref
->c
> 0)
6249 size
+= flexible_array
->type
.ref
->c
6250 * pointed_size(&flexible_array
->type
);
6251 /* take into account specified alignment if bigger */
6252 if (ad
->a
.aligned
) {
6253 if (ad
->a
.aligned
> align
)
6254 align
= ad
->a
.aligned
;
6255 } else if (ad
->a
.packed
) {
6258 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6260 #ifdef CONFIG_TCC_BCHECK
6261 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6265 loc
= (loc
- size
) & -align
;
6267 #ifdef CONFIG_TCC_BCHECK
6268 /* handles bounds */
6269 /* XXX: currently, since we do only one pass, we cannot track
6270 '&' operators, so we add only arrays */
6271 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6273 /* add padding between regions */
6275 /* then add local bound info */
6276 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6277 bounds_ptr
[0] = addr
;
6278 bounds_ptr
[1] = size
;
6282 /* local variable */
6283 sym_push(v
, type
, r
, addr
);
6285 /* push local reference */
6286 vset(type
, r
, addr
);
6292 if (v
&& scope
== VT_CONST
) {
6293 /* see if the symbol was already defined */
6296 if (!is_compatible_types(&sym
->type
, type
))
6297 tcc_error("incompatible types for redefinition of '%s'",
6298 get_tok_str(v
, NULL
));
6299 if (sym
->type
.t
& VT_EXTERN
) {
6300 /* if the variable is extern, it was not allocated */
6301 sym
->type
.t
&= ~VT_EXTERN
;
6302 /* set array size if it was omitted in extern
6304 if ((sym
->type
.t
& VT_ARRAY
) &&
6305 sym
->type
.ref
->c
< 0 &&
6307 sym
->type
.ref
->c
= type
->ref
->c
;
6309 /* we accept several definitions of the same
6310 global variable. this is tricky, because we
6311 must play with the SHN_COMMON type of the symbol */
6312 /* XXX: should check if the variable was already
6313 initialized. It is incorrect to initialized it
6315 /* no init data, we won't add more to the symbol */
6322 /* allocate symbol in corresponding section */
6327 else if (tcc_state
->nocommon
)
6331 data_offset
= sec
->data_offset
;
6332 data_offset
= (data_offset
+ align
- 1) & -align
;
6334 /* very important to increment global pointer at this time
6335 because initializers themselves can create new initializers */
6336 data_offset
+= size
;
6337 #ifdef CONFIG_TCC_BCHECK
6338 /* add padding if bound check */
6339 if (tcc_state
->do_bounds_check
)
6342 sec
->data_offset
= data_offset
;
6343 /* allocate section space to put the data */
6344 if (sec
->sh_type
!= SHT_NOBITS
&&
6345 data_offset
> sec
->data_allocated
)
6346 section_realloc(sec
, data_offset
);
6347 /* align section if needed */
6348 if (align
> sec
->sh_addralign
)
6349 sec
->sh_addralign
= align
;
6351 addr
= 0; /* avoid warning */
6355 if (scope
!= VT_CONST
|| !sym
) {
6356 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6357 sym
->asm_label
= ad
->asm_label
;
6359 /* update symbol definition */
6361 put_extern_sym(sym
, sec
, addr
, size
);
6364 /* put a common area */
6365 put_extern_sym(sym
, NULL
, align
, size
);
6366 /* XXX: find a nicer way */
6367 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6368 esym
->st_shndx
= SHN_COMMON
;
6371 /* push global reference */
6372 sym
= get_sym_ref(type
, sec
, addr
, size
);
6373 vpushsym(type
, sym
);
6375 /* patch symbol weakness */
6376 if (type
->t
& VT_WEAK
)
6378 apply_visibility(sym
, type
);
6379 #ifdef CONFIG_TCC_BCHECK
6380 /* handles bounds now because the symbol must be defined
6381 before for the relocation */
6382 if (tcc_state
->do_bounds_check
) {
6385 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6386 /* then add global bound info */
6387 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6388 bounds_ptr
[0] = 0; /* relocated */
6389 bounds_ptr
[1] = size
;
6393 if (has_init
|| (type
->t
& VT_VLA
)) {
6394 decl_initializer(type
, sec
, addr
, 1, 0);
6395 /* patch flexible array member size back to -1, */
6396 /* for possible subsequent similar declarations */
6398 flexible_array
->type
.ref
->c
= -1;
6401 /* restore parse state if needed */
6404 restore_parse_state(&saved_parse_state
);
6408 static void put_func_debug(Sym
*sym
)
6413 /* XXX: we put here a dummy type */
6414 snprintf(buf
, sizeof(buf
), "%s:%c1",
6415 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6416 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6417 cur_text_section
, sym
->c
);
6418 /* //gr gdb wants a line at the function */
6419 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6424 /* parse an old style function declaration list */
6425 /* XXX: check multiple parameter */
6426 static void func_decl_list(Sym
*func_sym
)
6433 /* parse each declaration */
6434 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6435 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6436 if (!parse_btype(&btype
, &ad
))
6437 expect("declaration list");
6438 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6439 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6441 /* we accept no variable after */
6445 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6446 /* find parameter in function parameter list */
6449 if ((s
->v
& ~SYM_FIELD
) == v
)
6453 tcc_error("declaration for parameter '%s' but no such parameter",
6454 get_tok_str(v
, NULL
));
6456 /* check that no storage specifier except 'register' was given */
6457 if (type
.t
& VT_STORAGE
)
6458 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6459 convert_parameter_type(&type
);
6460 /* we can add the type (NOTE: it could be local to the function) */
6462 /* accept other parameters */
6473 /* parse a function defined by symbol 'sym' and generate its code in
6474 'cur_text_section' */
6475 static void gen_function(Sym
*sym
)
6477 int saved_nocode_wanted
= nocode_wanted
;
6480 ind
= cur_text_section
->data_offset
;
6481 /* NOTE: we patch the symbol size later */
6482 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6483 funcname
= get_tok_str(sym
->v
, NULL
);
6485 /* Initialize VLA state */
6487 vla_sp_root_loc
= -1;
6488 /* put debug symbol */
6489 if (tcc_state
->do_debug
)
6490 put_func_debug(sym
);
6492 /* push a dummy symbol to enable local sym storage */
6493 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6494 local_scope
= 1; /* for function parameters */
6495 gfunc_prolog(&sym
->type
);
6499 block(NULL
, NULL
, 0);
6502 cur_text_section
->data_offset
= ind
;
6503 label_pop(&global_label_stack
, NULL
);
6504 /* reset local stack */
6506 sym_pop(&local_stack
, NULL
);
6507 /* end of function */
6508 /* patch symbol size */
6509 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6511 /* patch symbol weakness (this definition overrules any prototype) */
6512 if (sym
->type
.t
& VT_WEAK
)
6514 apply_visibility(sym
, &sym
->type
);
6515 if (tcc_state
->do_debug
) {
6516 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6518 /* It's better to crash than to generate wrong code */
6519 cur_text_section
= NULL
;
6520 funcname
= ""; /* for safety */
6521 func_vt
.t
= VT_VOID
; /* for safety */
6522 func_var
= 0; /* for safety */
6523 ind
= 0; /* for safety */
6524 nocode_wanted
= saved_nocode_wanted
;
6528 static void gen_inline_functions(TCCState
*s
)
6531 int inline_generated
, i
, ln
;
6532 struct InlineFunc
*fn
;
6534 ln
= file
->line_num
;
6535 /* iterate while inline function are referenced */
6537 inline_generated
= 0;
6538 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6539 fn
= s
->inline_fns
[i
];
6541 if (sym
&& sym
->c
) {
6542 /* the function was used: generate its code and
6543 convert it to a normal function */
6546 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6547 sym
->r
= VT_SYM
| VT_CONST
;
6548 sym
->type
.t
&= ~VT_INLINE
;
6550 begin_macro(fn
->func_str
, 1);
6552 cur_text_section
= text_section
;
6556 inline_generated
= 1;
6559 if (!inline_generated
)
6562 file
->line_num
= ln
;
6565 ST_FUNC
void free_inline_functions(TCCState
*s
)
6568 /* free tokens of unused inline functions */
6569 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6570 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6572 tok_str_free(fn
->func_str
);
6574 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6577 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6578 static int decl0(int l
, int is_for_loop_init
)
6586 if (!parse_btype(&btype
, &ad
)) {
6587 if (is_for_loop_init
)
6589 /* skip redundant ';' */
6590 /* XXX: find more elegant solution */
6595 if (l
== VT_CONST
&&
6596 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6597 /* global asm block */
6601 /* special test for old K&R protos without explicit int
6602 type. Only accepted when defining global data */
6603 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6607 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6608 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6610 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6611 int v
= btype
.ref
->v
;
6612 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6613 tcc_warning("unnamed struct/union that defines no instances");
6618 while (1) { /* iterate thru each declaration */
6620 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6624 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6625 printf("type = '%s'\n", buf
);
6628 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6629 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6630 tcc_error("function without file scope cannot be static");
6632 /* if old style function prototype, we accept a
6635 if (sym
->c
== FUNC_OLD
)
6636 func_decl_list(sym
);
6639 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6640 ad
.asm_label
= asm_label_instr();
6641 /* parse one last attribute list, after asm label */
6642 parse_attribute(&ad
);
6649 #ifdef TCC_TARGET_PE
6650 if (ad
.a
.func_import
)
6651 type
.t
|= VT_IMPORT
;
6652 if (ad
.a
.func_export
)
6653 type
.t
|= VT_EXPORT
;
6655 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6659 tcc_error("cannot use local functions");
6660 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6661 expect("function definition");
6663 /* reject abstract declarators in function definition */
6665 while ((sym
= sym
->next
) != NULL
)
6666 if (!(sym
->v
& ~SYM_FIELD
))
6667 expect("identifier");
6669 /* XXX: cannot do better now: convert extern line to static inline */
6670 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6671 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6676 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6679 ref
= sym
->type
.ref
;
6680 if (0 == ref
->a
.func_proto
)
6681 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6683 /* use func_call from prototype if not defined */
6684 if (ref
->a
.func_call
!= FUNC_CDECL
6685 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6686 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6688 /* use export from prototype */
6689 if (ref
->a
.func_export
)
6690 type
.ref
->a
.func_export
= 1;
6692 /* use static from prototype */
6693 if (sym
->type
.t
& VT_STATIC
)
6694 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6696 /* If the definition has no visibility use the
6697 one from prototype. */
6698 if (! (type
.t
& VT_VIS_MASK
))
6699 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6701 if (!is_compatible_types(&sym
->type
, &type
)) {
6703 tcc_error("incompatible types for redefinition of '%s'",
6704 get_tok_str(v
, NULL
));
6706 type
.ref
->a
.func_proto
= 0;
6707 /* if symbol is already defined, then put complete type */
6710 /* put function symbol */
6711 sym
= global_identifier_push(v
, type
.t
, 0);
6712 sym
->type
.ref
= type
.ref
;
6715 /* static inline functions are just recorded as a kind
6716 of macro. Their code will be emitted at the end of
6717 the compilation unit only if they are used */
6718 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6719 (VT_INLINE
| VT_STATIC
)) {
6721 struct InlineFunc
*fn
;
6722 const char *filename
;
6724 filename
= file
? file
->filename
: "";
6725 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6726 strcpy(fn
->filename
, filename
);
6728 fn
->func_str
= tok_str_alloc();
6734 tcc_error("unexpected end of file");
6735 tok_str_add_tok(fn
->func_str
);
6740 } else if (t
== '}') {
6742 if (block_level
== 0)
6746 tok_str_add(fn
->func_str
, -1);
6747 tok_str_add(fn
->func_str
, 0);
6748 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6751 /* compute text section */
6752 cur_text_section
= ad
.section
;
6753 if (!cur_text_section
)
6754 cur_text_section
= text_section
;
6755 sym
->r
= VT_SYM
| VT_CONST
;
6760 if (btype
.t
& VT_TYPEDEF
) {
6761 /* save typedefed type */
6762 /* XXX: test storage specifiers ? */
6764 if (sym
&& sym
->scope
== local_scope
) {
6765 if (!is_compatible_types(&sym
->type
, &type
)
6766 || !(sym
->type
.t
& VT_TYPEDEF
))
6767 tcc_error("incompatible redefinition of '%s'",
6768 get_tok_str(v
, NULL
));
6771 sym
= sym_push(v
, &type
, 0, 0);
6774 sym
->type
.t
|= VT_TYPEDEF
;
6777 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6778 /* external function definition */
6779 /* specific case for func_call attribute */
6780 ad
.a
.func_proto
= 1;
6782 } else if (!(type
.t
& VT_ARRAY
)) {
6783 /* not lvalue if array */
6784 r
|= lvalue_type(type
.t
);
6786 has_init
= (tok
== '=');
6787 if (has_init
&& (type
.t
& VT_VLA
))
6788 tcc_error("Variable length array cannot be initialized");
6789 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6790 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6791 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6792 /* external variable or function */
6793 /* NOTE: as GCC, uninitialized global static
6794 arrays of null size are considered as
6796 sym
= external_sym(v
, &type
, r
);
6797 sym
->asm_label
= ad
.asm_label
;
6799 if (ad
.alias_target
) {
6804 alias_target
= sym_find(ad
.alias_target
);
6805 if (!alias_target
|| !alias_target
->c
)
6806 tcc_error("unsupported forward __alias__ attribute");
6807 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6808 tsec
.sh_num
= esym
->st_shndx
;
6809 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6812 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6813 if (type
.t
& VT_STATIC
)
6819 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6823 if (is_for_loop_init
)
6836 ST_FUNC
void decl(int l
)
6841 /* ------------------------------------------------------------------------- */