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 then */
879 vset_VT_CMP(vtop
->c
.i
!= 0);
881 p
= inv
? &vtop
->jfalse
: &vtop
->jtrue
;
882 *p
= gjmp_append(*p
, t
);
885 /* Generate value test
887 * Generate a test for any value (jump, comparison and integers) */
888 static int gvtst(int inv
, int t
)
894 t
= vtop
->jtrue
, u
= vtop
->jfalse
;
899 /* jump to the wanted target */
901 t
= gjmp_cond(op
^ inv
, t
);
904 /* resolve complementary jumps to here */
911 /* ------------------------------------------------------------------------- */
912 /* push a symbol value of TYPE */
913 static inline void vpushsym(CType
*type
, Sym
*sym
)
917 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
921 /* Return a static symbol pointing to a section */
922 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
928 sym
= sym_push(v
, type
, VT_CONST
| VT_SYM
, 0);
929 sym
->type
.t
|= VT_STATIC
;
930 put_extern_sym(sym
, sec
, offset
, size
);
934 /* push a reference to a section offset by adding a dummy symbol */
935 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
937 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
940 /* define a new external reference to a symbol 'v' of type 'u' */
941 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
)
947 /* push forward reference */
948 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
949 s
->type
.ref
= type
->ref
;
950 } else if (IS_ASM_SYM(s
)) {
951 s
->type
.t
= type
->t
| (s
->type
.t
& VT_EXTERN
);
952 s
->type
.ref
= type
->ref
;
958 /* Merge symbol attributes. */
959 static void merge_symattr(struct SymAttr
*sa
, struct SymAttr
*sa1
)
961 if (sa1
->aligned
&& !sa
->aligned
)
962 sa
->aligned
= sa1
->aligned
;
963 sa
->packed
|= sa1
->packed
;
964 sa
->weak
|= sa1
->weak
;
965 if (sa1
->visibility
!= STV_DEFAULT
) {
966 int vis
= sa
->visibility
;
967 if (vis
== STV_DEFAULT
968 || vis
> sa1
->visibility
)
969 vis
= sa1
->visibility
;
970 sa
->visibility
= vis
;
972 sa
->dllexport
|= sa1
->dllexport
;
973 sa
->nodecorate
|= sa1
->nodecorate
;
974 sa
->dllimport
|= sa1
->dllimport
;
977 /* Merge function attributes. */
978 static void merge_funcattr(struct FuncAttr
*fa
, struct FuncAttr
*fa1
)
980 if (fa1
->func_call
&& !fa
->func_call
)
981 fa
->func_call
= fa1
->func_call
;
982 if (fa1
->func_type
&& !fa
->func_type
)
983 fa
->func_type
= fa1
->func_type
;
984 if (fa1
->func_args
&& !fa
->func_args
)
985 fa
->func_args
= fa1
->func_args
;
988 /* Merge attributes. */
989 static void merge_attr(AttributeDef
*ad
, AttributeDef
*ad1
)
991 merge_symattr(&ad
->a
, &ad1
->a
);
992 merge_funcattr(&ad
->f
, &ad1
->f
);
995 ad
->section
= ad1
->section
;
996 if (ad1
->alias_target
)
997 ad
->alias_target
= ad1
->alias_target
;
999 ad
->asm_label
= ad1
->asm_label
;
1001 ad
->attr_mode
= ad1
->attr_mode
;
1004 /* Merge some type attributes. */
1005 static void patch_type(Sym
*sym
, CType
*type
)
1007 if (!(type
->t
& VT_EXTERN
) || IS_ENUM_VAL(sym
->type
.t
)) {
1008 if (!(sym
->type
.t
& VT_EXTERN
))
1009 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
1010 sym
->type
.t
&= ~VT_EXTERN
;
1013 if (IS_ASM_SYM(sym
)) {
1014 /* stay static if both are static */
1015 sym
->type
.t
= type
->t
& (sym
->type
.t
| ~VT_STATIC
);
1016 sym
->type
.ref
= type
->ref
;
1019 if (!is_compatible_types(&sym
->type
, type
)) {
1020 tcc_error("incompatible types for redefinition of '%s'",
1021 get_tok_str(sym
->v
, NULL
));
1023 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1024 int static_proto
= sym
->type
.t
& VT_STATIC
;
1025 /* warn if static follows non-static function declaration */
1026 if ((type
->t
& VT_STATIC
) && !static_proto
1027 /* XXX this test for inline shouldn't be here. Until we
1028 implement gnu-inline mode again it silences a warning for
1029 mingw caused by our workarounds. */
1030 && !((type
->t
| sym
->type
.t
) & VT_INLINE
))
1031 tcc_warning("static storage ignored for redefinition of '%s'",
1032 get_tok_str(sym
->v
, NULL
));
1034 /* set 'inline' if both agree or if one has static */
1035 if ((type
->t
| sym
->type
.t
) & VT_INLINE
) {
1036 if (!((type
->t
^ sym
->type
.t
) & VT_INLINE
)
1037 || ((type
->t
| sym
->type
.t
) & VT_STATIC
))
1038 static_proto
|= VT_INLINE
;
1041 if (0 == (type
->t
& VT_EXTERN
)) {
1042 /* put complete type, use static from prototype */
1043 sym
->type
.t
= (type
->t
& ~(VT_STATIC
|VT_INLINE
)) | static_proto
;
1044 sym
->type
.ref
= type
->ref
;
1046 sym
->type
.t
&= ~VT_INLINE
| static_proto
;
1049 if (sym
->type
.ref
->f
.func_type
== FUNC_OLD
1050 && type
->ref
->f
.func_type
!= FUNC_OLD
) {
1051 sym
->type
.ref
= type
->ref
;
1055 if ((sym
->type
.t
& VT_ARRAY
) && type
->ref
->c
>= 0) {
1056 /* set array size if it was omitted in extern declaration */
1057 sym
->type
.ref
->c
= type
->ref
->c
;
1059 if ((type
->t
^ sym
->type
.t
) & VT_STATIC
)
1060 tcc_warning("storage mismatch for redefinition of '%s'",
1061 get_tok_str(sym
->v
, NULL
));
1065 /* Merge some storage attributes. */
1066 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
1069 patch_type(sym
, type
);
1071 #ifdef TCC_TARGET_PE
1072 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
1073 tcc_error("incompatible dll linkage for redefinition of '%s'",
1074 get_tok_str(sym
->v
, NULL
));
1076 merge_symattr(&sym
->a
, &ad
->a
);
1078 sym
->asm_label
= ad
->asm_label
;
1079 update_storage(sym
);
1082 /* copy sym to other stack */
1083 static Sym
*sym_copy(Sym
*s0
, Sym
**ps
)
1086 s
= sym_malloc(), *s
= *s0
;
1087 s
->prev
= *ps
, *ps
= s
;
1088 if (s
->v
< SYM_FIRST_ANOM
) {
1089 ps
= &table_ident
[s
->v
- TOK_IDENT
]->sym_identifier
;
1090 s
->prev_tok
= *ps
, *ps
= s
;
1095 /* copy a list of syms */
1096 static void sym_copy_ref(Sym
*s0
, Sym
**ps
)
1098 Sym
*s
, **sp
= &s0
->type
.ref
;
1099 for (s
= *sp
, *sp
= NULL
; s
; s
= s
->next
)
1100 sp
= &(*sp
= sym_copy(s
, ps
))->next
;
1103 /* define a new external reference to a symbol 'v' */
1104 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
1108 /* look for global symbol */
1110 while (s
&& s
->sym_scope
)
1114 /* push forward reference */
1115 s
= global_identifier_push(v
, type
->t
, 0);
1118 s
->asm_label
= ad
->asm_label
;
1119 s
->type
.ref
= type
->ref
;
1120 bt
= s
->type
.t
& (VT_BTYPE
|VT_ARRAY
);
1121 /* copy type to the global stack also */
1122 if (local_scope
&& (bt
== VT_FUNC
|| (bt
& VT_ARRAY
)))
1123 sym_copy_ref(s
, &global_stack
);
1125 patch_storage(s
, ad
, type
);
1126 bt
= s
->type
.t
& VT_BTYPE
;
1128 /* push variables to local scope if any */
1129 if (local_stack
&& bt
!= VT_FUNC
)
1130 s
= sym_copy(s
, &local_stack
);
1134 /* push a reference to global symbol v */
1135 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
1137 vpushsym(type
, external_global_sym(v
, type
));
1140 /* save registers up to (vtop - n) stack entry */
1141 ST_FUNC
void save_regs(int n
)
1144 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
1148 /* save r to the memory stack, and mark it as being free */
1149 ST_FUNC
void save_reg(int r
)
1151 save_reg_upstack(r
, 0);
1154 /* save r to the memory stack, and mark it as being free,
1155 if seen up to (vtop - n) stack entry */
1156 ST_FUNC
void save_reg_upstack(int r
, int n
)
1158 int l
, saved
, size
, align
;
1162 if ((r
&= VT_VALMASK
) >= VT_CONST
)
1167 /* modify all stack values */
1170 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
1171 if ((p
->r
& VT_VALMASK
) == r
||
1172 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
1173 /* must save value on stack if not already done */
1175 /* NOTE: must reload 'r' because r might be equal to r2 */
1176 r
= p
->r
& VT_VALMASK
;
1177 /* store register in the stack */
1179 if ((p
->r
& VT_LVAL
) ||
1180 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
1182 type
= &char_pointer_type
;
1186 size
= type_size(type
, &align
);
1187 l
=get_temp_local_var(size
,align
);
1188 sv
.type
.t
= type
->t
;
1189 sv
.r
= VT_LOCAL
| VT_LVAL
;
1192 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1193 /* x86 specific: need to pop fp register ST0 if saved */
1194 if (r
== TREG_ST0
) {
1195 o(0xd8dd); /* fstp %st(0) */
1199 /* special long long case */
1200 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
1207 /* mark that stack entry as being saved on the stack */
1208 if (p
->r
& VT_LVAL
) {
1209 /* also clear the bounded flag because the
1210 relocation address of the function was stored in
1212 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
1214 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
1222 #ifdef TCC_TARGET_ARM
1223 /* find a register of class 'rc2' with at most one reference on stack.
1224 * If none, call get_reg(rc) */
1225 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
1230 for(r
=0;r
<NB_REGS
;r
++) {
1231 if (reg_classes
[r
] & rc2
) {
1234 for(p
= vstack
; p
<= vtop
; p
++) {
1235 if ((p
->r
& VT_VALMASK
) == r
||
1236 (p
->r2
& VT_VALMASK
) == r
)
1247 /* find a free register of class 'rc'. If none, save one register */
1248 ST_FUNC
int get_reg(int rc
)
1253 /* find a free register */
1254 for(r
=0;r
<NB_REGS
;r
++) {
1255 if (reg_classes
[r
] & rc
) {
1258 for(p
=vstack
;p
<=vtop
;p
++) {
1259 if ((p
->r
& VT_VALMASK
) == r
||
1260 (p
->r2
& VT_VALMASK
) == r
)
1268 /* no register left : free the first one on the stack (VERY
1269 IMPORTANT to start from the bottom to ensure that we don't
1270 spill registers used in gen_opi()) */
1271 for(p
=vstack
;p
<=vtop
;p
++) {
1272 /* look at second register (if long long) */
1273 r
= p
->r2
& VT_VALMASK
;
1274 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1276 r
= p
->r
& VT_VALMASK
;
1277 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1283 /* Should never comes here */
1287 /* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
1288 static int get_temp_local_var(int size
,int align
){
1290 struct temp_local_variable
*temp_var
;
1297 for(i
=0;i
<nb_temp_local_vars
;i
++){
1298 temp_var
=&arr_temp_local_vars
[i
];
1299 if(temp_var
->size
<size
||align
!=temp_var
->align
){
1302 /*check if temp_var is free*/
1304 for(p
=vstack
;p
<=vtop
;p
++) {
1306 if(r
==VT_LOCAL
||r
==VT_LLOCAL
){
1307 if(p
->c
.i
==temp_var
->location
){
1314 found_var
=temp_var
->location
;
1320 loc
= (loc
- size
) & -align
;
1321 if(nb_temp_local_vars
<MAX_TEMP_LOCAL_VARIABLE_NUMBER
){
1322 temp_var
=&arr_temp_local_vars
[i
];
1323 temp_var
->location
=loc
;
1324 temp_var
->size
=size
;
1325 temp_var
->align
=align
;
1326 nb_temp_local_vars
++;
1333 static void clear_temp_local_var_list(){
1334 nb_temp_local_vars
=0;
1337 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1339 static void move_reg(int r
, int s
, int t
)
1353 /* get address of vtop (vtop MUST BE an lvalue) */
1354 ST_FUNC
void gaddrof(void)
1356 vtop
->r
&= ~VT_LVAL
;
1357 /* tricky: if saved lvalue, then we can go back to lvalue */
1358 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1359 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1364 #ifdef CONFIG_TCC_BCHECK
1365 /* generate lvalue bound code */
1366 static void gbound(void)
1371 vtop
->r
&= ~VT_MUSTBOUND
;
1372 /* if lvalue, then use checking code before dereferencing */
1373 if (vtop
->r
& VT_LVAL
) {
1374 /* if not VT_BOUNDED value, then make one */
1375 if (!(vtop
->r
& VT_BOUNDED
)) {
1376 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1377 /* must save type because we must set it to int to get pointer */
1379 vtop
->type
.t
= VT_PTR
;
1382 gen_bounded_ptr_add();
1383 vtop
->r
|= lval_type
;
1386 /* then check for dereferencing */
1387 gen_bounded_ptr_deref();
1392 static void incr_bf_adr(int o
)
1394 vtop
->type
= char_pointer_type
;
1398 vtop
->type
.t
= (vtop
->type
.t
& ~(VT_BTYPE
|VT_DEFSIGN
))
1399 | (VT_BYTE
|VT_UNSIGNED
);
1400 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1401 | (VT_LVAL_BYTE
|VT_LVAL_UNSIGNED
|VT_LVAL
);
1404 /* single-byte load mode for packed or otherwise unaligned bitfields */
1405 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1408 save_reg_upstack(vtop
->r
, 1);
1409 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1410 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1419 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1421 vpushi((1 << n
) - 1), gen_op('&');
1424 vpushi(bits
), gen_op(TOK_SHL
);
1427 bits
+= n
, bit_size
-= n
, o
= 1;
1430 if (!(type
->t
& VT_UNSIGNED
)) {
1431 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG
? 64 : 32) - bits
;
1432 vpushi(n
), gen_op(TOK_SHL
);
1433 vpushi(n
), gen_op(TOK_SAR
);
1437 /* single-byte store mode for packed or otherwise unaligned bitfields */
1438 static void store_packed_bf(int bit_pos
, int bit_size
)
1440 int bits
, n
, o
, m
, c
;
1442 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1444 save_reg_upstack(vtop
->r
, 1);
1445 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1447 incr_bf_adr(o
); // X B
1449 c
? vdup() : gv_dup(); // B V X
1452 vpushi(bits
), gen_op(TOK_SHR
);
1454 vpushi(bit_pos
), gen_op(TOK_SHL
);
1459 m
= ((1 << n
) - 1) << bit_pos
;
1460 vpushi(m
), gen_op('&'); // X B V1
1461 vpushv(vtop
-1); // X B V1 B
1462 vpushi(m
& 0x80 ? ~m
& 0x7f : ~m
);
1463 gen_op('&'); // X B V1 B1
1464 gen_op('|'); // X B V2
1466 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1467 vstore(), vpop(); // X B
1468 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1473 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1476 if (0 == sv
->type
.ref
)
1478 t
= sv
->type
.ref
->auxtype
;
1479 if (t
!= -1 && t
!= VT_STRUCT
) {
1480 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1481 sv
->r
= (sv
->r
& ~VT_LVAL_TYPE
) | lvalue_type(sv
->type
.t
);
1486 /* store vtop a register belonging to class 'rc'. lvalues are
1487 converted to values. Cannot be used if cannot be converted to
1488 register value (such as structures). */
1489 ST_FUNC
int gv(int rc
)
1491 int r
, bit_pos
, bit_size
, size
, align
, rc2
;
1493 /* NOTE: get_reg can modify vstack[] */
1494 if (vtop
->type
.t
& VT_BITFIELD
) {
1497 bit_pos
= BIT_POS(vtop
->type
.t
);
1498 bit_size
= BIT_SIZE(vtop
->type
.t
);
1499 /* remove bit field info to avoid loops */
1500 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1503 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1504 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1505 type
.t
|= VT_UNSIGNED
;
1507 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1509 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1514 if (r
== VT_STRUCT
) {
1515 load_packed_bf(&type
, bit_pos
, bit_size
);
1517 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG
? 64 : 32;
1518 /* cast to int to propagate signedness in following ops */
1520 /* generate shifts */
1521 vpushi(bits
- (bit_pos
+ bit_size
));
1523 vpushi(bits
- bit_size
);
1524 /* NOTE: transformed to SHR if unsigned */
1529 if (is_float(vtop
->type
.t
) &&
1530 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1531 unsigned long offset
;
1532 /* CPUs usually cannot use float constants, so we store them
1533 generically in data segment */
1534 size
= type_size(&vtop
->type
, &align
);
1536 size
= 0, align
= 1;
1537 offset
= section_add(data_section
, size
, align
);
1538 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1540 init_putv(&vtop
->type
, data_section
, offset
);
1543 #ifdef CONFIG_TCC_BCHECK
1544 if (vtop
->r
& VT_MUSTBOUND
)
1548 r
= vtop
->r
& VT_VALMASK
;
1549 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1550 #ifndef TCC_TARGET_ARM64
1553 #ifdef TCC_TARGET_X86_64
1554 else if (rc
== RC_FRET
)
1558 /* need to reload if:
1560 - lvalue (need to dereference pointer)
1561 - already a register, but not in the right class */
1563 || (vtop
->r
& VT_LVAL
)
1564 || !(reg_classes
[r
] & rc
)
1566 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1567 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1569 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1575 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1576 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1578 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1579 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1580 unsigned long long ll
;
1582 int r2
, original_type
;
1583 original_type
= vtop
->type
.t
;
1584 /* two register type load : expand to two words
1587 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1590 vtop
->c
.i
= ll
; /* first word */
1592 vtop
->r
= r
; /* save register value */
1593 vpushi(ll
>> 32); /* second word */
1596 if (vtop
->r
& VT_LVAL
) {
1597 /* We do not want to modifier the long long
1598 pointer here, so the safest (and less
1599 efficient) is to save all the other registers
1600 in the stack. XXX: totally inefficient. */
1604 /* lvalue_save: save only if used further down the stack */
1605 save_reg_upstack(vtop
->r
, 1);
1607 /* load from memory */
1608 vtop
->type
.t
= load_type
;
1611 vtop
[-1].r
= r
; /* save register value */
1612 /* increment pointer to get second word */
1613 vtop
->type
.t
= addr_type
;
1618 vtop
->type
.t
= load_type
;
1620 /* move registers */
1623 vtop
[-1].r
= r
; /* save register value */
1624 vtop
->r
= vtop
[-1].r2
;
1626 /* Allocate second register. Here we rely on the fact that
1627 get_reg() tries first to free r2 of an SValue. */
1631 /* write second register */
1633 vtop
->type
.t
= original_type
;
1634 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1636 /* lvalue of scalar type : need to use lvalue type
1637 because of possible cast */
1640 /* compute memory access type */
1641 if (vtop
->r
& VT_LVAL_BYTE
)
1643 else if (vtop
->r
& VT_LVAL_SHORT
)
1645 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1649 /* restore wanted type */
1652 if (vtop
->r
== VT_CMP
)
1654 /* one register type load */
1659 #ifdef TCC_TARGET_C67
1660 /* uses register pairs for doubles */
1661 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1668 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1669 ST_FUNC
void gv2(int rc1
, int rc2
)
1671 /* generate more generic register first. But VT_JMP or VT_CMP
1672 values must be generated first in all cases to avoid possible
1674 if (vtop
->r
!= VT_CMP
&& rc1
<= rc2
) {
1679 /* test if reload is needed for first register */
1680 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1690 /* test if reload is needed for first register */
1691 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1697 #ifndef TCC_TARGET_ARM64
1698 /* wrapper around RC_FRET to return a register by type */
1699 static int rc_fret(int t
)
1701 #ifdef TCC_TARGET_X86_64
1702 if (t
== VT_LDOUBLE
) {
1710 /* wrapper around REG_FRET to return a register by type */
1711 static int reg_fret(int t
)
1713 #ifdef TCC_TARGET_X86_64
1714 if (t
== VT_LDOUBLE
) {
1722 /* expand 64bit on stack in two ints */
1723 ST_FUNC
void lexpand(void)
1726 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1727 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1728 if (v
== VT_CONST
) {
1731 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1737 vtop
[0].r
= vtop
[-1].r2
;
1738 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1740 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1745 /* build a long long from two ints */
1746 static void lbuild(int t
)
1748 gv2(RC_INT
, RC_INT
);
1749 vtop
[-1].r2
= vtop
[0].r
;
1750 vtop
[-1].type
.t
= t
;
1755 /* convert stack entry to register and duplicate its value in another
1757 static void gv_dup(void)
1764 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1765 if (t
& VT_BITFIELD
) {
1775 /* stack: H L L1 H1 */
1785 /* duplicate value */
1790 #ifdef TCC_TARGET_X86_64
1791 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1801 load(r1
, &sv
); /* move r to r1 */
1803 /* duplicates value */
1810 /* generate CPU independent (unsigned) long long operations */
1811 static void gen_opl(int op
)
1813 int t
, a
, b
, op1
, c
, i
;
1815 unsigned short reg_iret
= REG_IRET
;
1816 unsigned short reg_lret
= REG_LRET
;
1822 func
= TOK___divdi3
;
1825 func
= TOK___udivdi3
;
1828 func
= TOK___moddi3
;
1831 func
= TOK___umoddi3
;
1838 /* call generic long long function */
1839 vpush_global_sym(&func_old_type
, func
);
1844 vtop
->r2
= reg_lret
;
1852 //pv("gen_opl A",0,2);
1858 /* stack: L1 H1 L2 H2 */
1863 vtop
[-2] = vtop
[-3];
1866 /* stack: H1 H2 L1 L2 */
1867 //pv("gen_opl B",0,4);
1873 /* stack: H1 H2 L1 L2 ML MH */
1876 /* stack: ML MH H1 H2 L1 L2 */
1880 /* stack: ML MH H1 L2 H2 L1 */
1885 /* stack: ML MH M1 M2 */
1888 } else if (op
== '+' || op
== '-') {
1889 /* XXX: add non carry method too (for MIPS or alpha) */
1895 /* stack: H1 H2 (L1 op L2) */
1898 gen_op(op1
+ 1); /* TOK_xxxC2 */
1901 /* stack: H1 H2 (L1 op L2) */
1904 /* stack: (L1 op L2) H1 H2 */
1906 /* stack: (L1 op L2) (H1 op H2) */
1914 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1915 t
= vtop
[-1].type
.t
;
1919 /* stack: L H shift */
1921 /* constant: simpler */
1922 /* NOTE: all comments are for SHL. the other cases are
1923 done by swapping words */
1934 if (op
!= TOK_SAR
) {
1967 /* XXX: should provide a faster fallback on x86 ? */
1970 func
= TOK___ashrdi3
;
1973 func
= TOK___lshrdi3
;
1976 func
= TOK___ashldi3
;
1982 /* compare operations */
1988 /* stack: L1 H1 L2 H2 */
1990 vtop
[-1] = vtop
[-2];
1992 /* stack: L1 L2 H1 H2 */
1995 /* when values are equal, we need to compare low words. since
1996 the jump is inverted, we invert the test too. */
1999 else if (op1
== TOK_GT
)
2001 else if (op1
== TOK_ULT
)
2003 else if (op1
== TOK_UGT
)
2013 /* generate non equal test */
2015 vset_VT_CMP(TOK_NE
);
2019 /* compare low. Always unsigned */
2023 else if (op1
== TOK_LE
)
2025 else if (op1
== TOK_GT
)
2027 else if (op1
== TOK_GE
)
2030 #if 0//def TCC_TARGET_I386
2031 if (op
== TOK_NE
) { gsym(b
); break; }
2032 if (op
== TOK_EQ
) { gsym(a
); break; }
2041 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
2043 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
2044 return (a
^ b
) >> 63 ? -x
: x
;
2047 static int gen_opic_lt(uint64_t a
, uint64_t b
)
2049 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
2052 /* handle integer constant optimizations and various machine
2054 static void gen_opic(int op
)
2056 SValue
*v1
= vtop
- 1;
2058 int t1
= v1
->type
.t
& VT_BTYPE
;
2059 int t2
= v2
->type
.t
& VT_BTYPE
;
2060 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2061 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2062 uint64_t l1
= c1
? v1
->c
.i
: 0;
2063 uint64_t l2
= c2
? v2
->c
.i
: 0;
2064 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
2066 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2067 l1
= ((uint32_t)l1
|
2068 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2069 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
2070 l2
= ((uint32_t)l2
|
2071 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
2075 case '+': l1
+= l2
; break;
2076 case '-': l1
-= l2
; break;
2077 case '&': l1
&= l2
; break;
2078 case '^': l1
^= l2
; break;
2079 case '|': l1
|= l2
; break;
2080 case '*': l1
*= l2
; break;
2087 /* if division by zero, generate explicit division */
2090 tcc_error("division by zero in constant");
2094 default: l1
= gen_opic_sdiv(l1
, l2
); break;
2095 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
2096 case TOK_UDIV
: l1
= l1
/ l2
; break;
2097 case TOK_UMOD
: l1
= l1
% l2
; break;
2100 case TOK_SHL
: l1
<<= (l2
& shm
); break;
2101 case TOK_SHR
: l1
>>= (l2
& shm
); break;
2103 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
2106 case TOK_ULT
: l1
= l1
< l2
; break;
2107 case TOK_UGE
: l1
= l1
>= l2
; break;
2108 case TOK_EQ
: l1
= l1
== l2
; break;
2109 case TOK_NE
: l1
= l1
!= l2
; break;
2110 case TOK_ULE
: l1
= l1
<= l2
; break;
2111 case TOK_UGT
: l1
= l1
> l2
; break;
2112 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
2113 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
2114 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
2115 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
2117 case TOK_LAND
: l1
= l1
&& l2
; break;
2118 case TOK_LOR
: l1
= l1
|| l2
; break;
2122 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2123 l1
= ((uint32_t)l1
|
2124 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2128 /* if commutative ops, put c2 as constant */
2129 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
2130 op
== '|' || op
== '*')) {
2132 c2
= c1
; //c = c1, c1 = c2, c2 = c;
2133 l2
= l1
; //l = l1, l1 = l2, l2 = l;
2135 if (!const_wanted
&&
2137 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
2138 (l1
== -1 && op
== TOK_SAR
))) {
2139 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
2141 } else if (!const_wanted
&&
2142 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
2144 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
2145 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
2146 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
2151 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
2154 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
2155 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
2158 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
2159 /* filter out NOP operations like x*1, x-0, x&-1... */
2161 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
2162 /* try to use shifts instead of muls or divs */
2163 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
2172 else if (op
== TOK_PDIV
)
2178 } else if (c2
&& (op
== '+' || op
== '-') &&
2179 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
2180 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
2181 /* symbol + constant case */
2185 /* The backends can't always deal with addends to symbols
2186 larger than +-1<<31. Don't construct such. */
2193 /* call low level op generator */
2194 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
2195 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
2203 /* generate a floating point operation with constant propagation */
2204 static void gen_opif(int op
)
2208 #if defined _MSC_VER && defined _AMD64_
2209 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
2216 /* currently, we cannot do computations with forward symbols */
2217 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2218 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2220 if (v1
->type
.t
== VT_FLOAT
) {
2223 } else if (v1
->type
.t
== VT_DOUBLE
) {
2231 /* NOTE: we only do constant propagation if finite number (not
2232 NaN or infinity) (ANSI spec) */
2233 if (!ieee_finite(f1
) || !ieee_finite(f2
))
2237 case '+': f1
+= f2
; break;
2238 case '-': f1
-= f2
; break;
2239 case '*': f1
*= f2
; break;
2242 /* If not in initializer we need to potentially generate
2243 FP exceptions at runtime, otherwise we want to fold. */
2249 /* XXX: also handles tests ? */
2253 /* XXX: overflow test ? */
2254 if (v1
->type
.t
== VT_FLOAT
) {
2256 } else if (v1
->type
.t
== VT_DOUBLE
) {
2268 static int pointed_size(CType
*type
)
2271 return type_size(pointed_type(type
), &align
);
2274 static void vla_runtime_pointed_size(CType
*type
)
2277 vla_runtime_type_size(pointed_type(type
), &align
);
2280 static inline int is_null_pointer(SValue
*p
)
2282 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2284 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2285 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2286 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2287 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0) &&
2288 ((pointed_type(&p
->type
)->t
& VT_BTYPE
) == VT_VOID
) &&
2289 0 == (pointed_type(&p
->type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
2292 static inline int is_integer_btype(int bt
)
2294 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2295 bt
== VT_INT
|| bt
== VT_LLONG
);
2298 /* check types for comparison or subtraction of pointers */
2299 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2301 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2304 /* null pointers are accepted for all comparisons as gcc */
2305 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2309 bt1
= type1
->t
& VT_BTYPE
;
2310 bt2
= type2
->t
& VT_BTYPE
;
2311 /* accept comparison between pointer and integer with a warning */
2312 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2313 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2314 tcc_warning("comparison between pointer and integer");
2318 /* both must be pointers or implicit function pointers */
2319 if (bt1
== VT_PTR
) {
2320 type1
= pointed_type(type1
);
2321 } else if (bt1
!= VT_FUNC
)
2322 goto invalid_operands
;
2324 if (bt2
== VT_PTR
) {
2325 type2
= pointed_type(type2
);
2326 } else if (bt2
!= VT_FUNC
) {
2328 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2330 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2331 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2335 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2336 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2337 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2338 /* gcc-like error if '-' is used */
2340 goto invalid_operands
;
2342 tcc_warning("comparison of distinct pointer types lacks a cast");
2346 /* generic gen_op: handles types problems */
2347 ST_FUNC
void gen_op(int op
)
2349 int u
, t1
, t2
, bt1
, bt2
, t
;
2353 t1
= vtop
[-1].type
.t
;
2354 t2
= vtop
[0].type
.t
;
2355 bt1
= t1
& VT_BTYPE
;
2356 bt2
= t2
& VT_BTYPE
;
2358 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2359 tcc_error("operation on a struct");
2360 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2361 if (bt2
== VT_FUNC
) {
2362 mk_pointer(&vtop
->type
);
2365 if (bt1
== VT_FUNC
) {
2367 mk_pointer(&vtop
->type
);
2372 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2373 /* at least one operand is a pointer */
2374 /* relational op: must be both pointers */
2375 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2376 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2377 /* pointers are handled are unsigned */
2379 t
= VT_LLONG
| VT_UNSIGNED
;
2381 t
= VT_INT
| VT_UNSIGNED
;
2385 /* if both pointers, then it must be the '-' op */
2386 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2388 tcc_error("cannot use pointers here");
2389 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2390 /* XXX: check that types are compatible */
2391 if (vtop
[-1].type
.t
& VT_VLA
) {
2392 vla_runtime_pointed_size(&vtop
[-1].type
);
2394 vpushi(pointed_size(&vtop
[-1].type
));
2398 vtop
->type
.t
= ptrdiff_type
.t
;
2402 /* exactly one pointer : must be '+' or '-'. */
2403 if (op
!= '-' && op
!= '+')
2404 tcc_error("cannot use pointers here");
2405 /* Put pointer as first operand */
2406 if (bt2
== VT_PTR
) {
2408 t
= t1
, t1
= t2
, t2
= t
;
2411 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2412 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2415 type1
= vtop
[-1].type
;
2416 type1
.t
&= ~VT_ARRAY
;
2417 if (vtop
[-1].type
.t
& VT_VLA
)
2418 vla_runtime_pointed_size(&vtop
[-1].type
);
2420 u
= pointed_size(&vtop
[-1].type
);
2422 tcc_error("unknown array element size");
2426 /* XXX: cast to int ? (long long case) */
2432 /* #ifdef CONFIG_TCC_BCHECK
2433 The main reason to removing this code:
2440 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2441 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2443 When this code is on. then the output looks like
2445 v+(i-j) = 0xbff84000
2447 /* if evaluating constant expression, no code should be
2448 generated, so no bound check */
2449 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2450 /* if bounded pointers, we generate a special code to
2457 gen_bounded_ptr_add();
2463 /* put again type if gen_opic() swaped operands */
2466 } else if (is_float(bt1
) || is_float(bt2
)) {
2467 /* compute bigger type and do implicit casts */
2468 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2470 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2475 /* floats can only be used for a few operations */
2476 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2477 (op
< TOK_ULT
|| op
> TOK_GT
))
2478 tcc_error("invalid operands for binary operation");
2480 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2481 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2482 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2484 t
|= (VT_LONG
& t1
);
2486 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2487 /* cast to biggest op */
2488 t
= VT_LLONG
| VT_LONG
;
2489 if (bt1
== VT_LLONG
)
2491 if (bt2
== VT_LLONG
)
2493 /* convert to unsigned if it does not fit in a long long */
2494 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2495 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2499 /* integer operations */
2500 t
= VT_INT
| (VT_LONG
& (t1
| t2
));
2501 /* convert to unsigned if it does not fit in an integer */
2502 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2503 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2506 /* XXX: currently, some unsigned operations are explicit, so
2507 we modify them here */
2508 if (t
& VT_UNSIGNED
) {
2515 else if (op
== TOK_LT
)
2517 else if (op
== TOK_GT
)
2519 else if (op
== TOK_LE
)
2521 else if (op
== TOK_GE
)
2529 /* special case for shifts and long long: we keep the shift as
2531 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2538 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2539 /* relational op: the result is an int */
2540 vtop
->type
.t
= VT_INT
;
2545 // Make sure that we have converted to an rvalue:
2546 if (vtop
->r
& VT_LVAL
)
2547 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2550 #ifndef TCC_TARGET_ARM
2551 /* generic itof for unsigned long long case */
2552 static void gen_cvt_itof1(int t
)
2554 #ifdef TCC_TARGET_ARM64
2557 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2558 (VT_LLONG
| VT_UNSIGNED
)) {
2561 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2562 #if LDOUBLE_SIZE != 8
2563 else if (t
== VT_LDOUBLE
)
2564 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2567 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2571 vtop
->r
= reg_fret(t
);
2579 /* generic ftoi for unsigned long long case */
2580 static void gen_cvt_ftoi1(int t
)
2582 #ifdef TCC_TARGET_ARM64
2587 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2588 /* not handled natively */
2589 st
= vtop
->type
.t
& VT_BTYPE
;
2591 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2592 #if LDOUBLE_SIZE != 8
2593 else if (st
== VT_LDOUBLE
)
2594 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2597 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2602 vtop
->r2
= REG_LRET
;
2609 /* force char or short cast */
2610 static void force_charshort_cast(int t
)
2614 /* cannot cast static initializers */
2615 if (STATIC_DATA_WANTED
)
2619 /* XXX: add optimization if lvalue : just change type and offset */
2624 if (t
& VT_UNSIGNED
) {
2625 vpushi((1 << bits
) - 1);
2628 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2634 /* result must be signed or the SAR is converted to an SHL
2635 This was not the case when "t" was a signed short
2636 and the last value on the stack was an unsigned int */
2637 vtop
->type
.t
&= ~VT_UNSIGNED
;
2643 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2644 static void gen_cast_s(int t
)
2652 static void gen_cast(CType
*type
)
2654 int sbt
, dbt
, sf
, df
, c
, p
;
2656 /* special delayed cast for char/short */
2657 /* XXX: in some cases (multiple cascaded casts), it may still
2659 if (vtop
->r
& VT_MUSTCAST
) {
2660 vtop
->r
&= ~VT_MUSTCAST
;
2661 force_charshort_cast(vtop
->type
.t
);
2664 /* bitfields first get cast to ints */
2665 if (vtop
->type
.t
& VT_BITFIELD
) {
2669 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2670 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2675 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2676 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2677 #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
2678 c
&= dbt
!= VT_LDOUBLE
;
2681 /* constant case: we can do it now */
2682 /* XXX: in ISOC, cannot do it if error in convert */
2683 if (sbt
== VT_FLOAT
)
2684 vtop
->c
.ld
= vtop
->c
.f
;
2685 else if (sbt
== VT_DOUBLE
)
2686 vtop
->c
.ld
= vtop
->c
.d
;
2689 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2690 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2691 vtop
->c
.ld
= vtop
->c
.i
;
2693 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2695 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2696 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2698 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2701 if (dbt
== VT_FLOAT
)
2702 vtop
->c
.f
= (float)vtop
->c
.ld
;
2703 else if (dbt
== VT_DOUBLE
)
2704 vtop
->c
.d
= (double)vtop
->c
.ld
;
2705 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2706 vtop
->c
.i
= vtop
->c
.ld
;
2707 } else if (sf
&& dbt
== VT_BOOL
) {
2708 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2711 vtop
->c
.i
= vtop
->c
.ld
;
2712 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2714 else if (sbt
& VT_UNSIGNED
)
2715 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2717 else if (sbt
== VT_PTR
)
2720 else if (sbt
!= VT_LLONG
)
2721 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2722 -(vtop
->c
.i
& 0x80000000));
2724 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2726 else if (dbt
== VT_BOOL
)
2727 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2729 else if (dbt
== VT_PTR
)
2732 else if (dbt
!= VT_LLONG
) {
2733 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2734 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2737 if (!(dbt
& VT_UNSIGNED
))
2738 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2741 } else if (p
&& dbt
== VT_BOOL
) {
2745 /* non constant case: generate code */
2747 /* convert from fp to fp */
2750 /* convert int to fp */
2753 /* convert fp to int */
2754 if (dbt
== VT_BOOL
) {
2758 /* we handle char/short/etc... with generic code */
2759 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2760 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2764 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2765 /* additional cast for char/short... */
2771 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2772 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2773 /* scalar to long long */
2774 /* machine independent conversion */
2776 /* generate high word */
2777 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2781 if (sbt
== VT_PTR
) {
2782 /* cast from pointer to int before we apply
2783 shift operation, which pointers don't support*/
2790 /* patch second register */
2791 vtop
[-1].r2
= vtop
->r
;
2795 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2796 (dbt
& VT_BTYPE
) == VT_PTR
||
2797 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2798 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2799 (sbt
& VT_BTYPE
) != VT_PTR
&&
2800 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2801 /* need to convert from 32bit to 64bit */
2803 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2804 #if defined(TCC_TARGET_ARM64)
2806 #elif defined(TCC_TARGET_X86_64)
2808 /* x86_64 specific: movslq */
2810 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2817 } else if (dbt
== VT_BOOL
) {
2818 /* scalar to bool */
2821 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2822 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2823 if (sbt
== VT_PTR
) {
2824 vtop
->type
.t
= VT_INT
;
2825 tcc_warning("nonportable conversion from pointer to char/short");
2827 force_charshort_cast(dbt
);
2828 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2830 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2832 /* from long long: just take low order word */
2837 vtop
->type
.t
|= VT_UNSIGNED
;
2841 /* if lvalue and single word type, nothing to do because
2842 the lvalue already contains the real type size (see
2843 VT_LVAL_xxx constants) */
2846 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2847 /* if we are casting between pointer types,
2848 we must update the VT_LVAL_xxx size */
2849 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2850 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2853 vtop
->type
.t
&= ~ ( VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
2856 /* return type size as known at compile time. Put alignment at 'a' */
2857 ST_FUNC
int type_size(CType
*type
, int *a
)
2862 bt
= type
->t
& VT_BTYPE
;
2863 if (bt
== VT_STRUCT
) {
2868 } else if (bt
== VT_PTR
) {
2869 if (type
->t
& VT_ARRAY
) {
2873 ts
= type_size(&s
->type
, a
);
2875 if (ts
< 0 && s
->c
< 0)
2883 } else if (IS_ENUM(type
->t
) && type
->ref
->c
< 0) {
2884 return -1; /* incomplete enum */
2885 } else if (bt
== VT_LDOUBLE
) {
2887 return LDOUBLE_SIZE
;
2888 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2889 #ifdef TCC_TARGET_I386
2890 #ifdef TCC_TARGET_PE
2895 #elif defined(TCC_TARGET_ARM)
2905 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2908 } else if (bt
== VT_SHORT
) {
2911 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2915 /* char, void, function, _Bool */
2921 /* push type size as known at runtime time on top of value stack. Put
2923 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2925 if (type
->t
& VT_VLA
) {
2926 type_size(&type
->ref
->type
, a
);
2927 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2929 vpushi(type_size(type
, a
));
2933 /* return the pointed type of t */
2934 static inline CType
*pointed_type(CType
*type
)
2936 return &type
->ref
->type
;
2939 /* modify type so that its it is a pointer to type. */
2940 ST_FUNC
void mk_pointer(CType
*type
)
2943 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2944 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2948 /* compare function types. OLD functions match any new functions */
2949 static int is_compatible_func(CType
*type1
, CType
*type2
)
2955 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2957 if (s1
->f
.func_type
!= s2
->f
.func_type
2958 && s1
->f
.func_type
!= FUNC_OLD
2959 && s2
->f
.func_type
!= FUNC_OLD
)
2961 /* we should check the function return type for FUNC_OLD too
2962 but that causes problems with the internally used support
2963 functions such as TOK_memmove */
2964 if (s1
->f
.func_type
== FUNC_OLD
&& !s1
->next
)
2966 if (s2
->f
.func_type
== FUNC_OLD
&& !s2
->next
)
2969 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2980 /* return true if type1 and type2 are the same. If unqualified is
2981 true, qualifiers on the types are ignored.
2983 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2987 t1
= type1
->t
& VT_TYPE
;
2988 t2
= type2
->t
& VT_TYPE
;
2990 /* strip qualifiers before comparing */
2991 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2992 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2995 /* Default Vs explicit signedness only matters for char */
2996 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
3000 /* XXX: bitfields ? */
3005 && !(type1
->ref
->c
< 0
3006 || type2
->ref
->c
< 0
3007 || type1
->ref
->c
== type2
->ref
->c
))
3010 /* test more complicated cases */
3011 bt1
= t1
& VT_BTYPE
;
3012 if (bt1
== VT_PTR
) {
3013 type1
= pointed_type(type1
);
3014 type2
= pointed_type(type2
);
3015 return is_compatible_types(type1
, type2
);
3016 } else if (bt1
== VT_STRUCT
) {
3017 return (type1
->ref
== type2
->ref
);
3018 } else if (bt1
== VT_FUNC
) {
3019 return is_compatible_func(type1
, type2
);
3020 } else if (IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)) {
3021 return type1
->ref
== type2
->ref
;
3027 /* return true if type1 and type2 are exactly the same (including
3030 static int is_compatible_types(CType
*type1
, CType
*type2
)
3032 return compare_types(type1
,type2
,0);
3035 /* return true if type1 and type2 are the same (ignoring qualifiers).
3037 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
3039 return compare_types(type1
,type2
,1);
3042 /* print a type. If 'varstr' is not NULL, then the variable is also
3043 printed in the type */
3045 /* XXX: add array and function pointers */
3046 static void type_to_str(char *buf
, int buf_size
,
3047 CType
*type
, const char *varstr
)
3059 pstrcat(buf
, buf_size
, "extern ");
3061 pstrcat(buf
, buf_size
, "static ");
3063 pstrcat(buf
, buf_size
, "typedef ");
3065 pstrcat(buf
, buf_size
, "inline ");
3066 if (t
& VT_VOLATILE
)
3067 pstrcat(buf
, buf_size
, "volatile ");
3068 if (t
& VT_CONSTANT
)
3069 pstrcat(buf
, buf_size
, "const ");
3071 if (((t
& VT_DEFSIGN
) && bt
== VT_BYTE
)
3072 || ((t
& VT_UNSIGNED
)
3073 && (bt
== VT_SHORT
|| bt
== VT_INT
|| bt
== VT_LLONG
)
3076 pstrcat(buf
, buf_size
, (t
& VT_UNSIGNED
) ? "unsigned " : "signed ");
3078 buf_size
-= strlen(buf
);
3113 tstr
= "long double";
3115 pstrcat(buf
, buf_size
, tstr
);
3122 pstrcat(buf
, buf_size
, tstr
);
3123 v
= type
->ref
->v
& ~SYM_STRUCT
;
3124 if (v
>= SYM_FIRST_ANOM
)
3125 pstrcat(buf
, buf_size
, "<anonymous>");
3127 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
3132 if (varstr
&& '*' == *varstr
) {
3133 pstrcat(buf1
, sizeof(buf1
), "(");
3134 pstrcat(buf1
, sizeof(buf1
), varstr
);
3135 pstrcat(buf1
, sizeof(buf1
), ")");
3137 pstrcat(buf1
, buf_size
, "(");
3139 while (sa
!= NULL
) {
3141 type_to_str(buf2
, sizeof(buf2
), &sa
->type
, NULL
);
3142 pstrcat(buf1
, sizeof(buf1
), buf2
);
3145 pstrcat(buf1
, sizeof(buf1
), ", ");
3147 if (s
->f
.func_type
== FUNC_ELLIPSIS
)
3148 pstrcat(buf1
, sizeof(buf1
), ", ...");
3149 pstrcat(buf1
, sizeof(buf1
), ")");
3150 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3155 if (varstr
&& '*' == *varstr
)
3156 snprintf(buf1
, sizeof(buf1
), "(%s)[%d]", varstr
, s
->c
);
3158 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
3159 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3162 pstrcpy(buf1
, sizeof(buf1
), "*");
3163 if (t
& VT_CONSTANT
)
3164 pstrcat(buf1
, buf_size
, "const ");
3165 if (t
& VT_VOLATILE
)
3166 pstrcat(buf1
, buf_size
, "volatile ");
3168 pstrcat(buf1
, sizeof(buf1
), varstr
);
3169 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3173 pstrcat(buf
, buf_size
, " ");
3174 pstrcat(buf
, buf_size
, varstr
);
3179 /* verify type compatibility to store vtop in 'dt' type, and generate
3181 static void gen_assign_cast(CType
*dt
)
3183 CType
*st
, *type1
, *type2
;
3184 char buf1
[256], buf2
[256];
3185 int dbt
, sbt
, qualwarn
, lvl
;
3187 st
= &vtop
->type
; /* source type */
3188 dbt
= dt
->t
& VT_BTYPE
;
3189 sbt
= st
->t
& VT_BTYPE
;
3190 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
3191 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
3192 ; /* It is Ok if both are void */
3194 tcc_error("cannot cast from/to void");
3196 if (dt
->t
& VT_CONSTANT
)
3197 tcc_warning("assignment of read-only location");
3200 /* special cases for pointers */
3201 /* '0' can also be a pointer */
3202 if (is_null_pointer(vtop
))
3204 /* accept implicit pointer to integer cast with warning */
3205 if (is_integer_btype(sbt
)) {
3206 tcc_warning("assignment makes pointer from integer without a cast");
3209 type1
= pointed_type(dt
);
3211 type2
= pointed_type(st
);
3212 else if (sbt
== VT_FUNC
)
3213 type2
= st
; /* a function is implicitly a function pointer */
3216 if (is_compatible_types(type1
, type2
))
3218 for (qualwarn
= lvl
= 0;; ++lvl
) {
3219 if (((type2
->t
& VT_CONSTANT
) && !(type1
->t
& VT_CONSTANT
)) ||
3220 ((type2
->t
& VT_VOLATILE
) && !(type1
->t
& VT_VOLATILE
)))
3222 dbt
= type1
->t
& (VT_BTYPE
|VT_LONG
);
3223 sbt
= type2
->t
& (VT_BTYPE
|VT_LONG
);
3224 if (dbt
!= VT_PTR
|| sbt
!= VT_PTR
)
3226 type1
= pointed_type(type1
);
3227 type2
= pointed_type(type2
);
3229 if (!is_compatible_unqualified_types(type1
, type2
)) {
3230 if ((dbt
== VT_VOID
|| sbt
== VT_VOID
) && lvl
== 0) {
3231 /* void * can match anything */
3232 } else if (dbt
== sbt
3233 && is_integer_btype(sbt
& VT_BTYPE
)
3234 && IS_ENUM(type1
->t
) + IS_ENUM(type2
->t
)
3235 + !!((type1
->t
^ type2
->t
) & VT_UNSIGNED
) < 2) {
3236 /* Like GCC don't warn by default for merely changes
3237 in pointer target signedness. Do warn for different
3238 base types, though, in particular for unsigned enums
3239 and signed int targets. */
3241 tcc_warning("assignment from incompatible pointer type");
3246 tcc_warning("assignment discards qualifiers from pointer target type");
3252 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
3253 tcc_warning("assignment makes integer from pointer without a cast");
3254 } else if (sbt
== VT_STRUCT
) {
3255 goto case_VT_STRUCT
;
3257 /* XXX: more tests */
3261 if (!is_compatible_unqualified_types(dt
, st
)) {
3263 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
3264 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
3265 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
3272 /* store vtop in lvalue pushed on stack */
3273 ST_FUNC
void vstore(void)
3275 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
3277 ft
= vtop
[-1].type
.t
;
3278 sbt
= vtop
->type
.t
& VT_BTYPE
;
3279 dbt
= ft
& VT_BTYPE
;
3280 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
3281 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
3282 && !(vtop
->type
.t
& VT_BITFIELD
)) {
3283 /* optimize char/short casts */
3284 delayed_cast
= VT_MUSTCAST
;
3285 vtop
->type
.t
= ft
& VT_TYPE
;
3286 /* XXX: factorize */
3287 if (ft
& VT_CONSTANT
)
3288 tcc_warning("assignment of read-only location");
3291 if (!(ft
& VT_BITFIELD
))
3292 gen_assign_cast(&vtop
[-1].type
);
3295 if (sbt
== VT_STRUCT
) {
3296 /* if structure, only generate pointer */
3297 /* structure assignment : generate memcpy */
3298 /* XXX: optimize if small size */
3299 size
= type_size(&vtop
->type
, &align
);
3303 vtop
->type
.t
= VT_PTR
;
3306 /* address of memcpy() */
3309 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3310 else if(!(align
& 3))
3311 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3314 /* Use memmove, rather than memcpy, as dest and src may be same: */
3315 vpush_global_sym(&func_old_type
, TOK_memmove
);
3320 vtop
->type
.t
= VT_PTR
;
3326 /* leave source on stack */
3327 } else if (ft
& VT_BITFIELD
) {
3328 /* bitfield store handling */
3330 /* save lvalue as expression result (example: s.b = s.a = n;) */
3331 vdup(), vtop
[-1] = vtop
[-2];
3333 bit_pos
= BIT_POS(ft
);
3334 bit_size
= BIT_SIZE(ft
);
3335 /* remove bit field info to avoid loops */
3336 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3338 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3339 gen_cast(&vtop
[-1].type
);
3340 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3343 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3344 if (r
== VT_STRUCT
) {
3345 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG
? VT_LLONG
: VT_INT
);
3346 store_packed_bf(bit_pos
, bit_size
);
3348 unsigned long long mask
= (1ULL << bit_size
) - 1;
3349 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3351 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3354 vpushi((unsigned)mask
);
3361 /* duplicate destination */
3364 /* load destination, mask and or with source */
3365 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3366 vpushll(~(mask
<< bit_pos
));
3368 vpushi(~((unsigned)mask
<< bit_pos
));
3373 /* ... and discard */
3376 } else if (dbt
== VT_VOID
) {
3379 #ifdef CONFIG_TCC_BCHECK
3380 /* bound check case */
3381 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3390 #ifdef TCC_TARGET_X86_64
3391 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3393 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3398 r
= gv(rc
); /* generate value */
3399 /* if lvalue was saved on stack, must read it */
3400 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3402 t
= get_reg(RC_INT
);
3408 sv
.r
= VT_LOCAL
| VT_LVAL
;
3409 sv
.c
.i
= vtop
[-1].c
.i
;
3411 vtop
[-1].r
= t
| VT_LVAL
;
3413 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3415 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3416 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3418 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3419 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3421 vtop
[-1].type
.t
= load_type
;
3424 /* convert to int to increment easily */
3425 vtop
->type
.t
= addr_type
;
3431 vtop
[-1].type
.t
= load_type
;
3432 /* XXX: it works because r2 is spilled last ! */
3433 store(vtop
->r2
, vtop
- 1);
3439 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3440 vtop
->r
|= delayed_cast
;
3444 /* post defines POST/PRE add. c is the token ++ or -- */
3445 ST_FUNC
void inc(int post
, int c
)
3448 vdup(); /* save lvalue */
3450 gv_dup(); /* duplicate value */
3455 vpushi(c
- TOK_MID
);
3457 vstore(); /* store value */
3459 vpop(); /* if post op, return saved value */
3462 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3464 /* read the string */
3468 while (tok
== TOK_STR
) {
3469 /* XXX: add \0 handling too ? */
3470 cstr_cat(astr
, tokc
.str
.data
, -1);
3473 cstr_ccat(astr
, '\0');
3476 /* If I is >= 1 and a power of two, returns log2(i)+1.
3477 If I is 0 returns 0. */
3478 static int exact_log2p1(int i
)
3483 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3494 /* Parse __attribute__((...)) GNUC extension. */
3495 static void parse_attribute(AttributeDef
*ad
)
3501 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3506 while (tok
!= ')') {
3507 if (tok
< TOK_IDENT
)
3508 expect("attribute name");
3520 tcc_warning("implicit declaration of function '%s'",
3521 get_tok_str(tok
, &tokc
));
3522 s
= external_global_sym(tok
, &func_old_type
);
3524 ad
->cleanup_func
= s
;
3532 parse_mult_str(&astr
, "section name");
3533 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3540 parse_mult_str(&astr
, "alias(\"target\")");
3541 ad
->alias_target
= /* save string as token, for later */
3542 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3546 case TOK_VISIBILITY1
:
3547 case TOK_VISIBILITY2
:
3549 parse_mult_str(&astr
,
3550 "visibility(\"default|hidden|internal|protected\")");
3551 if (!strcmp (astr
.data
, "default"))
3552 ad
->a
.visibility
= STV_DEFAULT
;
3553 else if (!strcmp (astr
.data
, "hidden"))
3554 ad
->a
.visibility
= STV_HIDDEN
;
3555 else if (!strcmp (astr
.data
, "internal"))
3556 ad
->a
.visibility
= STV_INTERNAL
;
3557 else if (!strcmp (astr
.data
, "protected"))
3558 ad
->a
.visibility
= STV_PROTECTED
;
3560 expect("visibility(\"default|hidden|internal|protected\")");
3569 if (n
<= 0 || (n
& (n
- 1)) != 0)
3570 tcc_error("alignment must be a positive power of two");
3575 ad
->a
.aligned
= exact_log2p1(n
);
3576 if (n
!= 1 << (ad
->a
.aligned
- 1))
3577 tcc_error("alignment of %d is larger than implemented", n
);
3589 /* currently, no need to handle it because tcc does not
3590 track unused objects */
3594 ad
->f
.func_noreturn
= 1;
3599 ad
->f
.func_call
= FUNC_CDECL
;
3604 ad
->f
.func_call
= FUNC_STDCALL
;
3606 #ifdef TCC_TARGET_I386
3616 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3622 ad
->f
.func_call
= FUNC_FASTCALLW
;
3629 ad
->attr_mode
= VT_LLONG
+ 1;
3632 ad
->attr_mode
= VT_BYTE
+ 1;
3635 ad
->attr_mode
= VT_SHORT
+ 1;
3639 ad
->attr_mode
= VT_INT
+ 1;
3642 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3649 ad
->a
.dllexport
= 1;
3651 case TOK_NODECORATE
:
3652 ad
->a
.nodecorate
= 1;
3655 ad
->a
.dllimport
= 1;
3658 if (tcc_state
->warn_unsupported
)
3659 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3660 /* skip parameters */
3662 int parenthesis
= 0;
3666 else if (tok
== ')')
3669 } while (parenthesis
&& tok
!= -1);
3682 static Sym
* find_field (CType
*type
, int v
, int *cumofs
)
3686 while ((s
= s
->next
) != NULL
) {
3687 if ((s
->v
& SYM_FIELD
) &&
3688 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3689 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3690 Sym
*ret
= find_field (&s
->type
, v
, cumofs
);
3702 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3704 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3705 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3706 int pcc
= !tcc_state
->ms_bitfields
;
3707 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3714 prevbt
= VT_STRUCT
; /* make it never match */
3719 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3720 if (f
->type
.t
& VT_BITFIELD
)
3721 bit_size
= BIT_SIZE(f
->type
.t
);
3724 size
= type_size(&f
->type
, &align
);
3725 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3728 if (pcc
&& bit_size
== 0) {
3729 /* in pcc mode, packing does not affect zero-width bitfields */
3732 /* in pcc mode, attribute packed overrides if set. */
3733 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3736 /* pragma pack overrides align if lesser and packs bitfields always */
3739 if (pragma_pack
< align
)
3740 align
= pragma_pack
;
3741 /* in pcc mode pragma pack also overrides individual align */
3742 if (pcc
&& pragma_pack
< a
)
3746 /* some individual align was specified */
3750 if (type
->ref
->type
.t
== VT_UNION
) {
3751 if (pcc
&& bit_size
>= 0)
3752 size
= (bit_size
+ 7) >> 3;
3757 } else if (bit_size
< 0) {
3759 c
+= (bit_pos
+ 7) >> 3;
3760 c
= (c
+ align
- 1) & -align
;
3769 /* A bit-field. Layout is more complicated. There are two
3770 options: PCC (GCC) compatible and MS compatible */
3772 /* In PCC layout a bit-field is placed adjacent to the
3773 preceding bit-fields, except if:
3775 - an individual alignment was given
3776 - it would overflow its base type container and
3777 there is no packing */
3778 if (bit_size
== 0) {
3780 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3782 } else if (f
->a
.aligned
) {
3784 } else if (!packed
) {
3786 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3787 if (ofs
> size
/ align
)
3791 /* in pcc mode, long long bitfields have type int if they fit */
3792 if (size
== 8 && bit_size
<= 32)
3793 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3795 while (bit_pos
>= align
* 8)
3796 c
+= align
, bit_pos
-= align
* 8;
3799 /* In PCC layout named bit-fields influence the alignment
3800 of the containing struct using the base types alignment,
3801 except for packed fields (which here have correct align). */
3802 if (f
->v
& SYM_FIRST_ANOM
3803 // && bit_size // ??? gcc on ARM/rpi does that
3808 bt
= f
->type
.t
& VT_BTYPE
;
3809 if ((bit_pos
+ bit_size
> size
* 8)
3810 || (bit_size
> 0) == (bt
!= prevbt
)
3812 c
= (c
+ align
- 1) & -align
;
3815 /* In MS bitfield mode a bit-field run always uses
3816 at least as many bits as the underlying type.
3817 To start a new run it's also required that this
3818 or the last bit-field had non-zero width. */
3819 if (bit_size
|| prev_bit_size
)
3822 /* In MS layout the records alignment is normally
3823 influenced by the field, except for a zero-width
3824 field at the start of a run (but by further zero-width
3825 fields it is again). */
3826 if (bit_size
== 0 && prevbt
!= bt
)
3829 prev_bit_size
= bit_size
;
3832 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3833 | (bit_pos
<< VT_STRUCT_SHIFT
);
3834 bit_pos
+= bit_size
;
3836 if (align
> maxalign
)
3840 printf("set field %s offset %-2d size %-2d align %-2d",
3841 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3842 if (f
->type
.t
& VT_BITFIELD
) {
3843 printf(" pos %-2d bits %-2d",
3856 c
+= (bit_pos
+ 7) >> 3;
3858 /* store size and alignment */
3859 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3863 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
3864 /* can happen if individual align for some member was given. In
3865 this case MSVC ignores maxalign when aligning the size */
3870 c
= (c
+ a
- 1) & -a
;
3874 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3877 /* check whether we can access bitfields by their type */
3878 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3882 if (0 == (f
->type
.t
& VT_BITFIELD
))
3886 bit_size
= BIT_SIZE(f
->type
.t
);
3889 bit_pos
= BIT_POS(f
->type
.t
);
3890 size
= type_size(&f
->type
, &align
);
3891 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3894 /* try to access the field using a different type */
3895 c0
= -1, s
= align
= 1;
3897 px
= f
->c
* 8 + bit_pos
;
3898 cx
= (px
>> 3) & -align
;
3899 px
= px
- (cx
<< 3);
3902 s
= (px
+ bit_size
+ 7) >> 3;
3912 s
= type_size(&t
, &align
);
3916 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3917 /* update offset and bit position */
3920 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3921 | (bit_pos
<< VT_STRUCT_SHIFT
);
3925 printf("FIX field %s offset %-2d size %-2d align %-2d "
3926 "pos %-2d bits %-2d\n",
3927 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3928 cx
, s
, align
, px
, bit_size
);
3931 /* fall back to load/store single-byte wise */
3932 f
->auxtype
= VT_STRUCT
;
3934 printf("FIX field %s : load byte-wise\n",
3935 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3941 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3942 static void struct_decl(CType
*type
, int u
)
3944 int v
, c
, size
, align
, flexible
;
3945 int bit_size
, bsize
, bt
;
3947 AttributeDef ad
, ad1
;
3950 memset(&ad
, 0, sizeof ad
);
3952 parse_attribute(&ad
);
3956 /* struct already defined ? return it */
3958 expect("struct/union/enum name");
3960 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3963 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3965 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3970 /* Record the original enum/struct/union token. */
3971 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3973 /* we put an undefined size for struct/union */
3974 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3975 s
->r
= 0; /* default alignment is zero as gcc */
3977 type
->t
= s
->type
.t
;
3983 tcc_error("struct/union/enum already defined");
3985 /* cannot be empty */
3986 /* non empty enums are not allowed */
3989 long long ll
= 0, pl
= 0, nl
= 0;
3992 /* enum symbols have static storage */
3993 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3997 expect("identifier");
3999 if (ss
&& !local_stack
)
4000 tcc_error("redefinition of enumerator '%s'",
4001 get_tok_str(v
, NULL
));
4005 ll
= expr_const64();
4007 ss
= sym_push(v
, &t
, VT_CONST
, 0);
4009 *ps
= ss
, ps
= &ss
->next
;
4018 /* NOTE: we accept a trailing comma */
4023 /* set integral type of the enum */
4026 if (pl
!= (unsigned)pl
)
4027 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4029 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
4030 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4031 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
4033 /* set type for enum members */
4034 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
4036 if (ll
== (int)ll
) /* default is int if it fits */
4038 if (t
.t
& VT_UNSIGNED
) {
4039 ss
->type
.t
|= VT_UNSIGNED
;
4040 if (ll
== (unsigned)ll
)
4043 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
)
4044 | (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4049 while (tok
!= '}') {
4050 if (!parse_btype(&btype
, &ad1
)) {
4056 tcc_error("flexible array member '%s' not at the end of struct",
4057 get_tok_str(v
, NULL
));
4063 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
4065 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
4066 expect("identifier");
4068 int v
= btype
.ref
->v
;
4069 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
4070 if (tcc_state
->ms_extensions
== 0)
4071 expect("identifier");
4075 if (type_size(&type1
, &align
) < 0) {
4076 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
4079 tcc_error("field '%s' has incomplete type",
4080 get_tok_str(v
, NULL
));
4082 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
4083 (type1
.t
& VT_BTYPE
) == VT_VOID
||
4084 (type1
.t
& VT_STORAGE
))
4085 tcc_error("invalid type for '%s'",
4086 get_tok_str(v
, NULL
));
4090 bit_size
= expr_const();
4091 /* XXX: handle v = 0 case for messages */
4093 tcc_error("negative width in bit-field '%s'",
4094 get_tok_str(v
, NULL
));
4095 if (v
&& bit_size
== 0)
4096 tcc_error("zero width for bit-field '%s'",
4097 get_tok_str(v
, NULL
));
4098 parse_attribute(&ad1
);
4100 size
= type_size(&type1
, &align
);
4101 if (bit_size
>= 0) {
4102 bt
= type1
.t
& VT_BTYPE
;
4108 tcc_error("bitfields must have scalar type");
4110 if (bit_size
> bsize
) {
4111 tcc_error("width of '%s' exceeds its type",
4112 get_tok_str(v
, NULL
));
4113 } else if (bit_size
== bsize
4114 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
4115 /* no need for bit fields */
4117 } else if (bit_size
== 64) {
4118 tcc_error("field width 64 not implemented");
4120 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
4122 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4125 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
4126 /* Remember we've seen a real field to check
4127 for placement of flexible array member. */
4130 /* If member is a struct or bit-field, enforce
4131 placing into the struct (as anonymous). */
4133 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
4138 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
4143 if (tok
== ';' || tok
== TOK_EOF
)
4150 parse_attribute(&ad
);
4151 struct_layout(type
, &ad
);
4156 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
4158 merge_symattr(&ad
->a
, &s
->a
);
4159 merge_funcattr(&ad
->f
, &s
->f
);
4162 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4163 are added to the element type, copied because it could be a typedef. */
4164 static void parse_btype_qualify(CType
*type
, int qualifiers
)
4166 while (type
->t
& VT_ARRAY
) {
4167 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
4168 type
= &type
->ref
->type
;
4170 type
->t
|= qualifiers
;
4173 /* return 0 if no type declaration. otherwise, return the basic type
4176 static int parse_btype(CType
*type
, AttributeDef
*ad
)
4178 int t
, u
, bt
, st
, type_found
, typespec_found
, g
, n
;
4182 memset(ad
, 0, sizeof(AttributeDef
));
4192 /* currently, we really ignore extension */
4202 if (u
== VT_SHORT
|| u
== VT_LONG
) {
4203 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
4204 tmbt
: tcc_error("too many basic types");
4207 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
4212 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4229 memset(&ad1
, 0, sizeof(AttributeDef
));
4230 if (parse_btype(&type1
, &ad1
)) {
4231 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4233 n
= 1 << (ad1
.a
.aligned
- 1);
4235 type_size(&type1
, &n
);
4238 if (n
<= 0 || (n
& (n
- 1)) != 0)
4239 tcc_error("alignment must be a positive power of two");
4242 ad
->a
.aligned
= exact_log2p1(n
);
4246 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4247 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4248 } else if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4249 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LLONG
;
4256 #ifdef TCC_TARGET_ARM64
4258 /* GCC's __uint128_t appears in some Linux header files. Make it a
4259 synonym for long double to get the size and alignment right. */
4270 if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4271 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4279 struct_decl(&type1
, VT_ENUM
);
4282 type
->ref
= type1
.ref
;
4285 struct_decl(&type1
, VT_STRUCT
);
4288 struct_decl(&type1
, VT_UNION
);
4291 /* type modifiers */
4296 parse_btype_qualify(type
, VT_CONSTANT
);
4304 parse_btype_qualify(type
, VT_VOLATILE
);
4311 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4312 tcc_error("signed and unsigned modifier");
4325 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4326 tcc_error("signed and unsigned modifier");
4327 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4343 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4344 tcc_error("multiple storage classes");
4355 /* currently, no need to handle it because tcc does not
4356 track unused objects */
4359 /* GNUC attribute */
4360 case TOK_ATTRIBUTE1
:
4361 case TOK_ATTRIBUTE2
:
4362 parse_attribute(ad
);
4363 if (ad
->attr_mode
) {
4364 u
= ad
->attr_mode
-1;
4365 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4373 parse_expr_type(&type1
);
4374 /* remove all storage modifiers except typedef */
4375 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4377 sym_to_attr(ad
, type1
.ref
);
4383 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4387 if (tok
== ':' && !in_generic
) {
4388 /* ignore if it's a label */
4393 t
&= ~(VT_BTYPE
|VT_LONG
);
4394 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4395 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4396 type
->ref
= s
->type
.ref
;
4398 parse_btype_qualify(type
, t
);
4400 /* get attributes from typedef */
4409 if (tcc_state
->char_is_unsigned
) {
4410 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4413 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4414 bt
= t
& (VT_BTYPE
|VT_LONG
);
4416 t
|= LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
;
4417 #ifdef TCC_TARGET_PE
4418 if (bt
== VT_LDOUBLE
)
4419 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_DOUBLE
;
4425 /* convert a function parameter type (array to pointer and function to
4426 function pointer) */
4427 static inline void convert_parameter_type(CType
*pt
)
4429 /* remove const and volatile qualifiers (XXX: const could be used
4430 to indicate a const function parameter */
4431 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4432 /* array must be transformed to pointer according to ANSI C */
4434 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4439 ST_FUNC
void parse_asm_str(CString
*astr
)
4442 parse_mult_str(astr
, "string constant");
4445 /* Parse an asm label and return the token */
4446 static int asm_label_instr(void)
4452 parse_asm_str(&astr
);
4455 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4457 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4462 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4464 int n
, l
, t1
, arg_size
, align
, unused_align
;
4465 Sym
**plast
, *s
, *first
;
4470 /* function type, or recursive declarator (return if so) */
4472 if (td
&& !(td
& TYPE_ABSTRACT
))
4476 else if (parse_btype(&pt
, &ad1
))
4479 merge_attr (ad
, &ad1
);
4488 /* read param name and compute offset */
4489 if (l
!= FUNC_OLD
) {
4490 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4492 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4493 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4494 tcc_error("parameter declared as void");
4498 expect("identifier");
4499 pt
.t
= VT_VOID
; /* invalid type */
4502 convert_parameter_type(&pt
);
4503 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4504 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4510 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4515 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4516 tcc_error("invalid type");
4519 /* if no parameters, then old type prototype */
4522 /* NOTE: const is ignored in returned type as it has a special
4523 meaning in gcc / C++ */
4524 type
->t
&= ~VT_CONSTANT
;
4525 /* some ancient pre-K&R C allows a function to return an array
4526 and the array brackets to be put after the arguments, such
4527 that "int c()[]" means something like "int[] c()" */
4530 skip(']'); /* only handle simple "[]" */
4533 /* we push a anonymous symbol which will contain the function prototype */
4534 ad
->f
.func_args
= arg_size
;
4535 ad
->f
.func_type
= l
;
4536 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4542 } else if (tok
== '[') {
4543 int saved_nocode_wanted
= nocode_wanted
;
4544 /* array definition */
4547 /* XXX The optional type-quals and static should only be accepted
4548 in parameter decls. The '*' as well, and then even only
4549 in prototypes (not function defs). */
4551 case TOK_RESTRICT1
: case TOK_RESTRICT2
: case TOK_RESTRICT3
:
4566 if (!local_stack
|| (storage
& VT_STATIC
))
4567 vpushi(expr_const());
4569 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4570 length must always be evaluated, even under nocode_wanted,
4571 so that its size slot is initialized (e.g. under sizeof
4576 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4579 tcc_error("invalid array size");
4581 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4582 tcc_error("size of variable length array should be an integer");
4588 /* parse next post type */
4589 post_type(type
, ad
, storage
, 0);
4591 if ((type
->t
& VT_BTYPE
) == VT_FUNC
)
4592 tcc_error("declaration of an array of functions");
4593 if ((type
->t
& VT_BTYPE
) == VT_VOID
4594 || type_size(type
, &unused_align
) < 0)
4595 tcc_error("declaration of an array of incomplete type elements");
4597 t1
|= type
->t
& VT_VLA
;
4601 tcc_error("need explicit inner array size in VLAs");
4602 loc
-= type_size(&int_type
, &align
);
4606 vla_runtime_type_size(type
, &align
);
4608 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4614 nocode_wanted
= saved_nocode_wanted
;
4616 /* we push an anonymous symbol which will contain the array
4618 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4619 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4625 /* Parse a type declarator (except basic type), and return the type
4626 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4627 expected. 'type' should contain the basic type. 'ad' is the
4628 attribute definition of the basic type. It can be modified by
4629 type_decl(). If this (possibly abstract) declarator is a pointer chain
4630 it returns the innermost pointed to type (equals *type, but is a different
4631 pointer), otherwise returns type itself, that's used for recursive calls. */
4632 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4635 int qualifiers
, storage
;
4637 /* recursive type, remove storage bits first, apply them later again */
4638 storage
= type
->t
& VT_STORAGE
;
4639 type
->t
&= ~VT_STORAGE
;
4642 while (tok
== '*') {
4650 qualifiers
|= VT_CONSTANT
;
4655 qualifiers
|= VT_VOLATILE
;
4661 /* XXX: clarify attribute handling */
4662 case TOK_ATTRIBUTE1
:
4663 case TOK_ATTRIBUTE2
:
4664 parse_attribute(ad
);
4668 type
->t
|= qualifiers
;
4670 /* innermost pointed to type is the one for the first derivation */
4671 ret
= pointed_type(type
);
4675 /* This is possibly a parameter type list for abstract declarators
4676 ('int ()'), use post_type for testing this. */
4677 if (!post_type(type
, ad
, 0, td
)) {
4678 /* It's not, so it's a nested declarator, and the post operations
4679 apply to the innermost pointed to type (if any). */
4680 /* XXX: this is not correct to modify 'ad' at this point, but
4681 the syntax is not clear */
4682 parse_attribute(ad
);
4683 post
= type_decl(type
, ad
, v
, td
);
4687 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4688 /* type identifier */
4693 if (!(td
& TYPE_ABSTRACT
))
4694 expect("identifier");
4697 post_type(post
, ad
, storage
, 0);
4698 parse_attribute(ad
);
4703 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4704 ST_FUNC
int lvalue_type(int t
)
4709 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4711 else if (bt
== VT_SHORT
)
4715 if (t
& VT_UNSIGNED
)
4716 r
|= VT_LVAL_UNSIGNED
;
4720 /* indirection with full error checking and bound check */
4721 ST_FUNC
void indir(void)
4723 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4724 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4728 if (vtop
->r
& VT_LVAL
)
4730 vtop
->type
= *pointed_type(&vtop
->type
);
4731 /* Arrays and functions are never lvalues */
4732 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4733 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4734 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4735 /* if bound checking, the referenced pointer must be checked */
4736 #ifdef CONFIG_TCC_BCHECK
4737 if (tcc_state
->do_bounds_check
)
4738 vtop
->r
|= VT_MUSTBOUND
;
4743 /* pass a parameter to a function and do type checking and casting */
4744 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4749 func_type
= func
->f
.func_type
;
4750 if (func_type
== FUNC_OLD
||
4751 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4752 /* default casting : only need to convert float to double */
4753 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4754 gen_cast_s(VT_DOUBLE
);
4755 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4756 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4757 type
.ref
= vtop
->type
.ref
;
4760 } else if (arg
== NULL
) {
4761 tcc_error("too many arguments to function");
4764 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4765 gen_assign_cast(&type
);
4769 /* parse an expression and return its type without any side effect. */
4770 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4779 /* parse an expression of the form '(type)' or '(expr)' and return its
4781 static void parse_expr_type(CType
*type
)
4787 if (parse_btype(type
, &ad
)) {
4788 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4790 expr_type(type
, gexpr
);
4795 static void parse_type(CType
*type
)
4800 if (!parse_btype(type
, &ad
)) {
4803 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4806 static void parse_builtin_params(int nc
, const char *args
)
4813 while ((c
= *args
++)) {
4817 case 'e': expr_eq(); continue;
4818 case 't': parse_type(&t
); vpush(&t
); continue;
4819 default: tcc_error("internal error"); break;
4827 ST_FUNC
void unary(void)
4829 int n
, t
, align
, size
, r
, sizeof_caller
;
4834 sizeof_caller
= in_sizeof
;
4837 /* XXX: GCC 2.95.3 does not generate a table although it should be
4845 #ifdef TCC_TARGET_PE
4846 t
= VT_SHORT
|VT_UNSIGNED
;
4854 vsetc(&type
, VT_CONST
, &tokc
);
4858 t
= VT_INT
| VT_UNSIGNED
;
4864 t
= VT_LLONG
| VT_UNSIGNED
;
4876 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
;
4879 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
| VT_UNSIGNED
;
4881 case TOK___FUNCTION__
:
4883 goto tok_identifier
;
4889 /* special function name identifier */
4890 len
= strlen(funcname
) + 1;
4891 /* generate char[len] type */
4896 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4897 if (!NODATA_WANTED
) {
4898 ptr
= section_ptr_add(data_section
, len
);
4899 memcpy(ptr
, funcname
, len
);
4905 #ifdef TCC_TARGET_PE
4906 t
= VT_SHORT
| VT_UNSIGNED
;
4912 /* string parsing */
4914 if (tcc_state
->char_is_unsigned
)
4915 t
= VT_BYTE
| VT_UNSIGNED
;
4917 if (tcc_state
->warn_write_strings
)
4922 memset(&ad
, 0, sizeof(AttributeDef
));
4923 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4928 if (parse_btype(&type
, &ad
)) {
4929 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4931 /* check ISOC99 compound literal */
4933 /* data is allocated locally by default */
4938 /* all except arrays are lvalues */
4939 if (!(type
.t
& VT_ARRAY
))
4940 r
|= lvalue_type(type
.t
);
4941 memset(&ad
, 0, sizeof(AttributeDef
));
4942 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4944 if (sizeof_caller
) {
4951 } else if (tok
== '{') {
4952 int saved_nocode_wanted
= nocode_wanted
;
4954 tcc_error("expected constant");
4955 /* save all registers */
4957 /* statement expression : we do not accept break/continue
4958 inside as GCC does. We do retain the nocode_wanted state,
4959 as statement expressions can't ever be entered from the
4960 outside, so any reactivation of code emission (from labels
4961 or loop heads) can be disabled again after the end of it. */
4963 nocode_wanted
= saved_nocode_wanted
;
4978 /* functions names must be treated as function pointers,
4979 except for unary '&' and sizeof. Since we consider that
4980 functions are not lvalues, we only have to handle it
4981 there and in function calls. */
4982 /* arrays can also be used although they are not lvalues */
4983 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4984 !(vtop
->type
.t
& VT_ARRAY
))
4986 mk_pointer(&vtop
->type
);
4992 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4993 gen_cast_s(VT_BOOL
);
4994 vtop
->c
.i
= !vtop
->c
.i
;
4995 } else if (vtop
->r
== VT_CMP
) {
4997 n
= vtop
->jfalse
, vtop
->jfalse
= vtop
->jtrue
, vtop
->jtrue
= n
;
5012 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
5013 tcc_error("pointer not accepted for unary plus");
5014 /* In order to force cast, we add zero, except for floating point
5015 where we really need an noop (otherwise -0.0 will be transformed
5017 if (!is_float(vtop
->type
.t
)) {
5029 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
5031 if (vtop
[1].r
& VT_SYM
)
5032 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
5033 size
= type_size(&type
, &align
);
5034 if (s
&& s
->a
.aligned
)
5035 align
= 1 << (s
->a
.aligned
- 1);
5036 if (t
== TOK_SIZEOF
) {
5037 if (!(type
.t
& VT_VLA
)) {
5039 tcc_error("sizeof applied to an incomplete type");
5042 vla_runtime_type_size(&type
, &align
);
5047 vtop
->type
.t
|= VT_UNSIGNED
;
5050 case TOK_builtin_expect
:
5051 /* __builtin_expect is a no-op for now */
5052 parse_builtin_params(0, "ee");
5055 case TOK_builtin_types_compatible_p
:
5056 parse_builtin_params(0, "tt");
5057 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5058 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5059 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
5063 case TOK_builtin_choose_expr
:
5090 case TOK_builtin_constant_p
:
5091 parse_builtin_params(1, "e");
5092 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5096 case TOK_builtin_frame_address
:
5097 case TOK_builtin_return_address
:
5103 if (tok
!= TOK_CINT
) {
5104 tcc_error("%s only takes positive integers",
5105 tok1
== TOK_builtin_return_address
?
5106 "__builtin_return_address" :
5107 "__builtin_frame_address");
5109 level
= (uint32_t)tokc
.i
;
5114 vset(&type
, VT_LOCAL
, 0); /* local frame */
5116 mk_pointer(&vtop
->type
);
5117 indir(); /* -> parent frame */
5119 if (tok1
== TOK_builtin_return_address
) {
5120 // assume return address is just above frame pointer on stack
5123 mk_pointer(&vtop
->type
);
5128 #ifdef TCC_TARGET_X86_64
5129 #ifdef TCC_TARGET_PE
5130 case TOK_builtin_va_start
:
5131 parse_builtin_params(0, "ee");
5132 r
= vtop
->r
& VT_VALMASK
;
5136 tcc_error("__builtin_va_start expects a local variable");
5138 vtop
->type
= char_pointer_type
;
5143 case TOK_builtin_va_arg_types
:
5144 parse_builtin_params(0, "t");
5145 vpushi(classify_x86_64_va_arg(&vtop
->type
));
5152 #ifdef TCC_TARGET_ARM64
5153 case TOK___va_start
: {
5154 parse_builtin_params(0, "ee");
5158 vtop
->type
.t
= VT_VOID
;
5161 case TOK___va_arg
: {
5162 parse_builtin_params(0, "et");
5170 case TOK___arm64_clear_cache
: {
5171 parse_builtin_params(0, "ee");
5174 vtop
->type
.t
= VT_VOID
;
5178 /* pre operations */
5189 t
= vtop
->type
.t
& VT_BTYPE
;
5191 /* In IEEE negate(x) isn't subtract(0,x), but rather
5195 vtop
->c
.f
= -1.0 * 0.0;
5196 else if (t
== VT_DOUBLE
)
5197 vtop
->c
.d
= -1.0 * 0.0;
5199 vtop
->c
.ld
= -1.0 * 0.0;
5207 goto tok_identifier
;
5209 /* allow to take the address of a label */
5210 if (tok
< TOK_UIDENT
)
5211 expect("label identifier");
5212 s
= label_find(tok
);
5214 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5216 if (s
->r
== LABEL_DECLARED
)
5217 s
->r
= LABEL_FORWARD
;
5220 s
->type
.t
= VT_VOID
;
5221 mk_pointer(&s
->type
);
5222 s
->type
.t
|= VT_STATIC
;
5224 vpushsym(&s
->type
, s
);
5230 CType controlling_type
;
5231 int has_default
= 0;
5234 TokenString
*str
= NULL
;
5235 int saved_const_wanted
= const_wanted
;
5240 expr_type(&controlling_type
, expr_eq
);
5241 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
5242 if ((controlling_type
.t
& VT_BTYPE
) == VT_FUNC
)
5243 mk_pointer(&controlling_type
);
5244 const_wanted
= saved_const_wanted
;
5248 if (tok
== TOK_DEFAULT
) {
5250 tcc_error("too many 'default'");
5256 AttributeDef ad_tmp
;
5261 parse_btype(&cur_type
, &ad_tmp
);
5264 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
5265 if (compare_types(&controlling_type
, &cur_type
, 0)) {
5267 tcc_error("type match twice");
5277 skip_or_save_block(&str
);
5279 skip_or_save_block(NULL
);
5286 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
5287 tcc_error("type '%s' does not match any association", buf
);
5289 begin_macro(str
, 1);
5298 // special qnan , snan and infinity values
5303 vtop
->type
.t
= VT_FLOAT
;
5308 goto special_math_val
;
5311 goto special_math_val
;
5318 expect("identifier");
5320 if (!s
|| IS_ASM_SYM(s
)) {
5321 const char *name
= get_tok_str(t
, NULL
);
5323 tcc_error("'%s' undeclared", name
);
5324 /* for simple function calls, we tolerate undeclared
5325 external reference to int() function */
5326 if (tcc_state
->warn_implicit_function_declaration
5327 #ifdef TCC_TARGET_PE
5328 /* people must be warned about using undeclared WINAPI functions
5329 (which usually start with uppercase letter) */
5330 || (name
[0] >= 'A' && name
[0] <= 'Z')
5333 tcc_warning("implicit declaration of function '%s'", name
);
5334 s
= external_global_sym(t
, &func_old_type
);
5338 /* A symbol that has a register is a local register variable,
5339 which starts out as VT_LOCAL value. */
5340 if ((r
& VT_VALMASK
) < VT_CONST
)
5341 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5343 vset(&s
->type
, r
, s
->c
);
5344 /* Point to s as backpointer (even without r&VT_SYM).
5345 Will be used by at least the x86 inline asm parser for
5351 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5352 vtop
->c
.i
= s
->enum_val
;
5357 /* post operations */
5359 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5362 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5363 int qualifiers
, cumofs
= 0;
5365 if (tok
== TOK_ARROW
)
5367 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5370 /* expect pointer on structure */
5371 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5372 expect("struct or union");
5373 if (tok
== TOK_CDOUBLE
)
5374 expect("field name");
5376 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5377 expect("field name");
5378 s
= find_field(&vtop
->type
, tok
, &cumofs
);
5380 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5381 /* add field offset to pointer */
5382 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5383 vpushi(cumofs
+ s
->c
);
5385 /* change type to field type, and set to lvalue */
5386 vtop
->type
= s
->type
;
5387 vtop
->type
.t
|= qualifiers
;
5388 /* an array is never an lvalue */
5389 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5390 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5391 #ifdef CONFIG_TCC_BCHECK
5392 /* if bound checking, the referenced pointer must be checked */
5393 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5394 vtop
->r
|= VT_MUSTBOUND
;
5398 } else if (tok
== '[') {
5404 } else if (tok
== '(') {
5407 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5410 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5411 /* pointer test (no array accepted) */
5412 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5413 vtop
->type
= *pointed_type(&vtop
->type
);
5414 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5418 expect("function pointer");
5421 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5423 /* get return type */
5426 sa
= s
->next
; /* first parameter */
5427 nb_args
= regsize
= 0;
5429 /* compute first implicit argument if a structure is returned */
5430 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5431 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5432 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5433 &ret_align
, ®size
);
5435 /* get some space for the returned structure */
5436 size
= type_size(&s
->type
, &align
);
5437 #ifdef TCC_TARGET_ARM64
5438 /* On arm64, a small struct is return in registers.
5439 It is much easier to write it to memory if we know
5440 that we are allowed to write some extra bytes, so
5441 round the allocated space up to a power of 2: */
5443 while (size
& (size
- 1))
5444 size
= (size
| (size
- 1)) + 1;
5446 loc
= (loc
- size
) & -align
;
5448 ret
.r
= VT_LOCAL
| VT_LVAL
;
5449 /* pass it as 'int' to avoid structure arg passing
5451 vseti(VT_LOCAL
, loc
);
5461 /* return in register */
5462 if (is_float(ret
.type
.t
)) {
5463 ret
.r
= reg_fret(ret
.type
.t
);
5464 #ifdef TCC_TARGET_X86_64
5465 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5469 #ifndef TCC_TARGET_ARM64
5470 #ifdef TCC_TARGET_X86_64
5471 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5473 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5484 gfunc_param_typed(s
, sa
);
5494 tcc_error("too few arguments to function");
5496 gfunc_call(nb_args
);
5499 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5500 vsetc(&ret
.type
, r
, &ret
.c
);
5501 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5504 /* handle packed struct return */
5505 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5508 size
= type_size(&s
->type
, &align
);
5509 /* We're writing whole regs often, make sure there's enough
5510 space. Assume register size is power of 2. */
5511 if (regsize
> align
)
5513 loc
= (loc
- size
) & -align
;
5517 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5521 if (--ret_nregs
== 0)
5525 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5527 if (s
->f
.func_noreturn
)
5535 ST_FUNC
void expr_prod(void)
5540 while (tok
== '*' || tok
== '/' || tok
== '%') {
5548 ST_FUNC
void expr_sum(void)
5553 while (tok
== '+' || tok
== '-') {
5561 static void expr_shift(void)
5566 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5574 static void expr_cmp(void)
5579 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5580 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5588 static void expr_cmpeq(void)
5593 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5601 static void expr_and(void)
5604 while (tok
== '&') {
5611 static void expr_xor(void)
5614 while (tok
== '^') {
5621 static void expr_or(void)
5624 while (tok
== '|') {
5631 static int condition_3way(void);
5633 static void expr_landor(void(*e_fn
)(void), int e_op
, int i
)
5635 int t
= 0, cc
= 1, f
= 0, c
;
5637 c
= f
? i
: condition_3way();
5639 save_regs(1), cc
= 0;
5640 } else if (c
!= i
) {
5641 nocode_wanted
++, f
= 1;
5663 static void expr_land(void)
5666 if (tok
== TOK_LAND
)
5667 expr_landor(expr_or
, TOK_LAND
, 1);
5670 static void expr_lor(void)
5674 expr_landor(expr_land
, TOK_LOR
, 0);
5677 /* Assuming vtop is a value used in a conditional context
5678 (i.e. compared with zero) return 0 if it's false, 1 if
5679 true and -1 if it can't be statically determined. */
5680 static int condition_3way(void)
5683 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5684 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5686 gen_cast_s(VT_BOOL
);
5693 static int is_cond_bool(SValue
*sv
)
5695 if ((sv
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
5696 && (sv
->type
.t
& VT_BTYPE
) == VT_INT
)
5697 return (unsigned)sv
->c
.i
< 2;
5698 if (sv
->r
== VT_CMP
)
5703 static void expr_cond(void)
5705 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5707 CType type
, type1
, type2
;
5713 c
= condition_3way();
5714 g
= (tok
== ':' && gnu_ext
);
5724 /* needed to avoid having different registers saved in
5727 if (is_float(vtop
->type
.t
)) {
5729 #ifdef TCC_TARGET_X86_64
5730 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5741 ncw_prev
= nocode_wanted
;
5748 if (c
< 0 && vtop
->r
== VT_CMP
) {
5754 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5755 mk_pointer(&vtop
->type
);
5757 sv
= *vtop
; /* save value to handle it later */
5758 vtop
--; /* no vpop so that FP stack is not flushed */
5768 nocode_wanted
= ncw_prev
;
5774 if (c
< 0 && is_cond_bool(vtop
) && is_cond_bool(&sv
)) {
5775 if (sv
.r
== VT_CMP
) {
5786 nocode_wanted
= ncw_prev
;
5787 // tcc_warning("two conditions expr_cond");
5791 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5792 mk_pointer(&vtop
->type
);
5795 bt1
= t1
& VT_BTYPE
;
5797 bt2
= t2
& VT_BTYPE
;
5800 /* cast operands to correct type according to ISOC rules */
5801 if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5802 type
.t
= VT_VOID
; /* NOTE: as an extension, we accept void on only one side */
5803 } else if (is_float(bt1
) || is_float(bt2
)) {
5804 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5805 type
.t
= VT_LDOUBLE
;
5807 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5812 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5813 /* cast to biggest op */
5814 type
.t
= VT_LLONG
| VT_LONG
;
5815 if (bt1
== VT_LLONG
)
5817 if (bt2
== VT_LLONG
)
5819 /* convert to unsigned if it does not fit in a long long */
5820 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5821 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5822 type
.t
|= VT_UNSIGNED
;
5823 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5824 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
5825 /* If one is a null ptr constant the result type
5827 if (is_null_pointer (vtop
)) type
= type1
;
5828 else if (is_null_pointer (&sv
)) type
= type2
;
5829 else if (bt1
!= bt2
)
5830 tcc_error("incompatible types in conditional expressions");
5832 CType
*pt1
= pointed_type(&type1
);
5833 CType
*pt2
= pointed_type(&type2
);
5834 int pbt1
= pt1
->t
& VT_BTYPE
;
5835 int pbt2
= pt2
->t
& VT_BTYPE
;
5836 int newquals
, copied
= 0;
5837 /* pointers to void get preferred, otherwise the
5838 pointed to types minus qualifs should be compatible */
5839 type
= (pbt1
== VT_VOID
) ? type1
: type2
;
5840 if (pbt1
!= VT_VOID
&& pbt2
!= VT_VOID
) {
5841 if(!compare_types(pt1
, pt2
, 1/*unqualif*/))
5842 tcc_warning("pointer type mismatch in conditional expression\n");
5844 /* combine qualifs */
5845 newquals
= ((pt1
->t
| pt2
->t
) & (VT_CONSTANT
| VT_VOLATILE
));
5846 if ((~pointed_type(&type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
5849 /* copy the pointer target symbol */
5850 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5853 pointed_type(&type
)->t
|= newquals
;
5855 /* pointers to incomplete arrays get converted to
5856 pointers to completed ones if possible */
5857 if (pt1
->t
& VT_ARRAY
5858 && pt2
->t
& VT_ARRAY
5859 && pointed_type(&type
)->ref
->c
< 0
5860 && (pt1
->ref
->c
> 0 || pt2
->ref
->c
> 0))
5863 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5865 pointed_type(&type
)->ref
=
5866 sym_push(SYM_FIELD
, &pointed_type(&type
)->ref
->type
,
5867 0, pointed_type(&type
)->ref
->c
);
5868 pointed_type(&type
)->ref
->c
=
5869 0 < pt1
->ref
->c
? pt1
->ref
->c
: pt2
->ref
->c
;
5872 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5873 /* XXX: test structure compatibility */
5874 type
= bt1
== VT_STRUCT
? type1
: type2
;
5876 /* integer operations */
5877 type
.t
= VT_INT
| (VT_LONG
& (t1
| t2
));
5878 /* convert to unsigned if it does not fit in an integer */
5879 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5880 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5881 type
.t
|= VT_UNSIGNED
;
5883 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5884 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5885 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5887 /* now we convert second operand */
5891 mk_pointer(&vtop
->type
);
5893 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5898 if (is_float(type
.t
)) {
5900 #ifdef TCC_TARGET_X86_64
5901 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5905 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5906 /* for long longs, we use fixed registers to avoid having
5907 to handle a complicated move */
5918 /* this is horrible, but we must also convert first
5924 mk_pointer(&vtop
->type
);
5926 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5932 move_reg(r2
, r1
, type
.t
);
5940 nocode_wanted
= ncw_prev
;
5944 static void expr_eq(void)
5950 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5951 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5952 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5967 ST_FUNC
void gexpr(void)
5978 /* parse a constant expression and return value in vtop. */
5979 static void expr_const1(void)
5988 /* parse an integer constant and return its value. */
5989 static inline int64_t expr_const64(void)
5993 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5994 expect("constant expression");
6000 /* parse an integer constant and return its value.
6001 Complain if it doesn't fit 32bit (signed or unsigned). */
6002 ST_FUNC
int expr_const(void)
6005 int64_t wc
= expr_const64();
6007 if (c
!= wc
&& (unsigned)c
!= wc
)
6008 tcc_error("constant exceeds 32 bit");
6012 /* ------------------------------------------------------------------------- */
6013 /* return from function */
6015 #ifndef TCC_TARGET_ARM64
6016 static void gfunc_return(CType
*func_type
)
6018 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6019 CType type
, ret_type
;
6020 int ret_align
, ret_nregs
, regsize
;
6021 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
6022 &ret_align
, ®size
);
6023 if (0 == ret_nregs
) {
6024 /* if returning structure, must copy it to implicit
6025 first pointer arg location */
6028 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
6031 /* copy structure value to pointer */
6034 /* returning structure packed into registers */
6035 int r
, size
, addr
, align
;
6036 size
= type_size(func_type
,&align
);
6037 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
6038 (vtop
->c
.i
& (ret_align
-1)))
6039 && (align
& (ret_align
-1))) {
6040 loc
= (loc
- size
) & -ret_align
;
6043 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
6047 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
6049 vtop
->type
= ret_type
;
6050 if (is_float(ret_type
.t
))
6051 r
= rc_fret(ret_type
.t
);
6062 if (--ret_nregs
== 0)
6064 /* We assume that when a structure is returned in multiple
6065 registers, their classes are consecutive values of the
6068 vtop
->c
.i
+= regsize
;
6072 } else if (is_float(func_type
->t
)) {
6073 gv(rc_fret(func_type
->t
));
6077 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6081 static void check_func_return(void)
6083 if ((func_vt
.t
& VT_BTYPE
) == VT_VOID
)
6085 if (!strcmp (funcname
, "main")
6086 && (func_vt
.t
& VT_BTYPE
) == VT_INT
) {
6087 /* main returns 0 by default */
6089 gen_assign_cast(&func_vt
);
6090 gfunc_return(&func_vt
);
6092 tcc_warning("function might return no value: '%s'", funcname
);
6096 /* ------------------------------------------------------------------------- */
6099 static int case_cmp(const void *pa
, const void *pb
)
6101 int64_t a
= (*(struct case_t
**) pa
)->v1
;
6102 int64_t b
= (*(struct case_t
**) pb
)->v1
;
6103 return a
< b
? -1 : a
> b
;
6106 static void gtst_addr(int t
, int a
)
6108 gsym_addr(gvtst(0, t
), a
);
6111 static void gcase(struct case_t
**base
, int len
, int *bsym
)
6115 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
6132 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
6134 gcase(base
, len
/2, bsym
);
6138 base
+= e
; len
-= e
;
6148 if (p
->v1
== p
->v2
) {
6150 gtst_addr(0, p
->sym
);
6160 gtst_addr(0, p
->sym
);
6164 *bsym
= gjmp(*bsym
);
6167 /* ------------------------------------------------------------------------- */
6168 /* __attribute__((cleanup(fn))) */
6170 static void try_call_scope_cleanup(Sym
*stop
)
6172 Sym
*cls
= cur_scope
->cl
.s
;
6174 for (; cls
!= stop
; cls
= cls
->ncl
) {
6175 Sym
*fs
= cls
->next
;
6176 Sym
*vs
= cls
->prev_tok
;
6178 vpushsym(&fs
->type
, fs
);
6179 vset(&vs
->type
, vs
->r
, vs
->c
);
6181 mk_pointer(&vtop
->type
);
6187 static void try_call_cleanup_goto(Sym
*cleanupstate
)
6192 if (!cur_scope
->cl
.s
)
6195 /* search NCA of both cleanup chains given parents and initial depth */
6196 ocd
= cleanupstate
? cleanupstate
->v
& ~SYM_FIELD
: 0;
6197 for (ccd
= cur_scope
->cl
.n
, oc
= cleanupstate
; ocd
> ccd
; --ocd
, oc
= oc
->ncl
)
6199 for (cc
= cur_scope
->cl
.s
; ccd
> ocd
; --ccd
, cc
= cc
->ncl
)
6201 for (; cc
!= oc
; cc
= cc
->ncl
, oc
= oc
->ncl
, --ccd
)
6204 try_call_scope_cleanup(cc
);
6207 /* call 'func' for each __attribute__((cleanup(func))) */
6208 static void block_cleanup(struct scope
*o
)
6212 for (pg
= &pending_gotos
; (g
= *pg
) && g
->c
> o
->cl
.n
;) {
6213 if (g
->prev_tok
->r
& LABEL_FORWARD
) {
6218 try_call_scope_cleanup(o
->cl
.s
);
6219 pcl
->jnext
= gjmp(0);
6221 goto remove_pending
;
6231 try_call_scope_cleanup(o
->cl
.s
);
6234 /* ------------------------------------------------------------------------- */
6237 static void vla_restore(int loc
)
6240 gen_vla_sp_restore(loc
);
6243 static void vla_leave(struct scope
*o
)
6245 if (o
->vla
.num
< cur_scope
->vla
.num
)
6246 vla_restore(o
->vla
.loc
);
6249 /* ------------------------------------------------------------------------- */
6252 void new_scope(struct scope
*o
)
6254 /* copy and link previous scope */
6256 o
->prev
= cur_scope
;
6259 /* record local declaration stack position */
6260 o
->lstk
= local_stack
;
6261 o
->llstk
= local_label_stack
;
6266 void prev_scope(struct scope
*o
, int is_expr
)
6270 if (o
->cl
.s
!= o
->prev
->cl
.s
)
6271 block_cleanup(o
->prev
);
6273 /* pop locally defined labels */
6274 label_pop(&local_label_stack
, o
->llstk
, is_expr
);
6276 /* In the is_expr case (a statement expression is finished here),
6277 vtop might refer to symbols on the local_stack. Either via the
6278 type or via vtop->sym. We can't pop those nor any that in turn
6279 might be referred to. To make it easier we don't roll back
6280 any symbols in that case; some upper level call to block() will
6281 do that. We do have to remove such symbols from the lookup
6282 tables, though. sym_pop will do that. */
6284 /* pop locally defined symbols */
6285 sym_pop(&local_stack
, o
->lstk
, is_expr
);
6287 cur_scope
= o
->prev
;
6291 /* leave a scope via break/continue(/goto) */
6292 void leave_scope(struct scope
*o
)
6296 try_call_scope_cleanup(o
->cl
.s
);
6300 /* ------------------------------------------------------------------------- */
6301 /* call block from 'for do while' loops */
6303 static void lblock(int *bsym
, int *csym
)
6305 struct scope
*lo
= loop_scope
, *co
= cur_scope
;
6306 int *b
= co
->bsym
, *c
= co
->csym
;
6320 static void block(int is_expr
)
6322 int a
, b
, c
, d
, e
, t
;
6326 /* default return value is (void) */
6328 vtop
->type
.t
= VT_VOID
;
6340 if (tok
== TOK_ELSE
) {
6345 gsym(d
); /* patch else jmp */
6350 } else if (t
== TOK_WHILE
) {
6362 } else if (t
== '{') {
6366 /* handle local labels declarations */
6367 while (tok
== TOK_LABEL
) {
6370 if (tok
< TOK_UIDENT
)
6371 expect("label identifier");
6372 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
6374 } while (tok
== ',');
6378 while (tok
!= '}') {
6387 prev_scope(&o
, is_expr
);
6389 if (0 == local_scope
&& !nocode_wanted
)
6390 check_func_return();
6393 } else if (t
== TOK_RETURN
) {
6395 b
= (func_vt
.t
& VT_BTYPE
) != VT_VOID
;
6397 gexpr(), gen_assign_cast(&func_vt
);
6398 leave_scope(root_scope
);
6400 gfunc_return(&func_vt
);
6404 tcc_warning("'return' with no value.");
6406 /* jump unless last stmt in top-level block */
6407 if (tok
!= '}' || local_scope
!= 1)
6411 } else if (t
== TOK_BREAK
) {
6413 if (!cur_scope
->bsym
)
6414 tcc_error("cannot break");
6415 if (!cur_switch
|| cur_scope
->bsym
!= cur_switch
->bsym
)
6416 leave_scope(loop_scope
);
6418 leave_scope(cur_switch
->scope
);
6419 *cur_scope
->bsym
= gjmp(*cur_scope
->bsym
);
6422 } else if (t
== TOK_CONTINUE
) {
6424 if (!cur_scope
->csym
)
6425 tcc_error("cannot continue");
6426 leave_scope(loop_scope
);
6427 *cur_scope
->csym
= gjmp(*cur_scope
->csym
);
6430 } else if (t
== TOK_FOR
) {
6436 /* c99 for-loop init decl? */
6437 if (!decl0(VT_LOCAL
, 1, NULL
)) {
6438 /* no, regular for-loop init expr */
6466 } else if (t
== TOK_DO
) {
6480 } else if (t
== TOK_SWITCH
) {
6481 struct switch_t
*saved
, sw
;
6488 sw
.scope
= cur_scope
;
6496 switchval
= *vtop
--;
6499 b
= gjmp(0); /* jump to first case */
6501 a
= gjmp(a
); /* add implicit break */
6505 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6506 for (b
= 1; b
< sw
.n
; b
++)
6507 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6508 tcc_error("duplicate case value");
6510 /* Our switch table sorting is signed, so the compared
6511 value needs to be as well when it's 64bit. */
6512 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6513 switchval
.type
.t
&= ~VT_UNSIGNED
;
6516 d
= 0, gcase(sw
.p
, sw
.n
, &d
);
6519 gsym_addr(d
, sw
.def_sym
);
6525 dynarray_reset(&sw
.p
, &sw
.n
);
6528 } else if (t
== TOK_CASE
) {
6529 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6532 cr
->v1
= cr
->v2
= expr_const64();
6533 if (gnu_ext
&& tok
== TOK_DOTS
) {
6535 cr
->v2
= expr_const64();
6536 if (cr
->v2
< cr
->v1
)
6537 tcc_warning("empty case range");
6540 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6543 goto block_after_label
;
6545 } else if (t
== TOK_DEFAULT
) {
6548 if (cur_switch
->def_sym
)
6549 tcc_error("too many 'default'");
6550 cur_switch
->def_sym
= gind();
6553 goto block_after_label
;
6555 } else if (t
== TOK_GOTO
) {
6556 vla_restore(root_scope
->vla
.loc
);
6557 if (tok
== '*' && gnu_ext
) {
6561 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6565 } else if (tok
>= TOK_UIDENT
) {
6566 s
= label_find(tok
);
6567 /* put forward definition if needed */
6569 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6570 else if (s
->r
== LABEL_DECLARED
)
6571 s
->r
= LABEL_FORWARD
;
6573 if (s
->r
& LABEL_FORWARD
) {
6574 /* start new goto chain for cleanups, linked via label->next */
6575 if (cur_scope
->cl
.s
&& !nocode_wanted
) {
6576 sym_push2(&pending_gotos
, SYM_FIELD
, 0, cur_scope
->cl
.n
);
6577 pending_gotos
->prev_tok
= s
;
6578 s
= sym_push2(&s
->next
, SYM_FIELD
, 0, 0);
6579 pending_gotos
->next
= s
;
6581 s
->jnext
= gjmp(s
->jnext
);
6583 try_call_cleanup_goto(s
->cleanupstate
);
6584 gjmp_addr(s
->jnext
);
6589 expect("label identifier");
6593 } else if (t
== TOK_ASM1
|| t
== TOK_ASM2
|| t
== TOK_ASM3
) {
6597 if (tok
== ':' && t
>= TOK_UIDENT
) {
6602 if (s
->r
== LABEL_DEFINED
)
6603 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6604 s
->r
= LABEL_DEFINED
;
6606 Sym
*pcl
; /* pending cleanup goto */
6607 for (pcl
= s
->next
; pcl
; pcl
= pcl
->prev
)
6609 sym_pop(&s
->next
, NULL
, 0);
6613 s
= label_push(&global_label_stack
, t
, LABEL_DEFINED
);
6616 s
->cleanupstate
= cur_scope
->cl
.s
;
6619 vla_restore(cur_scope
->vla
.loc
);
6620 /* we accept this, but it is a mistake */
6622 tcc_warning("deprecated use of label at end of compound statement");
6628 /* expression case */
6644 /* This skips over a stream of tokens containing balanced {} and ()
6645 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6646 with a '{'). If STR then allocates and stores the skipped tokens
6647 in *STR. This doesn't check if () and {} are nested correctly,
6648 i.e. "({)}" is accepted. */
6649 static void skip_or_save_block(TokenString
**str
)
6651 int braces
= tok
== '{';
6654 *str
= tok_str_alloc();
6656 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6658 if (tok
== TOK_EOF
) {
6659 if (str
|| level
> 0)
6660 tcc_error("unexpected end of file");
6665 tok_str_add_tok(*str
);
6668 if (t
== '{' || t
== '(') {
6670 } else if (t
== '}' || t
== ')') {
6672 if (level
== 0 && braces
&& t
== '}')
6677 tok_str_add(*str
, -1);
6678 tok_str_add(*str
, 0);
6682 #define EXPR_CONST 1
6685 static void parse_init_elem(int expr_type
)
6687 int saved_global_expr
;
6690 /* compound literals must be allocated globally in this case */
6691 saved_global_expr
= global_expr
;
6694 global_expr
= saved_global_expr
;
6695 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6696 (compound literals). */
6697 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6698 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6699 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6700 #ifdef TCC_TARGET_PE
6701 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6704 tcc_error("initializer element is not constant");
6712 /* put zeros for variable based init */
6713 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6716 /* nothing to do because globals are already set to zero */
6718 vpush_global_sym(&func_old_type
, TOK_memset
);
6720 #ifdef TCC_TARGET_ARM
6732 #define DIF_SIZE_ONLY 2
6733 #define DIF_HAVE_ELEM 4
6735 /* t is the array or struct type. c is the array or struct
6736 address. cur_field is the pointer to the current
6737 field, for arrays the 'c' member contains the current start
6738 index. 'flags' is as in decl_initializer.
6739 'al' contains the already initialized length of the
6740 current container (starting at c). This returns the new length of that. */
6741 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6742 Sym
**cur_field
, int flags
, int al
)
6745 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6746 unsigned long corig
= c
;
6751 if (flags
& DIF_HAVE_ELEM
)
6754 if (gnu_ext
&& tok
>= TOK_UIDENT
) {
6761 /* NOTE: we only support ranges for last designator */
6762 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6764 if (!(type
->t
& VT_ARRAY
))
6765 expect("array type");
6767 index
= index_last
= expr_const();
6768 if (tok
== TOK_DOTS
&& gnu_ext
) {
6770 index_last
= expr_const();
6774 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6776 tcc_error("invalid index");
6778 (*cur_field
)->c
= index_last
;
6779 type
= pointed_type(type
);
6780 elem_size
= type_size(type
, &align
);
6781 c
+= index
* elem_size
;
6782 nb_elems
= index_last
- index
+ 1;
6789 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6790 expect("struct/union type");
6791 f
= find_field(type
, l
, &cumofs
);
6804 } else if (!gnu_ext
) {
6809 if (type
->t
& VT_ARRAY
) {
6810 index
= (*cur_field
)->c
;
6811 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6812 tcc_error("index too large");
6813 type
= pointed_type(type
);
6814 c
+= index
* type_size(type
, &align
);
6817 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6818 *cur_field
= f
= f
->next
;
6820 tcc_error("too many field init");
6825 /* must put zero in holes (note that doing it that way
6826 ensures that it even works with designators) */
6827 if (!(flags
& DIF_SIZE_ONLY
) && c
- corig
> al
)
6828 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6829 decl_initializer(type
, sec
, c
, flags
& ~DIF_FIRST
);
6831 /* XXX: make it more general */
6832 if (!(flags
& DIF_SIZE_ONLY
) && nb_elems
> 1) {
6833 unsigned long c_end
;
6838 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6839 for (i
= 1; i
< nb_elems
; i
++) {
6840 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6845 } else if (!NODATA_WANTED
) {
6846 c_end
= c
+ nb_elems
* elem_size
;
6847 if (c_end
> sec
->data_allocated
)
6848 section_realloc(sec
, c_end
);
6849 src
= sec
->data
+ c
;
6851 for(i
= 1; i
< nb_elems
; i
++) {
6853 memcpy(dst
, src
, elem_size
);
6857 c
+= nb_elems
* type_size(type
, &align
);
6863 /* store a value or an expression directly in global data or in local array */
6864 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6871 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6875 /* XXX: not portable */
6876 /* XXX: generate error if incorrect relocation */
6877 gen_assign_cast(&dtype
);
6878 bt
= type
->t
& VT_BTYPE
;
6880 if ((vtop
->r
& VT_SYM
)
6883 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6884 || (type
->t
& VT_BITFIELD
))
6885 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6887 tcc_error("initializer element is not computable at load time");
6889 if (NODATA_WANTED
) {
6894 size
= type_size(type
, &align
);
6895 section_reserve(sec
, c
+ size
);
6896 ptr
= sec
->data
+ c
;
6898 /* XXX: make code faster ? */
6899 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6900 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6901 /* XXX This rejects compound literals like
6902 '(void *){ptr}'. The problem is that '&sym' is
6903 represented the same way, which would be ruled out
6904 by the SYM_FIRST_ANOM check above, but also '"string"'
6905 in 'char *p = "string"' is represented the same
6906 with the type being VT_PTR and the symbol being an
6907 anonymous one. That is, there's no difference in vtop
6908 between '(void *){x}' and '&(void *){x}'. Ignore
6909 pointer typed entities here. Hopefully no real code
6910 will every use compound literals with scalar type. */
6911 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6912 /* These come from compound literals, memcpy stuff over. */
6916 esym
= elfsym(vtop
->sym
);
6917 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6918 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6920 /* We need to copy over all memory contents, and that
6921 includes relocations. Use the fact that relocs are
6922 created it order, so look from the end of relocs
6923 until we hit one before the copied region. */
6924 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6925 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6926 while (num_relocs
--) {
6928 if (rel
->r_offset
>= esym
->st_value
+ size
)
6930 if (rel
->r_offset
< esym
->st_value
)
6932 /* Note: if the same fields are initialized multiple
6933 times (possible with designators) then we possibly
6934 add multiple relocations for the same offset here.
6935 That would lead to wrong code, the last reloc needs
6936 to win. We clean this up later after the whole
6937 initializer is parsed. */
6938 put_elf_reloca(symtab_section
, sec
,
6939 c
+ rel
->r_offset
- esym
->st_value
,
6940 ELFW(R_TYPE
)(rel
->r_info
),
6941 ELFW(R_SYM
)(rel
->r_info
),
6951 if (type
->t
& VT_BITFIELD
) {
6952 int bit_pos
, bit_size
, bits
, n
;
6953 unsigned char *p
, v
, m
;
6954 bit_pos
= BIT_POS(vtop
->type
.t
);
6955 bit_size
= BIT_SIZE(vtop
->type
.t
);
6956 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6957 bit_pos
&= 7, bits
= 0;
6962 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6963 m
= ((1 << n
) - 1) << bit_pos
;
6964 *p
= (*p
& ~m
) | (v
& m
);
6965 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6969 /* XXX: when cross-compiling we assume that each type has the
6970 same representation on host and target, which is likely to
6971 be wrong in the case of long double */
6973 vtop
->c
.i
= vtop
->c
.i
!= 0;
6975 *(char *)ptr
|= vtop
->c
.i
;
6978 *(short *)ptr
|= vtop
->c
.i
;
6981 *(float*)ptr
= vtop
->c
.f
;
6984 *(double *)ptr
= vtop
->c
.d
;
6987 #if defined TCC_IS_NATIVE_387
6988 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
6989 memcpy(ptr
, &vtop
->c
.ld
, 10);
6991 else if (sizeof (long double) == sizeof (double))
6992 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr
) : "m" (vtop
->c
.ld
));
6994 else if (vtop
->c
.ld
== 0.0)
6998 if (sizeof(long double) == LDOUBLE_SIZE
)
6999 *(long double*)ptr
= vtop
->c
.ld
;
7000 else if (sizeof(double) == LDOUBLE_SIZE
)
7001 *(double *)ptr
= (double)vtop
->c
.ld
;
7003 tcc_error("can't cross compile long double constants");
7007 *(long long *)ptr
|= vtop
->c
.i
;
7014 addr_t val
= vtop
->c
.i
;
7016 if (vtop
->r
& VT_SYM
)
7017 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
7019 *(addr_t
*)ptr
|= val
;
7021 if (vtop
->r
& VT_SYM
)
7022 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
7023 *(addr_t
*)ptr
|= val
;
7029 int val
= vtop
->c
.i
;
7031 if (vtop
->r
& VT_SYM
)
7032 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
7036 if (vtop
->r
& VT_SYM
)
7037 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
7046 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
7053 /* 't' contains the type and storage info. 'c' is the offset of the
7054 object in section 'sec'. If 'sec' is NULL, it means stack based
7055 allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
7056 dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
7057 size only evaluation is wanted (only for arrays). */
7058 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
7061 int len
, n
, no_oblock
, nb
, i
;
7067 if (!(flags
& DIF_HAVE_ELEM
) && tok
!= '{' &&
7068 /* In case of strings we have special handling for arrays, so
7069 don't consume them as initializer value (which would commit them
7070 to some anonymous symbol). */
7071 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
7072 !(flags
& DIF_SIZE_ONLY
)) {
7073 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7074 flags
|= DIF_HAVE_ELEM
;
7077 if ((flags
& DIF_HAVE_ELEM
) &&
7078 !(type
->t
& VT_ARRAY
) &&
7079 /* Use i_c_parameter_t, to strip toplevel qualifiers.
7080 The source type might have VT_CONSTANT set, which is
7081 of course assignable to non-const elements. */
7082 is_compatible_unqualified_types(type
, &vtop
->type
)) {
7083 init_putv(type
, sec
, c
);
7084 } else if (type
->t
& VT_ARRAY
) {
7087 t1
= pointed_type(type
);
7088 size1
= type_size(t1
, &align1
);
7091 if (((flags
& DIF_FIRST
) && tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
7094 tcc_error("character array initializer must be a literal,"
7095 " optionally enclosed in braces");
7100 /* only parse strings here if correct type (otherwise: handle
7101 them as ((w)char *) expressions */
7102 if ((tok
== TOK_LSTR
&&
7103 #ifdef TCC_TARGET_PE
7104 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
7106 (t1
->t
& VT_BTYPE
) == VT_INT
7108 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
7110 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7113 /* compute maximum number of chars wanted */
7115 cstr_len
= tokc
.str
.size
;
7117 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
7120 if (n
>= 0 && nb
> (n
- len
))
7122 if (!(flags
& DIF_SIZE_ONLY
)) {
7124 tcc_warning("initializer-string for array is too long");
7125 /* in order to go faster for common case (char
7126 string in global variable, we handle it
7128 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7130 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
7134 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
7136 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
7138 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
7145 /* only add trailing zero if enough storage (no
7146 warning in this case since it is standard) */
7147 if (n
< 0 || len
< n
) {
7148 if (!(flags
& DIF_SIZE_ONLY
)) {
7150 init_putv(t1
, sec
, c
+ (len
* size1
));
7161 while (tok
!= '}' || (flags
& DIF_HAVE_ELEM
)) {
7162 len
= decl_designator(type
, sec
, c
, &f
, flags
, len
);
7163 flags
&= ~DIF_HAVE_ELEM
;
7164 if (type
->t
& VT_ARRAY
) {
7166 /* special test for multi dimensional arrays (may not
7167 be strictly correct if designators are used at the
7169 if (no_oblock
&& len
>= n
*size1
)
7172 if (s
->type
.t
== VT_UNION
)
7176 if (no_oblock
&& f
== NULL
)
7185 /* put zeros at the end */
7186 if (!(flags
& DIF_SIZE_ONLY
) && len
< n
*size1
)
7187 init_putz(sec
, c
+ len
, n
*size1
- len
);
7190 /* patch type size if needed, which happens only for array types */
7192 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
7193 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7196 if ((flags
& DIF_FIRST
) || tok
== '{') {
7204 } else if (tok
== '{') {
7205 if (flags
& DIF_HAVE_ELEM
)
7208 decl_initializer(type
, sec
, c
, flags
& ~DIF_HAVE_ELEM
);
7210 } else if ((flags
& DIF_SIZE_ONLY
)) {
7211 /* If we supported only ISO C we wouldn't have to accept calling
7212 this on anything than an array if DIF_SIZE_ONLY (and even then
7213 only on the outermost level, so no recursion would be needed),
7214 because initializing a flex array member isn't supported.
7215 But GNU C supports it, so we need to recurse even into
7216 subfields of structs and arrays when DIF_SIZE_ONLY is set. */
7217 /* just skip expression */
7218 skip_or_save_block(NULL
);
7220 if (!(flags
& DIF_HAVE_ELEM
)) {
7221 /* This should happen only when we haven't parsed
7222 the init element above for fear of committing a
7223 string constant to memory too early. */
7224 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
7225 expect("string constant");
7226 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7228 init_putv(type
, sec
, c
);
7232 /* parse an initializer for type 't' if 'has_init' is non zero, and
7233 allocate space in local or global data space ('r' is either
7234 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7235 variable 'v' of scope 'scope' is declared before initializers
7236 are parsed. If 'v' is zero, then a reference to the new object
7237 is put in the value stack. If 'has_init' is 2, a special parsing
7238 is done to handle string constants. */
7239 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7240 int has_init
, int v
, int scope
)
7242 int size
, align
, addr
;
7243 TokenString
*init_str
= NULL
;
7246 Sym
*flexible_array
;
7248 int saved_nocode_wanted
= nocode_wanted
;
7249 #ifdef CONFIG_TCC_BCHECK
7253 /* Always allocate static or global variables */
7254 if (v
&& (r
& VT_VALMASK
) == VT_CONST
)
7255 nocode_wanted
|= 0x80000000;
7257 #ifdef CONFIG_TCC_BCHECK
7258 bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
7261 flexible_array
= NULL
;
7262 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7263 Sym
*field
= type
->ref
->next
;
7266 field
= field
->next
;
7267 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
7268 flexible_array
= field
;
7272 size
= type_size(type
, &align
);
7273 /* If unknown size, we must evaluate it before
7274 evaluating initializers because
7275 initializers can generate global data too
7276 (e.g. string pointers or ISOC99 compound
7277 literals). It also simplifies local
7278 initializers handling */
7279 if (size
< 0 || (flexible_array
&& has_init
)) {
7281 tcc_error("unknown type size");
7282 /* get all init string */
7283 if (has_init
== 2) {
7284 init_str
= tok_str_alloc();
7285 /* only get strings */
7286 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7287 tok_str_add_tok(init_str
);
7290 tok_str_add(init_str
, -1);
7291 tok_str_add(init_str
, 0);
7293 skip_or_save_block(&init_str
);
7298 begin_macro(init_str
, 1);
7300 decl_initializer(type
, NULL
, 0, DIF_FIRST
| DIF_SIZE_ONLY
);
7301 /* prepare second initializer parsing */
7302 macro_ptr
= init_str
->str
;
7305 /* if still unknown size, error */
7306 size
= type_size(type
, &align
);
7308 tcc_error("unknown type size");
7310 /* If there's a flex member and it was used in the initializer
7312 if (flexible_array
&&
7313 flexible_array
->type
.ref
->c
> 0)
7314 size
+= flexible_array
->type
.ref
->c
7315 * pointed_size(&flexible_array
->type
);
7316 /* take into account specified alignment if bigger */
7317 if (ad
->a
.aligned
) {
7318 int speca
= 1 << (ad
->a
.aligned
- 1);
7321 } else if (ad
->a
.packed
) {
7325 if (!v
&& NODATA_WANTED
)
7326 size
= 0, align
= 1;
7328 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7330 #ifdef CONFIG_TCC_BCHECK
7331 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7335 loc
= (loc
- size
) & -align
;
7337 #ifdef CONFIG_TCC_BCHECK
7338 /* handles bounds */
7339 /* XXX: currently, since we do only one pass, we cannot track
7340 '&' operators, so we add only arrays */
7341 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7343 /* add padding between regions */
7345 /* then add local bound info */
7346 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
7347 bounds_ptr
[0] = addr
;
7348 bounds_ptr
[1] = size
;
7352 /* local variable */
7353 #ifdef CONFIG_TCC_ASM
7354 if (ad
->asm_label
) {
7355 int reg
= asm_parse_regvar(ad
->asm_label
);
7357 r
= (r
& ~VT_VALMASK
) | reg
;
7360 sym
= sym_push(v
, type
, r
, addr
);
7361 if (ad
->cleanup_func
) {
7362 Sym
*cls
= sym_push2(&all_cleanups
,
7363 SYM_FIELD
| ++cur_scope
->cl
.n
, 0, 0);
7364 cls
->prev_tok
= sym
;
7365 cls
->next
= ad
->cleanup_func
;
7366 cls
->ncl
= cur_scope
->cl
.s
;
7367 cur_scope
->cl
.s
= cls
;
7372 /* push local reference */
7373 vset(type
, r
, addr
);
7376 if (v
&& scope
== VT_CONST
) {
7377 /* see if the symbol was already defined */
7380 patch_storage(sym
, ad
, type
);
7381 /* we accept several definitions of the same global variable. */
7382 if (!has_init
&& sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
)
7387 /* allocate symbol in corresponding section */
7392 else if (tcc_state
->nocommon
)
7397 addr
= section_add(sec
, size
, align
);
7398 #ifdef CONFIG_TCC_BCHECK
7399 /* add padding if bound check */
7401 section_add(sec
, 1, 1);
7404 addr
= align
; /* SHN_COMMON is special, symbol value is align */
7405 sec
= common_section
;
7410 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7411 patch_storage(sym
, ad
, NULL
);
7413 /* update symbol definition */
7414 put_extern_sym(sym
, sec
, addr
, size
);
7416 /* push global reference */
7417 vpush_ref(type
, sec
, addr
, size
);
7422 #ifdef CONFIG_TCC_BCHECK
7423 /* handles bounds now because the symbol must be defined
7424 before for the relocation */
7428 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
7429 /* then add global bound info */
7430 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
7431 bounds_ptr
[0] = 0; /* relocated */
7432 bounds_ptr
[1] = size
;
7437 if (type
->t
& VT_VLA
) {
7443 /* save current stack pointer */
7444 if (root_scope
->vla
.loc
== 0) {
7445 struct scope
*v
= cur_scope
;
7446 gen_vla_sp_save(loc
-= PTR_SIZE
);
7447 do v
->vla
.loc
= loc
; while ((v
= v
->prev
));
7450 vla_runtime_type_size(type
, &a
);
7451 gen_vla_alloc(type
, a
);
7452 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7453 /* on _WIN64, because of the function args scratch area, the
7454 result of alloca differs from RSP and is returned in RAX. */
7455 gen_vla_result(addr
), addr
= (loc
-= PTR_SIZE
);
7457 gen_vla_sp_save(addr
);
7458 cur_scope
->vla
.loc
= addr
;
7459 cur_scope
->vla
.num
++;
7461 } else if (has_init
) {
7462 size_t oldreloc_offset
= 0;
7463 if (sec
&& sec
->reloc
)
7464 oldreloc_offset
= sec
->reloc
->data_offset
;
7465 decl_initializer(type
, sec
, addr
, DIF_FIRST
);
7466 if (sec
&& sec
->reloc
)
7467 squeeze_multi_relocs(sec
, oldreloc_offset
);
7468 /* patch flexible array member size back to -1, */
7469 /* for possible subsequent similar declarations */
7471 flexible_array
->type
.ref
->c
= -1;
7475 /* restore parse state if needed */
7481 nocode_wanted
= saved_nocode_wanted
;
7484 /* parse a function defined by symbol 'sym' and generate its code in
7485 'cur_text_section' */
7486 static void gen_function(Sym
*sym
)
7488 /* Initialize VLA state */
7489 struct scope f
= { 0 };
7490 cur_scope
= root_scope
= &f
;
7493 ind
= cur_text_section
->data_offset
;
7494 if (sym
->a
.aligned
) {
7495 size_t newoff
= section_add(cur_text_section
, 0,
7496 1 << (sym
->a
.aligned
- 1));
7497 gen_fill_nops(newoff
- ind
);
7499 /* NOTE: we patch the symbol size later */
7500 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7502 funcname
= get_tok_str(sym
->v
, NULL
);
7505 /* put debug symbol */
7506 tcc_debug_funcstart(tcc_state
, sym
);
7507 /* push a dummy symbol to enable local sym storage */
7508 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7509 local_scope
= 1; /* for function parameters */
7510 gfunc_prolog(&sym
->type
);
7513 clear_temp_local_var_list();
7518 cur_text_section
->data_offset
= ind
;
7519 /* reset local stack */
7520 sym_pop(&local_stack
, NULL
, 0);
7522 label_pop(&global_label_stack
, NULL
, 0);
7523 sym_pop(&all_cleanups
, NULL
, 0);
7524 /* patch symbol size */
7525 elfsym(sym
)->st_size
= ind
- func_ind
;
7526 /* end of function */
7527 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7528 /* It's better to crash than to generate wrong code */
7529 cur_text_section
= NULL
;
7530 funcname
= ""; /* for safety */
7531 func_vt
.t
= VT_VOID
; /* for safety */
7532 func_var
= 0; /* for safety */
7533 ind
= 0; /* for safety */
7534 nocode_wanted
= 0x80000000;
7538 static void gen_inline_functions(TCCState
*s
)
7541 int inline_generated
, i
, ln
;
7542 struct InlineFunc
*fn
;
7544 ln
= file
->line_num
;
7545 /* iterate while inline function are referenced */
7547 inline_generated
= 0;
7548 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7549 fn
= s
->inline_fns
[i
];
7551 if (sym
&& (sym
->c
|| !(sym
->type
.t
& VT_INLINE
))) {
7552 /* the function was used or forced (and then not internal):
7553 generate its code and convert it to a normal function */
7556 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7557 begin_macro(fn
->func_str
, 1);
7559 cur_text_section
= text_section
;
7563 inline_generated
= 1;
7566 } while (inline_generated
);
7567 file
->line_num
= ln
;
7570 ST_FUNC
void free_inline_functions(TCCState
*s
)
7573 /* free tokens of unused inline functions */
7574 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7575 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7577 tok_str_free(fn
->func_str
);
7579 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7582 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7583 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7584 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7589 AttributeDef ad
, adbase
;
7592 if (tok
== TOK_STATIC_ASSERT
) {
7600 tcc_error("%s", get_tok_str(tok
, &tokc
));
7606 if (!parse_btype(&btype
, &adbase
)) {
7607 if (is_for_loop_init
)
7609 /* skip redundant ';' if not in old parameter decl scope */
7610 if (tok
== ';' && l
!= VT_CMP
) {
7616 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7617 /* global asm block */
7621 if (tok
>= TOK_UIDENT
) {
7622 /* special test for old K&R protos without explicit int
7623 type. Only accepted when defining global data */
7627 expect("declaration");
7632 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7633 int v
= btype
.ref
->v
;
7634 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7635 tcc_warning("unnamed struct/union that defines no instances");
7639 if (IS_ENUM(btype
.t
)) {
7644 while (1) { /* iterate thru each declaration */
7646 /* If the base type itself was an array type of unspecified
7647 size (like in 'typedef int arr[]; arr x = {1};') then
7648 we will overwrite the unknown size by the real one for
7649 this decl. We need to unshare the ref symbol holding
7651 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7652 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7655 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7659 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7660 printf("type = '%s'\n", buf
);
7663 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7664 /* if old style function prototype, we accept a
7667 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7668 decl0(VT_CMP
, 0, sym
);
7669 /* always compile 'extern inline' */
7670 if (type
.t
& VT_EXTERN
)
7671 type
.t
&= ~VT_INLINE
;
7674 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7675 ad
.asm_label
= asm_label_instr();
7676 /* parse one last attribute list, after asm label */
7677 parse_attribute(&ad
);
7679 /* gcc does not allow __asm__("label") with function definition,
7686 #ifdef TCC_TARGET_PE
7687 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7688 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7689 tcc_error("cannot have dll linkage with static or typedef");
7690 if (ad
.a
.dllimport
) {
7691 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7694 type
.t
|= VT_EXTERN
;
7700 tcc_error("cannot use local functions");
7701 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7702 expect("function definition");
7704 /* reject abstract declarators in function definition
7705 make old style params without decl have int type */
7707 while ((sym
= sym
->next
) != NULL
) {
7708 if (!(sym
->v
& ~SYM_FIELD
))
7709 expect("identifier");
7710 if (sym
->type
.t
== VT_VOID
)
7711 sym
->type
= int_type
;
7714 /* put function symbol */
7715 type
.t
&= ~VT_EXTERN
;
7716 sym
= external_sym(v
, &type
, 0, &ad
);
7717 /* static inline functions are just recorded as a kind
7718 of macro. Their code will be emitted at the end of
7719 the compilation unit only if they are used */
7720 if (sym
->type
.t
& VT_INLINE
) {
7721 struct InlineFunc
*fn
;
7722 const char *filename
;
7724 filename
= file
? file
->filename
: "";
7725 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7726 strcpy(fn
->filename
, filename
);
7728 skip_or_save_block(&fn
->func_str
);
7729 dynarray_add(&tcc_state
->inline_fns
,
7730 &tcc_state
->nb_inline_fns
, fn
);
7732 /* compute text section */
7733 cur_text_section
= ad
.section
;
7734 if (!cur_text_section
)
7735 cur_text_section
= text_section
;
7741 /* find parameter in function parameter list */
7742 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7743 if ((sym
->v
& ~SYM_FIELD
) == v
)
7745 tcc_error("declaration for parameter '%s' but no such parameter",
7746 get_tok_str(v
, NULL
));
7748 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7749 tcc_error("storage class specified for '%s'",
7750 get_tok_str(v
, NULL
));
7751 if (sym
->type
.t
!= VT_VOID
)
7752 tcc_error("redefinition of parameter '%s'",
7753 get_tok_str(v
, NULL
));
7754 convert_parameter_type(&type
);
7756 } else if (type
.t
& VT_TYPEDEF
) {
7757 /* save typedefed type */
7758 /* XXX: test storage specifiers ? */
7760 if (sym
&& sym
->sym_scope
== local_scope
) {
7761 if (!is_compatible_types(&sym
->type
, &type
)
7762 || !(sym
->type
.t
& VT_TYPEDEF
))
7763 tcc_error("incompatible redefinition of '%s'",
7764 get_tok_str(v
, NULL
));
7767 sym
= sym_push(v
, &type
, 0, 0);
7771 } else if ((type
.t
& VT_BTYPE
) == VT_VOID
7772 && !(type
.t
& VT_EXTERN
)) {
7773 tcc_error("declaration of void object");
7776 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7777 /* external function definition */
7778 /* specific case for func_call attribute */
7780 } else if (!(type
.t
& VT_ARRAY
)) {
7781 /* not lvalue if array */
7782 r
|= lvalue_type(type
.t
);
7784 has_init
= (tok
== '=');
7785 if (has_init
&& (type
.t
& VT_VLA
))
7786 tcc_error("variable length array cannot be initialized");
7787 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
))
7788 || (type
.t
& VT_BTYPE
) == VT_FUNC
7789 /* as with GCC, uninitialized global arrays with no size
7790 are considered extern: */
7791 || ((type
.t
& VT_ARRAY
) && !has_init
7792 && l
== VT_CONST
&& type
.ref
->c
< 0)
7794 /* external variable or function */
7795 type
.t
|= VT_EXTERN
;
7796 sym
= external_sym(v
, &type
, r
, &ad
);
7797 if (ad
.alias_target
) {
7800 alias_target
= sym_find(ad
.alias_target
);
7801 esym
= elfsym(alias_target
);
7803 tcc_error("unsupported forward __alias__ attribute");
7804 put_extern_sym2(sym
, esym
->st_shndx
, esym
->st_value
, esym
->st_size
, 0);
7807 if (type
.t
& VT_STATIC
)
7813 else if (l
== VT_CONST
)
7814 /* uninitialized global variables may be overridden */
7815 type
.t
|= VT_EXTERN
;
7816 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7820 if (is_for_loop_init
)
7832 static void decl(int l
)
7837 /* ------------------------------------------------------------------------- */
7840 /* ------------------------------------------------------------------------- */