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
24 /********************************************************/
25 /* global variables */
27 /* loc : local variable index
28 ind : output code index
30 anon_sym: anonymous symbol index
32 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
34 ST_DATA Sym
*global_stack
;
35 ST_DATA Sym
*local_stack
;
36 ST_DATA Sym
*define_stack
;
37 ST_DATA Sym
*global_label_stack
;
38 ST_DATA Sym
*local_label_stack
;
40 static Sym
*sym_free_first
;
41 static void **sym_pools
;
42 static int nb_sym_pools
;
44 static Sym
*all_cleanups
, *pending_gotos
;
45 static int local_scope
;
47 static int in_generic
;
48 static int section_sym
;
51 static SValue _vstack
[1 + VSTACK_SIZE
];
52 #define vstack (_vstack + 1)
54 ST_DATA
int const_wanted
; /* true if constant wanted */
55 ST_DATA
int nocode_wanted
; /* no code generation wanted */
56 #define unevalmask 0xffff /* unevaluated subexpression */
57 #define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
58 #define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
60 /* Automagical code suppression ----> */
61 #define CODE_OFF() (nocode_wanted |= 0x20000000)
62 #define CODE_ON() (nocode_wanted &= ~0x20000000)
64 /* Clear 'nocode_wanted' at label if it was used */
65 ST_FUNC
void gsym(int t
) { if (t
) { gsym_addr(t
, ind
); CODE_ON(); }}
66 static int gind(void) { CODE_ON(); return ind
; }
68 /* Set 'nocode_wanted' after unconditional jumps */
69 static void gjmp_addr_acs(int t
) { gjmp_addr(t
); CODE_OFF(); }
70 static int gjmp_acs(int t
) { t
= gjmp(t
); CODE_OFF(); return t
; }
72 /* These are #undef'd at the end of this file */
73 #define gjmp_addr gjmp_addr_acs
77 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
78 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
79 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
81 static int last_line_num
, new_file
, func_ind
; /* debug info control */
82 ST_DATA
const char *funcname
;
83 ST_DATA CType int_type
, func_old_type
, char_pointer_type
;
86 #define VT_SIZE_T (VT_INT | VT_UNSIGNED)
87 #define VT_PTRDIFF_T VT_INT
89 #define VT_SIZE_T (VT_LLONG | VT_UNSIGNED)
90 #define VT_PTRDIFF_T VT_LLONG
92 #define VT_SIZE_T (VT_LONG | VT_LLONG | VT_UNSIGNED)
93 #define VT_PTRDIFF_T (VT_LONG | VT_LLONG)
96 ST_DATA
struct switch_t
{
100 } **p
; int n
; /* list of case ranges */
101 int def_sym
; /* default symbol */
104 } *cur_switch
; /* current switch */
106 #define MAX_TEMP_LOCAL_VARIABLE_NUMBER 0x4
107 /*list of temporary local variables on the stack in current function. */
108 ST_DATA
struct temp_local_variable
{
109 int location
; //offset on stack. Svalue.c.i
112 } arr_temp_local_vars
[MAX_TEMP_LOCAL_VARIABLE_NUMBER
];
113 short nb_temp_local_vars
;
115 static struct scope
{
117 struct { int loc
, num
; } vla
;
118 struct { Sym
*s
; int n
; } cl
;
121 } *cur_scope
, *loop_scope
, *root_scope
;
123 /********************************************************/
124 #ifndef CONFIG_TCC_ASM
125 ST_FUNC
void asm_instr(void)
127 tcc_error("inline asm() not supported");
129 ST_FUNC
void asm_global_instr(void)
131 tcc_error("inline asm() not supported");
135 /* ------------------------------------------------------------------------- */
137 static void gen_cast(CType
*type
);
138 static void gen_cast_s(int t
);
139 static inline CType
*pointed_type(CType
*type
);
140 static int is_compatible_types(CType
*type1
, CType
*type2
);
141 static int parse_btype(CType
*type
, AttributeDef
*ad
);
142 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
143 static void parse_expr_type(CType
*type
);
144 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
);
145 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int flags
);
146 static void block(int is_expr
);
147 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
148 static void decl(int l
);
149 static int decl0(int l
, int is_for_loop_init
, Sym
*);
150 static void expr_eq(void);
151 static void vla_runtime_type_size(CType
*type
, int *a
);
152 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
);
153 static inline int64_t expr_const64(void);
154 static void vpush64(int ty
, unsigned long long v
);
155 static void vpush(CType
*type
);
156 static int gvtst(int inv
, int t
);
157 static void gen_inline_functions(TCCState
*s
);
158 static void free_inline_functions(TCCState
*s
);
159 static void skip_or_save_block(TokenString
**str
);
160 static void gv_dup(void);
161 static int get_temp_local_var(int size
,int align
);
162 static void clear_temp_local_var_list();
163 static void cast_error(CType
*st
, CType
*dt
);
165 ST_INLN
int is_float(int t
)
167 int bt
= t
& VT_BTYPE
;
168 return bt
== VT_LDOUBLE
174 static inline int is_integer_btype(int bt
)
183 static int btype_size(int bt
)
185 return bt
== VT_BYTE
|| bt
== VT_BOOL
? 1 :
189 bt
== VT_PTR
? PTR_SIZE
: 0;
192 /* returns function return register from type */
193 static int R_RET(int t
)
197 #ifdef TCC_TARGET_X86_64
198 if ((t
& VT_BTYPE
) == VT_LDOUBLE
)
200 #elif defined TCC_TARGET_RISCV64
201 if ((t
& VT_BTYPE
) == VT_LDOUBLE
)
207 /* returns 2nd function return register, if any */
208 static int R2_RET(int t
)
214 #elif defined TCC_TARGET_X86_64
219 #elif defined TCC_TARGET_RISCV64
226 /* returns true for two-word types */
227 #define USING_TWO_WORDS(t) (R2_RET(t) != VT_CONST)
229 /* put function return registers to stack value */
230 static void PUT_R_RET(SValue
*sv
, int t
)
232 sv
->r
= R_RET(t
), sv
->r2
= R2_RET(t
);
235 /* returns function return register class for type t */
236 static int RC_RET(int t
)
238 return reg_classes
[R_RET(t
)] & ~(RC_FLOAT
| RC_INT
);
241 /* returns generic register class for type t */
242 static int RC_TYPE(int t
)
246 #ifdef TCC_TARGET_X86_64
247 if ((t
& VT_BTYPE
) == VT_LDOUBLE
)
249 if ((t
& VT_BTYPE
) == VT_QFLOAT
)
251 #elif defined TCC_TARGET_RISCV64
252 if ((t
& VT_BTYPE
) == VT_LDOUBLE
)
258 /* returns 2nd register class corresponding to t and rc */
259 static int RC2_TYPE(int t
, int rc
)
261 if (!USING_TWO_WORDS(t
))
276 /* we use our own 'finite' function to avoid potential problems with
277 non standard math libs */
278 /* XXX: endianness dependent */
279 ST_FUNC
int ieee_finite(double d
)
282 memcpy(p
, &d
, sizeof(double));
283 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
286 /* compiling intel long double natively */
287 #if (defined __i386__ || defined __x86_64__) \
288 && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
289 # define TCC_IS_NATIVE_387
292 ST_FUNC
void test_lvalue(void)
294 if (!(vtop
->r
& VT_LVAL
))
298 ST_FUNC
void check_vstack(void)
300 if (vtop
!= vstack
- 1)
301 tcc_error("internal compiler error: vstack leak (%d)", vtop
- vstack
+ 1);
304 /* ------------------------------------------------------------------------- */
305 /* vstack debugging aid */
308 void pv (const char *lbl
, int a
, int b
)
311 for (i
= a
; i
< a
+ b
; ++i
) {
312 SValue
*p
= &vtop
[-i
];
313 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
314 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
319 /* ------------------------------------------------------------------------- */
320 /* start of translation unit info */
321 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
326 /* file info: full path + filename */
327 section_sym
= put_elf_sym(symtab_section
, 0, 0,
328 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
329 text_section
->sh_num
, NULL
);
330 getcwd(buf
, sizeof(buf
));
332 normalize_slashes(buf
);
334 pstrcat(buf
, sizeof(buf
), "/");
335 put_stabs_r(s1
, buf
, N_SO
, 0, 0,
336 text_section
->data_offset
, text_section
, section_sym
);
337 put_stabs_r(s1
, file
->prev
->filename
, N_SO
, 0, 0,
338 text_section
->data_offset
, text_section
, section_sym
);
339 new_file
= last_line_num
= 0;
341 /* we're currently 'including' the <command line> */
345 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
346 symbols can be safely used */
347 put_elf_sym(symtab_section
, 0, 0,
348 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
349 SHN_ABS
, file
->filename
);
352 /* put end of translation unit info */
353 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
357 put_stabs_r(s1
, NULL
, N_SO
, 0, 0,
358 text_section
->data_offset
, text_section
, section_sym
);
361 static BufferedFile
* put_new_file(TCCState
*s1
)
363 BufferedFile
*f
= file
;
364 /* use upper file if from inline ":asm:" */
365 if (f
->filename
[0] == ':')
368 put_stabs_r(s1
, f
->filename
, N_SOL
, 0, 0, ind
, text_section
, section_sym
);
369 new_file
= last_line_num
= 0;
374 /* generate line number info */
375 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
378 if (!s1
->do_debug
|| !(f
= put_new_file(s1
)))
380 if (last_line_num
== f
->line_num
)
382 if (text_section
!= cur_text_section
)
384 if (func_ind
!= -1) {
385 put_stabn(s1
, N_SLINE
, 0, f
->line_num
, ind
- func_ind
);
387 /* from tcc_assemble */
388 put_stabs_r(s1
, NULL
, N_SLINE
, 0, f
->line_num
, ind
, text_section
, section_sym
);
390 last_line_num
= f
->line_num
;
393 /* put function symbol */
394 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
398 if (!s1
->do_debug
|| !(f
= put_new_file(s1
)))
400 /* XXX: we put here a dummy type */
401 snprintf(buf
, sizeof(buf
), "%s:%c1",
402 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
403 put_stabs_r(s1
, buf
, N_FUN
, 0, f
->line_num
, 0, cur_text_section
, sym
->c
);
407 /* put function size */
408 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
412 #if 0 // this seems to confuse gnu tools
413 put_stabn(s1
, N_FUN
, 0, 0, size
);
417 /* put alternative filename */
418 ST_FUNC
void tcc_debug_putfile(TCCState
*s1
, const char *filename
)
420 if (0 == strcmp(file
->filename
, filename
))
422 pstrcpy(file
->filename
, sizeof(file
->filename
), filename
);
426 /* begin of #include */
427 ST_FUNC
void tcc_debug_bincl(TCCState
*s1
)
431 put_stabs(s1
, file
->filename
, N_BINCL
, 0, 0, 0);
435 /* end of #include */
436 ST_FUNC
void tcc_debug_eincl(TCCState
*s1
)
440 put_stabn(s1
, N_EINCL
, 0, 0, 0);
444 /* ------------------------------------------------------------------------- */
445 /* initialize vstack and types. This must be done also for tcc -E */
446 ST_FUNC
void tccgen_init(TCCState
*s1
)
449 memset(vtop
, 0, sizeof *vtop
);
451 /* define some often used types */
453 char_pointer_type
.t
= VT_BYTE
;
454 mk_pointer(&char_pointer_type
);
455 func_old_type
.t
= VT_FUNC
;
456 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, 0, 0);
457 func_old_type
.ref
->f
.func_call
= FUNC_CDECL
;
458 func_old_type
.ref
->f
.func_type
= FUNC_OLD
;
461 ST_FUNC
int tccgen_compile(TCCState
*s1
)
463 cur_text_section
= NULL
;
465 anon_sym
= SYM_FIRST_ANOM
;
468 nocode_wanted
= 0x80000000;
472 #ifdef TCC_TARGET_ARM
476 printf("%s: **** new file\n", file
->filename
);
478 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
481 gen_inline_functions(s1
);
483 /* end of translation unit info */
488 ST_FUNC
void tccgen_finish(TCCState
*s1
)
490 free_inline_functions(s1
);
491 sym_pop(&global_stack
, NULL
, 0);
492 sym_pop(&local_stack
, NULL
, 0);
493 /* free preprocessor macros */
496 dynarray_reset(&sym_pools
, &nb_sym_pools
);
497 sym_free_first
= NULL
;
500 /* ------------------------------------------------------------------------- */
501 ST_FUNC ElfSym
*elfsym(Sym
*s
)
505 return &((ElfSym
*)symtab_section
->data
)[s
->c
];
508 /* apply storage attributes to Elf symbol */
509 ST_FUNC
void update_storage(Sym
*sym
)
512 int sym_bind
, old_sym_bind
;
518 if (sym
->a
.visibility
)
519 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
522 if (sym
->type
.t
& (VT_STATIC
| VT_INLINE
))
523 sym_bind
= STB_LOCAL
;
524 else if (sym
->a
.weak
)
527 sym_bind
= STB_GLOBAL
;
528 old_sym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
529 if (sym_bind
!= old_sym_bind
) {
530 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, ELFW(ST_TYPE
)(esym
->st_info
));
534 if (sym
->a
.dllimport
)
535 esym
->st_other
|= ST_PE_IMPORT
;
536 if (sym
->a
.dllexport
)
537 esym
->st_other
|= ST_PE_EXPORT
;
541 printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
542 get_tok_str(sym
->v
, NULL
),
543 sym_bind
== STB_WEAK
? 'w' : sym_bind
== STB_LOCAL
? 'l' : 'g',
551 /* ------------------------------------------------------------------------- */
552 /* update sym->c so that it points to an external symbol in section
553 'section' with value 'value' */
555 ST_FUNC
void put_extern_sym2(Sym
*sym
, int sh_num
,
556 addr_t value
, unsigned long size
,
557 int can_add_underscore
)
559 int sym_type
, sym_bind
, info
, other
, t
;
563 #ifdef CONFIG_TCC_BCHECK
568 name
= get_tok_str(sym
->v
, NULL
);
569 #ifdef CONFIG_TCC_BCHECK
570 if (tcc_state
->do_bounds_check
) {
571 /* XXX: avoid doing that for statics ? */
572 /* if bound checking is activated, we change some function
573 names by adding the "__bound" prefix */
576 /* XXX: we rely only on malloc hooks */
598 strcpy(buf
, "__bound_");
606 if ((t
& VT_BTYPE
) == VT_FUNC
) {
608 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
609 sym_type
= STT_NOTYPE
;
611 sym_type
= STT_OBJECT
;
613 if (t
& (VT_STATIC
| VT_INLINE
))
614 sym_bind
= STB_LOCAL
;
616 sym_bind
= STB_GLOBAL
;
619 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
620 Sym
*ref
= sym
->type
.ref
;
621 if (ref
->a
.nodecorate
) {
622 can_add_underscore
= 0;
624 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
625 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
627 other
|= ST_PE_STDCALL
;
628 can_add_underscore
= 0;
632 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
634 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
638 name
= get_tok_str(sym
->asm_label
, NULL
);
639 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
640 sym
->c
= put_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
643 esym
->st_value
= value
;
644 esym
->st_size
= size
;
645 esym
->st_shndx
= sh_num
;
650 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
651 addr_t value
, unsigned long size
)
653 int sh_num
= section
? section
->sh_num
: SHN_UNDEF
;
654 put_extern_sym2(sym
, sh_num
, value
, size
, 1);
657 /* add a new relocation entry to symbol 'sym' in section 's' */
658 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
663 if (nocode_wanted
&& s
== cur_text_section
)
668 put_extern_sym(sym
, NULL
, 0, 0);
672 /* now we can add ELF relocation info */
673 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
677 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
679 greloca(s
, sym
, offset
, type
, 0);
683 /* ------------------------------------------------------------------------- */
684 /* symbol allocator */
685 static Sym
*__sym_malloc(void)
687 Sym
*sym_pool
, *sym
, *last_sym
;
690 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
691 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
693 last_sym
= sym_free_first
;
695 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
696 sym
->next
= last_sym
;
700 sym_free_first
= last_sym
;
704 static inline Sym
*sym_malloc(void)
708 sym
= sym_free_first
;
710 sym
= __sym_malloc();
711 sym_free_first
= sym
->next
;
714 sym
= tcc_malloc(sizeof(Sym
));
719 ST_INLN
void sym_free(Sym
*sym
)
722 sym
->next
= sym_free_first
;
723 sym_free_first
= sym
;
729 /* push, without hashing */
730 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
735 memset(s
, 0, sizeof *s
);
745 /* find a symbol and return its associated structure. 's' is the top
746 of the symbol stack */
747 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
759 /* structure lookup */
760 ST_INLN Sym
*struct_find(int v
)
763 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
765 return table_ident
[v
]->sym_struct
;
768 /* find an identifier */
769 ST_INLN Sym
*sym_find(int v
)
772 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
774 return table_ident
[v
]->sym_identifier
;
777 static int sym_scope(Sym
*s
)
779 if (IS_ENUM_VAL (s
->type
.t
))
780 return s
->type
.ref
->sym_scope
;
785 /* push a given symbol on the symbol stack */
786 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
795 s
= sym_push2(ps
, v
, type
->t
, c
);
796 s
->type
.ref
= type
->ref
;
798 /* don't record fields or anonymous symbols */
800 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
801 /* record symbol in token array */
802 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
804 ps
= &ts
->sym_struct
;
806 ps
= &ts
->sym_identifier
;
809 s
->sym_scope
= local_scope
;
810 if (s
->prev_tok
&& sym_scope(s
->prev_tok
) == s
->sym_scope
)
811 tcc_error("redeclaration of '%s'",
812 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
817 /* push a global identifier */
818 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
821 s
= sym_push2(&global_stack
, v
, t
, c
);
822 s
->r
= VT_CONST
| VT_SYM
;
823 /* don't record anonymous symbol */
824 if (v
< SYM_FIRST_ANOM
) {
825 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
826 /* modify the top most local identifier, so that sym_identifier will
827 point to 's' when popped; happens when called from inline asm */
828 while (*ps
!= NULL
&& (*ps
)->sym_scope
)
829 ps
= &(*ps
)->prev_tok
;
836 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
837 pop them yet from the list, but do remove them from the token array. */
838 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
848 /* remove symbol in token array */
850 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
851 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
853 ps
= &ts
->sym_struct
;
855 ps
= &ts
->sym_identifier
;
866 /* ------------------------------------------------------------------------- */
867 static void vcheck_cmp(void)
869 /* cannot let cpu flags if other instruction are generated. Also
870 avoid leaving VT_JMP anywhere except on the top of the stack
871 because it would complicate the code generator.
873 Don't do this when nocode_wanted. vtop might come from
874 !nocode_wanted regions (see 88_codeopt.c) and transforming
875 it to a register without actually generating code is wrong
876 as their value might still be used for real. All values
877 we push under nocode_wanted will eventually be popped
878 again, so that the VT_CMP/VT_JMP value will be in vtop
879 when code is unsuppressed again. */
881 if (vtop
->r
== VT_CMP
&& !nocode_wanted
)
885 static void vsetc(CType
*type
, int r
, CValue
*vc
)
887 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
888 tcc_error("memory full (vstack)");
898 ST_FUNC
void vswap(void)
908 /* pop stack value */
909 ST_FUNC
void vpop(void)
912 v
= vtop
->r
& VT_VALMASK
;
913 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
914 /* for x86, we need to pop the FP stack */
916 o(0xd8dd); /* fstp %st(0) */
920 /* need to put correct jump if && or || without test */
927 /* push constant of type "type" with useless value */
928 static void vpush(CType
*type
)
930 vset(type
, VT_CONST
, 0);
933 /* push arbitrary 64bit constant */
934 static void vpush64(int ty
, unsigned long long v
)
941 vsetc(&ctype
, VT_CONST
, &cval
);
944 /* push integer constant */
945 ST_FUNC
void vpushi(int v
)
950 /* push a pointer sized constant */
951 static void vpushs(addr_t v
)
953 vpush64(VT_SIZE_T
, v
);
956 /* push long long constant */
957 static inline void vpushll(long long v
)
959 vpush64(VT_LLONG
, v
);
962 ST_FUNC
void vset(CType
*type
, int r
, int v
)
966 vsetc(type
, r
, &cval
);
969 static void vseti(int r
, int v
)
977 ST_FUNC
void vpushv(SValue
*v
)
979 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
980 tcc_error("memory full (vstack)");
985 static void vdup(void)
990 /* rotate n first stack elements to the bottom
991 I1 ... In -> I2 ... In I1 [top is right]
993 ST_FUNC
void vrotb(int n
)
1000 for(i
=-n
+1;i
!=0;i
++)
1001 vtop
[i
] = vtop
[i
+1];
1005 /* rotate the n elements before entry e towards the top
1006 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1008 ST_FUNC
void vrote(SValue
*e
, int n
)
1015 for(i
= 0;i
< n
- 1; i
++)
1020 /* rotate n first stack elements to the top
1021 I1 ... In -> In I1 ... I(n-1) [top is right]
1023 ST_FUNC
void vrott(int n
)
1028 /* ------------------------------------------------------------------------- */
1029 /* vtop->r = VT_CMP means CPU-flags have been set from comparison or test. */
1031 /* called from generators to set the result from relational ops */
1032 ST_FUNC
void vset_VT_CMP(int op
)
1040 /* called once before asking generators to load VT_CMP to a register */
1041 static void vset_VT_JMP(void)
1043 int op
= vtop
->cmp_op
;
1044 if (vtop
->jtrue
|| vtop
->jfalse
) {
1045 /* we need to jump to 'mov $0,%R' or 'mov $1,%R' */
1046 int inv
= op
& (op
< 2); /* small optimization */
1047 vseti(VT_JMP
+inv
, gvtst(inv
, 0));
1049 /* otherwise convert flags (rsp. 0/1) to register */
1051 if (op
< 2) /* doesn't seem to happen */
1056 /* Set CPU Flags, doesn't yet jump */
1057 static void gvtst_set(int inv
, int t
)
1060 if (vtop
->r
!= VT_CMP
) {
1063 if (vtop
->r
== VT_CMP
) /* must be VT_CONST otherwise */
1065 else if (vtop
->r
== VT_CONST
)
1066 vset_VT_CMP(vtop
->c
.i
!= 0);
1070 p
= inv
? &vtop
->jfalse
: &vtop
->jtrue
;
1071 *p
= gjmp_append(*p
, t
);
1074 /* Generate value test
1076 * Generate a test for any value (jump, comparison and integers) */
1077 static int gvtst(int inv
, int t
)
1083 t
= vtop
->jtrue
, u
= vtop
->jfalse
;
1085 x
= u
, u
= t
, t
= x
;
1088 /* jump to the wanted target */
1090 t
= gjmp_cond(op
^ inv
, t
);
1093 /* resolve complementary jumps to here */
1100 /* generate a zero or nozero test */
1101 static void gen_test_zero(int op
)
1103 if (vtop
->r
== VT_CMP
) {
1107 vtop
->jfalse
= vtop
->jtrue
;
1117 /* ------------------------------------------------------------------------- */
1118 /* push a symbol value of TYPE */
1119 static inline void vpushsym(CType
*type
, Sym
*sym
)
1123 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
1127 /* Return a static symbol pointing to a section */
1128 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
1134 sym
= sym_push(v
, type
, VT_CONST
| VT_SYM
, 0);
1135 sym
->type
.t
|= VT_STATIC
;
1136 put_extern_sym(sym
, sec
, offset
, size
);
1140 /* push a reference to a section offset by adding a dummy symbol */
1141 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
1143 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
1146 /* define a new external reference to a symbol 'v' of type 'u' */
1147 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
)
1153 /* push forward reference */
1154 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
1155 s
->type
.ref
= type
->ref
;
1156 } else if (IS_ASM_SYM(s
)) {
1157 s
->type
.t
= type
->t
| (s
->type
.t
& VT_EXTERN
);
1158 s
->type
.ref
= type
->ref
;
1164 /* Merge symbol attributes. */
1165 static void merge_symattr(struct SymAttr
*sa
, struct SymAttr
*sa1
)
1167 if (sa1
->aligned
&& !sa
->aligned
)
1168 sa
->aligned
= sa1
->aligned
;
1169 sa
->packed
|= sa1
->packed
;
1170 sa
->weak
|= sa1
->weak
;
1171 if (sa1
->visibility
!= STV_DEFAULT
) {
1172 int vis
= sa
->visibility
;
1173 if (vis
== STV_DEFAULT
1174 || vis
> sa1
->visibility
)
1175 vis
= sa1
->visibility
;
1176 sa
->visibility
= vis
;
1178 sa
->dllexport
|= sa1
->dllexport
;
1179 sa
->nodecorate
|= sa1
->nodecorate
;
1180 sa
->dllimport
|= sa1
->dllimport
;
1183 /* Merge function attributes. */
1184 static void merge_funcattr(struct FuncAttr
*fa
, struct FuncAttr
*fa1
)
1186 if (fa1
->func_call
&& !fa
->func_call
)
1187 fa
->func_call
= fa1
->func_call
;
1188 if (fa1
->func_type
&& !fa
->func_type
)
1189 fa
->func_type
= fa1
->func_type
;
1190 if (fa1
->func_args
&& !fa
->func_args
)
1191 fa
->func_args
= fa1
->func_args
;
1194 /* Merge attributes. */
1195 static void merge_attr(AttributeDef
*ad
, AttributeDef
*ad1
)
1197 merge_symattr(&ad
->a
, &ad1
->a
);
1198 merge_funcattr(&ad
->f
, &ad1
->f
);
1201 ad
->section
= ad1
->section
;
1202 if (ad1
->alias_target
)
1203 ad
->alias_target
= ad1
->alias_target
;
1205 ad
->asm_label
= ad1
->asm_label
;
1207 ad
->attr_mode
= ad1
->attr_mode
;
1210 /* Merge some type attributes. */
1211 static void patch_type(Sym
*sym
, CType
*type
)
1213 if (!(type
->t
& VT_EXTERN
) || IS_ENUM_VAL(sym
->type
.t
)) {
1214 if (!(sym
->type
.t
& VT_EXTERN
))
1215 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
1216 sym
->type
.t
&= ~VT_EXTERN
;
1219 if (IS_ASM_SYM(sym
)) {
1220 /* stay static if both are static */
1221 sym
->type
.t
= type
->t
& (sym
->type
.t
| ~VT_STATIC
);
1222 sym
->type
.ref
= type
->ref
;
1225 if (!is_compatible_types(&sym
->type
, type
)) {
1226 tcc_error("incompatible types for redefinition of '%s'",
1227 get_tok_str(sym
->v
, NULL
));
1229 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1230 int static_proto
= sym
->type
.t
& VT_STATIC
;
1231 /* warn if static follows non-static function declaration */
1232 if ((type
->t
& VT_STATIC
) && !static_proto
1233 /* XXX this test for inline shouldn't be here. Until we
1234 implement gnu-inline mode again it silences a warning for
1235 mingw caused by our workarounds. */
1236 && !((type
->t
| sym
->type
.t
) & VT_INLINE
))
1237 tcc_warning("static storage ignored for redefinition of '%s'",
1238 get_tok_str(sym
->v
, NULL
));
1240 /* set 'inline' if both agree or if one has static */
1241 if ((type
->t
| sym
->type
.t
) & VT_INLINE
) {
1242 if (!((type
->t
^ sym
->type
.t
) & VT_INLINE
)
1243 || ((type
->t
| sym
->type
.t
) & VT_STATIC
))
1244 static_proto
|= VT_INLINE
;
1247 if (0 == (type
->t
& VT_EXTERN
)) {
1248 /* put complete type, use static from prototype */
1249 sym
->type
.t
= (type
->t
& ~(VT_STATIC
|VT_INLINE
)) | static_proto
;
1250 sym
->type
.ref
= type
->ref
;
1252 sym
->type
.t
&= ~VT_INLINE
| static_proto
;
1255 if (sym
->type
.ref
->f
.func_type
== FUNC_OLD
1256 && type
->ref
->f
.func_type
!= FUNC_OLD
) {
1257 sym
->type
.ref
= type
->ref
;
1261 if ((sym
->type
.t
& VT_ARRAY
) && type
->ref
->c
>= 0) {
1262 /* set array size if it was omitted in extern declaration */
1263 sym
->type
.ref
->c
= type
->ref
->c
;
1265 if ((type
->t
^ sym
->type
.t
) & VT_STATIC
)
1266 tcc_warning("storage mismatch for redefinition of '%s'",
1267 get_tok_str(sym
->v
, NULL
));
1271 /* Merge some storage attributes. */
1272 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
1275 patch_type(sym
, type
);
1277 #ifdef TCC_TARGET_PE
1278 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
1279 tcc_error("incompatible dll linkage for redefinition of '%s'",
1280 get_tok_str(sym
->v
, NULL
));
1282 merge_symattr(&sym
->a
, &ad
->a
);
1284 sym
->asm_label
= ad
->asm_label
;
1285 update_storage(sym
);
1288 /* copy sym to other stack */
1289 static Sym
*sym_copy(Sym
*s0
, Sym
**ps
)
1292 s
= sym_malloc(), *s
= *s0
;
1293 s
->prev
= *ps
, *ps
= s
;
1294 if (s
->v
< SYM_FIRST_ANOM
) {
1295 ps
= &table_ident
[s
->v
- TOK_IDENT
]->sym_identifier
;
1296 s
->prev_tok
= *ps
, *ps
= s
;
1301 /* copy s->type.ref to stack 'ps' for VT_FUNC and VT_PTR */
1302 static void sym_copy_ref(Sym
*s
, Sym
**ps
)
1304 int bt
= s
->type
.t
& VT_BTYPE
;
1305 if (bt
== VT_FUNC
|| bt
== VT_PTR
) {
1306 Sym
**sp
= &s
->type
.ref
;
1307 for (s
= *sp
, *sp
= NULL
; s
; s
= s
->next
) {
1308 Sym
*s2
= sym_copy(s
, ps
);
1309 sp
= &(*sp
= s2
)->next
;
1310 sym_copy_ref(s2
, ps
);
1315 /* define a new external reference to a symbol 'v' */
1316 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
1320 /* look for global symbol */
1322 while (s
&& s
->sym_scope
)
1326 /* push forward reference */
1327 s
= global_identifier_push(v
, type
->t
, 0);
1330 s
->asm_label
= ad
->asm_label
;
1331 s
->type
.ref
= type
->ref
;
1332 /* copy type to the global stack */
1334 sym_copy_ref(s
, &global_stack
);
1336 patch_storage(s
, ad
, type
);
1338 /* push variables on local_stack if any */
1339 if (local_stack
&& (s
->type
.t
& VT_BTYPE
) != VT_FUNC
)
1340 s
= sym_copy(s
, &local_stack
);
1344 /* push a reference to global symbol v */
1345 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
1347 vpushsym(type
, external_global_sym(v
, type
));
1350 /* save registers up to (vtop - n) stack entry */
1351 ST_FUNC
void save_regs(int n
)
1354 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
1358 /* save r to the memory stack, and mark it as being free */
1359 ST_FUNC
void save_reg(int r
)
1361 save_reg_upstack(r
, 0);
1364 /* save r to the memory stack, and mark it as being free,
1365 if seen up to (vtop - n) stack entry */
1366 ST_FUNC
void save_reg_upstack(int r
, int n
)
1368 int l
, size
, align
, bt
;
1371 if ((r
&= VT_VALMASK
) >= VT_CONST
)
1376 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
1377 if ((p
->r
& VT_VALMASK
) == r
|| p
->r2
== r
) {
1378 /* must save value on stack if not already done */
1380 bt
= p
->type
.t
& VT_BTYPE
;
1383 if ((p
->r
& VT_LVAL
) || bt
== VT_FUNC
)
1386 size
= type_size(&sv
.type
, &align
);
1387 l
= get_temp_local_var(size
,align
);
1388 sv
.r
= VT_LOCAL
| VT_LVAL
;
1390 store(p
->r
& VT_VALMASK
, &sv
);
1391 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1392 /* x86 specific: need to pop fp register ST0 if saved */
1393 if (r
== TREG_ST0
) {
1394 o(0xd8dd); /* fstp %st(0) */
1397 /* special long long case */
1398 if (p
->r2
< VT_CONST
&& USING_TWO_WORDS(bt
)) {
1403 /* mark that stack entry as being saved on the stack */
1404 if (p
->r
& VT_LVAL
) {
1405 /* also clear the bounded flag because the
1406 relocation address of the function was stored in
1408 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
1410 p
->r
= VT_LVAL
| VT_LOCAL
;
1418 #ifdef TCC_TARGET_ARM
1419 /* find a register of class 'rc2' with at most one reference on stack.
1420 * If none, call get_reg(rc) */
1421 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
1426 for(r
=0;r
<NB_REGS
;r
++) {
1427 if (reg_classes
[r
] & rc2
) {
1430 for(p
= vstack
; p
<= vtop
; p
++) {
1431 if ((p
->r
& VT_VALMASK
) == r
||
1443 /* find a free register of class 'rc'. If none, save one register */
1444 ST_FUNC
int get_reg(int rc
)
1449 /* find a free register */
1450 for(r
=0;r
<NB_REGS
;r
++) {
1451 if (reg_classes
[r
] & rc
) {
1454 for(p
=vstack
;p
<=vtop
;p
++) {
1455 if ((p
->r
& VT_VALMASK
) == r
||
1464 /* no register left : free the first one on the stack (VERY
1465 IMPORTANT to start from the bottom to ensure that we don't
1466 spill registers used in gen_opi()) */
1467 for(p
=vstack
;p
<=vtop
;p
++) {
1468 /* look at second register (if long long) */
1470 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1472 r
= p
->r
& VT_VALMASK
;
1473 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1479 /* Should never comes here */
1483 /* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
1484 static int get_temp_local_var(int size
,int align
){
1486 struct temp_local_variable
*temp_var
;
1493 for(i
=0;i
<nb_temp_local_vars
;i
++){
1494 temp_var
=&arr_temp_local_vars
[i
];
1495 if(temp_var
->size
<size
||align
!=temp_var
->align
){
1498 /*check if temp_var is free*/
1500 for(p
=vstack
;p
<=vtop
;p
++) {
1502 if(r
==VT_LOCAL
||r
==VT_LLOCAL
){
1503 if(p
->c
.i
==temp_var
->location
){
1510 found_var
=temp_var
->location
;
1516 loc
= (loc
- size
) & -align
;
1517 if(nb_temp_local_vars
<MAX_TEMP_LOCAL_VARIABLE_NUMBER
){
1518 temp_var
=&arr_temp_local_vars
[i
];
1519 temp_var
->location
=loc
;
1520 temp_var
->size
=size
;
1521 temp_var
->align
=align
;
1522 nb_temp_local_vars
++;
1529 static void clear_temp_local_var_list(){
1530 nb_temp_local_vars
=0;
1533 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1535 static void move_reg(int r
, int s
, int t
)
1549 /* get address of vtop (vtop MUST BE an lvalue) */
1550 ST_FUNC
void gaddrof(void)
1552 vtop
->r
&= ~VT_LVAL
;
1553 /* tricky: if saved lvalue, then we can go back to lvalue */
1554 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1555 vtop
->r
= (vtop
->r
& ~VT_VALMASK
) | VT_LOCAL
| VT_LVAL
;
1558 #ifdef CONFIG_TCC_BCHECK
1559 /* generate lvalue bound code */
1560 static void gbound(void)
1564 vtop
->r
&= ~VT_MUSTBOUND
;
1565 /* if lvalue, then use checking code before dereferencing */
1566 if (vtop
->r
& VT_LVAL
) {
1567 /* if not VT_BOUNDED value, then make one */
1568 if (!(vtop
->r
& VT_BOUNDED
)) {
1569 /* must save type because we must set it to int to get pointer */
1571 vtop
->type
.t
= VT_PTR
;
1574 gen_bounded_ptr_add();
1578 /* then check for dereferencing */
1579 gen_bounded_ptr_deref();
1583 /* we need to call __bound_ptr_add before we start to load function
1584 args into registers */
1585 ST_FUNC
void gbound_args(int nb_args
)
1588 for (i
= 1; i
<= nb_args
; ++i
)
1589 if (vtop
[1 - i
].r
& VT_MUSTBOUND
) {
1596 /* Add bounds for local symbols from S to E (via ->prev) */
1597 static void add_local_bounds(Sym
*s
, Sym
*e
)
1599 for (; s
!= e
; s
= s
->prev
) {
1600 if (!s
->v
|| (s
->r
& VT_VALMASK
) != VT_LOCAL
)
1602 /* Add arrays/structs/unions because we always take address */
1603 if ((s
->type
.t
& VT_ARRAY
)
1604 || (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
1605 || s
->a
.addrtaken
) {
1606 /* add local bound info */
1607 int align
, size
= type_size(&s
->type
, &align
);
1608 addr_t
*bounds_ptr
= section_ptr_add(lbounds_section
,
1609 2 * sizeof(addr_t
));
1610 bounds_ptr
[0] = s
->c
;
1611 bounds_ptr
[1] = size
;
1617 /* Wrapper around sym_pop, that potentially also registers local bounds. */
1618 static void pop_local_syms(Sym
**ptop
, Sym
*b
, int keep
)
1620 #ifdef CONFIG_TCC_BCHECK
1621 if (!keep
&& tcc_state
->do_bounds_check
)
1622 add_local_bounds(*ptop
, b
);
1624 sym_pop(ptop
, b
, keep
);
1627 static void incr_bf_adr(int o
)
1629 vtop
->type
= char_pointer_type
;
1633 vtop
->type
.t
= VT_BYTE
| VT_UNSIGNED
;
1637 /* single-byte load mode for packed or otherwise unaligned bitfields */
1638 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1641 save_reg_upstack(vtop
->r
, 1);
1642 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1643 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1652 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1654 vpushi((1 << n
) - 1), gen_op('&');
1657 vpushi(bits
), gen_op(TOK_SHL
);
1660 bits
+= n
, bit_size
-= n
, o
= 1;
1663 if (!(type
->t
& VT_UNSIGNED
)) {
1664 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG
? 64 : 32) - bits
;
1665 vpushi(n
), gen_op(TOK_SHL
);
1666 vpushi(n
), gen_op(TOK_SAR
);
1670 /* single-byte store mode for packed or otherwise unaligned bitfields */
1671 static void store_packed_bf(int bit_pos
, int bit_size
)
1673 int bits
, n
, o
, m
, c
;
1675 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1677 save_reg_upstack(vtop
->r
, 1);
1678 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1680 incr_bf_adr(o
); // X B
1682 c
? vdup() : gv_dup(); // B V X
1685 vpushi(bits
), gen_op(TOK_SHR
);
1687 vpushi(bit_pos
), gen_op(TOK_SHL
);
1692 m
= ((1 << n
) - 1) << bit_pos
;
1693 vpushi(m
), gen_op('&'); // X B V1
1694 vpushv(vtop
-1); // X B V1 B
1695 vpushi(m
& 0x80 ? ~m
& 0x7f : ~m
);
1696 gen_op('&'); // X B V1 B1
1697 gen_op('|'); // X B V2
1699 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1700 vstore(), vpop(); // X B
1701 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1706 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1709 if (0 == sv
->type
.ref
)
1711 t
= sv
->type
.ref
->auxtype
;
1712 if (t
!= -1 && t
!= VT_STRUCT
) {
1713 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1719 /* store vtop a register belonging to class 'rc'. lvalues are
1720 converted to values. Cannot be used if cannot be converted to
1721 register value (such as structures). */
1722 ST_FUNC
int gv(int rc
)
1724 int r
, r2
, r_ok
, r2_ok
, rc2
, bt
;
1725 int bit_pos
, bit_size
, size
, align
;
1727 /* NOTE: get_reg can modify vstack[] */
1728 if (vtop
->type
.t
& VT_BITFIELD
) {
1731 bit_pos
= BIT_POS(vtop
->type
.t
);
1732 bit_size
= BIT_SIZE(vtop
->type
.t
);
1733 /* remove bit field info to avoid loops */
1734 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1737 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1738 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1739 type
.t
|= VT_UNSIGNED
;
1741 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1743 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1748 if (r
== VT_STRUCT
) {
1749 load_packed_bf(&type
, bit_pos
, bit_size
);
1751 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG
? 64 : 32;
1752 /* cast to int to propagate signedness in following ops */
1754 /* generate shifts */
1755 vpushi(bits
- (bit_pos
+ bit_size
));
1757 vpushi(bits
- bit_size
);
1758 /* NOTE: transformed to SHR if unsigned */
1763 if (is_float(vtop
->type
.t
) &&
1764 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1765 unsigned long offset
;
1766 /* CPUs usually cannot use float constants, so we store them
1767 generically in data segment */
1768 size
= type_size(&vtop
->type
, &align
);
1770 size
= 0, align
= 1;
1771 offset
= section_add(data_section
, size
, align
);
1772 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1774 init_putv(&vtop
->type
, data_section
, offset
);
1777 #ifdef CONFIG_TCC_BCHECK
1778 if (vtop
->r
& VT_MUSTBOUND
)
1782 bt
= vtop
->type
.t
& VT_BTYPE
;
1784 #ifdef TCC_TARGET_RISCV64
1786 if (bt
== VT_LDOUBLE
&& rc
== RC_FLOAT
)
1789 rc2
= RC2_TYPE(bt
, rc
);
1791 /* need to reload if:
1793 - lvalue (need to dereference pointer)
1794 - already a register, but not in the right class */
1795 r
= vtop
->r
& VT_VALMASK
;
1796 r_ok
= !(vtop
->r
& VT_LVAL
) && (r
< VT_CONST
) && (reg_classes
[r
] & rc
);
1797 r2_ok
= !rc2
|| ((vtop
->r2
< VT_CONST
) && (reg_classes
[vtop
->r2
] & rc2
));
1799 if (!r_ok
|| !r2_ok
) {
1803 int load_type
= (bt
== VT_QFLOAT
) ? VT_DOUBLE
: VT_PTRDIFF_T
;
1804 int original_type
= vtop
->type
.t
;
1806 /* two register type load :
1807 expand to two words temporarily */
1808 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1810 unsigned long long ll
= vtop
->c
.i
;
1811 vtop
->c
.i
= ll
; /* first word */
1813 vtop
->r
= r
; /* save register value */
1814 vpushi(ll
>> 32); /* second word */
1815 } else if (vtop
->r
& VT_LVAL
) {
1816 /* We do not want to modifier the long long pointer here.
1817 So we save any other instances down the stack */
1818 save_reg_upstack(vtop
->r
, 1);
1819 /* load from memory */
1820 vtop
->type
.t
= load_type
;
1823 vtop
[-1].r
= r
; /* save register value */
1824 /* increment pointer to get second word */
1825 vtop
->type
.t
= VT_PTRDIFF_T
;
1830 vtop
->type
.t
= load_type
;
1832 /* move registers */
1835 if (r2_ok
&& vtop
->r2
< VT_CONST
)
1838 vtop
[-1].r
= r
; /* save register value */
1839 vtop
->r
= vtop
[-1].r2
;
1841 /* Allocate second register. Here we rely on the fact that
1842 get_reg() tries first to free r2 of an SValue. */
1846 /* write second register */
1849 vtop
->type
.t
= original_type
;
1851 if (vtop
->r
== VT_CMP
)
1853 /* one register type load */
1858 #ifdef TCC_TARGET_C67
1859 /* uses register pairs for doubles */
1860 if (bt
== VT_DOUBLE
)
1867 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1868 ST_FUNC
void gv2(int rc1
, int rc2
)
1870 /* generate more generic register first. But VT_JMP or VT_CMP
1871 values must be generated first in all cases to avoid possible
1873 if (vtop
->r
!= VT_CMP
&& rc1
<= rc2
) {
1878 /* test if reload is needed for first register */
1879 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1889 /* test if reload is needed for first register */
1890 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1897 /* expand 64bit on stack in two ints */
1898 ST_FUNC
void lexpand(void)
1901 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1902 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1903 if (v
== VT_CONST
) {
1906 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1912 vtop
[0].r
= vtop
[-1].r2
;
1913 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1915 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1920 /* build a long long from two ints */
1921 static void lbuild(int t
)
1923 gv2(RC_INT
, RC_INT
);
1924 vtop
[-1].r2
= vtop
[0].r
;
1925 vtop
[-1].type
.t
= t
;
1930 /* convert stack entry to register and duplicate its value in another
1932 static void gv_dup(void)
1938 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1939 if (t
& VT_BITFIELD
) {
1949 /* stack: H L L1 H1 */
1959 /* duplicate value */
1969 /* generate CPU independent (unsigned) long long operations */
1970 static void gen_opl(int op
)
1972 int t
, a
, b
, op1
, c
, i
;
1974 unsigned short reg_iret
= REG_IRET
;
1975 unsigned short reg_lret
= REG_IRE2
;
1981 func
= TOK___divdi3
;
1984 func
= TOK___udivdi3
;
1987 func
= TOK___moddi3
;
1990 func
= TOK___umoddi3
;
1997 /* call generic long long function */
1998 vpush_global_sym(&func_old_type
, func
);
2003 vtop
->r2
= reg_lret
;
2011 //pv("gen_opl A",0,2);
2017 /* stack: L1 H1 L2 H2 */
2022 vtop
[-2] = vtop
[-3];
2025 /* stack: H1 H2 L1 L2 */
2026 //pv("gen_opl B",0,4);
2032 /* stack: H1 H2 L1 L2 ML MH */
2035 /* stack: ML MH H1 H2 L1 L2 */
2039 /* stack: ML MH H1 L2 H2 L1 */
2044 /* stack: ML MH M1 M2 */
2047 } else if (op
== '+' || op
== '-') {
2048 /* XXX: add non carry method too (for MIPS or alpha) */
2054 /* stack: H1 H2 (L1 op L2) */
2057 gen_op(op1
+ 1); /* TOK_xxxC2 */
2060 /* stack: H1 H2 (L1 op L2) */
2063 /* stack: (L1 op L2) H1 H2 */
2065 /* stack: (L1 op L2) (H1 op H2) */
2073 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
2074 t
= vtop
[-1].type
.t
;
2078 /* stack: L H shift */
2080 /* constant: simpler */
2081 /* NOTE: all comments are for SHL. the other cases are
2082 done by swapping words */
2093 if (op
!= TOK_SAR
) {
2126 /* XXX: should provide a faster fallback on x86 ? */
2129 func
= TOK___ashrdi3
;
2132 func
= TOK___lshrdi3
;
2135 func
= TOK___ashldi3
;
2141 /* compare operations */
2147 /* stack: L1 H1 L2 H2 */
2149 vtop
[-1] = vtop
[-2];
2151 /* stack: L1 L2 H1 H2 */
2155 /* when values are equal, we need to compare low words. since
2156 the jump is inverted, we invert the test too. */
2159 else if (op1
== TOK_GT
)
2161 else if (op1
== TOK_ULT
)
2163 else if (op1
== TOK_UGT
)
2173 /* generate non equal test */
2175 vset_VT_CMP(TOK_NE
);
2179 /* compare low. Always unsigned */
2183 else if (op1
== TOK_LE
)
2185 else if (op1
== TOK_GT
)
2187 else if (op1
== TOK_GE
)
2190 #if 0//def TCC_TARGET_I386
2191 if (op
== TOK_NE
) { gsym(b
); break; }
2192 if (op
== TOK_EQ
) { gsym(a
); break; }
2201 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
2203 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
2204 return (a
^ b
) >> 63 ? -x
: x
;
2207 static int gen_opic_lt(uint64_t a
, uint64_t b
)
2209 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
2212 /* handle integer constant optimizations and various machine
2214 static void gen_opic(int op
)
2216 SValue
*v1
= vtop
- 1;
2218 int t1
= v1
->type
.t
& VT_BTYPE
;
2219 int t2
= v2
->type
.t
& VT_BTYPE
;
2220 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2221 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2222 uint64_t l1
= c1
? v1
->c
.i
: 0;
2223 uint64_t l2
= c2
? v2
->c
.i
: 0;
2224 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
2226 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2227 l1
= ((uint32_t)l1
|
2228 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2229 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
2230 l2
= ((uint32_t)l2
|
2231 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
2235 case '+': l1
+= l2
; break;
2236 case '-': l1
-= l2
; break;
2237 case '&': l1
&= l2
; break;
2238 case '^': l1
^= l2
; break;
2239 case '|': l1
|= l2
; break;
2240 case '*': l1
*= l2
; break;
2247 /* if division by zero, generate explicit division */
2249 if (const_wanted
&& !(nocode_wanted
& unevalmask
))
2250 tcc_error("division by zero in constant");
2254 default: l1
= gen_opic_sdiv(l1
, l2
); break;
2255 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
2256 case TOK_UDIV
: l1
= l1
/ l2
; break;
2257 case TOK_UMOD
: l1
= l1
% l2
; break;
2260 case TOK_SHL
: l1
<<= (l2
& shm
); break;
2261 case TOK_SHR
: l1
>>= (l2
& shm
); break;
2263 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
2266 case TOK_ULT
: l1
= l1
< l2
; break;
2267 case TOK_UGE
: l1
= l1
>= l2
; break;
2268 case TOK_EQ
: l1
= l1
== l2
; break;
2269 case TOK_NE
: l1
= l1
!= l2
; break;
2270 case TOK_ULE
: l1
= l1
<= l2
; break;
2271 case TOK_UGT
: l1
= l1
> l2
; break;
2272 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
2273 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
2274 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
2275 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
2277 case TOK_LAND
: l1
= l1
&& l2
; break;
2278 case TOK_LOR
: l1
= l1
|| l2
; break;
2282 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2283 l1
= ((uint32_t)l1
|
2284 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2288 /* if commutative ops, put c2 as constant */
2289 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
2290 op
== '|' || op
== '*' || op
== TOK_EQ
|| op
== TOK_NE
)) {
2292 c2
= c1
; //c = c1, c1 = c2, c2 = c;
2293 l2
= l1
; //l = l1, l1 = l2, l2 = l;
2295 if (!const_wanted
&&
2297 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
2298 (l1
== -1 && op
== TOK_SAR
))) {
2299 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
2301 } else if (!const_wanted
&&
2302 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
2304 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
2305 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
2306 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
2311 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
2314 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
2315 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
2318 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
2319 /* filter out NOP operations like x*1, x-0, x&-1... */
2321 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
2322 /* try to use shifts instead of muls or divs */
2323 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
2332 else if (op
== TOK_PDIV
)
2338 } else if (c2
&& (op
== '+' || op
== '-') &&
2339 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
2340 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
2341 /* symbol + constant case */
2345 /* The backends can't always deal with addends to symbols
2346 larger than +-1<<31. Don't construct such. */
2353 /* call low level op generator */
2354 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
2355 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
2363 /* generate a floating point operation with constant propagation */
2364 static void gen_opif(int op
)
2368 #if defined _MSC_VER && defined __x86_64__
2369 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
2376 /* currently, we cannot do computations with forward symbols */
2377 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2378 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2380 if (v1
->type
.t
== VT_FLOAT
) {
2383 } else if (v1
->type
.t
== VT_DOUBLE
) {
2391 /* NOTE: we only do constant propagation if finite number (not
2392 NaN or infinity) (ANSI spec) */
2393 if (!ieee_finite(f1
) || !ieee_finite(f2
))
2397 case '+': f1
+= f2
; break;
2398 case '-': f1
-= f2
; break;
2399 case '*': f1
*= f2
; break;
2402 /* If not in initializer we need to potentially generate
2403 FP exceptions at runtime, otherwise we want to fold. */
2409 /* XXX: also handles tests ? */
2413 /* XXX: overflow test ? */
2414 if (v1
->type
.t
== VT_FLOAT
) {
2416 } else if (v1
->type
.t
== VT_DOUBLE
) {
2428 static int pointed_size(CType
*type
)
2431 return type_size(pointed_type(type
), &align
);
2434 static void vla_runtime_pointed_size(CType
*type
)
2437 vla_runtime_type_size(pointed_type(type
), &align
);
2440 static inline int is_null_pointer(SValue
*p
)
2442 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2444 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2445 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2446 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2447 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0) &&
2448 ((pointed_type(&p
->type
)->t
& VT_BTYPE
) == VT_VOID
) &&
2449 0 == (pointed_type(&p
->type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
2453 /* check types for comparison or subtraction of pointers */
2454 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2456 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2459 /* null pointers are accepted for all comparisons as gcc */
2460 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2464 bt1
= type1
->t
& VT_BTYPE
;
2465 bt2
= type2
->t
& VT_BTYPE
;
2466 /* accept comparison between pointer and integer with a warning */
2467 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2468 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2469 tcc_warning("comparison between pointer and integer");
2473 /* both must be pointers or implicit function pointers */
2474 if (bt1
== VT_PTR
) {
2475 type1
= pointed_type(type1
);
2476 } else if (bt1
!= VT_FUNC
)
2477 goto invalid_operands
;
2479 if (bt2
== VT_PTR
) {
2480 type2
= pointed_type(type2
);
2481 } else if (bt2
!= VT_FUNC
) {
2483 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2485 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2486 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2490 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2491 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2492 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2493 /* gcc-like error if '-' is used */
2495 goto invalid_operands
;
2497 tcc_warning("comparison of distinct pointer types lacks a cast");
2501 /* generic gen_op: handles types problems */
2502 ST_FUNC
void gen_op(int op
)
2504 int u
, t1
, t2
, bt1
, bt2
, t
;
2508 t1
= vtop
[-1].type
.t
;
2509 t2
= vtop
[0].type
.t
;
2510 bt1
= t1
& VT_BTYPE
;
2511 bt2
= t2
& VT_BTYPE
;
2513 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2514 tcc_error("operation on a struct");
2515 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2516 if (bt2
== VT_FUNC
) {
2517 mk_pointer(&vtop
->type
);
2520 if (bt1
== VT_FUNC
) {
2522 mk_pointer(&vtop
->type
);
2527 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2528 /* at least one operand is a pointer */
2529 /* relational op: must be both pointers */
2530 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2531 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2532 /* pointers are handled are unsigned */
2534 t
= VT_LLONG
| VT_UNSIGNED
;
2536 t
= VT_INT
| VT_UNSIGNED
;
2540 /* if both pointers, then it must be the '-' op */
2541 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2543 tcc_error("cannot use pointers here");
2544 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2545 /* XXX: check that types are compatible */
2546 if (vtop
[-1].type
.t
& VT_VLA
) {
2547 vla_runtime_pointed_size(&vtop
[-1].type
);
2549 vpushi(pointed_size(&vtop
[-1].type
));
2553 vtop
->type
.t
= VT_PTRDIFF_T
;
2557 /* exactly one pointer : must be '+' or '-'. */
2558 if (op
!= '-' && op
!= '+')
2559 tcc_error("cannot use pointers here");
2560 /* Put pointer as first operand */
2561 if (bt2
== VT_PTR
) {
2563 t
= t1
, t1
= t2
, t2
= t
;
2566 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2567 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2570 type1
= vtop
[-1].type
;
2571 type1
.t
&= ~VT_ARRAY
;
2572 if (vtop
[-1].type
.t
& VT_VLA
)
2573 vla_runtime_pointed_size(&vtop
[-1].type
);
2575 u
= pointed_size(&vtop
[-1].type
);
2577 tcc_error("unknown array element size");
2581 /* XXX: cast to int ? (long long case) */
2586 #ifdef CONFIG_TCC_BCHECK
2587 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2588 /* if bounded pointers, we generate a special code to
2595 vtop
[-1].r
&= ~VT_MUSTBOUND
;
2596 gen_bounded_ptr_add();
2602 /* put again type if gen_opic() swaped operands */
2605 } else if (is_float(bt1
) || is_float(bt2
)) {
2606 /* compute bigger type and do implicit casts */
2607 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2609 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2614 /* floats can only be used for a few operations */
2615 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2616 (op
< TOK_ULT
|| op
> TOK_GT
))
2617 tcc_error("invalid operands for binary operation");
2619 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2620 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2621 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2623 t
|= (VT_LONG
& t1
);
2625 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2626 /* cast to biggest op */
2627 t
= VT_LLONG
| VT_LONG
;
2628 if (bt1
== VT_LLONG
)
2630 if (bt2
== VT_LLONG
)
2632 /* convert to unsigned if it does not fit in a long long */
2633 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2634 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2638 /* integer operations */
2639 t
= VT_INT
| (VT_LONG
& (t1
| t2
));
2640 /* convert to unsigned if it does not fit in an integer */
2641 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2642 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2645 /* XXX: currently, some unsigned operations are explicit, so
2646 we modify them here */
2647 if (t
& VT_UNSIGNED
) {
2654 else if (op
== TOK_LT
)
2656 else if (op
== TOK_GT
)
2658 else if (op
== TOK_LE
)
2660 else if (op
== TOK_GE
)
2668 /* special case for shifts and long long: we keep the shift as
2670 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2677 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2678 /* relational op: the result is an int */
2679 vtop
->type
.t
= VT_INT
;
2684 // Make sure that we have converted to an rvalue:
2685 if (vtop
->r
& VT_LVAL
)
2686 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2689 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64 || defined TCC_TARGET_ARM
2690 #define gen_cvt_itof1 gen_cvt_itof
2692 /* generic itof for unsigned long long case */
2693 static void gen_cvt_itof1(int t
)
2695 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2696 (VT_LLONG
| VT_UNSIGNED
)) {
2699 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2700 #if LDOUBLE_SIZE != 8
2701 else if (t
== VT_LDOUBLE
)
2702 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2705 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2716 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
2717 #define gen_cvt_ftoi1 gen_cvt_ftoi
2719 /* generic ftoi for unsigned long long case */
2720 static void gen_cvt_ftoi1(int t
)
2723 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2724 /* not handled natively */
2725 st
= vtop
->type
.t
& VT_BTYPE
;
2727 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2728 #if LDOUBLE_SIZE != 8
2729 else if (st
== VT_LDOUBLE
)
2730 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2733 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2744 /* special delayed cast for char/short */
2745 static void force_charshort_cast(void)
2747 int sbt
= BFGET(vtop
->r
, VT_MUSTCAST
) == 2 ? VT_LLONG
: VT_INT
;
2748 int dbt
= vtop
->type
.t
;
2749 vtop
->r
&= ~VT_MUSTCAST
;
2751 gen_cast_s(dbt
== VT_BOOL
? VT_BYTE
|VT_UNSIGNED
: dbt
);
2755 static void gen_cast_s(int t
)
2763 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2764 static void gen_cast(CType
*type
)
2766 int sbt
, dbt
, sf
, df
, c
;
2767 int dbt_bt
, sbt_bt
, ds
, ss
, bits
, trunc
;
2769 /* special delayed cast for char/short */
2770 if (vtop
->r
& VT_MUSTCAST
)
2771 force_charshort_cast();
2773 /* bitfields first get cast to ints */
2774 if (vtop
->type
.t
& VT_BITFIELD
)
2777 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2778 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2786 dbt_bt
= dbt
& VT_BTYPE
;
2787 sbt_bt
= sbt
& VT_BTYPE
;
2789 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2790 #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
2791 c
&= (dbt
!= VT_LDOUBLE
) | !!nocode_wanted
;
2794 /* constant case: we can do it now */
2795 /* XXX: in ISOC, cannot do it if error in convert */
2796 if (sbt
== VT_FLOAT
)
2797 vtop
->c
.ld
= vtop
->c
.f
;
2798 else if (sbt
== VT_DOUBLE
)
2799 vtop
->c
.ld
= vtop
->c
.d
;
2802 if (sbt_bt
== VT_LLONG
) {
2803 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2804 vtop
->c
.ld
= vtop
->c
.i
;
2806 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2808 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2809 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2811 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2814 if (dbt
== VT_FLOAT
)
2815 vtop
->c
.f
= (float)vtop
->c
.ld
;
2816 else if (dbt
== VT_DOUBLE
)
2817 vtop
->c
.d
= (double)vtop
->c
.ld
;
2818 } else if (sf
&& dbt
== VT_BOOL
) {
2819 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2822 vtop
->c
.i
= vtop
->c
.ld
;
2823 else if (sbt_bt
== VT_LLONG
|| (PTR_SIZE
== 8 && sbt
== VT_PTR
))
2825 else if (sbt
& VT_UNSIGNED
)
2826 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2828 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
| -(vtop
->c
.i
& 0x80000000));
2830 if (dbt_bt
== VT_LLONG
|| (PTR_SIZE
== 8 && dbt
== VT_PTR
))
2832 else if (dbt
== VT_BOOL
)
2833 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2835 uint32_t m
= dbt_bt
== VT_BYTE
? 0xff :
2836 dbt_bt
== VT_SHORT
? 0xffff :
2839 if (!(dbt
& VT_UNSIGNED
))
2840 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2845 } else if (dbt
== VT_BOOL
2846 && (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
))
2847 == (VT_CONST
| VT_SYM
)) {
2848 /* addresses are considered non-zero (see tcctest.c:sinit23) */
2854 /* cannot generate code for global or static initializers */
2855 if (STATIC_DATA_WANTED
)
2858 /* non constant case: generate code */
2859 if (dbt
== VT_BOOL
) {
2860 gen_test_zero(TOK_NE
);
2866 /* convert from fp to fp */
2869 /* convert int to fp */
2872 /* convert fp to int */
2874 if (dbt_bt
!= VT_LLONG
&& dbt_bt
!= VT_INT
)
2877 goto again
; /* may need char/short cast */
2882 ds
= btype_size(dbt_bt
);
2883 ss
= btype_size(sbt_bt
);
2884 if (ds
== 0 || ss
== 0) {
2885 if (dbt_bt
== VT_VOID
)
2887 cast_error(&vtop
->type
, type
);
2889 if (IS_ENUM(type
->t
) && type
->ref
->c
< 0)
2890 tcc_error("cast to incomplete type");
2892 /* same size and no sign conversion needed */
2893 if (ds
== ss
&& ds
>= 4)
2895 if (dbt_bt
== VT_PTR
|| sbt_bt
== VT_PTR
) {
2896 tcc_warning("cast between pointer and integer of different size");
2897 if (sbt_bt
== VT_PTR
) {
2898 /* put integer type to allow logical operations below */
2899 vtop
->type
.t
= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
);
2903 /* processor allows { int a = 0, b = *(char*)&a; }
2904 That means that if we cast to less width, we can just
2905 change the type and read it still later. */
2906 #define ALLOW_SUBTYPE_ACCESS 1
2908 if (ALLOW_SUBTYPE_ACCESS
&& (vtop
->r
& VT_LVAL
)) {
2909 /* value still in memory */
2915 goto done
; /* no 64bit envolved */
2923 /* generate high word */
2924 if (sbt
& VT_UNSIGNED
) {
2933 } else if (ss
== 8) {
2934 /* from long long: just take low order word */
2942 /* need to convert from 32bit to 64bit */
2943 if (sbt
& VT_UNSIGNED
) {
2944 #if defined(TCC_TARGET_RISCV64)
2945 /* RISC-V keeps 32bit vals in registers sign-extended.
2946 So here we need a zero-extension. */
2955 ss
= ds
, ds
= 4, dbt
= sbt
;
2956 } else if (ss
== 8) {
2957 /* XXX some architectures (e.g. risc-v) would like it
2958 better for this merely being a 32-to-64 sign or zero-
2960 trunc
= 32; /* zero upper 32 bits */
2968 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM64
2974 bits
= (ss
- ds
) * 8;
2975 /* for unsigned, gen_op will convert SAR to SHR */
2976 vtop
->type
.t
= (ss
== 8 ? VT_LLONG
: VT_INT
) | (dbt
& VT_UNSIGNED
);
2979 vpushi(bits
- trunc
);
2986 vtop
->type
.t
&= ~ ( VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
2989 /* return type size as known at compile time. Put alignment at 'a' */
2990 ST_FUNC
int type_size(CType
*type
, int *a
)
2995 bt
= type
->t
& VT_BTYPE
;
2996 if (bt
== VT_STRUCT
) {
3001 } else if (bt
== VT_PTR
) {
3002 if (type
->t
& VT_ARRAY
) {
3006 ts
= type_size(&s
->type
, a
);
3008 if (ts
< 0 && s
->c
< 0)
3016 } else if (IS_ENUM(type
->t
) && type
->ref
->c
< 0) {
3017 return -1; /* incomplete enum */
3018 } else if (bt
== VT_LDOUBLE
) {
3020 return LDOUBLE_SIZE
;
3021 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
3022 #ifdef TCC_TARGET_I386
3023 #ifdef TCC_TARGET_PE
3028 #elif defined(TCC_TARGET_ARM)
3038 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
3041 } else if (bt
== VT_SHORT
) {
3044 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
3048 /* char, void, function, _Bool */
3054 /* push type size as known at runtime time on top of value stack. Put
3056 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
3058 if (type
->t
& VT_VLA
) {
3059 type_size(&type
->ref
->type
, a
);
3060 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
3062 vpushi(type_size(type
, a
));
3066 /* return the pointed type of t */
3067 static inline CType
*pointed_type(CType
*type
)
3069 return &type
->ref
->type
;
3072 /* modify type so that its it is a pointer to type. */
3073 ST_FUNC
void mk_pointer(CType
*type
)
3076 s
= sym_push(SYM_FIELD
, type
, 0, -1);
3077 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
3081 /* compare function types. OLD functions match any new functions */
3082 static int is_compatible_func(CType
*type1
, CType
*type2
)
3088 if (s1
->f
.func_call
!= s2
->f
.func_call
)
3090 if (s1
->f
.func_type
!= s2
->f
.func_type
3091 && s1
->f
.func_type
!= FUNC_OLD
3092 && s2
->f
.func_type
!= FUNC_OLD
)
3094 /* we should check the function return type for FUNC_OLD too
3095 but that causes problems with the internally used support
3096 functions such as TOK_memmove */
3097 if (s1
->f
.func_type
== FUNC_OLD
&& !s1
->next
)
3099 if (s2
->f
.func_type
== FUNC_OLD
&& !s2
->next
)
3102 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
3113 /* return true if type1 and type2 are the same. If unqualified is
3114 true, qualifiers on the types are ignored.
3116 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
3120 t1
= type1
->t
& VT_TYPE
;
3121 t2
= type2
->t
& VT_TYPE
;
3123 /* strip qualifiers before comparing */
3124 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3125 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3128 /* Default Vs explicit signedness only matters for char */
3129 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
3133 /* XXX: bitfields ? */
3138 && !(type1
->ref
->c
< 0
3139 || type2
->ref
->c
< 0
3140 || type1
->ref
->c
== type2
->ref
->c
))
3143 /* test more complicated cases */
3144 bt1
= t1
& VT_BTYPE
;
3145 if (bt1
== VT_PTR
) {
3146 type1
= pointed_type(type1
);
3147 type2
= pointed_type(type2
);
3148 return is_compatible_types(type1
, type2
);
3149 } else if (bt1
== VT_STRUCT
) {
3150 return (type1
->ref
== type2
->ref
);
3151 } else if (bt1
== VT_FUNC
) {
3152 return is_compatible_func(type1
, type2
);
3153 } else if (IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)) {
3154 return type1
->ref
== type2
->ref
;
3160 /* return true if type1 and type2 are exactly the same (including
3163 static int is_compatible_types(CType
*type1
, CType
*type2
)
3165 return compare_types(type1
,type2
,0);
3168 /* return true if type1 and type2 are the same (ignoring qualifiers).
3170 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
3172 return compare_types(type1
,type2
,1);
3175 /* print a type. If 'varstr' is not NULL, then the variable is also
3176 printed in the type */
3178 /* XXX: add array and function pointers */
3179 static void type_to_str(char *buf
, int buf_size
,
3180 CType
*type
, const char *varstr
)
3192 pstrcat(buf
, buf_size
, "extern ");
3194 pstrcat(buf
, buf_size
, "static ");
3196 pstrcat(buf
, buf_size
, "typedef ");
3198 pstrcat(buf
, buf_size
, "inline ");
3199 if (t
& VT_VOLATILE
)
3200 pstrcat(buf
, buf_size
, "volatile ");
3201 if (t
& VT_CONSTANT
)
3202 pstrcat(buf
, buf_size
, "const ");
3204 if (((t
& VT_DEFSIGN
) && bt
== VT_BYTE
)
3205 || ((t
& VT_UNSIGNED
)
3206 && (bt
== VT_SHORT
|| bt
== VT_INT
|| bt
== VT_LLONG
)
3209 pstrcat(buf
, buf_size
, (t
& VT_UNSIGNED
) ? "unsigned " : "signed ");
3211 buf_size
-= strlen(buf
);
3246 tstr
= "long double";
3248 pstrcat(buf
, buf_size
, tstr
);
3255 pstrcat(buf
, buf_size
, tstr
);
3256 v
= type
->ref
->v
& ~SYM_STRUCT
;
3257 if (v
>= SYM_FIRST_ANOM
)
3258 pstrcat(buf
, buf_size
, "<anonymous>");
3260 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
3265 if (varstr
&& '*' == *varstr
) {
3266 pstrcat(buf1
, sizeof(buf1
), "(");
3267 pstrcat(buf1
, sizeof(buf1
), varstr
);
3268 pstrcat(buf1
, sizeof(buf1
), ")");
3270 pstrcat(buf1
, buf_size
, "(");
3272 while (sa
!= NULL
) {
3274 type_to_str(buf2
, sizeof(buf2
), &sa
->type
, NULL
);
3275 pstrcat(buf1
, sizeof(buf1
), buf2
);
3278 pstrcat(buf1
, sizeof(buf1
), ", ");
3280 if (s
->f
.func_type
== FUNC_ELLIPSIS
)
3281 pstrcat(buf1
, sizeof(buf1
), ", ...");
3282 pstrcat(buf1
, sizeof(buf1
), ")");
3283 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3288 if (varstr
&& '*' == *varstr
)
3289 snprintf(buf1
, sizeof(buf1
), "(%s)[%d]", varstr
, s
->c
);
3291 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
3292 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3295 pstrcpy(buf1
, sizeof(buf1
), "*");
3296 if (t
& VT_CONSTANT
)
3297 pstrcat(buf1
, buf_size
, "const ");
3298 if (t
& VT_VOLATILE
)
3299 pstrcat(buf1
, buf_size
, "volatile ");
3301 pstrcat(buf1
, sizeof(buf1
), varstr
);
3302 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3306 pstrcat(buf
, buf_size
, " ");
3307 pstrcat(buf
, buf_size
, varstr
);
3312 static void cast_error(CType
*st
, CType
*dt
)
3314 char buf1
[256], buf2
[256];
3315 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
3316 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
3317 tcc_error("cannot convert '%s' to '%s'", buf1
, buf2
);
3320 /* verify type compatibility to store vtop in 'dt' type */
3321 static void verify_assign_cast(CType
*dt
)
3323 CType
*st
, *type1
, *type2
;
3324 int dbt
, sbt
, qualwarn
, lvl
;
3326 st
= &vtop
->type
; /* source type */
3327 dbt
= dt
->t
& VT_BTYPE
;
3328 sbt
= st
->t
& VT_BTYPE
;
3329 if (dt
->t
& VT_CONSTANT
)
3330 tcc_warning("assignment of read-only location");
3334 tcc_error("assignment to void expression");
3337 /* special cases for pointers */
3338 /* '0' can also be a pointer */
3339 if (is_null_pointer(vtop
))
3341 /* accept implicit pointer to integer cast with warning */
3342 if (is_integer_btype(sbt
)) {
3343 tcc_warning("assignment makes pointer from integer without a cast");
3346 type1
= pointed_type(dt
);
3348 type2
= pointed_type(st
);
3349 else if (sbt
== VT_FUNC
)
3350 type2
= st
; /* a function is implicitly a function pointer */
3353 if (is_compatible_types(type1
, type2
))
3355 for (qualwarn
= lvl
= 0;; ++lvl
) {
3356 if (((type2
->t
& VT_CONSTANT
) && !(type1
->t
& VT_CONSTANT
)) ||
3357 ((type2
->t
& VT_VOLATILE
) && !(type1
->t
& VT_VOLATILE
)))
3359 dbt
= type1
->t
& (VT_BTYPE
|VT_LONG
);
3360 sbt
= type2
->t
& (VT_BTYPE
|VT_LONG
);
3361 if (dbt
!= VT_PTR
|| sbt
!= VT_PTR
)
3363 type1
= pointed_type(type1
);
3364 type2
= pointed_type(type2
);
3366 if (!is_compatible_unqualified_types(type1
, type2
)) {
3367 if ((dbt
== VT_VOID
|| sbt
== VT_VOID
) && lvl
== 0) {
3368 /* void * can match anything */
3369 } else if (dbt
== sbt
3370 && is_integer_btype(sbt
& VT_BTYPE
)
3371 && IS_ENUM(type1
->t
) + IS_ENUM(type2
->t
)
3372 + !!((type1
->t
^ type2
->t
) & VT_UNSIGNED
) < 2) {
3373 /* Like GCC don't warn by default for merely changes
3374 in pointer target signedness. Do warn for different
3375 base types, though, in particular for unsigned enums
3376 and signed int targets. */
3378 tcc_warning("assignment from incompatible pointer type");
3383 tcc_warning("assignment discards qualifiers from pointer target type");
3389 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
3390 tcc_warning("assignment makes integer from pointer without a cast");
3391 } else if (sbt
== VT_STRUCT
) {
3392 goto case_VT_STRUCT
;
3394 /* XXX: more tests */
3398 if (!is_compatible_unqualified_types(dt
, st
)) {
3406 static void gen_assign_cast(CType
*dt
)
3408 verify_assign_cast(dt
);
3412 /* store vtop in lvalue pushed on stack */
3413 ST_FUNC
void vstore(void)
3415 int sbt
, dbt
, ft
, r
, size
, align
, bit_size
, bit_pos
, delayed_cast
;
3417 ft
= vtop
[-1].type
.t
;
3418 sbt
= vtop
->type
.t
& VT_BTYPE
;
3419 dbt
= ft
& VT_BTYPE
;
3421 verify_assign_cast(&vtop
[-1].type
);
3423 if (sbt
== VT_STRUCT
) {
3424 /* if structure, only generate pointer */
3425 /* structure assignment : generate memcpy */
3426 /* XXX: optimize if small size */
3427 size
= type_size(&vtop
->type
, &align
);
3431 vtop
->type
.t
= VT_PTR
;
3434 /* address of memcpy() */
3437 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3438 else if(!(align
& 3))
3439 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3442 /* Use memmove, rather than memcpy, as dest and src may be same: */
3443 vpush_global_sym(&func_old_type
, TOK_memmove
);
3447 vtop
->r
&= ~VT_MUSTBOUND
;
3449 vtop
->type
.t
= VT_PTR
;
3454 /* leave source on stack */
3456 } else if (ft
& VT_BITFIELD
) {
3457 /* bitfield store handling */
3459 /* save lvalue as expression result (example: s.b = s.a = n;) */
3460 vdup(), vtop
[-1] = vtop
[-2];
3462 bit_pos
= BIT_POS(ft
);
3463 bit_size
= BIT_SIZE(ft
);
3464 /* remove bit field info to avoid loops */
3465 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3467 if (dbt
== VT_BOOL
) {
3468 gen_cast(&vtop
[-1].type
);
3469 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3471 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3472 if (dbt
!= VT_BOOL
) {
3473 gen_cast(&vtop
[-1].type
);
3474 dbt
= vtop
[-1].type
.t
& VT_BTYPE
;
3476 if (r
== VT_STRUCT
) {
3477 store_packed_bf(bit_pos
, bit_size
);
3479 unsigned long long mask
= (1ULL << bit_size
) - 1;
3480 if (dbt
!= VT_BOOL
) {
3482 if (dbt
== VT_LLONG
)
3485 vpushi((unsigned)mask
);
3492 /* duplicate destination */
3495 /* load destination, mask and or with source */
3496 if (dbt
== VT_LLONG
)
3497 vpushll(~(mask
<< bit_pos
));
3499 vpushi(~((unsigned)mask
<< bit_pos
));
3504 /* ... and discard */
3507 } else if (dbt
== VT_VOID
) {
3510 /* optimize char/short casts */
3512 if ((dbt
== VT_BYTE
|| dbt
== VT_SHORT
)
3513 && is_integer_btype(sbt
)
3515 if ((vtop
->r
& VT_MUSTCAST
)
3516 && btype_size(dbt
) > btype_size(sbt
)
3518 force_charshort_cast();
3521 gen_cast(&vtop
[-1].type
);
3524 #ifdef CONFIG_TCC_BCHECK
3525 /* bound check case */
3526 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3532 gv(RC_TYPE(dbt
)); /* generate value */
3535 vtop
->r
|= BFVAL(VT_MUSTCAST
, (sbt
== VT_LLONG
) + 1);
3536 //tcc_warning("deley cast %x -> %x", sbt, dbt);
3537 vtop
->type
.t
= ft
& VT_TYPE
;
3540 /* if lvalue was saved on stack, must read it */
3541 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3543 r
= get_reg(RC_INT
);
3544 sv
.type
.t
= VT_PTRDIFF_T
;
3545 sv
.r
= VT_LOCAL
| VT_LVAL
;
3546 sv
.c
.i
= vtop
[-1].c
.i
;
3548 vtop
[-1].r
= r
| VT_LVAL
;
3551 r
= vtop
->r
& VT_VALMASK
;
3552 /* two word case handling :
3553 store second register at word + 4 (or +8 for x86-64) */
3554 if (USING_TWO_WORDS(dbt
)) {
3555 int load_type
= (dbt
== VT_QFLOAT
) ? VT_DOUBLE
: VT_PTRDIFF_T
;
3556 vtop
[-1].type
.t
= load_type
;
3559 /* convert to int to increment easily */
3560 vtop
->type
.t
= VT_PTRDIFF_T
;
3566 vtop
[-1].type
.t
= load_type
;
3567 /* XXX: it works because r2 is spilled last ! */
3568 store(vtop
->r2
, vtop
- 1);
3574 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3578 /* post defines POST/PRE add. c is the token ++ or -- */
3579 ST_FUNC
void inc(int post
, int c
)
3582 vdup(); /* save lvalue */
3584 gv_dup(); /* duplicate value */
3589 vpushi(c
- TOK_MID
);
3591 vstore(); /* store value */
3593 vpop(); /* if post op, return saved value */
3596 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3598 /* read the string */
3602 while (tok
== TOK_STR
) {
3603 /* XXX: add \0 handling too ? */
3604 cstr_cat(astr
, tokc
.str
.data
, -1);
3607 cstr_ccat(astr
, '\0');
3610 /* If I is >= 1 and a power of two, returns log2(i)+1.
3611 If I is 0 returns 0. */
3612 static int exact_log2p1(int i
)
3617 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3628 /* Parse __attribute__((...)) GNUC extension. */
3629 static void parse_attribute(AttributeDef
*ad
)
3635 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3640 while (tok
!= ')') {
3641 if (tok
< TOK_IDENT
)
3642 expect("attribute name");
3654 tcc_warning("implicit declaration of function '%s'",
3655 get_tok_str(tok
, &tokc
));
3656 s
= external_global_sym(tok
, &func_old_type
);
3658 ad
->cleanup_func
= s
;
3663 case TOK_CONSTRUCTOR1
:
3664 case TOK_CONSTRUCTOR2
:
3665 ad
->constructor
= 1;
3667 case TOK_DESTRUCTOR1
:
3668 case TOK_DESTRUCTOR2
:
3674 parse_mult_str(&astr
, "section name");
3675 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3682 parse_mult_str(&astr
, "alias(\"target\")");
3683 ad
->alias_target
= /* save string as token, for later */
3684 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3688 case TOK_VISIBILITY1
:
3689 case TOK_VISIBILITY2
:
3691 parse_mult_str(&astr
,
3692 "visibility(\"default|hidden|internal|protected\")");
3693 if (!strcmp (astr
.data
, "default"))
3694 ad
->a
.visibility
= STV_DEFAULT
;
3695 else if (!strcmp (astr
.data
, "hidden"))
3696 ad
->a
.visibility
= STV_HIDDEN
;
3697 else if (!strcmp (astr
.data
, "internal"))
3698 ad
->a
.visibility
= STV_INTERNAL
;
3699 else if (!strcmp (astr
.data
, "protected"))
3700 ad
->a
.visibility
= STV_PROTECTED
;
3702 expect("visibility(\"default|hidden|internal|protected\")");
3711 if (n
<= 0 || (n
& (n
- 1)) != 0)
3712 tcc_error("alignment must be a positive power of two");
3717 ad
->a
.aligned
= exact_log2p1(n
);
3718 if (n
!= 1 << (ad
->a
.aligned
- 1))
3719 tcc_error("alignment of %d is larger than implemented", n
);
3731 /* currently, no need to handle it because tcc does not
3732 track unused objects */
3736 ad
->f
.func_noreturn
= 1;
3741 ad
->f
.func_call
= FUNC_CDECL
;
3746 ad
->f
.func_call
= FUNC_STDCALL
;
3748 #ifdef TCC_TARGET_I386
3758 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3764 ad
->f
.func_call
= FUNC_FASTCALLW
;
3771 ad
->attr_mode
= VT_LLONG
+ 1;
3774 ad
->attr_mode
= VT_BYTE
+ 1;
3777 ad
->attr_mode
= VT_SHORT
+ 1;
3781 ad
->attr_mode
= VT_INT
+ 1;
3784 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3791 ad
->a
.dllexport
= 1;
3793 case TOK_NODECORATE
:
3794 ad
->a
.nodecorate
= 1;
3797 ad
->a
.dllimport
= 1;
3800 if (tcc_state
->warn_unsupported
)
3801 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3802 /* skip parameters */
3804 int parenthesis
= 0;
3808 else if (tok
== ')')
3811 } while (parenthesis
&& tok
!= -1);
3824 static Sym
* find_field (CType
*type
, int v
, int *cumofs
)
3828 while ((s
= s
->next
) != NULL
) {
3829 if ((s
->v
& SYM_FIELD
) &&
3830 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3831 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3832 Sym
*ret
= find_field (&s
->type
, v
, cumofs
);
3844 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3846 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3847 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3848 int pcc
= !tcc_state
->ms_bitfields
;
3849 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3856 prevbt
= VT_STRUCT
; /* make it never match */
3861 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3862 if (f
->type
.t
& VT_BITFIELD
)
3863 bit_size
= BIT_SIZE(f
->type
.t
);
3866 size
= type_size(&f
->type
, &align
);
3867 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3870 if (pcc
&& bit_size
== 0) {
3871 /* in pcc mode, packing does not affect zero-width bitfields */
3874 /* in pcc mode, attribute packed overrides if set. */
3875 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3878 /* pragma pack overrides align if lesser and packs bitfields always */
3881 if (pragma_pack
< align
)
3882 align
= pragma_pack
;
3883 /* in pcc mode pragma pack also overrides individual align */
3884 if (pcc
&& pragma_pack
< a
)
3888 /* some individual align was specified */
3892 if (type
->ref
->type
.t
== VT_UNION
) {
3893 if (pcc
&& bit_size
>= 0)
3894 size
= (bit_size
+ 7) >> 3;
3899 } else if (bit_size
< 0) {
3901 c
+= (bit_pos
+ 7) >> 3;
3902 c
= (c
+ align
- 1) & -align
;
3911 /* A bit-field. Layout is more complicated. There are two
3912 options: PCC (GCC) compatible and MS compatible */
3914 /* In PCC layout a bit-field is placed adjacent to the
3915 preceding bit-fields, except if:
3917 - an individual alignment was given
3918 - it would overflow its base type container and
3919 there is no packing */
3920 if (bit_size
== 0) {
3922 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3924 } else if (f
->a
.aligned
) {
3926 } else if (!packed
) {
3928 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3929 if (ofs
> size
/ align
)
3933 /* in pcc mode, long long bitfields have type int if they fit */
3934 if (size
== 8 && bit_size
<= 32)
3935 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3937 while (bit_pos
>= align
* 8)
3938 c
+= align
, bit_pos
-= align
* 8;
3941 /* In PCC layout named bit-fields influence the alignment
3942 of the containing struct using the base types alignment,
3943 except for packed fields (which here have correct align). */
3944 if (f
->v
& SYM_FIRST_ANOM
3945 // && bit_size // ??? gcc on ARM/rpi does that
3950 bt
= f
->type
.t
& VT_BTYPE
;
3951 if ((bit_pos
+ bit_size
> size
* 8)
3952 || (bit_size
> 0) == (bt
!= prevbt
)
3954 c
= (c
+ align
- 1) & -align
;
3957 /* In MS bitfield mode a bit-field run always uses
3958 at least as many bits as the underlying type.
3959 To start a new run it's also required that this
3960 or the last bit-field had non-zero width. */
3961 if (bit_size
|| prev_bit_size
)
3964 /* In MS layout the records alignment is normally
3965 influenced by the field, except for a zero-width
3966 field at the start of a run (but by further zero-width
3967 fields it is again). */
3968 if (bit_size
== 0 && prevbt
!= bt
)
3971 prev_bit_size
= bit_size
;
3974 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3975 | (bit_pos
<< VT_STRUCT_SHIFT
);
3976 bit_pos
+= bit_size
;
3978 if (align
> maxalign
)
3982 printf("set field %s offset %-2d size %-2d align %-2d",
3983 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3984 if (f
->type
.t
& VT_BITFIELD
) {
3985 printf(" pos %-2d bits %-2d",
3998 c
+= (bit_pos
+ 7) >> 3;
4000 /* store size and alignment */
4001 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
4005 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
4006 /* can happen if individual align for some member was given. In
4007 this case MSVC ignores maxalign when aligning the size */
4012 c
= (c
+ a
- 1) & -a
;
4016 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
4019 /* check whether we can access bitfields by their type */
4020 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
4024 if (0 == (f
->type
.t
& VT_BITFIELD
))
4028 bit_size
= BIT_SIZE(f
->type
.t
);
4031 bit_pos
= BIT_POS(f
->type
.t
);
4032 size
= type_size(&f
->type
, &align
);
4033 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
4036 /* try to access the field using a different type */
4037 c0
= -1, s
= align
= 1;
4039 px
= f
->c
* 8 + bit_pos
;
4040 cx
= (px
>> 3) & -align
;
4041 px
= px
- (cx
<< 3);
4044 s
= (px
+ bit_size
+ 7) >> 3;
4054 s
= type_size(&t
, &align
);
4058 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
4059 /* update offset and bit position */
4062 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
4063 | (bit_pos
<< VT_STRUCT_SHIFT
);
4067 printf("FIX field %s offset %-2d size %-2d align %-2d "
4068 "pos %-2d bits %-2d\n",
4069 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
4070 cx
, s
, align
, px
, bit_size
);
4073 /* fall back to load/store single-byte wise */
4074 f
->auxtype
= VT_STRUCT
;
4076 printf("FIX field %s : load byte-wise\n",
4077 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
4083 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
4084 static void struct_decl(CType
*type
, int u
)
4086 int v
, c
, size
, align
, flexible
;
4087 int bit_size
, bsize
, bt
;
4089 AttributeDef ad
, ad1
;
4092 memset(&ad
, 0, sizeof ad
);
4094 parse_attribute(&ad
);
4098 /* struct already defined ? return it */
4100 expect("struct/union/enum name");
4102 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
4105 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
4107 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
4112 /* Record the original enum/struct/union token. */
4113 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
4115 /* we put an undefined size for struct/union */
4116 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
4117 s
->r
= 0; /* default alignment is zero as gcc */
4119 type
->t
= s
->type
.t
;
4125 tcc_error("struct/union/enum already defined");
4127 /* cannot be empty */
4128 /* non empty enums are not allowed */
4131 long long ll
= 0, pl
= 0, nl
= 0;
4134 /* enum symbols have static storage */
4135 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
4139 expect("identifier");
4141 if (ss
&& !local_stack
)
4142 tcc_error("redefinition of enumerator '%s'",
4143 get_tok_str(v
, NULL
));
4147 ll
= expr_const64();
4149 ss
= sym_push(v
, &t
, VT_CONST
, 0);
4151 *ps
= ss
, ps
= &ss
->next
;
4160 /* NOTE: we accept a trailing comma */
4165 /* set integral type of the enum */
4168 if (pl
!= (unsigned)pl
)
4169 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4171 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
4172 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4173 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
4175 /* set type for enum members */
4176 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
4178 if (ll
== (int)ll
) /* default is int if it fits */
4180 if (t
.t
& VT_UNSIGNED
) {
4181 ss
->type
.t
|= VT_UNSIGNED
;
4182 if (ll
== (unsigned)ll
)
4185 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
)
4186 | (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
4191 while (tok
!= '}') {
4192 if (!parse_btype(&btype
, &ad1
)) {
4198 tcc_error("flexible array member '%s' not at the end of struct",
4199 get_tok_str(v
, NULL
));
4205 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
4207 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
4208 expect("identifier");
4210 int v
= btype
.ref
->v
;
4211 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
4212 if (tcc_state
->ms_extensions
== 0)
4213 expect("identifier");
4217 if (type_size(&type1
, &align
) < 0) {
4218 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
4221 tcc_error("field '%s' has incomplete type",
4222 get_tok_str(v
, NULL
));
4224 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
4225 (type1
.t
& VT_BTYPE
) == VT_VOID
||
4226 (type1
.t
& VT_STORAGE
))
4227 tcc_error("invalid type for '%s'",
4228 get_tok_str(v
, NULL
));
4232 bit_size
= expr_const();
4233 /* XXX: handle v = 0 case for messages */
4235 tcc_error("negative width in bit-field '%s'",
4236 get_tok_str(v
, NULL
));
4237 if (v
&& bit_size
== 0)
4238 tcc_error("zero width for bit-field '%s'",
4239 get_tok_str(v
, NULL
));
4240 parse_attribute(&ad1
);
4242 size
= type_size(&type1
, &align
);
4243 if (bit_size
>= 0) {
4244 bt
= type1
.t
& VT_BTYPE
;
4250 tcc_error("bitfields must have scalar type");
4252 if (bit_size
> bsize
) {
4253 tcc_error("width of '%s' exceeds its type",
4254 get_tok_str(v
, NULL
));
4255 } else if (bit_size
== bsize
4256 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
4257 /* no need for bit fields */
4259 } else if (bit_size
== 64) {
4260 tcc_error("field width 64 not implemented");
4262 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
4264 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4267 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
4268 /* Remember we've seen a real field to check
4269 for placement of flexible array member. */
4272 /* If member is a struct or bit-field, enforce
4273 placing into the struct (as anonymous). */
4275 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
4280 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
4285 if (tok
== ';' || tok
== TOK_EOF
)
4292 parse_attribute(&ad
);
4293 struct_layout(type
, &ad
);
4298 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
4300 merge_symattr(&ad
->a
, &s
->a
);
4301 merge_funcattr(&ad
->f
, &s
->f
);
4304 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4305 are added to the element type, copied because it could be a typedef. */
4306 static void parse_btype_qualify(CType
*type
, int qualifiers
)
4308 while (type
->t
& VT_ARRAY
) {
4309 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
4310 type
= &type
->ref
->type
;
4312 type
->t
|= qualifiers
;
4315 /* return 0 if no type declaration. otherwise, return the basic type
4318 static int parse_btype(CType
*type
, AttributeDef
*ad
)
4320 int t
, u
, bt
, st
, type_found
, typespec_found
, g
, n
;
4324 memset(ad
, 0, sizeof(AttributeDef
));
4334 /* currently, we really ignore extension */
4344 if (u
== VT_SHORT
|| u
== VT_LONG
) {
4345 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
4346 tmbt
: tcc_error("too many basic types");
4349 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
4354 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4371 memset(&ad1
, 0, sizeof(AttributeDef
));
4372 if (parse_btype(&type1
, &ad1
)) {
4373 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4375 n
= 1 << (ad1
.a
.aligned
- 1);
4377 type_size(&type1
, &n
);
4380 if (n
<= 0 || (n
& (n
- 1)) != 0)
4381 tcc_error("alignment must be a positive power of two");
4384 ad
->a
.aligned
= exact_log2p1(n
);
4388 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4389 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4390 } else if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4391 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LLONG
;
4398 #ifdef TCC_TARGET_ARM64
4400 /* GCC's __uint128_t appears in some Linux header files. Make it a
4401 synonym for long double to get the size and alignment right. */
4412 if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4413 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4421 struct_decl(&type1
, VT_ENUM
);
4424 type
->ref
= type1
.ref
;
4427 struct_decl(&type1
, VT_STRUCT
);
4430 struct_decl(&type1
, VT_UNION
);
4433 /* type modifiers */
4438 parse_btype_qualify(type
, VT_CONSTANT
);
4446 parse_btype_qualify(type
, VT_VOLATILE
);
4453 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4454 tcc_error("signed and unsigned modifier");
4467 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4468 tcc_error("signed and unsigned modifier");
4469 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4485 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4486 tcc_error("multiple storage classes");
4498 ad
->f
.func_noreturn
= 1;
4500 /* GNUC attribute */
4501 case TOK_ATTRIBUTE1
:
4502 case TOK_ATTRIBUTE2
:
4503 parse_attribute(ad
);
4504 if (ad
->attr_mode
) {
4505 u
= ad
->attr_mode
-1;
4506 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4514 parse_expr_type(&type1
);
4515 /* remove all storage modifiers except typedef */
4516 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4518 sym_to_attr(ad
, type1
.ref
);
4524 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4528 if (tok
== ':' && !in_generic
) {
4529 /* ignore if it's a label */
4534 t
&= ~(VT_BTYPE
|VT_LONG
);
4535 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4536 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4537 type
->ref
= s
->type
.ref
;
4539 parse_btype_qualify(type
, t
);
4541 /* get attributes from typedef */
4550 if (tcc_state
->char_is_unsigned
) {
4551 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4554 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4555 bt
= t
& (VT_BTYPE
|VT_LONG
);
4557 t
|= LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
;
4558 #ifdef TCC_TARGET_PE
4559 if (bt
== VT_LDOUBLE
)
4560 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_DOUBLE
;
4566 /* convert a function parameter type (array to pointer and function to
4567 function pointer) */
4568 static inline void convert_parameter_type(CType
*pt
)
4570 /* remove const and volatile qualifiers (XXX: const could be used
4571 to indicate a const function parameter */
4572 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4573 /* array must be transformed to pointer according to ANSI C */
4575 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4580 ST_FUNC
void parse_asm_str(CString
*astr
)
4583 parse_mult_str(astr
, "string constant");
4586 /* Parse an asm label and return the token */
4587 static int asm_label_instr(void)
4593 parse_asm_str(&astr
);
4596 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4598 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4603 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4605 int n
, l
, t1
, arg_size
, align
, unused_align
;
4606 Sym
**plast
, *s
, *first
;
4611 /* function type, or recursive declarator (return if so) */
4613 if (td
&& !(td
& TYPE_ABSTRACT
))
4617 else if (parse_btype(&pt
, &ad1
))
4620 merge_attr (ad
, &ad1
);
4629 /* read param name and compute offset */
4630 if (l
!= FUNC_OLD
) {
4631 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4633 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4634 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4635 tcc_error("parameter declared as void");
4639 expect("identifier");
4640 pt
.t
= VT_VOID
; /* invalid type */
4644 convert_parameter_type(&pt
);
4645 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4646 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4652 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4657 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4658 tcc_error("invalid type");
4661 /* if no parameters, then old type prototype */
4664 /* NOTE: const is ignored in returned type as it has a special
4665 meaning in gcc / C++ */
4666 type
->t
&= ~VT_CONSTANT
;
4667 /* some ancient pre-K&R C allows a function to return an array
4668 and the array brackets to be put after the arguments, such
4669 that "int c()[]" means something like "int[] c()" */
4672 skip(']'); /* only handle simple "[]" */
4675 /* we push a anonymous symbol which will contain the function prototype */
4676 ad
->f
.func_args
= arg_size
;
4677 ad
->f
.func_type
= l
;
4678 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4684 } else if (tok
== '[') {
4685 int saved_nocode_wanted
= nocode_wanted
;
4686 /* array definition */
4689 /* XXX The optional type-quals and static should only be accepted
4690 in parameter decls. The '*' as well, and then even only
4691 in prototypes (not function defs). */
4693 case TOK_RESTRICT1
: case TOK_RESTRICT2
: case TOK_RESTRICT3
:
4708 if (!local_stack
|| (storage
& VT_STATIC
))
4709 vpushi(expr_const());
4711 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4712 length must always be evaluated, even under nocode_wanted,
4713 so that its size slot is initialized (e.g. under sizeof
4718 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4721 tcc_error("invalid array size");
4723 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4724 tcc_error("size of variable length array should be an integer");
4730 /* parse next post type */
4731 post_type(type
, ad
, storage
, 0);
4733 if ((type
->t
& VT_BTYPE
) == VT_FUNC
)
4734 tcc_error("declaration of an array of functions");
4735 if ((type
->t
& VT_BTYPE
) == VT_VOID
4736 || type_size(type
, &unused_align
) < 0)
4737 tcc_error("declaration of an array of incomplete type elements");
4739 t1
|= type
->t
& VT_VLA
;
4743 tcc_error("need explicit inner array size in VLAs");
4744 loc
-= type_size(&int_type
, &align
);
4748 vla_runtime_type_size(type
, &align
);
4750 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4756 nocode_wanted
= saved_nocode_wanted
;
4758 /* we push an anonymous symbol which will contain the array
4760 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4761 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4767 /* Parse a type declarator (except basic type), and return the type
4768 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4769 expected. 'type' should contain the basic type. 'ad' is the
4770 attribute definition of the basic type. It can be modified by
4771 type_decl(). If this (possibly abstract) declarator is a pointer chain
4772 it returns the innermost pointed to type (equals *type, but is a different
4773 pointer), otherwise returns type itself, that's used for recursive calls. */
4774 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4777 int qualifiers
, storage
;
4779 /* recursive type, remove storage bits first, apply them later again */
4780 storage
= type
->t
& VT_STORAGE
;
4781 type
->t
&= ~VT_STORAGE
;
4784 while (tok
== '*') {
4792 qualifiers
|= VT_CONSTANT
;
4797 qualifiers
|= VT_VOLATILE
;
4803 /* XXX: clarify attribute handling */
4804 case TOK_ATTRIBUTE1
:
4805 case TOK_ATTRIBUTE2
:
4806 parse_attribute(ad
);
4810 type
->t
|= qualifiers
;
4812 /* innermost pointed to type is the one for the first derivation */
4813 ret
= pointed_type(type
);
4817 /* This is possibly a parameter type list for abstract declarators
4818 ('int ()'), use post_type for testing this. */
4819 if (!post_type(type
, ad
, 0, td
)) {
4820 /* It's not, so it's a nested declarator, and the post operations
4821 apply to the innermost pointed to type (if any). */
4822 /* XXX: this is not correct to modify 'ad' at this point, but
4823 the syntax is not clear */
4824 parse_attribute(ad
);
4825 post
= type_decl(type
, ad
, v
, td
);
4829 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4830 /* type identifier */
4835 if (!(td
& TYPE_ABSTRACT
))
4836 expect("identifier");
4839 post_type(post
, ad
, storage
, 0);
4840 parse_attribute(ad
);
4845 /* indirection with full error checking and bound check */
4846 ST_FUNC
void indir(void)
4848 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4849 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4853 if (vtop
->r
& VT_LVAL
)
4855 vtop
->type
= *pointed_type(&vtop
->type
);
4856 /* Arrays and functions are never lvalues */
4857 if (!(vtop
->type
.t
& (VT_ARRAY
| VT_VLA
))
4858 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4860 /* if bound checking, the referenced pointer must be checked */
4861 #ifdef CONFIG_TCC_BCHECK
4862 if (tcc_state
->do_bounds_check
)
4863 vtop
->r
|= VT_MUSTBOUND
;
4868 /* pass a parameter to a function and do type checking and casting */
4869 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4874 func_type
= func
->f
.func_type
;
4875 if (func_type
== FUNC_OLD
||
4876 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4877 /* default casting : only need to convert float to double */
4878 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4879 gen_cast_s(VT_DOUBLE
);
4880 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4881 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4882 type
.ref
= vtop
->type
.ref
;
4884 } else if (vtop
->r
& VT_MUSTCAST
) {
4885 force_charshort_cast();
4887 } else if (arg
== NULL
) {
4888 tcc_error("too many arguments to function");
4891 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4892 gen_assign_cast(&type
);
4896 /* parse an expression and return its type without any side effect. */
4897 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4906 /* parse an expression of the form '(type)' or '(expr)' and return its
4908 static void parse_expr_type(CType
*type
)
4914 if (parse_btype(type
, &ad
)) {
4915 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4917 expr_type(type
, gexpr
);
4922 static void parse_type(CType
*type
)
4927 if (!parse_btype(type
, &ad
)) {
4930 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4933 static void parse_builtin_params(int nc
, const char *args
)
4940 while ((c
= *args
++)) {
4944 case 'e': expr_eq(); continue;
4945 case 't': parse_type(&t
); vpush(&t
); continue;
4946 default: tcc_error("internal error"); break;
4954 ST_FUNC
void unary(void)
4956 int n
, t
, align
, size
, r
, sizeof_caller
;
4961 sizeof_caller
= in_sizeof
;
4964 /* XXX: GCC 2.95.3 does not generate a table although it should be
4972 #ifdef TCC_TARGET_PE
4973 t
= VT_SHORT
|VT_UNSIGNED
;
4981 vsetc(&type
, VT_CONST
, &tokc
);
4985 t
= VT_INT
| VT_UNSIGNED
;
4991 t
= VT_LLONG
| VT_UNSIGNED
;
5003 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
;
5006 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
| VT_UNSIGNED
;
5008 case TOK___FUNCTION__
:
5010 goto tok_identifier
;
5016 /* special function name identifier */
5017 len
= strlen(funcname
) + 1;
5018 /* generate char[len] type */
5023 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
5024 if (!NODATA_WANTED
) {
5025 ptr
= section_ptr_add(data_section
, len
);
5026 memcpy(ptr
, funcname
, len
);
5032 #ifdef TCC_TARGET_PE
5033 t
= VT_SHORT
| VT_UNSIGNED
;
5039 /* string parsing */
5041 if (tcc_state
->char_is_unsigned
)
5042 t
= VT_BYTE
| VT_UNSIGNED
;
5044 if (tcc_state
->warn_write_strings
)
5049 memset(&ad
, 0, sizeof(AttributeDef
));
5050 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
5055 if (parse_btype(&type
, &ad
)) {
5056 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
5058 /* check ISOC99 compound literal */
5060 /* data is allocated locally by default */
5065 /* all except arrays are lvalues */
5066 if (!(type
.t
& VT_ARRAY
))
5068 memset(&ad
, 0, sizeof(AttributeDef
));
5069 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
5071 if (sizeof_caller
) {
5078 } else if (tok
== '{') {
5079 int saved_nocode_wanted
= nocode_wanted
;
5080 if (const_wanted
&& !(nocode_wanted
& unevalmask
))
5081 tcc_error("expected constant");
5082 /* save all registers */
5084 /* statement expression : we do not accept break/continue
5085 inside as GCC does. We do retain the nocode_wanted state,
5086 as statement expressions can't ever be entered from the
5087 outside, so any reactivation of code emission (from labels
5088 or loop heads) can be disabled again after the end of it. */
5090 nocode_wanted
= saved_nocode_wanted
;
5105 /* functions names must be treated as function pointers,
5106 except for unary '&' and sizeof. Since we consider that
5107 functions are not lvalues, we only have to handle it
5108 there and in function calls. */
5109 /* arrays can also be used although they are not lvalues */
5110 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
5111 !(vtop
->type
.t
& VT_ARRAY
))
5114 vtop
->sym
->a
.addrtaken
= 1;
5115 mk_pointer(&vtop
->type
);
5121 gen_test_zero(TOK_EQ
);
5132 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
5133 tcc_error("pointer not accepted for unary plus");
5134 /* In order to force cast, we add zero, except for floating point
5135 where we really need an noop (otherwise -0.0 will be transformed
5137 if (!is_float(vtop
->type
.t
)) {
5149 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
5151 if (vtop
[1].r
& VT_SYM
)
5152 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
5153 size
= type_size(&type
, &align
);
5154 if (s
&& s
->a
.aligned
)
5155 align
= 1 << (s
->a
.aligned
- 1);
5156 if (t
== TOK_SIZEOF
) {
5157 if (!(type
.t
& VT_VLA
)) {
5159 tcc_error("sizeof applied to an incomplete type");
5162 vla_runtime_type_size(&type
, &align
);
5167 vtop
->type
.t
|= VT_UNSIGNED
;
5170 case TOK_builtin_expect
:
5171 /* __builtin_expect is a no-op for now */
5172 parse_builtin_params(0, "ee");
5175 case TOK_builtin_types_compatible_p
:
5176 parse_builtin_params(0, "tt");
5177 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5178 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5179 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
5183 case TOK_builtin_choose_expr
:
5210 case TOK_builtin_constant_p
:
5211 parse_builtin_params(1, "e");
5212 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5216 case TOK_builtin_frame_address
:
5217 case TOK_builtin_return_address
:
5223 if (tok
!= TOK_CINT
) {
5224 tcc_error("%s only takes positive integers",
5225 tok1
== TOK_builtin_return_address
?
5226 "__builtin_return_address" :
5227 "__builtin_frame_address");
5229 level
= (uint32_t)tokc
.i
;
5234 vset(&type
, VT_LOCAL
, 0); /* local frame */
5236 mk_pointer(&vtop
->type
);
5237 indir(); /* -> parent frame */
5239 if (tok1
== TOK_builtin_return_address
) {
5240 // assume return address is just above frame pointer on stack
5243 mk_pointer(&vtop
->type
);
5248 #ifdef TCC_TARGET_RISCV64
5249 case TOK_builtin_va_start
:
5250 parse_builtin_params(0, "ee");
5251 r
= vtop
->r
& VT_VALMASK
;
5255 tcc_error("__builtin_va_start expects a local variable");
5260 #ifdef TCC_TARGET_X86_64
5261 #ifdef TCC_TARGET_PE
5262 case TOK_builtin_va_start
:
5263 parse_builtin_params(0, "ee");
5264 r
= vtop
->r
& VT_VALMASK
;
5268 tcc_error("__builtin_va_start expects a local variable");
5270 vtop
->type
= char_pointer_type
;
5275 case TOK_builtin_va_arg_types
:
5276 parse_builtin_params(0, "t");
5277 vpushi(classify_x86_64_va_arg(&vtop
->type
));
5284 #ifdef TCC_TARGET_ARM64
5285 case TOK___va_start
: {
5286 parse_builtin_params(0, "ee");
5290 vtop
->type
.t
= VT_VOID
;
5293 case TOK___va_arg
: {
5294 parse_builtin_params(0, "et");
5302 case TOK___arm64_clear_cache
: {
5303 parse_builtin_params(0, "ee");
5306 vtop
->type
.t
= VT_VOID
;
5310 /* pre operations */
5321 t
= vtop
->type
.t
& VT_BTYPE
;
5323 /* In IEEE negate(x) isn't subtract(0,x), but rather
5327 vtop
->c
.f
= -1.0 * 0.0;
5328 else if (t
== VT_DOUBLE
)
5329 vtop
->c
.d
= -1.0 * 0.0;
5331 vtop
->c
.ld
= -1.0 * 0.0;
5339 goto tok_identifier
;
5341 /* allow to take the address of a label */
5342 if (tok
< TOK_UIDENT
)
5343 expect("label identifier");
5344 s
= label_find(tok
);
5346 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5348 if (s
->r
== LABEL_DECLARED
)
5349 s
->r
= LABEL_FORWARD
;
5352 s
->type
.t
= VT_VOID
;
5353 mk_pointer(&s
->type
);
5354 s
->type
.t
|= VT_STATIC
;
5356 vpushsym(&s
->type
, s
);
5362 CType controlling_type
;
5363 int has_default
= 0;
5366 TokenString
*str
= NULL
;
5367 int saved_const_wanted
= const_wanted
;
5372 expr_type(&controlling_type
, expr_eq
);
5373 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
5374 if ((controlling_type
.t
& VT_BTYPE
) == VT_FUNC
)
5375 mk_pointer(&controlling_type
);
5376 const_wanted
= saved_const_wanted
;
5380 if (tok
== TOK_DEFAULT
) {
5382 tcc_error("too many 'default'");
5388 AttributeDef ad_tmp
;
5393 parse_btype(&cur_type
, &ad_tmp
);
5396 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
5397 if (compare_types(&controlling_type
, &cur_type
, 0)) {
5399 tcc_error("type match twice");
5409 skip_or_save_block(&str
);
5411 skip_or_save_block(NULL
);
5418 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
5419 tcc_error("type '%s' does not match any association", buf
);
5421 begin_macro(str
, 1);
5430 // special qnan , snan and infinity values
5435 vtop
->type
.t
= VT_FLOAT
;
5440 goto special_math_val
;
5443 goto special_math_val
;
5450 expect("identifier");
5452 if (!s
|| IS_ASM_SYM(s
)) {
5453 const char *name
= get_tok_str(t
, NULL
);
5455 tcc_error("'%s' undeclared", name
);
5456 /* for simple function calls, we tolerate undeclared
5457 external reference to int() function */
5458 if (tcc_state
->warn_implicit_function_declaration
5459 #ifdef TCC_TARGET_PE
5460 /* people must be warned about using undeclared WINAPI functions
5461 (which usually start with uppercase letter) */
5462 || (name
[0] >= 'A' && name
[0] <= 'Z')
5465 tcc_warning("implicit declaration of function '%s'", name
);
5466 s
= external_global_sym(t
, &func_old_type
);
5470 /* A symbol that has a register is a local register variable,
5471 which starts out as VT_LOCAL value. */
5472 if ((r
& VT_VALMASK
) < VT_CONST
)
5473 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5475 vset(&s
->type
, r
, s
->c
);
5476 /* Point to s as backpointer (even without r&VT_SYM).
5477 Will be used by at least the x86 inline asm parser for
5483 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5484 vtop
->c
.i
= s
->enum_val
;
5489 /* post operations */
5491 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5494 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5495 int qualifiers
, cumofs
= 0;
5497 if (tok
== TOK_ARROW
)
5499 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5502 /* expect pointer on structure */
5503 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5504 expect("struct or union");
5505 if (tok
== TOK_CDOUBLE
)
5506 expect("field name");
5508 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5509 expect("field name");
5510 s
= find_field(&vtop
->type
, tok
, &cumofs
);
5512 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5513 /* add field offset to pointer */
5514 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5515 vpushi(cumofs
+ s
->c
);
5517 /* change type to field type, and set to lvalue */
5518 vtop
->type
= s
->type
;
5519 vtop
->type
.t
|= qualifiers
;
5520 /* an array is never an lvalue */
5521 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5523 #ifdef CONFIG_TCC_BCHECK
5524 /* if bound checking, the referenced pointer must be checked */
5525 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5526 vtop
->r
|= VT_MUSTBOUND
;
5530 } else if (tok
== '[') {
5536 } else if (tok
== '(') {
5539 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5541 #ifdef CONFIG_TCC_BCHECK
5542 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_SYM
) && vtop
->sym
->v
== TOK_alloca
) {
5545 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
5546 bounds_ptr
[0] = 1; /* marks alloca/vla used */
5551 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5552 /* pointer test (no array accepted) */
5553 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5554 vtop
->type
= *pointed_type(&vtop
->type
);
5555 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5559 expect("function pointer");
5562 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5564 /* get return type */
5567 sa
= s
->next
; /* first parameter */
5568 nb_args
= regsize
= 0;
5570 /* compute first implicit argument if a structure is returned */
5571 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5572 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5573 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5574 &ret_align
, ®size
);
5575 if (ret_nregs
<= 0) {
5576 /* get some space for the returned structure */
5577 size
= type_size(&s
->type
, &align
);
5578 #ifdef TCC_TARGET_ARM64
5579 /* On arm64, a small struct is return in registers.
5580 It is much easier to write it to memory if we know
5581 that we are allowed to write some extra bytes, so
5582 round the allocated space up to a power of 2: */
5584 while (size
& (size
- 1))
5585 size
= (size
| (size
- 1)) + 1;
5587 loc
= (loc
- size
) & -align
;
5589 ret
.r
= VT_LOCAL
| VT_LVAL
;
5590 /* pass it as 'int' to avoid structure arg passing
5592 vseti(VT_LOCAL
, loc
);
5604 if (ret_nregs
> 0) {
5605 /* return in register */
5607 PUT_R_RET(&ret
, ret
.type
.t
);
5612 gfunc_param_typed(s
, sa
);
5622 tcc_error("too few arguments to function");
5624 gfunc_call(nb_args
);
5626 if (ret_nregs
< 0) {
5627 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
5628 #ifdef TCC_TARGET_RISCV64
5629 arch_transfer_ret_regs(1);
5633 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5634 vsetc(&ret
.type
, r
, &ret
.c
);
5635 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5638 /* handle packed struct return */
5639 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5642 size
= type_size(&s
->type
, &align
);
5643 /* We're writing whole regs often, make sure there's enough
5644 space. Assume register size is power of 2. */
5645 if (regsize
> align
)
5647 loc
= (loc
- size
) & -align
;
5651 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5655 if (--ret_nregs
== 0)
5659 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5662 /* Promote char/short return values. This is matters only
5663 for calling function that were not compiled by TCC and
5664 only on some architectures. For those where it doesn't
5665 matter we expect things to be already promoted to int,
5667 t
= s
->type
.t
& VT_BTYPE
;
5668 if (t
== VT_BYTE
|| t
== VT_SHORT
|| t
== VT_BOOL
) {
5670 vtop
->r
|= BFVAL(VT_MUSTCAST
, 1);
5672 vtop
->type
.t
= VT_INT
;
5676 if (s
->f
.func_noreturn
)
5684 ST_FUNC
void expr_prod(void)
5689 while (tok
== '*' || tok
== '/' || tok
== '%') {
5697 ST_FUNC
void expr_sum(void)
5702 while (tok
== '+' || tok
== '-') {
5710 static void expr_shift(void)
5715 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5723 static void expr_cmp(void)
5728 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5729 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5737 static void expr_cmpeq(void)
5742 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5750 static void expr_and(void)
5753 while (tok
== '&') {
5760 static void expr_xor(void)
5763 while (tok
== '^') {
5770 static void expr_or(void)
5773 while (tok
== '|') {
5780 static int condition_3way(void);
5782 static void expr_landor(void(*e_fn
)(void), int e_op
, int i
)
5784 int t
= 0, cc
= 1, f
= 0, c
;
5786 c
= f
? i
: condition_3way();
5788 save_regs(1), cc
= 0;
5789 } else if (c
!= i
) {
5790 nocode_wanted
++, f
= 1;
5812 static void expr_land(void)
5815 if (tok
== TOK_LAND
)
5816 expr_landor(expr_or
, TOK_LAND
, 1);
5819 static void expr_lor(void)
5823 expr_landor(expr_land
, TOK_LOR
, 0);
5826 /* Assuming vtop is a value used in a conditional context
5827 (i.e. compared with zero) return 0 if it's false, 1 if
5828 true and -1 if it can't be statically determined. */
5829 static int condition_3way(void)
5832 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5833 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5835 gen_cast_s(VT_BOOL
);
5842 static int is_cond_bool(SValue
*sv
)
5844 if ((sv
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
5845 && (sv
->type
.t
& VT_BTYPE
) == VT_INT
)
5846 return (unsigned)sv
->c
.i
< 2;
5847 if (sv
->r
== VT_CMP
)
5852 static void expr_cond(void)
5854 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5856 CType type
, type1
, type2
;
5862 c
= condition_3way();
5863 g
= (tok
== ':' && gnu_ext
);
5873 /* needed to avoid having different registers saved in
5880 ncw_prev
= nocode_wanted
;
5887 if (c
< 0 && vtop
->r
== VT_CMP
) {
5893 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5894 mk_pointer(&vtop
->type
);
5896 sv
= *vtop
; /* save value to handle it later */
5897 vtop
--; /* no vpop so that FP stack is not flushed */
5907 nocode_wanted
= ncw_prev
;
5913 if (c
< 0 && is_cond_bool(vtop
) && is_cond_bool(&sv
)) {
5914 if (sv
.r
== VT_CMP
) {
5925 nocode_wanted
= ncw_prev
;
5926 // tcc_warning("two conditions expr_cond");
5930 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5931 mk_pointer(&vtop
->type
);
5934 bt1
= t1
& VT_BTYPE
;
5936 bt2
= t2
& VT_BTYPE
;
5939 /* cast operands to correct type according to ISOC rules */
5940 if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5941 type
.t
= VT_VOID
; /* NOTE: as an extension, we accept void on only one side */
5942 } else if (is_float(bt1
) || is_float(bt2
)) {
5943 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5944 type
.t
= VT_LDOUBLE
;
5946 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5951 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5952 /* cast to biggest op */
5953 type
.t
= VT_LLONG
| VT_LONG
;
5954 if (bt1
== VT_LLONG
)
5956 if (bt2
== VT_LLONG
)
5958 /* convert to unsigned if it does not fit in a long long */
5959 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5960 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5961 type
.t
|= VT_UNSIGNED
;
5962 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5963 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
5964 /* If one is a null ptr constant the result type
5966 if (is_null_pointer (vtop
)) type
= type1
;
5967 else if (is_null_pointer (&sv
)) type
= type2
;
5968 else if (bt1
!= bt2
)
5969 tcc_error("incompatible types in conditional expressions");
5971 CType
*pt1
= pointed_type(&type1
);
5972 CType
*pt2
= pointed_type(&type2
);
5973 int pbt1
= pt1
->t
& VT_BTYPE
;
5974 int pbt2
= pt2
->t
& VT_BTYPE
;
5975 int newquals
, copied
= 0;
5976 /* pointers to void get preferred, otherwise the
5977 pointed to types minus qualifs should be compatible */
5978 type
= (pbt1
== VT_VOID
) ? type1
: type2
;
5979 if (pbt1
!= VT_VOID
&& pbt2
!= VT_VOID
) {
5980 if(!compare_types(pt1
, pt2
, 1/*unqualif*/))
5981 tcc_warning("pointer type mismatch in conditional expression\n");
5983 /* combine qualifs */
5984 newquals
= ((pt1
->t
| pt2
->t
) & (VT_CONSTANT
| VT_VOLATILE
));
5985 if ((~pointed_type(&type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
5988 /* copy the pointer target symbol */
5989 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5992 pointed_type(&type
)->t
|= newquals
;
5994 /* pointers to incomplete arrays get converted to
5995 pointers to completed ones if possible */
5996 if (pt1
->t
& VT_ARRAY
5997 && pt2
->t
& VT_ARRAY
5998 && pointed_type(&type
)->ref
->c
< 0
5999 && (pt1
->ref
->c
> 0 || pt2
->ref
->c
> 0))
6002 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
6004 pointed_type(&type
)->ref
=
6005 sym_push(SYM_FIELD
, &pointed_type(&type
)->ref
->type
,
6006 0, pointed_type(&type
)->ref
->c
);
6007 pointed_type(&type
)->ref
->c
=
6008 0 < pt1
->ref
->c
? pt1
->ref
->c
: pt2
->ref
->c
;
6011 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
6012 /* XXX: test structure compatibility */
6013 type
= bt1
== VT_STRUCT
? type1
: type2
;
6015 /* integer operations */
6016 type
.t
= VT_INT
| (VT_LONG
& (t1
| t2
));
6017 /* convert to unsigned if it does not fit in an integer */
6018 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
6019 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
6020 type
.t
|= VT_UNSIGNED
;
6022 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
6023 that `(expr ? a : b).mem` does not error with "lvalue expected" */
6024 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
6026 /* now we convert second operand */
6030 mk_pointer(&vtop
->type
);
6032 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
6036 rc
= RC_TYPE(type
.t
);
6037 /* for long longs, we use fixed registers to avoid having
6038 to handle a complicated move */
6039 if (USING_TWO_WORDS(type
.t
))
6040 rc
= RC_RET(type
.t
);
6048 nocode_wanted
= ncw_prev
;
6050 /* this is horrible, but we must also convert first
6056 mk_pointer(&vtop
->type
);
6058 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
6064 move_reg(r2
, r1
, islv
? VT_PTR
: type
.t
);
6075 static void expr_eq(void)
6081 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
6082 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
6083 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
6098 ST_FUNC
void gexpr(void)
6109 /* parse a constant expression and return value in vtop. */
6110 static void expr_const1(void)
6113 nocode_wanted
+= unevalmask
+ 1;
6115 nocode_wanted
-= unevalmask
+ 1;
6119 /* parse an integer constant and return its value. */
6120 static inline int64_t expr_const64(void)
6124 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
6125 expect("constant expression");
6131 /* parse an integer constant and return its value.
6132 Complain if it doesn't fit 32bit (signed or unsigned). */
6133 ST_FUNC
int expr_const(void)
6136 int64_t wc
= expr_const64();
6138 if (c
!= wc
&& (unsigned)c
!= wc
)
6139 tcc_error("constant exceeds 32 bit");
6143 /* ------------------------------------------------------------------------- */
6144 /* return from function */
6146 #ifndef TCC_TARGET_ARM64
6147 static void gfunc_return(CType
*func_type
)
6149 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6150 CType type
, ret_type
;
6151 int ret_align
, ret_nregs
, regsize
;
6152 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
6153 &ret_align
, ®size
);
6154 if (ret_nregs
< 0) {
6155 #ifdef TCC_TARGET_RISCV64
6156 arch_transfer_ret_regs(0);
6158 } else if (0 == ret_nregs
) {
6159 /* if returning structure, must copy it to implicit
6160 first pointer arg location */
6163 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
6166 /* copy structure value to pointer */
6169 /* returning structure packed into registers */
6170 int size
, addr
, align
, rc
;
6171 size
= type_size(func_type
,&align
);
6172 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
6173 (vtop
->c
.i
& (ret_align
-1)))
6174 && (align
& (ret_align
-1))) {
6175 loc
= (loc
- size
) & -ret_align
;
6178 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
6182 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
6184 vtop
->type
= ret_type
;
6185 rc
= RC_RET(ret_type
.t
);
6193 if (--ret_nregs
== 0)
6195 /* We assume that when a structure is returned in multiple
6196 registers, their classes are consecutive values of the
6199 vtop
->c
.i
+= regsize
;
6204 gv(RC_RET(func_type
->t
));
6206 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6210 static void check_func_return(void)
6212 if ((func_vt
.t
& VT_BTYPE
) == VT_VOID
)
6214 if (!strcmp (funcname
, "main")
6215 && (func_vt
.t
& VT_BTYPE
) == VT_INT
) {
6216 /* main returns 0 by default */
6218 gen_assign_cast(&func_vt
);
6219 gfunc_return(&func_vt
);
6221 tcc_warning("function might return no value: '%s'", funcname
);
6225 /* ------------------------------------------------------------------------- */
6228 static int case_cmp(const void *pa
, const void *pb
)
6230 int64_t a
= (*(struct case_t
**) pa
)->v1
;
6231 int64_t b
= (*(struct case_t
**) pb
)->v1
;
6232 return a
< b
? -1 : a
> b
;
6235 static void gtst_addr(int t
, int a
)
6237 gsym_addr(gvtst(0, t
), a
);
6240 static void gcase(struct case_t
**base
, int len
, int *bsym
)
6244 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
6261 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
6263 gcase(base
, len
/2, bsym
);
6267 base
+= e
; len
-= e
;
6277 if (p
->v1
== p
->v2
) {
6279 gtst_addr(0, p
->sym
);
6289 gtst_addr(0, p
->sym
);
6293 *bsym
= gjmp(*bsym
);
6296 /* ------------------------------------------------------------------------- */
6297 /* __attribute__((cleanup(fn))) */
6299 static void try_call_scope_cleanup(Sym
*stop
)
6301 Sym
*cls
= cur_scope
->cl
.s
;
6303 for (; cls
!= stop
; cls
= cls
->ncl
) {
6304 Sym
*fs
= cls
->next
;
6305 Sym
*vs
= cls
->prev_tok
;
6307 vpushsym(&fs
->type
, fs
);
6308 vset(&vs
->type
, vs
->r
, vs
->c
);
6310 mk_pointer(&vtop
->type
);
6316 static void try_call_cleanup_goto(Sym
*cleanupstate
)
6321 if (!cur_scope
->cl
.s
)
6324 /* search NCA of both cleanup chains given parents and initial depth */
6325 ocd
= cleanupstate
? cleanupstate
->v
& ~SYM_FIELD
: 0;
6326 for (ccd
= cur_scope
->cl
.n
, oc
= cleanupstate
; ocd
> ccd
; --ocd
, oc
= oc
->ncl
)
6328 for (cc
= cur_scope
->cl
.s
; ccd
> ocd
; --ccd
, cc
= cc
->ncl
)
6330 for (; cc
!= oc
; cc
= cc
->ncl
, oc
= oc
->ncl
, --ccd
)
6333 try_call_scope_cleanup(cc
);
6336 /* call 'func' for each __attribute__((cleanup(func))) */
6337 static void block_cleanup(struct scope
*o
)
6341 for (pg
= &pending_gotos
; (g
= *pg
) && g
->c
> o
->cl
.n
;) {
6342 if (g
->prev_tok
->r
& LABEL_FORWARD
) {
6347 try_call_scope_cleanup(o
->cl
.s
);
6348 pcl
->jnext
= gjmp(0);
6350 goto remove_pending
;
6360 try_call_scope_cleanup(o
->cl
.s
);
6363 /* ------------------------------------------------------------------------- */
6366 static void vla_restore(int loc
)
6369 gen_vla_sp_restore(loc
);
6372 static void vla_leave(struct scope
*o
)
6374 if (o
->vla
.num
< cur_scope
->vla
.num
)
6375 vla_restore(o
->vla
.loc
);
6378 /* ------------------------------------------------------------------------- */
6381 void new_scope(struct scope
*o
)
6383 /* copy and link previous scope */
6385 o
->prev
= cur_scope
;
6388 /* record local declaration stack position */
6389 o
->lstk
= local_stack
;
6390 o
->llstk
= local_label_stack
;
6395 void prev_scope(struct scope
*o
, int is_expr
)
6399 if (o
->cl
.s
!= o
->prev
->cl
.s
)
6400 block_cleanup(o
->prev
);
6402 /* pop locally defined labels */
6403 label_pop(&local_label_stack
, o
->llstk
, is_expr
);
6405 /* In the is_expr case (a statement expression is finished here),
6406 vtop might refer to symbols on the local_stack. Either via the
6407 type or via vtop->sym. We can't pop those nor any that in turn
6408 might be referred to. To make it easier we don't roll back
6409 any symbols in that case; some upper level call to block() will
6410 do that. We do have to remove such symbols from the lookup
6411 tables, though. sym_pop will do that. */
6413 /* pop locally defined symbols */
6414 pop_local_syms(&local_stack
, o
->lstk
, is_expr
);
6416 cur_scope
= o
->prev
;
6420 /* leave a scope via break/continue(/goto) */
6421 void leave_scope(struct scope
*o
)
6425 try_call_scope_cleanup(o
->cl
.s
);
6429 /* ------------------------------------------------------------------------- */
6430 /* call block from 'for do while' loops */
6432 static void lblock(int *bsym
, int *csym
)
6434 struct scope
*lo
= loop_scope
, *co
= cur_scope
;
6435 int *b
= co
->bsym
, *c
= co
->csym
;
6449 static void block(int is_expr
)
6451 int a
, b
, c
, d
, e
, t
;
6455 /* default return value is (void) */
6457 vtop
->type
.t
= VT_VOID
;
6469 if (tok
== TOK_ELSE
) {
6474 gsym(d
); /* patch else jmp */
6479 } else if (t
== TOK_WHILE
) {
6491 } else if (t
== '{') {
6495 /* handle local labels declarations */
6496 while (tok
== TOK_LABEL
) {
6499 if (tok
< TOK_UIDENT
)
6500 expect("label identifier");
6501 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
6503 } while (tok
== ',');
6507 while (tok
!= '}') {
6516 prev_scope(&o
, is_expr
);
6518 if (0 == local_scope
&& !nocode_wanted
)
6519 check_func_return();
6522 } else if (t
== TOK_RETURN
) {
6523 b
= (func_vt
.t
& VT_BTYPE
) != VT_VOID
;
6527 gen_assign_cast(&func_vt
);
6529 if (vtop
->type
.t
!= VT_VOID
)
6530 tcc_warning("void function returns a value");
6534 tcc_warning("'return' with no value");
6537 leave_scope(root_scope
);
6539 gfunc_return(&func_vt
);
6541 /* jump unless last stmt in top-level block */
6542 if (tok
!= '}' || local_scope
!= 1)
6546 } else if (t
== TOK_BREAK
) {
6548 if (!cur_scope
->bsym
)
6549 tcc_error("cannot break");
6550 if (!cur_switch
|| cur_scope
->bsym
!= cur_switch
->bsym
)
6551 leave_scope(loop_scope
);
6553 leave_scope(cur_switch
->scope
);
6554 *cur_scope
->bsym
= gjmp(*cur_scope
->bsym
);
6557 } else if (t
== TOK_CONTINUE
) {
6559 if (!cur_scope
->csym
)
6560 tcc_error("cannot continue");
6561 leave_scope(loop_scope
);
6562 *cur_scope
->csym
= gjmp(*cur_scope
->csym
);
6565 } else if (t
== TOK_FOR
) {
6571 /* c99 for-loop init decl? */
6572 if (!decl0(VT_LOCAL
, 1, NULL
)) {
6573 /* no, regular for-loop init expr */
6601 } else if (t
== TOK_DO
) {
6615 } else if (t
== TOK_SWITCH
) {
6616 struct switch_t
*saved
, sw
;
6623 sw
.scope
= cur_scope
;
6631 switchval
= *vtop
--;
6634 b
= gjmp(0); /* jump to first case */
6636 a
= gjmp(a
); /* add implicit break */
6640 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6641 for (b
= 1; b
< sw
.n
; b
++)
6642 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6643 tcc_error("duplicate case value");
6645 /* Our switch table sorting is signed, so the compared
6646 value needs to be as well when it's 64bit. */
6647 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6648 switchval
.type
.t
&= ~VT_UNSIGNED
;
6651 d
= 0, gcase(sw
.p
, sw
.n
, &d
);
6654 gsym_addr(d
, sw
.def_sym
);
6660 dynarray_reset(&sw
.p
, &sw
.n
);
6663 } else if (t
== TOK_CASE
) {
6664 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6667 cr
->v1
= cr
->v2
= expr_const64();
6668 if (gnu_ext
&& tok
== TOK_DOTS
) {
6670 cr
->v2
= expr_const64();
6671 if (cr
->v2
< cr
->v1
)
6672 tcc_warning("empty case range");
6675 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6678 goto block_after_label
;
6680 } else if (t
== TOK_DEFAULT
) {
6683 if (cur_switch
->def_sym
)
6684 tcc_error("too many 'default'");
6685 cur_switch
->def_sym
= gind();
6688 goto block_after_label
;
6690 } else if (t
== TOK_GOTO
) {
6691 vla_restore(root_scope
->vla
.loc
);
6692 if (tok
== '*' && gnu_ext
) {
6696 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6700 } else if (tok
>= TOK_UIDENT
) {
6701 s
= label_find(tok
);
6702 /* put forward definition if needed */
6704 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6705 else if (s
->r
== LABEL_DECLARED
)
6706 s
->r
= LABEL_FORWARD
;
6708 if (s
->r
& LABEL_FORWARD
) {
6709 /* start new goto chain for cleanups, linked via label->next */
6710 if (cur_scope
->cl
.s
&& !nocode_wanted
) {
6711 sym_push2(&pending_gotos
, SYM_FIELD
, 0, cur_scope
->cl
.n
);
6712 pending_gotos
->prev_tok
= s
;
6713 s
= sym_push2(&s
->next
, SYM_FIELD
, 0, 0);
6714 pending_gotos
->next
= s
;
6716 s
->jnext
= gjmp(s
->jnext
);
6718 try_call_cleanup_goto(s
->cleanupstate
);
6719 gjmp_addr(s
->jnext
);
6724 expect("label identifier");
6728 } else if (t
== TOK_ASM1
|| t
== TOK_ASM2
|| t
== TOK_ASM3
) {
6732 if (tok
== ':' && t
>= TOK_UIDENT
) {
6737 if (s
->r
== LABEL_DEFINED
)
6738 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6739 s
->r
= LABEL_DEFINED
;
6741 Sym
*pcl
; /* pending cleanup goto */
6742 for (pcl
= s
->next
; pcl
; pcl
= pcl
->prev
)
6744 sym_pop(&s
->next
, NULL
, 0);
6748 s
= label_push(&global_label_stack
, t
, LABEL_DEFINED
);
6751 s
->cleanupstate
= cur_scope
->cl
.s
;
6754 vla_restore(cur_scope
->vla
.loc
);
6755 /* we accept this, but it is a mistake */
6757 tcc_warning("deprecated use of label at end of compound statement");
6763 /* expression case */
6779 /* This skips over a stream of tokens containing balanced {} and ()
6780 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6781 with a '{'). If STR then allocates and stores the skipped tokens
6782 in *STR. This doesn't check if () and {} are nested correctly,
6783 i.e. "({)}" is accepted. */
6784 static void skip_or_save_block(TokenString
**str
)
6786 int braces
= tok
== '{';
6789 *str
= tok_str_alloc();
6791 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6793 if (tok
== TOK_EOF
) {
6794 if (str
|| level
> 0)
6795 tcc_error("unexpected end of file");
6800 tok_str_add_tok(*str
);
6803 if (t
== '{' || t
== '(') {
6805 } else if (t
== '}' || t
== ')') {
6807 if (level
== 0 && braces
&& t
== '}')
6812 tok_str_add(*str
, -1);
6813 tok_str_add(*str
, 0);
6817 #define EXPR_CONST 1
6820 static void parse_init_elem(int expr_type
)
6822 int saved_global_expr
;
6825 /* compound literals must be allocated globally in this case */
6826 saved_global_expr
= global_expr
;
6829 global_expr
= saved_global_expr
;
6830 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6831 (compound literals). */
6832 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6833 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6834 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6835 #ifdef TCC_TARGET_PE
6836 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6839 tcc_error("initializer element is not constant");
6847 /* put zeros for variable based init */
6848 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6851 /* nothing to do because globals are already set to zero */
6853 vpush_global_sym(&func_old_type
, TOK_memset
);
6855 #ifdef TCC_TARGET_ARM
6867 #define DIF_SIZE_ONLY 2
6868 #define DIF_HAVE_ELEM 4
6870 /* t is the array or struct type. c is the array or struct
6871 address. cur_field is the pointer to the current
6872 field, for arrays the 'c' member contains the current start
6873 index. 'flags' is as in decl_initializer.
6874 'al' contains the already initialized length of the
6875 current container (starting at c). This returns the new length of that. */
6876 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6877 Sym
**cur_field
, int flags
, int al
)
6880 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6881 unsigned long corig
= c
;
6886 if (flags
& DIF_HAVE_ELEM
)
6889 if (gnu_ext
&& tok
>= TOK_UIDENT
) {
6896 /* NOTE: we only support ranges for last designator */
6897 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6899 if (!(type
->t
& VT_ARRAY
))
6900 expect("array type");
6902 index
= index_last
= expr_const();
6903 if (tok
== TOK_DOTS
&& gnu_ext
) {
6905 index_last
= expr_const();
6909 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6911 tcc_error("invalid index");
6913 (*cur_field
)->c
= index_last
;
6914 type
= pointed_type(type
);
6915 elem_size
= type_size(type
, &align
);
6916 c
+= index
* elem_size
;
6917 nb_elems
= index_last
- index
+ 1;
6924 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6925 expect("struct/union type");
6927 f
= find_field(type
, l
, &cumofs
);
6940 } else if (!gnu_ext
) {
6945 if (type
->t
& VT_ARRAY
) {
6946 index
= (*cur_field
)->c
;
6947 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6948 tcc_error("index too large");
6949 type
= pointed_type(type
);
6950 c
+= index
* type_size(type
, &align
);
6953 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6954 *cur_field
= f
= f
->next
;
6956 tcc_error("too many field init");
6961 /* must put zero in holes (note that doing it that way
6962 ensures that it even works with designators) */
6963 if (!(flags
& DIF_SIZE_ONLY
) && c
- corig
> al
)
6964 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6965 decl_initializer(type
, sec
, c
, flags
& ~DIF_FIRST
);
6967 /* XXX: make it more general */
6968 if (!(flags
& DIF_SIZE_ONLY
) && nb_elems
> 1) {
6969 unsigned long c_end
;
6974 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6975 for (i
= 1; i
< nb_elems
; i
++) {
6976 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6981 } else if (!NODATA_WANTED
) {
6982 c_end
= c
+ nb_elems
* elem_size
;
6983 if (c_end
> sec
->data_allocated
)
6984 section_realloc(sec
, c_end
);
6985 src
= sec
->data
+ c
;
6987 for(i
= 1; i
< nb_elems
; i
++) {
6989 memcpy(dst
, src
, elem_size
);
6993 c
+= nb_elems
* type_size(type
, &align
);
6999 /* store a value or an expression directly in global data or in local array */
7000 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
7007 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7011 /* XXX: not portable */
7012 /* XXX: generate error if incorrect relocation */
7013 gen_assign_cast(&dtype
);
7014 bt
= type
->t
& VT_BTYPE
;
7016 if ((vtop
->r
& VT_SYM
)
7019 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
7020 || (type
->t
& VT_BITFIELD
))
7021 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
7023 tcc_error("initializer element is not computable at load time");
7025 if (NODATA_WANTED
) {
7030 size
= type_size(type
, &align
);
7031 section_reserve(sec
, c
+ size
);
7032 ptr
= sec
->data
+ c
;
7034 /* XXX: make code faster ? */
7035 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
7036 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
7037 /* XXX This rejects compound literals like
7038 '(void *){ptr}'. The problem is that '&sym' is
7039 represented the same way, which would be ruled out
7040 by the SYM_FIRST_ANOM check above, but also '"string"'
7041 in 'char *p = "string"' is represented the same
7042 with the type being VT_PTR and the symbol being an
7043 anonymous one. That is, there's no difference in vtop
7044 between '(void *){x}' and '&(void *){x}'. Ignore
7045 pointer typed entities here. Hopefully no real code
7046 will every use compound literals with scalar type. */
7047 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
7048 /* These come from compound literals, memcpy stuff over. */
7052 esym
= elfsym(vtop
->sym
);
7053 ssec
= tcc_state
->sections
[esym
->st_shndx
];
7054 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
7056 /* We need to copy over all memory contents, and that
7057 includes relocations. Use the fact that relocs are
7058 created it order, so look from the end of relocs
7059 until we hit one before the copied region. */
7060 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
7061 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
7062 while (num_relocs
--) {
7064 if (rel
->r_offset
>= esym
->st_value
+ size
)
7066 if (rel
->r_offset
< esym
->st_value
)
7068 /* Note: if the same fields are initialized multiple
7069 times (possible with designators) then we possibly
7070 add multiple relocations for the same offset here.
7071 That would lead to wrong code, the last reloc needs
7072 to win. We clean this up later after the whole
7073 initializer is parsed. */
7074 put_elf_reloca(symtab_section
, sec
,
7075 c
+ rel
->r_offset
- esym
->st_value
,
7076 ELFW(R_TYPE
)(rel
->r_info
),
7077 ELFW(R_SYM
)(rel
->r_info
),
7087 if (type
->t
& VT_BITFIELD
) {
7088 int bit_pos
, bit_size
, bits
, n
;
7089 unsigned char *p
, v
, m
;
7090 bit_pos
= BIT_POS(vtop
->type
.t
);
7091 bit_size
= BIT_SIZE(vtop
->type
.t
);
7092 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
7093 bit_pos
&= 7, bits
= 0;
7098 v
= vtop
->c
.i
>> bits
<< bit_pos
;
7099 m
= ((1 << n
) - 1) << bit_pos
;
7100 *p
= (*p
& ~m
) | (v
& m
);
7101 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
7105 /* XXX: when cross-compiling we assume that each type has the
7106 same representation on host and target, which is likely to
7107 be wrong in the case of long double */
7109 vtop
->c
.i
= vtop
->c
.i
!= 0;
7111 *(char *)ptr
|= vtop
->c
.i
;
7114 *(short *)ptr
|= vtop
->c
.i
;
7117 *(float*)ptr
= vtop
->c
.f
;
7120 *(double *)ptr
= vtop
->c
.d
;
7123 #if defined TCC_IS_NATIVE_387
7124 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
7125 memcpy(ptr
, &vtop
->c
.ld
, 10);
7127 else if (sizeof (long double) == sizeof (double))
7128 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr
) : "m" (vtop
->c
.ld
));
7130 else if (vtop
->c
.ld
== 0.0)
7134 if (sizeof(long double) == LDOUBLE_SIZE
)
7135 *(long double*)ptr
= vtop
->c
.ld
;
7136 else if (sizeof(double) == LDOUBLE_SIZE
)
7137 *(double *)ptr
= (double)vtop
->c
.ld
;
7139 tcc_error("can't cross compile long double constants");
7143 *(long long *)ptr
|= vtop
->c
.i
;
7150 addr_t val
= vtop
->c
.i
;
7152 if (vtop
->r
& VT_SYM
)
7153 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
7155 *(addr_t
*)ptr
|= val
;
7157 if (vtop
->r
& VT_SYM
)
7158 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
7159 *(addr_t
*)ptr
|= val
;
7165 int val
= vtop
->c
.i
;
7167 if (vtop
->r
& VT_SYM
)
7168 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
7172 if (vtop
->r
& VT_SYM
)
7173 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
7182 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
7189 /* 't' contains the type and storage info. 'c' is the offset of the
7190 object in section 'sec'. If 'sec' is NULL, it means stack based
7191 allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
7192 dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
7193 size only evaluation is wanted (only for arrays). */
7194 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
7197 int len
, n
, no_oblock
, nb
, i
;
7203 if (!(flags
& DIF_HAVE_ELEM
) && tok
!= '{' &&
7204 /* In case of strings we have special handling for arrays, so
7205 don't consume them as initializer value (which would commit them
7206 to some anonymous symbol). */
7207 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
7208 !(flags
& DIF_SIZE_ONLY
)) {
7209 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7210 flags
|= DIF_HAVE_ELEM
;
7213 if ((flags
& DIF_HAVE_ELEM
) &&
7214 !(type
->t
& VT_ARRAY
) &&
7215 /* Use i_c_parameter_t, to strip toplevel qualifiers.
7216 The source type might have VT_CONSTANT set, which is
7217 of course assignable to non-const elements. */
7218 is_compatible_unqualified_types(type
, &vtop
->type
)) {
7219 init_putv(type
, sec
, c
);
7220 } else if (type
->t
& VT_ARRAY
) {
7223 t1
= pointed_type(type
);
7224 size1
= type_size(t1
, &align1
);
7227 if (((flags
& DIF_FIRST
) && tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
7230 tcc_error("character array initializer must be a literal,"
7231 " optionally enclosed in braces");
7236 /* only parse strings here if correct type (otherwise: handle
7237 them as ((w)char *) expressions */
7238 if ((tok
== TOK_LSTR
&&
7239 #ifdef TCC_TARGET_PE
7240 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
7242 (t1
->t
& VT_BTYPE
) == VT_INT
7244 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
7246 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7249 /* compute maximum number of chars wanted */
7251 cstr_len
= tokc
.str
.size
;
7253 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
7256 if (n
>= 0 && nb
> (n
- len
))
7258 if (!(flags
& DIF_SIZE_ONLY
)) {
7260 tcc_warning("initializer-string for array is too long");
7261 /* in order to go faster for common case (char
7262 string in global variable, we handle it
7264 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7266 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
7270 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
7272 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
7274 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
7281 /* only add trailing zero if enough storage (no
7282 warning in this case since it is standard) */
7283 if (n
< 0 || len
< n
) {
7284 if (!(flags
& DIF_SIZE_ONLY
)) {
7286 init_putv(t1
, sec
, c
+ (len
* size1
));
7297 while (tok
!= '}' || (flags
& DIF_HAVE_ELEM
)) {
7298 len
= decl_designator(type
, sec
, c
, &f
, flags
, len
);
7299 flags
&= ~DIF_HAVE_ELEM
;
7300 if (type
->t
& VT_ARRAY
) {
7302 /* special test for multi dimensional arrays (may not
7303 be strictly correct if designators are used at the
7305 if (no_oblock
&& len
>= n
*size1
)
7308 if (s
->type
.t
== VT_UNION
)
7312 if (no_oblock
&& f
== NULL
)
7321 /* put zeros at the end */
7322 if (!(flags
& DIF_SIZE_ONLY
) && len
< n
*size1
)
7323 init_putz(sec
, c
+ len
, n
*size1
- len
);
7326 /* patch type size if needed, which happens only for array types */
7328 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
7329 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7332 if ((flags
& DIF_FIRST
) || tok
== '{') {
7340 } else if (tok
== '{') {
7341 if (flags
& DIF_HAVE_ELEM
)
7344 decl_initializer(type
, sec
, c
, flags
& ~DIF_HAVE_ELEM
);
7346 } else if ((flags
& DIF_SIZE_ONLY
)) {
7347 /* If we supported only ISO C we wouldn't have to accept calling
7348 this on anything than an array if DIF_SIZE_ONLY (and even then
7349 only on the outermost level, so no recursion would be needed),
7350 because initializing a flex array member isn't supported.
7351 But GNU C supports it, so we need to recurse even into
7352 subfields of structs and arrays when DIF_SIZE_ONLY is set. */
7353 /* just skip expression */
7354 skip_or_save_block(NULL
);
7356 if (!(flags
& DIF_HAVE_ELEM
)) {
7357 /* This should happen only when we haven't parsed
7358 the init element above for fear of committing a
7359 string constant to memory too early. */
7360 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
7361 expect("string constant");
7362 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7364 init_putv(type
, sec
, c
);
7368 /* parse an initializer for type 't' if 'has_init' is non zero, and
7369 allocate space in local or global data space ('r' is either
7370 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7371 variable 'v' of scope 'scope' is declared before initializers
7372 are parsed. If 'v' is zero, then a reference to the new object
7373 is put in the value stack. If 'has_init' is 2, a special parsing
7374 is done to handle string constants. */
7375 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7376 int has_init
, int v
, int scope
)
7378 int size
, align
, addr
;
7379 TokenString
*init_str
= NULL
;
7382 Sym
*flexible_array
;
7384 int saved_nocode_wanted
= nocode_wanted
;
7385 #ifdef CONFIG_TCC_BCHECK
7386 int bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
7389 /* Always allocate static or global variables */
7390 if (v
&& (r
& VT_VALMASK
) == VT_CONST
)
7391 nocode_wanted
|= 0x80000000;
7393 flexible_array
= NULL
;
7394 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7395 Sym
*field
= type
->ref
->next
;
7398 field
= field
->next
;
7399 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
7400 flexible_array
= field
;
7404 size
= type_size(type
, &align
);
7405 /* If unknown size, we must evaluate it before
7406 evaluating initializers because
7407 initializers can generate global data too
7408 (e.g. string pointers or ISOC99 compound
7409 literals). It also simplifies local
7410 initializers handling */
7411 if (size
< 0 || (flexible_array
&& has_init
)) {
7413 tcc_error("unknown type size");
7414 /* get all init string */
7415 if (has_init
== 2) {
7416 init_str
= tok_str_alloc();
7417 /* only get strings */
7418 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7419 tok_str_add_tok(init_str
);
7422 tok_str_add(init_str
, -1);
7423 tok_str_add(init_str
, 0);
7425 skip_or_save_block(&init_str
);
7430 begin_macro(init_str
, 1);
7432 decl_initializer(type
, NULL
, 0, DIF_FIRST
| DIF_SIZE_ONLY
);
7433 /* prepare second initializer parsing */
7434 macro_ptr
= init_str
->str
;
7437 /* if still unknown size, error */
7438 size
= type_size(type
, &align
);
7440 tcc_error("unknown type size");
7442 /* If there's a flex member and it was used in the initializer
7444 if (flexible_array
&&
7445 flexible_array
->type
.ref
->c
> 0)
7446 size
+= flexible_array
->type
.ref
->c
7447 * pointed_size(&flexible_array
->type
);
7448 /* take into account specified alignment if bigger */
7449 if (ad
->a
.aligned
) {
7450 int speca
= 1 << (ad
->a
.aligned
- 1);
7453 } else if (ad
->a
.packed
) {
7457 if (!v
&& NODATA_WANTED
)
7458 size
= 0, align
= 1;
7460 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7462 #ifdef CONFIG_TCC_BCHECK
7464 /* add padding between stack variables for bound checking */
7468 loc
= (loc
- size
) & -align
;
7470 #ifdef CONFIG_TCC_BCHECK
7472 /* add padding between stack variables for bound checking */
7477 /* local variable */
7478 #ifdef CONFIG_TCC_ASM
7479 if (ad
->asm_label
) {
7480 int reg
= asm_parse_regvar(ad
->asm_label
);
7482 r
= (r
& ~VT_VALMASK
) | reg
;
7485 sym
= sym_push(v
, type
, r
, addr
);
7486 if (ad
->cleanup_func
) {
7487 Sym
*cls
= sym_push2(&all_cleanups
,
7488 SYM_FIELD
| ++cur_scope
->cl
.n
, 0, 0);
7489 cls
->prev_tok
= sym
;
7490 cls
->next
= ad
->cleanup_func
;
7491 cls
->ncl
= cur_scope
->cl
.s
;
7492 cur_scope
->cl
.s
= cls
;
7497 /* push local reference */
7498 vset(type
, r
, addr
);
7501 if (v
&& scope
== VT_CONST
) {
7502 /* see if the symbol was already defined */
7505 patch_storage(sym
, ad
, type
);
7506 /* we accept several definitions of the same global variable. */
7507 if (!has_init
&& sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
)
7512 /* allocate symbol in corresponding section */
7517 else if (tcc_state
->nocommon
)
7522 addr
= section_add(sec
, size
, align
);
7523 #ifdef CONFIG_TCC_BCHECK
7524 /* add padding if bound check */
7526 section_add(sec
, 1, 1);
7529 addr
= align
; /* SHN_COMMON is special, symbol value is align */
7530 sec
= common_section
;
7535 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7536 patch_storage(sym
, ad
, NULL
);
7538 /* update symbol definition */
7539 put_extern_sym(sym
, sec
, addr
, size
);
7541 /* push global reference */
7542 vpush_ref(type
, sec
, addr
, size
);
7547 #ifdef CONFIG_TCC_BCHECK
7548 /* handles bounds now because the symbol must be defined
7549 before for the relocation */
7553 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
7554 /* then add global bound info */
7555 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
7556 bounds_ptr
[0] = 0; /* relocated */
7557 bounds_ptr
[1] = size
;
7562 if (type
->t
& VT_VLA
) {
7568 /* save current stack pointer */
7569 if (root_scope
->vla
.loc
== 0) {
7570 struct scope
*v
= cur_scope
;
7571 gen_vla_sp_save(loc
-= PTR_SIZE
);
7572 do v
->vla
.loc
= loc
; while ((v
= v
->prev
));
7575 vla_runtime_type_size(type
, &a
);
7576 gen_vla_alloc(type
, a
);
7577 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7578 /* on _WIN64, because of the function args scratch area, the
7579 result of alloca differs from RSP and is returned in RAX. */
7580 gen_vla_result(addr
), addr
= (loc
-= PTR_SIZE
);
7582 gen_vla_sp_save(addr
);
7583 cur_scope
->vla
.loc
= addr
;
7584 cur_scope
->vla
.num
++;
7585 #ifdef CONFIG_TCC_BCHECK
7589 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
7590 bounds_ptr
[0] = 1; /* marks alloca/vla used */
7595 } else if (has_init
) {
7596 size_t oldreloc_offset
= 0;
7597 if (sec
&& sec
->reloc
)
7598 oldreloc_offset
= sec
->reloc
->data_offset
;
7599 decl_initializer(type
, sec
, addr
, DIF_FIRST
);
7600 if (sec
&& sec
->reloc
)
7601 squeeze_multi_relocs(sec
, oldreloc_offset
);
7602 /* patch flexible array member size back to -1, */
7603 /* for possible subsequent similar declarations */
7605 flexible_array
->type
.ref
->c
= -1;
7609 /* restore parse state if needed */
7615 nocode_wanted
= saved_nocode_wanted
;
7618 /* parse a function defined by symbol 'sym' and generate its code in
7619 'cur_text_section' */
7620 static void gen_function(Sym
*sym
, AttributeDef
*ad
)
7622 /* Initialize VLA state */
7623 struct scope f
= { 0 };
7624 cur_scope
= root_scope
= &f
;
7627 ind
= cur_text_section
->data_offset
;
7628 if (sym
->a
.aligned
) {
7629 size_t newoff
= section_add(cur_text_section
, 0,
7630 1 << (sym
->a
.aligned
- 1));
7631 gen_fill_nops(newoff
- ind
);
7633 /* NOTE: we patch the symbol size later */
7634 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7636 if (ad
&& ad
->constructor
) {
7637 add_init_array (tcc_state
, sym
);
7639 if (ad
&& ad
->destructor
) {
7640 add_fini_array (tcc_state
, sym
);
7643 funcname
= get_tok_str(sym
->v
, NULL
);
7646 /* put debug symbol */
7647 tcc_debug_funcstart(tcc_state
, sym
);
7648 /* push a dummy symbol to enable local sym storage */
7649 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7650 local_scope
= 1; /* for function parameters */
7654 clear_temp_local_var_list();
7658 /* reset local stack */
7659 pop_local_syms(&local_stack
, NULL
, 0);
7661 cur_text_section
->data_offset
= ind
;
7663 label_pop(&global_label_stack
, NULL
, 0);
7664 sym_pop(&all_cleanups
, NULL
, 0);
7665 /* patch symbol size */
7666 elfsym(sym
)->st_size
= ind
- func_ind
;
7667 /* end of function */
7668 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7669 /* It's better to crash than to generate wrong code */
7670 cur_text_section
= NULL
;
7671 funcname
= ""; /* for safety */
7672 func_vt
.t
= VT_VOID
; /* for safety */
7673 func_var
= 0; /* for safety */
7674 ind
= 0; /* for safety */
7675 nocode_wanted
= 0x80000000;
7679 static void gen_inline_functions(TCCState
*s
)
7682 int inline_generated
, i
;
7683 struct InlineFunc
*fn
;
7685 tcc_open_bf(s
, ":inline:", 0);
7686 /* iterate while inline function are referenced */
7688 inline_generated
= 0;
7689 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7690 fn
= s
->inline_fns
[i
];
7692 if (sym
&& (sym
->c
|| !(sym
->type
.t
& VT_INLINE
))) {
7693 /* the function was used or forced (and then not internal):
7694 generate its code and convert it to a normal function */
7696 tcc_debug_putfile(s
, fn
->filename
);
7697 begin_macro(fn
->func_str
, 1);
7699 cur_text_section
= text_section
;
7700 gen_function(sym
, NULL
);
7703 inline_generated
= 1;
7706 } while (inline_generated
);
7710 static void free_inline_functions(TCCState
*s
)
7713 /* free tokens of unused inline functions */
7714 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7715 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7717 tok_str_free(fn
->func_str
);
7719 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7722 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7723 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7724 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7729 AttributeDef ad
, adbase
;
7732 if (tok
== TOK_STATIC_ASSERT
) {
7740 tcc_error("%s", get_tok_str(tok
, &tokc
));
7746 if (!parse_btype(&btype
, &adbase
)) {
7747 if (is_for_loop_init
)
7749 /* skip redundant ';' if not in old parameter decl scope */
7750 if (tok
== ';' && l
!= VT_CMP
) {
7756 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7757 /* global asm block */
7761 if (tok
>= TOK_UIDENT
) {
7762 /* special test for old K&R protos without explicit int
7763 type. Only accepted when defining global data */
7767 expect("declaration");
7772 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7773 int v
= btype
.ref
->v
;
7774 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7775 tcc_warning("unnamed struct/union that defines no instances");
7779 if (IS_ENUM(btype
.t
)) {
7784 while (1) { /* iterate thru each declaration */
7786 /* If the base type itself was an array type of unspecified
7787 size (like in 'typedef int arr[]; arr x = {1};') then
7788 we will overwrite the unknown size by the real one for
7789 this decl. We need to unshare the ref symbol holding
7791 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7792 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7795 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7799 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7800 printf("type = '%s'\n", buf
);
7803 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7804 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
))
7805 tcc_error("function without file scope cannot be static");
7806 /* if old style function prototype, we accept a
7809 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7810 decl0(VT_CMP
, 0, sym
);
7811 /* always compile 'extern inline' */
7812 if (type
.t
& VT_EXTERN
)
7813 type
.t
&= ~VT_INLINE
;
7816 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7817 ad
.asm_label
= asm_label_instr();
7818 /* parse one last attribute list, after asm label */
7819 parse_attribute(&ad
);
7821 /* gcc does not allow __asm__("label") with function definition,
7828 #ifdef TCC_TARGET_PE
7829 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7830 if (type
.t
& VT_STATIC
)
7831 tcc_error("cannot have dll linkage with static");
7832 if (type
.t
& VT_TYPEDEF
) {
7833 tcc_warning("'%s' attribute ignored for typedef",
7834 ad
.a
.dllimport
? (ad
.a
.dllimport
= 0, "dllimport") :
7835 (ad
.a
.dllexport
= 0, "dllexport"));
7836 } else if (ad
.a
.dllimport
) {
7837 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7840 type
.t
|= VT_EXTERN
;
7846 tcc_error("cannot use local functions");
7847 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7848 expect("function definition");
7850 /* reject abstract declarators in function definition
7851 make old style params without decl have int type */
7853 while ((sym
= sym
->next
) != NULL
) {
7854 if (!(sym
->v
& ~SYM_FIELD
))
7855 expect("identifier");
7856 if (sym
->type
.t
== VT_VOID
)
7857 sym
->type
= int_type
;
7860 /* put function symbol */
7861 type
.t
&= ~VT_EXTERN
;
7862 sym
= external_sym(v
, &type
, 0, &ad
);
7863 /* static inline functions are just recorded as a kind
7864 of macro. Their code will be emitted at the end of
7865 the compilation unit only if they are used */
7866 if (sym
->type
.t
& VT_INLINE
) {
7867 struct InlineFunc
*fn
;
7868 const char *filename
;
7870 filename
= file
? file
->filename
: "";
7871 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7872 strcpy(fn
->filename
, filename
);
7874 skip_or_save_block(&fn
->func_str
);
7875 dynarray_add(&tcc_state
->inline_fns
,
7876 &tcc_state
->nb_inline_fns
, fn
);
7878 /* compute text section */
7879 cur_text_section
= ad
.section
;
7880 if (!cur_text_section
)
7881 cur_text_section
= text_section
;
7882 gen_function(sym
, &ad
);
7887 /* find parameter in function parameter list */
7888 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7889 if ((sym
->v
& ~SYM_FIELD
) == v
)
7891 tcc_error("declaration for parameter '%s' but no such parameter",
7892 get_tok_str(v
, NULL
));
7894 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7895 tcc_error("storage class specified for '%s'",
7896 get_tok_str(v
, NULL
));
7897 if (sym
->type
.t
!= VT_VOID
)
7898 tcc_error("redefinition of parameter '%s'",
7899 get_tok_str(v
, NULL
));
7900 convert_parameter_type(&type
);
7902 } else if (type
.t
& VT_TYPEDEF
) {
7903 /* save typedefed type */
7904 /* XXX: test storage specifiers ? */
7906 if (sym
&& sym
->sym_scope
== local_scope
) {
7907 if (!is_compatible_types(&sym
->type
, &type
)
7908 || !(sym
->type
.t
& VT_TYPEDEF
))
7909 tcc_error("incompatible redefinition of '%s'",
7910 get_tok_str(v
, NULL
));
7913 sym
= sym_push(v
, &type
, 0, 0);
7917 } else if ((type
.t
& VT_BTYPE
) == VT_VOID
7918 && !(type
.t
& VT_EXTERN
)) {
7919 tcc_error("declaration of void object");
7922 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7923 /* external function definition */
7924 /* specific case for func_call attribute */
7926 } else if (!(type
.t
& VT_ARRAY
)) {
7927 /* not lvalue if array */
7930 has_init
= (tok
== '=');
7931 if (has_init
&& (type
.t
& VT_VLA
))
7932 tcc_error("variable length array cannot be initialized");
7933 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
))
7934 || (type
.t
& VT_BTYPE
) == VT_FUNC
7935 /* as with GCC, uninitialized global arrays with no size
7936 are considered extern: */
7937 || ((type
.t
& VT_ARRAY
) && !has_init
7938 && l
== VT_CONST
&& type
.ref
->c
< 0)
7940 /* external variable or function */
7941 type
.t
|= VT_EXTERN
;
7942 sym
= external_sym(v
, &type
, r
, &ad
);
7943 if (ad
.alias_target
) {
7946 alias_target
= sym_find(ad
.alias_target
);
7947 esym
= elfsym(alias_target
);
7949 tcc_error("unsupported forward __alias__ attribute");
7950 put_extern_sym2(sym
, esym
->st_shndx
, esym
->st_value
, esym
->st_size
, 0);
7953 if (type
.t
& VT_STATIC
)
7959 else if (l
== VT_CONST
)
7960 /* uninitialized global variables may be overridden */
7961 type
.t
|= VT_EXTERN
;
7962 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7966 if (is_for_loop_init
)
7978 static void decl(int l
)
7983 /* ------------------------------------------------------------------------- */
7986 /* ------------------------------------------------------------------------- */