2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
60 ST_DATA
int const_wanted
; /* true if constant wanted */
61 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
62 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
63 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
65 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
66 ST_DATA
char *funcname
;
68 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void unary_type(CType
*type
);
83 static void vla_runtime_type_size(CType
*type
, int *a
);
84 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
85 static void expr_type(CType
*type
);
87 ST_INLN
int is_float(int t
)
91 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
94 /* we use our own 'finite' function to avoid potential problems with
95 non standard math libs */
96 /* XXX: endianness dependent */
97 ST_FUNC
int ieee_finite(double d
)
100 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
103 ST_FUNC
void test_lvalue(void)
105 if (!(vtop
->r
& VT_LVAL
))
109 /* ------------------------------------------------------------------------- */
110 /* symbol allocator */
111 static Sym
*__sym_malloc(void)
113 Sym
*sym_pool
, *sym
, *last_sym
;
116 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
117 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
119 last_sym
= sym_free_first
;
121 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
122 sym
->next
= last_sym
;
126 sym_free_first
= last_sym
;
130 static inline Sym
*sym_malloc(void)
133 sym
= sym_free_first
;
135 sym
= __sym_malloc();
136 sym_free_first
= sym
->next
;
140 ST_INLN
void sym_free(Sym
*sym
)
142 sym
->next
= sym_free_first
;
143 tcc_free(sym
->asm_label
);
144 sym_free_first
= sym
;
147 /* push, without hashing */
148 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
151 if (ps
== &local_stack
) {
152 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
153 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
154 tcc_error("incompatible types for redefinition of '%s'",
155 get_tok_str(v
, NULL
));
174 /* find a symbol and return its associated structure. 's' is the top
175 of the symbol stack */
176 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
186 /* structure lookup */
187 ST_INLN Sym
*struct_find(int v
)
190 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
192 return table_ident
[v
]->sym_struct
;
195 /* find an identifier */
196 ST_INLN Sym
*sym_find(int v
)
199 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
201 return table_ident
[v
]->sym_identifier
;
204 /* push a given symbol on the symbol stack */
205 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
214 s
= sym_push2(ps
, v
, type
->t
, c
);
215 s
->type
.ref
= type
->ref
;
217 /* don't record fields or anonymous symbols */
219 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
220 /* record symbol in token array */
221 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
223 ps
= &ts
->sym_struct
;
225 ps
= &ts
->sym_identifier
;
232 /* push a global identifier */
233 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
236 s
= sym_push2(&global_stack
, v
, t
, c
);
237 /* don't record anonymous symbol */
238 if (v
< SYM_FIRST_ANOM
) {
239 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
240 /* modify the top most local identifier, so that
241 sym_identifier will point to 's' when popped */
243 ps
= &(*ps
)->prev_tok
;
250 /* pop symbols until top reaches 'b' */
251 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
261 /* remove symbol in token array */
263 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
264 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
266 ps
= &ts
->sym_struct
;
268 ps
= &ts
->sym_identifier
;
277 static void weaken_symbol(Sym
*sym
)
279 sym
->type
.t
|= VT_WEAK
;
284 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
285 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
286 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
290 /* ------------------------------------------------------------------------- */
292 ST_FUNC
void swap(int *p
, int *q
)
300 static void vsetc(CType
*type
, int r
, CValue
*vc
)
304 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
305 tcc_error("memory full");
306 /* cannot let cpu flags if other instruction are generated. Also
307 avoid leaving VT_JMP anywhere except on the top of the stack
308 because it would complicate the code generator. */
309 if (vtop
>= vstack
) {
310 v
= vtop
->r
& VT_VALMASK
;
311 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
321 /* push constant of type "type" with useless value */
322 void vpush(CType
*type
)
325 vsetc(type
, VT_CONST
, &cval
);
328 /* push integer constant */
329 ST_FUNC
void vpushi(int v
)
333 vsetc(&int_type
, VT_CONST
, &cval
);
336 /* push a pointer sized constant */
337 static void vpushs(long long v
)
344 vsetc(&size_type
, VT_CONST
, &cval
);
347 /* push long long constant */
348 static void vpushll(long long v
)
355 vsetc(&ctype
, VT_CONST
, &cval
);
358 /* push arbitrary 64bit constant */
359 void vpush64(int ty
, unsigned long long v
)
366 vsetc(&ctype
, VT_CONST
, &cval
);
369 /* Return a static symbol pointing to a section */
370 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
376 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
377 sym
->type
.ref
= type
->ref
;
378 sym
->r
= VT_CONST
| VT_SYM
;
379 put_extern_sym(sym
, sec
, offset
, size
);
383 /* push a reference to a section offset by adding a dummy symbol */
384 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
389 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
390 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
393 /* define a new external reference to a symbol 'v' of type 'u' */
394 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
400 /* push forward reference */
401 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
402 s
->type
.ref
= type
->ref
;
403 s
->r
= r
| VT_CONST
| VT_SYM
;
408 /* define a new external reference to a symbol 'v' with alternate asm
409 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
410 is no alternate name (most cases) */
411 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
417 /* push forward reference */
418 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
419 s
->asm_label
= asm_label
;
420 s
->type
.t
|= VT_EXTERN
;
421 } else if (s
->type
.ref
== func_old_type
.ref
) {
422 s
->type
.ref
= type
->ref
;
423 s
->r
= r
| VT_CONST
| VT_SYM
;
424 s
->type
.t
|= VT_EXTERN
;
425 } else if (!is_compatible_types(&s
->type
, type
)) {
426 tcc_error("incompatible types for redefinition of '%s'",
427 get_tok_str(v
, NULL
));
432 /* push a reference to global symbol v */
433 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
438 sym
= external_global_sym(v
, type
, 0);
440 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
444 ST_FUNC
void vset(CType
*type
, int r
, int v
)
449 vsetc(type
, r
, &cval
);
452 static void vseti(int r
, int v
)
460 ST_FUNC
void vswap(void)
463 /* cannot let cpu flags if other instruction are generated. Also
464 avoid leaving VT_JMP anywhere except on the top of the stack
465 because it would complicate the code generator. */
466 if (vtop
>= vstack
) {
467 int v
= vtop
->r
& VT_VALMASK
;
468 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
475 /* XXX: +2% overall speed possible with optimized memswap
477 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
481 ST_FUNC
void vpushv(SValue
*v
)
483 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
484 tcc_error("memory full");
489 static void vdup(void)
494 /* save r to the memory stack, and mark it as being free */
495 ST_FUNC
void save_reg(int r
)
497 int l
, saved
, size
, align
;
501 /* modify all stack values */
504 for(p
=vstack
;p
<=vtop
;p
++) {
505 if ((p
->r
& VT_VALMASK
) == r
||
506 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
507 /* must save value on stack if not already done */
509 /* NOTE: must reload 'r' because r might be equal to r2 */
510 r
= p
->r
& VT_VALMASK
;
511 /* store register in the stack */
513 if ((p
->r
& VT_LVAL
) ||
514 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
515 #ifdef TCC_TARGET_X86_64
516 type
= &char_pointer_type
;
520 size
= type_size(type
, &align
);
521 loc
= (loc
- size
) & -align
;
523 sv
.r
= VT_LOCAL
| VT_LVAL
;
526 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
527 /* x86 specific: need to pop fp register ST0 if saved */
529 o(0xd8dd); /* fstp %st(0) */
532 #ifndef TCC_TARGET_X86_64
533 /* special long long case */
534 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
542 /* mark that stack entry as being saved on the stack */
543 if (p
->r
& VT_LVAL
) {
544 /* also clear the bounded flag because the
545 relocation address of the function was stored in
547 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
549 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
557 #ifdef TCC_TARGET_ARM
558 /* find a register of class 'rc2' with at most one reference on stack.
559 * If none, call get_reg(rc) */
560 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
565 for(r
=0;r
<NB_REGS
;r
++) {
566 if (reg_classes
[r
] & rc2
) {
569 for(p
= vstack
; p
<= vtop
; p
++) {
570 if ((p
->r
& VT_VALMASK
) == r
||
571 (p
->r2
& VT_VALMASK
) == r
)
582 /* find a free register of class 'rc'. If none, save one register */
583 ST_FUNC
int get_reg(int rc
)
588 /* find a free register */
589 for(r
=0;r
<NB_REGS
;r
++) {
590 if (reg_classes
[r
] & rc
) {
591 for(p
=vstack
;p
<=vtop
;p
++) {
592 if ((p
->r
& VT_VALMASK
) == r
||
593 (p
->r2
& VT_VALMASK
) == r
)
601 /* no register left : free the first one on the stack (VERY
602 IMPORTANT to start from the bottom to ensure that we don't
603 spill registers used in gen_opi()) */
604 for(p
=vstack
;p
<=vtop
;p
++) {
605 /* look at second register (if long long) */
606 r
= p
->r2
& VT_VALMASK
;
607 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
609 r
= p
->r
& VT_VALMASK
;
610 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
616 /* Should never comes here */
620 /* save registers up to (vtop - n) stack entry */
621 ST_FUNC
void save_regs(int n
)
626 for(p
= vstack
;p
<= p1
; p
++) {
627 r
= p
->r
& VT_VALMASK
;
634 /* move register 's' to 'r', and flush previous value of r to memory
636 static void move_reg(int r
, int s
)
649 /* get address of vtop (vtop MUST BE an lvalue) */
650 static void gaddrof(void)
652 if (vtop
->r
& VT_REF
)
655 /* tricky: if saved lvalue, then we can go back to lvalue */
656 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
657 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
662 #ifdef CONFIG_TCC_BCHECK
663 /* generate lvalue bound code */
664 static void gbound(void)
669 vtop
->r
&= ~VT_MUSTBOUND
;
670 /* if lvalue, then use checking code before dereferencing */
671 if (vtop
->r
& VT_LVAL
) {
672 /* if not VT_BOUNDED value, then make one */
673 if (!(vtop
->r
& VT_BOUNDED
)) {
674 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
675 /* must save type because we must set it to int to get pointer */
677 vtop
->type
.t
= VT_INT
;
680 gen_bounded_ptr_add();
681 vtop
->r
|= lval_type
;
684 /* then check for dereferencing */
685 gen_bounded_ptr_deref();
690 /* store vtop a register belonging to class 'rc'. lvalues are
691 converted to values. Cannot be used if cannot be converted to
692 register value (such as structures). */
693 ST_FUNC
int gv(int rc
)
695 int r
, bit_pos
, bit_size
, size
, align
, i
;
696 #ifndef TCC_TARGET_X86_64
700 /* NOTE: get_reg can modify vstack[] */
701 if (vtop
->type
.t
& VT_BITFIELD
) {
704 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
705 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
706 /* remove bit field info to avoid loops */
707 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
708 /* cast to int to propagate signedness in following ops */
709 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
714 if((vtop
->type
.t
& VT_UNSIGNED
) ||
715 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
716 type
.t
|= VT_UNSIGNED
;
718 /* generate shifts */
719 vpushi(bits
- (bit_pos
+ bit_size
));
721 vpushi(bits
- bit_size
);
722 /* NOTE: transformed to SHR if unsigned */
726 if (is_float(vtop
->type
.t
) &&
727 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
730 unsigned long offset
;
731 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
735 /* XXX: unify with initializers handling ? */
736 /* CPUs usually cannot use float constants, so we store them
737 generically in data segment */
738 size
= type_size(&vtop
->type
, &align
);
739 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
740 data_section
->data_offset
= offset
;
741 /* XXX: not portable yet */
742 #if defined(__i386__) || defined(__x86_64__)
743 /* Zero pad x87 tenbyte long doubles */
744 if (size
== LDOUBLE_SIZE
) {
745 vtop
->c
.tab
[2] &= 0xffff;
746 #if LDOUBLE_SIZE == 16
751 ptr
= section_ptr_add(data_section
, size
);
753 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
757 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
761 ptr
[i
] = vtop
->c
.tab
[i
];
762 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
763 vtop
->r
|= VT_LVAL
| VT_SYM
;
767 #ifdef CONFIG_TCC_BCHECK
768 if (vtop
->r
& VT_MUSTBOUND
)
772 r
= vtop
->r
& VT_VALMASK
;
773 #ifndef TCC_TARGET_X86_64
778 /* need to reload if:
780 - lvalue (need to dereference pointer)
781 - already a register, but not in the right class */
783 || (vtop
->r
& VT_LVAL
)
784 || !(reg_classes
[r
] & rc
)
785 #ifndef TCC_TARGET_X86_64
786 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
791 #ifndef TCC_TARGET_X86_64
792 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
794 unsigned long long ll
;
795 /* two register type load : expand to two words
797 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
800 vtop
->c
.ui
= ll
; /* first word */
802 vtop
->r
= r
; /* save register value */
803 vpushi(ll
>> 32); /* second word */
804 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
805 (vtop
->r
& VT_LVAL
)) {
806 /* We do not want to modifier the long long
807 pointer here, so the safest (and less
808 efficient) is to save all the other registers
809 in the stack. XXX: totally inefficient. */
811 /* load from memory */
814 vtop
[-1].r
= r
; /* save register value */
815 /* increment pointer to get second word */
816 vtop
->type
.t
= VT_INT
;
825 vtop
[-1].r
= r
; /* save register value */
826 vtop
->r
= vtop
[-1].r2
;
828 /* Allocate second register. Here we rely on the fact that
829 get_reg() tries first to free r2 of an SValue. */
833 /* write second register */
837 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
839 /* lvalue of scalar type : need to use lvalue type
840 because of possible cast */
843 /* compute memory access type */
844 if (vtop
->r
& VT_LVAL_BYTE
)
846 else if (vtop
->r
& VT_LVAL_SHORT
)
848 if (vtop
->r
& VT_LVAL_UNSIGNED
)
852 /* restore wanted type */
855 /* one register type load */
860 #ifdef TCC_TARGET_C67
861 /* uses register pairs for doubles */
862 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
869 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
870 ST_FUNC
void gv2(int rc1
, int rc2
)
874 /* generate more generic register first. But VT_JMP or VT_CMP
875 values must be generated first in all cases to avoid possible
877 v
= vtop
[0].r
& VT_VALMASK
;
878 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
883 /* test if reload is needed for first register */
884 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
894 /* test if reload is needed for first register */
895 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
901 /* wrapper around RC_FRET to return a register by type */
902 static int rc_fret(int t
)
904 #ifdef TCC_TARGET_X86_64
905 if (t
== VT_LDOUBLE
) {
912 /* wrapper around REG_FRET to return a register by type */
913 static int reg_fret(int t
)
915 #ifdef TCC_TARGET_X86_64
916 if (t
== VT_LDOUBLE
) {
923 /* expand long long on stack in two int registers */
924 static void lexpand(void)
928 u
= vtop
->type
.t
& VT_UNSIGNED
;
931 vtop
[0].r
= vtop
[-1].r2
;
932 vtop
[0].r2
= VT_CONST
;
933 vtop
[-1].r2
= VT_CONST
;
934 vtop
[0].type
.t
= VT_INT
| u
;
935 vtop
[-1].type
.t
= VT_INT
| u
;
938 #ifdef TCC_TARGET_ARM
939 /* expand long long on stack */
940 ST_FUNC
void lexpand_nr(void)
944 u
= vtop
->type
.t
& VT_UNSIGNED
;
947 vtop
->type
.t
= VT_INT
| u
;
948 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
950 vtop
[-1].c
.ui
= vtop
->c
.ull
;
951 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
953 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
955 vtop
->r
= vtop
[-1].r
;
956 } else if (v
> VT_CONST
) {
960 vtop
->r
= vtop
[-1].r2
;
961 vtop
[-1].r2
= VT_CONST
;
962 vtop
[-1].type
.t
= VT_INT
| u
;
966 /* build a long long from two ints */
967 static void lbuild(int t
)
970 vtop
[-1].r2
= vtop
[0].r
;
975 /* rotate n first stack elements to the bottom
976 I1 ... In -> I2 ... In I1 [top is right]
978 ST_FUNC
void vrotb(int n
)
989 /* rotate the n elements before entry e towards the top
990 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
992 ST_FUNC
void vrote(SValue
*e
, int n
)
998 for(i
= 0;i
< n
- 1; i
++)
1003 /* rotate n first stack elements to the top
1004 I1 ... In -> In I1 ... I(n-1) [top is right]
1006 ST_FUNC
void vrott(int n
)
1011 /* pop stack value */
1012 ST_FUNC
void vpop(void)
1015 v
= vtop
->r
& VT_VALMASK
;
1016 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1017 /* for x86, we need to pop the FP stack */
1018 if (v
== TREG_ST0
&& !nocode_wanted
) {
1019 o(0xd8dd); /* fstp %st(0) */
1022 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1023 /* need to put correct jump if && or || without test */
1029 /* convert stack entry to register and duplicate its value in another
1031 static void gv_dup(void)
1037 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1044 /* stack: H L L1 H1 */
1052 /* duplicate value */
1057 #ifdef TCC_TARGET_X86_64
1058 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1068 load(r1
, &sv
); /* move r to r1 */
1070 /* duplicates value */
1076 #ifndef TCC_TARGET_X86_64
1077 /* generate CPU independent (unsigned) long long operations */
1078 static void gen_opl(int op
)
1080 int t
, a
, b
, op1
, c
, i
;
1082 unsigned short reg_iret
= REG_IRET
;
1083 unsigned short reg_lret
= REG_LRET
;
1089 func
= TOK___divdi3
;
1092 func
= TOK___udivdi3
;
1095 func
= TOK___moddi3
;
1098 func
= TOK___umoddi3
;
1105 /* call generic long long function */
1106 vpush_global_sym(&func_old_type
, func
);
1111 vtop
->r2
= reg_lret
;
1124 /* stack: L1 H1 L2 H2 */
1129 vtop
[-2] = vtop
[-3];
1132 /* stack: H1 H2 L1 L2 */
1138 /* stack: H1 H2 L1 L2 ML MH */
1141 /* stack: ML MH H1 H2 L1 L2 */
1145 /* stack: ML MH H1 L2 H2 L1 */
1150 /* stack: ML MH M1 M2 */
1153 } else if (op
== '+' || op
== '-') {
1154 /* XXX: add non carry method too (for MIPS or alpha) */
1160 /* stack: H1 H2 (L1 op L2) */
1163 gen_op(op1
+ 1); /* TOK_xxxC2 */
1166 /* stack: H1 H2 (L1 op L2) */
1169 /* stack: (L1 op L2) H1 H2 */
1171 /* stack: (L1 op L2) (H1 op H2) */
1179 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1180 t
= vtop
[-1].type
.t
;
1184 /* stack: L H shift */
1186 /* constant: simpler */
1187 /* NOTE: all comments are for SHL. the other cases are
1188 done by swaping words */
1199 if (op
!= TOK_SAR
) {
1232 /* XXX: should provide a faster fallback on x86 ? */
1235 func
= TOK___ashrdi3
;
1238 func
= TOK___lshrdi3
;
1241 func
= TOK___ashldi3
;
1247 /* compare operations */
1253 /* stack: L1 H1 L2 H2 */
1255 vtop
[-1] = vtop
[-2];
1257 /* stack: L1 L2 H1 H2 */
1260 /* when values are equal, we need to compare low words. since
1261 the jump is inverted, we invert the test too. */
1264 else if (op1
== TOK_GT
)
1266 else if (op1
== TOK_ULT
)
1268 else if (op1
== TOK_UGT
)
1273 if (op1
!= TOK_NE
) {
1277 /* generate non equal test */
1278 /* XXX: NOT PORTABLE yet */
1282 #if defined(TCC_TARGET_I386)
1283 b
= psym(0x850f, 0);
1284 #elif defined(TCC_TARGET_ARM)
1286 o(0x1A000000 | encbranch(ind
, 0, 1));
1287 #elif defined(TCC_TARGET_C67)
1288 tcc_error("not implemented");
1290 #error not supported
1294 /* compare low. Always unsigned */
1298 else if (op1
== TOK_LE
)
1300 else if (op1
== TOK_GT
)
1302 else if (op1
== TOK_GE
)
1313 /* handle integer constant optimizations and various machine
1315 static void gen_opic(int op
)
1317 int c1
, c2
, t1
, t2
, n
;
1320 typedef unsigned long long U
;
1324 t1
= v1
->type
.t
& VT_BTYPE
;
1325 t2
= v2
->type
.t
& VT_BTYPE
;
1329 else if (v1
->type
.t
& VT_UNSIGNED
)
1336 else if (v2
->type
.t
& VT_UNSIGNED
)
1341 /* currently, we cannot do computations with forward symbols */
1342 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1343 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1346 case '+': l1
+= l2
; break;
1347 case '-': l1
-= l2
; break;
1348 case '&': l1
&= l2
; break;
1349 case '^': l1
^= l2
; break;
1350 case '|': l1
|= l2
; break;
1351 case '*': l1
*= l2
; break;
1358 /* if division by zero, generate explicit division */
1361 tcc_error("division by zero in constant");
1365 default: l1
/= l2
; break;
1366 case '%': l1
%= l2
; break;
1367 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1368 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1371 case TOK_SHL
: l1
<<= l2
; break;
1372 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1373 case TOK_SAR
: l1
>>= l2
; break;
1375 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1376 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1377 case TOK_EQ
: l1
= l1
== l2
; break;
1378 case TOK_NE
: l1
= l1
!= l2
; break;
1379 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1380 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1381 case TOK_LT
: l1
= l1
< l2
; break;
1382 case TOK_GE
: l1
= l1
>= l2
; break;
1383 case TOK_LE
: l1
= l1
<= l2
; break;
1384 case TOK_GT
: l1
= l1
> l2
; break;
1386 case TOK_LAND
: l1
= l1
&& l2
; break;
1387 case TOK_LOR
: l1
= l1
|| l2
; break;
1394 /* if commutative ops, put c2 as constant */
1395 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1396 op
== '|' || op
== '*')) {
1398 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1399 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1401 /* Filter out NOP operations like x*1, x-0, x&-1... */
1402 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1405 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1406 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1412 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1413 /* try to use shifts instead of muls or divs */
1414 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1423 else if (op
== TOK_PDIV
)
1429 } else if (c2
&& (op
== '+' || op
== '-') &&
1430 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1431 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1432 /* symbol + constant case */
1439 if (!nocode_wanted
) {
1440 /* call low level op generator */
1441 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1452 /* generate a floating point operation with constant propagation */
1453 static void gen_opif(int op
)
1461 /* currently, we cannot do computations with forward symbols */
1462 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1463 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1465 if (v1
->type
.t
== VT_FLOAT
) {
1468 } else if (v1
->type
.t
== VT_DOUBLE
) {
1476 /* NOTE: we only do constant propagation if finite number (not
1477 NaN or infinity) (ANSI spec) */
1478 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1482 case '+': f1
+= f2
; break;
1483 case '-': f1
-= f2
; break;
1484 case '*': f1
*= f2
; break;
1488 tcc_error("division by zero in constant");
1493 /* XXX: also handles tests ? */
1497 /* XXX: overflow test ? */
1498 if (v1
->type
.t
== VT_FLOAT
) {
1500 } else if (v1
->type
.t
== VT_DOUBLE
) {
1508 if (!nocode_wanted
) {
1516 static int pointed_size(CType
*type
)
1519 return type_size(pointed_type(type
), &align
);
1522 static void vla_runtime_pointed_size(CType
*type
)
1525 vla_runtime_type_size(pointed_type(type
), &align
);
1528 static inline int is_null_pointer(SValue
*p
)
1530 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1532 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1533 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1534 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1537 static inline int is_integer_btype(int bt
)
1539 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1540 bt
== VT_INT
|| bt
== VT_LLONG
);
1543 /* check types for comparison or substraction of pointers */
1544 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1546 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1549 /* null pointers are accepted for all comparisons as gcc */
1550 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1554 bt1
= type1
->t
& VT_BTYPE
;
1555 bt2
= type2
->t
& VT_BTYPE
;
1556 /* accept comparison between pointer and integer with a warning */
1557 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1558 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1559 tcc_warning("comparison between pointer and integer");
1563 /* both must be pointers or implicit function pointers */
1564 if (bt1
== VT_PTR
) {
1565 type1
= pointed_type(type1
);
1566 } else if (bt1
!= VT_FUNC
)
1567 goto invalid_operands
;
1569 if (bt2
== VT_PTR
) {
1570 type2
= pointed_type(type2
);
1571 } else if (bt2
!= VT_FUNC
) {
1573 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1575 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1576 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1580 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1581 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1582 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1583 /* gcc-like error if '-' is used */
1585 goto invalid_operands
;
1587 tcc_warning("comparison of distinct pointer types lacks a cast");
1591 /* generic gen_op: handles types problems */
1592 ST_FUNC
void gen_op(int op
)
1594 int u
, t1
, t2
, bt1
, bt2
, t
;
1597 t1
= vtop
[-1].type
.t
;
1598 t2
= vtop
[0].type
.t
;
1599 bt1
= t1
& VT_BTYPE
;
1600 bt2
= t2
& VT_BTYPE
;
1602 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1603 /* at least one operand is a pointer */
1604 /* relationnal op: must be both pointers */
1605 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1606 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1607 /* pointers are handled are unsigned */
1608 #ifdef TCC_TARGET_X86_64
1609 t
= VT_LLONG
| VT_UNSIGNED
;
1611 t
= VT_INT
| VT_UNSIGNED
;
1615 /* if both pointers, then it must be the '-' op */
1616 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1618 tcc_error("cannot use pointers here");
1619 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1620 /* XXX: check that types are compatible */
1621 if (vtop
[-1].type
.t
& VT_VLA
) {
1622 vla_runtime_pointed_size(&vtop
[-1].type
);
1624 vpushi(pointed_size(&vtop
[-1].type
));
1628 /* set to integer type */
1629 #ifdef TCC_TARGET_X86_64
1630 vtop
->type
.t
= VT_LLONG
;
1632 vtop
->type
.t
= VT_INT
;
1637 /* exactly one pointer : must be '+' or '-'. */
1638 if (op
!= '-' && op
!= '+')
1639 tcc_error("cannot use pointers here");
1640 /* Put pointer as first operand */
1641 if (bt2
== VT_PTR
) {
1645 type1
= vtop
[-1].type
;
1646 type1
.t
&= ~VT_ARRAY
;
1647 if (vtop
[-1].type
.t
& VT_VLA
)
1648 vla_runtime_pointed_size(&vtop
[-1].type
);
1650 u
= pointed_size(&vtop
[-1].type
);
1652 tcc_error("unknown array element size");
1653 #ifdef TCC_TARGET_X86_64
1656 /* XXX: cast to int ? (long long case) */
1661 #ifdef CONFIG_TCC_BCHECK
1662 /* if evaluating constant expression, no code should be
1663 generated, so no bound check */
1664 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1665 /* if bounded pointers, we generate a special code to
1672 gen_bounded_ptr_add();
1678 /* put again type if gen_opic() swaped operands */
1681 } else if (is_float(bt1
) || is_float(bt2
)) {
1682 /* compute bigger type and do implicit casts */
1683 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1685 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1690 /* floats can only be used for a few operations */
1691 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1692 (op
< TOK_ULT
|| op
> TOK_GT
))
1693 tcc_error("invalid operands for binary operation");
1695 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1696 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1697 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1700 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1701 /* cast to biggest op */
1703 /* convert to unsigned if it does not fit in a long long */
1704 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1705 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1708 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1709 tcc_error("comparison of struct");
1711 /* integer operations */
1713 /* convert to unsigned if it does not fit in an integer */
1714 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1715 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1718 /* XXX: currently, some unsigned operations are explicit, so
1719 we modify them here */
1720 if (t
& VT_UNSIGNED
) {
1727 else if (op
== TOK_LT
)
1729 else if (op
== TOK_GT
)
1731 else if (op
== TOK_LE
)
1733 else if (op
== TOK_GE
)
1741 /* special case for shifts and long long: we keep the shift as
1743 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1750 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1751 /* relationnal op: the result is an int */
1752 vtop
->type
.t
= VT_INT
;
1759 #ifndef TCC_TARGET_ARM
1760 /* generic itof for unsigned long long case */
1761 static void gen_cvt_itof1(int t
)
1763 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1764 (VT_LLONG
| VT_UNSIGNED
)) {
1767 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1768 #if LDOUBLE_SIZE != 8
1769 else if (t
== VT_LDOUBLE
)
1770 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1773 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1777 vtop
->r
= reg_fret(t
);
1784 /* generic ftoi for unsigned long long case */
1785 static void gen_cvt_ftoi1(int t
)
1789 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1790 /* not handled natively */
1791 st
= vtop
->type
.t
& VT_BTYPE
;
1793 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1794 #if LDOUBLE_SIZE != 8
1795 else if (st
== VT_LDOUBLE
)
1796 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1799 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1804 vtop
->r2
= REG_LRET
;
1810 /* force char or short cast */
1811 static void force_charshort_cast(int t
)
1815 /* XXX: add optimization if lvalue : just change type and offset */
1820 if (t
& VT_UNSIGNED
) {
1821 vpushi((1 << bits
) - 1);
1827 /* result must be signed or the SAR is converted to an SHL
1828 This was not the case when "t" was a signed short
1829 and the last value on the stack was an unsigned int */
1830 vtop
->type
.t
&= ~VT_UNSIGNED
;
1836 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1837 static void gen_cast(CType
*type
)
1839 int sbt
, dbt
, sf
, df
, c
, p
;
1841 /* special delayed cast for char/short */
1842 /* XXX: in some cases (multiple cascaded casts), it may still
1844 if (vtop
->r
& VT_MUSTCAST
) {
1845 vtop
->r
&= ~VT_MUSTCAST
;
1846 force_charshort_cast(vtop
->type
.t
);
1849 /* bitfields first get cast to ints */
1850 if (vtop
->type
.t
& VT_BITFIELD
) {
1854 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1855 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1860 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1861 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1863 /* constant case: we can do it now */
1864 /* XXX: in ISOC, cannot do it if error in convert */
1865 if (sbt
== VT_FLOAT
)
1866 vtop
->c
.ld
= vtop
->c
.f
;
1867 else if (sbt
== VT_DOUBLE
)
1868 vtop
->c
.ld
= vtop
->c
.d
;
1871 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1872 if (sbt
& VT_UNSIGNED
)
1873 vtop
->c
.ld
= vtop
->c
.ull
;
1875 vtop
->c
.ld
= vtop
->c
.ll
;
1877 if (sbt
& VT_UNSIGNED
)
1878 vtop
->c
.ld
= vtop
->c
.ui
;
1880 vtop
->c
.ld
= vtop
->c
.i
;
1883 if (dbt
== VT_FLOAT
)
1884 vtop
->c
.f
= (float)vtop
->c
.ld
;
1885 else if (dbt
== VT_DOUBLE
)
1886 vtop
->c
.d
= (double)vtop
->c
.ld
;
1887 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1888 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1889 } else if (sf
&& dbt
== VT_BOOL
) {
1890 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1893 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1894 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1895 vtop
->c
.ll
= vtop
->c
.ull
;
1896 else if (sbt
& VT_UNSIGNED
)
1897 vtop
->c
.ll
= vtop
->c
.ui
;
1898 #ifdef TCC_TARGET_X86_64
1899 else if (sbt
== VT_PTR
)
1902 else if (sbt
!= VT_LLONG
)
1903 vtop
->c
.ll
= vtop
->c
.i
;
1905 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1906 vtop
->c
.ull
= vtop
->c
.ll
;
1907 else if (dbt
== VT_BOOL
)
1908 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1909 else if (dbt
!= VT_LLONG
) {
1911 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1913 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1916 if(dbt
& VT_UNSIGNED
)
1917 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1919 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1922 } else if (p
&& dbt
== VT_BOOL
) {
1925 } else if (!nocode_wanted
) {
1926 /* non constant case: generate code */
1928 /* convert from fp to fp */
1931 /* convert int to fp */
1934 /* convert fp to int */
1935 if (dbt
== VT_BOOL
) {
1939 /* we handle char/short/etc... with generic code */
1940 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1941 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1945 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1946 /* additional cast for char/short... */
1951 #ifndef TCC_TARGET_X86_64
1952 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1953 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1954 /* scalar to long long */
1955 /* machine independent conversion */
1957 /* generate high word */
1958 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1962 if (sbt
== VT_PTR
) {
1963 /* cast from pointer to int before we apply
1964 shift operation, which pointers don't support*/
1965 gen_cast(&int_type
);
1971 /* patch second register */
1972 vtop
[-1].r2
= vtop
->r
;
1976 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1977 (dbt
& VT_BTYPE
) == VT_PTR
||
1978 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1979 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1980 (sbt
& VT_BTYPE
) != VT_PTR
&&
1981 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1982 /* need to convert from 32bit to 64bit */
1984 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1985 /* x86_64 specific: movslq */
1987 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1991 } else if (dbt
== VT_BOOL
) {
1992 /* scalar to bool */
1995 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1996 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1997 if (sbt
== VT_PTR
) {
1998 vtop
->type
.t
= VT_INT
;
1999 tcc_warning("nonportable conversion from pointer to char/short");
2001 force_charshort_cast(dbt
);
2002 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2004 if (sbt
== VT_LLONG
) {
2005 /* from long long: just take low order word */
2009 /* if lvalue and single word type, nothing to do because
2010 the lvalue already contains the real type size (see
2011 VT_LVAL_xxx constants) */
2014 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2015 /* if we are casting between pointer types,
2016 we must update the VT_LVAL_xxx size */
2017 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2018 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2023 /* return type size as known at compile time. Put alignment at 'a' */
2024 ST_FUNC
int type_size(CType
*type
, int *a
)
2029 bt
= type
->t
& VT_BTYPE
;
2030 if (bt
== VT_STRUCT
) {
2035 } else if (bt
== VT_PTR
) {
2036 if (type
->t
& VT_ARRAY
) {
2040 ts
= type_size(&s
->type
, a
);
2042 if (ts
< 0 && s
->c
< 0)
2050 } else if (bt
== VT_LDOUBLE
) {
2052 return LDOUBLE_SIZE
;
2053 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2054 #ifdef TCC_TARGET_I386
2055 #ifdef TCC_TARGET_PE
2060 #elif defined(TCC_TARGET_ARM)
2070 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2073 } else if (bt
== VT_SHORT
) {
2077 /* char, void, function, _Bool */
2083 /* push type size as known at runtime time on top of value stack. Put
2085 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2087 if (type
->t
& VT_VLA
) {
2088 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2090 vpushi(type_size(type
, a
));
2094 /* return the pointed type of t */
2095 static inline CType
*pointed_type(CType
*type
)
2097 return &type
->ref
->type
;
2100 /* modify type so that its it is a pointer to type. */
2101 ST_FUNC
void mk_pointer(CType
*type
)
2104 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2105 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2109 /* compare function types. OLD functions match any new functions */
2110 static int is_compatible_func(CType
*type1
, CType
*type2
)
2116 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2118 /* check func_call */
2119 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2121 /* XXX: not complete */
2122 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2126 while (s1
!= NULL
) {
2129 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2139 /* return true if type1 and type2 are the same. If unqualified is
2140 true, qualifiers on the types are ignored.
2142 - enums are not checked as gcc __builtin_types_compatible_p ()
2144 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2148 t1
= type1
->t
& VT_TYPE
;
2149 t2
= type2
->t
& VT_TYPE
;
2151 /* strip qualifiers before comparing */
2152 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2153 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2155 /* XXX: bitfields ? */
2158 /* test more complicated cases */
2159 bt1
= t1
& VT_BTYPE
;
2160 if (bt1
== VT_PTR
) {
2161 type1
= pointed_type(type1
);
2162 type2
= pointed_type(type2
);
2163 return is_compatible_types(type1
, type2
);
2164 } else if (bt1
== VT_STRUCT
) {
2165 return (type1
->ref
== type2
->ref
);
2166 } else if (bt1
== VT_FUNC
) {
2167 return is_compatible_func(type1
, type2
);
2173 /* return true if type1 and type2 are exactly the same (including
2176 static int is_compatible_types(CType
*type1
, CType
*type2
)
2178 return compare_types(type1
,type2
,0);
2181 /* return true if type1 and type2 are the same (ignoring qualifiers).
2183 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2185 return compare_types(type1
,type2
,1);
2188 /* print a type. If 'varstr' is not NULL, then the variable is also
2189 printed in the type */
2191 /* XXX: add array and function pointers */
2192 static void type_to_str(char *buf
, int buf_size
,
2193 CType
*type
, const char *varstr
)
2200 t
= type
->t
& VT_TYPE
;
2203 if (t
& VT_CONSTANT
)
2204 pstrcat(buf
, buf_size
, "const ");
2205 if (t
& VT_VOLATILE
)
2206 pstrcat(buf
, buf_size
, "volatile ");
2207 if (t
& VT_UNSIGNED
)
2208 pstrcat(buf
, buf_size
, "unsigned ");
2238 tstr
= "long double";
2240 pstrcat(buf
, buf_size
, tstr
);
2244 if (bt
== VT_STRUCT
)
2248 pstrcat(buf
, buf_size
, tstr
);
2249 v
= type
->ref
->v
& ~SYM_STRUCT
;
2250 if (v
>= SYM_FIRST_ANOM
)
2251 pstrcat(buf
, buf_size
, "<anonymous>");
2253 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2257 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2258 pstrcat(buf
, buf_size
, "(");
2260 while (sa
!= NULL
) {
2261 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2262 pstrcat(buf
, buf_size
, buf1
);
2265 pstrcat(buf
, buf_size
, ", ");
2267 pstrcat(buf
, buf_size
, ")");
2271 pstrcpy(buf1
, sizeof(buf1
), "*");
2273 pstrcat(buf1
, sizeof(buf1
), varstr
);
2274 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2278 pstrcat(buf
, buf_size
, " ");
2279 pstrcat(buf
, buf_size
, varstr
);
2284 /* verify type compatibility to store vtop in 'dt' type, and generate
2286 static void gen_assign_cast(CType
*dt
)
2288 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2289 char buf1
[256], buf2
[256];
2292 st
= &vtop
->type
; /* source type */
2293 dbt
= dt
->t
& VT_BTYPE
;
2294 sbt
= st
->t
& VT_BTYPE
;
2296 tcc_error("Cannot assign void value");
2297 if (dt
->t
& VT_CONSTANT
)
2298 tcc_warning("assignment of read-only location");
2301 /* special cases for pointers */
2302 /* '0' can also be a pointer */
2303 if (is_null_pointer(vtop
))
2305 /* accept implicit pointer to integer cast with warning */
2306 if (is_integer_btype(sbt
)) {
2307 tcc_warning("assignment makes pointer from integer without a cast");
2310 type1
= pointed_type(dt
);
2311 /* a function is implicitely a function pointer */
2312 if (sbt
== VT_FUNC
) {
2313 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2314 !is_compatible_types(pointed_type(dt
), st
))
2315 tcc_warning("assignment from incompatible pointer type");
2320 type2
= pointed_type(st
);
2321 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2322 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2323 /* void * can match anything */
2325 /* exact type match, except for unsigned */
2328 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2329 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2330 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2331 tcc_warning("assignment from incompatible pointer type");
2333 /* check const and volatile */
2334 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2335 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2336 tcc_warning("assignment discards qualifiers from pointer target type");
2342 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2343 tcc_warning("assignment makes integer from pointer without a cast");
2345 /* XXX: more tests */
2350 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2351 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2352 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2354 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2355 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2356 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2364 /* store vtop in lvalue pushed on stack */
2365 ST_FUNC
void vstore(void)
2367 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2369 ft
= vtop
[-1].type
.t
;
2370 sbt
= vtop
->type
.t
& VT_BTYPE
;
2371 dbt
= ft
& VT_BTYPE
;
2372 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2373 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2374 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2375 /* optimize char/short casts */
2376 delayed_cast
= VT_MUSTCAST
;
2377 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2378 /* XXX: factorize */
2379 if (ft
& VT_CONSTANT
)
2380 tcc_warning("assignment of read-only location");
2383 if (!(ft
& VT_BITFIELD
))
2384 gen_assign_cast(&vtop
[-1].type
);
2387 if (sbt
== VT_STRUCT
) {
2388 /* if structure, only generate pointer */
2389 /* structure assignment : generate memcpy */
2390 /* XXX: optimize if small size */
2391 if (!nocode_wanted
) {
2392 size
= type_size(&vtop
->type
, &align
);
2396 vtop
->type
.t
= VT_PTR
;
2399 /* address of memcpy() */
2402 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2403 else if(!(align
& 3))
2404 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2407 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2412 vtop
->type
.t
= VT_PTR
;
2421 /* leave source on stack */
2422 } else if (ft
& VT_BITFIELD
) {
2423 /* bitfield store handling */
2424 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2425 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2426 /* remove bit field info to avoid loops */
2427 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2429 /* duplicate source into other register */
2434 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2435 gen_cast(&vtop
[-1].type
);
2436 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2439 /* duplicate destination */
2441 vtop
[-1] = vtop
[-2];
2443 /* mask and shift source */
2444 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2445 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2446 vpushll((1ULL << bit_size
) - 1ULL);
2448 vpushi((1 << bit_size
) - 1);
2454 /* load destination, mask and or with source */
2456 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2457 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2459 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2466 /* pop off shifted source from "duplicate source..." above */
2470 #ifdef CONFIG_TCC_BCHECK
2471 /* bound check case */
2472 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2478 if (!nocode_wanted
) {
2482 #ifdef TCC_TARGET_X86_64
2483 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2488 r
= gv(rc
); /* generate value */
2489 /* if lvalue was saved on stack, must read it */
2490 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2492 t
= get_reg(RC_INT
);
2493 #ifdef TCC_TARGET_X86_64
2498 sv
.r
= VT_LOCAL
| VT_LVAL
;
2499 sv
.c
.ul
= vtop
[-1].c
.ul
;
2501 vtop
[-1].r
= t
| VT_LVAL
;
2504 #ifndef TCC_TARGET_X86_64
2505 /* two word case handling : store second register at word + 4 */
2506 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2508 /* convert to int to increment easily */
2509 vtop
->type
.t
= VT_INT
;
2515 /* XXX: it works because r2 is spilled last ! */
2516 store(vtop
->r2
, vtop
- 1);
2521 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2522 vtop
->r
|= delayed_cast
;
2526 /* post defines POST/PRE add. c is the token ++ or -- */
2527 ST_FUNC
void inc(int post
, int c
)
2530 vdup(); /* save lvalue */
2532 gv_dup(); /* duplicate value */
2537 vpushi(c
- TOK_MID
);
2539 vstore(); /* store value */
2541 vpop(); /* if post op, return saved value */
2544 /* Parse GNUC __attribute__ extension. Currently, the following
2545 extensions are recognized:
2546 - aligned(n) : set data/function alignment.
2547 - packed : force data alignment to 1
2548 - section(x) : generate data/code in this section.
2549 - unused : currently ignored, but may be used someday.
2550 - regparm(n) : pass function parameters in registers (i386 only)
2552 static void parse_attribute(AttributeDef
*ad
)
2556 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2560 while (tok
!= ')') {
2561 if (tok
< TOK_IDENT
)
2562 expect("attribute name");
2570 expect("section name");
2571 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2579 expect("alias(\"target\")");
2580 ad
->alias_target
= /* save string as token, for later */
2581 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2590 if (n
<= 0 || (n
& (n
- 1)) != 0)
2591 tcc_error("alignment must be a positive power of two");
2608 /* currently, no need to handle it because tcc does not
2609 track unused objects */
2613 /* currently, no need to handle it because tcc does not
2614 track unused objects */
2619 ad
->func_call
= FUNC_CDECL
;
2624 ad
->func_call
= FUNC_STDCALL
;
2626 #ifdef TCC_TARGET_I386
2636 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2642 ad
->func_call
= FUNC_FASTCALLW
;
2649 ad
->mode
= VT_LLONG
+ 1;
2652 ad
->mode
= VT_SHORT
+ 1;
2655 ad
->mode
= VT_INT
+ 1;
2658 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2665 ad
->func_export
= 1;
2668 ad
->func_import
= 1;
2671 if (tcc_state
->warn_unsupported
)
2672 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2673 /* skip parameters */
2675 int parenthesis
= 0;
2679 else if (tok
== ')')
2682 } while (parenthesis
&& tok
!= -1);
2695 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2696 static void struct_decl(CType
*type
, int u
)
2698 int a
, v
, size
, align
, maxalign
, c
, offset
;
2699 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2700 Sym
*s
, *ss
, *ass
, **ps
;
2704 a
= tok
; /* save decl type */
2709 /* struct already defined ? return it */
2711 expect("struct/union/enum name");
2715 tcc_error("invalid type");
2723 /* we put an undefined size for struct/union */
2724 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2725 s
->r
= 0; /* default alignment is zero as gcc */
2726 /* put struct/union/enum name in type */
2734 tcc_error("struct/union/enum already defined");
2735 /* cannot be empty */
2737 /* non empty enums are not allowed */
2738 if (a
== TOK_ENUM
) {
2742 expect("identifier");
2748 /* enum symbols have static storage */
2749 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2750 ss
->type
.t
|= VT_STATIC
;
2755 /* NOTE: we accept a trailing comma */
2766 while (tok
!= '}') {
2767 parse_btype(&btype
, &ad
);
2773 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2774 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2775 expect("identifier");
2776 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2777 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2778 tcc_error("invalid type for '%s'",
2779 get_tok_str(v
, NULL
));
2783 bit_size
= expr_const();
2784 /* XXX: handle v = 0 case for messages */
2786 tcc_error("negative width in bit-field '%s'",
2787 get_tok_str(v
, NULL
));
2788 if (v
&& bit_size
== 0)
2789 tcc_error("zero width for bit-field '%s'",
2790 get_tok_str(v
, NULL
));
2792 size
= type_size(&type1
, &align
);
2794 if (align
< ad
.aligned
)
2796 } else if (ad
.packed
) {
2798 } else if (*tcc_state
->pack_stack_ptr
) {
2799 if (align
> *tcc_state
->pack_stack_ptr
)
2800 align
= *tcc_state
->pack_stack_ptr
;
2803 if (bit_size
>= 0) {
2804 bt
= type1
.t
& VT_BTYPE
;
2811 tcc_error("bitfields must have scalar type");
2813 if (bit_size
> bsize
) {
2814 tcc_error("width of '%s' exceeds its type",
2815 get_tok_str(v
, NULL
));
2816 } else if (bit_size
== bsize
) {
2817 /* no need for bit fields */
2819 } else if (bit_size
== 0) {
2820 /* XXX: what to do if only padding in a
2822 /* zero size: means to pad */
2825 /* we do not have enough room ?
2826 did the type change?
2828 if ((bit_pos
+ bit_size
) > bsize
||
2829 bt
!= prevbt
|| a
== TOK_UNION
)
2832 /* XXX: handle LSB first */
2833 type1
.t
|= VT_BITFIELD
|
2834 (bit_pos
<< VT_STRUCT_SHIFT
) |
2835 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2836 bit_pos
+= bit_size
;
2842 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2843 /* add new memory data only if starting
2845 if (lbit_pos
== 0) {
2846 if (a
== TOK_STRUCT
) {
2847 c
= (c
+ align
- 1) & -align
;
2856 if (align
> maxalign
)
2860 printf("add field %s offset=%d",
2861 get_tok_str(v
, NULL
), offset
);
2862 if (type1
.t
& VT_BITFIELD
) {
2863 printf(" pos=%d size=%d",
2864 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2865 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2870 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2872 while ((ass
= ass
->next
) != NULL
) {
2873 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2878 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2882 if (tok
== ';' || tok
== TOK_EOF
)
2889 /* store size and alignment */
2890 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2896 /* return 0 if no type declaration. otherwise, return the basic type
2899 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2901 int t
, u
, type_found
, typespec_found
, typedef_found
;
2905 memset(ad
, 0, sizeof(AttributeDef
));
2913 /* currently, we really ignore extension */
2923 if ((t
& VT_BTYPE
) != 0)
2924 tcc_error("too many basic types");
2940 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2941 #ifndef TCC_TARGET_PE
2942 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2944 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2945 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2959 if ((t
& VT_BTYPE
) == VT_LONG
) {
2960 #ifdef TCC_TARGET_PE
2961 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2963 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2971 struct_decl(&type1
, VT_ENUM
);
2974 type
->ref
= type1
.ref
;
2978 struct_decl(&type1
, VT_STRUCT
);
2981 /* type modifiers */
3034 /* GNUC attribute */
3035 case TOK_ATTRIBUTE1
:
3036 case TOK_ATTRIBUTE2
:
3037 parse_attribute(ad
);
3040 t
= (t
& ~VT_BTYPE
) | u
;
3048 parse_expr_type(&type1
);
3049 /* remove all storage modifiers except typedef */
3050 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3053 if (typespec_found
|| typedef_found
)
3056 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3059 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3060 type
->ref
= s
->type
.ref
;
3062 /* get attributes from typedef */
3063 if (0 == ad
->aligned
)
3064 ad
->aligned
= FUNC_ALIGN(s
->r
);
3065 if (0 == ad
->func_call
)
3066 ad
->func_call
= FUNC_CALL(s
->r
);
3067 ad
->packed
|= FUNC_PACKED(s
->r
);
3076 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3077 tcc_error("signed and unsigned modifier");
3078 if (tcc_state
->char_is_unsigned
) {
3079 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3084 /* long is never used as type */
3085 if ((t
& VT_BTYPE
) == VT_LONG
)
3086 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3087 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3089 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3095 /* convert a function parameter type (array to pointer and function to
3096 function pointer) */
3097 static inline void convert_parameter_type(CType
*pt
)
3099 /* remove const and volatile qualifiers (XXX: const could be used
3100 to indicate a const function parameter */
3101 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3102 /* array must be transformed to pointer according to ANSI C */
3104 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3109 ST_FUNC
void parse_asm_str(CString
*astr
)
3112 /* read the string */
3114 expect("string constant");
3116 while (tok
== TOK_STR
) {
3117 /* XXX: add \0 handling too ? */
3118 cstr_cat(astr
, tokc
.cstr
->data
);
3121 cstr_ccat(astr
, '\0');
3124 /* Parse an asm label and return the label
3125 * Don't forget to free the CString in the caller! */
3126 static void asm_label_instr(CString
*astr
)
3129 parse_asm_str(astr
);
3132 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3136 static void post_type(CType
*type
, AttributeDef
*ad
)
3138 int n
, l
, t1
, arg_size
, align
;
3139 Sym
**plast
, *s
, *first
;
3144 /* function declaration */
3152 /* read param name and compute offset */
3153 if (l
!= FUNC_OLD
) {
3154 if (!parse_btype(&pt
, &ad1
)) {
3156 tcc_error("invalid type");
3163 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3165 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3166 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3167 tcc_error("parameter declared as void");
3168 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3173 expect("identifier");
3177 convert_parameter_type(&pt
);
3178 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3184 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3191 /* if no parameters, then old type prototype */
3195 /* NOTE: const is ignored in returned type as it has a special
3196 meaning in gcc / C++ */
3197 type
->t
&= ~VT_CONSTANT
;
3198 /* some ancient pre-K&R C allows a function to return an array
3199 and the array brackets to be put after the arguments, such
3200 that "int c()[]" means something like "int[] c()" */
3203 skip(']'); /* only handle simple "[]" */
3206 /* we push a anonymous symbol which will contain the function prototype */
3207 ad
->func_args
= arg_size
;
3208 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3212 } else if (tok
== '[') {
3213 /* array definition */
3215 if (tok
== TOK_RESTRICT1
)
3220 if (!local_stack
|| nocode_wanted
)
3221 vpushi(expr_const());
3223 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3226 tcc_error("invalid array size");
3228 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3229 tcc_error("size of variable length array should be an integer");
3234 /* parse next post type */
3235 post_type(type
, ad
);
3236 t1
|= type
->t
& VT_VLA
;
3239 loc
-= type_size(&int_type
, &align
);
3243 vla_runtime_type_size(type
, &align
);
3245 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3252 /* we push an anonymous symbol which will contain the array
3254 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3255 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3260 /* Parse a type declaration (except basic type), and return the type
3261 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3262 expected. 'type' should contain the basic type. 'ad' is the
3263 attribute definition of the basic type. It can be modified by
3266 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3269 CType type1
, *type2
;
3270 int qualifiers
, storage
;
3272 while (tok
== '*') {
3280 qualifiers
|= VT_CONSTANT
;
3285 qualifiers
|= VT_VOLATILE
;
3293 type
->t
|= qualifiers
;
3296 /* XXX: clarify attribute handling */
3297 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3298 parse_attribute(ad
);
3300 /* recursive type */
3301 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3302 type1
.t
= 0; /* XXX: same as int */
3305 /* XXX: this is not correct to modify 'ad' at this point, but
3306 the syntax is not clear */
3307 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3308 parse_attribute(ad
);
3309 type_decl(&type1
, ad
, v
, td
);
3312 /* type identifier */
3313 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3317 if (!(td
& TYPE_ABSTRACT
))
3318 expect("identifier");
3322 storage
= type
->t
& VT_STORAGE
;
3323 type
->t
&= ~VT_STORAGE
;
3324 if (storage
& VT_STATIC
) {
3325 int saved_nocode_wanted
= nocode_wanted
;
3327 post_type(type
, ad
);
3328 nocode_wanted
= saved_nocode_wanted
;
3330 post_type(type
, ad
);
3332 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3333 parse_attribute(ad
);
3337 /* append type at the end of type1 */
3350 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3351 ST_FUNC
int lvalue_type(int t
)
3356 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3358 else if (bt
== VT_SHORT
)
3362 if (t
& VT_UNSIGNED
)
3363 r
|= VT_LVAL_UNSIGNED
;
3367 /* indirection with full error checking and bound check */
3368 ST_FUNC
void indir(void)
3370 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3371 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3375 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3377 vtop
->type
= *pointed_type(&vtop
->type
);
3378 /* Arrays and functions are never lvalues */
3379 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3380 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3381 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3382 /* if bound checking, the referenced pointer must be checked */
3383 #ifdef CONFIG_TCC_BCHECK
3384 if (tcc_state
->do_bounds_check
)
3385 vtop
->r
|= VT_MUSTBOUND
;
3390 /* pass a parameter to a function and do type checking and casting */
3391 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3396 func_type
= func
->c
;
3397 if (func_type
== FUNC_OLD
||
3398 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3399 /* default casting : only need to convert float to double */
3400 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3405 } else if (arg
== NULL
) {
3406 tcc_error("too many arguments to function");
3409 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3410 gen_assign_cast(&type
);
3414 /* parse an expression of the form '(type)' or '(expr)' and return its
3416 static void parse_expr_type(CType
*type
)
3422 if (parse_btype(type
, &ad
)) {
3423 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3430 static void parse_type(CType
*type
)
3435 if (!parse_btype(type
, &ad
)) {
3438 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3441 static void vpush_tokc(int t
)
3446 vsetc(&type
, VT_CONST
, &tokc
);
3449 ST_FUNC
void unary(void)
3451 int n
, t
, align
, size
, r
, sizeof_caller
;
3455 static int in_sizeof
= 0;
3457 sizeof_caller
= in_sizeof
;
3459 /* XXX: GCC 2.95.3 does not generate a table although it should be
3473 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3477 vpush_tokc(VT_LLONG
);
3481 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3485 vpush_tokc(VT_FLOAT
);
3489 vpush_tokc(VT_DOUBLE
);
3493 vpush_tokc(VT_LDOUBLE
);
3496 case TOK___FUNCTION__
:
3498 goto tok_identifier
;
3504 /* special function name identifier */
3505 len
= strlen(funcname
) + 1;
3506 /* generate char[len] type */
3511 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3512 ptr
= section_ptr_add(data_section
, len
);
3513 memcpy(ptr
, funcname
, len
);
3518 #ifdef TCC_TARGET_PE
3519 t
= VT_SHORT
| VT_UNSIGNED
;
3525 /* string parsing */
3528 if (tcc_state
->warn_write_strings
)
3533 memset(&ad
, 0, sizeof(AttributeDef
));
3534 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3539 if (parse_btype(&type
, &ad
)) {
3540 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3542 /* check ISOC99 compound literal */
3544 /* data is allocated locally by default */
3549 /* all except arrays are lvalues */
3550 if (!(type
.t
& VT_ARRAY
))
3551 r
|= lvalue_type(type
.t
);
3552 memset(&ad
, 0, sizeof(AttributeDef
));
3553 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3555 if (sizeof_caller
) {
3562 } else if (tok
== '{') {
3563 /* save all registers */
3565 /* statement expression : we do not accept break/continue
3566 inside as GCC does */
3567 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3582 /* functions names must be treated as function pointers,
3583 except for unary '&' and sizeof. Since we consider that
3584 functions are not lvalues, we only have to handle it
3585 there and in function calls. */
3586 /* arrays can also be used although they are not lvalues */
3587 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3588 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3590 mk_pointer(&vtop
->type
);
3596 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3598 boolean
.t
= VT_BOOL
;
3601 vtop
->c
.i
= !vtop
->c
.i
;
3602 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3603 vtop
->c
.i
= vtop
->c
.i
^ 1;
3606 vseti(VT_JMP
, gtst(1, 0));
3617 /* in order to force cast, we add zero */
3619 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3620 tcc_error("pointer not accepted for unary plus");
3630 unary_type(&type
); // Perform a in_sizeof = 0;
3631 size
= type_size(&type
, &align
);
3632 if (t
== TOK_SIZEOF
) {
3633 if (!(type
.t
& VT_VLA
)) {
3635 tcc_error("sizeof applied to an incomplete type");
3638 vla_runtime_type_size(&type
, &align
);
3643 vtop
->type
.t
|= VT_UNSIGNED
;
3646 case TOK_builtin_types_compatible_p
:
3655 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3656 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3657 vpushi(is_compatible_types(&type1
, &type2
));
3660 case TOK_builtin_constant_p
:
3662 int saved_nocode_wanted
, res
;
3665 saved_nocode_wanted
= nocode_wanted
;
3668 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3670 nocode_wanted
= saved_nocode_wanted
;
3675 case TOK_builtin_frame_address
:
3681 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3682 tcc_error("__builtin_frame_address only takes positive integers");
3689 vset(&type
, VT_LOCAL
, 0); /* local frame */
3691 mk_pointer(&vtop
->type
);
3692 indir(); /* -> parent frame */
3696 #ifdef TCC_TARGET_X86_64
3697 case TOK_builtin_va_arg_types
:
3699 /* This definition must be synced with stdarg.h */
3700 enum __va_arg_type
{
3701 __va_gen_reg
, __va_float_reg
, __va_stack
3709 bt
= type
.t
& VT_BTYPE
;
3710 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3712 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3713 vpushi(__va_float_reg
);
3715 vpushi(__va_gen_reg
);
3735 goto tok_identifier
;
3737 /* allow to take the address of a label */
3738 if (tok
< TOK_UIDENT
)
3739 expect("label identifier");
3740 s
= label_find(tok
);
3742 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3744 if (s
->r
== LABEL_DECLARED
)
3745 s
->r
= LABEL_FORWARD
;
3748 s
->type
.t
= VT_VOID
;
3749 mk_pointer(&s
->type
);
3750 s
->type
.t
|= VT_STATIC
;
3752 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3757 // special qnan , snan and infinity values
3759 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3763 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3767 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3776 expect("identifier");
3780 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3781 /* for simple function calls, we tolerate undeclared
3782 external reference to int() function */
3783 if (tcc_state
->warn_implicit_function_declaration
)
3784 tcc_warning("implicit declaration of function '%s'",
3785 get_tok_str(t
, NULL
));
3786 s
= external_global_sym(t
, &func_old_type
, 0);
3788 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3789 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3790 /* if referencing an inline function, then we generate a
3791 symbol to it if not already done. It will have the
3792 effect to generate code for it at the end of the
3793 compilation unit. Inline function as always
3794 generated in the text section. */
3796 put_extern_sym(s
, text_section
, 0, 0);
3797 r
= VT_SYM
| VT_CONST
;
3801 vset(&s
->type
, r
, s
->c
);
3802 /* if forward reference, we must point to s */
3803 if (vtop
->r
& VT_SYM
) {
3810 /* post operations */
3812 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3815 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3818 if (tok
== TOK_ARROW
)
3820 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3824 /* expect pointer on structure */
3825 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3826 expect("struct or union");
3830 while ((s
= s
->next
) != NULL
) {
3835 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3836 /* add field offset to pointer */
3837 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3840 /* change type to field type, and set to lvalue */
3841 vtop
->type
= s
->type
;
3842 vtop
->type
.t
|= qualifiers
;
3843 /* an array is never an lvalue */
3844 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3845 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3846 #ifdef CONFIG_TCC_BCHECK
3847 /* if bound checking, the referenced pointer must be checked */
3848 if (tcc_state
->do_bounds_check
)
3849 vtop
->r
|= VT_MUSTBOUND
;
3853 } else if (tok
== '[') {
3859 } else if (tok
== '(') {
3865 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3866 /* pointer test (no array accepted) */
3867 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3868 vtop
->type
= *pointed_type(&vtop
->type
);
3869 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3873 expect("function pointer");
3876 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3878 /* get return type */
3881 sa
= s
->next
; /* first parameter */
3884 /* compute first implicit argument if a structure is returned */
3885 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3886 /* get some space for the returned structure */
3887 size
= type_size(&s
->type
, &align
);
3888 loc
= (loc
- size
) & -align
;
3890 ret
.r
= VT_LOCAL
| VT_LVAL
;
3891 /* pass it as 'int' to avoid structure arg passing
3893 vseti(VT_LOCAL
, loc
);
3898 /* return in register */
3899 if (is_float(ret
.type
.t
)) {
3900 ret
.r
= reg_fret(ret
.type
.t
);
3902 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3911 gfunc_param_typed(s
, sa
);
3921 tcc_error("too few arguments to function");
3923 if (!nocode_wanted
) {
3924 gfunc_call(nb_args
);
3926 vtop
-= (nb_args
+ 1);
3929 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3937 ST_FUNC
void expr_prod(void)
3942 while (tok
== '*' || tok
== '/' || tok
== '%') {
3950 ST_FUNC
void expr_sum(void)
3955 while (tok
== '+' || tok
== '-') {
3963 static void expr_shift(void)
3968 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3976 static void expr_cmp(void)
3981 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3982 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3990 static void expr_cmpeq(void)
3995 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4003 static void expr_and(void)
4006 while (tok
== '&') {
4013 static void expr_xor(void)
4016 while (tok
== '^') {
4023 static void expr_or(void)
4026 while (tok
== '|') {
4033 /* XXX: fix this mess */
4034 static void expr_land_const(void)
4037 while (tok
== TOK_LAND
) {
4044 /* XXX: fix this mess */
4045 static void expr_lor_const(void)
4048 while (tok
== TOK_LOR
) {
4055 /* only used if non constant */
4056 static void expr_land(void)
4061 if (tok
== TOK_LAND
) {
4066 if (tok
!= TOK_LAND
) {
4076 static void expr_lor(void)
4081 if (tok
== TOK_LOR
) {
4086 if (tok
!= TOK_LOR
) {
4096 /* XXX: better constant handling */
4097 static void expr_cond(void)
4099 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4101 CType type
, type1
, type2
;
4108 boolean
.t
= VT_BOOL
;
4115 if (tok
!= ':' || !gnu_ext
) {
4130 if (vtop
!= vstack
) {
4131 /* needed to avoid having different registers saved in
4133 if (is_float(vtop
->type
.t
)) {
4135 #ifdef TCC_TARGET_X86_64
4136 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4146 if (tok
== ':' && gnu_ext
) {
4154 sv
= *vtop
; /* save value to handle it later */
4155 vtop
--; /* no vpop so that FP stack is not flushed */
4163 bt1
= t1
& VT_BTYPE
;
4165 bt2
= t2
& VT_BTYPE
;
4166 /* cast operands to correct type according to ISOC rules */
4167 if (is_float(bt1
) || is_float(bt2
)) {
4168 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4169 type
.t
= VT_LDOUBLE
;
4170 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4175 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4176 /* cast to biggest op */
4178 /* convert to unsigned if it does not fit in a long long */
4179 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4180 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4181 type
.t
|= VT_UNSIGNED
;
4182 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4183 /* If one is a null ptr constant the result type
4185 if (is_null_pointer (vtop
))
4187 else if (is_null_pointer (&sv
))
4189 /* XXX: test pointer compatibility, C99 has more elaborate
4193 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4194 /* XXX: test function pointer compatibility */
4195 type
= bt1
== VT_FUNC
? type1
: type2
;
4196 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4197 /* XXX: test structure compatibility */
4198 type
= bt1
== VT_STRUCT
? type1
: type2
;
4199 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4200 /* NOTE: as an extension, we accept void on only one side */
4203 /* integer operations */
4205 /* convert to unsigned if it does not fit in an integer */
4206 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4207 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4208 type
.t
|= VT_UNSIGNED
;
4211 /* now we convert second operand */
4213 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4216 if (is_float(type
.t
)) {
4218 #ifdef TCC_TARGET_X86_64
4219 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4223 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4224 /* for long longs, we use fixed registers to avoid having
4225 to handle a complicated move */
4230 /* this is horrible, but we must also convert first
4234 /* put again first value and cast it */
4237 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4247 static void expr_eq(void)
4253 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4254 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4255 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4270 ST_FUNC
void gexpr(void)
4281 /* parse an expression and return its type without any side effect. */
4282 static void expr_type(CType
*type
)
4284 int saved_nocode_wanted
;
4286 saved_nocode_wanted
= nocode_wanted
;
4291 nocode_wanted
= saved_nocode_wanted
;
4294 /* parse a unary expression and return its type without any side
4296 static void unary_type(CType
*type
)
4308 /* parse a constant expression and return value in vtop. */
4309 static void expr_const1(void)
4318 /* parse an integer constant and return its value. */
4319 ST_FUNC
int expr_const(void)
4323 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4324 expect("constant expression");
4330 /* return the label token if current token is a label, otherwise
4332 static int is_label(void)
4336 /* fast test first */
4337 if (tok
< TOK_UIDENT
)
4339 /* no need to save tokc because tok is an identifier */
4346 unget_tok(last_tok
);
4351 static void label_or_decl(int l
)
4355 /* fast test first */
4356 if (tok
>= TOK_UIDENT
)
4358 /* no need to save tokc because tok is an identifier */
4362 unget_tok(last_tok
);
4365 unget_tok(last_tok
);
4370 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4371 int case_reg
, int is_expr
)
4374 Sym
*s
, *frame_bottom
;
4376 /* generate line number info */
4377 if (tcc_state
->do_debug
&&
4378 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4379 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4381 last_line_num
= file
->line_num
;
4385 /* default return value is (void) */
4387 vtop
->type
.t
= VT_VOID
;
4390 if (tok
== TOK_IF
) {
4397 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4399 if (c
== TOK_ELSE
) {
4403 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4404 gsym(d
); /* patch else jmp */
4407 } else if (tok
== TOK_WHILE
) {
4415 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4419 } else if (tok
== '{') {
4423 /* record local declaration stack position */
4425 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4426 frame_bottom
->next
= scope_stack_bottom
;
4427 scope_stack_bottom
= frame_bottom
;
4428 llabel
= local_label_stack
;
4429 /* handle local labels declarations */
4430 if (tok
== TOK_LABEL
) {
4433 if (tok
< TOK_UIDENT
)
4434 expect("label identifier");
4435 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4445 while (tok
!= '}') {
4446 label_or_decl(VT_LOCAL
);
4450 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4453 /* pop locally defined labels */
4454 label_pop(&local_label_stack
, llabel
);
4456 /* XXX: this solution makes only valgrind happy...
4457 triggered by gcc.c-torture/execute/20000917-1.c */
4459 switch(vtop
->type
.t
& VT_BTYPE
) {
4464 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4466 tcc_error("unsupported expression type");
4469 /* pop locally defined symbols */
4470 scope_stack_bottom
= scope_stack_bottom
->next
;
4471 sym_pop(&local_stack
, s
);
4473 } else if (tok
== TOK_RETURN
) {
4477 gen_assign_cast(&func_vt
);
4478 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4480 /* if returning structure, must copy it to implicit
4481 first pointer arg location */
4484 size
= type_size(&func_vt
,&align
);
4487 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4491 loc
= (loc
- size
) & -4;
4494 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4497 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4499 vtop
->type
= int_type
;
4505 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4508 /* copy structure value to pointer */
4513 } else if (is_float(func_vt
.t
)) {
4514 gv(rc_fret(func_vt
.t
));
4518 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4521 rsym
= gjmp(rsym
); /* jmp */
4522 } else if (tok
== TOK_BREAK
) {
4525 tcc_error("cannot break");
4526 *bsym
= gjmp(*bsym
);
4529 } else if (tok
== TOK_CONTINUE
) {
4532 tcc_error("cannot continue");
4533 *csym
= gjmp(*csym
);
4536 } else if (tok
== TOK_FOR
) {
4541 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4542 frame_bottom
->next
= scope_stack_bottom
;
4543 scope_stack_bottom
= frame_bottom
;
4545 /* c99 for-loop init decl? */
4546 if (!decl0(VT_LOCAL
, 1)) {
4547 /* no, regular for-loop init expr */
4571 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4575 scope_stack_bottom
= scope_stack_bottom
->next
;
4576 sym_pop(&local_stack
, s
);
4578 if (tok
== TOK_DO
) {
4583 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4594 if (tok
== TOK_SWITCH
) {
4598 /* XXX: other types than integer */
4599 case_reg
= gv(RC_INT
);
4603 b
= gjmp(0); /* jump to first case */
4605 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4606 /* if no default, jmp after switch */
4614 if (tok
== TOK_CASE
) {
4621 if (gnu_ext
&& tok
== TOK_DOTS
) {
4625 tcc_warning("empty case range");
4627 /* since a case is like a label, we must skip it with a jmp */
4634 *case_sym
= gtst(1, 0);
4637 *case_sym
= gtst(1, 0);
4641 *case_sym
= gtst(1, *case_sym
);
4646 goto block_after_label
;
4648 if (tok
== TOK_DEFAULT
) {
4654 tcc_error("too many 'default'");
4657 goto block_after_label
;
4659 if (tok
== TOK_GOTO
) {
4661 if (tok
== '*' && gnu_ext
) {
4665 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4668 } else if (tok
>= TOK_UIDENT
) {
4669 s
= label_find(tok
);
4670 /* put forward definition if needed */
4672 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4674 if (s
->r
== LABEL_DECLARED
)
4675 s
->r
= LABEL_FORWARD
;
4677 /* label already defined */
4678 if (s
->r
& LABEL_FORWARD
)
4679 s
->jnext
= gjmp(s
->jnext
);
4681 gjmp_addr(s
->jnext
);
4684 expect("label identifier");
4687 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4695 if (s
->r
== LABEL_DEFINED
)
4696 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4698 s
->r
= LABEL_DEFINED
;
4700 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4703 /* we accept this, but it is a mistake */
4706 tcc_warning("deprecated use of label at end of compound statement");
4710 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4713 /* expression case */
4728 /* t is the array or struct type. c is the array or struct
4729 address. cur_index/cur_field is the pointer to the current
4730 value. 'size_only' is true if only size info is needed (only used
4732 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4733 int *cur_index
, Sym
**cur_field
,
4737 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4743 if (gnu_ext
&& (l
= is_label()) != 0)
4745 while (tok
== '[' || tok
== '.') {
4747 if (!(type
->t
& VT_ARRAY
))
4748 expect("array type");
4751 index
= expr_const();
4752 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4753 expect("invalid index");
4754 if (tok
== TOK_DOTS
&& gnu_ext
) {
4756 index_last
= expr_const();
4757 if (index_last
< 0 ||
4758 (s
->c
>= 0 && index_last
>= s
->c
) ||
4760 expect("invalid index");
4766 *cur_index
= index_last
;
4767 type
= pointed_type(type
);
4768 elem_size
= type_size(type
, &align
);
4769 c
+= index
* elem_size
;
4770 /* NOTE: we only support ranges for last designator */
4771 nb_elems
= index_last
- index
+ 1;
4772 if (nb_elems
!= 1) {
4781 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4782 expect("struct/union type");
4795 /* XXX: fix this mess by using explicit storage field */
4797 type1
.t
|= (type
->t
& ~VT_TYPE
);
4811 if (type
->t
& VT_ARRAY
) {
4813 type
= pointed_type(type
);
4814 c
+= index
* type_size(type
, &align
);
4818 tcc_error("too many field init");
4819 /* XXX: fix this mess by using explicit storage field */
4821 type1
.t
|= (type
->t
& ~VT_TYPE
);
4826 decl_initializer(type
, sec
, c
, 0, size_only
);
4828 /* XXX: make it more general */
4829 if (!size_only
&& nb_elems
> 1) {
4830 unsigned long c_end
;
4835 tcc_error("range init not supported yet for dynamic storage");
4836 c_end
= c
+ nb_elems
* elem_size
;
4837 if (c_end
> sec
->data_allocated
)
4838 section_realloc(sec
, c_end
);
4839 src
= sec
->data
+ c
;
4841 for(i
= 1; i
< nb_elems
; i
++) {
4843 memcpy(dst
, src
, elem_size
);
4849 #define EXPR_CONST 1
4852 /* store a value or an expression directly in global data or in local array */
4853 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4854 int v
, int expr_type
)
4856 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4858 unsigned long long bit_mask
;
4866 /* compound literals must be allocated globally in this case */
4867 saved_global_expr
= global_expr
;
4870 global_expr
= saved_global_expr
;
4871 /* NOTE: symbols are accepted */
4872 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4873 tcc_error("initializer element is not constant");
4881 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4884 /* XXX: not portable */
4885 /* XXX: generate error if incorrect relocation */
4886 gen_assign_cast(&dtype
);
4887 bt
= type
->t
& VT_BTYPE
;
4888 /* we'll write at most 12 bytes */
4889 if (c
+ 12 > sec
->data_allocated
) {
4890 section_realloc(sec
, c
+ 12);
4892 ptr
= sec
->data
+ c
;
4893 /* XXX: make code faster ? */
4894 if (!(type
->t
& VT_BITFIELD
)) {
4899 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4900 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4901 bit_mask
= (1LL << bit_size
) - 1;
4903 if ((vtop
->r
& VT_SYM
) &&
4909 (bt
== VT_INT
&& bit_size
!= 32)))
4910 tcc_error("initializer element is not computable at load time");
4913 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4915 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4918 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4921 *(double *)ptr
= vtop
->c
.d
;
4924 *(long double *)ptr
= vtop
->c
.ld
;
4927 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4930 if (vtop
->r
& VT_SYM
) {
4931 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4933 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4938 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4945 /* put zeros for variable based init */
4946 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4949 /* nothing to do because globals are already set to zero */
4951 vpush_global_sym(&func_old_type
, TOK_memset
);
4959 /* 't' contains the type and storage info. 'c' is the offset of the
4960 object in section 'sec'. If 'sec' is NULL, it means stack based
4961 allocation. 'first' is true if array '{' must be read (multi
4962 dimension implicit array init handling). 'size_only' is true if
4963 size only evaluation is wanted (only for arrays). */
4964 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4965 int first
, int size_only
)
4967 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4968 int size1
, align1
, expr_type
;
4972 if (type
->t
& VT_VLA
) {
4973 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4977 vpush_global_sym(&func_old_type
, TOK_alloca
);
4978 vla_runtime_type_size(type
, &a
);
4983 vsetc(type
, REG_IRET
, &retcval
);
4984 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
4989 tcc_error("variable length arrays unsupported for this target");
4991 } else if (type
->t
& VT_ARRAY
) {
4995 t1
= pointed_type(type
);
4996 size1
= type_size(t1
, &align1
);
4999 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5002 tcc_error("character array initializer must be a literal,"
5003 " optionally enclosed in braces");
5008 /* only parse strings here if correct type (otherwise: handle
5009 them as ((w)char *) expressions */
5010 if ((tok
== TOK_LSTR
&&
5011 #ifdef TCC_TARGET_PE
5012 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5014 (t1
->t
& VT_BTYPE
) == VT_INT
5016 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5017 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5022 /* compute maximum number of chars wanted */
5024 cstr_len
= cstr
->size
;
5026 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5029 if (n
>= 0 && nb
> (n
- array_length
))
5030 nb
= n
- array_length
;
5033 tcc_warning("initializer-string for array is too long");
5034 /* in order to go faster for common case (char
5035 string in global variable, we handle it
5037 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5038 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5042 ch
= ((unsigned char *)cstr
->data
)[i
];
5044 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5045 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5053 /* only add trailing zero if enough storage (no
5054 warning in this case since it is standard) */
5055 if (n
< 0 || array_length
< n
) {
5057 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5063 while (tok
!= '}') {
5064 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5065 if (n
>= 0 && index
>= n
)
5066 tcc_error("index too large");
5067 /* must put zero in holes (note that doing it that way
5068 ensures that it even works with designators) */
5069 if (!size_only
&& array_length
< index
) {
5070 init_putz(t1
, sec
, c
+ array_length
* size1
,
5071 (index
- array_length
) * size1
);
5074 if (index
> array_length
)
5075 array_length
= index
;
5076 /* special test for multi dimensional arrays (may not
5077 be strictly correct if designators are used at the
5079 if (index
>= n
&& no_oblock
)
5088 /* put zeros at the end */
5089 if (!size_only
&& n
>= 0 && array_length
< n
) {
5090 init_putz(t1
, sec
, c
+ array_length
* size1
,
5091 (n
- array_length
) * size1
);
5093 /* patch type size if needed */
5095 s
->c
= array_length
;
5096 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5097 (sec
|| !first
|| tok
== '{')) {
5100 /* NOTE: the previous test is a specific case for automatic
5101 struct/union init */
5102 /* XXX: union needs only one init */
5104 /* XXX: this test is incorrect for local initializers
5105 beginning with ( without {. It would be much more difficult
5106 to do it correctly (ideally, the expression parser should
5107 be used in all cases) */
5113 while (tok
== '(') {
5117 if (!parse_btype(&type1
, &ad1
))
5119 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5121 if (!is_assignable_types(type
, &type1
))
5122 tcc_error("invalid type for cast");
5127 if (first
|| tok
== '{') {
5136 while (tok
!= '}') {
5137 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5139 if (!size_only
&& array_length
< index
) {
5140 init_putz(type
, sec
, c
+ array_length
,
5141 index
- array_length
);
5143 index
= index
+ type_size(&f
->type
, &align1
);
5144 if (index
> array_length
)
5145 array_length
= index
;
5147 /* gr: skip fields from same union - ugly. */
5149 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5150 /* test for same offset */
5151 if (f
->next
->c
!= f
->c
)
5153 /* if yes, test for bitfield shift */
5154 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5155 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5156 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5157 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5158 if (bit_pos_1
!= bit_pos_2
)
5165 if (no_oblock
&& f
== NULL
)
5171 /* put zeros at the end */
5172 if (!size_only
&& array_length
< n
) {
5173 init_putz(type
, sec
, c
+ array_length
,
5182 } else if (tok
== '{') {
5184 decl_initializer(type
, sec
, c
, first
, size_only
);
5186 } else if (size_only
) {
5187 /* just skip expression */
5188 parlevel
= parlevel1
= 0;
5189 while ((parlevel
> 0 || parlevel1
> 0 ||
5190 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5193 else if (tok
== ')')
5195 else if (tok
== '{')
5197 else if (tok
== '}')
5202 /* currently, we always use constant expression for globals
5203 (may change for scripting case) */
5204 expr_type
= EXPR_CONST
;
5206 expr_type
= EXPR_ANY
;
5207 init_putv(type
, sec
, c
, 0, expr_type
);
5211 /* parse an initializer for type 't' if 'has_init' is non zero, and
5212 allocate space in local or global data space ('r' is either
5213 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5214 variable 'v' with an associated name represented by 'asm_label' of
5215 scope 'scope' is declared before initializers are parsed. If 'v' is
5216 zero, then a reference to the new object is put in the value stack.
5217 If 'has_init' is 2, a special parsing is done to handle string
5219 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5220 int has_init
, int v
, char *asm_label
,
5223 int size
, align
, addr
, data_offset
;
5225 ParseState saved_parse_state
= {0};
5226 TokenString init_str
;
5228 Sym
*flexible_array
;
5230 flexible_array
= NULL
;
5231 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5234 while (field
&& field
->next
)
5235 field
= field
->next
;
5236 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5237 flexible_array
= field
;
5240 size
= type_size(type
, &align
);
5241 /* If unknown size, we must evaluate it before
5242 evaluating initializers because
5243 initializers can generate global data too
5244 (e.g. string pointers or ISOC99 compound
5245 literals). It also simplifies local
5246 initializers handling */
5247 tok_str_new(&init_str
);
5248 if (size
< 0 || (flexible_array
&& has_init
)) {
5250 tcc_error("unknown type size");
5251 /* get all init string */
5252 if (has_init
== 2) {
5253 /* only get strings */
5254 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5255 tok_str_add_tok(&init_str
);
5260 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5262 tcc_error("unexpected end of file in initializer");
5263 tok_str_add_tok(&init_str
);
5266 else if (tok
== '}') {
5276 tok_str_add(&init_str
, -1);
5277 tok_str_add(&init_str
, 0);
5280 save_parse_state(&saved_parse_state
);
5282 macro_ptr
= init_str
.str
;
5284 decl_initializer(type
, NULL
, 0, 1, 1);
5285 /* prepare second initializer parsing */
5286 macro_ptr
= init_str
.str
;
5289 /* if still unknown size, error */
5290 size
= type_size(type
, &align
);
5292 tcc_error("unknown type size");
5295 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5296 /* take into account specified alignment if bigger */
5298 if (ad
->aligned
> align
)
5299 align
= ad
->aligned
;
5300 } else if (ad
->packed
) {
5303 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5305 #ifdef CONFIG_TCC_BCHECK
5306 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5310 loc
= (loc
- size
) & -align
;
5312 #ifdef CONFIG_TCC_BCHECK
5313 /* handles bounds */
5314 /* XXX: currently, since we do only one pass, we cannot track
5315 '&' operators, so we add only arrays */
5316 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5317 unsigned long *bounds_ptr
;
5318 /* add padding between regions */
5320 /* then add local bound info */
5321 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5322 bounds_ptr
[0] = addr
;
5323 bounds_ptr
[1] = size
;
5327 /* local variable */
5328 sym_push(v
, type
, r
, addr
);
5330 /* push local reference */
5331 vset(type
, r
, addr
);
5337 if (v
&& scope
== VT_CONST
) {
5338 /* see if the symbol was already defined */
5341 if (!is_compatible_types(&sym
->type
, type
))
5342 tcc_error("incompatible types for redefinition of '%s'",
5343 get_tok_str(v
, NULL
));
5344 if (sym
->type
.t
& VT_EXTERN
) {
5345 /* if the variable is extern, it was not allocated */
5346 sym
->type
.t
&= ~VT_EXTERN
;
5347 /* set array size if it was ommited in extern
5349 if ((sym
->type
.t
& VT_ARRAY
) &&
5350 sym
->type
.ref
->c
< 0 &&
5352 sym
->type
.ref
->c
= type
->ref
->c
;
5354 /* we accept several definitions of the same
5355 global variable. this is tricky, because we
5356 must play with the SHN_COMMON type of the symbol */
5357 /* XXX: should check if the variable was already
5358 initialized. It is incorrect to initialized it
5360 /* no init data, we won't add more to the symbol */
5367 /* allocate symbol in corresponding section */
5372 else if (tcc_state
->nocommon
)
5376 data_offset
= sec
->data_offset
;
5377 data_offset
= (data_offset
+ align
- 1) & -align
;
5379 /* very important to increment global pointer at this time
5380 because initializers themselves can create new initializers */
5381 data_offset
+= size
;
5382 #ifdef CONFIG_TCC_BCHECK
5383 /* add padding if bound check */
5384 if (tcc_state
->do_bounds_check
)
5387 sec
->data_offset
= data_offset
;
5388 /* allocate section space to put the data */
5389 if (sec
->sh_type
!= SHT_NOBITS
&&
5390 data_offset
> sec
->data_allocated
)
5391 section_realloc(sec
, data_offset
);
5392 /* align section if needed */
5393 if (align
> sec
->sh_addralign
)
5394 sec
->sh_addralign
= align
;
5396 addr
= 0; /* avoid warning */
5400 if (scope
!= VT_CONST
|| !sym
) {
5401 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5402 sym
->asm_label
= asm_label
;
5404 /* update symbol definition */
5406 put_extern_sym(sym
, sec
, addr
, size
);
5409 /* put a common area */
5410 put_extern_sym(sym
, NULL
, align
, size
);
5411 /* XXX: find a nicer way */
5412 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5413 esym
->st_shndx
= SHN_COMMON
;
5418 /* push global reference */
5419 sym
= get_sym_ref(type
, sec
, addr
, size
);
5421 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5424 /* patch symbol weakness */
5425 if (type
->t
& VT_WEAK
)
5427 #ifdef CONFIG_TCC_BCHECK
5428 /* handles bounds now because the symbol must be defined
5429 before for the relocation */
5430 if (tcc_state
->do_bounds_check
) {
5431 unsigned long *bounds_ptr
;
5433 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5434 /* then add global bound info */
5435 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5436 bounds_ptr
[0] = 0; /* relocated */
5437 bounds_ptr
[1] = size
;
5441 if (has_init
|| (type
->t
& VT_VLA
)) {
5442 decl_initializer(type
, sec
, addr
, 1, 0);
5443 /* restore parse state if needed */
5445 tok_str_free(init_str
.str
);
5446 restore_parse_state(&saved_parse_state
);
5448 /* patch flexible array member size back to -1, */
5449 /* for possible subsequent similar declarations */
5451 flexible_array
->type
.ref
->c
= -1;
5456 static void put_func_debug(Sym
*sym
)
5461 /* XXX: we put here a dummy type */
5462 snprintf(buf
, sizeof(buf
), "%s:%c1",
5463 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5464 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5465 cur_text_section
, sym
->c
);
5466 /* //gr gdb wants a line at the function */
5467 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5472 /* parse an old style function declaration list */
5473 /* XXX: check multiple parameter */
5474 static void func_decl_list(Sym
*func_sym
)
5481 /* parse each declaration */
5482 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5483 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5484 if (!parse_btype(&btype
, &ad
))
5485 expect("declaration list");
5486 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5487 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5489 /* we accept no variable after */
5493 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5494 /* find parameter in function parameter list */
5497 if ((s
->v
& ~SYM_FIELD
) == v
)
5501 tcc_error("declaration for parameter '%s' but no such parameter",
5502 get_tok_str(v
, NULL
));
5504 /* check that no storage specifier except 'register' was given */
5505 if (type
.t
& VT_STORAGE
)
5506 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5507 convert_parameter_type(&type
);
5508 /* we can add the type (NOTE: it could be local to the function) */
5510 /* accept other parameters */
5521 /* parse a function defined by symbol 'sym' and generate its code in
5522 'cur_text_section' */
5523 static void gen_function(Sym
*sym
)
5525 int saved_nocode_wanted
= nocode_wanted
;
5527 ind
= cur_text_section
->data_offset
;
5528 /* NOTE: we patch the symbol size later */
5529 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5530 funcname
= get_tok_str(sym
->v
, NULL
);
5532 /* put debug symbol */
5533 if (tcc_state
->do_debug
)
5534 put_func_debug(sym
);
5535 /* push a dummy symbol to enable local sym storage */
5536 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5537 gfunc_prolog(&sym
->type
);
5539 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5542 cur_text_section
->data_offset
= ind
;
5543 label_pop(&global_label_stack
, NULL
);
5544 /* reset local stack */
5545 scope_stack_bottom
= NULL
;
5546 sym_pop(&local_stack
, NULL
);
5547 /* end of function */
5548 /* patch symbol size */
5549 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5551 /* patch symbol weakness (this definition overrules any prototype) */
5552 if (sym
->type
.t
& VT_WEAK
)
5554 if (tcc_state
->do_debug
) {
5555 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5557 /* It's better to crash than to generate wrong code */
5558 cur_text_section
= NULL
;
5559 funcname
= ""; /* for safety */
5560 func_vt
.t
= VT_VOID
; /* for safety */
5561 ind
= 0; /* for safety */
5562 nocode_wanted
= saved_nocode_wanted
;
5565 ST_FUNC
void gen_inline_functions(void)
5568 int *str
, inline_generated
, i
;
5569 struct InlineFunc
*fn
;
5571 /* iterate while inline function are referenced */
5573 inline_generated
= 0;
5574 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5575 fn
= tcc_state
->inline_fns
[i
];
5577 if (sym
&& sym
->c
) {
5578 /* the function was used: generate its code and
5579 convert it to a normal function */
5580 str
= fn
->token_str
;
5583 strcpy(file
->filename
, fn
->filename
);
5584 sym
->r
= VT_SYM
| VT_CONST
;
5585 sym
->type
.t
&= ~VT_INLINE
;
5589 cur_text_section
= text_section
;
5591 macro_ptr
= NULL
; /* fail safe */
5593 inline_generated
= 1;
5596 if (!inline_generated
)
5599 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5600 fn
= tcc_state
->inline_fns
[i
];
5601 str
= fn
->token_str
;
5604 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5607 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5608 static int decl0(int l
, int is_for_loop_init
)
5616 if (!parse_btype(&btype
, &ad
)) {
5617 if (is_for_loop_init
)
5619 /* skip redundant ';' */
5620 /* XXX: find more elegant solution */
5625 if (l
== VT_CONST
&&
5626 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5627 /* global asm block */
5631 /* special test for old K&R protos without explicit int
5632 type. Only accepted when defining global data */
5633 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5637 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5638 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5640 /* we accept no variable after */
5644 while (1) { /* iterate thru each declaration */
5645 char *asm_label
; // associated asm label
5647 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5651 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5652 printf("type = '%s'\n", buf
);
5655 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5656 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5657 tcc_error("function without file scope cannot be static");
5659 /* if old style function prototype, we accept a
5662 if (sym
->c
== FUNC_OLD
)
5663 func_decl_list(sym
);
5667 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5670 asm_label_instr(&astr
);
5671 asm_label
= tcc_strdup(astr
.data
);
5674 /* parse one last attribute list, after asm label */
5675 parse_attribute(&ad
);
5680 #ifdef TCC_TARGET_PE
5682 type
.t
|= VT_IMPORT
;
5684 type
.t
|= VT_EXPORT
;
5688 tcc_error("cannot use local functions");
5689 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5690 expect("function definition");
5692 /* reject abstract declarators in function definition */
5694 while ((sym
= sym
->next
) != NULL
)
5695 if (!(sym
->v
& ~SYM_FIELD
))
5696 expect("identifier");
5698 /* XXX: cannot do better now: convert extern line to static inline */
5699 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5700 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5704 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5707 r
= sym
->type
.ref
->r
;
5708 /* use func_call from prototype if not defined */
5709 if (FUNC_CALL(r
) != FUNC_CDECL
5710 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5711 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5713 /* use export from prototype */
5715 FUNC_EXPORT(type
.ref
->r
) = 1;
5717 /* use static from prototype */
5718 if (sym
->type
.t
& VT_STATIC
)
5719 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5721 if (!is_compatible_types(&sym
->type
, &type
)) {
5723 tcc_error("incompatible types for redefinition of '%s'",
5724 get_tok_str(v
, NULL
));
5726 /* if symbol is already defined, then put complete type */
5729 /* put function symbol */
5730 sym
= global_identifier_push(v
, type
.t
, 0);
5731 sym
->type
.ref
= type
.ref
;
5734 /* static inline functions are just recorded as a kind
5735 of macro. Their code will be emitted at the end of
5736 the compilation unit only if they are used */
5737 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5738 (VT_INLINE
| VT_STATIC
)) {
5739 TokenString func_str
;
5741 struct InlineFunc
*fn
;
5742 const char *filename
;
5744 tok_str_new(&func_str
);
5750 tcc_error("unexpected end of file");
5751 tok_str_add_tok(&func_str
);
5756 } else if (t
== '}') {
5758 if (block_level
== 0)
5762 tok_str_add(&func_str
, -1);
5763 tok_str_add(&func_str
, 0);
5764 filename
= file
? file
->filename
: "";
5765 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5766 strcpy(fn
->filename
, filename
);
5768 fn
->token_str
= func_str
.str
;
5769 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5772 /* compute text section */
5773 cur_text_section
= ad
.section
;
5774 if (!cur_text_section
)
5775 cur_text_section
= text_section
;
5776 sym
->r
= VT_SYM
| VT_CONST
;
5781 if (btype
.t
& VT_TYPEDEF
) {
5782 /* save typedefed type */
5783 /* XXX: test storage specifiers ? */
5784 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5785 sym
->type
.t
|= VT_TYPEDEF
;
5788 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5789 /* external function definition */
5790 /* specific case for func_call attribute */
5791 type
.ref
->r
= INT_ATTR(&ad
);
5792 } else if (!(type
.t
& VT_ARRAY
)) {
5793 /* not lvalue if array */
5794 r
|= lvalue_type(type
.t
);
5796 has_init
= (tok
== '=');
5797 if (has_init
&& (type
.t
& VT_VLA
))
5798 tcc_error("Variable length array cannot be initialized");
5799 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5800 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5801 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5802 /* external variable or function */
5803 /* NOTE: as GCC, uninitialized global static
5804 arrays of null size are considered as
5806 sym
= external_sym(v
, &type
, r
, asm_label
);
5808 if (type
.t
& VT_WEAK
)
5811 if (ad
.alias_target
) {
5816 alias_target
= sym_find(ad
.alias_target
);
5817 if (!alias_target
|| !alias_target
->c
)
5818 tcc_error("unsupported forward __alias__ attribute");
5819 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5820 tsec
.sh_num
= esym
->st_shndx
;
5821 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5824 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5825 if (type
.t
& VT_STATIC
)
5831 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5835 if (is_for_loop_init
)
5848 ST_FUNC
void decl(int l
)