2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /********************************************************/
23 /* global variables */
25 /* loc : local variable index
26 ind : output code index
28 anon_sym: anonymous symbol index
30 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
32 ST_DATA Sym
*sym_free_first
;
33 ST_DATA
void **sym_pools
;
34 ST_DATA
int nb_sym_pools
;
36 ST_DATA Sym
*global_stack
;
37 ST_DATA Sym
*local_stack
;
38 ST_DATA Sym
*define_stack
;
39 ST_DATA Sym
*global_label_stack
;
40 ST_DATA Sym
*local_label_stack
;
42 static Sym
*all_cleanups
, *pending_gotos
;
43 static int local_scope
;
45 static int in_generic
;
46 static int section_sym
;
48 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
50 ST_DATA
int const_wanted
; /* true if constant wanted */
51 ST_DATA
int nocode_wanted
; /* no code generation wanted */
52 #define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
53 #define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
55 /* Automagical code suppression ----> */
56 #define CODE_OFF() (nocode_wanted |= 0x20000000)
57 #define CODE_ON() (nocode_wanted &= ~0x20000000)
59 /* Clear 'nocode_wanted' at label if it was used */
60 ST_FUNC
void gsym(int t
) { if (t
) { gsym_addr(t
, ind
); CODE_ON(); }}
61 static int gind(void) { CODE_ON(); return ind
; }
63 /* Set 'nocode_wanted' after unconditional jumps */
64 static void gjmp_addr_acs(int t
) { gjmp_addr(t
); CODE_OFF(); }
65 static int gjmp_acs(int t
) { t
= gjmp(t
); CODE_OFF(); return t
; }
67 /* These are #undef'd at the end of this file */
68 #define gjmp_addr gjmp_addr_acs
72 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
73 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
74 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
76 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
77 ST_DATA
const char *funcname
;
80 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
, ptrdiff_type
;
82 ST_DATA
struct switch_t
{
86 } **p
; int n
; /* list of case ranges */
87 int def_sym
; /* default symbol */
90 } *cur_switch
; /* current switch */
92 #define MAX_TEMP_LOCAL_VARIABLE_NUMBER 0x4
93 /*list of temporary local variables on the stack in current function. */
94 ST_DATA
struct temp_local_variable
{
95 int location
; //offset on stack. Svalue.c.i
98 } arr_temp_local_vars
[MAX_TEMP_LOCAL_VARIABLE_NUMBER
];
99 short nb_temp_local_vars
;
101 static struct scope
{
103 struct { int loc
, num
; } vla
;
104 struct { Sym
*s
; int n
; } cl
;
107 } *cur_scope
, *loop_scope
, *root_scope
;
109 /* ------------------------------------------------------------------------- */
111 static void gen_cast(CType
*type
);
112 static void gen_cast_s(int t
);
113 static inline CType
*pointed_type(CType
*type
);
114 static int is_compatible_types(CType
*type1
, CType
*type2
);
115 static int parse_btype(CType
*type
, AttributeDef
*ad
);
116 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
117 static void parse_expr_type(CType
*type
);
118 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
);
119 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int flags
);
120 static void block(int is_expr
);
121 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
122 static void decl(int l
);
123 static int decl0(int l
, int is_for_loop_init
, Sym
*);
124 static void expr_eq(void);
125 static void vla_runtime_type_size(CType
*type
, int *a
);
126 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
);
127 static inline int64_t expr_const64(void);
128 static void vpush64(int ty
, unsigned long long v
);
129 static void vpush(CType
*type
);
130 static int gvtst(int inv
, int t
);
131 static void gen_inline_functions(TCCState
*s
);
132 static void skip_or_save_block(TokenString
**str
);
133 static void gv_dup(void);
134 static int get_temp_local_var(int size
,int align
);
135 static void clear_temp_local_var_list();
137 ST_INLN
int is_float(int t
)
141 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
144 /* we use our own 'finite' function to avoid potential problems with
145 non standard math libs */
146 /* XXX: endianness dependent */
147 ST_FUNC
int ieee_finite(double d
)
150 memcpy(p
, &d
, sizeof(double));
151 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
154 /* compiling intel long double natively */
155 #if (defined __i386__ || defined __x86_64__) \
156 && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
157 # define TCC_IS_NATIVE_387
160 ST_FUNC
void test_lvalue(void)
162 if (!(vtop
->r
& VT_LVAL
))
166 ST_FUNC
void check_vstack(void)
169 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
172 /* ------------------------------------------------------------------------- */
173 /* vstack debugging aid */
176 void pv (const char *lbl
, int a
, int b
)
179 for (i
= a
; i
< a
+ b
; ++i
) {
180 SValue
*p
= &vtop
[-i
];
181 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
182 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
187 /* ------------------------------------------------------------------------- */
188 /* start of translation unit info */
189 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
194 /* file info: full path + filename */
195 section_sym
= put_elf_sym(symtab_section
, 0, 0,
196 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
197 text_section
->sh_num
, NULL
);
198 getcwd(buf
, sizeof(buf
));
200 normalize_slashes(buf
);
202 pstrcat(buf
, sizeof(buf
), "/");
203 put_stabs_r(buf
, N_SO
, 0, 0,
204 text_section
->data_offset
, text_section
, section_sym
);
205 put_stabs_r(file
->filename
, N_SO
, 0, 0,
206 text_section
->data_offset
, text_section
, section_sym
);
211 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
212 symbols can be safely used */
213 put_elf_sym(symtab_section
, 0, 0,
214 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
215 SHN_ABS
, file
->filename
);
218 /* put end of translation unit info */
219 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
223 put_stabs_r(NULL
, N_SO
, 0, 0,
224 text_section
->data_offset
, text_section
, section_sym
);
228 /* generate line number info */
229 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
233 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
234 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
236 last_line_num
= file
->line_num
;
240 /* put function symbol */
241 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
249 /* XXX: we put here a dummy type */
250 snprintf(buf
, sizeof(buf
), "%s:%c1",
251 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
252 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
253 cur_text_section
, sym
->c
);
254 /* //gr gdb wants a line at the function */
255 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
261 /* put function size */
262 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
266 put_stabn(N_FUN
, 0, 0, size
);
269 /* ------------------------------------------------------------------------- */
270 ST_FUNC
int tccgen_compile(TCCState
*s1
)
272 cur_text_section
= NULL
;
274 anon_sym
= SYM_FIRST_ANOM
;
277 nocode_wanted
= 0x80000000;
280 /* define some often used types */
282 char_pointer_type
.t
= VT_BYTE
;
283 mk_pointer(&char_pointer_type
);
285 size_type
.t
= VT_INT
| VT_UNSIGNED
;
286 ptrdiff_type
.t
= VT_INT
;
288 size_type
.t
= VT_LLONG
| VT_UNSIGNED
;
289 ptrdiff_type
.t
= VT_LLONG
;
291 size_type
.t
= VT_LONG
| VT_LLONG
| VT_UNSIGNED
;
292 ptrdiff_type
.t
= VT_LONG
| VT_LLONG
;
294 func_old_type
.t
= VT_FUNC
;
295 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, 0, 0);
296 func_old_type
.ref
->f
.func_call
= FUNC_CDECL
;
297 func_old_type
.ref
->f
.func_type
= FUNC_OLD
;
301 #ifdef TCC_TARGET_ARM
306 printf("%s: **** new file\n", file
->filename
);
309 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
312 gen_inline_functions(s1
);
314 /* end of translation unit info */
319 /* ------------------------------------------------------------------------- */
320 ST_FUNC ElfSym
*elfsym(Sym
*s
)
324 return &((ElfSym
*)symtab_section
->data
)[s
->c
];
327 /* apply storage attributes to Elf symbol */
328 ST_FUNC
void update_storage(Sym
*sym
)
331 int sym_bind
, old_sym_bind
;
337 if (sym
->a
.visibility
)
338 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
341 if (sym
->type
.t
& (VT_STATIC
| VT_INLINE
))
342 sym_bind
= STB_LOCAL
;
343 else if (sym
->a
.weak
)
346 sym_bind
= STB_GLOBAL
;
347 old_sym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
348 if (sym_bind
!= old_sym_bind
) {
349 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, ELFW(ST_TYPE
)(esym
->st_info
));
353 if (sym
->a
.dllimport
)
354 esym
->st_other
|= ST_PE_IMPORT
;
355 if (sym
->a
.dllexport
)
356 esym
->st_other
|= ST_PE_EXPORT
;
360 printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
361 get_tok_str(sym
->v
, NULL
),
362 sym_bind
== STB_WEAK
? 'w' : sym_bind
== STB_LOCAL
? 'l' : 'g',
370 /* ------------------------------------------------------------------------- */
371 /* update sym->c so that it points to an external symbol in section
372 'section' with value 'value' */
374 ST_FUNC
void put_extern_sym2(Sym
*sym
, int sh_num
,
375 addr_t value
, unsigned long size
,
376 int can_add_underscore
)
378 int sym_type
, sym_bind
, info
, other
, t
;
382 #ifdef CONFIG_TCC_BCHECK
387 name
= get_tok_str(sym
->v
, NULL
);
388 #ifdef CONFIG_TCC_BCHECK
389 if (tcc_state
->do_bounds_check
) {
390 /* XXX: avoid doing that for statics ? */
391 /* if bound checking is activated, we change some function
392 names by adding the "__bound" prefix */
395 /* XXX: we rely only on malloc hooks */
408 strcpy(buf
, "__bound_");
416 if ((t
& VT_BTYPE
) == VT_FUNC
) {
418 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
419 sym_type
= STT_NOTYPE
;
421 sym_type
= STT_OBJECT
;
423 if (t
& (VT_STATIC
| VT_INLINE
))
424 sym_bind
= STB_LOCAL
;
426 sym_bind
= STB_GLOBAL
;
429 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
430 Sym
*ref
= sym
->type
.ref
;
431 if (ref
->a
.nodecorate
) {
432 can_add_underscore
= 0;
434 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
435 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
437 other
|= ST_PE_STDCALL
;
438 can_add_underscore
= 0;
442 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
444 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
448 name
= get_tok_str(sym
->asm_label
, NULL
);
449 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
450 sym
->c
= put_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
453 esym
->st_value
= value
;
454 esym
->st_size
= size
;
455 esym
->st_shndx
= sh_num
;
460 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
461 addr_t value
, unsigned long size
)
463 int sh_num
= section
? section
->sh_num
: SHN_UNDEF
;
464 put_extern_sym2(sym
, sh_num
, value
, size
, 1);
467 /* add a new relocation entry to symbol 'sym' in section 's' */
468 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
473 if (nocode_wanted
&& s
== cur_text_section
)
478 put_extern_sym(sym
, NULL
, 0, 0);
482 /* now we can add ELF relocation info */
483 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
487 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
489 greloca(s
, sym
, offset
, type
, 0);
493 /* ------------------------------------------------------------------------- */
494 /* symbol allocator */
495 static Sym
*__sym_malloc(void)
497 Sym
*sym_pool
, *sym
, *last_sym
;
500 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
501 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
503 last_sym
= sym_free_first
;
505 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
506 sym
->next
= last_sym
;
510 sym_free_first
= last_sym
;
514 static inline Sym
*sym_malloc(void)
518 sym
= sym_free_first
;
520 sym
= __sym_malloc();
521 sym_free_first
= sym
->next
;
524 sym
= tcc_malloc(sizeof(Sym
));
529 ST_INLN
void sym_free(Sym
*sym
)
532 sym
->next
= sym_free_first
;
533 sym_free_first
= sym
;
539 /* push, without hashing */
540 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
545 memset(s
, 0, sizeof *s
);
555 /* find a symbol and return its associated structure. 's' is the top
556 of the symbol stack */
557 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
569 /* structure lookup */
570 ST_INLN Sym
*struct_find(int v
)
573 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
575 return table_ident
[v
]->sym_struct
;
578 /* find an identifier */
579 ST_INLN Sym
*sym_find(int v
)
582 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
584 return table_ident
[v
]->sym_identifier
;
587 static int sym_scope(Sym
*s
)
589 if (IS_ENUM_VAL (s
->type
.t
))
590 return s
->type
.ref
->sym_scope
;
595 /* push a given symbol on the symbol stack */
596 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
605 s
= sym_push2(ps
, v
, type
->t
, c
);
606 s
->type
.ref
= type
->ref
;
608 /* don't record fields or anonymous symbols */
610 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
611 /* record symbol in token array */
612 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
614 ps
= &ts
->sym_struct
;
616 ps
= &ts
->sym_identifier
;
619 s
->sym_scope
= local_scope
;
620 if (s
->prev_tok
&& sym_scope(s
->prev_tok
) == s
->sym_scope
)
621 tcc_error("redeclaration of '%s'",
622 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
627 /* push a global identifier */
628 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
631 s
= sym_push2(&global_stack
, v
, t
, c
);
632 s
->r
= VT_CONST
| VT_SYM
;
633 /* don't record anonymous symbol */
634 if (v
< SYM_FIRST_ANOM
) {
635 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
636 /* modify the top most local identifier, so that sym_identifier will
637 point to 's' when popped; happens when called from inline asm */
638 while (*ps
!= NULL
&& (*ps
)->sym_scope
)
639 ps
= &(*ps
)->prev_tok
;
646 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
647 pop them yet from the list, but do remove them from the token array. */
648 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
658 /* remove symbol in token array */
660 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
661 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
663 ps
= &ts
->sym_struct
;
665 ps
= &ts
->sym_identifier
;
676 /* ------------------------------------------------------------------------- */
677 static void vcheck_cmp(void)
679 /* cannot let cpu flags if other instruction are generated. Also
680 avoid leaving VT_JMP anywhere except on the top of the stack
681 because it would complicate the code generator.
683 Don't do this when nocode_wanted. vtop might come from
684 !nocode_wanted regions (see 88_codeopt.c) and transforming
685 it to a register without actually generating code is wrong
686 as their value might still be used for real. All values
687 we push under nocode_wanted will eventually be popped
688 again, so that the VT_CMP/VT_JMP value will be in vtop
689 when code is unsuppressed again. */
691 if (vtop
->r
== VT_CMP
&& !nocode_wanted
)
695 static void vsetc(CType
*type
, int r
, CValue
*vc
)
697 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
698 tcc_error("memory full (vstack)");
708 ST_FUNC
void vswap(void)
718 /* pop stack value */
719 ST_FUNC
void vpop(void)
722 v
= vtop
->r
& VT_VALMASK
;
723 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
724 /* for x86, we need to pop the FP stack */
726 o(0xd8dd); /* fstp %st(0) */
730 /* need to put correct jump if && or || without test */
737 /* push constant of type "type" with useless value */
738 ST_FUNC
void vpush(CType
*type
)
740 vset(type
, VT_CONST
, 0);
743 /* push integer constant */
744 ST_FUNC
void vpushi(int v
)
748 vsetc(&int_type
, VT_CONST
, &cval
);
751 /* push a pointer sized constant */
752 static void vpushs(addr_t v
)
756 vsetc(&size_type
, VT_CONST
, &cval
);
759 /* push arbitrary 64bit constant */
760 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
767 vsetc(&ctype
, VT_CONST
, &cval
);
770 /* push long long constant */
771 static inline void vpushll(long long v
)
773 vpush64(VT_LLONG
, v
);
776 ST_FUNC
void vset(CType
*type
, int r
, int v
)
781 vsetc(type
, r
, &cval
);
784 static void vseti(int r
, int v
)
792 ST_FUNC
void vpushv(SValue
*v
)
794 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
795 tcc_error("memory full (vstack)");
800 static void vdup(void)
805 /* rotate n first stack elements to the bottom
806 I1 ... In -> I2 ... In I1 [top is right]
808 ST_FUNC
void vrotb(int n
)
820 /* rotate the n elements before entry e towards the top
821 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
823 ST_FUNC
void vrote(SValue
*e
, int n
)
830 for(i
= 0;i
< n
- 1; i
++)
835 /* rotate n first stack elements to the top
836 I1 ... In -> In I1 ... I(n-1) [top is right]
838 ST_FUNC
void vrott(int n
)
843 /* ------------------------------------------------------------------------- */
844 /* vtop->r = VT_CMP means CPU-flags have been set from comparison or test. */
846 /* called from generators to set the result from relational ops */
847 ST_FUNC
void vset_VT_CMP(int op
)
855 /* called once before asking generators to load VT_CMP to a register */
856 static void vset_VT_JMP(void)
858 int op
= vtop
->cmp_op
;
859 if (vtop
->jtrue
|| vtop
->jfalse
) {
860 /* we need to jump to 'mov $0,%R' or 'mov $1,%R' */
861 int inv
= op
& (op
< 2); /* small optimization */
862 vseti(VT_JMP
+inv
, gvtst(inv
, 0));
864 /* otherwise convert flags (rsp. 0/1) to register */
866 if (op
< 2) /* doesn't seem to happen */
871 /* Set CPU Flags, doesn't yet jump */
872 static void gvtst_set(int inv
, int t
)
875 if (vtop
->r
!= VT_CMP
) {
878 if (vtop
->r
== VT_CMP
) /* must be VT_CONST otherwise */
880 else if (vtop
->r
== VT_CONST
)
881 vset_VT_CMP(vtop
->c
.i
!= 0);
885 p
= inv
? &vtop
->jfalse
: &vtop
->jtrue
;
886 *p
= gjmp_append(*p
, t
);
889 /* Generate value test
891 * Generate a test for any value (jump, comparison and integers) */
892 static int gvtst(int inv
, int t
)
898 t
= vtop
->jtrue
, u
= vtop
->jfalse
;
903 /* jump to the wanted target */
905 t
= gjmp_cond(op
^ inv
, t
);
908 /* resolve complementary jumps to here */
915 /* ------------------------------------------------------------------------- */
916 /* push a symbol value of TYPE */
917 static inline void vpushsym(CType
*type
, Sym
*sym
)
921 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
925 /* Return a static symbol pointing to a section */
926 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
932 sym
= sym_push(v
, type
, VT_CONST
| VT_SYM
, 0);
933 sym
->type
.t
|= VT_STATIC
;
934 put_extern_sym(sym
, sec
, offset
, size
);
938 /* push a reference to a section offset by adding a dummy symbol */
939 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
941 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
944 /* define a new external reference to a symbol 'v' of type 'u' */
945 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
)
951 /* push forward reference */
952 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
953 s
->type
.ref
= type
->ref
;
954 } else if (IS_ASM_SYM(s
)) {
955 s
->type
.t
= type
->t
| (s
->type
.t
& VT_EXTERN
);
956 s
->type
.ref
= type
->ref
;
962 /* Merge symbol attributes. */
963 static void merge_symattr(struct SymAttr
*sa
, struct SymAttr
*sa1
)
965 if (sa1
->aligned
&& !sa
->aligned
)
966 sa
->aligned
= sa1
->aligned
;
967 sa
->packed
|= sa1
->packed
;
968 sa
->weak
|= sa1
->weak
;
969 if (sa1
->visibility
!= STV_DEFAULT
) {
970 int vis
= sa
->visibility
;
971 if (vis
== STV_DEFAULT
972 || vis
> sa1
->visibility
)
973 vis
= sa1
->visibility
;
974 sa
->visibility
= vis
;
976 sa
->dllexport
|= sa1
->dllexport
;
977 sa
->nodecorate
|= sa1
->nodecorate
;
978 sa
->dllimport
|= sa1
->dllimport
;
981 /* Merge function attributes. */
982 static void merge_funcattr(struct FuncAttr
*fa
, struct FuncAttr
*fa1
)
984 if (fa1
->func_call
&& !fa
->func_call
)
985 fa
->func_call
= fa1
->func_call
;
986 if (fa1
->func_type
&& !fa
->func_type
)
987 fa
->func_type
= fa1
->func_type
;
988 if (fa1
->func_args
&& !fa
->func_args
)
989 fa
->func_args
= fa1
->func_args
;
992 /* Merge attributes. */
993 static void merge_attr(AttributeDef
*ad
, AttributeDef
*ad1
)
995 merge_symattr(&ad
->a
, &ad1
->a
);
996 merge_funcattr(&ad
->f
, &ad1
->f
);
999 ad
->section
= ad1
->section
;
1000 if (ad1
->alias_target
)
1001 ad
->alias_target
= ad1
->alias_target
;
1003 ad
->asm_label
= ad1
->asm_label
;
1005 ad
->attr_mode
= ad1
->attr_mode
;
1008 /* Merge some type attributes. */
1009 static void patch_type(Sym
*sym
, CType
*type
)
1011 if (!(type
->t
& VT_EXTERN
) || IS_ENUM_VAL(sym
->type
.t
)) {
1012 if (!(sym
->type
.t
& VT_EXTERN
))
1013 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
1014 sym
->type
.t
&= ~VT_EXTERN
;
1017 if (IS_ASM_SYM(sym
)) {
1018 /* stay static if both are static */
1019 sym
->type
.t
= type
->t
& (sym
->type
.t
| ~VT_STATIC
);
1020 sym
->type
.ref
= type
->ref
;
1023 if (!is_compatible_types(&sym
->type
, type
)) {
1024 tcc_error("incompatible types for redefinition of '%s'",
1025 get_tok_str(sym
->v
, NULL
));
1027 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1028 int static_proto
= sym
->type
.t
& VT_STATIC
;
1029 /* warn if static follows non-static function declaration */
1030 if ((type
->t
& VT_STATIC
) && !static_proto
1031 /* XXX this test for inline shouldn't be here. Until we
1032 implement gnu-inline mode again it silences a warning for
1033 mingw caused by our workarounds. */
1034 && !((type
->t
| sym
->type
.t
) & VT_INLINE
))
1035 tcc_warning("static storage ignored for redefinition of '%s'",
1036 get_tok_str(sym
->v
, NULL
));
1038 /* set 'inline' if both agree or if one has static */
1039 if ((type
->t
| sym
->type
.t
) & VT_INLINE
) {
1040 if (!((type
->t
^ sym
->type
.t
) & VT_INLINE
)
1041 || ((type
->t
| sym
->type
.t
) & VT_STATIC
))
1042 static_proto
|= VT_INLINE
;
1045 if (0 == (type
->t
& VT_EXTERN
)) {
1046 /* put complete type, use static from prototype */
1047 sym
->type
.t
= (type
->t
& ~(VT_STATIC
|VT_INLINE
)) | static_proto
;
1048 sym
->type
.ref
= type
->ref
;
1050 sym
->type
.t
&= ~VT_INLINE
| static_proto
;
1053 if (sym
->type
.ref
->f
.func_type
== FUNC_OLD
1054 && type
->ref
->f
.func_type
!= FUNC_OLD
) {
1055 sym
->type
.ref
= type
->ref
;
1059 if ((sym
->type
.t
& VT_ARRAY
) && type
->ref
->c
>= 0) {
1060 /* set array size if it was omitted in extern declaration */
1061 sym
->type
.ref
->c
= type
->ref
->c
;
1063 if ((type
->t
^ sym
->type
.t
) & VT_STATIC
)
1064 tcc_warning("storage mismatch for redefinition of '%s'",
1065 get_tok_str(sym
->v
, NULL
));
1069 /* Merge some storage attributes. */
1070 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
1073 patch_type(sym
, type
);
1075 #ifdef TCC_TARGET_PE
1076 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
1077 tcc_error("incompatible dll linkage for redefinition of '%s'",
1078 get_tok_str(sym
->v
, NULL
));
1080 merge_symattr(&sym
->a
, &ad
->a
);
1082 sym
->asm_label
= ad
->asm_label
;
1083 update_storage(sym
);
1086 /* copy sym to other stack */
1087 static Sym
*sym_copy(Sym
*s0
, Sym
**ps
)
1090 s
= sym_malloc(), *s
= *s0
;
1091 s
->prev
= *ps
, *ps
= s
;
1092 if (s
->v
< SYM_FIRST_ANOM
) {
1093 ps
= &table_ident
[s
->v
- TOK_IDENT
]->sym_identifier
;
1094 s
->prev_tok
= *ps
, *ps
= s
;
1099 /* copy a list of syms */
1100 static void sym_copy_ref(Sym
*s0
, Sym
**ps
)
1102 Sym
*s
, **sp
= &s0
->type
.ref
;
1103 for (s
= *sp
, *sp
= NULL
; s
; s
= s
->next
)
1104 sp
= &(*sp
= sym_copy(s
, ps
))->next
;
1107 /* define a new external reference to a symbol 'v' */
1108 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
1112 /* look for global symbol */
1114 while (s
&& s
->sym_scope
)
1118 /* push forward reference */
1119 s
= global_identifier_push(v
, type
->t
, 0);
1122 s
->asm_label
= ad
->asm_label
;
1123 s
->type
.ref
= type
->ref
;
1124 bt
= s
->type
.t
& (VT_BTYPE
|VT_ARRAY
);
1125 /* copy type to the global stack also */
1126 if (local_scope
&& (bt
== VT_FUNC
|| (bt
& VT_ARRAY
)))
1127 sym_copy_ref(s
, &global_stack
);
1129 patch_storage(s
, ad
, type
);
1130 bt
= s
->type
.t
& VT_BTYPE
;
1132 /* push variables to local scope if any */
1133 if (local_stack
&& bt
!= VT_FUNC
)
1134 s
= sym_copy(s
, &local_stack
);
1138 /* push a reference to global symbol v */
1139 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
1141 vpushsym(type
, external_global_sym(v
, type
));
1144 /* save registers up to (vtop - n) stack entry */
1145 ST_FUNC
void save_regs(int n
)
1148 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
1152 /* save r to the memory stack, and mark it as being free */
1153 ST_FUNC
void save_reg(int r
)
1155 save_reg_upstack(r
, 0);
1158 /* save r to the memory stack, and mark it as being free,
1159 if seen up to (vtop - n) stack entry */
1160 ST_FUNC
void save_reg_upstack(int r
, int n
)
1162 int l
, saved
, size
, align
;
1166 if ((r
&= VT_VALMASK
) >= VT_CONST
)
1171 /* modify all stack values */
1174 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
1175 if ((p
->r
& VT_VALMASK
) == r
||
1176 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
1177 /* must save value on stack if not already done */
1179 /* NOTE: must reload 'r' because r might be equal to r2 */
1180 r
= p
->r
& VT_VALMASK
;
1181 /* store register in the stack */
1183 if ((p
->r
& VT_LVAL
) ||
1184 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
1186 type
= &char_pointer_type
;
1190 size
= type_size(type
, &align
);
1191 l
=get_temp_local_var(size
,align
);
1192 sv
.type
.t
= type
->t
;
1193 sv
.r
= VT_LOCAL
| VT_LVAL
;
1196 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1197 /* x86 specific: need to pop fp register ST0 if saved */
1198 if (r
== TREG_ST0
) {
1199 o(0xd8dd); /* fstp %st(0) */
1203 /* special long long case */
1204 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
1211 /* mark that stack entry as being saved on the stack */
1212 if (p
->r
& VT_LVAL
) {
1213 /* also clear the bounded flag because the
1214 relocation address of the function was stored in
1216 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
1218 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
1226 #ifdef TCC_TARGET_ARM
1227 /* find a register of class 'rc2' with at most one reference on stack.
1228 * If none, call get_reg(rc) */
1229 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
1234 for(r
=0;r
<NB_REGS
;r
++) {
1235 if (reg_classes
[r
] & rc2
) {
1238 for(p
= vstack
; p
<= vtop
; p
++) {
1239 if ((p
->r
& VT_VALMASK
) == r
||
1240 (p
->r2
& VT_VALMASK
) == r
)
1251 /* find a free register of class 'rc'. If none, save one register */
1252 ST_FUNC
int get_reg(int rc
)
1257 /* find a free register */
1258 for(r
=0;r
<NB_REGS
;r
++) {
1259 if (reg_classes
[r
] & rc
) {
1262 for(p
=vstack
;p
<=vtop
;p
++) {
1263 if ((p
->r
& VT_VALMASK
) == r
||
1264 (p
->r2
& VT_VALMASK
) == r
)
1272 /* no register left : free the first one on the stack (VERY
1273 IMPORTANT to start from the bottom to ensure that we don't
1274 spill registers used in gen_opi()) */
1275 for(p
=vstack
;p
<=vtop
;p
++) {
1276 /* look at second register (if long long) */
1277 r
= p
->r2
& VT_VALMASK
;
1278 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1280 r
= p
->r
& VT_VALMASK
;
1281 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1287 /* Should never comes here */
1291 /* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
1292 static int get_temp_local_var(int size
,int align
){
1294 struct temp_local_variable
*temp_var
;
1301 for(i
=0;i
<nb_temp_local_vars
;i
++){
1302 temp_var
=&arr_temp_local_vars
[i
];
1303 if(temp_var
->size
<size
||align
!=temp_var
->align
){
1306 /*check if temp_var is free*/
1308 for(p
=vstack
;p
<=vtop
;p
++) {
1310 if(r
==VT_LOCAL
||r
==VT_LLOCAL
){
1311 if(p
->c
.i
==temp_var
->location
){
1318 found_var
=temp_var
->location
;
1324 loc
= (loc
- size
) & -align
;
1325 if(nb_temp_local_vars
<MAX_TEMP_LOCAL_VARIABLE_NUMBER
){
1326 temp_var
=&arr_temp_local_vars
[i
];
1327 temp_var
->location
=loc
;
1328 temp_var
->size
=size
;
1329 temp_var
->align
=align
;
1330 nb_temp_local_vars
++;
1337 static void clear_temp_local_var_list(){
1338 nb_temp_local_vars
=0;
1341 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1343 static void move_reg(int r
, int s
, int t
)
1357 /* get address of vtop (vtop MUST BE an lvalue) */
1358 ST_FUNC
void gaddrof(void)
1360 vtop
->r
&= ~VT_LVAL
;
1361 /* tricky: if saved lvalue, then we can go back to lvalue */
1362 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1363 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1368 #ifdef CONFIG_TCC_BCHECK
1369 /* generate lvalue bound code */
1370 static void gbound(void)
1375 vtop
->r
&= ~VT_MUSTBOUND
;
1376 /* if lvalue, then use checking code before dereferencing */
1377 if (vtop
->r
& VT_LVAL
) {
1378 /* if not VT_BOUNDED value, then make one */
1379 if (!(vtop
->r
& VT_BOUNDED
)) {
1380 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1381 /* must save type because we must set it to int to get pointer */
1383 vtop
->type
.t
= VT_PTR
;
1386 gen_bounded_ptr_add();
1387 vtop
->r
|= lval_type
;
1390 /* then check for dereferencing */
1391 gen_bounded_ptr_deref();
1396 static void incr_bf_adr(int o
)
1398 vtop
->type
= char_pointer_type
;
1402 vtop
->type
.t
= (vtop
->type
.t
& ~(VT_BTYPE
|VT_DEFSIGN
))
1403 | (VT_BYTE
|VT_UNSIGNED
);
1404 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1405 | (VT_LVAL_BYTE
|VT_LVAL_UNSIGNED
|VT_LVAL
);
1408 /* single-byte load mode for packed or otherwise unaligned bitfields */
1409 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1412 save_reg_upstack(vtop
->r
, 1);
1413 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1414 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1423 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1425 vpushi((1 << n
) - 1), gen_op('&');
1428 vpushi(bits
), gen_op(TOK_SHL
);
1431 bits
+= n
, bit_size
-= n
, o
= 1;
1434 if (!(type
->t
& VT_UNSIGNED
)) {
1435 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG
? 64 : 32) - bits
;
1436 vpushi(n
), gen_op(TOK_SHL
);
1437 vpushi(n
), gen_op(TOK_SAR
);
1441 /* single-byte store mode for packed or otherwise unaligned bitfields */
1442 static void store_packed_bf(int bit_pos
, int bit_size
)
1444 int bits
, n
, o
, m
, c
;
1446 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1448 save_reg_upstack(vtop
->r
, 1);
1449 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1451 incr_bf_adr(o
); // X B
1453 c
? vdup() : gv_dup(); // B V X
1456 vpushi(bits
), gen_op(TOK_SHR
);
1458 vpushi(bit_pos
), gen_op(TOK_SHL
);
1463 m
= ((1 << n
) - 1) << bit_pos
;
1464 vpushi(m
), gen_op('&'); // X B V1
1465 vpushv(vtop
-1); // X B V1 B
1466 vpushi(m
& 0x80 ? ~m
& 0x7f : ~m
);
1467 gen_op('&'); // X B V1 B1
1468 gen_op('|'); // X B V2
1470 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1471 vstore(), vpop(); // X B
1472 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1477 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1480 if (0 == sv
->type
.ref
)
1482 t
= sv
->type
.ref
->auxtype
;
1483 if (t
!= -1 && t
!= VT_STRUCT
) {
1484 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1485 sv
->r
= (sv
->r
& ~VT_LVAL_TYPE
) | lvalue_type(sv
->type
.t
);
1490 /* store vtop a register belonging to class 'rc'. lvalues are
1491 converted to values. Cannot be used if cannot be converted to
1492 register value (such as structures). */
1493 ST_FUNC
int gv(int rc
)
1495 int r
, bit_pos
, bit_size
, size
, align
, rc2
;
1497 /* NOTE: get_reg can modify vstack[] */
1498 if (vtop
->type
.t
& VT_BITFIELD
) {
1501 bit_pos
= BIT_POS(vtop
->type
.t
);
1502 bit_size
= BIT_SIZE(vtop
->type
.t
);
1503 /* remove bit field info to avoid loops */
1504 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1507 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1508 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1509 type
.t
|= VT_UNSIGNED
;
1511 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1513 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1518 if (r
== VT_STRUCT
) {
1519 load_packed_bf(&type
, bit_pos
, bit_size
);
1521 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG
? 64 : 32;
1522 /* cast to int to propagate signedness in following ops */
1524 /* generate shifts */
1525 vpushi(bits
- (bit_pos
+ bit_size
));
1527 vpushi(bits
- bit_size
);
1528 /* NOTE: transformed to SHR if unsigned */
1533 if (is_float(vtop
->type
.t
) &&
1534 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1535 unsigned long offset
;
1536 /* CPUs usually cannot use float constants, so we store them
1537 generically in data segment */
1538 size
= type_size(&vtop
->type
, &align
);
1540 size
= 0, align
= 1;
1541 offset
= section_add(data_section
, size
, align
);
1542 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1544 init_putv(&vtop
->type
, data_section
, offset
);
1547 #ifdef CONFIG_TCC_BCHECK
1548 if (vtop
->r
& VT_MUSTBOUND
)
1552 r
= vtop
->r
& VT_VALMASK
;
1553 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1554 #ifndef TCC_TARGET_ARM64
1555 #ifndef TCC_TARGET_RISCV64 /* XXX: remove the whole LRET/QRET class */
1558 #ifdef TCC_TARGET_X86_64
1559 else if (rc
== RC_FRET
)
1564 /* need to reload if:
1566 - lvalue (need to dereference pointer)
1567 - already a register, but not in the right class */
1569 || (vtop
->r
& VT_LVAL
)
1570 || !(reg_classes
[r
] & rc
)
1572 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1573 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1575 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1581 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1582 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1584 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1585 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1586 unsigned long long ll
;
1588 int r2
, original_type
;
1589 original_type
= vtop
->type
.t
;
1590 /* two register type load : expand to two words
1593 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1596 vtop
->c
.i
= ll
; /* first word */
1598 vtop
->r
= r
; /* save register value */
1599 vpushi(ll
>> 32); /* second word */
1602 if (vtop
->r
& VT_LVAL
) {
1603 /* We do not want to modifier the long long
1604 pointer here, so the safest (and less
1605 efficient) is to save all the other registers
1606 in the stack. XXX: totally inefficient. */
1610 /* lvalue_save: save only if used further down the stack */
1611 save_reg_upstack(vtop
->r
, 1);
1613 /* load from memory */
1614 vtop
->type
.t
= load_type
;
1617 vtop
[-1].r
= r
; /* save register value */
1618 /* increment pointer to get second word */
1619 vtop
->type
.t
= addr_type
;
1624 vtop
->type
.t
= load_type
;
1626 /* move registers */
1629 vtop
[-1].r
= r
; /* save register value */
1630 vtop
->r
= vtop
[-1].r2
;
1632 /* Allocate second register. Here we rely on the fact that
1633 get_reg() tries first to free r2 of an SValue. */
1637 /* write second register */
1639 vtop
->type
.t
= original_type
;
1640 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1642 /* lvalue of scalar type : need to use lvalue type
1643 because of possible cast */
1646 /* compute memory access type */
1647 if (vtop
->r
& VT_LVAL_BYTE
)
1649 else if (vtop
->r
& VT_LVAL_SHORT
)
1651 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1655 /* restore wanted type */
1658 if (vtop
->r
== VT_CMP
)
1660 /* one register type load */
1665 #ifdef TCC_TARGET_C67
1666 /* uses register pairs for doubles */
1667 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1674 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1675 ST_FUNC
void gv2(int rc1
, int rc2
)
1677 /* generate more generic register first. But VT_JMP or VT_CMP
1678 values must be generated first in all cases to avoid possible
1680 if (vtop
->r
!= VT_CMP
&& rc1
<= rc2
) {
1685 /* test if reload is needed for first register */
1686 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1696 /* test if reload is needed for first register */
1697 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1703 #ifndef TCC_TARGET_ARM64
1704 /* wrapper around RC_FRET to return a register by type */
1705 static int rc_fret(int t
)
1707 #ifdef TCC_TARGET_X86_64
1708 if (t
== VT_LDOUBLE
) {
1716 /* wrapper around REG_FRET to return a register by type */
1717 static int reg_fret(int t
)
1719 #ifdef TCC_TARGET_X86_64
1720 if (t
== VT_LDOUBLE
) {
1728 /* expand 64bit on stack in two ints */
1729 ST_FUNC
void lexpand(void)
1732 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1733 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1734 if (v
== VT_CONST
) {
1737 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1743 vtop
[0].r
= vtop
[-1].r2
;
1744 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1746 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1751 /* build a long long from two ints */
1752 static void lbuild(int t
)
1754 gv2(RC_INT
, RC_INT
);
1755 vtop
[-1].r2
= vtop
[0].r
;
1756 vtop
[-1].type
.t
= t
;
1761 /* convert stack entry to register and duplicate its value in another
1763 static void gv_dup(void)
1770 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1771 if (t
& VT_BITFIELD
) {
1781 /* stack: H L L1 H1 */
1791 /* duplicate value */
1796 #ifdef TCC_TARGET_X86_64
1797 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1807 load(r1
, &sv
); /* move r to r1 */
1809 /* duplicates value */
1816 /* generate CPU independent (unsigned) long long operations */
1817 static void gen_opl(int op
)
1819 int t
, a
, b
, op1
, c
, i
;
1821 unsigned short reg_iret
= REG_IRET
;
1822 unsigned short reg_lret
= REG_LRET
;
1828 func
= TOK___divdi3
;
1831 func
= TOK___udivdi3
;
1834 func
= TOK___moddi3
;
1837 func
= TOK___umoddi3
;
1844 /* call generic long long function */
1845 vpush_global_sym(&func_old_type
, func
);
1850 vtop
->r2
= reg_lret
;
1858 //pv("gen_opl A",0,2);
1864 /* stack: L1 H1 L2 H2 */
1869 vtop
[-2] = vtop
[-3];
1872 /* stack: H1 H2 L1 L2 */
1873 //pv("gen_opl B",0,4);
1879 /* stack: H1 H2 L1 L2 ML MH */
1882 /* stack: ML MH H1 H2 L1 L2 */
1886 /* stack: ML MH H1 L2 H2 L1 */
1891 /* stack: ML MH M1 M2 */
1894 } else if (op
== '+' || op
== '-') {
1895 /* XXX: add non carry method too (for MIPS or alpha) */
1901 /* stack: H1 H2 (L1 op L2) */
1904 gen_op(op1
+ 1); /* TOK_xxxC2 */
1907 /* stack: H1 H2 (L1 op L2) */
1910 /* stack: (L1 op L2) H1 H2 */
1912 /* stack: (L1 op L2) (H1 op H2) */
1920 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1921 t
= vtop
[-1].type
.t
;
1925 /* stack: L H shift */
1927 /* constant: simpler */
1928 /* NOTE: all comments are for SHL. the other cases are
1929 done by swapping words */
1940 if (op
!= TOK_SAR
) {
1973 /* XXX: should provide a faster fallback on x86 ? */
1976 func
= TOK___ashrdi3
;
1979 func
= TOK___lshrdi3
;
1982 func
= TOK___ashldi3
;
1988 /* compare operations */
1994 /* stack: L1 H1 L2 H2 */
1996 vtop
[-1] = vtop
[-2];
1998 /* stack: L1 L2 H1 H2 */
2002 /* when values are equal, we need to compare low words. since
2003 the jump is inverted, we invert the test too. */
2006 else if (op1
== TOK_GT
)
2008 else if (op1
== TOK_ULT
)
2010 else if (op1
== TOK_UGT
)
2020 /* generate non equal test */
2022 vset_VT_CMP(TOK_NE
);
2026 /* compare low. Always unsigned */
2030 else if (op1
== TOK_LE
)
2032 else if (op1
== TOK_GT
)
2034 else if (op1
== TOK_GE
)
2037 #if 0//def TCC_TARGET_I386
2038 if (op
== TOK_NE
) { gsym(b
); break; }
2039 if (op
== TOK_EQ
) { gsym(a
); break; }
2048 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
2050 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
2051 return (a
^ b
) >> 63 ? -x
: x
;
2054 static int gen_opic_lt(uint64_t a
, uint64_t b
)
2056 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
2059 /* handle integer constant optimizations and various machine
2061 static void gen_opic(int op
)
2063 SValue
*v1
= vtop
- 1;
2065 int t1
= v1
->type
.t
& VT_BTYPE
;
2066 int t2
= v2
->type
.t
& VT_BTYPE
;
2067 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2068 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2069 uint64_t l1
= c1
? v1
->c
.i
: 0;
2070 uint64_t l2
= c2
? v2
->c
.i
: 0;
2071 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
2073 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2074 l1
= ((uint32_t)l1
|
2075 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2076 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
2077 l2
= ((uint32_t)l2
|
2078 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
2082 case '+': l1
+= l2
; break;
2083 case '-': l1
-= l2
; break;
2084 case '&': l1
&= l2
; break;
2085 case '^': l1
^= l2
; break;
2086 case '|': l1
|= l2
; break;
2087 case '*': l1
*= l2
; break;
2094 /* if division by zero, generate explicit division */
2097 tcc_error("division by zero in constant");
2101 default: l1
= gen_opic_sdiv(l1
, l2
); break;
2102 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
2103 case TOK_UDIV
: l1
= l1
/ l2
; break;
2104 case TOK_UMOD
: l1
= l1
% l2
; break;
2107 case TOK_SHL
: l1
<<= (l2
& shm
); break;
2108 case TOK_SHR
: l1
>>= (l2
& shm
); break;
2110 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
2113 case TOK_ULT
: l1
= l1
< l2
; break;
2114 case TOK_UGE
: l1
= l1
>= l2
; break;
2115 case TOK_EQ
: l1
= l1
== l2
; break;
2116 case TOK_NE
: l1
= l1
!= l2
; break;
2117 case TOK_ULE
: l1
= l1
<= l2
; break;
2118 case TOK_UGT
: l1
= l1
> l2
; break;
2119 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
2120 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
2121 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
2122 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
2124 case TOK_LAND
: l1
= l1
&& l2
; break;
2125 case TOK_LOR
: l1
= l1
|| l2
; break;
2129 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2130 l1
= ((uint32_t)l1
|
2131 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2135 /* if commutative ops, put c2 as constant */
2136 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
2137 op
== '|' || op
== '*')) {
2139 c2
= c1
; //c = c1, c1 = c2, c2 = c;
2140 l2
= l1
; //l = l1, l1 = l2, l2 = l;
2142 if (!const_wanted
&&
2144 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
2145 (l1
== -1 && op
== TOK_SAR
))) {
2146 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
2148 } else if (!const_wanted
&&
2149 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
2151 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
2152 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
2153 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
2158 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
2161 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
2162 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
2165 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
2166 /* filter out NOP operations like x*1, x-0, x&-1... */
2168 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
2169 /* try to use shifts instead of muls or divs */
2170 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
2179 else if (op
== TOK_PDIV
)
2185 } else if (c2
&& (op
== '+' || op
== '-') &&
2186 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
2187 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
2188 /* symbol + constant case */
2192 /* The backends can't always deal with addends to symbols
2193 larger than +-1<<31. Don't construct such. */
2200 /* call low level op generator */
2201 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
2202 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
2210 /* generate a floating point operation with constant propagation */
2211 static void gen_opif(int op
)
2215 #if defined _MSC_VER && defined __x86_64__
2216 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
2223 /* currently, we cannot do computations with forward symbols */
2224 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2225 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2227 if (v1
->type
.t
== VT_FLOAT
) {
2230 } else if (v1
->type
.t
== VT_DOUBLE
) {
2238 /* NOTE: we only do constant propagation if finite number (not
2239 NaN or infinity) (ANSI spec) */
2240 if (!ieee_finite(f1
) || !ieee_finite(f2
))
2244 case '+': f1
+= f2
; break;
2245 case '-': f1
-= f2
; break;
2246 case '*': f1
*= f2
; break;
2249 /* If not in initializer we need to potentially generate
2250 FP exceptions at runtime, otherwise we want to fold. */
2256 /* XXX: also handles tests ? */
2260 /* XXX: overflow test ? */
2261 if (v1
->type
.t
== VT_FLOAT
) {
2263 } else if (v1
->type
.t
== VT_DOUBLE
) {
2275 static int pointed_size(CType
*type
)
2278 return type_size(pointed_type(type
), &align
);
2281 static void vla_runtime_pointed_size(CType
*type
)
2284 vla_runtime_type_size(pointed_type(type
), &align
);
2287 static inline int is_null_pointer(SValue
*p
)
2289 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2291 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2292 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2293 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2294 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0) &&
2295 ((pointed_type(&p
->type
)->t
& VT_BTYPE
) == VT_VOID
) &&
2296 0 == (pointed_type(&p
->type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
2299 static inline int is_integer_btype(int bt
)
2301 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2302 bt
== VT_INT
|| bt
== VT_LLONG
);
2305 /* check types for comparison or subtraction of pointers */
2306 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2308 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2311 /* null pointers are accepted for all comparisons as gcc */
2312 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2316 bt1
= type1
->t
& VT_BTYPE
;
2317 bt2
= type2
->t
& VT_BTYPE
;
2318 /* accept comparison between pointer and integer with a warning */
2319 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2320 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2321 tcc_warning("comparison between pointer and integer");
2325 /* both must be pointers or implicit function pointers */
2326 if (bt1
== VT_PTR
) {
2327 type1
= pointed_type(type1
);
2328 } else if (bt1
!= VT_FUNC
)
2329 goto invalid_operands
;
2331 if (bt2
== VT_PTR
) {
2332 type2
= pointed_type(type2
);
2333 } else if (bt2
!= VT_FUNC
) {
2335 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2337 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2338 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2342 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2343 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2344 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2345 /* gcc-like error if '-' is used */
2347 goto invalid_operands
;
2349 tcc_warning("comparison of distinct pointer types lacks a cast");
2353 /* generic gen_op: handles types problems */
2354 ST_FUNC
void gen_op(int op
)
2356 int u
, t1
, t2
, bt1
, bt2
, t
;
2360 t1
= vtop
[-1].type
.t
;
2361 t2
= vtop
[0].type
.t
;
2362 bt1
= t1
& VT_BTYPE
;
2363 bt2
= t2
& VT_BTYPE
;
2365 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2366 tcc_error("operation on a struct");
2367 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2368 if (bt2
== VT_FUNC
) {
2369 mk_pointer(&vtop
->type
);
2372 if (bt1
== VT_FUNC
) {
2374 mk_pointer(&vtop
->type
);
2379 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2380 /* at least one operand is a pointer */
2381 /* relational op: must be both pointers */
2382 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2383 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2384 /* pointers are handled are unsigned */
2386 t
= VT_LLONG
| VT_UNSIGNED
;
2388 t
= VT_INT
| VT_UNSIGNED
;
2392 /* if both pointers, then it must be the '-' op */
2393 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2395 tcc_error("cannot use pointers here");
2396 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2397 /* XXX: check that types are compatible */
2398 if (vtop
[-1].type
.t
& VT_VLA
) {
2399 vla_runtime_pointed_size(&vtop
[-1].type
);
2401 vpushi(pointed_size(&vtop
[-1].type
));
2405 vtop
->type
.t
= ptrdiff_type
.t
;
2409 /* exactly one pointer : must be '+' or '-'. */
2410 if (op
!= '-' && op
!= '+')
2411 tcc_error("cannot use pointers here");
2412 /* Put pointer as first operand */
2413 if (bt2
== VT_PTR
) {
2415 t
= t1
, t1
= t2
, t2
= t
;
2418 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2419 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2422 type1
= vtop
[-1].type
;
2423 type1
.t
&= ~VT_ARRAY
;
2424 if (vtop
[-1].type
.t
& VT_VLA
)
2425 vla_runtime_pointed_size(&vtop
[-1].type
);
2427 u
= pointed_size(&vtop
[-1].type
);
2429 tcc_error("unknown array element size");
2433 /* XXX: cast to int ? (long long case) */
2439 /* #ifdef CONFIG_TCC_BCHECK
2440 The main reason to removing this code:
2447 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2448 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2450 When this code is on. then the output looks like
2452 v+(i-j) = 0xbff84000
2454 /* if evaluating constant expression, no code should be
2455 generated, so no bound check */
2456 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2457 /* if bounded pointers, we generate a special code to
2464 gen_bounded_ptr_add();
2470 /* put again type if gen_opic() swaped operands */
2473 } else if (is_float(bt1
) || is_float(bt2
)) {
2474 /* compute bigger type and do implicit casts */
2475 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2477 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2482 /* floats can only be used for a few operations */
2483 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2484 (op
< TOK_ULT
|| op
> TOK_GT
))
2485 tcc_error("invalid operands for binary operation");
2487 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2488 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2489 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2491 t
|= (VT_LONG
& t1
);
2493 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2494 /* cast to biggest op */
2495 t
= VT_LLONG
| VT_LONG
;
2496 if (bt1
== VT_LLONG
)
2498 if (bt2
== VT_LLONG
)
2500 /* convert to unsigned if it does not fit in a long long */
2501 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2502 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2506 /* integer operations */
2507 t
= VT_INT
| (VT_LONG
& (t1
| t2
));
2508 /* convert to unsigned if it does not fit in an integer */
2509 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2510 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2513 /* XXX: currently, some unsigned operations are explicit, so
2514 we modify them here */
2515 if (t
& VT_UNSIGNED
) {
2522 else if (op
== TOK_LT
)
2524 else if (op
== TOK_GT
)
2526 else if (op
== TOK_LE
)
2528 else if (op
== TOK_GE
)
2536 /* special case for shifts and long long: we keep the shift as
2538 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2545 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2546 /* relational op: the result is an int */
2547 vtop
->type
.t
= VT_INT
;
2552 // Make sure that we have converted to an rvalue:
2553 if (vtop
->r
& VT_LVAL
)
2554 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2557 #ifndef TCC_TARGET_ARM
2558 /* generic itof for unsigned long long case */
2559 static void gen_cvt_itof1(int t
)
2561 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
2564 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2565 (VT_LLONG
| VT_UNSIGNED
)) {
2568 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2569 #if LDOUBLE_SIZE != 8
2570 else if (t
== VT_LDOUBLE
)
2571 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2574 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2578 vtop
->r
= reg_fret(t
);
2586 /* generic ftoi for unsigned long long case */
2587 static void gen_cvt_ftoi1(int t
)
2589 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
2594 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2595 /* not handled natively */
2596 st
= vtop
->type
.t
& VT_BTYPE
;
2598 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2599 #if LDOUBLE_SIZE != 8
2600 else if (st
== VT_LDOUBLE
)
2601 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2604 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2609 vtop
->r2
= REG_LRET
;
2616 /* force char or short cast */
2617 static void force_charshort_cast(int t
)
2621 /* cannot cast static initializers */
2622 if (STATIC_DATA_WANTED
)
2626 /* XXX: add optimization if lvalue : just change type and offset */
2631 if (t
& VT_UNSIGNED
) {
2632 vpushi((1 << bits
) - 1);
2635 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2641 /* result must be signed or the SAR is converted to an SHL
2642 This was not the case when "t" was a signed short
2643 and the last value on the stack was an unsigned int */
2644 vtop
->type
.t
&= ~VT_UNSIGNED
;
2650 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2651 static void gen_cast_s(int t
)
2659 static void gen_cast(CType
*type
)
2661 int sbt
, dbt
, sf
, df
, c
, p
;
2663 /* special delayed cast for char/short */
2664 /* XXX: in some cases (multiple cascaded casts), it may still
2666 if (vtop
->r
& VT_MUSTCAST
) {
2667 vtop
->r
&= ~VT_MUSTCAST
;
2668 force_charshort_cast(vtop
->type
.t
);
2671 /* bitfields first get cast to ints */
2672 if (vtop
->type
.t
& VT_BITFIELD
) {
2676 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2677 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2682 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2683 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2684 #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
2685 c
&= dbt
!= VT_LDOUBLE
;
2688 /* constant case: we can do it now */
2689 /* XXX: in ISOC, cannot do it if error in convert */
2690 if (sbt
== VT_FLOAT
)
2691 vtop
->c
.ld
= vtop
->c
.f
;
2692 else if (sbt
== VT_DOUBLE
)
2693 vtop
->c
.ld
= vtop
->c
.d
;
2696 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2697 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2698 vtop
->c
.ld
= vtop
->c
.i
;
2700 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2702 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2703 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2705 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2708 if (dbt
== VT_FLOAT
)
2709 vtop
->c
.f
= (float)vtop
->c
.ld
;
2710 else if (dbt
== VT_DOUBLE
)
2711 vtop
->c
.d
= (double)vtop
->c
.ld
;
2712 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2713 vtop
->c
.i
= vtop
->c
.ld
;
2714 } else if (sf
&& dbt
== VT_BOOL
) {
2715 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2718 vtop
->c
.i
= vtop
->c
.ld
;
2719 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2721 else if (sbt
& VT_UNSIGNED
)
2722 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2724 else if (sbt
== VT_PTR
)
2727 else if (sbt
!= VT_LLONG
)
2728 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2729 -(vtop
->c
.i
& 0x80000000));
2731 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2733 else if (dbt
== VT_BOOL
)
2734 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2736 else if (dbt
== VT_PTR
)
2739 else if (dbt
!= VT_LLONG
) {
2740 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2741 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2744 if (!(dbt
& VT_UNSIGNED
))
2745 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2748 } else if (p
&& dbt
== VT_BOOL
) {
2752 /* non constant case: generate code */
2754 /* convert from fp to fp */
2757 /* convert int to fp */
2760 /* convert fp to int */
2761 if (dbt
== VT_BOOL
) {
2765 /* we handle char/short/etc... with generic code */
2766 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2767 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2771 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2772 /* additional cast for char/short... */
2778 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2779 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2780 /* scalar to long long */
2781 /* machine independent conversion */
2783 /* generate high word */
2784 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2788 if (sbt
== VT_PTR
) {
2789 /* cast from pointer to int before we apply
2790 shift operation, which pointers don't support*/
2797 /* patch second register */
2798 vtop
[-1].r2
= vtop
->r
;
2802 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2803 (dbt
& VT_BTYPE
) == VT_PTR
||
2804 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2805 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2806 (sbt
& VT_BTYPE
) != VT_PTR
&&
2807 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2808 /* need to convert from 32bit to 64bit */
2810 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2811 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_RISCV64)
2813 #elif defined(TCC_TARGET_X86_64)
2815 /* x86_64 specific: movslq */
2817 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2824 } else if (dbt
== VT_BOOL
) {
2825 /* scalar to bool */
2828 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2829 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2830 if (sbt
== VT_PTR
) {
2831 vtop
->type
.t
= VT_INT
;
2832 tcc_warning("nonportable conversion from pointer to char/short");
2834 force_charshort_cast(dbt
);
2835 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2837 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2839 /* from long long: just take low order word */
2843 /* XXX some architectures (e.g. risc-v) would like it
2844 better for this merely being a 32-to-64 sign or zero-
2847 vtop
->type
.t
|= VT_UNSIGNED
;
2851 /* if lvalue and single word type, nothing to do because
2852 the lvalue already contains the real type size (see
2853 VT_LVAL_xxx constants) */
2856 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2857 /* if we are casting between pointer types,
2858 we must update the VT_LVAL_xxx size */
2859 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2860 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2863 vtop
->type
.t
&= ~ ( VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
2866 /* return type size as known at compile time. Put alignment at 'a' */
2867 ST_FUNC
int type_size(CType
*type
, int *a
)
2872 bt
= type
->t
& VT_BTYPE
;
2873 if (bt
== VT_STRUCT
) {
2878 } else if (bt
== VT_PTR
) {
2879 if (type
->t
& VT_ARRAY
) {
2883 ts
= type_size(&s
->type
, a
);
2885 if (ts
< 0 && s
->c
< 0)
2893 } else if (IS_ENUM(type
->t
) && type
->ref
->c
< 0) {
2894 return -1; /* incomplete enum */
2895 } else if (bt
== VT_LDOUBLE
) {
2897 return LDOUBLE_SIZE
;
2898 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2899 #ifdef TCC_TARGET_I386
2900 #ifdef TCC_TARGET_PE
2905 #elif defined(TCC_TARGET_ARM)
2915 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2918 } else if (bt
== VT_SHORT
) {
2921 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2925 /* char, void, function, _Bool */
2931 /* push type size as known at runtime time on top of value stack. Put
2933 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2935 if (type
->t
& VT_VLA
) {
2936 type_size(&type
->ref
->type
, a
);
2937 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2939 vpushi(type_size(type
, a
));
2943 /* return the pointed type of t */
2944 static inline CType
*pointed_type(CType
*type
)
2946 return &type
->ref
->type
;
2949 /* modify type so that its it is a pointer to type. */
2950 ST_FUNC
void mk_pointer(CType
*type
)
2953 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2954 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2958 /* compare function types. OLD functions match any new functions */
2959 static int is_compatible_func(CType
*type1
, CType
*type2
)
2965 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2967 if (s1
->f
.func_type
!= s2
->f
.func_type
2968 && s1
->f
.func_type
!= FUNC_OLD
2969 && s2
->f
.func_type
!= FUNC_OLD
)
2971 /* we should check the function return type for FUNC_OLD too
2972 but that causes problems with the internally used support
2973 functions such as TOK_memmove */
2974 if (s1
->f
.func_type
== FUNC_OLD
&& !s1
->next
)
2976 if (s2
->f
.func_type
== FUNC_OLD
&& !s2
->next
)
2979 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2990 /* return true if type1 and type2 are the same. If unqualified is
2991 true, qualifiers on the types are ignored.
2993 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2997 t1
= type1
->t
& VT_TYPE
;
2998 t2
= type2
->t
& VT_TYPE
;
3000 /* strip qualifiers before comparing */
3001 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3002 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3005 /* Default Vs explicit signedness only matters for char */
3006 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
3010 /* XXX: bitfields ? */
3015 && !(type1
->ref
->c
< 0
3016 || type2
->ref
->c
< 0
3017 || type1
->ref
->c
== type2
->ref
->c
))
3020 /* test more complicated cases */
3021 bt1
= t1
& VT_BTYPE
;
3022 if (bt1
== VT_PTR
) {
3023 type1
= pointed_type(type1
);
3024 type2
= pointed_type(type2
);
3025 return is_compatible_types(type1
, type2
);
3026 } else if (bt1
== VT_STRUCT
) {
3027 return (type1
->ref
== type2
->ref
);
3028 } else if (bt1
== VT_FUNC
) {
3029 return is_compatible_func(type1
, type2
);
3030 } else if (IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)) {
3031 return type1
->ref
== type2
->ref
;
3037 /* return true if type1 and type2 are exactly the same (including
3040 static int is_compatible_types(CType
*type1
, CType
*type2
)
3042 return compare_types(type1
,type2
,0);
3045 /* return true if type1 and type2 are the same (ignoring qualifiers).
3047 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
3049 return compare_types(type1
,type2
,1);
3052 /* print a type. If 'varstr' is not NULL, then the variable is also
3053 printed in the type */
3055 /* XXX: add array and function pointers */
3056 static void type_to_str(char *buf
, int buf_size
,
3057 CType
*type
, const char *varstr
)
3069 pstrcat(buf
, buf_size
, "extern ");
3071 pstrcat(buf
, buf_size
, "static ");
3073 pstrcat(buf
, buf_size
, "typedef ");
3075 pstrcat(buf
, buf_size
, "inline ");
3076 if (t
& VT_VOLATILE
)
3077 pstrcat(buf
, buf_size
, "volatile ");
3078 if (t
& VT_CONSTANT
)
3079 pstrcat(buf
, buf_size
, "const ");
3081 if (((t
& VT_DEFSIGN
) && bt
== VT_BYTE
)
3082 || ((t
& VT_UNSIGNED
)
3083 && (bt
== VT_SHORT
|| bt
== VT_INT
|| bt
== VT_LLONG
)
3086 pstrcat(buf
, buf_size
, (t
& VT_UNSIGNED
) ? "unsigned " : "signed ");
3088 buf_size
-= strlen(buf
);
3123 tstr
= "long double";
3125 pstrcat(buf
, buf_size
, tstr
);
3132 pstrcat(buf
, buf_size
, tstr
);
3133 v
= type
->ref
->v
& ~SYM_STRUCT
;
3134 if (v
>= SYM_FIRST_ANOM
)
3135 pstrcat(buf
, buf_size
, "<anonymous>");
3137 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
3142 if (varstr
&& '*' == *varstr
) {
3143 pstrcat(buf1
, sizeof(buf1
), "(");
3144 pstrcat(buf1
, sizeof(buf1
), varstr
);
3145 pstrcat(buf1
, sizeof(buf1
), ")");
3147 pstrcat(buf1
, buf_size
, "(");
3149 while (sa
!= NULL
) {
3151 type_to_str(buf2
, sizeof(buf2
), &sa
->type
, NULL
);
3152 pstrcat(buf1
, sizeof(buf1
), buf2
);
3155 pstrcat(buf1
, sizeof(buf1
), ", ");
3157 if (s
->f
.func_type
== FUNC_ELLIPSIS
)
3158 pstrcat(buf1
, sizeof(buf1
), ", ...");
3159 pstrcat(buf1
, sizeof(buf1
), ")");
3160 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3165 if (varstr
&& '*' == *varstr
)
3166 snprintf(buf1
, sizeof(buf1
), "(%s)[%d]", varstr
, s
->c
);
3168 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
3169 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3172 pstrcpy(buf1
, sizeof(buf1
), "*");
3173 if (t
& VT_CONSTANT
)
3174 pstrcat(buf1
, buf_size
, "const ");
3175 if (t
& VT_VOLATILE
)
3176 pstrcat(buf1
, buf_size
, "volatile ");
3178 pstrcat(buf1
, sizeof(buf1
), varstr
);
3179 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3183 pstrcat(buf
, buf_size
, " ");
3184 pstrcat(buf
, buf_size
, varstr
);
3189 /* verify type compatibility to store vtop in 'dt' type, and generate
3191 static void gen_assign_cast(CType
*dt
)
3193 CType
*st
, *type1
, *type2
;
3194 char buf1
[256], buf2
[256];
3195 int dbt
, sbt
, qualwarn
, lvl
;
3197 st
= &vtop
->type
; /* source type */
3198 dbt
= dt
->t
& VT_BTYPE
;
3199 sbt
= st
->t
& VT_BTYPE
;
3200 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
3201 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
3202 ; /* It is Ok if both are void */
3204 tcc_error("cannot cast from/to void");
3206 if (dt
->t
& VT_CONSTANT
)
3207 tcc_warning("assignment of read-only location");
3210 /* special cases for pointers */
3211 /* '0' can also be a pointer */
3212 if (is_null_pointer(vtop
))
3214 /* accept implicit pointer to integer cast with warning */
3215 if (is_integer_btype(sbt
)) {
3216 tcc_warning("assignment makes pointer from integer without a cast");
3219 type1
= pointed_type(dt
);
3221 type2
= pointed_type(st
);
3222 else if (sbt
== VT_FUNC
)
3223 type2
= st
; /* a function is implicitly a function pointer */
3226 if (is_compatible_types(type1
, type2
))
3228 for (qualwarn
= lvl
= 0;; ++lvl
) {
3229 if (((type2
->t
& VT_CONSTANT
) && !(type1
->t
& VT_CONSTANT
)) ||
3230 ((type2
->t
& VT_VOLATILE
) && !(type1
->t
& VT_VOLATILE
)))
3232 dbt
= type1
->t
& (VT_BTYPE
|VT_LONG
);
3233 sbt
= type2
->t
& (VT_BTYPE
|VT_LONG
);
3234 if (dbt
!= VT_PTR
|| sbt
!= VT_PTR
)
3236 type1
= pointed_type(type1
);
3237 type2
= pointed_type(type2
);
3239 if (!is_compatible_unqualified_types(type1
, type2
)) {
3240 if ((dbt
== VT_VOID
|| sbt
== VT_VOID
) && lvl
== 0) {
3241 /* void * can match anything */
3242 } else if (dbt
== sbt
3243 && is_integer_btype(sbt
& VT_BTYPE
)
3244 && IS_ENUM(type1
->t
) + IS_ENUM(type2
->t
)
3245 + !!((type1
->t
^ type2
->t
) & VT_UNSIGNED
) < 2) {
3246 /* Like GCC don't warn by default for merely changes
3247 in pointer target signedness. Do warn for different
3248 base types, though, in particular for unsigned enums
3249 and signed int targets. */
3251 tcc_warning("assignment from incompatible pointer type");
3256 tcc_warning("assignment discards qualifiers from pointer target type");
3262 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
3263 tcc_warning("assignment makes integer from pointer without a cast");
3264 } else if (sbt
== VT_STRUCT
) {
3265 goto case_VT_STRUCT
;
3267 /* XXX: more tests */
3271 if (!is_compatible_unqualified_types(dt
, st
)) {
3273 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
3274 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
3275 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
3282 /* store vtop in lvalue pushed on stack */
3283 ST_FUNC
void vstore(void)
3285 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
3287 ft
= vtop
[-1].type
.t
;
3288 sbt
= vtop
->type
.t
& VT_BTYPE
;
3289 dbt
= ft
& VT_BTYPE
;
3290 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
3291 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
3292 && !(vtop
->type
.t
& VT_BITFIELD
)) {
3293 /* optimize char/short casts */
3294 delayed_cast
= VT_MUSTCAST
;
3295 vtop
->type
.t
= ft
& VT_TYPE
;
3296 /* XXX: factorize */
3297 if (ft
& VT_CONSTANT
)
3298 tcc_warning("assignment of read-only location");
3301 if (!(ft
& VT_BITFIELD
))
3302 gen_assign_cast(&vtop
[-1].type
);
3305 if (sbt
== VT_STRUCT
) {
3306 /* if structure, only generate pointer */
3307 /* structure assignment : generate memcpy */
3308 /* XXX: optimize if small size */
3309 size
= type_size(&vtop
->type
, &align
);
3313 vtop
->type
.t
= VT_PTR
;
3316 /* address of memcpy() */
3319 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3320 else if(!(align
& 3))
3321 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3324 /* Use memmove, rather than memcpy, as dest and src may be same: */
3325 vpush_global_sym(&func_old_type
, TOK_memmove
);
3330 vtop
->type
.t
= VT_PTR
;
3336 /* leave source on stack */
3337 } else if (ft
& VT_BITFIELD
) {
3338 /* bitfield store handling */
3340 /* save lvalue as expression result (example: s.b = s.a = n;) */
3341 vdup(), vtop
[-1] = vtop
[-2];
3343 bit_pos
= BIT_POS(ft
);
3344 bit_size
= BIT_SIZE(ft
);
3345 /* remove bit field info to avoid loops */
3346 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3348 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3349 gen_cast(&vtop
[-1].type
);
3350 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3353 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3354 if (r
== VT_STRUCT
) {
3355 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG
? VT_LLONG
: VT_INT
);
3356 store_packed_bf(bit_pos
, bit_size
);
3358 unsigned long long mask
= (1ULL << bit_size
) - 1;
3359 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3361 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3364 vpushi((unsigned)mask
);
3371 /* duplicate destination */
3374 /* load destination, mask and or with source */
3375 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3376 vpushll(~(mask
<< bit_pos
));
3378 vpushi(~((unsigned)mask
<< bit_pos
));
3383 /* ... and discard */
3386 } else if (dbt
== VT_VOID
) {
3389 #ifdef CONFIG_TCC_BCHECK
3390 /* bound check case */
3391 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3400 #ifdef TCC_TARGET_X86_64
3401 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3403 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3408 r
= gv(rc
); /* generate value */
3409 /* if lvalue was saved on stack, must read it */
3410 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3412 t
= get_reg(RC_INT
);
3418 sv
.r
= VT_LOCAL
| VT_LVAL
;
3419 sv
.c
.i
= vtop
[-1].c
.i
;
3421 vtop
[-1].r
= t
| VT_LVAL
;
3423 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3425 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3426 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3428 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3429 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3431 vtop
[-1].type
.t
= load_type
;
3434 /* convert to int to increment easily */
3435 vtop
->type
.t
= addr_type
;
3441 vtop
[-1].type
.t
= load_type
;
3442 /* XXX: it works because r2 is spilled last ! */
3443 store(vtop
->r2
, vtop
- 1);
3449 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3450 vtop
->r
|= delayed_cast
;
3454 /* post defines POST/PRE add. c is the token ++ or -- */
3455 ST_FUNC
void inc(int post
, int c
)
3458 vdup(); /* save lvalue */
3460 gv_dup(); /* duplicate value */
3465 vpushi(c
- TOK_MID
);
3467 vstore(); /* store value */
3469 vpop(); /* if post op, return saved value */
3472 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3474 /* read the string */
3478 while (tok
== TOK_STR
) {
3479 /* XXX: add \0 handling too ? */
3480 cstr_cat(astr
, tokc
.str
.data
, -1);
3483 cstr_ccat(astr
, '\0');
3486 /* If I is >= 1 and a power of two, returns log2(i)+1.
3487 If I is 0 returns 0. */
3488 static int exact_log2p1(int i
)
3493 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3504 /* Parse __attribute__((...)) GNUC extension. */
3505 static void parse_attribute(AttributeDef
*ad
)
3511 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3516 while (tok
!= ')') {
3517 if (tok
< TOK_IDENT
)
3518 expect("attribute name");
3530 tcc_warning("implicit declaration of function '%s'",
3531 get_tok_str(tok
, &tokc
));
3532 s
= external_global_sym(tok
, &func_old_type
);
3534 ad
->cleanup_func
= s
;
3542 parse_mult_str(&astr
, "section name");
3543 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3550 parse_mult_str(&astr
, "alias(\"target\")");
3551 ad
->alias_target
= /* save string as token, for later */
3552 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3556 case TOK_VISIBILITY1
:
3557 case TOK_VISIBILITY2
:
3559 parse_mult_str(&astr
,
3560 "visibility(\"default|hidden|internal|protected\")");
3561 if (!strcmp (astr
.data
, "default"))
3562 ad
->a
.visibility
= STV_DEFAULT
;
3563 else if (!strcmp (astr
.data
, "hidden"))
3564 ad
->a
.visibility
= STV_HIDDEN
;
3565 else if (!strcmp (astr
.data
, "internal"))
3566 ad
->a
.visibility
= STV_INTERNAL
;
3567 else if (!strcmp (astr
.data
, "protected"))
3568 ad
->a
.visibility
= STV_PROTECTED
;
3570 expect("visibility(\"default|hidden|internal|protected\")");
3579 if (n
<= 0 || (n
& (n
- 1)) != 0)
3580 tcc_error("alignment must be a positive power of two");
3585 ad
->a
.aligned
= exact_log2p1(n
);
3586 if (n
!= 1 << (ad
->a
.aligned
- 1))
3587 tcc_error("alignment of %d is larger than implemented", n
);
3599 /* currently, no need to handle it because tcc does not
3600 track unused objects */
3604 ad
->f
.func_noreturn
= 1;
3609 ad
->f
.func_call
= FUNC_CDECL
;
3614 ad
->f
.func_call
= FUNC_STDCALL
;
3616 #ifdef TCC_TARGET_I386
3626 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3632 ad
->f
.func_call
= FUNC_FASTCALLW
;
3639 ad
->attr_mode
= VT_LLONG
+ 1;
3642 ad
->attr_mode
= VT_BYTE
+ 1;
3645 ad
->attr_mode
= VT_SHORT
+ 1;
3649 ad
->attr_mode
= VT_INT
+ 1;
3652 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3659 ad
->a
.dllexport
= 1;
3661 case TOK_NODECORATE
:
3662 ad
->a
.nodecorate
= 1;
3665 ad
->a
.dllimport
= 1;
3668 if (tcc_state
->warn_unsupported
)
3669 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3670 /* skip parameters */
3672 int parenthesis
= 0;
3676 else if (tok
== ')')
3679 } while (parenthesis
&& tok
!= -1);
3692 static Sym
* find_field (CType
*type
, int v
, int *cumofs
)
3696 while ((s
= s
->next
) != NULL
) {
3697 if ((s
->v
& SYM_FIELD
) &&
3698 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3699 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3700 Sym
*ret
= find_field (&s
->type
, v
, cumofs
);
3712 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3714 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3715 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3716 int pcc
= !tcc_state
->ms_bitfields
;
3717 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3724 prevbt
= VT_STRUCT
; /* make it never match */
3729 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3730 if (f
->type
.t
& VT_BITFIELD
)
3731 bit_size
= BIT_SIZE(f
->type
.t
);
3734 size
= type_size(&f
->type
, &align
);
3735 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3738 if (pcc
&& bit_size
== 0) {
3739 /* in pcc mode, packing does not affect zero-width bitfields */
3742 /* in pcc mode, attribute packed overrides if set. */
3743 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3746 /* pragma pack overrides align if lesser and packs bitfields always */
3749 if (pragma_pack
< align
)
3750 align
= pragma_pack
;
3751 /* in pcc mode pragma pack also overrides individual align */
3752 if (pcc
&& pragma_pack
< a
)
3756 /* some individual align was specified */
3760 if (type
->ref
->type
.t
== VT_UNION
) {
3761 if (pcc
&& bit_size
>= 0)
3762 size
= (bit_size
+ 7) >> 3;
3767 } else if (bit_size
< 0) {
3769 c
+= (bit_pos
+ 7) >> 3;
3770 c
= (c
+ align
- 1) & -align
;
3779 /* A bit-field. Layout is more complicated. There are two
3780 options: PCC (GCC) compatible and MS compatible */
3782 /* In PCC layout a bit-field is placed adjacent to the
3783 preceding bit-fields, except if:
3785 - an individual alignment was given
3786 - it would overflow its base type container and
3787 there is no packing */
3788 if (bit_size
== 0) {
3790 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3792 } else if (f
->a
.aligned
) {
3794 } else if (!packed
) {
3796 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3797 if (ofs
> size
/ align
)
3801 /* in pcc mode, long long bitfields have type int if they fit */
3802 if (size
== 8 && bit_size
<= 32)
3803 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3805 while (bit_pos
>= align
* 8)
3806 c
+= align
, bit_pos
-= align
* 8;
3809 /* In PCC layout named bit-fields influence the alignment
3810 of the containing struct using the base types alignment,
3811 except for packed fields (which here have correct align). */
3812 if (f
->v
& SYM_FIRST_ANOM
3813 // && bit_size // ??? gcc on ARM/rpi does that
3818 bt
= f
->type
.t
& VT_BTYPE
;
3819 if ((bit_pos
+ bit_size
> size
* 8)
3820 || (bit_size
> 0) == (bt
!= prevbt
)
3822 c
= (c
+ align
- 1) & -align
;
3825 /* In MS bitfield mode a bit-field run always uses
3826 at least as many bits as the underlying type.
3827 To start a new run it's also required that this
3828 or the last bit-field had non-zero width. */
3829 if (bit_size
|| prev_bit_size
)
3832 /* In MS layout the records alignment is normally
3833 influenced by the field, except for a zero-width
3834 field at the start of a run (but by further zero-width
3835 fields it is again). */
3836 if (bit_size
== 0 && prevbt
!= bt
)
3839 prev_bit_size
= bit_size
;
3842 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3843 | (bit_pos
<< VT_STRUCT_SHIFT
);
3844 bit_pos
+= bit_size
;
3846 if (align
> maxalign
)
3850 printf("set field %s offset %-2d size %-2d align %-2d",
3851 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3852 if (f
->type
.t
& VT_BITFIELD
) {
3853 printf(" pos %-2d bits %-2d",
3866 c
+= (bit_pos
+ 7) >> 3;
3868 /* store size and alignment */
3869 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3873 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
3874 /* can happen if individual align for some member was given. In
3875 this case MSVC ignores maxalign when aligning the size */
3880 c
= (c
+ a
- 1) & -a
;
3884 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3887 /* check whether we can access bitfields by their type */
3888 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3892 if (0 == (f
->type
.t
& VT_BITFIELD
))
3896 bit_size
= BIT_SIZE(f
->type
.t
);
3899 bit_pos
= BIT_POS(f
->type
.t
);
3900 size
= type_size(&f
->type
, &align
);
3901 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3904 /* try to access the field using a different type */
3905 c0
= -1, s
= align
= 1;
3907 px
= f
->c
* 8 + bit_pos
;
3908 cx
= (px
>> 3) & -align
;
3909 px
= px
- (cx
<< 3);
3912 s
= (px
+ bit_size
+ 7) >> 3;
3922 s
= type_size(&t
, &align
);
3926 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3927 /* update offset and bit position */
3930 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3931 | (bit_pos
<< VT_STRUCT_SHIFT
);
3935 printf("FIX field %s offset %-2d size %-2d align %-2d "
3936 "pos %-2d bits %-2d\n",
3937 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3938 cx
, s
, align
, px
, bit_size
);
3941 /* fall back to load/store single-byte wise */
3942 f
->auxtype
= VT_STRUCT
;
3944 printf("FIX field %s : load byte-wise\n",
3945 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3951 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3952 static void struct_decl(CType
*type
, int u
)
3954 int v
, c
, size
, align
, flexible
;
3955 int bit_size
, bsize
, bt
;
3957 AttributeDef ad
, ad1
;
3960 memset(&ad
, 0, sizeof ad
);
3962 parse_attribute(&ad
);
3966 /* struct already defined ? return it */
3968 expect("struct/union/enum name");
3970 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3973 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3975 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3980 /* Record the original enum/struct/union token. */
3981 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3983 /* we put an undefined size for struct/union */
3984 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3985 s
->r
= 0; /* default alignment is zero as gcc */
3987 type
->t
= s
->type
.t
;
3993 tcc_error("struct/union/enum already defined");
3995 /* cannot be empty */
3996 /* non empty enums are not allowed */
3999 long long ll
= 0, pl
= 0, nl
= 0;
4002 /* enum symbols have static storage */
4003 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
4007 expect("identifier");
4009 if (ss
&& !local_stack
)
4010 tcc_error("redefinition of enumerator '%s'",
4011 get_tok_str(v
, NULL
));
4015 ll
= expr_const64();
4017 ss
= sym_push(v
, &t
, VT_CONST
, 0);
4019 *ps
= ss
, ps
= &ss
->next
;
4028 /* NOTE: we accept a trailing comma */
4033 /* set integral type of the enum */
4036 if (pl
!= (unsigned)pl
)
4037 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4039 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
4040 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4041 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
4043 /* set type for enum members */
4044 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
4046 if (ll
== (int)ll
) /* default is int if it fits */
4048 if (t
.t
& VT_UNSIGNED
) {
4049 ss
->type
.t
|= VT_UNSIGNED
;
4050 if (ll
== (unsigned)ll
)
4053 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
)
4054 | (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4059 while (tok
!= '}') {
4060 if (!parse_btype(&btype
, &ad1
)) {
4066 tcc_error("flexible array member '%s' not at the end of struct",
4067 get_tok_str(v
, NULL
));
4073 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
4075 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
4076 expect("identifier");
4078 int v
= btype
.ref
->v
;
4079 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
4080 if (tcc_state
->ms_extensions
== 0)
4081 expect("identifier");
4085 if (type_size(&type1
, &align
) < 0) {
4086 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
4089 tcc_error("field '%s' has incomplete type",
4090 get_tok_str(v
, NULL
));
4092 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
4093 (type1
.t
& VT_BTYPE
) == VT_VOID
||
4094 (type1
.t
& VT_STORAGE
))
4095 tcc_error("invalid type for '%s'",
4096 get_tok_str(v
, NULL
));
4100 bit_size
= expr_const();
4101 /* XXX: handle v = 0 case for messages */
4103 tcc_error("negative width in bit-field '%s'",
4104 get_tok_str(v
, NULL
));
4105 if (v
&& bit_size
== 0)
4106 tcc_error("zero width for bit-field '%s'",
4107 get_tok_str(v
, NULL
));
4108 parse_attribute(&ad1
);
4110 size
= type_size(&type1
, &align
);
4111 if (bit_size
>= 0) {
4112 bt
= type1
.t
& VT_BTYPE
;
4118 tcc_error("bitfields must have scalar type");
4120 if (bit_size
> bsize
) {
4121 tcc_error("width of '%s' exceeds its type",
4122 get_tok_str(v
, NULL
));
4123 } else if (bit_size
== bsize
4124 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
4125 /* no need for bit fields */
4127 } else if (bit_size
== 64) {
4128 tcc_error("field width 64 not implemented");
4130 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
4132 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4135 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
4136 /* Remember we've seen a real field to check
4137 for placement of flexible array member. */
4140 /* If member is a struct or bit-field, enforce
4141 placing into the struct (as anonymous). */
4143 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
4148 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
4153 if (tok
== ';' || tok
== TOK_EOF
)
4160 parse_attribute(&ad
);
4161 struct_layout(type
, &ad
);
4166 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
4168 merge_symattr(&ad
->a
, &s
->a
);
4169 merge_funcattr(&ad
->f
, &s
->f
);
4172 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4173 are added to the element type, copied because it could be a typedef. */
4174 static void parse_btype_qualify(CType
*type
, int qualifiers
)
4176 while (type
->t
& VT_ARRAY
) {
4177 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
4178 type
= &type
->ref
->type
;
4180 type
->t
|= qualifiers
;
4183 /* return 0 if no type declaration. otherwise, return the basic type
4186 static int parse_btype(CType
*type
, AttributeDef
*ad
)
4188 int t
, u
, bt
, st
, type_found
, typespec_found
, g
, n
;
4192 memset(ad
, 0, sizeof(AttributeDef
));
4202 /* currently, we really ignore extension */
4212 if (u
== VT_SHORT
|| u
== VT_LONG
) {
4213 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
4214 tmbt
: tcc_error("too many basic types");
4217 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
4222 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4239 memset(&ad1
, 0, sizeof(AttributeDef
));
4240 if (parse_btype(&type1
, &ad1
)) {
4241 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4243 n
= 1 << (ad1
.a
.aligned
- 1);
4245 type_size(&type1
, &n
);
4248 if (n
<= 0 || (n
& (n
- 1)) != 0)
4249 tcc_error("alignment must be a positive power of two");
4252 ad
->a
.aligned
= exact_log2p1(n
);
4256 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4257 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4258 } else if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4259 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LLONG
;
4266 #ifdef TCC_TARGET_ARM64
4268 /* GCC's __uint128_t appears in some Linux header files. Make it a
4269 synonym for long double to get the size and alignment right. */
4280 if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4281 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4289 struct_decl(&type1
, VT_ENUM
);
4292 type
->ref
= type1
.ref
;
4295 struct_decl(&type1
, VT_STRUCT
);
4298 struct_decl(&type1
, VT_UNION
);
4301 /* type modifiers */
4306 parse_btype_qualify(type
, VT_CONSTANT
);
4314 parse_btype_qualify(type
, VT_VOLATILE
);
4321 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4322 tcc_error("signed and unsigned modifier");
4335 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4336 tcc_error("signed and unsigned modifier");
4337 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4353 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4354 tcc_error("multiple storage classes");
4365 /* currently, no need to handle it because tcc does not
4366 track unused objects */
4369 /* GNUC attribute */
4370 case TOK_ATTRIBUTE1
:
4371 case TOK_ATTRIBUTE2
:
4372 parse_attribute(ad
);
4373 if (ad
->attr_mode
) {
4374 u
= ad
->attr_mode
-1;
4375 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4383 parse_expr_type(&type1
);
4384 /* remove all storage modifiers except typedef */
4385 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4387 sym_to_attr(ad
, type1
.ref
);
4393 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4397 if (tok
== ':' && !in_generic
) {
4398 /* ignore if it's a label */
4403 t
&= ~(VT_BTYPE
|VT_LONG
);
4404 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4405 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4406 type
->ref
= s
->type
.ref
;
4408 parse_btype_qualify(type
, t
);
4410 /* get attributes from typedef */
4419 if (tcc_state
->char_is_unsigned
) {
4420 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4423 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4424 bt
= t
& (VT_BTYPE
|VT_LONG
);
4426 t
|= LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
;
4427 #ifdef TCC_TARGET_PE
4428 if (bt
== VT_LDOUBLE
)
4429 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_DOUBLE
;
4435 /* convert a function parameter type (array to pointer and function to
4436 function pointer) */
4437 static inline void convert_parameter_type(CType
*pt
)
4439 /* remove const and volatile qualifiers (XXX: const could be used
4440 to indicate a const function parameter */
4441 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4442 /* array must be transformed to pointer according to ANSI C */
4444 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4449 ST_FUNC
void parse_asm_str(CString
*astr
)
4452 parse_mult_str(astr
, "string constant");
4455 /* Parse an asm label and return the token */
4456 static int asm_label_instr(void)
4462 parse_asm_str(&astr
);
4465 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4467 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4472 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4474 int n
, l
, t1
, arg_size
, align
, unused_align
;
4475 Sym
**plast
, *s
, *first
;
4480 /* function type, or recursive declarator (return if so) */
4482 if (td
&& !(td
& TYPE_ABSTRACT
))
4486 else if (parse_btype(&pt
, &ad1
))
4489 merge_attr (ad
, &ad1
);
4498 /* read param name and compute offset */
4499 if (l
!= FUNC_OLD
) {
4500 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4502 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4503 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4504 tcc_error("parameter declared as void");
4508 expect("identifier");
4509 pt
.t
= VT_VOID
; /* invalid type */
4513 convert_parameter_type(&pt
);
4514 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4515 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4521 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4526 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4527 tcc_error("invalid type");
4530 /* if no parameters, then old type prototype */
4533 /* NOTE: const is ignored in returned type as it has a special
4534 meaning in gcc / C++ */
4535 type
->t
&= ~VT_CONSTANT
;
4536 /* some ancient pre-K&R C allows a function to return an array
4537 and the array brackets to be put after the arguments, such
4538 that "int c()[]" means something like "int[] c()" */
4541 skip(']'); /* only handle simple "[]" */
4544 /* we push a anonymous symbol which will contain the function prototype */
4545 ad
->f
.func_args
= arg_size
;
4546 ad
->f
.func_type
= l
;
4547 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4553 } else if (tok
== '[') {
4554 int saved_nocode_wanted
= nocode_wanted
;
4555 /* array definition */
4558 /* XXX The optional type-quals and static should only be accepted
4559 in parameter decls. The '*' as well, and then even only
4560 in prototypes (not function defs). */
4562 case TOK_RESTRICT1
: case TOK_RESTRICT2
: case TOK_RESTRICT3
:
4577 if (!local_stack
|| (storage
& VT_STATIC
))
4578 vpushi(expr_const());
4580 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4581 length must always be evaluated, even under nocode_wanted,
4582 so that its size slot is initialized (e.g. under sizeof
4587 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4590 tcc_error("invalid array size");
4592 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4593 tcc_error("size of variable length array should be an integer");
4599 /* parse next post type */
4600 post_type(type
, ad
, storage
, 0);
4602 if ((type
->t
& VT_BTYPE
) == VT_FUNC
)
4603 tcc_error("declaration of an array of functions");
4604 if ((type
->t
& VT_BTYPE
) == VT_VOID
4605 || type_size(type
, &unused_align
) < 0)
4606 tcc_error("declaration of an array of incomplete type elements");
4608 t1
|= type
->t
& VT_VLA
;
4612 tcc_error("need explicit inner array size in VLAs");
4613 loc
-= type_size(&int_type
, &align
);
4617 vla_runtime_type_size(type
, &align
);
4619 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4625 nocode_wanted
= saved_nocode_wanted
;
4627 /* we push an anonymous symbol which will contain the array
4629 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4630 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4636 /* Parse a type declarator (except basic type), and return the type
4637 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4638 expected. 'type' should contain the basic type. 'ad' is the
4639 attribute definition of the basic type. It can be modified by
4640 type_decl(). If this (possibly abstract) declarator is a pointer chain
4641 it returns the innermost pointed to type (equals *type, but is a different
4642 pointer), otherwise returns type itself, that's used for recursive calls. */
4643 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4646 int qualifiers
, storage
;
4648 /* recursive type, remove storage bits first, apply them later again */
4649 storage
= type
->t
& VT_STORAGE
;
4650 type
->t
&= ~VT_STORAGE
;
4653 while (tok
== '*') {
4661 qualifiers
|= VT_CONSTANT
;
4666 qualifiers
|= VT_VOLATILE
;
4672 /* XXX: clarify attribute handling */
4673 case TOK_ATTRIBUTE1
:
4674 case TOK_ATTRIBUTE2
:
4675 parse_attribute(ad
);
4679 type
->t
|= qualifiers
;
4681 /* innermost pointed to type is the one for the first derivation */
4682 ret
= pointed_type(type
);
4686 /* This is possibly a parameter type list for abstract declarators
4687 ('int ()'), use post_type for testing this. */
4688 if (!post_type(type
, ad
, 0, td
)) {
4689 /* It's not, so it's a nested declarator, and the post operations
4690 apply to the innermost pointed to type (if any). */
4691 /* XXX: this is not correct to modify 'ad' at this point, but
4692 the syntax is not clear */
4693 parse_attribute(ad
);
4694 post
= type_decl(type
, ad
, v
, td
);
4698 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4699 /* type identifier */
4704 if (!(td
& TYPE_ABSTRACT
))
4705 expect("identifier");
4708 post_type(post
, ad
, storage
, 0);
4709 parse_attribute(ad
);
4714 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4715 ST_FUNC
int lvalue_type(int t
)
4720 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4722 else if (bt
== VT_SHORT
)
4726 if (t
& VT_UNSIGNED
)
4727 r
|= VT_LVAL_UNSIGNED
;
4731 /* indirection with full error checking and bound check */
4732 ST_FUNC
void indir(void)
4734 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4735 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4739 if (vtop
->r
& VT_LVAL
)
4741 vtop
->type
= *pointed_type(&vtop
->type
);
4742 /* Arrays and functions are never lvalues */
4743 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4744 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4745 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4746 /* if bound checking, the referenced pointer must be checked */
4747 #ifdef CONFIG_TCC_BCHECK
4748 if (tcc_state
->do_bounds_check
)
4749 vtop
->r
|= VT_MUSTBOUND
;
4754 /* pass a parameter to a function and do type checking and casting */
4755 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4760 func_type
= func
->f
.func_type
;
4761 if (func_type
== FUNC_OLD
||
4762 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4763 /* default casting : only need to convert float to double */
4764 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4765 gen_cast_s(VT_DOUBLE
);
4766 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4767 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4768 type
.ref
= vtop
->type
.ref
;
4771 } else if (arg
== NULL
) {
4772 tcc_error("too many arguments to function");
4775 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4776 gen_assign_cast(&type
);
4780 /* parse an expression and return its type without any side effect. */
4781 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4790 /* parse an expression of the form '(type)' or '(expr)' and return its
4792 static void parse_expr_type(CType
*type
)
4798 if (parse_btype(type
, &ad
)) {
4799 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4801 expr_type(type
, gexpr
);
4806 static void parse_type(CType
*type
)
4811 if (!parse_btype(type
, &ad
)) {
4814 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4817 static void parse_builtin_params(int nc
, const char *args
)
4824 while ((c
= *args
++)) {
4828 case 'e': expr_eq(); continue;
4829 case 't': parse_type(&t
); vpush(&t
); continue;
4830 default: tcc_error("internal error"); break;
4838 ST_FUNC
void unary(void)
4840 int n
, t
, align
, size
, r
, sizeof_caller
;
4845 sizeof_caller
= in_sizeof
;
4848 /* XXX: GCC 2.95.3 does not generate a table although it should be
4856 #ifdef TCC_TARGET_PE
4857 t
= VT_SHORT
|VT_UNSIGNED
;
4865 vsetc(&type
, VT_CONST
, &tokc
);
4869 t
= VT_INT
| VT_UNSIGNED
;
4875 t
= VT_LLONG
| VT_UNSIGNED
;
4887 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
;
4890 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
| VT_UNSIGNED
;
4892 case TOK___FUNCTION__
:
4894 goto tok_identifier
;
4900 /* special function name identifier */
4901 len
= strlen(funcname
) + 1;
4902 /* generate char[len] type */
4907 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4908 if (!NODATA_WANTED
) {
4909 ptr
= section_ptr_add(data_section
, len
);
4910 memcpy(ptr
, funcname
, len
);
4916 #ifdef TCC_TARGET_PE
4917 t
= VT_SHORT
| VT_UNSIGNED
;
4923 /* string parsing */
4925 if (tcc_state
->char_is_unsigned
)
4926 t
= VT_BYTE
| VT_UNSIGNED
;
4928 if (tcc_state
->warn_write_strings
)
4933 memset(&ad
, 0, sizeof(AttributeDef
));
4934 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4939 if (parse_btype(&type
, &ad
)) {
4940 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4942 /* check ISOC99 compound literal */
4944 /* data is allocated locally by default */
4949 /* all except arrays are lvalues */
4950 if (!(type
.t
& VT_ARRAY
))
4951 r
|= lvalue_type(type
.t
);
4952 memset(&ad
, 0, sizeof(AttributeDef
));
4953 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4955 if (sizeof_caller
) {
4962 } else if (tok
== '{') {
4963 int saved_nocode_wanted
= nocode_wanted
;
4965 tcc_error("expected constant");
4966 /* save all registers */
4968 /* statement expression : we do not accept break/continue
4969 inside as GCC does. We do retain the nocode_wanted state,
4970 as statement expressions can't ever be entered from the
4971 outside, so any reactivation of code emission (from labels
4972 or loop heads) can be disabled again after the end of it. */
4974 nocode_wanted
= saved_nocode_wanted
;
4989 /* functions names must be treated as function pointers,
4990 except for unary '&' and sizeof. Since we consider that
4991 functions are not lvalues, we only have to handle it
4992 there and in function calls. */
4993 /* arrays can also be used although they are not lvalues */
4994 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4995 !(vtop
->type
.t
& VT_ARRAY
))
4997 mk_pointer(&vtop
->type
);
5003 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5004 gen_cast_s(VT_BOOL
);
5005 vtop
->c
.i
= !vtop
->c
.i
;
5006 } else if (vtop
->r
== VT_CMP
) {
5008 n
= vtop
->jfalse
, vtop
->jfalse
= vtop
->jtrue
, vtop
->jtrue
= n
;
5023 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
5024 tcc_error("pointer not accepted for unary plus");
5025 /* In order to force cast, we add zero, except for floating point
5026 where we really need an noop (otherwise -0.0 will be transformed
5028 if (!is_float(vtop
->type
.t
)) {
5040 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
5042 if (vtop
[1].r
& VT_SYM
)
5043 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
5044 size
= type_size(&type
, &align
);
5045 if (s
&& s
->a
.aligned
)
5046 align
= 1 << (s
->a
.aligned
- 1);
5047 if (t
== TOK_SIZEOF
) {
5048 if (!(type
.t
& VT_VLA
)) {
5050 tcc_error("sizeof applied to an incomplete type");
5053 vla_runtime_type_size(&type
, &align
);
5058 vtop
->type
.t
|= VT_UNSIGNED
;
5061 case TOK_builtin_expect
:
5062 /* __builtin_expect is a no-op for now */
5063 parse_builtin_params(0, "ee");
5066 case TOK_builtin_types_compatible_p
:
5067 parse_builtin_params(0, "tt");
5068 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5069 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5070 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
5074 case TOK_builtin_choose_expr
:
5101 case TOK_builtin_constant_p
:
5102 parse_builtin_params(1, "e");
5103 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5107 case TOK_builtin_frame_address
:
5108 case TOK_builtin_return_address
:
5114 if (tok
!= TOK_CINT
) {
5115 tcc_error("%s only takes positive integers",
5116 tok1
== TOK_builtin_return_address
?
5117 "__builtin_return_address" :
5118 "__builtin_frame_address");
5120 level
= (uint32_t)tokc
.i
;
5125 vset(&type
, VT_LOCAL
, 0); /* local frame */
5127 mk_pointer(&vtop
->type
);
5128 indir(); /* -> parent frame */
5130 if (tok1
== TOK_builtin_return_address
) {
5131 // assume return address is just above frame pointer on stack
5134 mk_pointer(&vtop
->type
);
5139 #ifdef TCC_TARGET_X86_64
5140 #ifdef TCC_TARGET_PE
5141 case TOK_builtin_va_start
:
5142 parse_builtin_params(0, "ee");
5143 r
= vtop
->r
& VT_VALMASK
;
5147 tcc_error("__builtin_va_start expects a local variable");
5149 vtop
->type
= char_pointer_type
;
5154 case TOK_builtin_va_arg_types
:
5155 parse_builtin_params(0, "t");
5156 vpushi(classify_x86_64_va_arg(&vtop
->type
));
5163 #ifdef TCC_TARGET_ARM64
5164 case TOK___va_start
: {
5165 parse_builtin_params(0, "ee");
5169 vtop
->type
.t
= VT_VOID
;
5172 case TOK___va_arg
: {
5173 parse_builtin_params(0, "et");
5181 case TOK___arm64_clear_cache
: {
5182 parse_builtin_params(0, "ee");
5185 vtop
->type
.t
= VT_VOID
;
5189 /* pre operations */
5200 t
= vtop
->type
.t
& VT_BTYPE
;
5202 /* In IEEE negate(x) isn't subtract(0,x), but rather
5206 vtop
->c
.f
= -1.0 * 0.0;
5207 else if (t
== VT_DOUBLE
)
5208 vtop
->c
.d
= -1.0 * 0.0;
5210 vtop
->c
.ld
= -1.0 * 0.0;
5218 goto tok_identifier
;
5220 /* allow to take the address of a label */
5221 if (tok
< TOK_UIDENT
)
5222 expect("label identifier");
5223 s
= label_find(tok
);
5225 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5227 if (s
->r
== LABEL_DECLARED
)
5228 s
->r
= LABEL_FORWARD
;
5231 s
->type
.t
= VT_VOID
;
5232 mk_pointer(&s
->type
);
5233 s
->type
.t
|= VT_STATIC
;
5235 vpushsym(&s
->type
, s
);
5241 CType controlling_type
;
5242 int has_default
= 0;
5245 TokenString
*str
= NULL
;
5246 int saved_const_wanted
= const_wanted
;
5251 expr_type(&controlling_type
, expr_eq
);
5252 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
5253 if ((controlling_type
.t
& VT_BTYPE
) == VT_FUNC
)
5254 mk_pointer(&controlling_type
);
5255 const_wanted
= saved_const_wanted
;
5259 if (tok
== TOK_DEFAULT
) {
5261 tcc_error("too many 'default'");
5267 AttributeDef ad_tmp
;
5272 parse_btype(&cur_type
, &ad_tmp
);
5275 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
5276 if (compare_types(&controlling_type
, &cur_type
, 0)) {
5278 tcc_error("type match twice");
5288 skip_or_save_block(&str
);
5290 skip_or_save_block(NULL
);
5297 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
5298 tcc_error("type '%s' does not match any association", buf
);
5300 begin_macro(str
, 1);
5309 // special qnan , snan and infinity values
5314 vtop
->type
.t
= VT_FLOAT
;
5319 goto special_math_val
;
5322 goto special_math_val
;
5329 expect("identifier");
5331 if (!s
|| IS_ASM_SYM(s
)) {
5332 const char *name
= get_tok_str(t
, NULL
);
5334 tcc_error("'%s' undeclared", name
);
5335 /* for simple function calls, we tolerate undeclared
5336 external reference to int() function */
5337 if (tcc_state
->warn_implicit_function_declaration
5338 #ifdef TCC_TARGET_PE
5339 /* people must be warned about using undeclared WINAPI functions
5340 (which usually start with uppercase letter) */
5341 || (name
[0] >= 'A' && name
[0] <= 'Z')
5344 tcc_warning("implicit declaration of function '%s'", name
);
5345 s
= external_global_sym(t
, &func_old_type
);
5349 /* A symbol that has a register is a local register variable,
5350 which starts out as VT_LOCAL value. */
5351 if ((r
& VT_VALMASK
) < VT_CONST
)
5352 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5354 vset(&s
->type
, r
, s
->c
);
5355 /* Point to s as backpointer (even without r&VT_SYM).
5356 Will be used by at least the x86 inline asm parser for
5362 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5363 vtop
->c
.i
= s
->enum_val
;
5368 /* post operations */
5370 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5373 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5374 int qualifiers
, cumofs
= 0;
5376 if (tok
== TOK_ARROW
)
5378 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5381 /* expect pointer on structure */
5382 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5383 expect("struct or union");
5384 if (tok
== TOK_CDOUBLE
)
5385 expect("field name");
5387 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5388 expect("field name");
5389 s
= find_field(&vtop
->type
, tok
, &cumofs
);
5391 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5392 /* add field offset to pointer */
5393 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5394 vpushi(cumofs
+ s
->c
);
5396 /* change type to field type, and set to lvalue */
5397 vtop
->type
= s
->type
;
5398 vtop
->type
.t
|= qualifiers
;
5399 /* an array is never an lvalue */
5400 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5401 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5402 #ifdef CONFIG_TCC_BCHECK
5403 /* if bound checking, the referenced pointer must be checked */
5404 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5405 vtop
->r
|= VT_MUSTBOUND
;
5409 } else if (tok
== '[') {
5415 } else if (tok
== '(') {
5418 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5421 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5422 /* pointer test (no array accepted) */
5423 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5424 vtop
->type
= *pointed_type(&vtop
->type
);
5425 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5429 expect("function pointer");
5432 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5434 /* get return type */
5437 sa
= s
->next
; /* first parameter */
5438 nb_args
= regsize
= 0;
5440 /* compute first implicit argument if a structure is returned */
5441 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5442 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5443 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5444 &ret_align
, ®size
);
5446 /* get some space for the returned structure */
5447 size
= type_size(&s
->type
, &align
);
5448 #ifdef TCC_TARGET_ARM64
5449 /* On arm64, a small struct is return in registers.
5450 It is much easier to write it to memory if we know
5451 that we are allowed to write some extra bytes, so
5452 round the allocated space up to a power of 2: */
5454 while (size
& (size
- 1))
5455 size
= (size
| (size
- 1)) + 1;
5457 loc
= (loc
- size
) & -align
;
5459 ret
.r
= VT_LOCAL
| VT_LVAL
;
5460 /* pass it as 'int' to avoid structure arg passing
5462 vseti(VT_LOCAL
, loc
);
5472 /* return in register */
5473 if (is_float(ret
.type
.t
)) {
5474 ret
.r
= reg_fret(ret
.type
.t
);
5475 #ifdef TCC_TARGET_X86_64
5476 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5480 #ifndef TCC_TARGET_ARM64
5481 #ifndef TCC_TARGET_RISCV64
5482 #ifdef TCC_TARGET_X86_64
5483 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5485 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5497 gfunc_param_typed(s
, sa
);
5507 tcc_error("too few arguments to function");
5509 gfunc_call(nb_args
);
5512 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5513 vsetc(&ret
.type
, r
, &ret
.c
);
5514 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5517 /* handle packed struct return */
5518 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5521 size
= type_size(&s
->type
, &align
);
5522 /* We're writing whole regs often, make sure there's enough
5523 space. Assume register size is power of 2. */
5524 if (regsize
> align
)
5526 loc
= (loc
- size
) & -align
;
5530 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5534 if (--ret_nregs
== 0)
5538 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5540 if (s
->f
.func_noreturn
)
5548 ST_FUNC
void expr_prod(void)
5553 while (tok
== '*' || tok
== '/' || tok
== '%') {
5561 ST_FUNC
void expr_sum(void)
5566 while (tok
== '+' || tok
== '-') {
5574 static void expr_shift(void)
5579 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5587 static void expr_cmp(void)
5592 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5593 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5601 static void expr_cmpeq(void)
5606 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5614 static void expr_and(void)
5617 while (tok
== '&') {
5624 static void expr_xor(void)
5627 while (tok
== '^') {
5634 static void expr_or(void)
5637 while (tok
== '|') {
5644 static int condition_3way(void);
5646 static void expr_landor(void(*e_fn
)(void), int e_op
, int i
)
5648 int t
= 0, cc
= 1, f
= 0, c
;
5650 c
= f
? i
: condition_3way();
5652 save_regs(1), cc
= 0;
5653 } else if (c
!= i
) {
5654 nocode_wanted
++, f
= 1;
5676 static void expr_land(void)
5679 if (tok
== TOK_LAND
)
5680 expr_landor(expr_or
, TOK_LAND
, 1);
5683 static void expr_lor(void)
5687 expr_landor(expr_land
, TOK_LOR
, 0);
5690 /* Assuming vtop is a value used in a conditional context
5691 (i.e. compared with zero) return 0 if it's false, 1 if
5692 true and -1 if it can't be statically determined. */
5693 static int condition_3way(void)
5696 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5697 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5699 gen_cast_s(VT_BOOL
);
5706 static int is_cond_bool(SValue
*sv
)
5708 if ((sv
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
5709 && (sv
->type
.t
& VT_BTYPE
) == VT_INT
)
5710 return (unsigned)sv
->c
.i
< 2;
5711 if (sv
->r
== VT_CMP
)
5716 static void expr_cond(void)
5718 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5720 CType type
, type1
, type2
;
5726 c
= condition_3way();
5727 g
= (tok
== ':' && gnu_ext
);
5737 /* needed to avoid having different registers saved in
5740 if (is_float(vtop
->type
.t
)) {
5742 #ifdef TCC_TARGET_X86_64
5743 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5754 ncw_prev
= nocode_wanted
;
5761 if (c
< 0 && vtop
->r
== VT_CMP
) {
5767 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5768 mk_pointer(&vtop
->type
);
5770 sv
= *vtop
; /* save value to handle it later */
5771 vtop
--; /* no vpop so that FP stack is not flushed */
5781 nocode_wanted
= ncw_prev
;
5787 if (c
< 0 && is_cond_bool(vtop
) && is_cond_bool(&sv
)) {
5788 if (sv
.r
== VT_CMP
) {
5799 nocode_wanted
= ncw_prev
;
5800 // tcc_warning("two conditions expr_cond");
5804 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5805 mk_pointer(&vtop
->type
);
5808 bt1
= t1
& VT_BTYPE
;
5810 bt2
= t2
& VT_BTYPE
;
5813 /* cast operands to correct type according to ISOC rules */
5814 if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5815 type
.t
= VT_VOID
; /* NOTE: as an extension, we accept void on only one side */
5816 } else if (is_float(bt1
) || is_float(bt2
)) {
5817 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5818 type
.t
= VT_LDOUBLE
;
5820 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5825 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5826 /* cast to biggest op */
5827 type
.t
= VT_LLONG
| VT_LONG
;
5828 if (bt1
== VT_LLONG
)
5830 if (bt2
== VT_LLONG
)
5832 /* convert to unsigned if it does not fit in a long long */
5833 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5834 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5835 type
.t
|= VT_UNSIGNED
;
5836 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5837 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
5838 /* If one is a null ptr constant the result type
5840 if (is_null_pointer (vtop
)) type
= type1
;
5841 else if (is_null_pointer (&sv
)) type
= type2
;
5842 else if (bt1
!= bt2
)
5843 tcc_error("incompatible types in conditional expressions");
5845 CType
*pt1
= pointed_type(&type1
);
5846 CType
*pt2
= pointed_type(&type2
);
5847 int pbt1
= pt1
->t
& VT_BTYPE
;
5848 int pbt2
= pt2
->t
& VT_BTYPE
;
5849 int newquals
, copied
= 0;
5850 /* pointers to void get preferred, otherwise the
5851 pointed to types minus qualifs should be compatible */
5852 type
= (pbt1
== VT_VOID
) ? type1
: type2
;
5853 if (pbt1
!= VT_VOID
&& pbt2
!= VT_VOID
) {
5854 if(!compare_types(pt1
, pt2
, 1/*unqualif*/))
5855 tcc_warning("pointer type mismatch in conditional expression\n");
5857 /* combine qualifs */
5858 newquals
= ((pt1
->t
| pt2
->t
) & (VT_CONSTANT
| VT_VOLATILE
));
5859 if ((~pointed_type(&type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
5862 /* copy the pointer target symbol */
5863 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5866 pointed_type(&type
)->t
|= newquals
;
5868 /* pointers to incomplete arrays get converted to
5869 pointers to completed ones if possible */
5870 if (pt1
->t
& VT_ARRAY
5871 && pt2
->t
& VT_ARRAY
5872 && pointed_type(&type
)->ref
->c
< 0
5873 && (pt1
->ref
->c
> 0 || pt2
->ref
->c
> 0))
5876 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5878 pointed_type(&type
)->ref
=
5879 sym_push(SYM_FIELD
, &pointed_type(&type
)->ref
->type
,
5880 0, pointed_type(&type
)->ref
->c
);
5881 pointed_type(&type
)->ref
->c
=
5882 0 < pt1
->ref
->c
? pt1
->ref
->c
: pt2
->ref
->c
;
5885 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5886 /* XXX: test structure compatibility */
5887 type
= bt1
== VT_STRUCT
? type1
: type2
;
5889 /* integer operations */
5890 type
.t
= VT_INT
| (VT_LONG
& (t1
| t2
));
5891 /* convert to unsigned if it does not fit in an integer */
5892 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5893 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5894 type
.t
|= VT_UNSIGNED
;
5896 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5897 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5898 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5900 /* now we convert second operand */
5904 mk_pointer(&vtop
->type
);
5906 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5911 if (is_float(type
.t
)) {
5913 #ifdef TCC_TARGET_X86_64
5914 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5918 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5919 /* for long longs, we use fixed registers to avoid having
5920 to handle a complicated move */
5930 nocode_wanted
= ncw_prev
;
5932 /* this is horrible, but we must also convert first
5938 mk_pointer(&vtop
->type
);
5940 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5946 move_reg(r2
, r1
, type
.t
);
5957 static void expr_eq(void)
5963 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5964 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5965 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5980 ST_FUNC
void gexpr(void)
5991 /* parse a constant expression and return value in vtop. */
5992 static void expr_const1(void)
6001 /* parse an integer constant and return its value. */
6002 static inline int64_t expr_const64(void)
6006 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
6007 expect("constant expression");
6013 /* parse an integer constant and return its value.
6014 Complain if it doesn't fit 32bit (signed or unsigned). */
6015 ST_FUNC
int expr_const(void)
6018 int64_t wc
= expr_const64();
6020 if (c
!= wc
&& (unsigned)c
!= wc
)
6021 tcc_error("constant exceeds 32 bit");
6025 /* ------------------------------------------------------------------------- */
6026 /* return from function */
6028 #ifndef TCC_TARGET_ARM64
6029 #ifndef TCC_TARGET_RISCV64
6030 static void gfunc_return(CType
*func_type
)
6032 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6033 CType type
, ret_type
;
6034 int ret_align
, ret_nregs
, regsize
;
6035 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
6036 &ret_align
, ®size
);
6037 if (0 == ret_nregs
) {
6038 /* if returning structure, must copy it to implicit
6039 first pointer arg location */
6042 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
6045 /* copy structure value to pointer */
6048 /* returning structure packed into registers */
6049 int r
, size
, addr
, align
;
6050 size
= type_size(func_type
,&align
);
6051 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
6052 (vtop
->c
.i
& (ret_align
-1)))
6053 && (align
& (ret_align
-1))) {
6054 loc
= (loc
- size
) & -ret_align
;
6057 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
6061 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
6063 vtop
->type
= ret_type
;
6064 if (is_float(ret_type
.t
))
6065 r
= rc_fret(ret_type
.t
);
6076 if (--ret_nregs
== 0)
6078 /* We assume that when a structure is returned in multiple
6079 registers, their classes are consecutive values of the
6082 vtop
->c
.i
+= regsize
;
6086 } else if (is_float(func_type
->t
)) {
6087 gv(rc_fret(func_type
->t
));
6091 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6096 static void check_func_return(void)
6098 if ((func_vt
.t
& VT_BTYPE
) == VT_VOID
)
6100 if (!strcmp (funcname
, "main")
6101 && (func_vt
.t
& VT_BTYPE
) == VT_INT
) {
6102 /* main returns 0 by default */
6104 gen_assign_cast(&func_vt
);
6105 gfunc_return(&func_vt
);
6107 tcc_warning("function might return no value: '%s'", funcname
);
6111 /* ------------------------------------------------------------------------- */
6114 static int case_cmp(const void *pa
, const void *pb
)
6116 int64_t a
= (*(struct case_t
**) pa
)->v1
;
6117 int64_t b
= (*(struct case_t
**) pb
)->v1
;
6118 return a
< b
? -1 : a
> b
;
6121 static void gtst_addr(int t
, int a
)
6123 gsym_addr(gvtst(0, t
), a
);
6126 static void gcase(struct case_t
**base
, int len
, int *bsym
)
6130 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
6147 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
6149 gcase(base
, len
/2, bsym
);
6153 base
+= e
; len
-= e
;
6163 if (p
->v1
== p
->v2
) {
6165 gtst_addr(0, p
->sym
);
6175 gtst_addr(0, p
->sym
);
6179 *bsym
= gjmp(*bsym
);
6182 /* ------------------------------------------------------------------------- */
6183 /* __attribute__((cleanup(fn))) */
6185 static void try_call_scope_cleanup(Sym
*stop
)
6187 Sym
*cls
= cur_scope
->cl
.s
;
6189 for (; cls
!= stop
; cls
= cls
->ncl
) {
6190 Sym
*fs
= cls
->next
;
6191 Sym
*vs
= cls
->prev_tok
;
6193 vpushsym(&fs
->type
, fs
);
6194 vset(&vs
->type
, vs
->r
, vs
->c
);
6196 mk_pointer(&vtop
->type
);
6202 static void try_call_cleanup_goto(Sym
*cleanupstate
)
6207 if (!cur_scope
->cl
.s
)
6210 /* search NCA of both cleanup chains given parents and initial depth */
6211 ocd
= cleanupstate
? cleanupstate
->v
& ~SYM_FIELD
: 0;
6212 for (ccd
= cur_scope
->cl
.n
, oc
= cleanupstate
; ocd
> ccd
; --ocd
, oc
= oc
->ncl
)
6214 for (cc
= cur_scope
->cl
.s
; ccd
> ocd
; --ccd
, cc
= cc
->ncl
)
6216 for (; cc
!= oc
; cc
= cc
->ncl
, oc
= oc
->ncl
, --ccd
)
6219 try_call_scope_cleanup(cc
);
6222 /* call 'func' for each __attribute__((cleanup(func))) */
6223 static void block_cleanup(struct scope
*o
)
6227 for (pg
= &pending_gotos
; (g
= *pg
) && g
->c
> o
->cl
.n
;) {
6228 if (g
->prev_tok
->r
& LABEL_FORWARD
) {
6233 try_call_scope_cleanup(o
->cl
.s
);
6234 pcl
->jnext
= gjmp(0);
6236 goto remove_pending
;
6246 try_call_scope_cleanup(o
->cl
.s
);
6249 /* ------------------------------------------------------------------------- */
6252 static void vla_restore(int loc
)
6255 gen_vla_sp_restore(loc
);
6258 static void vla_leave(struct scope
*o
)
6260 if (o
->vla
.num
< cur_scope
->vla
.num
)
6261 vla_restore(o
->vla
.loc
);
6264 /* ------------------------------------------------------------------------- */
6267 void new_scope(struct scope
*o
)
6269 /* copy and link previous scope */
6271 o
->prev
= cur_scope
;
6274 /* record local declaration stack position */
6275 o
->lstk
= local_stack
;
6276 o
->llstk
= local_label_stack
;
6281 void prev_scope(struct scope
*o
, int is_expr
)
6285 if (o
->cl
.s
!= o
->prev
->cl
.s
)
6286 block_cleanup(o
->prev
);
6288 /* pop locally defined labels */
6289 label_pop(&local_label_stack
, o
->llstk
, is_expr
);
6291 /* In the is_expr case (a statement expression is finished here),
6292 vtop might refer to symbols on the local_stack. Either via the
6293 type or via vtop->sym. We can't pop those nor any that in turn
6294 might be referred to. To make it easier we don't roll back
6295 any symbols in that case; some upper level call to block() will
6296 do that. We do have to remove such symbols from the lookup
6297 tables, though. sym_pop will do that. */
6299 /* pop locally defined symbols */
6300 sym_pop(&local_stack
, o
->lstk
, is_expr
);
6302 cur_scope
= o
->prev
;
6306 /* leave a scope via break/continue(/goto) */
6307 void leave_scope(struct scope
*o
)
6311 try_call_scope_cleanup(o
->cl
.s
);
6315 /* ------------------------------------------------------------------------- */
6316 /* call block from 'for do while' loops */
6318 static void lblock(int *bsym
, int *csym
)
6320 struct scope
*lo
= loop_scope
, *co
= cur_scope
;
6321 int *b
= co
->bsym
, *c
= co
->csym
;
6335 static void block(int is_expr
)
6337 int a
, b
, c
, d
, e
, t
;
6341 /* default return value is (void) */
6343 vtop
->type
.t
= VT_VOID
;
6355 if (tok
== TOK_ELSE
) {
6360 gsym(d
); /* patch else jmp */
6365 } else if (t
== TOK_WHILE
) {
6377 } else if (t
== '{') {
6381 /* handle local labels declarations */
6382 while (tok
== TOK_LABEL
) {
6385 if (tok
< TOK_UIDENT
)
6386 expect("label identifier");
6387 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
6389 } while (tok
== ',');
6393 while (tok
!= '}') {
6402 prev_scope(&o
, is_expr
);
6404 if (0 == local_scope
&& !nocode_wanted
)
6405 check_func_return();
6408 } else if (t
== TOK_RETURN
) {
6410 b
= (func_vt
.t
& VT_BTYPE
) != VT_VOID
;
6412 gexpr(), gen_assign_cast(&func_vt
);
6413 leave_scope(root_scope
);
6415 gfunc_return(&func_vt
);
6419 tcc_warning("'return' with no value.");
6421 /* jump unless last stmt in top-level block */
6422 if (tok
!= '}' || local_scope
!= 1)
6426 } else if (t
== TOK_BREAK
) {
6428 if (!cur_scope
->bsym
)
6429 tcc_error("cannot break");
6430 if (!cur_switch
|| cur_scope
->bsym
!= cur_switch
->bsym
)
6431 leave_scope(loop_scope
);
6433 leave_scope(cur_switch
->scope
);
6434 *cur_scope
->bsym
= gjmp(*cur_scope
->bsym
);
6437 } else if (t
== TOK_CONTINUE
) {
6439 if (!cur_scope
->csym
)
6440 tcc_error("cannot continue");
6441 leave_scope(loop_scope
);
6442 *cur_scope
->csym
= gjmp(*cur_scope
->csym
);
6445 } else if (t
== TOK_FOR
) {
6451 /* c99 for-loop init decl? */
6452 if (!decl0(VT_LOCAL
, 1, NULL
)) {
6453 /* no, regular for-loop init expr */
6481 } else if (t
== TOK_DO
) {
6495 } else if (t
== TOK_SWITCH
) {
6496 struct switch_t
*saved
, sw
;
6503 sw
.scope
= cur_scope
;
6511 switchval
= *vtop
--;
6514 b
= gjmp(0); /* jump to first case */
6516 a
= gjmp(a
); /* add implicit break */
6520 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6521 for (b
= 1; b
< sw
.n
; b
++)
6522 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6523 tcc_error("duplicate case value");
6525 /* Our switch table sorting is signed, so the compared
6526 value needs to be as well when it's 64bit. */
6527 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6528 switchval
.type
.t
&= ~VT_UNSIGNED
;
6531 d
= 0, gcase(sw
.p
, sw
.n
, &d
);
6534 gsym_addr(d
, sw
.def_sym
);
6540 dynarray_reset(&sw
.p
, &sw
.n
);
6543 } else if (t
== TOK_CASE
) {
6544 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6547 cr
->v1
= cr
->v2
= expr_const64();
6548 if (gnu_ext
&& tok
== TOK_DOTS
) {
6550 cr
->v2
= expr_const64();
6551 if (cr
->v2
< cr
->v1
)
6552 tcc_warning("empty case range");
6555 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6558 goto block_after_label
;
6560 } else if (t
== TOK_DEFAULT
) {
6563 if (cur_switch
->def_sym
)
6564 tcc_error("too many 'default'");
6565 cur_switch
->def_sym
= gind();
6568 goto block_after_label
;
6570 } else if (t
== TOK_GOTO
) {
6571 vla_restore(root_scope
->vla
.loc
);
6572 if (tok
== '*' && gnu_ext
) {
6576 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6580 } else if (tok
>= TOK_UIDENT
) {
6581 s
= label_find(tok
);
6582 /* put forward definition if needed */
6584 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6585 else if (s
->r
== LABEL_DECLARED
)
6586 s
->r
= LABEL_FORWARD
;
6588 if (s
->r
& LABEL_FORWARD
) {
6589 /* start new goto chain for cleanups, linked via label->next */
6590 if (cur_scope
->cl
.s
&& !nocode_wanted
) {
6591 sym_push2(&pending_gotos
, SYM_FIELD
, 0, cur_scope
->cl
.n
);
6592 pending_gotos
->prev_tok
= s
;
6593 s
= sym_push2(&s
->next
, SYM_FIELD
, 0, 0);
6594 pending_gotos
->next
= s
;
6596 s
->jnext
= gjmp(s
->jnext
);
6598 try_call_cleanup_goto(s
->cleanupstate
);
6599 gjmp_addr(s
->jnext
);
6604 expect("label identifier");
6608 } else if (t
== TOK_ASM1
|| t
== TOK_ASM2
|| t
== TOK_ASM3
) {
6612 if (tok
== ':' && t
>= TOK_UIDENT
) {
6617 if (s
->r
== LABEL_DEFINED
)
6618 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6619 s
->r
= LABEL_DEFINED
;
6621 Sym
*pcl
; /* pending cleanup goto */
6622 for (pcl
= s
->next
; pcl
; pcl
= pcl
->prev
)
6624 sym_pop(&s
->next
, NULL
, 0);
6628 s
= label_push(&global_label_stack
, t
, LABEL_DEFINED
);
6631 s
->cleanupstate
= cur_scope
->cl
.s
;
6634 vla_restore(cur_scope
->vla
.loc
);
6635 /* we accept this, but it is a mistake */
6637 tcc_warning("deprecated use of label at end of compound statement");
6643 /* expression case */
6659 /* This skips over a stream of tokens containing balanced {} and ()
6660 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6661 with a '{'). If STR then allocates and stores the skipped tokens
6662 in *STR. This doesn't check if () and {} are nested correctly,
6663 i.e. "({)}" is accepted. */
6664 static void skip_or_save_block(TokenString
**str
)
6666 int braces
= tok
== '{';
6669 *str
= tok_str_alloc();
6671 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6673 if (tok
== TOK_EOF
) {
6674 if (str
|| level
> 0)
6675 tcc_error("unexpected end of file");
6680 tok_str_add_tok(*str
);
6683 if (t
== '{' || t
== '(') {
6685 } else if (t
== '}' || t
== ')') {
6687 if (level
== 0 && braces
&& t
== '}')
6692 tok_str_add(*str
, -1);
6693 tok_str_add(*str
, 0);
6697 #define EXPR_CONST 1
6700 static void parse_init_elem(int expr_type
)
6702 int saved_global_expr
;
6705 /* compound literals must be allocated globally in this case */
6706 saved_global_expr
= global_expr
;
6709 global_expr
= saved_global_expr
;
6710 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6711 (compound literals). */
6712 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6713 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6714 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6715 #ifdef TCC_TARGET_PE
6716 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6719 tcc_error("initializer element is not constant");
6727 /* put zeros for variable based init */
6728 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6731 /* nothing to do because globals are already set to zero */
6733 vpush_global_sym(&func_old_type
, TOK_memset
);
6735 #ifdef TCC_TARGET_ARM
6747 #define DIF_SIZE_ONLY 2
6748 #define DIF_HAVE_ELEM 4
6750 /* t is the array or struct type. c is the array or struct
6751 address. cur_field is the pointer to the current
6752 field, for arrays the 'c' member contains the current start
6753 index. 'flags' is as in decl_initializer.
6754 'al' contains the already initialized length of the
6755 current container (starting at c). This returns the new length of that. */
6756 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6757 Sym
**cur_field
, int flags
, int al
)
6760 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6761 unsigned long corig
= c
;
6766 if (flags
& DIF_HAVE_ELEM
)
6769 if (gnu_ext
&& tok
>= TOK_UIDENT
) {
6776 /* NOTE: we only support ranges for last designator */
6777 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6779 if (!(type
->t
& VT_ARRAY
))
6780 expect("array type");
6782 index
= index_last
= expr_const();
6783 if (tok
== TOK_DOTS
&& gnu_ext
) {
6785 index_last
= expr_const();
6789 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6791 tcc_error("invalid index");
6793 (*cur_field
)->c
= index_last
;
6794 type
= pointed_type(type
);
6795 elem_size
= type_size(type
, &align
);
6796 c
+= index
* elem_size
;
6797 nb_elems
= index_last
- index
+ 1;
6804 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6805 expect("struct/union type");
6807 f
= find_field(type
, l
, &cumofs
);
6820 } else if (!gnu_ext
) {
6825 if (type
->t
& VT_ARRAY
) {
6826 index
= (*cur_field
)->c
;
6827 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6828 tcc_error("index too large");
6829 type
= pointed_type(type
);
6830 c
+= index
* type_size(type
, &align
);
6833 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6834 *cur_field
= f
= f
->next
;
6836 tcc_error("too many field init");
6841 /* must put zero in holes (note that doing it that way
6842 ensures that it even works with designators) */
6843 if (!(flags
& DIF_SIZE_ONLY
) && c
- corig
> al
)
6844 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6845 decl_initializer(type
, sec
, c
, flags
& ~DIF_FIRST
);
6847 /* XXX: make it more general */
6848 if (!(flags
& DIF_SIZE_ONLY
) && nb_elems
> 1) {
6849 unsigned long c_end
;
6854 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6855 for (i
= 1; i
< nb_elems
; i
++) {
6856 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6861 } else if (!NODATA_WANTED
) {
6862 c_end
= c
+ nb_elems
* elem_size
;
6863 if (c_end
> sec
->data_allocated
)
6864 section_realloc(sec
, c_end
);
6865 src
= sec
->data
+ c
;
6867 for(i
= 1; i
< nb_elems
; i
++) {
6869 memcpy(dst
, src
, elem_size
);
6873 c
+= nb_elems
* type_size(type
, &align
);
6879 /* store a value or an expression directly in global data or in local array */
6880 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6887 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6891 /* XXX: not portable */
6892 /* XXX: generate error if incorrect relocation */
6893 gen_assign_cast(&dtype
);
6894 bt
= type
->t
& VT_BTYPE
;
6896 if ((vtop
->r
& VT_SYM
)
6899 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6900 || (type
->t
& VT_BITFIELD
))
6901 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6903 tcc_error("initializer element is not computable at load time");
6905 if (NODATA_WANTED
) {
6910 size
= type_size(type
, &align
);
6911 section_reserve(sec
, c
+ size
);
6912 ptr
= sec
->data
+ c
;
6914 /* XXX: make code faster ? */
6915 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6916 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6917 /* XXX This rejects compound literals like
6918 '(void *){ptr}'. The problem is that '&sym' is
6919 represented the same way, which would be ruled out
6920 by the SYM_FIRST_ANOM check above, but also '"string"'
6921 in 'char *p = "string"' is represented the same
6922 with the type being VT_PTR and the symbol being an
6923 anonymous one. That is, there's no difference in vtop
6924 between '(void *){x}' and '&(void *){x}'. Ignore
6925 pointer typed entities here. Hopefully no real code
6926 will every use compound literals with scalar type. */
6927 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6928 /* These come from compound literals, memcpy stuff over. */
6932 esym
= elfsym(vtop
->sym
);
6933 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6934 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6936 /* We need to copy over all memory contents, and that
6937 includes relocations. Use the fact that relocs are
6938 created it order, so look from the end of relocs
6939 until we hit one before the copied region. */
6940 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6941 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6942 while (num_relocs
--) {
6944 if (rel
->r_offset
>= esym
->st_value
+ size
)
6946 if (rel
->r_offset
< esym
->st_value
)
6948 /* Note: if the same fields are initialized multiple
6949 times (possible with designators) then we possibly
6950 add multiple relocations for the same offset here.
6951 That would lead to wrong code, the last reloc needs
6952 to win. We clean this up later after the whole
6953 initializer is parsed. */
6954 put_elf_reloca(symtab_section
, sec
,
6955 c
+ rel
->r_offset
- esym
->st_value
,
6956 ELFW(R_TYPE
)(rel
->r_info
),
6957 ELFW(R_SYM
)(rel
->r_info
),
6967 if (type
->t
& VT_BITFIELD
) {
6968 int bit_pos
, bit_size
, bits
, n
;
6969 unsigned char *p
, v
, m
;
6970 bit_pos
= BIT_POS(vtop
->type
.t
);
6971 bit_size
= BIT_SIZE(vtop
->type
.t
);
6972 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6973 bit_pos
&= 7, bits
= 0;
6978 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6979 m
= ((1 << n
) - 1) << bit_pos
;
6980 *p
= (*p
& ~m
) | (v
& m
);
6981 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6985 /* XXX: when cross-compiling we assume that each type has the
6986 same representation on host and target, which is likely to
6987 be wrong in the case of long double */
6989 vtop
->c
.i
= vtop
->c
.i
!= 0;
6991 *(char *)ptr
|= vtop
->c
.i
;
6994 *(short *)ptr
|= vtop
->c
.i
;
6997 *(float*)ptr
= vtop
->c
.f
;
7000 *(double *)ptr
= vtop
->c
.d
;
7003 #if defined TCC_IS_NATIVE_387
7004 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
7005 memcpy(ptr
, &vtop
->c
.ld
, 10);
7007 else if (sizeof (long double) == sizeof (double))
7008 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr
) : "m" (vtop
->c
.ld
));
7010 else if (vtop
->c
.ld
== 0.0)
7014 if (sizeof(long double) == LDOUBLE_SIZE
)
7015 *(long double*)ptr
= vtop
->c
.ld
;
7016 else if (sizeof(double) == LDOUBLE_SIZE
)
7017 *(double *)ptr
= (double)vtop
->c
.ld
;
7019 tcc_error("can't cross compile long double constants");
7023 *(long long *)ptr
|= vtop
->c
.i
;
7030 addr_t val
= vtop
->c
.i
;
7032 if (vtop
->r
& VT_SYM
)
7033 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
7035 *(addr_t
*)ptr
|= val
;
7037 if (vtop
->r
& VT_SYM
)
7038 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
7039 *(addr_t
*)ptr
|= val
;
7045 int val
= vtop
->c
.i
;
7047 if (vtop
->r
& VT_SYM
)
7048 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
7052 if (vtop
->r
& VT_SYM
)
7053 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
7062 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
7069 /* 't' contains the type and storage info. 'c' is the offset of the
7070 object in section 'sec'. If 'sec' is NULL, it means stack based
7071 allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
7072 dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
7073 size only evaluation is wanted (only for arrays). */
7074 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
7077 int len
, n
, no_oblock
, nb
, i
;
7083 if (!(flags
& DIF_HAVE_ELEM
) && tok
!= '{' &&
7084 /* In case of strings we have special handling for arrays, so
7085 don't consume them as initializer value (which would commit them
7086 to some anonymous symbol). */
7087 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
7088 !(flags
& DIF_SIZE_ONLY
)) {
7089 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7090 flags
|= DIF_HAVE_ELEM
;
7093 if ((flags
& DIF_HAVE_ELEM
) &&
7094 !(type
->t
& VT_ARRAY
) &&
7095 /* Use i_c_parameter_t, to strip toplevel qualifiers.
7096 The source type might have VT_CONSTANT set, which is
7097 of course assignable to non-const elements. */
7098 is_compatible_unqualified_types(type
, &vtop
->type
)) {
7099 init_putv(type
, sec
, c
);
7100 } else if (type
->t
& VT_ARRAY
) {
7103 t1
= pointed_type(type
);
7104 size1
= type_size(t1
, &align1
);
7107 if (((flags
& DIF_FIRST
) && tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
7110 tcc_error("character array initializer must be a literal,"
7111 " optionally enclosed in braces");
7116 /* only parse strings here if correct type (otherwise: handle
7117 them as ((w)char *) expressions */
7118 if ((tok
== TOK_LSTR
&&
7119 #ifdef TCC_TARGET_PE
7120 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
7122 (t1
->t
& VT_BTYPE
) == VT_INT
7124 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
7126 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7129 /* compute maximum number of chars wanted */
7131 cstr_len
= tokc
.str
.size
;
7133 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
7136 if (n
>= 0 && nb
> (n
- len
))
7138 if (!(flags
& DIF_SIZE_ONLY
)) {
7140 tcc_warning("initializer-string for array is too long");
7141 /* in order to go faster for common case (char
7142 string in global variable, we handle it
7144 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7146 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
7150 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
7152 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
7154 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
7161 /* only add trailing zero if enough storage (no
7162 warning in this case since it is standard) */
7163 if (n
< 0 || len
< n
) {
7164 if (!(flags
& DIF_SIZE_ONLY
)) {
7166 init_putv(t1
, sec
, c
+ (len
* size1
));
7177 while (tok
!= '}' || (flags
& DIF_HAVE_ELEM
)) {
7178 len
= decl_designator(type
, sec
, c
, &f
, flags
, len
);
7179 flags
&= ~DIF_HAVE_ELEM
;
7180 if (type
->t
& VT_ARRAY
) {
7182 /* special test for multi dimensional arrays (may not
7183 be strictly correct if designators are used at the
7185 if (no_oblock
&& len
>= n
*size1
)
7188 if (s
->type
.t
== VT_UNION
)
7192 if (no_oblock
&& f
== NULL
)
7201 /* put zeros at the end */
7202 if (!(flags
& DIF_SIZE_ONLY
) && len
< n
*size1
)
7203 init_putz(sec
, c
+ len
, n
*size1
- len
);
7206 /* patch type size if needed, which happens only for array types */
7208 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
7209 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7212 if ((flags
& DIF_FIRST
) || tok
== '{') {
7220 } else if (tok
== '{') {
7221 if (flags
& DIF_HAVE_ELEM
)
7224 decl_initializer(type
, sec
, c
, flags
& ~DIF_HAVE_ELEM
);
7226 } else if ((flags
& DIF_SIZE_ONLY
)) {
7227 /* If we supported only ISO C we wouldn't have to accept calling
7228 this on anything than an array if DIF_SIZE_ONLY (and even then
7229 only on the outermost level, so no recursion would be needed),
7230 because initializing a flex array member isn't supported.
7231 But GNU C supports it, so we need to recurse even into
7232 subfields of structs and arrays when DIF_SIZE_ONLY is set. */
7233 /* just skip expression */
7234 skip_or_save_block(NULL
);
7236 if (!(flags
& DIF_HAVE_ELEM
)) {
7237 /* This should happen only when we haven't parsed
7238 the init element above for fear of committing a
7239 string constant to memory too early. */
7240 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
7241 expect("string constant");
7242 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7244 init_putv(type
, sec
, c
);
7248 /* parse an initializer for type 't' if 'has_init' is non zero, and
7249 allocate space in local or global data space ('r' is either
7250 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7251 variable 'v' of scope 'scope' is declared before initializers
7252 are parsed. If 'v' is zero, then a reference to the new object
7253 is put in the value stack. If 'has_init' is 2, a special parsing
7254 is done to handle string constants. */
7255 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7256 int has_init
, int v
, int scope
)
7258 int size
, align
, addr
;
7259 TokenString
*init_str
= NULL
;
7262 Sym
*flexible_array
;
7264 int saved_nocode_wanted
= nocode_wanted
;
7265 #ifdef CONFIG_TCC_BCHECK
7269 /* Always allocate static or global variables */
7270 if (v
&& (r
& VT_VALMASK
) == VT_CONST
)
7271 nocode_wanted
|= 0x80000000;
7273 #ifdef CONFIG_TCC_BCHECK
7274 bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
7277 flexible_array
= NULL
;
7278 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7279 Sym
*field
= type
->ref
->next
;
7282 field
= field
->next
;
7283 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
7284 flexible_array
= field
;
7288 size
= type_size(type
, &align
);
7289 /* If unknown size, we must evaluate it before
7290 evaluating initializers because
7291 initializers can generate global data too
7292 (e.g. string pointers or ISOC99 compound
7293 literals). It also simplifies local
7294 initializers handling */
7295 if (size
< 0 || (flexible_array
&& has_init
)) {
7297 tcc_error("unknown type size");
7298 /* get all init string */
7299 if (has_init
== 2) {
7300 init_str
= tok_str_alloc();
7301 /* only get strings */
7302 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7303 tok_str_add_tok(init_str
);
7306 tok_str_add(init_str
, -1);
7307 tok_str_add(init_str
, 0);
7309 skip_or_save_block(&init_str
);
7314 begin_macro(init_str
, 1);
7316 decl_initializer(type
, NULL
, 0, DIF_FIRST
| DIF_SIZE_ONLY
);
7317 /* prepare second initializer parsing */
7318 macro_ptr
= init_str
->str
;
7321 /* if still unknown size, error */
7322 size
= type_size(type
, &align
);
7324 tcc_error("unknown type size");
7326 /* If there's a flex member and it was used in the initializer
7328 if (flexible_array
&&
7329 flexible_array
->type
.ref
->c
> 0)
7330 size
+= flexible_array
->type
.ref
->c
7331 * pointed_size(&flexible_array
->type
);
7332 /* take into account specified alignment if bigger */
7333 if (ad
->a
.aligned
) {
7334 int speca
= 1 << (ad
->a
.aligned
- 1);
7337 } else if (ad
->a
.packed
) {
7341 if (!v
&& NODATA_WANTED
)
7342 size
= 0, align
= 1;
7344 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7346 #ifdef CONFIG_TCC_BCHECK
7347 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7351 loc
= (loc
- size
) & -align
;
7353 #ifdef CONFIG_TCC_BCHECK
7354 /* handles bounds */
7355 /* XXX: currently, since we do only one pass, we cannot track
7356 '&' operators, so we add only arrays */
7357 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7359 /* add padding between regions */
7361 /* then add local bound info */
7362 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
7363 bounds_ptr
[0] = addr
;
7364 bounds_ptr
[1] = size
;
7368 /* local variable */
7369 #ifdef CONFIG_TCC_ASM
7370 if (ad
->asm_label
) {
7371 int reg
= asm_parse_regvar(ad
->asm_label
);
7373 r
= (r
& ~VT_VALMASK
) | reg
;
7376 sym
= sym_push(v
, type
, r
, addr
);
7377 if (ad
->cleanup_func
) {
7378 Sym
*cls
= sym_push2(&all_cleanups
,
7379 SYM_FIELD
| ++cur_scope
->cl
.n
, 0, 0);
7380 cls
->prev_tok
= sym
;
7381 cls
->next
= ad
->cleanup_func
;
7382 cls
->ncl
= cur_scope
->cl
.s
;
7383 cur_scope
->cl
.s
= cls
;
7388 /* push local reference */
7389 vset(type
, r
, addr
);
7392 if (v
&& scope
== VT_CONST
) {
7393 /* see if the symbol was already defined */
7396 patch_storage(sym
, ad
, type
);
7397 /* we accept several definitions of the same global variable. */
7398 if (!has_init
&& sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
)
7403 /* allocate symbol in corresponding section */
7408 else if (tcc_state
->nocommon
)
7413 addr
= section_add(sec
, size
, align
);
7414 #ifdef CONFIG_TCC_BCHECK
7415 /* add padding if bound check */
7417 section_add(sec
, 1, 1);
7420 addr
= align
; /* SHN_COMMON is special, symbol value is align */
7421 sec
= common_section
;
7426 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7427 patch_storage(sym
, ad
, NULL
);
7429 /* update symbol definition */
7430 put_extern_sym(sym
, sec
, addr
, size
);
7432 /* push global reference */
7433 vpush_ref(type
, sec
, addr
, size
);
7438 #ifdef CONFIG_TCC_BCHECK
7439 /* handles bounds now because the symbol must be defined
7440 before for the relocation */
7444 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
7445 /* then add global bound info */
7446 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
7447 bounds_ptr
[0] = 0; /* relocated */
7448 bounds_ptr
[1] = size
;
7453 if (type
->t
& VT_VLA
) {
7459 /* save current stack pointer */
7460 if (root_scope
->vla
.loc
== 0) {
7461 struct scope
*v
= cur_scope
;
7462 gen_vla_sp_save(loc
-= PTR_SIZE
);
7463 do v
->vla
.loc
= loc
; while ((v
= v
->prev
));
7466 vla_runtime_type_size(type
, &a
);
7467 gen_vla_alloc(type
, a
);
7468 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7469 /* on _WIN64, because of the function args scratch area, the
7470 result of alloca differs from RSP and is returned in RAX. */
7471 gen_vla_result(addr
), addr
= (loc
-= PTR_SIZE
);
7473 gen_vla_sp_save(addr
);
7474 cur_scope
->vla
.loc
= addr
;
7475 cur_scope
->vla
.num
++;
7477 } else if (has_init
) {
7478 size_t oldreloc_offset
= 0;
7479 if (sec
&& sec
->reloc
)
7480 oldreloc_offset
= sec
->reloc
->data_offset
;
7481 decl_initializer(type
, sec
, addr
, DIF_FIRST
);
7482 if (sec
&& sec
->reloc
)
7483 squeeze_multi_relocs(sec
, oldreloc_offset
);
7484 /* patch flexible array member size back to -1, */
7485 /* for possible subsequent similar declarations */
7487 flexible_array
->type
.ref
->c
= -1;
7491 /* restore parse state if needed */
7497 nocode_wanted
= saved_nocode_wanted
;
7500 /* parse a function defined by symbol 'sym' and generate its code in
7501 'cur_text_section' */
7502 static void gen_function(Sym
*sym
)
7504 /* Initialize VLA state */
7505 struct scope f
= { 0 };
7506 cur_scope
= root_scope
= &f
;
7509 ind
= cur_text_section
->data_offset
;
7510 if (sym
->a
.aligned
) {
7511 size_t newoff
= section_add(cur_text_section
, 0,
7512 1 << (sym
->a
.aligned
- 1));
7513 gen_fill_nops(newoff
- ind
);
7515 /* NOTE: we patch the symbol size later */
7516 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7518 funcname
= get_tok_str(sym
->v
, NULL
);
7521 /* put debug symbol */
7522 tcc_debug_funcstart(tcc_state
, sym
);
7523 /* push a dummy symbol to enable local sym storage */
7524 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7525 local_scope
= 1; /* for function parameters */
7526 gfunc_prolog(&sym
->type
);
7529 clear_temp_local_var_list();
7534 cur_text_section
->data_offset
= ind
;
7535 /* reset local stack */
7536 sym_pop(&local_stack
, NULL
, 0);
7538 label_pop(&global_label_stack
, NULL
, 0);
7539 sym_pop(&all_cleanups
, NULL
, 0);
7540 /* patch symbol size */
7541 elfsym(sym
)->st_size
= ind
- func_ind
;
7542 /* end of function */
7543 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7544 /* It's better to crash than to generate wrong code */
7545 cur_text_section
= NULL
;
7546 funcname
= ""; /* for safety */
7547 func_vt
.t
= VT_VOID
; /* for safety */
7548 func_var
= 0; /* for safety */
7549 ind
= 0; /* for safety */
7550 nocode_wanted
= 0x80000000;
7554 static void gen_inline_functions(TCCState
*s
)
7557 int inline_generated
, i
;
7558 struct InlineFunc
*fn
;
7560 tcc_open_bf(s
, ":inline:", 0);
7561 /* iterate while inline function are referenced */
7563 inline_generated
= 0;
7564 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7565 fn
= s
->inline_fns
[i
];
7567 if (sym
&& (sym
->c
|| !(sym
->type
.t
& VT_INLINE
))) {
7568 /* the function was used or forced (and then not internal):
7569 generate its code and convert it to a normal function */
7572 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7573 begin_macro(fn
->func_str
, 1);
7575 cur_text_section
= text_section
;
7579 inline_generated
= 1;
7582 } while (inline_generated
);
7586 ST_FUNC
void free_inline_functions(TCCState
*s
)
7589 /* free tokens of unused inline functions */
7590 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7591 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7593 tok_str_free(fn
->func_str
);
7595 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7598 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7599 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7600 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7605 AttributeDef ad
, adbase
;
7608 if (tok
== TOK_STATIC_ASSERT
) {
7616 tcc_error("%s", get_tok_str(tok
, &tokc
));
7622 if (!parse_btype(&btype
, &adbase
)) {
7623 if (is_for_loop_init
)
7625 /* skip redundant ';' if not in old parameter decl scope */
7626 if (tok
== ';' && l
!= VT_CMP
) {
7632 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7633 /* global asm block */
7637 if (tok
>= TOK_UIDENT
) {
7638 /* special test for old K&R protos without explicit int
7639 type. Only accepted when defining global data */
7643 expect("declaration");
7648 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7649 int v
= btype
.ref
->v
;
7650 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7651 tcc_warning("unnamed struct/union that defines no instances");
7655 if (IS_ENUM(btype
.t
)) {
7660 while (1) { /* iterate thru each declaration */
7662 /* If the base type itself was an array type of unspecified
7663 size (like in 'typedef int arr[]; arr x = {1};') then
7664 we will overwrite the unknown size by the real one for
7665 this decl. We need to unshare the ref symbol holding
7667 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7668 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7671 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7675 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7676 printf("type = '%s'\n", buf
);
7679 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7680 /* if old style function prototype, we accept a
7683 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7684 decl0(VT_CMP
, 0, sym
);
7685 /* always compile 'extern inline' */
7686 if (type
.t
& VT_EXTERN
)
7687 type
.t
&= ~VT_INLINE
;
7690 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7691 ad
.asm_label
= asm_label_instr();
7692 /* parse one last attribute list, after asm label */
7693 parse_attribute(&ad
);
7695 /* gcc does not allow __asm__("label") with function definition,
7702 #ifdef TCC_TARGET_PE
7703 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7704 if (type
.t
& VT_STATIC
)
7705 tcc_error("cannot have dll linkage with static");
7706 if (type
.t
& VT_TYPEDEF
) {
7707 tcc_warning("'%s' attribute ignored for typedef",
7708 ad
.a
.dllimport
? (ad
.a
.dllimport
= 0, "dllimport") :
7709 (ad
.a
.dllexport
= 0, "dllexport"));
7710 } else if (ad
.a
.dllimport
) {
7711 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7714 type
.t
|= VT_EXTERN
;
7720 tcc_error("cannot use local functions");
7721 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7722 expect("function definition");
7724 /* reject abstract declarators in function definition
7725 make old style params without decl have int type */
7727 while ((sym
= sym
->next
) != NULL
) {
7728 if (!(sym
->v
& ~SYM_FIELD
))
7729 expect("identifier");
7730 if (sym
->type
.t
== VT_VOID
)
7731 sym
->type
= int_type
;
7734 /* put function symbol */
7735 type
.t
&= ~VT_EXTERN
;
7736 sym
= external_sym(v
, &type
, 0, &ad
);
7737 /* static inline functions are just recorded as a kind
7738 of macro. Their code will be emitted at the end of
7739 the compilation unit only if they are used */
7740 if (sym
->type
.t
& VT_INLINE
) {
7741 struct InlineFunc
*fn
;
7742 const char *filename
;
7744 filename
= file
? file
->filename
: "";
7745 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7746 strcpy(fn
->filename
, filename
);
7748 skip_or_save_block(&fn
->func_str
);
7749 dynarray_add(&tcc_state
->inline_fns
,
7750 &tcc_state
->nb_inline_fns
, fn
);
7752 /* compute text section */
7753 cur_text_section
= ad
.section
;
7754 if (!cur_text_section
)
7755 cur_text_section
= text_section
;
7761 /* find parameter in function parameter list */
7762 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7763 if ((sym
->v
& ~SYM_FIELD
) == v
)
7765 tcc_error("declaration for parameter '%s' but no such parameter",
7766 get_tok_str(v
, NULL
));
7768 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7769 tcc_error("storage class specified for '%s'",
7770 get_tok_str(v
, NULL
));
7771 if (sym
->type
.t
!= VT_VOID
)
7772 tcc_error("redefinition of parameter '%s'",
7773 get_tok_str(v
, NULL
));
7774 convert_parameter_type(&type
);
7776 } else if (type
.t
& VT_TYPEDEF
) {
7777 /* save typedefed type */
7778 /* XXX: test storage specifiers ? */
7780 if (sym
&& sym
->sym_scope
== local_scope
) {
7781 if (!is_compatible_types(&sym
->type
, &type
)
7782 || !(sym
->type
.t
& VT_TYPEDEF
))
7783 tcc_error("incompatible redefinition of '%s'",
7784 get_tok_str(v
, NULL
));
7787 sym
= sym_push(v
, &type
, 0, 0);
7791 } else if ((type
.t
& VT_BTYPE
) == VT_VOID
7792 && !(type
.t
& VT_EXTERN
)) {
7793 tcc_error("declaration of void object");
7796 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7797 /* external function definition */
7798 /* specific case for func_call attribute */
7800 } else if (!(type
.t
& VT_ARRAY
)) {
7801 /* not lvalue if array */
7802 r
|= lvalue_type(type
.t
);
7804 has_init
= (tok
== '=');
7805 if (has_init
&& (type
.t
& VT_VLA
))
7806 tcc_error("variable length array cannot be initialized");
7807 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
))
7808 || (type
.t
& VT_BTYPE
) == VT_FUNC
7809 /* as with GCC, uninitialized global arrays with no size
7810 are considered extern: */
7811 || ((type
.t
& VT_ARRAY
) && !has_init
7812 && l
== VT_CONST
&& type
.ref
->c
< 0)
7814 /* external variable or function */
7815 type
.t
|= VT_EXTERN
;
7816 sym
= external_sym(v
, &type
, r
, &ad
);
7817 if (ad
.alias_target
) {
7820 alias_target
= sym_find(ad
.alias_target
);
7821 esym
= elfsym(alias_target
);
7823 tcc_error("unsupported forward __alias__ attribute");
7824 put_extern_sym2(sym
, esym
->st_shndx
, esym
->st_value
, esym
->st_size
, 0);
7827 if (type
.t
& VT_STATIC
)
7833 else if (l
== VT_CONST
)
7834 /* uninitialized global variables may be overridden */
7835 type
.t
|= VT_EXTERN
;
7836 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7840 if (is_for_loop_init
)
7852 static void decl(int l
)
7857 /* ------------------------------------------------------------------------- */
7860 /* ------------------------------------------------------------------------- */