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
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
57 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
59 ST_DATA
int const_wanted
; /* true if constant wanted */
60 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
61 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
64 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
65 ST_DATA
char *funcname
;
67 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType
*type
);
71 static inline CType
*pointed_type(CType
*type
);
72 static int is_compatible_types(CType
*type1
, CType
*type2
);
73 static int parse_btype(CType
*type
, AttributeDef
*ad
);
74 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
75 static void parse_expr_type(CType
*type
);
76 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
77 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
78 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
79 static int decl0(int l
, int is_for_loop_init
);
80 static void expr_eq(void);
81 static void unary_type(CType
*type
);
82 static void vla_runtime_type_size(CType
*type
, int *a
);
83 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
84 static void expr_type(CType
*type
);
86 ST_INLN
int is_float(int t
)
90 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
93 /* we use our own 'finite' function to avoid potential problems with
94 non standard math libs */
95 /* XXX: endianness dependent */
96 ST_FUNC
int ieee_finite(double d
)
99 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
102 ST_FUNC
void test_lvalue(void)
104 if (!(vtop
->r
& VT_LVAL
))
108 /* ------------------------------------------------------------------------- */
109 /* symbol allocator */
110 static Sym
*__sym_malloc(void)
112 Sym
*sym_pool
, *sym
, *last_sym
;
115 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
116 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
118 last_sym
= sym_free_first
;
120 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
121 sym
->next
= last_sym
;
125 sym_free_first
= last_sym
;
129 static inline Sym
*sym_malloc(void)
132 sym
= sym_free_first
;
134 sym
= __sym_malloc();
135 sym_free_first
= sym
->next
;
139 ST_INLN
void sym_free(Sym
*sym
)
141 sym
->next
= sym_free_first
;
142 tcc_free(sym
->asm_label
);
143 sym_free_first
= sym
;
146 /* push, without hashing */
147 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
166 /* find a symbol and return its associated structure. 's' is the top
167 of the symbol stack */
168 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
178 /* structure lookup */
179 ST_INLN Sym
*struct_find(int v
)
182 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
184 return table_ident
[v
]->sym_struct
;
187 /* find an identifier */
188 ST_INLN Sym
*sym_find(int v
)
191 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
193 return table_ident
[v
]->sym_identifier
;
196 /* push a given symbol on the symbol stack */
197 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
206 s
= sym_push2(ps
, v
, type
->t
, c
);
207 s
->type
.ref
= type
->ref
;
209 /* don't record fields or anonymous symbols */
211 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
212 /* record symbol in token array */
213 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
215 ps
= &ts
->sym_struct
;
217 ps
= &ts
->sym_identifier
;
224 /* push a global identifier */
225 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
228 s
= sym_push2(&global_stack
, v
, t
, c
);
229 /* don't record anonymous symbol */
230 if (v
< SYM_FIRST_ANOM
) {
231 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
232 /* modify the top most local identifier, so that
233 sym_identifier will point to 's' when popped */
235 ps
= &(*ps
)->prev_tok
;
242 /* pop symbols until top reaches 'b' */
243 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
253 /* remove symbol in token array */
255 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
256 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
258 ps
= &ts
->sym_struct
;
260 ps
= &ts
->sym_identifier
;
269 static void weaken_symbol(Sym
*sym
)
271 sym
->type
.t
|= VT_WEAK
;
276 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
277 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
278 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
282 /* ------------------------------------------------------------------------- */
284 ST_FUNC
void swap(int *p
, int *q
)
292 static void vsetc(CType
*type
, int r
, CValue
*vc
)
296 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
297 tcc_error("memory full");
298 /* cannot let cpu flags if other instruction are generated. Also
299 avoid leaving VT_JMP anywhere except on the top of the stack
300 because it would complicate the code generator. */
301 if (vtop
>= vstack
) {
302 v
= vtop
->r
& VT_VALMASK
;
303 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
313 /* push constant of type "type" with useless value */
314 void vpush(CType
*type
)
317 vsetc(type
, VT_CONST
, &cval
);
320 /* push integer constant */
321 ST_FUNC
void vpushi(int v
)
325 vsetc(&int_type
, VT_CONST
, &cval
);
328 /* push a pointer sized constant */
329 static void vpushs(long long v
)
336 vsetc(&size_type
, VT_CONST
, &cval
);
339 /* push long long constant */
340 static void vpushll(long long v
)
347 vsetc(&ctype
, VT_CONST
, &cval
);
350 /* push arbitrary 64bit constant */
351 void vpush64(int ty
, unsigned long long v
)
357 vsetc(&ctype
, VT_CONST
, &cval
);
360 /* Return a static symbol pointing to a section */
361 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
367 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
368 sym
->type
.ref
= type
->ref
;
369 sym
->r
= VT_CONST
| VT_SYM
;
370 put_extern_sym(sym
, sec
, offset
, size
);
374 /* push a reference to a section offset by adding a dummy symbol */
375 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
380 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
381 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
384 /* define a new external reference to a symbol 'v' of type 'u' */
385 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
391 /* push forward reference */
392 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
393 s
->type
.ref
= type
->ref
;
394 s
->r
= r
| VT_CONST
| VT_SYM
;
399 /* define a new external reference to a symbol 'v' with alternate asm
400 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
401 is no alternate name (most cases) */
402 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
408 /* push forward reference */
409 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
410 s
->asm_label
= asm_label
;
411 s
->type
.t
|= VT_EXTERN
;
412 } else if (s
->type
.ref
== func_old_type
.ref
) {
413 s
->type
.ref
= type
->ref
;
414 s
->r
= r
| VT_CONST
| VT_SYM
;
415 s
->type
.t
|= VT_EXTERN
;
416 } else if (!is_compatible_types(&s
->type
, type
)) {
417 tcc_error("incompatible types for redefinition of '%s'",
418 get_tok_str(v
, NULL
));
423 /* push a reference to global symbol v */
424 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
429 sym
= external_global_sym(v
, type
, 0);
431 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
435 ST_FUNC
void vset(CType
*type
, int r
, int v
)
440 vsetc(type
, r
, &cval
);
443 static void vseti(int r
, int v
)
451 ST_FUNC
void vswap(void)
455 /* cannot let cpu flags if other instruction are generated. Also
456 avoid leaving VT_JMP anywhere except on the top of the stack
457 because it would complicate the code generator. */
458 if (vtop
>= vstack
) {
459 int v
= vtop
->r
& VT_VALMASK
;
460 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
468 ST_FUNC
void vpushv(SValue
*v
)
470 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
471 tcc_error("memory full");
476 static void vdup(void)
481 /* save r to the memory stack, and mark it as being free */
482 ST_FUNC
void save_reg(int r
)
484 int l
, saved
, size
, align
;
488 /* modify all stack values */
491 for(p
=vstack
;p
<=vtop
;p
++) {
492 if ((p
->r
& VT_VALMASK
) == r
||
493 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
494 /* must save value on stack if not already done */
496 /* NOTE: must reload 'r' because r might be equal to r2 */
497 r
= p
->r
& VT_VALMASK
;
498 /* store register in the stack */
500 if ((p
->r
& VT_LVAL
) ||
501 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
502 #ifdef TCC_TARGET_X86_64
503 type
= &char_pointer_type
;
507 size
= type_size(type
, &align
);
508 loc
= (loc
- size
) & -align
;
510 sv
.r
= VT_LOCAL
| VT_LVAL
;
513 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
514 /* x86 specific: need to pop fp register ST0 if saved */
516 o(0xd8dd); /* fstp %st(0) */
519 #ifndef TCC_TARGET_X86_64
520 /* special long long case */
521 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
529 /* mark that stack entry as being saved on the stack */
530 if (p
->r
& VT_LVAL
) {
531 /* also clear the bounded flag because the
532 relocation address of the function was stored in
534 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
536 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
544 #ifdef TCC_TARGET_ARM
545 /* find a register of class 'rc2' with at most one reference on stack.
546 * If none, call get_reg(rc) */
547 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
552 for(r
=0;r
<NB_REGS
;r
++) {
553 if (reg_classes
[r
] & rc2
) {
556 for(p
= vstack
; p
<= vtop
; p
++) {
557 if ((p
->r
& VT_VALMASK
) == r
||
558 (p
->r2
& VT_VALMASK
) == r
)
569 /* find a free register of class 'rc'. If none, save one register */
570 ST_FUNC
int get_reg(int rc
)
575 /* find a free register */
576 for(r
=0;r
<NB_REGS
;r
++) {
577 if (reg_classes
[r
] & rc
) {
578 for(p
=vstack
;p
<=vtop
;p
++) {
579 if ((p
->r
& VT_VALMASK
) == r
||
580 (p
->r2
& VT_VALMASK
) == r
)
588 /* no register left : free the first one on the stack (VERY
589 IMPORTANT to start from the bottom to ensure that we don't
590 spill registers used in gen_opi()) */
591 for(p
=vstack
;p
<=vtop
;p
++) {
592 r
= p
->r
& VT_VALMASK
;
593 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
595 /* also look at second register (if long long) */
596 r
= p
->r2
& VT_VALMASK
;
597 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
603 /* Should never comes here */
607 /* save registers up to (vtop - n) stack entry */
608 ST_FUNC
void save_regs(int n
)
613 for(p
= vstack
;p
<= p1
; p
++) {
614 r
= p
->r
& VT_VALMASK
;
621 /* move register 's' to 'r', and flush previous value of r to memory
623 static void move_reg(int r
, int s
)
636 /* get address of vtop (vtop MUST BE an lvalue) */
637 static void gaddrof(void)
639 if (vtop
->r
& VT_REF
)
642 /* tricky: if saved lvalue, then we can go back to lvalue */
643 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
644 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
649 #ifdef CONFIG_TCC_BCHECK
650 /* generate lvalue bound code */
651 static void gbound(void)
656 vtop
->r
&= ~VT_MUSTBOUND
;
657 /* if lvalue, then use checking code before dereferencing */
658 if (vtop
->r
& VT_LVAL
) {
659 /* if not VT_BOUNDED value, then make one */
660 if (!(vtop
->r
& VT_BOUNDED
)) {
661 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
662 /* must save type because we must set it to int to get pointer */
664 vtop
->type
.t
= VT_INT
;
667 gen_bounded_ptr_add();
668 vtop
->r
|= lval_type
;
671 /* then check for dereferencing */
672 gen_bounded_ptr_deref();
677 /* store vtop a register belonging to class 'rc'. lvalues are
678 converted to values. Cannot be used if cannot be converted to
679 register value (such as structures). */
680 ST_FUNC
int gv(int rc
)
682 int r
, bit_pos
, bit_size
, size
, align
, i
;
683 #ifndef TCC_TARGET_X86_64
687 /* NOTE: get_reg can modify vstack[] */
688 if (vtop
->type
.t
& VT_BITFIELD
) {
691 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
692 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
693 /* remove bit field info to avoid loops */
694 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
695 /* cast to int to propagate signedness in following ops */
696 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
701 if((vtop
->type
.t
& VT_UNSIGNED
) ||
702 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
703 type
.t
|= VT_UNSIGNED
;
705 /* generate shifts */
706 vpushi(bits
- (bit_pos
+ bit_size
));
708 vpushi(bits
- bit_size
);
709 /* NOTE: transformed to SHR if unsigned */
713 if (is_float(vtop
->type
.t
) &&
714 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
717 unsigned long offset
;
718 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
722 /* XXX: unify with initializers handling ? */
723 /* CPUs usually cannot use float constants, so we store them
724 generically in data segment */
725 size
= type_size(&vtop
->type
, &align
);
726 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
727 data_section
->data_offset
= offset
;
728 /* XXX: not portable yet */
729 #if defined(__i386__) || defined(__x86_64__)
730 /* Zero pad x87 tenbyte long doubles */
731 if (size
== LDOUBLE_SIZE
) {
732 vtop
->c
.tab
[2] &= 0xffff;
733 #if LDOUBLE_SIZE == 16
738 ptr
= section_ptr_add(data_section
, size
);
740 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
744 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
748 ptr
[i
] = vtop
->c
.tab
[i
];
749 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
750 vtop
->r
|= VT_LVAL
| VT_SYM
;
754 #ifdef CONFIG_TCC_BCHECK
755 if (vtop
->r
& VT_MUSTBOUND
)
759 r
= vtop
->r
& VT_VALMASK
;
760 #ifndef TCC_TARGET_X86_64
765 /* need to reload if:
767 - lvalue (need to dereference pointer)
768 - already a register, but not in the right class */
770 || (vtop
->r
& VT_LVAL
)
771 || !(reg_classes
[r
] & rc
)
772 #ifndef TCC_TARGET_X86_64
773 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
778 #ifndef TCC_TARGET_X86_64
779 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
781 unsigned long long ll
;
782 /* two register type load : expand to two words
784 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
787 vtop
->c
.ui
= ll
; /* first word */
789 vtop
->r
= r
; /* save register value */
790 vpushi(ll
>> 32); /* second word */
791 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
792 (vtop
->r
& VT_LVAL
)) {
793 /* We do not want to modifier the long long
794 pointer here, so the safest (and less
795 efficient) is to save all the other registers
796 in the stack. XXX: totally inefficient. */
798 /* load from memory */
801 vtop
[-1].r
= r
; /* save register value */
802 /* increment pointer to get second word */
803 vtop
->type
.t
= VT_INT
;
812 vtop
[-1].r
= r
; /* save register value */
813 vtop
->r
= vtop
[-1].r2
;
815 /* allocate second register */
819 /* write second register */
823 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
825 /* lvalue of scalar type : need to use lvalue type
826 because of possible cast */
829 /* compute memory access type */
830 if (vtop
->r
& VT_LVAL_BYTE
)
832 else if (vtop
->r
& VT_LVAL_SHORT
)
834 if (vtop
->r
& VT_LVAL_UNSIGNED
)
838 /* restore wanted type */
841 /* one register type load */
846 #ifdef TCC_TARGET_C67
847 /* uses register pairs for doubles */
848 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
855 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
856 ST_FUNC
void gv2(int rc1
, int rc2
)
860 /* generate more generic register first. But VT_JMP or VT_CMP
861 values must be generated first in all cases to avoid possible
863 v
= vtop
[0].r
& VT_VALMASK
;
864 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
869 /* test if reload is needed for first register */
870 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
880 /* test if reload is needed for first register */
881 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
887 /* wrapper around RC_FRET to return a register by type */
888 static int rc_fret(int t
)
890 #ifdef TCC_TARGET_X86_64
891 if (t
== VT_LDOUBLE
) {
898 /* wrapper around REG_FRET to return a register by type */
899 static int reg_fret(int t
)
901 #ifdef TCC_TARGET_X86_64
902 if (t
== VT_LDOUBLE
) {
909 /* expand long long on stack in two int registers */
910 static void lexpand(void)
914 u
= vtop
->type
.t
& VT_UNSIGNED
;
917 vtop
[0].r
= vtop
[-1].r2
;
918 vtop
[0].r2
= VT_CONST
;
919 vtop
[-1].r2
= VT_CONST
;
920 vtop
[0].type
.t
= VT_INT
| u
;
921 vtop
[-1].type
.t
= VT_INT
| u
;
924 #ifdef TCC_TARGET_ARM
925 /* expand long long on stack */
926 ST_FUNC
void lexpand_nr(void)
930 u
= vtop
->type
.t
& VT_UNSIGNED
;
933 vtop
->type
.t
= VT_INT
| u
;
934 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
936 vtop
[-1].c
.ui
= vtop
->c
.ull
;
937 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
939 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
941 vtop
->r
= vtop
[-1].r
;
942 } else if (v
> VT_CONST
) {
946 vtop
->r
= vtop
[-1].r2
;
947 vtop
[-1].r2
= VT_CONST
;
948 vtop
[-1].type
.t
= VT_INT
| u
;
952 /* build a long long from two ints */
953 static void lbuild(int t
)
956 vtop
[-1].r2
= vtop
[0].r
;
961 /* rotate n first stack elements to the bottom
962 I1 ... In -> I2 ... In I1 [top is right]
964 ST_FUNC
void vrotb(int n
)
975 /* rotate the n elements before entry e towards the top
976 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
978 ST_FUNC
void vrote(SValue
*e
, int n
)
984 for(i
= 0;i
< n
- 1; i
++)
989 /* rotate n first stack elements to the top
990 I1 ... In -> In I1 ... I(n-1) [top is right]
992 ST_FUNC
void vrott(int n
)
997 /* pop stack value */
998 ST_FUNC
void vpop(void)
1001 v
= vtop
->r
& VT_VALMASK
;
1002 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1003 /* for x86, we need to pop the FP stack */
1004 if (v
== TREG_ST0
&& !nocode_wanted
) {
1005 o(0xd8dd); /* fstp %st(0) */
1008 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1009 /* need to put correct jump if && or || without test */
1015 /* convert stack entry to register and duplicate its value in another
1017 static void gv_dup(void)
1023 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1030 /* stack: H L L1 H1 */
1038 /* duplicate value */
1043 #ifdef TCC_TARGET_X86_64
1044 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1054 load(r1
, &sv
); /* move r to r1 */
1056 /* duplicates value */
1062 #ifndef TCC_TARGET_X86_64
1063 /* generate CPU independent (unsigned) long long operations */
1064 static void gen_opl(int op
)
1066 int t
, a
, b
, op1
, c
, i
;
1068 unsigned short reg_iret
= REG_IRET
;
1069 unsigned short reg_lret
= REG_LRET
;
1075 func
= TOK___divdi3
;
1078 func
= TOK___udivdi3
;
1081 func
= TOK___moddi3
;
1084 func
= TOK___umoddi3
;
1091 /* call generic long long function */
1092 vpush_global_sym(&func_old_type
, func
);
1097 vtop
->r2
= reg_lret
;
1110 /* stack: L1 H1 L2 H2 */
1115 vtop
[-2] = vtop
[-3];
1118 /* stack: H1 H2 L1 L2 */
1124 /* stack: H1 H2 L1 L2 ML MH */
1127 /* stack: ML MH H1 H2 L1 L2 */
1131 /* stack: ML MH H1 L2 H2 L1 */
1136 /* stack: ML MH M1 M2 */
1139 } else if (op
== '+' || op
== '-') {
1140 /* XXX: add non carry method too (for MIPS or alpha) */
1146 /* stack: H1 H2 (L1 op L2) */
1149 gen_op(op1
+ 1); /* TOK_xxxC2 */
1152 /* stack: H1 H2 (L1 op L2) */
1155 /* stack: (L1 op L2) H1 H2 */
1157 /* stack: (L1 op L2) (H1 op H2) */
1165 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1166 t
= vtop
[-1].type
.t
;
1170 /* stack: L H shift */
1172 /* constant: simpler */
1173 /* NOTE: all comments are for SHL. the other cases are
1174 done by swaping words */
1185 if (op
!= TOK_SAR
) {
1218 /* XXX: should provide a faster fallback on x86 ? */
1221 func
= TOK___ashrdi3
;
1224 func
= TOK___lshrdi3
;
1227 func
= TOK___ashldi3
;
1233 /* compare operations */
1239 /* stack: L1 H1 L2 H2 */
1241 vtop
[-1] = vtop
[-2];
1243 /* stack: L1 L2 H1 H2 */
1246 /* when values are equal, we need to compare low words. since
1247 the jump is inverted, we invert the test too. */
1250 else if (op1
== TOK_GT
)
1252 else if (op1
== TOK_ULT
)
1254 else if (op1
== TOK_UGT
)
1259 if (op1
!= TOK_NE
) {
1263 /* generate non equal test */
1264 /* XXX: NOT PORTABLE yet */
1268 #if defined(TCC_TARGET_I386)
1269 b
= psym(0x850f, 0);
1270 #elif defined(TCC_TARGET_ARM)
1272 o(0x1A000000 | encbranch(ind
, 0, 1));
1273 #elif defined(TCC_TARGET_C67)
1274 tcc_error("not implemented");
1276 #error not supported
1280 /* compare low. Always unsigned */
1284 else if (op1
== TOK_LE
)
1286 else if (op1
== TOK_GT
)
1288 else if (op1
== TOK_GE
)
1299 /* handle integer constant optimizations and various machine
1301 static void gen_opic(int op
)
1303 int c1
, c2
, t1
, t2
, n
;
1306 typedef unsigned long long U
;
1310 t1
= v1
->type
.t
& VT_BTYPE
;
1311 t2
= v2
->type
.t
& VT_BTYPE
;
1315 else if (v1
->type
.t
& VT_UNSIGNED
)
1322 else if (v2
->type
.t
& VT_UNSIGNED
)
1327 /* currently, we cannot do computations with forward symbols */
1328 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1329 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1332 case '+': l1
+= l2
; break;
1333 case '-': l1
-= l2
; break;
1334 case '&': l1
&= l2
; break;
1335 case '^': l1
^= l2
; break;
1336 case '|': l1
|= l2
; break;
1337 case '*': l1
*= l2
; break;
1344 /* if division by zero, generate explicit division */
1347 tcc_error("division by zero in constant");
1351 default: l1
/= l2
; break;
1352 case '%': l1
%= l2
; break;
1353 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1354 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1357 case TOK_SHL
: l1
<<= l2
; break;
1358 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1359 case TOK_SAR
: l1
>>= l2
; break;
1361 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1362 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1363 case TOK_EQ
: l1
= l1
== l2
; break;
1364 case TOK_NE
: l1
= l1
!= l2
; break;
1365 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1366 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1367 case TOK_LT
: l1
= l1
< l2
; break;
1368 case TOK_GE
: l1
= l1
>= l2
; break;
1369 case TOK_LE
: l1
= l1
<= l2
; break;
1370 case TOK_GT
: l1
= l1
> l2
; break;
1372 case TOK_LAND
: l1
= l1
&& l2
; break;
1373 case TOK_LOR
: l1
= l1
|| l2
; break;
1380 /* if commutative ops, put c2 as constant */
1381 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1382 op
== '|' || op
== '*')) {
1384 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1385 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1387 /* Filter out NOP operations like x*1, x-0, x&-1... */
1388 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1391 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1392 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1398 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1399 /* try to use shifts instead of muls or divs */
1400 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1409 else if (op
== TOK_PDIV
)
1415 } else if (c2
&& (op
== '+' || op
== '-') &&
1416 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1417 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1418 /* symbol + constant case */
1425 if (!nocode_wanted
) {
1426 /* call low level op generator */
1427 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1438 /* generate a floating point operation with constant propagation */
1439 static void gen_opif(int op
)
1447 /* currently, we cannot do computations with forward symbols */
1448 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1449 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1451 if (v1
->type
.t
== VT_FLOAT
) {
1454 } else if (v1
->type
.t
== VT_DOUBLE
) {
1462 /* NOTE: we only do constant propagation if finite number (not
1463 NaN or infinity) (ANSI spec) */
1464 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1468 case '+': f1
+= f2
; break;
1469 case '-': f1
-= f2
; break;
1470 case '*': f1
*= f2
; break;
1474 tcc_error("division by zero in constant");
1479 /* XXX: also handles tests ? */
1483 /* XXX: overflow test ? */
1484 if (v1
->type
.t
== VT_FLOAT
) {
1486 } else if (v1
->type
.t
== VT_DOUBLE
) {
1494 if (!nocode_wanted
) {
1502 static int pointed_size(CType
*type
)
1505 return type_size(pointed_type(type
), &align
);
1508 static void vla_runtime_pointed_size(CType
*type
)
1511 vla_runtime_type_size(pointed_type(type
), &align
);
1514 static inline int is_null_pointer(SValue
*p
)
1516 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1518 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1519 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1520 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1523 static inline int is_integer_btype(int bt
)
1525 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1526 bt
== VT_INT
|| bt
== VT_LLONG
);
1529 /* check types for comparison or substraction of pointers */
1530 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1532 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1535 /* null pointers are accepted for all comparisons as gcc */
1536 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1540 bt1
= type1
->t
& VT_BTYPE
;
1541 bt2
= type2
->t
& VT_BTYPE
;
1542 /* accept comparison between pointer and integer with a warning */
1543 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1544 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1545 tcc_warning("comparison between pointer and integer");
1549 /* both must be pointers or implicit function pointers */
1550 if (bt1
== VT_PTR
) {
1551 type1
= pointed_type(type1
);
1552 } else if (bt1
!= VT_FUNC
)
1553 goto invalid_operands
;
1555 if (bt2
== VT_PTR
) {
1556 type2
= pointed_type(type2
);
1557 } else if (bt2
!= VT_FUNC
) {
1559 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1561 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1562 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1566 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1567 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1568 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1569 /* gcc-like error if '-' is used */
1571 goto invalid_operands
;
1573 tcc_warning("comparison of distinct pointer types lacks a cast");
1577 /* generic gen_op: handles types problems */
1578 ST_FUNC
void gen_op(int op
)
1580 int u
, t1
, t2
, bt1
, bt2
, t
;
1583 t1
= vtop
[-1].type
.t
;
1584 t2
= vtop
[0].type
.t
;
1585 bt1
= t1
& VT_BTYPE
;
1586 bt2
= t2
& VT_BTYPE
;
1588 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1589 /* at least one operand is a pointer */
1590 /* relationnal op: must be both pointers */
1591 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1592 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1593 /* pointers are handled are unsigned */
1594 #ifdef TCC_TARGET_X86_64
1595 t
= VT_LLONG
| VT_UNSIGNED
;
1597 t
= VT_INT
| VT_UNSIGNED
;
1601 /* if both pointers, then it must be the '-' op */
1602 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1604 tcc_error("cannot use pointers here");
1605 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1606 /* XXX: check that types are compatible */
1607 if (vtop
[-1].type
.t
& VT_VLA
) {
1608 vla_runtime_pointed_size(&vtop
[-1].type
);
1610 vpushi(pointed_size(&vtop
[-1].type
));
1614 /* set to integer type */
1615 #ifdef TCC_TARGET_X86_64
1616 vtop
->type
.t
= VT_LLONG
;
1618 vtop
->type
.t
= VT_INT
;
1623 /* exactly one pointer : must be '+' or '-'. */
1624 if (op
!= '-' && op
!= '+')
1625 tcc_error("cannot use pointers here");
1626 /* Put pointer as first operand */
1627 if (bt2
== VT_PTR
) {
1631 type1
= vtop
[-1].type
;
1632 type1
.t
&= ~VT_ARRAY
;
1633 if (vtop
[-1].type
.t
& VT_VLA
)
1634 vla_runtime_pointed_size(&vtop
[-1].type
);
1636 u
= pointed_size(&vtop
[-1].type
);
1638 tcc_error("unknown array element size");
1639 #ifdef TCC_TARGET_X86_64
1642 /* XXX: cast to int ? (long long case) */
1647 #ifdef CONFIG_TCC_BCHECK
1648 /* if evaluating constant expression, no code should be
1649 generated, so no bound check */
1650 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1651 /* if bounded pointers, we generate a special code to
1658 gen_bounded_ptr_add();
1664 /* put again type if gen_opic() swaped operands */
1667 } else if (is_float(bt1
) || is_float(bt2
)) {
1668 /* compute bigger type and do implicit casts */
1669 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1671 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1676 /* floats can only be used for a few operations */
1677 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1678 (op
< TOK_ULT
|| op
> TOK_GT
))
1679 tcc_error("invalid operands for binary operation");
1681 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1682 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1683 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1686 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1687 /* cast to biggest op */
1689 /* convert to unsigned if it does not fit in a long long */
1690 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1691 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1695 /* integer operations */
1697 /* convert to unsigned if it does not fit in an integer */
1698 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1699 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1702 /* XXX: currently, some unsigned operations are explicit, so
1703 we modify them here */
1704 if (t
& VT_UNSIGNED
) {
1711 else if (op
== TOK_LT
)
1713 else if (op
== TOK_GT
)
1715 else if (op
== TOK_LE
)
1717 else if (op
== TOK_GE
)
1724 /* special case for shifts and long long: we keep the shift as
1726 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1733 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1734 /* relationnal op: the result is an int */
1735 vtop
->type
.t
= VT_INT
;
1742 #ifndef TCC_TARGET_ARM
1743 /* generic itof for unsigned long long case */
1744 static void gen_cvt_itof1(int t
)
1746 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1747 (VT_LLONG
| VT_UNSIGNED
)) {
1750 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1751 #if LDOUBLE_SIZE != 8
1752 else if (t
== VT_LDOUBLE
)
1753 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1756 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1760 vtop
->r
= reg_fret(t
);
1767 /* generic ftoi for unsigned long long case */
1768 static void gen_cvt_ftoi1(int t
)
1772 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1773 /* not handled natively */
1774 st
= vtop
->type
.t
& VT_BTYPE
;
1776 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1777 #if LDOUBLE_SIZE != 8
1778 else if (st
== VT_LDOUBLE
)
1779 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1782 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1787 vtop
->r2
= REG_LRET
;
1793 /* force char or short cast */
1794 static void force_charshort_cast(int t
)
1798 /* XXX: add optimization if lvalue : just change type and offset */
1803 if (t
& VT_UNSIGNED
) {
1804 vpushi((1 << bits
) - 1);
1810 /* result must be signed or the SAR is converted to an SHL
1811 This was not the case when "t" was a signed short
1812 and the last value on the stack was an unsigned int */
1813 vtop
->type
.t
&= ~VT_UNSIGNED
;
1819 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1820 static void gen_cast(CType
*type
)
1822 int sbt
, dbt
, sf
, df
, c
, p
;
1824 /* special delayed cast for char/short */
1825 /* XXX: in some cases (multiple cascaded casts), it may still
1827 if (vtop
->r
& VT_MUSTCAST
) {
1828 vtop
->r
&= ~VT_MUSTCAST
;
1829 force_charshort_cast(vtop
->type
.t
);
1832 /* bitfields first get cast to ints */
1833 if (vtop
->type
.t
& VT_BITFIELD
) {
1837 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1838 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1843 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1844 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1846 /* constant case: we can do it now */
1847 /* XXX: in ISOC, cannot do it if error in convert */
1848 if (sbt
== VT_FLOAT
)
1849 vtop
->c
.ld
= vtop
->c
.f
;
1850 else if (sbt
== VT_DOUBLE
)
1851 vtop
->c
.ld
= vtop
->c
.d
;
1854 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1855 if (sbt
& VT_UNSIGNED
)
1856 vtop
->c
.ld
= vtop
->c
.ull
;
1858 vtop
->c
.ld
= vtop
->c
.ll
;
1860 if (sbt
& VT_UNSIGNED
)
1861 vtop
->c
.ld
= vtop
->c
.ui
;
1863 vtop
->c
.ld
= vtop
->c
.i
;
1866 if (dbt
== VT_FLOAT
)
1867 vtop
->c
.f
= (float)vtop
->c
.ld
;
1868 else if (dbt
== VT_DOUBLE
)
1869 vtop
->c
.d
= (double)vtop
->c
.ld
;
1870 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1871 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1872 } else if (sf
&& dbt
== VT_BOOL
) {
1873 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1876 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1877 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1878 vtop
->c
.ll
= vtop
->c
.ull
;
1879 else if (sbt
& VT_UNSIGNED
)
1880 vtop
->c
.ll
= vtop
->c
.ui
;
1881 #ifdef TCC_TARGET_X86_64
1882 else if (sbt
== VT_PTR
)
1885 else if (sbt
!= VT_LLONG
)
1886 vtop
->c
.ll
= vtop
->c
.i
;
1888 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1889 vtop
->c
.ull
= vtop
->c
.ll
;
1890 else if (dbt
== VT_BOOL
)
1891 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1892 else if (dbt
!= VT_LLONG
) {
1894 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1896 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1899 if(dbt
& VT_UNSIGNED
)
1900 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1902 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1905 } else if (p
&& dbt
== VT_BOOL
) {
1908 } else if (!nocode_wanted
) {
1909 /* non constant case: generate code */
1911 /* convert from fp to fp */
1914 /* convert int to fp */
1917 /* convert fp to int */
1918 if (dbt
== VT_BOOL
) {
1922 /* we handle char/short/etc... with generic code */
1923 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1924 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1928 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1929 /* additional cast for char/short... */
1934 #ifndef TCC_TARGET_X86_64
1935 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1936 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1937 /* scalar to long long */
1938 /* machine independent conversion */
1940 /* generate high word */
1941 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1945 if (sbt
== VT_PTR
) {
1946 /* cast from pointer to int before we apply
1947 shift operation, which pointers don't support*/
1948 gen_cast(&int_type
);
1954 /* patch second register */
1955 vtop
[-1].r2
= vtop
->r
;
1959 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1960 (dbt
& VT_BTYPE
) == VT_PTR
||
1961 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1962 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1963 (sbt
& VT_BTYPE
) != VT_PTR
&&
1964 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1965 /* need to convert from 32bit to 64bit */
1967 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1968 /* x86_64 specific: movslq */
1970 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1974 } else if (dbt
== VT_BOOL
) {
1975 /* scalar to bool */
1978 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1979 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1980 if (sbt
== VT_PTR
) {
1981 vtop
->type
.t
= VT_INT
;
1982 tcc_warning("nonportable conversion from pointer to char/short");
1984 force_charshort_cast(dbt
);
1985 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1987 if (sbt
== VT_LLONG
) {
1988 /* from long long: just take low order word */
1992 /* if lvalue and single word type, nothing to do because
1993 the lvalue already contains the real type size (see
1994 VT_LVAL_xxx constants) */
1997 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1998 /* if we are casting between pointer types,
1999 we must update the VT_LVAL_xxx size */
2000 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2001 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2006 /* return type size as known at compile time. Put alignment at 'a' */
2007 ST_FUNC
int type_size(CType
*type
, int *a
)
2012 bt
= type
->t
& VT_BTYPE
;
2013 if (bt
== VT_STRUCT
) {
2018 } else if (bt
== VT_PTR
) {
2019 if (type
->t
& VT_ARRAY
) {
2023 ts
= type_size(&s
->type
, a
);
2025 if (ts
< 0 && s
->c
< 0)
2033 } else if (bt
== VT_LDOUBLE
) {
2035 return LDOUBLE_SIZE
;
2036 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2037 #ifdef TCC_TARGET_I386
2038 #ifdef TCC_TARGET_PE
2043 #elif defined(TCC_TARGET_ARM)
2053 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2056 } else if (bt
== VT_SHORT
) {
2060 /* char, void, function, _Bool */
2066 /* push type size as known at runtime time on top of value stack. Put
2068 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2070 if (type
->t
& VT_VLA
) {
2071 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2073 vpushi(type_size(type
, a
));
2077 /* return the pointed type of t */
2078 static inline CType
*pointed_type(CType
*type
)
2080 return &type
->ref
->type
;
2083 /* modify type so that its it is a pointer to type. */
2084 ST_FUNC
void mk_pointer(CType
*type
)
2087 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2088 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2092 /* compare function types. OLD functions match any new functions */
2093 static int is_compatible_func(CType
*type1
, CType
*type2
)
2099 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2101 /* check func_call */
2102 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2104 /* XXX: not complete */
2105 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2109 while (s1
!= NULL
) {
2112 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2122 /* return true if type1 and type2 are the same. If unqualified is
2123 true, qualifiers on the types are ignored.
2125 - enums are not checked as gcc __builtin_types_compatible_p ()
2127 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2131 t1
= type1
->t
& VT_TYPE
;
2132 t2
= type2
->t
& VT_TYPE
;
2134 /* strip qualifiers before comparing */
2135 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2136 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2138 /* XXX: bitfields ? */
2141 /* test more complicated cases */
2142 bt1
= t1
& VT_BTYPE
;
2143 if (bt1
== VT_PTR
) {
2144 type1
= pointed_type(type1
);
2145 type2
= pointed_type(type2
);
2146 return is_compatible_types(type1
, type2
);
2147 } else if (bt1
== VT_STRUCT
) {
2148 return (type1
->ref
== type2
->ref
);
2149 } else if (bt1
== VT_FUNC
) {
2150 return is_compatible_func(type1
, type2
);
2156 /* return true if type1 and type2 are exactly the same (including
2159 static int is_compatible_types(CType
*type1
, CType
*type2
)
2161 return compare_types(type1
,type2
,0);
2164 /* return true if type1 and type2 are the same (ignoring qualifiers).
2166 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2168 return compare_types(type1
,type2
,1);
2171 /* print a type. If 'varstr' is not NULL, then the variable is also
2172 printed in the type */
2174 /* XXX: add array and function pointers */
2175 static void type_to_str(char *buf
, int buf_size
,
2176 CType
*type
, const char *varstr
)
2183 t
= type
->t
& VT_TYPE
;
2186 if (t
& VT_CONSTANT
)
2187 pstrcat(buf
, buf_size
, "const ");
2188 if (t
& VT_VOLATILE
)
2189 pstrcat(buf
, buf_size
, "volatile ");
2190 if (t
& VT_UNSIGNED
)
2191 pstrcat(buf
, buf_size
, "unsigned ");
2221 tstr
= "long double";
2223 pstrcat(buf
, buf_size
, tstr
);
2227 if (bt
== VT_STRUCT
)
2231 pstrcat(buf
, buf_size
, tstr
);
2232 v
= type
->ref
->v
& ~SYM_STRUCT
;
2233 if (v
>= SYM_FIRST_ANOM
)
2234 pstrcat(buf
, buf_size
, "<anonymous>");
2236 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2240 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2241 pstrcat(buf
, buf_size
, "(");
2243 while (sa
!= NULL
) {
2244 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2245 pstrcat(buf
, buf_size
, buf1
);
2248 pstrcat(buf
, buf_size
, ", ");
2250 pstrcat(buf
, buf_size
, ")");
2254 pstrcpy(buf1
, sizeof(buf1
), "*");
2256 pstrcat(buf1
, sizeof(buf1
), varstr
);
2257 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2261 pstrcat(buf
, buf_size
, " ");
2262 pstrcat(buf
, buf_size
, varstr
);
2267 /* verify type compatibility to store vtop in 'dt' type, and generate
2269 static void gen_assign_cast(CType
*dt
)
2271 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2272 char buf1
[256], buf2
[256];
2275 st
= &vtop
->type
; /* source type */
2276 dbt
= dt
->t
& VT_BTYPE
;
2277 sbt
= st
->t
& VT_BTYPE
;
2279 tcc_error("Cannot assign void value");
2280 if (dt
->t
& VT_CONSTANT
)
2281 tcc_warning("assignment of read-only location");
2284 /* special cases for pointers */
2285 /* '0' can also be a pointer */
2286 if (is_null_pointer(vtop
))
2288 /* accept implicit pointer to integer cast with warning */
2289 if (is_integer_btype(sbt
)) {
2290 tcc_warning("assignment makes pointer from integer without a cast");
2293 type1
= pointed_type(dt
);
2294 /* a function is implicitely a function pointer */
2295 if (sbt
== VT_FUNC
) {
2296 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2297 !is_compatible_types(pointed_type(dt
), st
))
2298 tcc_warning("assignment from incompatible pointer type");
2303 type2
= pointed_type(st
);
2304 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2305 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2306 /* void * can match anything */
2308 /* exact type match, except for unsigned */
2311 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2312 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2313 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2314 tcc_warning("assignment from incompatible pointer type");
2316 /* check const and volatile */
2317 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2318 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2319 tcc_warning("assignment discards qualifiers from pointer target type");
2325 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2326 tcc_warning("assignment makes integer from pointer without a cast");
2328 /* XXX: more tests */
2333 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2334 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2335 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2337 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2338 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2339 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2347 /* store vtop in lvalue pushed on stack */
2348 ST_FUNC
void vstore(void)
2350 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2352 ft
= vtop
[-1].type
.t
;
2353 sbt
= vtop
->type
.t
& VT_BTYPE
;
2354 dbt
= ft
& VT_BTYPE
;
2355 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2356 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2357 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2358 /* optimize char/short casts */
2359 delayed_cast
= VT_MUSTCAST
;
2360 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2361 /* XXX: factorize */
2362 if (ft
& VT_CONSTANT
)
2363 tcc_warning("assignment of read-only location");
2366 if (!(ft
& VT_BITFIELD
))
2367 gen_assign_cast(&vtop
[-1].type
);
2370 if (sbt
== VT_STRUCT
) {
2371 /* if structure, only generate pointer */
2372 /* structure assignment : generate memcpy */
2373 /* XXX: optimize if small size */
2374 if (!nocode_wanted
) {
2375 size
= type_size(&vtop
->type
, &align
);
2379 vtop
->type
.t
= VT_PTR
;
2382 /* address of memcpy() */
2385 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2386 else if(!(align
& 3))
2387 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2390 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2395 vtop
->type
.t
= VT_PTR
;
2404 /* leave source on stack */
2405 } else if (ft
& VT_BITFIELD
) {
2406 /* bitfield store handling */
2407 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2408 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2409 /* remove bit field info to avoid loops */
2410 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2412 /* duplicate source into other register */
2417 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2418 gen_cast(&vtop
[-1].type
);
2419 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2422 /* duplicate destination */
2424 vtop
[-1] = vtop
[-2];
2426 /* mask and shift source */
2427 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2428 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2429 vpushll((1ULL << bit_size
) - 1ULL);
2431 vpushi((1 << bit_size
) - 1);
2437 /* load destination, mask and or with source */
2439 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2440 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2442 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2449 /* pop off shifted source from "duplicate source..." above */
2453 #ifdef CONFIG_TCC_BCHECK
2454 /* bound check case */
2455 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2461 if (!nocode_wanted
) {
2465 #ifdef TCC_TARGET_X86_64
2466 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2471 r
= gv(rc
); /* generate value */
2472 /* if lvalue was saved on stack, must read it */
2473 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2475 t
= get_reg(RC_INT
);
2476 #ifdef TCC_TARGET_X86_64
2481 sv
.r
= VT_LOCAL
| VT_LVAL
;
2482 sv
.c
.ul
= vtop
[-1].c
.ul
;
2484 vtop
[-1].r
= t
| VT_LVAL
;
2487 #ifndef TCC_TARGET_X86_64
2488 /* two word case handling : store second register at word + 4 */
2489 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2491 /* convert to int to increment easily */
2492 vtop
->type
.t
= VT_INT
;
2498 /* XXX: it works because r2 is spilled last ! */
2499 store(vtop
->r2
, vtop
- 1);
2504 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2505 vtop
->r
|= delayed_cast
;
2509 /* post defines POST/PRE add. c is the token ++ or -- */
2510 ST_FUNC
void inc(int post
, int c
)
2513 vdup(); /* save lvalue */
2515 gv_dup(); /* duplicate value */
2520 vpushi(c
- TOK_MID
);
2522 vstore(); /* store value */
2524 vpop(); /* if post op, return saved value */
2527 /* Parse GNUC __attribute__ extension. Currently, the following
2528 extensions are recognized:
2529 - aligned(n) : set data/function alignment.
2530 - packed : force data alignment to 1
2531 - section(x) : generate data/code in this section.
2532 - unused : currently ignored, but may be used someday.
2533 - regparm(n) : pass function parameters in registers (i386 only)
2535 static void parse_attribute(AttributeDef
*ad
)
2539 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2543 while (tok
!= ')') {
2544 if (tok
< TOK_IDENT
)
2545 expect("attribute name");
2553 expect("section name");
2554 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2562 expect("alias(\"target\")");
2563 ad
->alias_target
= /* save string as token, for later */
2564 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2573 if (n
<= 0 || (n
& (n
- 1)) != 0)
2574 tcc_error("alignment must be a positive power of two");
2591 /* currently, no need to handle it because tcc does not
2592 track unused objects */
2596 /* currently, no need to handle it because tcc does not
2597 track unused objects */
2602 ad
->func_call
= FUNC_CDECL
;
2607 ad
->func_call
= FUNC_STDCALL
;
2609 #ifdef TCC_TARGET_I386
2619 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2625 ad
->func_call
= FUNC_FASTCALLW
;
2632 ad
->mode
= VT_LLONG
+ 1;
2635 ad
->mode
= VT_SHORT
+ 1;
2638 ad
->mode
= VT_INT
+ 1;
2641 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2648 ad
->func_export
= 1;
2651 ad
->func_import
= 1;
2654 if (tcc_state
->warn_unsupported
)
2655 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2656 /* skip parameters */
2658 int parenthesis
= 0;
2662 else if (tok
== ')')
2665 } while (parenthesis
&& tok
!= -1);
2678 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2679 static void struct_decl(CType
*type
, int u
)
2681 int a
, v
, size
, align
, maxalign
, c
, offset
;
2682 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2683 Sym
*s
, *ss
, *ass
, **ps
;
2687 a
= tok
; /* save decl type */
2692 /* struct already defined ? return it */
2694 expect("struct/union/enum name");
2698 tcc_error("invalid type");
2705 /* we put an undefined size for struct/union */
2706 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2707 s
->r
= 0; /* default alignment is zero as gcc */
2708 /* put struct/union/enum name in type */
2716 tcc_error("struct/union/enum already defined");
2717 /* cannot be empty */
2719 /* non empty enums are not allowed */
2720 if (a
== TOK_ENUM
) {
2724 expect("identifier");
2730 /* enum symbols have static storage */
2731 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2732 ss
->type
.t
|= VT_STATIC
;
2737 /* NOTE: we accept a trailing comma */
2748 while (tok
!= '}') {
2749 parse_btype(&btype
, &ad
);
2755 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2756 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2757 expect("identifier");
2758 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2759 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2760 tcc_error("invalid type for '%s'",
2761 get_tok_str(v
, NULL
));
2765 bit_size
= expr_const();
2766 /* XXX: handle v = 0 case for messages */
2768 tcc_error("negative width in bit-field '%s'",
2769 get_tok_str(v
, NULL
));
2770 if (v
&& bit_size
== 0)
2771 tcc_error("zero width for bit-field '%s'",
2772 get_tok_str(v
, NULL
));
2774 size
= type_size(&type1
, &align
);
2776 if (align
< ad
.aligned
)
2778 } else if (ad
.packed
) {
2780 } else if (*tcc_state
->pack_stack_ptr
) {
2781 if (align
> *tcc_state
->pack_stack_ptr
)
2782 align
= *tcc_state
->pack_stack_ptr
;
2785 if (bit_size
>= 0) {
2786 bt
= type1
.t
& VT_BTYPE
;
2793 tcc_error("bitfields must have scalar type");
2795 if (bit_size
> bsize
) {
2796 tcc_error("width of '%s' exceeds its type",
2797 get_tok_str(v
, NULL
));
2798 } else if (bit_size
== bsize
) {
2799 /* no need for bit fields */
2801 } else if (bit_size
== 0) {
2802 /* XXX: what to do if only padding in a
2804 /* zero size: means to pad */
2807 /* we do not have enough room ?
2808 did the type change?
2810 if ((bit_pos
+ bit_size
) > bsize
||
2811 bt
!= prevbt
|| a
== TOK_UNION
)
2814 /* XXX: handle LSB first */
2815 type1
.t
|= VT_BITFIELD
|
2816 (bit_pos
<< VT_STRUCT_SHIFT
) |
2817 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2818 bit_pos
+= bit_size
;
2824 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2825 /* add new memory data only if starting
2827 if (lbit_pos
== 0) {
2828 if (a
== TOK_STRUCT
) {
2829 c
= (c
+ align
- 1) & -align
;
2838 if (align
> maxalign
)
2842 printf("add field %s offset=%d",
2843 get_tok_str(v
, NULL
), offset
);
2844 if (type1
.t
& VT_BITFIELD
) {
2845 printf(" pos=%d size=%d",
2846 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2847 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2852 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2854 while ((ass
= ass
->next
) != NULL
) {
2855 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2860 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2864 if (tok
== ';' || tok
== TOK_EOF
)
2871 /* store size and alignment */
2872 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2878 /* return 0 if no type declaration. otherwise, return the basic type
2881 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2883 int t
, u
, type_found
, typespec_found
, typedef_found
;
2887 memset(ad
, 0, sizeof(AttributeDef
));
2895 /* currently, we really ignore extension */
2905 if ((t
& VT_BTYPE
) != 0)
2906 tcc_error("too many basic types");
2922 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2923 #ifndef TCC_TARGET_PE
2924 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2926 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2927 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2941 if ((t
& VT_BTYPE
) == VT_LONG
) {
2942 #ifdef TCC_TARGET_PE
2943 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2945 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2953 struct_decl(&type1
, VT_ENUM
);
2956 type
->ref
= type1
.ref
;
2960 struct_decl(&type1
, VT_STRUCT
);
2963 /* type modifiers */
3016 /* GNUC attribute */
3017 case TOK_ATTRIBUTE1
:
3018 case TOK_ATTRIBUTE2
:
3019 parse_attribute(ad
);
3022 t
= (t
& ~VT_BTYPE
) | u
;
3030 parse_expr_type(&type1
);
3031 /* remove all storage modifiers except typedef */
3032 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3035 if (typespec_found
|| typedef_found
)
3038 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3041 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3042 type
->ref
= s
->type
.ref
;
3044 /* get attributes from typedef */
3045 if (0 == ad
->aligned
)
3046 ad
->aligned
= FUNC_ALIGN(s
->r
);
3047 if (0 == ad
->func_call
)
3048 ad
->func_call
= FUNC_CALL(s
->r
);
3049 ad
->packed
|= FUNC_PACKED(s
->r
);
3058 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3059 tcc_error("signed and unsigned modifier");
3060 if (tcc_state
->char_is_unsigned
) {
3061 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3066 /* long is never used as type */
3067 if ((t
& VT_BTYPE
) == VT_LONG
)
3068 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3069 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3071 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3077 /* convert a function parameter type (array to pointer and function to
3078 function pointer) */
3079 static inline void convert_parameter_type(CType
*pt
)
3081 /* remove const and volatile qualifiers (XXX: const could be used
3082 to indicate a const function parameter */
3083 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3084 /* array must be transformed to pointer according to ANSI C */
3086 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3091 ST_FUNC
void parse_asm_str(CString
*astr
)
3094 /* read the string */
3096 expect("string constant");
3098 while (tok
== TOK_STR
) {
3099 /* XXX: add \0 handling too ? */
3100 cstr_cat(astr
, tokc
.cstr
->data
);
3103 cstr_ccat(astr
, '\0');
3106 /* Parse an asm label and return the label
3107 * Don't forget to free the CString in the caller! */
3108 static void asm_label_instr(CString
*astr
)
3111 parse_asm_str(astr
);
3114 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3118 static void post_type(CType
*type
, AttributeDef
*ad
)
3120 int n
, l
, t1
, arg_size
, align
;
3121 Sym
**plast
, *s
, *first
;
3126 /* function declaration */
3134 /* read param name and compute offset */
3135 if (l
!= FUNC_OLD
) {
3136 if (!parse_btype(&pt
, &ad1
)) {
3138 tcc_error("invalid type");
3145 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3147 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3148 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3149 tcc_error("parameter declared as void");
3150 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3155 expect("identifier");
3159 convert_parameter_type(&pt
);
3160 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3166 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3173 /* if no parameters, then old type prototype */
3177 /* NOTE: const is ignored in returned type as it has a special
3178 meaning in gcc / C++ */
3179 type
->t
&= ~VT_CONSTANT
;
3180 /* some ancient pre-K&R C allows a function to return an array
3181 and the array brackets to be put after the arguments, such
3182 that "int c()[]" means something like "int[] c()" */
3185 skip(']'); /* only handle simple "[]" */
3188 /* we push a anonymous symbol which will contain the function prototype */
3189 ad
->func_args
= arg_size
;
3190 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3194 } else if (tok
== '[') {
3195 /* array definition */
3197 if (tok
== TOK_RESTRICT1
)
3202 if (!local_stack
|| nocode_wanted
)
3203 vpushi(expr_const());
3205 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3208 tcc_error("invalid array size");
3210 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3211 tcc_error("size of variable length array should be an integer");
3216 /* parse next post type */
3217 post_type(type
, ad
);
3218 t1
|= type
->t
& VT_VLA
;
3221 loc
-= type_size(&int_type
, &align
);
3225 vla_runtime_type_size(type
, &align
);
3227 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3234 /* we push an anonymous symbol which will contain the array
3236 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3237 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3242 /* Parse a type declaration (except basic type), and return the type
3243 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3244 expected. 'type' should contain the basic type. 'ad' is the
3245 attribute definition of the basic type. It can be modified by
3248 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3251 CType type1
, *type2
;
3252 int qualifiers
, storage
;
3254 while (tok
== '*') {
3262 qualifiers
|= VT_CONSTANT
;
3267 qualifiers
|= VT_VOLATILE
;
3275 type
->t
|= qualifiers
;
3278 /* XXX: clarify attribute handling */
3279 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3280 parse_attribute(ad
);
3282 /* recursive type */
3283 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3284 type1
.t
= 0; /* XXX: same as int */
3287 /* XXX: this is not correct to modify 'ad' at this point, but
3288 the syntax is not clear */
3289 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3290 parse_attribute(ad
);
3291 type_decl(&type1
, ad
, v
, td
);
3294 /* type identifier */
3295 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3299 if (!(td
& TYPE_ABSTRACT
))
3300 expect("identifier");
3304 storage
= type
->t
& VT_STORAGE
;
3305 type
->t
&= ~VT_STORAGE
;
3306 post_type(type
, ad
);
3308 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3309 parse_attribute(ad
);
3313 /* append type at the end of type1 */
3326 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3327 ST_FUNC
int lvalue_type(int t
)
3332 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3334 else if (bt
== VT_SHORT
)
3338 if (t
& VT_UNSIGNED
)
3339 r
|= VT_LVAL_UNSIGNED
;
3343 /* indirection with full error checking and bound check */
3344 ST_FUNC
void indir(void)
3346 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3347 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3351 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3353 vtop
->type
= *pointed_type(&vtop
->type
);
3354 /* Arrays and functions are never lvalues */
3355 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3356 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3357 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3358 /* if bound checking, the referenced pointer must be checked */
3359 #ifdef CONFIG_TCC_BCHECK
3360 if (tcc_state
->do_bounds_check
)
3361 vtop
->r
|= VT_MUSTBOUND
;
3366 /* pass a parameter to a function and do type checking and casting */
3367 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3372 func_type
= func
->c
;
3373 if (func_type
== FUNC_OLD
||
3374 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3375 /* default casting : only need to convert float to double */
3376 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3380 } else if (arg
== NULL
) {
3381 tcc_error("too many arguments to function");
3384 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3385 gen_assign_cast(&type
);
3389 /* parse an expression of the form '(type)' or '(expr)' and return its
3391 static void parse_expr_type(CType
*type
)
3397 if (parse_btype(type
, &ad
)) {
3398 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3405 static void parse_type(CType
*type
)
3410 if (!parse_btype(type
, &ad
)) {
3413 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3416 static void vpush_tokc(int t
)
3421 vsetc(&type
, VT_CONST
, &tokc
);
3424 ST_FUNC
void unary(void)
3426 int n
, t
, align
, size
, r
, sizeof_caller
;
3430 static int in_sizeof
= 0;
3432 sizeof_caller
= in_sizeof
;
3434 /* XXX: GCC 2.95.3 does not generate a table although it should be
3448 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3452 vpush_tokc(VT_LLONG
);
3456 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3460 vpush_tokc(VT_FLOAT
);
3464 vpush_tokc(VT_DOUBLE
);
3468 vpush_tokc(VT_LDOUBLE
);
3471 case TOK___FUNCTION__
:
3473 goto tok_identifier
;
3479 /* special function name identifier */
3480 len
= strlen(funcname
) + 1;
3481 /* generate char[len] type */
3486 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3487 ptr
= section_ptr_add(data_section
, len
);
3488 memcpy(ptr
, funcname
, len
);
3493 #ifdef TCC_TARGET_PE
3494 t
= VT_SHORT
| VT_UNSIGNED
;
3500 /* string parsing */
3503 if (tcc_state
->warn_write_strings
)
3508 memset(&ad
, 0, sizeof(AttributeDef
));
3509 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3514 if (parse_btype(&type
, &ad
)) {
3515 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3517 /* check ISOC99 compound literal */
3519 /* data is allocated locally by default */
3524 /* all except arrays are lvalues */
3525 if (!(type
.t
& VT_ARRAY
))
3526 r
|= lvalue_type(type
.t
);
3527 memset(&ad
, 0, sizeof(AttributeDef
));
3528 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3530 if (sizeof_caller
) {
3537 } else if (tok
== '{') {
3538 /* save all registers */
3540 /* statement expression : we do not accept break/continue
3541 inside as GCC does */
3542 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3557 /* functions names must be treated as function pointers,
3558 except for unary '&' and sizeof. Since we consider that
3559 functions are not lvalues, we only have to handle it
3560 there and in function calls. */
3561 /* arrays can also be used although they are not lvalues */
3562 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3563 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3565 mk_pointer(&vtop
->type
);
3571 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3573 boolean
.t
= VT_BOOL
;
3575 vtop
->c
.i
= !vtop
->c
.i
;
3576 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3577 vtop
->c
.i
= vtop
->c
.i
^ 1;
3580 vseti(VT_JMP
, gtst(1, 0));
3591 /* in order to force cast, we add zero */
3593 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3594 tcc_error("pointer not accepted for unary plus");
3604 unary_type(&type
); // Perform a in_sizeof = 0;
3605 size
= type_size(&type
, &align
);
3606 if (t
== TOK_SIZEOF
) {
3607 if (!(type
.t
& VT_VLA
)) {
3609 tcc_error("sizeof applied to an incomplete type");
3612 vla_runtime_type_size(&type
, &align
);
3617 vtop
->type
.t
|= VT_UNSIGNED
;
3620 case TOK_builtin_types_compatible_p
:
3629 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3630 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3631 vpushi(is_compatible_types(&type1
, &type2
));
3634 case TOK_builtin_constant_p
:
3636 int saved_nocode_wanted
, res
;
3639 saved_nocode_wanted
= nocode_wanted
;
3642 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3644 nocode_wanted
= saved_nocode_wanted
;
3649 case TOK_builtin_frame_address
:
3654 if (tok
!= TOK_CINT
) {
3655 tcc_error("__builtin_frame_address only takes integers");
3658 tcc_error("TCC only supports __builtin_frame_address(0)");
3664 vset(&type
, VT_LOCAL
, 0);
3667 #ifdef TCC_TARGET_X86_64
3668 case TOK_builtin_va_arg_types
:
3670 /* This definition must be synced with stdarg.h */
3671 enum __va_arg_type
{
3672 __va_gen_reg
, __va_float_reg
, __va_stack
3680 bt
= type
.t
& VT_BTYPE
;
3681 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3683 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3684 vpushi(__va_float_reg
);
3686 vpushi(__va_gen_reg
);
3706 goto tok_identifier
;
3708 /* allow to take the address of a label */
3709 if (tok
< TOK_UIDENT
)
3710 expect("label identifier");
3711 s
= label_find(tok
);
3713 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3715 if (s
->r
== LABEL_DECLARED
)
3716 s
->r
= LABEL_FORWARD
;
3719 s
->type
.t
= VT_VOID
;
3720 mk_pointer(&s
->type
);
3721 s
->type
.t
|= VT_STATIC
;
3723 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3728 // special qnan , snan and infinity values
3730 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3734 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3738 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3747 expect("identifier");
3751 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3752 /* for simple function calls, we tolerate undeclared
3753 external reference to int() function */
3754 if (tcc_state
->warn_implicit_function_declaration
)
3755 tcc_warning("implicit declaration of function '%s'",
3756 get_tok_str(t
, NULL
));
3757 s
= external_global_sym(t
, &func_old_type
, 0);
3759 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3760 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3761 /* if referencing an inline function, then we generate a
3762 symbol to it if not already done. It will have the
3763 effect to generate code for it at the end of the
3764 compilation unit. Inline function as always
3765 generated in the text section. */
3767 put_extern_sym(s
, text_section
, 0, 0);
3768 r
= VT_SYM
| VT_CONST
;
3772 vset(&s
->type
, r
, s
->c
);
3773 /* if forward reference, we must point to s */
3774 if (vtop
->r
& VT_SYM
) {
3781 /* post operations */
3783 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3786 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3789 if (tok
== TOK_ARROW
)
3791 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3795 /* expect pointer on structure */
3796 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3797 expect("struct or union");
3801 while ((s
= s
->next
) != NULL
) {
3806 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3807 /* add field offset to pointer */
3808 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3811 /* change type to field type, and set to lvalue */
3812 vtop
->type
= s
->type
;
3813 vtop
->type
.t
|= qualifiers
;
3814 /* an array is never an lvalue */
3815 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3816 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3817 #ifdef CONFIG_TCC_BCHECK
3818 /* if bound checking, the referenced pointer must be checked */
3819 if (tcc_state
->do_bounds_check
)
3820 vtop
->r
|= VT_MUSTBOUND
;
3824 } else if (tok
== '[') {
3830 } else if (tok
== '(') {
3836 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3837 /* pointer test (no array accepted) */
3838 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3839 vtop
->type
= *pointed_type(&vtop
->type
);
3840 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3844 expect("function pointer");
3847 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3849 /* get return type */
3852 sa
= s
->next
; /* first parameter */
3855 /* compute first implicit argument if a structure is returned */
3856 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3857 /* get some space for the returned structure */
3858 size
= type_size(&s
->type
, &align
);
3859 loc
= (loc
- size
) & -align
;
3861 ret
.r
= VT_LOCAL
| VT_LVAL
;
3862 /* pass it as 'int' to avoid structure arg passing
3864 vseti(VT_LOCAL
, loc
);
3869 /* return in register */
3870 if (is_float(ret
.type
.t
)) {
3871 ret
.r
= reg_fret(ret
.type
.t
);
3873 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3882 gfunc_param_typed(s
, sa
);
3892 tcc_error("too few arguments to function");
3894 if (!nocode_wanted
) {
3895 gfunc_call(nb_args
);
3897 vtop
-= (nb_args
+ 1);
3900 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3908 ST_FUNC
void expr_prod(void)
3913 while (tok
== '*' || tok
== '/' || tok
== '%') {
3921 ST_FUNC
void expr_sum(void)
3926 while (tok
== '+' || tok
== '-') {
3934 static void expr_shift(void)
3939 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3947 static void expr_cmp(void)
3952 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3953 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3961 static void expr_cmpeq(void)
3966 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3974 static void expr_and(void)
3977 while (tok
== '&') {
3984 static void expr_xor(void)
3987 while (tok
== '^') {
3994 static void expr_or(void)
3997 while (tok
== '|') {
4004 /* XXX: fix this mess */
4005 static void expr_land_const(void)
4008 while (tok
== TOK_LAND
) {
4015 /* XXX: fix this mess */
4016 static void expr_lor_const(void)
4019 while (tok
== TOK_LOR
) {
4026 /* only used if non constant */
4027 static void expr_land(void)
4032 if (tok
== TOK_LAND
) {
4037 if (tok
!= TOK_LAND
) {
4047 static void expr_lor(void)
4052 if (tok
== TOK_LOR
) {
4057 if (tok
!= TOK_LOR
) {
4067 /* XXX: better constant handling */
4068 static void expr_cond(void)
4070 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4072 CType type
, type1
, type2
;
4079 boolean
.t
= VT_BOOL
;
4085 if (tok
!= ':' || !gnu_ext
) {
4100 if (vtop
!= vstack
) {
4101 /* needed to avoid having different registers saved in
4103 if (is_float(vtop
->type
.t
)) {
4105 #ifdef TCC_TARGET_X86_64
4106 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4116 if (tok
== ':' && gnu_ext
) {
4124 sv
= *vtop
; /* save value to handle it later */
4125 vtop
--; /* no vpop so that FP stack is not flushed */
4133 bt1
= t1
& VT_BTYPE
;
4135 bt2
= t2
& VT_BTYPE
;
4136 /* cast operands to correct type according to ISOC rules */
4137 if (is_float(bt1
) || is_float(bt2
)) {
4138 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4139 type
.t
= VT_LDOUBLE
;
4140 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4145 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4146 /* cast to biggest op */
4148 /* convert to unsigned if it does not fit in a long long */
4149 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4150 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4151 type
.t
|= VT_UNSIGNED
;
4152 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4153 /* If one is a null ptr constant the result type
4155 if (is_null_pointer (vtop
))
4157 else if (is_null_pointer (&sv
))
4159 /* XXX: test pointer compatibility, C99 has more elaborate
4163 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4164 /* XXX: test function pointer compatibility */
4165 type
= bt1
== VT_FUNC
? type1
: type2
;
4166 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4167 /* XXX: test structure compatibility */
4168 type
= bt1
== VT_STRUCT
? type1
: type2
;
4169 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4170 /* NOTE: as an extension, we accept void on only one side */
4173 /* integer operations */
4175 /* convert to unsigned if it does not fit in an integer */
4176 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4177 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4178 type
.t
|= VT_UNSIGNED
;
4181 /* now we convert second operand */
4183 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4186 if (is_float(type
.t
)) {
4188 #ifdef TCC_TARGET_X86_64
4189 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4193 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4194 /* for long longs, we use fixed registers to avoid having
4195 to handle a complicated move */
4200 /* this is horrible, but we must also convert first
4204 /* put again first value and cast it */
4207 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4217 static void expr_eq(void)
4223 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4224 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4225 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4240 ST_FUNC
void gexpr(void)
4251 /* parse an expression and return its type without any side effect. */
4252 static void expr_type(CType
*type
)
4254 int saved_nocode_wanted
;
4256 saved_nocode_wanted
= nocode_wanted
;
4261 nocode_wanted
= saved_nocode_wanted
;
4264 /* parse a unary expression and return its type without any side
4266 static void unary_type(CType
*type
)
4278 /* parse a constant expression and return value in vtop. */
4279 static void expr_const1(void)
4288 /* parse an integer constant and return its value. */
4289 ST_FUNC
int expr_const(void)
4293 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4294 expect("constant expression");
4300 /* return the label token if current token is a label, otherwise
4302 static int is_label(void)
4306 /* fast test first */
4307 if (tok
< TOK_UIDENT
)
4309 /* no need to save tokc because tok is an identifier */
4316 unget_tok(last_tok
);
4321 static void label_or_decl(int l
)
4325 /* fast test first */
4326 if (tok
>= TOK_UIDENT
)
4328 /* no need to save tokc because tok is an identifier */
4332 unget_tok(last_tok
);
4335 unget_tok(last_tok
);
4340 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4341 int case_reg
, int is_expr
)
4346 /* generate line number info */
4347 if (tcc_state
->do_debug
&&
4348 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4349 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4351 last_line_num
= file
->line_num
;
4355 /* default return value is (void) */
4357 vtop
->type
.t
= VT_VOID
;
4360 if (tok
== TOK_IF
) {
4367 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4369 if (c
== TOK_ELSE
) {
4373 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4374 gsym(d
); /* patch else jmp */
4377 } else if (tok
== TOK_WHILE
) {
4385 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4389 } else if (tok
== '{') {
4393 /* record local declaration stack position */
4395 llabel
= local_label_stack
;
4396 /* handle local labels declarations */
4397 if (tok
== TOK_LABEL
) {
4400 if (tok
< TOK_UIDENT
)
4401 expect("label identifier");
4402 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4412 while (tok
!= '}') {
4413 label_or_decl(VT_LOCAL
);
4417 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4420 /* pop locally defined labels */
4421 label_pop(&local_label_stack
, llabel
);
4423 /* XXX: this solution makes only valgrind happy...
4424 triggered by gcc.c-torture/execute/20000917-1.c */
4426 switch(vtop
->type
.t
& VT_BTYPE
) {
4431 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4433 tcc_error("unsupported expression type");
4436 /* pop locally defined symbols */
4437 sym_pop(&local_stack
, s
);
4439 } else if (tok
== TOK_RETURN
) {
4443 gen_assign_cast(&func_vt
);
4444 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4446 /* if returning structure, must copy it to implicit
4447 first pointer arg location */
4450 size
= type_size(&func_vt
,&align
);
4453 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4457 loc
= (loc
- size
) & -4;
4460 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4463 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4465 vtop
->type
= int_type
;
4471 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4474 /* copy structure value to pointer */
4479 } else if (is_float(func_vt
.t
)) {
4480 gv(rc_fret(func_vt
.t
));
4484 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4487 rsym
= gjmp(rsym
); /* jmp */
4488 } else if (tok
== TOK_BREAK
) {
4491 tcc_error("cannot break");
4492 *bsym
= gjmp(*bsym
);
4495 } else if (tok
== TOK_CONTINUE
) {
4498 tcc_error("cannot continue");
4499 *csym
= gjmp(*csym
);
4502 } else if (tok
== TOK_FOR
) {
4508 /* c99 for-loop init decl? */
4509 if (!decl0(VT_LOCAL
, 1)) {
4510 /* no, regular for-loop init expr */
4534 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4538 sym_pop(&local_stack
, s
);
4540 if (tok
== TOK_DO
) {
4545 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4556 if (tok
== TOK_SWITCH
) {
4560 /* XXX: other types than integer */
4561 case_reg
= gv(RC_INT
);
4565 b
= gjmp(0); /* jump to first case */
4567 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4568 /* if no default, jmp after switch */
4576 if (tok
== TOK_CASE
) {
4583 if (gnu_ext
&& tok
== TOK_DOTS
) {
4587 tcc_warning("empty case range");
4589 /* since a case is like a label, we must skip it with a jmp */
4596 *case_sym
= gtst(1, 0);
4599 *case_sym
= gtst(1, 0);
4603 *case_sym
= gtst(1, *case_sym
);
4608 goto block_after_label
;
4610 if (tok
== TOK_DEFAULT
) {
4616 tcc_error("too many 'default'");
4619 goto block_after_label
;
4621 if (tok
== TOK_GOTO
) {
4623 if (tok
== '*' && gnu_ext
) {
4627 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4630 } else if (tok
>= TOK_UIDENT
) {
4631 s
= label_find(tok
);
4632 /* put forward definition if needed */
4634 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4636 if (s
->r
== LABEL_DECLARED
)
4637 s
->r
= LABEL_FORWARD
;
4639 /* label already defined */
4640 if (s
->r
& LABEL_FORWARD
)
4641 s
->jnext
= gjmp(s
->jnext
);
4643 gjmp_addr(s
->jnext
);
4646 expect("label identifier");
4649 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4657 if (s
->r
== LABEL_DEFINED
)
4658 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4660 s
->r
= LABEL_DEFINED
;
4662 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4665 /* we accept this, but it is a mistake */
4668 tcc_warning("deprecated use of label at end of compound statement");
4672 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4675 /* expression case */
4690 /* t is the array or struct type. c is the array or struct
4691 address. cur_index/cur_field is the pointer to the current
4692 value. 'size_only' is true if only size info is needed (only used
4694 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4695 int *cur_index
, Sym
**cur_field
,
4699 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4705 if (gnu_ext
&& (l
= is_label()) != 0)
4707 while (tok
== '[' || tok
== '.') {
4709 if (!(type
->t
& VT_ARRAY
))
4710 expect("array type");
4713 index
= expr_const();
4714 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4715 expect("invalid index");
4716 if (tok
== TOK_DOTS
&& gnu_ext
) {
4718 index_last
= expr_const();
4719 if (index_last
< 0 ||
4720 (s
->c
>= 0 && index_last
>= s
->c
) ||
4722 expect("invalid index");
4728 *cur_index
= index_last
;
4729 type
= pointed_type(type
);
4730 elem_size
= type_size(type
, &align
);
4731 c
+= index
* elem_size
;
4732 /* NOTE: we only support ranges for last designator */
4733 nb_elems
= index_last
- index
+ 1;
4734 if (nb_elems
!= 1) {
4743 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4744 expect("struct/union type");
4757 /* XXX: fix this mess by using explicit storage field */
4759 type1
.t
|= (type
->t
& ~VT_TYPE
);
4773 if (type
->t
& VT_ARRAY
) {
4775 type
= pointed_type(type
);
4776 c
+= index
* type_size(type
, &align
);
4780 tcc_error("too many field init");
4781 /* XXX: fix this mess by using explicit storage field */
4783 type1
.t
|= (type
->t
& ~VT_TYPE
);
4788 decl_initializer(type
, sec
, c
, 0, size_only
);
4790 /* XXX: make it more general */
4791 if (!size_only
&& nb_elems
> 1) {
4792 unsigned long c_end
;
4797 tcc_error("range init not supported yet for dynamic storage");
4798 c_end
= c
+ nb_elems
* elem_size
;
4799 if (c_end
> sec
->data_allocated
)
4800 section_realloc(sec
, c_end
);
4801 src
= sec
->data
+ c
;
4803 for(i
= 1; i
< nb_elems
; i
++) {
4805 memcpy(dst
, src
, elem_size
);
4811 #define EXPR_CONST 1
4814 /* store a value or an expression directly in global data or in local array */
4815 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4816 int v
, int expr_type
)
4818 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4820 unsigned long long bit_mask
;
4828 /* compound literals must be allocated globally in this case */
4829 saved_global_expr
= global_expr
;
4832 global_expr
= saved_global_expr
;
4833 /* NOTE: symbols are accepted */
4834 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4835 tcc_error("initializer element is not constant");
4843 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4846 /* XXX: not portable */
4847 /* XXX: generate error if incorrect relocation */
4848 gen_assign_cast(&dtype
);
4849 bt
= type
->t
& VT_BTYPE
;
4850 /* we'll write at most 12 bytes */
4851 if (c
+ 12 > sec
->data_allocated
) {
4852 section_realloc(sec
, c
+ 12);
4854 ptr
= sec
->data
+ c
;
4855 /* XXX: make code faster ? */
4856 if (!(type
->t
& VT_BITFIELD
)) {
4861 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4862 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4863 bit_mask
= (1LL << bit_size
) - 1;
4865 if ((vtop
->r
& VT_SYM
) &&
4871 (bt
== VT_INT
&& bit_size
!= 32)))
4872 tcc_error("initializer element is not computable at load time");
4875 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4877 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4880 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4883 *(double *)ptr
= vtop
->c
.d
;
4886 *(long double *)ptr
= vtop
->c
.ld
;
4889 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4892 if (vtop
->r
& VT_SYM
) {
4893 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4895 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4900 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4907 /* put zeros for variable based init */
4908 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4911 /* nothing to do because globals are already set to zero */
4913 vpush_global_sym(&func_old_type
, TOK_memset
);
4921 /* 't' contains the type and storage info. 'c' is the offset of the
4922 object in section 'sec'. If 'sec' is NULL, it means stack based
4923 allocation. 'first' is true if array '{' must be read (multi
4924 dimension implicit array init handling). 'size_only' is true if
4925 size only evaluation is wanted (only for arrays). */
4926 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4927 int first
, int size_only
)
4929 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4930 int size1
, align1
, expr_type
;
4934 if (type
->t
& VT_VLA
) {
4935 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4939 vpush_global_sym(&func_old_type
, TOK_alloca
);
4940 vla_runtime_type_size(type
, &a
);
4945 vsetc(type
, REG_IRET
, &retcval
);
4946 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
4951 tcc_error("variable length arrays unsupported for this target");
4953 } else if (type
->t
& VT_ARRAY
) {
4957 t1
= pointed_type(type
);
4958 size1
= type_size(t1
, &align1
);
4961 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4964 tcc_error("character array initializer must be a literal,"
4965 " optionally enclosed in braces");
4970 /* only parse strings here if correct type (otherwise: handle
4971 them as ((w)char *) expressions */
4972 if ((tok
== TOK_LSTR
&&
4973 #ifdef TCC_TARGET_PE
4974 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4976 (t1
->t
& VT_BTYPE
) == VT_INT
4978 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4979 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4984 /* compute maximum number of chars wanted */
4986 cstr_len
= cstr
->size
;
4988 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4991 if (n
>= 0 && nb
> (n
- array_length
))
4992 nb
= n
- array_length
;
4995 tcc_warning("initializer-string for array is too long");
4996 /* in order to go faster for common case (char
4997 string in global variable, we handle it
4999 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5000 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5004 ch
= ((unsigned char *)cstr
->data
)[i
];
5006 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5007 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5015 /* only add trailing zero if enough storage (no
5016 warning in this case since it is standard) */
5017 if (n
< 0 || array_length
< n
) {
5019 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5025 while (tok
!= '}') {
5026 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5027 if (n
>= 0 && index
>= n
)
5028 tcc_error("index too large");
5029 /* must put zero in holes (note that doing it that way
5030 ensures that it even works with designators) */
5031 if (!size_only
&& array_length
< index
) {
5032 init_putz(t1
, sec
, c
+ array_length
* size1
,
5033 (index
- array_length
) * size1
);
5036 if (index
> array_length
)
5037 array_length
= index
;
5038 /* special test for multi dimensional arrays (may not
5039 be strictly correct if designators are used at the
5041 if (index
>= n
&& no_oblock
)
5050 /* put zeros at the end */
5051 if (!size_only
&& n
>= 0 && array_length
< n
) {
5052 init_putz(t1
, sec
, c
+ array_length
* size1
,
5053 (n
- array_length
) * size1
);
5055 /* patch type size if needed */
5057 s
->c
= array_length
;
5058 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5059 (sec
|| !first
|| tok
== '{')) {
5062 /* NOTE: the previous test is a specific case for automatic
5063 struct/union init */
5064 /* XXX: union needs only one init */
5066 /* XXX: this test is incorrect for local initializers
5067 beginning with ( without {. It would be much more difficult
5068 to do it correctly (ideally, the expression parser should
5069 be used in all cases) */
5075 while (tok
== '(') {
5079 if (!parse_btype(&type1
, &ad1
))
5081 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5083 if (!is_assignable_types(type
, &type1
))
5084 tcc_error("invalid type for cast");
5089 if (first
|| tok
== '{') {
5098 while (tok
!= '}') {
5099 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5101 if (!size_only
&& array_length
< index
) {
5102 init_putz(type
, sec
, c
+ array_length
,
5103 index
- array_length
);
5105 index
= index
+ type_size(&f
->type
, &align1
);
5106 if (index
> array_length
)
5107 array_length
= index
;
5109 /* gr: skip fields from same union - ugly. */
5111 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5112 /* test for same offset */
5113 if (f
->next
->c
!= f
->c
)
5115 /* if yes, test for bitfield shift */
5116 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5117 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5118 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5119 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5120 if (bit_pos_1
!= bit_pos_2
)
5127 if (no_oblock
&& f
== NULL
)
5133 /* put zeros at the end */
5134 if (!size_only
&& array_length
< n
) {
5135 init_putz(type
, sec
, c
+ array_length
,
5144 } else if (tok
== '{') {
5146 decl_initializer(type
, sec
, c
, first
, size_only
);
5148 } else if (size_only
) {
5149 /* just skip expression */
5150 parlevel
= parlevel1
= 0;
5151 while ((parlevel
> 0 || parlevel1
> 0 ||
5152 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5155 else if (tok
== ')')
5157 else if (tok
== '{')
5159 else if (tok
== '}')
5164 /* currently, we always use constant expression for globals
5165 (may change for scripting case) */
5166 expr_type
= EXPR_CONST
;
5168 expr_type
= EXPR_ANY
;
5169 init_putv(type
, sec
, c
, 0, expr_type
);
5173 /* parse an initializer for type 't' if 'has_init' is non zero, and
5174 allocate space in local or global data space ('r' is either
5175 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5176 variable 'v' with an associated name represented by 'asm_label' of
5177 scope 'scope' is declared before initializers are parsed. If 'v' is
5178 zero, then a reference to the new object is put in the value stack.
5179 If 'has_init' is 2, a special parsing is done to handle string
5181 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5182 int has_init
, int v
, char *asm_label
,
5185 int size
, align
, addr
, data_offset
;
5187 ParseState saved_parse_state
= {0};
5188 TokenString init_str
;
5190 Sym
*flexible_array
;
5192 flexible_array
= NULL
;
5193 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5196 while (field
&& field
->next
)
5197 field
= field
->next
;
5198 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5199 flexible_array
= field
;
5202 size
= type_size(type
, &align
);
5203 /* If unknown size, we must evaluate it before
5204 evaluating initializers because
5205 initializers can generate global data too
5206 (e.g. string pointers or ISOC99 compound
5207 literals). It also simplifies local
5208 initializers handling */
5209 tok_str_new(&init_str
);
5210 if (size
< 0 || (flexible_array
&& has_init
)) {
5212 tcc_error("unknown type size");
5213 /* get all init string */
5214 if (has_init
== 2) {
5215 /* only get strings */
5216 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5217 tok_str_add_tok(&init_str
);
5222 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5224 tcc_error("unexpected end of file in initializer");
5225 tok_str_add_tok(&init_str
);
5228 else if (tok
== '}') {
5238 tok_str_add(&init_str
, -1);
5239 tok_str_add(&init_str
, 0);
5242 save_parse_state(&saved_parse_state
);
5244 macro_ptr
= init_str
.str
;
5246 decl_initializer(type
, NULL
, 0, 1, 1);
5247 /* prepare second initializer parsing */
5248 macro_ptr
= init_str
.str
;
5251 /* if still unknown size, error */
5252 size
= type_size(type
, &align
);
5254 tcc_error("unknown type size");
5257 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5258 /* take into account specified alignment if bigger */
5260 if (ad
->aligned
> align
)
5261 align
= ad
->aligned
;
5262 } else if (ad
->packed
) {
5265 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5267 #ifdef CONFIG_TCC_BCHECK
5268 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5272 loc
= (loc
- size
) & -align
;
5274 #ifdef CONFIG_TCC_BCHECK
5275 /* handles bounds */
5276 /* XXX: currently, since we do only one pass, we cannot track
5277 '&' operators, so we add only arrays */
5278 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5279 unsigned long *bounds_ptr
;
5280 /* add padding between regions */
5282 /* then add local bound info */
5283 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5284 bounds_ptr
[0] = addr
;
5285 bounds_ptr
[1] = size
;
5289 /* local variable */
5290 sym_push(v
, type
, r
, addr
);
5292 /* push local reference */
5293 vset(type
, r
, addr
);
5299 if (v
&& scope
== VT_CONST
) {
5300 /* see if the symbol was already defined */
5303 if (!is_compatible_types(&sym
->type
, type
))
5304 tcc_error("incompatible types for redefinition of '%s'",
5305 get_tok_str(v
, NULL
));
5306 if (sym
->type
.t
& VT_EXTERN
) {
5307 /* if the variable is extern, it was not allocated */
5308 sym
->type
.t
&= ~VT_EXTERN
;
5309 /* set array size if it was ommited in extern
5311 if ((sym
->type
.t
& VT_ARRAY
) &&
5312 sym
->type
.ref
->c
< 0 &&
5314 sym
->type
.ref
->c
= type
->ref
->c
;
5316 /* we accept several definitions of the same
5317 global variable. this is tricky, because we
5318 must play with the SHN_COMMON type of the symbol */
5319 /* XXX: should check if the variable was already
5320 initialized. It is incorrect to initialized it
5322 /* no init data, we won't add more to the symbol */
5329 /* allocate symbol in corresponding section */
5334 else if (tcc_state
->nocommon
)
5338 data_offset
= sec
->data_offset
;
5339 data_offset
= (data_offset
+ align
- 1) & -align
;
5341 /* very important to increment global pointer at this time
5342 because initializers themselves can create new initializers */
5343 data_offset
+= size
;
5344 #ifdef CONFIG_TCC_BCHECK
5345 /* add padding if bound check */
5346 if (tcc_state
->do_bounds_check
)
5349 sec
->data_offset
= data_offset
;
5350 /* allocate section space to put the data */
5351 if (sec
->sh_type
!= SHT_NOBITS
&&
5352 data_offset
> sec
->data_allocated
)
5353 section_realloc(sec
, data_offset
);
5354 /* align section if needed */
5355 if (align
> sec
->sh_addralign
)
5356 sec
->sh_addralign
= align
;
5358 addr
= 0; /* avoid warning */
5362 if (scope
!= VT_CONST
|| !sym
) {
5363 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5364 sym
->asm_label
= asm_label
;
5366 /* update symbol definition */
5368 put_extern_sym(sym
, sec
, addr
, size
);
5371 /* put a common area */
5372 put_extern_sym(sym
, NULL
, align
, size
);
5373 /* XXX: find a nicer way */
5374 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5375 esym
->st_shndx
= SHN_COMMON
;
5380 /* push global reference */
5381 sym
= get_sym_ref(type
, sec
, addr
, size
);
5383 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5386 /* patch symbol weakness */
5387 if (type
->t
& VT_WEAK
)
5389 #ifdef CONFIG_TCC_BCHECK
5390 /* handles bounds now because the symbol must be defined
5391 before for the relocation */
5392 if (tcc_state
->do_bounds_check
) {
5393 unsigned long *bounds_ptr
;
5395 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5396 /* then add global bound info */
5397 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5398 bounds_ptr
[0] = 0; /* relocated */
5399 bounds_ptr
[1] = size
;
5403 if (has_init
|| (type
->t
& VT_VLA
)) {
5404 decl_initializer(type
, sec
, addr
, 1, 0);
5405 /* restore parse state if needed */
5407 tok_str_free(init_str
.str
);
5408 restore_parse_state(&saved_parse_state
);
5410 /* patch flexible array member size back to -1, */
5411 /* for possible subsequent similar declarations */
5413 flexible_array
->type
.ref
->c
= -1;
5418 static void put_func_debug(Sym
*sym
)
5423 /* XXX: we put here a dummy type */
5424 snprintf(buf
, sizeof(buf
), "%s:%c1",
5425 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5426 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5427 cur_text_section
, sym
->c
);
5428 /* //gr gdb wants a line at the function */
5429 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5434 /* parse an old style function declaration list */
5435 /* XXX: check multiple parameter */
5436 static void func_decl_list(Sym
*func_sym
)
5443 /* parse each declaration */
5444 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5445 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5446 if (!parse_btype(&btype
, &ad
))
5447 expect("declaration list");
5448 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5449 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5451 /* we accept no variable after */
5455 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5456 /* find parameter in function parameter list */
5459 if ((s
->v
& ~SYM_FIELD
) == v
)
5463 tcc_error("declaration for parameter '%s' but no such parameter",
5464 get_tok_str(v
, NULL
));
5466 /* check that no storage specifier except 'register' was given */
5467 if (type
.t
& VT_STORAGE
)
5468 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5469 convert_parameter_type(&type
);
5470 /* we can add the type (NOTE: it could be local to the function) */
5472 /* accept other parameters */
5483 /* parse a function defined by symbol 'sym' and generate its code in
5484 'cur_text_section' */
5485 static void gen_function(Sym
*sym
)
5487 int saved_nocode_wanted
= nocode_wanted
;
5489 ind
= cur_text_section
->data_offset
;
5490 /* NOTE: we patch the symbol size later */
5491 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5492 funcname
= get_tok_str(sym
->v
, NULL
);
5494 /* put debug symbol */
5495 if (tcc_state
->do_debug
)
5496 put_func_debug(sym
);
5497 /* push a dummy symbol to enable local sym storage */
5498 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5499 gfunc_prolog(&sym
->type
);
5501 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5504 cur_text_section
->data_offset
= ind
;
5505 label_pop(&global_label_stack
, NULL
);
5506 sym_pop(&local_stack
, NULL
); /* reset local stack */
5507 /* end of function */
5508 /* patch symbol size */
5509 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5511 /* patch symbol weakness (this definition overrules any prototype) */
5512 if (sym
->type
.t
& VT_WEAK
)
5514 if (tcc_state
->do_debug
) {
5515 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5517 /* It's better to crash than to generate wrong code */
5518 cur_text_section
= NULL
;
5519 funcname
= ""; /* for safety */
5520 func_vt
.t
= VT_VOID
; /* for safety */
5521 ind
= 0; /* for safety */
5522 nocode_wanted
= saved_nocode_wanted
;
5525 ST_FUNC
void gen_inline_functions(void)
5528 int *str
, inline_generated
, i
;
5529 struct InlineFunc
*fn
;
5531 /* iterate while inline function are referenced */
5533 inline_generated
= 0;
5534 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5535 fn
= tcc_state
->inline_fns
[i
];
5537 if (sym
&& sym
->c
) {
5538 /* the function was used: generate its code and
5539 convert it to a normal function */
5540 str
= fn
->token_str
;
5543 strcpy(file
->filename
, fn
->filename
);
5544 sym
->r
= VT_SYM
| VT_CONST
;
5545 sym
->type
.t
&= ~VT_INLINE
;
5549 cur_text_section
= text_section
;
5551 macro_ptr
= NULL
; /* fail safe */
5553 inline_generated
= 1;
5556 if (!inline_generated
)
5559 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5560 fn
= tcc_state
->inline_fns
[i
];
5561 str
= fn
->token_str
;
5564 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5567 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5568 static int decl0(int l
, int is_for_loop_init
)
5576 if (!parse_btype(&btype
, &ad
)) {
5577 if (is_for_loop_init
)
5579 /* skip redundant ';' */
5580 /* XXX: find more elegant solution */
5585 if (l
== VT_CONST
&&
5586 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5587 /* global asm block */
5591 /* special test for old K&R protos without explicit int
5592 type. Only accepted when defining global data */
5593 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5597 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5598 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5600 /* we accept no variable after */
5604 while (1) { /* iterate thru each declaration */
5605 char *asm_label
; // associated asm label
5607 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5611 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5612 printf("type = '%s'\n", buf
);
5615 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5616 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5617 tcc_error("function without file scope cannot be static");
5619 /* if old style function prototype, we accept a
5622 if (sym
->c
== FUNC_OLD
)
5623 func_decl_list(sym
);
5627 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5630 asm_label_instr(&astr
);
5631 asm_label
= tcc_strdup(astr
.data
);
5634 /* parse one last attribute list, after asm label */
5635 parse_attribute(&ad
);
5640 #ifdef TCC_TARGET_PE
5642 type
.t
|= VT_IMPORT
;
5644 type
.t
|= VT_EXPORT
;
5648 tcc_error("cannot use local functions");
5649 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5650 expect("function definition");
5652 /* reject abstract declarators in function definition */
5654 while ((sym
= sym
->next
) != NULL
)
5655 if (!(sym
->v
& ~SYM_FIELD
))
5656 expect("identifier");
5658 /* XXX: cannot do better now: convert extern line to static inline */
5659 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5660 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5664 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5667 r
= sym
->type
.ref
->r
;
5668 /* use func_call from prototype if not defined */
5669 if (FUNC_CALL(r
) != FUNC_CDECL
5670 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5671 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5673 /* use export from prototype */
5675 FUNC_EXPORT(type
.ref
->r
) = 1;
5677 /* use static from prototype */
5678 if (sym
->type
.t
& VT_STATIC
)
5679 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5681 if (!is_compatible_types(&sym
->type
, &type
)) {
5683 tcc_error("incompatible types for redefinition of '%s'",
5684 get_tok_str(v
, NULL
));
5686 /* if symbol is already defined, then put complete type */
5689 /* put function symbol */
5690 sym
= global_identifier_push(v
, type
.t
, 0);
5691 sym
->type
.ref
= type
.ref
;
5694 /* static inline functions are just recorded as a kind
5695 of macro. Their code will be emitted at the end of
5696 the compilation unit only if they are used */
5697 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5698 (VT_INLINE
| VT_STATIC
)) {
5699 TokenString func_str
;
5701 struct InlineFunc
*fn
;
5702 const char *filename
;
5704 tok_str_new(&func_str
);
5710 tcc_error("unexpected end of file");
5711 tok_str_add_tok(&func_str
);
5716 } else if (t
== '}') {
5718 if (block_level
== 0)
5722 tok_str_add(&func_str
, -1);
5723 tok_str_add(&func_str
, 0);
5724 filename
= file
? file
->filename
: "";
5725 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5726 strcpy(fn
->filename
, filename
);
5728 fn
->token_str
= func_str
.str
;
5729 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5732 /* compute text section */
5733 cur_text_section
= ad
.section
;
5734 if (!cur_text_section
)
5735 cur_text_section
= text_section
;
5736 sym
->r
= VT_SYM
| VT_CONST
;
5741 if (btype
.t
& VT_TYPEDEF
) {
5742 /* save typedefed type */
5743 /* XXX: test storage specifiers ? */
5744 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5745 sym
->type
.t
|= VT_TYPEDEF
;
5748 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5749 /* external function definition */
5750 /* specific case for func_call attribute */
5751 type
.ref
->r
= INT_ATTR(&ad
);
5752 } else if (!(type
.t
& VT_ARRAY
)) {
5753 /* not lvalue if array */
5754 r
|= lvalue_type(type
.t
);
5756 has_init
= (tok
== '=');
5757 if (has_init
&& (type
.t
& VT_VLA
))
5758 tcc_error("Variable length array cannot be initialized");
5759 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5760 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5761 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5762 /* external variable or function */
5763 /* NOTE: as GCC, uninitialized global static
5764 arrays of null size are considered as
5766 sym
= external_sym(v
, &type
, r
, asm_label
);
5768 if (type
.t
& VT_WEAK
)
5771 if (ad
.alias_target
) {
5776 alias_target
= sym_find(ad
.alias_target
);
5777 if (!alias_target
|| !alias_target
->c
)
5778 tcc_error("unsupported forward __alias__ attribute");
5779 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5780 tsec
.sh_num
= esym
->st_shndx
;
5781 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5784 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5785 if (type
.t
& VT_STATIC
)
5791 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5795 if (is_for_loop_init
)
5808 ST_FUNC
void decl(int l
)