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
;
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 void expr_eq(void);
80 static void unary_type(CType
*type
);
81 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
82 static void expr_type(CType
*type
);
84 ST_INLN
int is_float(int t
)
88 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
91 ST_FUNC
void test_lvalue(void)
93 if (!(vtop
->r
& VT_LVAL
))
97 /* ------------------------------------------------------------------------- */
98 /* symbol allocator */
99 static Sym
*__sym_malloc(void)
101 Sym
*sym_pool
, *sym
, *last_sym
;
104 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
105 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
107 last_sym
= sym_free_first
;
109 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
110 sym
->next
= last_sym
;
114 sym_free_first
= last_sym
;
118 static inline Sym
*sym_malloc(void)
121 sym
= sym_free_first
;
123 sym
= __sym_malloc();
124 sym_free_first
= sym
->next
;
128 ST_INLN
void sym_free(Sym
*sym
)
130 sym
->next
= sym_free_first
;
131 tcc_free(sym
->asm_label
);
132 sym_free_first
= sym
;
135 /* push, without hashing */
136 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
155 /* find a symbol and return its associated structure. 's' is the top
156 of the symbol stack */
157 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
167 /* structure lookup */
168 ST_INLN Sym
*struct_find(int v
)
171 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
173 return table_ident
[v
]->sym_struct
;
176 /* find an identifier */
177 ST_INLN Sym
*sym_find(int v
)
180 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
182 return table_ident
[v
]->sym_identifier
;
185 /* push a given symbol on the symbol stack */
186 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
195 s
= sym_push2(ps
, v
, type
->t
, c
);
196 s
->type
.ref
= type
->ref
;
198 /* don't record fields or anonymous symbols */
200 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
201 /* record symbol in token array */
202 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
204 ps
= &ts
->sym_struct
;
206 ps
= &ts
->sym_identifier
;
213 /* push a global identifier */
214 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
217 s
= sym_push2(&global_stack
, v
, t
, c
);
218 /* don't record anonymous symbol */
219 if (v
< SYM_FIRST_ANOM
) {
220 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
221 /* modify the top most local identifier, so that
222 sym_identifier will point to 's' when popped */
224 ps
= &(*ps
)->prev_tok
;
231 /* pop symbols until top reaches 'b' */
232 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
242 /* remove symbol in token array */
244 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
245 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
247 ps
= &ts
->sym_struct
;
249 ps
= &ts
->sym_identifier
;
258 static void weaken_symbol(Sym
*sym
)
260 sym
->type
.t
|= VT_WEAK
;
265 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
266 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
267 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
271 /* ------------------------------------------------------------------------- */
273 ST_FUNC
void swap(int *p
, int *q
)
281 static void vsetc(CType
*type
, int r
, CValue
*vc
)
285 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
286 error("memory full");
287 /* cannot let cpu flags if other instruction are generated. Also
288 avoid leaving VT_JMP anywhere except on the top of the stack
289 because it would complicate the code generator. */
290 if (vtop
>= vstack
) {
291 v
= vtop
->r
& VT_VALMASK
;
292 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
302 /* push constant of type "type" with useless value */
303 void vpush(CType
*type
)
306 vsetc(type
, VT_CONST
, &cval
);
309 /* push integer constant */
310 ST_FUNC
void vpushi(int v
)
314 vsetc(&int_type
, VT_CONST
, &cval
);
317 /* push long long constant */
318 static void vpushll(long long v
)
325 vsetc(&ctype
, VT_CONST
, &cval
);
328 /* push arbitrary 64bit constant */
329 void vpush64(int ty
, unsigned long long v
)
335 vsetc(&ctype
, VT_CONST
, &cval
);
338 /* Return a static symbol pointing to a section */
339 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
345 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
346 sym
->type
.ref
= type
->ref
;
347 sym
->r
= VT_CONST
| VT_SYM
;
348 put_extern_sym(sym
, sec
, offset
, size
);
352 /* push a reference to a section offset by adding a dummy symbol */
353 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
358 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
359 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
362 /* define a new external reference to a symbol 'v' of type 'u' */
363 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
369 /* push forward reference */
370 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
371 s
->type
.ref
= type
->ref
;
372 s
->r
= r
| VT_CONST
| VT_SYM
;
377 /* define a new external reference to a symbol 'v' with alternate asm
378 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
379 is no alternate name (most cases) */
380 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
386 /* push forward reference */
387 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
388 s
->asm_label
= asm_label
;
389 s
->type
.t
|= VT_EXTERN
;
390 } else if (s
->type
.ref
== func_old_type
.ref
) {
391 s
->type
.ref
= type
->ref
;
392 s
->r
= r
| VT_CONST
| VT_SYM
;
393 s
->type
.t
|= VT_EXTERN
;
394 } else if (!is_compatible_types(&s
->type
, type
)) {
395 error("incompatible types for redefinition of '%s'",
396 get_tok_str(v
, NULL
));
401 /* push a reference to global symbol v */
402 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
407 sym
= external_global_sym(v
, type
, 0);
409 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
413 ST_FUNC
void vset(CType
*type
, int r
, int v
)
418 vsetc(type
, r
, &cval
);
421 static void vseti(int r
, int v
)
429 ST_FUNC
void vswap(void)
438 ST_FUNC
void vpushv(SValue
*v
)
440 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
441 error("memory full");
446 static void vdup(void)
451 /* save r to the memory stack, and mark it as being free */
452 ST_FUNC
void save_reg(int r
)
454 int l
, saved
, size
, align
;
458 /* modify all stack values */
461 for(p
=vstack
;p
<=vtop
;p
++) {
462 if ((p
->r
& VT_VALMASK
) == r
||
463 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
464 /* must save value on stack if not already done */
466 /* NOTE: must reload 'r' because r might be equal to r2 */
467 r
= p
->r
& VT_VALMASK
;
468 /* store register in the stack */
470 if ((p
->r
& VT_LVAL
) ||
471 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
472 #ifdef TCC_TARGET_X86_64
473 type
= &char_pointer_type
;
477 size
= type_size(type
, &align
);
478 loc
= (loc
- size
) & -align
;
480 sv
.r
= VT_LOCAL
| VT_LVAL
;
483 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
484 /* x86 specific: need to pop fp register ST0 if saved */
486 o(0xd8dd); /* fstp %st(0) */
489 #ifndef TCC_TARGET_X86_64
490 /* special long long case */
491 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
499 /* mark that stack entry as being saved on the stack */
500 if (p
->r
& VT_LVAL
) {
501 /* also clear the bounded flag because the
502 relocation address of the function was stored in
504 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
506 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
514 #ifdef TCC_TARGET_ARM
515 /* find a register of class 'rc2' with at most one reference on stack.
516 * If none, call get_reg(rc) */
517 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
522 for(r
=0;r
<NB_REGS
;r
++) {
523 if (reg_classes
[r
] & rc2
) {
526 for(p
= vstack
; p
<= vtop
; p
++) {
527 if ((p
->r
& VT_VALMASK
) == r
||
528 (p
->r2
& VT_VALMASK
) == r
)
539 /* find a free register of class 'rc'. If none, save one register */
540 ST_FUNC
int get_reg(int rc
)
545 /* find a free register */
546 for(r
=0;r
<NB_REGS
;r
++) {
547 if (reg_classes
[r
] & rc
) {
548 for(p
=vstack
;p
<=vtop
;p
++) {
549 if ((p
->r
& VT_VALMASK
) == r
||
550 (p
->r2
& VT_VALMASK
) == r
)
558 /* no register left : free the first one on the stack (VERY
559 IMPORTANT to start from the bottom to ensure that we don't
560 spill registers used in gen_opi()) */
561 for(p
=vstack
;p
<=vtop
;p
++) {
562 r
= p
->r
& VT_VALMASK
;
563 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
565 /* also look at second register (if long long) */
566 r
= p
->r2
& VT_VALMASK
;
567 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
573 /* Should never comes here */
577 /* save registers up to (vtop - n) stack entry */
578 ST_FUNC
void save_regs(int n
)
583 for(p
= vstack
;p
<= p1
; p
++) {
584 r
= p
->r
& VT_VALMASK
;
591 /* move register 's' to 'r', and flush previous value of r to memory
593 static void move_reg(int r
, int s
)
606 /* get address of vtop (vtop MUST BE an lvalue) */
607 static void gaddrof(void)
610 /* tricky: if saved lvalue, then we can go back to lvalue */
611 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
612 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
615 #ifdef CONFIG_TCC_BCHECK
616 /* generate lvalue bound code */
617 static void gbound(void)
622 vtop
->r
&= ~VT_MUSTBOUND
;
623 /* if lvalue, then use checking code before dereferencing */
624 if (vtop
->r
& VT_LVAL
) {
625 /* if not VT_BOUNDED value, then make one */
626 if (!(vtop
->r
& VT_BOUNDED
)) {
627 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
628 /* must save type because we must set it to int to get pointer */
630 vtop
->type
.t
= VT_INT
;
633 gen_bounded_ptr_add();
634 vtop
->r
|= lval_type
;
637 /* then check for dereferencing */
638 gen_bounded_ptr_deref();
643 /* store vtop a register belonging to class 'rc'. lvalues are
644 converted to values. Cannot be used if cannot be converted to
645 register value (such as structures). */
646 ST_FUNC
int gv(int rc
)
648 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
650 /* NOTE: get_reg can modify vstack[] */
651 if (vtop
->type
.t
& VT_BITFIELD
) {
654 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
655 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
656 /* remove bit field info to avoid loops */
657 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
658 /* cast to int to propagate signedness in following ops */
659 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
664 if((vtop
->type
.t
& VT_UNSIGNED
) ||
665 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
666 type
.t
|= VT_UNSIGNED
;
668 /* generate shifts */
669 vpushi(bits
- (bit_pos
+ bit_size
));
671 vpushi(bits
- bit_size
);
672 /* NOTE: transformed to SHR if unsigned */
676 if (is_float(vtop
->type
.t
) &&
677 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
680 unsigned long offset
;
681 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
685 /* XXX: unify with initializers handling ? */
686 /* CPUs usually cannot use float constants, so we store them
687 generically in data segment */
688 size
= type_size(&vtop
->type
, &align
);
689 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
690 data_section
->data_offset
= offset
;
691 /* XXX: not portable yet */
692 #if defined(__i386__) || defined(__x86_64__)
693 /* Zero pad x87 tenbyte long doubles */
694 if (size
== LDOUBLE_SIZE
)
695 vtop
->c
.tab
[2] &= 0xffff;
697 ptr
= section_ptr_add(data_section
, size
);
699 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
703 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
707 ptr
[i
] = vtop
->c
.tab
[i
];
708 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
709 vtop
->r
|= VT_LVAL
| VT_SYM
;
713 #ifdef CONFIG_TCC_BCHECK
714 if (vtop
->r
& VT_MUSTBOUND
)
718 r
= vtop
->r
& VT_VALMASK
;
722 /* need to reload if:
724 - lvalue (need to dereference pointer)
725 - already a register, but not in the right class */
727 || (vtop
->r
& VT_LVAL
)
728 || !(reg_classes
[r
] & rc
)
729 #ifndef TCC_TARGET_X86_64
730 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
735 #ifndef TCC_TARGET_X86_64
736 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
738 unsigned long long ll
;
739 /* two register type load : expand to two words
741 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
744 vtop
->c
.ui
= ll
; /* first word */
746 vtop
->r
= r
; /* save register value */
747 vpushi(ll
>> 32); /* second word */
748 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
749 (vtop
->r
& VT_LVAL
)) {
750 /* We do not want to modifier the long long
751 pointer here, so the safest (and less
752 efficient) is to save all the other registers
753 in the stack. XXX: totally inefficient. */
755 /* load from memory */
758 vtop
[-1].r
= r
; /* save register value */
759 /* increment pointer to get second word */
760 vtop
->type
.t
= VT_INT
;
769 vtop
[-1].r
= r
; /* save register value */
770 vtop
->r
= vtop
[-1].r2
;
772 /* allocate second register */
776 /* write second register */
780 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
782 /* lvalue of scalar type : need to use lvalue type
783 because of possible cast */
786 /* compute memory access type */
787 if (vtop
->r
& VT_LVAL_BYTE
)
789 else if (vtop
->r
& VT_LVAL_SHORT
)
791 if (vtop
->r
& VT_LVAL_UNSIGNED
)
795 /* restore wanted type */
798 /* one register type load */
803 #ifdef TCC_TARGET_C67
804 /* uses register pairs for doubles */
805 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
812 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
813 ST_FUNC
void gv2(int rc1
, int rc2
)
817 /* generate more generic register first. But VT_JMP or VT_CMP
818 values must be generated first in all cases to avoid possible
820 v
= vtop
[0].r
& VT_VALMASK
;
821 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
826 /* test if reload is needed for first register */
827 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
837 /* test if reload is needed for first register */
838 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
844 /* wrapper around RC_FRET to return a register by type */
845 static int rc_fret(int t
)
847 #ifdef TCC_TARGET_X86_64
848 if (t
== VT_LDOUBLE
) {
855 /* wrapper around REG_FRET to return a register by type */
856 static int reg_fret(int t
)
858 #ifdef TCC_TARGET_X86_64
859 if (t
== VT_LDOUBLE
) {
866 /* expand long long on stack in two int registers */
867 static void lexpand(void)
871 u
= vtop
->type
.t
& VT_UNSIGNED
;
874 vtop
[0].r
= vtop
[-1].r2
;
875 vtop
[0].r2
= VT_CONST
;
876 vtop
[-1].r2
= VT_CONST
;
877 vtop
[0].type
.t
= VT_INT
| u
;
878 vtop
[-1].type
.t
= VT_INT
| u
;
881 #ifdef TCC_TARGET_ARM
882 /* expand long long on stack */
883 ST_FUNC
void lexpand_nr(void)
887 u
= vtop
->type
.t
& VT_UNSIGNED
;
890 vtop
->type
.t
= VT_INT
| u
;
891 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
893 vtop
[-1].c
.ui
= vtop
->c
.ull
;
894 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
896 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
898 vtop
->r
= vtop
[-1].r
;
899 } else if (v
> VT_CONST
) {
903 vtop
->r
= vtop
[-1].r2
;
904 vtop
[-1].r2
= VT_CONST
;
905 vtop
[-1].type
.t
= VT_INT
| u
;
909 /* build a long long from two ints */
910 static void lbuild(int t
)
913 vtop
[-1].r2
= vtop
[0].r
;
918 /* rotate n first stack elements to the bottom
919 I1 ... In -> I2 ... In I1 [top is right]
921 static void vrotb(int n
)
932 /* rotate n first stack elements to the top
933 I1 ... In -> In I1 ... I(n-1) [top is right]
935 ST_FUNC
void vrott(int n
)
941 for(i
= 0;i
< n
- 1; i
++)
942 vtop
[-i
] = vtop
[-i
- 1];
946 #ifdef TCC_TARGET_ARM
947 /* like vrott but in other direction
948 In ... I1 -> I(n-1) ... I1 In [top is right]
950 ST_FUNC
void vnrott(int n
)
956 for(i
= n
- 1; i
> 0; i
--)
957 vtop
[-i
] = vtop
[-i
+ 1];
962 /* pop stack value */
963 ST_FUNC
void vpop(void)
966 v
= vtop
->r
& VT_VALMASK
;
967 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
968 /* for x86, we need to pop the FP stack */
969 if (v
== TREG_ST0
&& !nocode_wanted
) {
970 o(0xd8dd); /* fstp %st(0) */
973 if (v
== VT_JMP
|| v
== VT_JMPI
) {
974 /* need to put correct jump if && or || without test */
980 /* convert stack entry to register and duplicate its value in another
982 static void gv_dup(void)
988 if ((t
& VT_BTYPE
) == VT_LLONG
) {
995 /* stack: H L L1 H1 */
1003 /* duplicate value */
1008 #ifdef TCC_TARGET_X86_64
1009 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1019 load(r1
, &sv
); /* move r to r1 */
1021 /* duplicates value */
1027 #ifndef TCC_TARGET_X86_64
1028 /* generate CPU independent (unsigned) long long operations */
1029 static void gen_opl(int op
)
1031 int t
, a
, b
, op1
, c
, i
;
1033 unsigned short reg_iret
= REG_IRET
;
1034 unsigned short reg_lret
= REG_LRET
;
1040 func
= TOK___divdi3
;
1043 func
= TOK___udivdi3
;
1046 func
= TOK___moddi3
;
1049 func
= TOK___umoddi3
;
1056 /* call generic long long function */
1057 vpush_global_sym(&func_old_type
, func
);
1062 vtop
->r2
= reg_lret
;
1075 /* stack: L1 H1 L2 H2 */
1080 vtop
[-2] = vtop
[-3];
1083 /* stack: H1 H2 L1 L2 */
1089 /* stack: H1 H2 L1 L2 ML MH */
1092 /* stack: ML MH H1 H2 L1 L2 */
1096 /* stack: ML MH H1 L2 H2 L1 */
1101 /* stack: ML MH M1 M2 */
1104 } else if (op
== '+' || op
== '-') {
1105 /* XXX: add non carry method too (for MIPS or alpha) */
1111 /* stack: H1 H2 (L1 op L2) */
1114 gen_op(op1
+ 1); /* TOK_xxxC2 */
1117 /* stack: H1 H2 (L1 op L2) */
1120 /* stack: (L1 op L2) H1 H2 */
1122 /* stack: (L1 op L2) (H1 op H2) */
1130 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1131 t
= vtop
[-1].type
.t
;
1135 /* stack: L H shift */
1137 /* constant: simpler */
1138 /* NOTE: all comments are for SHL. the other cases are
1139 done by swaping words */
1150 if (op
!= TOK_SAR
) {
1183 /* XXX: should provide a faster fallback on x86 ? */
1186 func
= TOK___ashrdi3
;
1189 func
= TOK___lshrdi3
;
1192 func
= TOK___ashldi3
;
1198 /* compare operations */
1204 /* stack: L1 H1 L2 H2 */
1206 vtop
[-1] = vtop
[-2];
1208 /* stack: L1 L2 H1 H2 */
1211 /* when values are equal, we need to compare low words. since
1212 the jump is inverted, we invert the test too. */
1215 else if (op1
== TOK_GT
)
1217 else if (op1
== TOK_ULT
)
1219 else if (op1
== TOK_UGT
)
1224 if (op1
!= TOK_NE
) {
1228 /* generate non equal test */
1229 /* XXX: NOT PORTABLE yet */
1233 #if defined(TCC_TARGET_I386)
1234 b
= psym(0x850f, 0);
1235 #elif defined(TCC_TARGET_ARM)
1237 o(0x1A000000 | encbranch(ind
, 0, 1));
1238 #elif defined(TCC_TARGET_C67)
1239 error("not implemented");
1241 #error not supported
1245 /* compare low. Always unsigned */
1249 else if (op1
== TOK_LE
)
1251 else if (op1
== TOK_GT
)
1253 else if (op1
== TOK_GE
)
1264 /* handle integer constant optimizations and various machine
1266 static void gen_opic(int op
)
1268 int c1
, c2
, t1
, t2
, n
;
1271 typedef unsigned long long U
;
1275 t1
= v1
->type
.t
& VT_BTYPE
;
1276 t2
= v2
->type
.t
& VT_BTYPE
;
1280 else if (v1
->type
.t
& VT_UNSIGNED
)
1287 else if (v2
->type
.t
& VT_UNSIGNED
)
1292 /* currently, we cannot do computations with forward symbols */
1293 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1294 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1297 case '+': l1
+= l2
; break;
1298 case '-': l1
-= l2
; break;
1299 case '&': l1
&= l2
; break;
1300 case '^': l1
^= l2
; break;
1301 case '|': l1
|= l2
; break;
1302 case '*': l1
*= l2
; break;
1309 /* if division by zero, generate explicit division */
1312 error("division by zero in constant");
1316 default: l1
/= l2
; break;
1317 case '%': l1
%= l2
; break;
1318 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1319 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1322 case TOK_SHL
: l1
<<= l2
; break;
1323 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1324 case TOK_SAR
: l1
>>= l2
; break;
1326 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1327 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1328 case TOK_EQ
: l1
= l1
== l2
; break;
1329 case TOK_NE
: l1
= l1
!= l2
; break;
1330 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1331 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1332 case TOK_LT
: l1
= l1
< l2
; break;
1333 case TOK_GE
: l1
= l1
>= l2
; break;
1334 case TOK_LE
: l1
= l1
<= l2
; break;
1335 case TOK_GT
: l1
= l1
> l2
; break;
1337 case TOK_LAND
: l1
= l1
&& l2
; break;
1338 case TOK_LOR
: l1
= l1
|| l2
; break;
1345 /* if commutative ops, put c2 as constant */
1346 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1347 op
== '|' || op
== '*')) {
1349 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1350 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1352 /* Filter out NOP operations like x*1, x-0, x&-1... */
1353 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1356 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1357 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1363 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1364 /* try to use shifts instead of muls or divs */
1365 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1374 else if (op
== TOK_PDIV
)
1380 } else if (c2
&& (op
== '+' || op
== '-') &&
1381 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1382 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1383 /* symbol + constant case */
1390 if (!nocode_wanted
) {
1391 /* call low level op generator */
1392 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1403 /* generate a floating point operation with constant propagation */
1404 static void gen_opif(int op
)
1412 /* currently, we cannot do computations with forward symbols */
1413 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1414 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1416 if (v1
->type
.t
== VT_FLOAT
) {
1419 } else if (v1
->type
.t
== VT_DOUBLE
) {
1427 /* NOTE: we only do constant propagation if finite number (not
1428 NaN or infinity) (ANSI spec) */
1429 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1433 case '+': f1
+= f2
; break;
1434 case '-': f1
-= f2
; break;
1435 case '*': f1
*= f2
; break;
1439 error("division by zero in constant");
1444 /* XXX: also handles tests ? */
1448 /* XXX: overflow test ? */
1449 if (v1
->type
.t
== VT_FLOAT
) {
1451 } else if (v1
->type
.t
== VT_DOUBLE
) {
1459 if (!nocode_wanted
) {
1467 static int pointed_size(CType
*type
)
1470 return type_size(pointed_type(type
), &align
);
1473 static inline int is_null_pointer(SValue
*p
)
1475 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1477 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1478 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1481 static inline int is_integer_btype(int bt
)
1483 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1484 bt
== VT_INT
|| bt
== VT_LLONG
);
1487 /* check types for comparison or substraction of pointers */
1488 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1490 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1493 /* null pointers are accepted for all comparisons as gcc */
1494 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1498 bt1
= type1
->t
& VT_BTYPE
;
1499 bt2
= type2
->t
& VT_BTYPE
;
1500 /* accept comparison between pointer and integer with a warning */
1501 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1502 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1503 warning("comparison between pointer and integer");
1507 /* both must be pointers or implicit function pointers */
1508 if (bt1
== VT_PTR
) {
1509 type1
= pointed_type(type1
);
1510 } else if (bt1
!= VT_FUNC
)
1511 goto invalid_operands
;
1513 if (bt2
== VT_PTR
) {
1514 type2
= pointed_type(type2
);
1515 } else if (bt2
!= VT_FUNC
) {
1517 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1519 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1520 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1524 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1525 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1526 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1527 /* gcc-like error if '-' is used */
1529 goto invalid_operands
;
1531 warning("comparison of distinct pointer types lacks a cast");
1535 /* generic gen_op: handles types problems */
1536 ST_FUNC
void gen_op(int op
)
1538 int u
, t1
, t2
, bt1
, bt2
, t
;
1541 t1
= vtop
[-1].type
.t
;
1542 t2
= vtop
[0].type
.t
;
1543 bt1
= t1
& VT_BTYPE
;
1544 bt2
= t2
& VT_BTYPE
;
1546 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1547 /* at least one operand is a pointer */
1548 /* relationnal op: must be both pointers */
1549 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1550 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1551 /* pointers are handled are unsigned */
1552 #ifdef TCC_TARGET_X86_64
1553 t
= VT_LLONG
| VT_UNSIGNED
;
1555 t
= VT_INT
| VT_UNSIGNED
;
1559 /* if both pointers, then it must be the '-' op */
1560 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1562 error("cannot use pointers here");
1563 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1564 /* XXX: check that types are compatible */
1565 u
= pointed_size(&vtop
[-1].type
);
1567 /* set to integer type */
1568 #ifdef TCC_TARGET_X86_64
1569 vtop
->type
.t
= VT_LLONG
;
1571 vtop
->type
.t
= VT_INT
;
1576 /* exactly one pointer : must be '+' or '-'. */
1577 if (op
!= '-' && op
!= '+')
1578 error("cannot use pointers here");
1579 /* Put pointer as first operand */
1580 if (bt2
== VT_PTR
) {
1584 type1
= vtop
[-1].type
;
1585 type1
.t
&= ~VT_ARRAY
;
1586 u
= pointed_size(&vtop
[-1].type
);
1588 error("unknown array element size");
1589 #ifdef TCC_TARGET_X86_64
1592 /* XXX: cast to int ? (long long case) */
1596 #ifdef CONFIG_TCC_BCHECK
1597 /* if evaluating constant expression, no code should be
1598 generated, so no bound check */
1599 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1600 /* if bounded pointers, we generate a special code to
1607 gen_bounded_ptr_add();
1613 /* put again type if gen_opic() swaped operands */
1616 } else if (is_float(bt1
) || is_float(bt2
)) {
1617 /* compute bigger type and do implicit casts */
1618 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1620 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1625 /* floats can only be used for a few operations */
1626 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1627 (op
< TOK_ULT
|| op
> TOK_GT
))
1628 error("invalid operands for binary operation");
1630 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1631 /* cast to biggest op */
1633 /* convert to unsigned if it does not fit in a long long */
1634 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1635 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1639 /* integer operations */
1641 /* convert to unsigned if it does not fit in an integer */
1642 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1643 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1646 /* XXX: currently, some unsigned operations are explicit, so
1647 we modify them here */
1648 if (t
& VT_UNSIGNED
) {
1655 else if (op
== TOK_LT
)
1657 else if (op
== TOK_GT
)
1659 else if (op
== TOK_LE
)
1661 else if (op
== TOK_GE
)
1668 /* special case for shifts and long long: we keep the shift as
1670 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1677 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1678 /* relationnal op: the result is an int */
1679 vtop
->type
.t
= VT_INT
;
1686 #ifndef TCC_TARGET_ARM
1687 /* generic itof for unsigned long long case */
1688 static void gen_cvt_itof1(int t
)
1690 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1691 (VT_LLONG
| VT_UNSIGNED
)) {
1694 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1695 #if LDOUBLE_SIZE != 8
1696 else if (t
== VT_LDOUBLE
)
1697 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1700 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1704 vtop
->r
= reg_fret(t
);
1711 /* generic ftoi for unsigned long long case */
1712 static void gen_cvt_ftoi1(int t
)
1716 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1717 /* not handled natively */
1718 st
= vtop
->type
.t
& VT_BTYPE
;
1720 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1721 #if LDOUBLE_SIZE != 8
1722 else if (st
== VT_LDOUBLE
)
1723 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1726 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1731 vtop
->r2
= REG_LRET
;
1737 /* force char or short cast */
1738 static void force_charshort_cast(int t
)
1742 /* XXX: add optimization if lvalue : just change type and offset */
1747 if (t
& VT_UNSIGNED
) {
1748 vpushi((1 << bits
) - 1);
1754 /* result must be signed or the SAR is converted to an SHL
1755 This was not the case when "t" was a signed short
1756 and the last value on the stack was an unsigned int */
1757 vtop
->type
.t
&= ~VT_UNSIGNED
;
1763 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1764 static void gen_cast(CType
*type
)
1766 int sbt
, dbt
, sf
, df
, c
, p
;
1768 /* special delayed cast for char/short */
1769 /* XXX: in some cases (multiple cascaded casts), it may still
1771 if (vtop
->r
& VT_MUSTCAST
) {
1772 vtop
->r
&= ~VT_MUSTCAST
;
1773 force_charshort_cast(vtop
->type
.t
);
1776 /* bitfields first get cast to ints */
1777 if (vtop
->type
.t
& VT_BITFIELD
) {
1781 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1782 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1787 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1788 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1790 /* constant case: we can do it now */
1791 /* XXX: in ISOC, cannot do it if error in convert */
1792 if (sbt
== VT_FLOAT
)
1793 vtop
->c
.ld
= vtop
->c
.f
;
1794 else if (sbt
== VT_DOUBLE
)
1795 vtop
->c
.ld
= vtop
->c
.d
;
1798 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1799 if (sbt
& VT_UNSIGNED
)
1800 vtop
->c
.ld
= vtop
->c
.ull
;
1802 vtop
->c
.ld
= vtop
->c
.ll
;
1804 if (sbt
& VT_UNSIGNED
)
1805 vtop
->c
.ld
= vtop
->c
.ui
;
1807 vtop
->c
.ld
= vtop
->c
.i
;
1810 if (dbt
== VT_FLOAT
)
1811 vtop
->c
.f
= (float)vtop
->c
.ld
;
1812 else if (dbt
== VT_DOUBLE
)
1813 vtop
->c
.d
= (double)vtop
->c
.ld
;
1814 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1815 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1816 } else if (sf
&& dbt
== VT_BOOL
) {
1817 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1820 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1821 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1822 vtop
->c
.ll
= vtop
->c
.ull
;
1823 else if (sbt
& VT_UNSIGNED
)
1824 vtop
->c
.ll
= vtop
->c
.ui
;
1825 #ifdef TCC_TARGET_X86_64
1826 else if (sbt
== VT_PTR
)
1829 else if (sbt
!= VT_LLONG
)
1830 vtop
->c
.ll
= vtop
->c
.i
;
1832 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1833 vtop
->c
.ull
= vtop
->c
.ll
;
1834 else if (dbt
== VT_BOOL
)
1835 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1836 else if (dbt
!= VT_LLONG
) {
1838 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1840 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1843 if(dbt
& VT_UNSIGNED
)
1844 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1846 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1849 } else if (p
&& dbt
== VT_BOOL
) {
1852 } else if (!nocode_wanted
) {
1853 /* non constant case: generate code */
1855 /* convert from fp to fp */
1858 /* convert int to fp */
1861 /* convert fp to int */
1862 if (dbt
== VT_BOOL
) {
1866 /* we handle char/short/etc... with generic code */
1867 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1868 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1872 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1873 /* additional cast for char/short... */
1878 #ifndef TCC_TARGET_X86_64
1879 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1880 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1881 /* scalar to long long */
1882 /* machine independent conversion */
1884 /* generate high word */
1885 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1889 if (sbt
== VT_PTR
) {
1890 /* cast from pointer to int before we apply
1891 shift operation, which pointers don't support*/
1892 gen_cast(&int_type
);
1898 /* patch second register */
1899 vtop
[-1].r2
= vtop
->r
;
1903 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1904 (dbt
& VT_BTYPE
) == VT_PTR
||
1905 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1906 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1907 (sbt
& VT_BTYPE
) != VT_PTR
&&
1908 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1909 /* need to convert from 32bit to 64bit */
1911 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1912 /* x86_64 specific: movslq */
1914 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1918 } else if (dbt
== VT_BOOL
) {
1919 /* scalar to bool */
1922 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1923 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1924 if (sbt
== VT_PTR
) {
1925 vtop
->type
.t
= VT_INT
;
1926 warning("nonportable conversion from pointer to char/short");
1928 force_charshort_cast(dbt
);
1929 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1931 if (sbt
== VT_LLONG
) {
1932 /* from long long: just take low order word */
1936 /* if lvalue and single word type, nothing to do because
1937 the lvalue already contains the real type size (see
1938 VT_LVAL_xxx constants) */
1941 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1942 /* if we are casting between pointer types,
1943 we must update the VT_LVAL_xxx size */
1944 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1945 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1950 /* return type size. Put alignment at 'a' */
1951 ST_FUNC
int type_size(CType
*type
, int *a
)
1956 bt
= type
->t
& VT_BTYPE
;
1957 if (bt
== VT_STRUCT
) {
1960 *a
= s
->r
& 0xffffff;
1962 } else if (bt
== VT_PTR
) {
1963 if (type
->t
& VT_ARRAY
) {
1967 ts
= type_size(&s
->type
, a
);
1969 if (ts
< 0 && s
->c
< 0)
1977 } else if (bt
== VT_LDOUBLE
) {
1979 return LDOUBLE_SIZE
;
1980 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1981 #ifdef TCC_TARGET_I386
1982 #ifdef TCC_TARGET_PE
1987 #elif defined(TCC_TARGET_ARM)
1997 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2000 } else if (bt
== VT_SHORT
) {
2004 /* char, void, function, _Bool */
2010 /* return the pointed type of t */
2011 static inline CType
*pointed_type(CType
*type
)
2013 return &type
->ref
->type
;
2016 /* modify type so that its it is a pointer to type. */
2017 ST_FUNC
void mk_pointer(CType
*type
)
2020 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2021 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2025 /* compare function types. OLD functions match any new functions */
2026 static int is_compatible_func(CType
*type1
, CType
*type2
)
2032 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2034 /* check func_call */
2035 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2037 /* XXX: not complete */
2038 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2042 while (s1
!= NULL
) {
2045 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2055 /* return true if type1 and type2 are the same. If unqualified is
2056 true, qualifiers on the types are ignored.
2058 - enums are not checked as gcc __builtin_types_compatible_p ()
2060 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2064 t1
= type1
->t
& VT_TYPE
;
2065 t2
= type2
->t
& VT_TYPE
;
2067 /* strip qualifiers before comparing */
2068 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2069 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2071 /* XXX: bitfields ? */
2074 /* test more complicated cases */
2075 bt1
= t1
& VT_BTYPE
;
2076 if (bt1
== VT_PTR
) {
2077 type1
= pointed_type(type1
);
2078 type2
= pointed_type(type2
);
2079 return is_compatible_types(type1
, type2
);
2080 } else if (bt1
== VT_STRUCT
) {
2081 return (type1
->ref
== type2
->ref
);
2082 } else if (bt1
== VT_FUNC
) {
2083 return is_compatible_func(type1
, type2
);
2089 /* return true if type1 and type2 are exactly the same (including
2092 static int is_compatible_types(CType
*type1
, CType
*type2
)
2094 return compare_types(type1
,type2
,0);
2097 /* return true if type1 and type2 are the same (ignoring qualifiers).
2099 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2101 return compare_types(type1
,type2
,1);
2104 /* print a type. If 'varstr' is not NULL, then the variable is also
2105 printed in the type */
2107 /* XXX: add array and function pointers */
2108 static void type_to_str(char *buf
, int buf_size
,
2109 CType
*type
, const char *varstr
)
2116 t
= type
->t
& VT_TYPE
;
2119 if (t
& VT_CONSTANT
)
2120 pstrcat(buf
, buf_size
, "const ");
2121 if (t
& VT_VOLATILE
)
2122 pstrcat(buf
, buf_size
, "volatile ");
2123 if (t
& VT_UNSIGNED
)
2124 pstrcat(buf
, buf_size
, "unsigned ");
2154 tstr
= "long double";
2156 pstrcat(buf
, buf_size
, tstr
);
2160 if (bt
== VT_STRUCT
)
2164 pstrcat(buf
, buf_size
, tstr
);
2165 v
= type
->ref
->v
& ~SYM_STRUCT
;
2166 if (v
>= SYM_FIRST_ANOM
)
2167 pstrcat(buf
, buf_size
, "<anonymous>");
2169 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2173 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2174 pstrcat(buf
, buf_size
, "(");
2176 while (sa
!= NULL
) {
2177 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2178 pstrcat(buf
, buf_size
, buf1
);
2181 pstrcat(buf
, buf_size
, ", ");
2183 pstrcat(buf
, buf_size
, ")");
2187 pstrcpy(buf1
, sizeof(buf1
), "*");
2189 pstrcat(buf1
, sizeof(buf1
), varstr
);
2190 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2194 pstrcat(buf
, buf_size
, " ");
2195 pstrcat(buf
, buf_size
, varstr
);
2200 /* verify type compatibility to store vtop in 'dt' type, and generate
2202 static void gen_assign_cast(CType
*dt
)
2204 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2205 char buf1
[256], buf2
[256];
2208 st
= &vtop
->type
; /* source type */
2209 dbt
= dt
->t
& VT_BTYPE
;
2210 sbt
= st
->t
& VT_BTYPE
;
2211 if (dt
->t
& VT_CONSTANT
)
2212 warning("assignment of read-only location");
2215 /* special cases for pointers */
2216 /* '0' can also be a pointer */
2217 if (is_null_pointer(vtop
))
2219 /* accept implicit pointer to integer cast with warning */
2220 if (is_integer_btype(sbt
)) {
2221 warning("assignment makes pointer from integer without a cast");
2224 type1
= pointed_type(dt
);
2225 /* a function is implicitely a function pointer */
2226 if (sbt
== VT_FUNC
) {
2227 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2228 !is_compatible_types(pointed_type(dt
), st
))
2229 warning("assignment from incompatible pointer type");
2234 type2
= pointed_type(st
);
2235 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2236 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2237 /* void * can match anything */
2239 /* exact type match, except for unsigned */
2242 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2243 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2244 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2245 warning("assignment from incompatible pointer type");
2247 /* check const and volatile */
2248 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2249 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2250 warning("assignment discards qualifiers from pointer target type");
2256 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2257 warning("assignment makes integer from pointer without a cast");
2259 /* XXX: more tests */
2264 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2265 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2266 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2268 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2269 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2270 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2278 /* store vtop in lvalue pushed on stack */
2279 ST_FUNC
void vstore(void)
2281 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2283 ft
= vtop
[-1].type
.t
;
2284 sbt
= vtop
->type
.t
& VT_BTYPE
;
2285 dbt
= ft
& VT_BTYPE
;
2286 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2287 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2288 /* optimize char/short casts */
2289 delayed_cast
= VT_MUSTCAST
;
2290 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2291 /* XXX: factorize */
2292 if (ft
& VT_CONSTANT
)
2293 warning("assignment of read-only location");
2296 if (!(ft
& VT_BITFIELD
))
2297 gen_assign_cast(&vtop
[-1].type
);
2300 if (sbt
== VT_STRUCT
) {
2301 /* if structure, only generate pointer */
2302 /* structure assignment : generate memcpy */
2303 /* XXX: optimize if small size */
2304 if (!nocode_wanted
) {
2305 size
= type_size(&vtop
->type
, &align
);
2309 vtop
->type
.t
= VT_PTR
;
2312 /* address of memcpy() */
2315 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2316 else if(!(align
& 3))
2317 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2320 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2325 vtop
->type
.t
= VT_PTR
;
2334 /* leave source on stack */
2335 } else if (ft
& VT_BITFIELD
) {
2336 /* bitfield store handling */
2337 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2338 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2339 /* remove bit field info to avoid loops */
2340 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2342 /* duplicate source into other register */
2347 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2348 gen_cast(&vtop
[-1].type
);
2349 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2352 /* duplicate destination */
2354 vtop
[-1] = vtop
[-2];
2356 /* mask and shift source */
2357 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2358 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2359 vpushll((1ULL << bit_size
) - 1ULL);
2361 vpushi((1 << bit_size
) - 1);
2367 /* load destination, mask and or with source */
2369 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2370 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2372 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2379 /* pop off shifted source from "duplicate source..." above */
2383 #ifdef CONFIG_TCC_BCHECK
2384 /* bound check case */
2385 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2391 if (!nocode_wanted
) {
2395 #ifdef TCC_TARGET_X86_64
2396 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2401 r
= gv(rc
); /* generate value */
2402 /* if lvalue was saved on stack, must read it */
2403 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2405 t
= get_reg(RC_INT
);
2406 #ifdef TCC_TARGET_X86_64
2411 sv
.r
= VT_LOCAL
| VT_LVAL
;
2412 sv
.c
.ul
= vtop
[-1].c
.ul
;
2414 vtop
[-1].r
= t
| VT_LVAL
;
2417 #ifndef TCC_TARGET_X86_64
2418 /* two word case handling : store second register at word + 4 */
2419 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2421 /* convert to int to increment easily */
2422 vtop
->type
.t
= VT_INT
;
2428 /* XXX: it works because r2 is spilled last ! */
2429 store(vtop
->r2
, vtop
- 1);
2434 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2435 vtop
->r
|= delayed_cast
;
2439 /* post defines POST/PRE add. c is the token ++ or -- */
2440 ST_FUNC
void inc(int post
, int c
)
2443 vdup(); /* save lvalue */
2445 gv_dup(); /* duplicate value */
2450 vpushi(c
- TOK_MID
);
2452 vstore(); /* store value */
2454 vpop(); /* if post op, return saved value */
2457 /* Parse GNUC __attribute__ extension. Currently, the following
2458 extensions are recognized:
2459 - aligned(n) : set data/function alignment.
2460 - packed : force data alignment to 1
2461 - section(x) : generate data/code in this section.
2462 - unused : currently ignored, but may be used someday.
2463 - regparm(n) : pass function parameters in registers (i386 only)
2465 static void parse_attribute(AttributeDef
*ad
)
2469 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2473 while (tok
!= ')') {
2474 if (tok
< TOK_IDENT
)
2475 expect("attribute name");
2483 expect("section name");
2484 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2492 expect("alias(\"target\")");
2493 ad
->alias_target
= /* save string as token, for later */
2494 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2503 if (n
<= 0 || (n
& (n
- 1)) != 0)
2504 error("alignment must be a positive power of two");
2521 /* currently, no need to handle it because tcc does not
2522 track unused objects */
2526 /* currently, no need to handle it because tcc does not
2527 track unused objects */
2532 ad
->func_call
= FUNC_CDECL
;
2537 ad
->func_call
= FUNC_STDCALL
;
2539 #ifdef TCC_TARGET_I386
2549 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2555 ad
->func_call
= FUNC_FASTCALLW
;
2562 ad
->mode
= VT_LLONG
+ 1;
2565 ad
->mode
= VT_SHORT
+ 1;
2568 ad
->mode
= VT_INT
+ 1;
2571 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2578 ad
->func_export
= 1;
2581 ad
->func_import
= 1;
2584 if (tcc_state
->warn_unsupported
)
2585 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2586 /* skip parameters */
2588 int parenthesis
= 0;
2592 else if (tok
== ')')
2595 } while (parenthesis
&& tok
!= -1);
2608 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2609 static void struct_decl(CType
*type
, int u
)
2611 int a
, v
, size
, align
, maxalign
, c
, offset
;
2612 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
, resize
;
2613 Sym
*s
, *ss
, *ass
, **ps
;
2617 a
= tok
; /* save decl type */
2622 /* struct already defined ? return it */
2624 expect("struct/union/enum name");
2628 error("invalid type");
2635 /* we put an undefined size for struct/union */
2636 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2637 s
->r
= 0; /* default alignment is zero as gcc */
2638 /* put struct/union/enum name in type */
2646 error("struct/union/enum already defined");
2647 /* cannot be empty */
2649 /* non empty enums are not allowed */
2650 if (a
== TOK_ENUM
) {
2654 expect("identifier");
2660 /* enum symbols have static storage */
2661 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2662 ss
->type
.t
|= VT_STATIC
;
2667 /* NOTE: we accept a trailing comma */
2679 while (tok
!= '}') {
2680 parse_btype(&btype
, &ad
);
2686 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2687 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2688 expect("identifier");
2689 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2690 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2691 error("invalid type for '%s'",
2692 get_tok_str(v
, NULL
));
2696 bit_size
= expr_const();
2697 /* XXX: handle v = 0 case for messages */
2699 error("negative width in bit-field '%s'",
2700 get_tok_str(v
, NULL
));
2701 if (v
&& bit_size
== 0)
2702 error("zero width for bit-field '%s'",
2703 get_tok_str(v
, NULL
));
2705 size
= type_size(&type1
, &align
);
2707 if (align
< ad
.aligned
)
2709 } else if (ad
.packed
) {
2711 } else if (*tcc_state
->pack_stack_ptr
) {
2712 if (align
> *tcc_state
->pack_stack_ptr
)
2713 align
= *tcc_state
->pack_stack_ptr
;
2716 if (bit_size
>= 0) {
2717 bt
= type1
.t
& VT_BTYPE
;
2724 error("bitfields must have scalar type");
2726 if (bit_size
> bsize
) {
2727 error("width of '%s' exceeds its type",
2728 get_tok_str(v
, NULL
));
2729 } else if (bit_size
== bsize
) {
2730 /* no need for bit fields */
2732 } else if (bit_size
== 0) {
2733 /* XXX: what to do if only padding in a
2735 /* zero size: means to pad */
2738 /* we do not have enough room ?
2739 did the type change?
2741 if ((bit_pos
+ bit_size
) > bsize
||
2742 bt
!= prevbt
|| a
== TOK_UNION
)
2745 /* XXX: handle LSB first */
2746 type1
.t
|= VT_BITFIELD
|
2747 (bit_pos
<< VT_STRUCT_SHIFT
) |
2748 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2749 bit_pos
+= bit_size
;
2755 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2756 /* add new memory data only if starting
2758 if (lbit_pos
== 0) {
2759 if (a
== TOK_STRUCT
) {
2760 c
= (c
+ align
- 1) & -align
;
2771 if (align
> maxalign
)
2775 printf("add field %s offset=%d",
2776 get_tok_str(v
, NULL
), offset
);
2777 if (type1
.t
& VT_BITFIELD
) {
2778 printf(" pos=%d size=%d",
2779 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2780 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2785 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2787 while ((ass
= ass
->next
) != NULL
) {
2788 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2793 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2797 if (tok
== ';' || tok
== TOK_EOF
)
2804 /* store size and alignment */
2805 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2806 s
->r
= maxalign
| (resize
? (1 << 31) : 0);
2811 /* return 0 if no type declaration. otherwise, return the basic type
2814 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2816 int t
, u
, type_found
, typespec_found
, typedef_found
;
2820 memset(ad
, 0, sizeof(AttributeDef
));
2828 /* currently, we really ignore extension */
2838 if ((t
& VT_BTYPE
) != 0)
2839 error("too many basic types");
2855 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2856 #ifndef TCC_TARGET_PE
2857 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2859 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2860 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2874 if ((t
& VT_BTYPE
) == VT_LONG
) {
2875 #ifdef TCC_TARGET_PE
2876 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2878 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2886 struct_decl(&type1
, VT_ENUM
);
2889 type
->ref
= type1
.ref
;
2893 struct_decl(&type1
, VT_STRUCT
);
2896 /* type modifiers */
2949 /* GNUC attribute */
2950 case TOK_ATTRIBUTE1
:
2951 case TOK_ATTRIBUTE2
:
2952 parse_attribute(ad
);
2955 t
= (t
& ~VT_BTYPE
) | u
;
2963 parse_expr_type(&type1
);
2964 /* remove all storage modifiers except typedef */
2965 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
2968 if (typespec_found
|| typedef_found
)
2971 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2974 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2975 type
->ref
= s
->type
.ref
;
2977 /* get attributes from typedef */
2978 if (0 == ad
->aligned
)
2979 ad
->aligned
= FUNC_ALIGN(s
->r
);
2980 if (0 == ad
->func_call
)
2981 ad
->func_call
= FUNC_CALL(s
->r
);
2982 ad
->packed
|= FUNC_PACKED(s
->r
);
2991 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2992 error("signed and unsigned modifier");
2993 if (tcc_state
->char_is_unsigned
) {
2994 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2999 /* long is never used as type */
3000 if ((t
& VT_BTYPE
) == VT_LONG
)
3001 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3002 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3004 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3010 /* convert a function parameter type (array to pointer and function to
3011 function pointer) */
3012 static inline void convert_parameter_type(CType
*pt
)
3014 /* remove const and volatile qualifiers (XXX: const could be used
3015 to indicate a const function parameter */
3016 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3017 /* array must be transformed to pointer according to ANSI C */
3019 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3024 ST_FUNC
void parse_asm_str(CString
*astr
)
3027 /* read the string */
3029 expect("string constant");
3031 while (tok
== TOK_STR
) {
3032 /* XXX: add \0 handling too ? */
3033 cstr_cat(astr
, tokc
.cstr
->data
);
3036 cstr_ccat(astr
, '\0');
3039 /* Parse an asm label and return the label
3040 * Don't forget to free the CString in the caller! */
3041 static void asm_label_instr(CString
*astr
)
3044 parse_asm_str(astr
);
3047 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3051 static void post_type(CType
*type
, AttributeDef
*ad
)
3053 int n
, l
, t1
, arg_size
, align
;
3054 Sym
**plast
, *s
, *first
;
3059 /* function declaration */
3067 /* read param name and compute offset */
3068 if (l
!= FUNC_OLD
) {
3069 if (!parse_btype(&pt
, &ad1
)) {
3071 error("invalid type");
3078 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3080 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3081 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3082 error("parameter declared as void");
3083 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3088 expect("identifier");
3092 convert_parameter_type(&pt
);
3093 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3099 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3106 /* if no parameters, then old type prototype */
3110 t1
= type
->t
& VT_STORAGE
;
3111 /* NOTE: const is ignored in returned type as it has a special
3112 meaning in gcc / C++ */
3113 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3114 /* some ancient pre-K&R C allows a function to return an array
3115 and the array brackets to be put after the arguments, such
3116 that "int c()[]" means the same as "int[] c()" */
3117 post_type(type
, ad
);
3118 /* we push a anonymous symbol which will contain the function prototype */
3119 ad
->func_args
= arg_size
;
3120 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3122 type
->t
= t1
| VT_FUNC
;
3124 } else if (tok
== '[') {
3125 /* array definition */
3127 if (tok
== TOK_RESTRICT1
)
3133 error("invalid array size");
3136 /* parse next post type */
3137 t1
= type
->t
& VT_STORAGE
;
3138 type
->t
&= ~VT_STORAGE
;
3139 post_type(type
, ad
);
3141 /* we push a anonymous symbol which will contain the array
3143 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3145 ARRAY_RESIZE(s
->r
) = 1;
3146 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3151 /* Parse a type declaration (except basic type), and return the type
3152 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3153 expected. 'type' should contain the basic type. 'ad' is the
3154 attribute definition of the basic type. It can be modified by
3157 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3160 CType type1
, *type2
;
3163 while (tok
== '*') {
3171 qualifiers
|= VT_CONSTANT
;
3176 qualifiers
|= VT_VOLATILE
;
3184 type
->t
|= qualifiers
;
3187 /* XXX: clarify attribute handling */
3188 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3189 parse_attribute(ad
);
3191 /* recursive type */
3192 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3193 type1
.t
= 0; /* XXX: same as int */
3196 /* XXX: this is not correct to modify 'ad' at this point, but
3197 the syntax is not clear */
3198 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3199 parse_attribute(ad
);
3200 type_decl(&type1
, ad
, v
, td
);
3203 /* type identifier */
3204 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3208 if (!(td
& TYPE_ABSTRACT
))
3209 expect("identifier");
3213 post_type(type
, ad
);
3214 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3215 parse_attribute(ad
);
3219 /* append type at the end of type1 */
3232 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3233 ST_FUNC
int lvalue_type(int t
)
3238 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3240 else if (bt
== VT_SHORT
)
3244 if (t
& VT_UNSIGNED
)
3245 r
|= VT_LVAL_UNSIGNED
;
3249 /* indirection with full error checking and bound check */
3250 ST_FUNC
void indir(void)
3252 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3253 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3257 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3259 vtop
->type
= *pointed_type(&vtop
->type
);
3260 /* Arrays and functions are never lvalues */
3261 if (!(vtop
->type
.t
& VT_ARRAY
)
3262 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3263 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3264 /* if bound checking, the referenced pointer must be checked */
3265 #ifdef CONFIG_TCC_BCHECK
3266 if (tcc_state
->do_bounds_check
)
3267 vtop
->r
|= VT_MUSTBOUND
;
3272 /* pass a parameter to a function and do type checking and casting */
3273 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3278 func_type
= func
->c
;
3279 if (func_type
== FUNC_OLD
||
3280 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3281 /* default casting : only need to convert float to double */
3282 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3286 } else if (arg
== NULL
) {
3287 error("too many arguments to function");
3290 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3291 gen_assign_cast(&type
);
3295 /* parse an expression of the form '(type)' or '(expr)' and return its
3297 static void parse_expr_type(CType
*type
)
3303 if (parse_btype(type
, &ad
)) {
3304 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3311 static void parse_type(CType
*type
)
3316 if (!parse_btype(type
, &ad
)) {
3319 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3322 static void vpush_tokc(int t
)
3327 vsetc(&type
, VT_CONST
, &tokc
);
3330 ST_FUNC
void unary(void)
3332 int n
, t
, align
, size
, r
, sizeof_caller
;
3336 static int in_sizeof
= 0;
3338 sizeof_caller
= in_sizeof
;
3340 /* XXX: GCC 2.95.3 does not generate a table although it should be
3354 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3358 vpush_tokc(VT_LLONG
);
3362 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3366 vpush_tokc(VT_FLOAT
);
3370 vpush_tokc(VT_DOUBLE
);
3374 vpush_tokc(VT_LDOUBLE
);
3377 case TOK___FUNCTION__
:
3379 goto tok_identifier
;
3385 /* special function name identifier */
3386 len
= strlen(funcname
) + 1;
3387 /* generate char[len] type */
3392 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3393 ptr
= section_ptr_add(data_section
, len
);
3394 memcpy(ptr
, funcname
, len
);
3399 #ifdef TCC_TARGET_PE
3400 t
= VT_SHORT
| VT_UNSIGNED
;
3406 /* string parsing */
3409 if (tcc_state
->warn_write_strings
)
3414 memset(&ad
, 0, sizeof(AttributeDef
));
3415 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3420 if (parse_btype(&type
, &ad
)) {
3421 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3423 /* check ISOC99 compound literal */
3425 /* data is allocated locally by default */
3430 /* all except arrays are lvalues */
3431 if (!(type
.t
& VT_ARRAY
))
3432 r
|= lvalue_type(type
.t
);
3433 memset(&ad
, 0, sizeof(AttributeDef
));
3434 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3436 if (sizeof_caller
) {
3443 } else if (tok
== '{') {
3444 /* save all registers */
3446 /* statement expression : we do not accept break/continue
3447 inside as GCC does */
3448 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3463 /* functions names must be treated as function pointers,
3464 except for unary '&' and sizeof. Since we consider that
3465 functions are not lvalues, we only have to handle it
3466 there and in function calls. */
3467 /* arrays can also be used although they are not lvalues */
3468 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3469 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3471 mk_pointer(&vtop
->type
);
3477 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3479 boolean
.t
= VT_BOOL
;
3481 vtop
->c
.i
= !vtop
->c
.i
;
3482 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3483 vtop
->c
.i
= vtop
->c
.i
^ 1;
3486 vseti(VT_JMP
, gtst(1, 0));
3497 /* in order to force cast, we add zero */
3499 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3500 error("pointer not accepted for unary plus");
3510 unary_type(&type
); // Perform a in_sizeof = 0;
3511 size
= type_size(&type
, &align
);
3512 if (t
== TOK_SIZEOF
) {
3514 error("sizeof applied to an incomplete type");
3519 vtop
->type
.t
|= VT_UNSIGNED
;
3522 case TOK_builtin_types_compatible_p
:
3531 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3532 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3533 vpushi(is_compatible_types(&type1
, &type2
));
3536 case TOK_builtin_constant_p
:
3538 int saved_nocode_wanted
, res
;
3541 saved_nocode_wanted
= nocode_wanted
;
3544 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3546 nocode_wanted
= saved_nocode_wanted
;
3551 case TOK_builtin_frame_address
:
3556 if (tok
!= TOK_CINT
) {
3557 error("__builtin_frame_address only takes integers");
3560 error("TCC only supports __builtin_frame_address(0)");
3566 vset(&type
, VT_LOCAL
, 0);
3569 #ifdef TCC_TARGET_X86_64
3570 case TOK_builtin_va_arg_types
:
3572 /* This definition must be synced with stdarg.h */
3573 enum __va_arg_type
{
3574 __va_gen_reg
, __va_float_reg
, __va_stack
3582 bt
= type
.t
& VT_BTYPE
;
3583 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3585 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3586 vpushi(__va_float_reg
);
3588 vpushi(__va_gen_reg
);
3608 goto tok_identifier
;
3610 /* allow to take the address of a label */
3611 if (tok
< TOK_UIDENT
)
3612 expect("label identifier");
3613 s
= label_find(tok
);
3615 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3617 if (s
->r
== LABEL_DECLARED
)
3618 s
->r
= LABEL_FORWARD
;
3621 s
->type
.t
= VT_VOID
;
3622 mk_pointer(&s
->type
);
3623 s
->type
.t
|= VT_STATIC
;
3625 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3630 // special qnan , snan and infinity values
3632 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3636 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3640 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3649 expect("identifier");
3653 error("'%s' undeclared", get_tok_str(t
, NULL
));
3654 /* for simple function calls, we tolerate undeclared
3655 external reference to int() function */
3656 if (tcc_state
->warn_implicit_function_declaration
)
3657 warning("implicit declaration of function '%s'",
3658 get_tok_str(t
, NULL
));
3659 s
= external_global_sym(t
, &func_old_type
, 0);
3661 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3662 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3663 /* if referencing an inline function, then we generate a
3664 symbol to it if not already done. It will have the
3665 effect to generate code for it at the end of the
3666 compilation unit. Inline function as always
3667 generated in the text section. */
3669 put_extern_sym(s
, text_section
, 0, 0);
3670 r
= VT_SYM
| VT_CONST
;
3674 vset(&s
->type
, r
, s
->c
);
3675 /* if forward reference, we must point to s */
3676 if (vtop
->r
& VT_SYM
) {
3683 /* post operations */
3685 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3688 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3691 if (tok
== TOK_ARROW
)
3693 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3697 /* expect pointer on structure */
3698 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3699 expect("struct or union");
3703 while ((s
= s
->next
) != NULL
) {
3708 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3709 /* add field offset to pointer */
3710 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3713 /* change type to field type, and set to lvalue */
3714 vtop
->type
= s
->type
;
3715 vtop
->type
.t
|= qualifiers
;
3716 /* an array is never an lvalue */
3717 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3718 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3719 #ifdef CONFIG_TCC_BCHECK
3720 /* if bound checking, the referenced pointer must be checked */
3721 if (tcc_state
->do_bounds_check
)
3722 vtop
->r
|= VT_MUSTBOUND
;
3726 } else if (tok
== '[') {
3732 } else if (tok
== '(') {
3738 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3739 /* pointer test (no array accepted) */
3740 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3741 vtop
->type
= *pointed_type(&vtop
->type
);
3742 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3746 expect("function pointer");
3749 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3751 /* get return type */
3754 sa
= s
->next
; /* first parameter */
3757 /* compute first implicit argument if a structure is returned */
3758 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3759 /* get some space for the returned structure */
3760 size
= type_size(&s
->type
, &align
);
3761 loc
= (loc
- size
) & -align
;
3763 ret
.r
= VT_LOCAL
| VT_LVAL
;
3764 /* pass it as 'int' to avoid structure arg passing
3766 vseti(VT_LOCAL
, loc
);
3771 /* return in register */
3772 if (is_float(ret
.type
.t
)) {
3773 ret
.r
= reg_fret(ret
.type
.t
);
3775 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3784 gfunc_param_typed(s
, sa
);
3794 error("too few arguments to function");
3796 if (!nocode_wanted
) {
3797 gfunc_call(nb_args
);
3799 vtop
-= (nb_args
+ 1);
3802 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3810 ST_FUNC
void expr_prod(void)
3815 while (tok
== '*' || tok
== '/' || tok
== '%') {
3823 ST_FUNC
void expr_sum(void)
3828 while (tok
== '+' || tok
== '-') {
3836 static void expr_shift(void)
3841 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3849 static void expr_cmp(void)
3854 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3855 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3863 static void expr_cmpeq(void)
3868 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3876 static void expr_and(void)
3879 while (tok
== '&') {
3886 static void expr_xor(void)
3889 while (tok
== '^') {
3896 static void expr_or(void)
3899 while (tok
== '|') {
3906 /* XXX: fix this mess */
3907 static void expr_land_const(void)
3910 while (tok
== TOK_LAND
) {
3917 /* XXX: fix this mess */
3918 static void expr_lor_const(void)
3921 while (tok
== TOK_LOR
) {
3928 /* only used if non constant */
3929 static void expr_land(void)
3934 if (tok
== TOK_LAND
) {
3939 if (tok
!= TOK_LAND
) {
3949 static void expr_lor(void)
3954 if (tok
== TOK_LOR
) {
3959 if (tok
!= TOK_LOR
) {
3969 /* XXX: better constant handling */
3970 static void expr_cond(void)
3972 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3974 CType type
, type1
, type2
;
3981 boolean
.t
= VT_BOOL
;
3987 if (tok
!= ':' || !gnu_ext
) {
4002 if (vtop
!= vstack
) {
4003 /* needed to avoid having different registers saved in
4005 if (is_float(vtop
->type
.t
)) {
4007 #ifdef TCC_TARGET_X86_64
4008 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4018 if (tok
== ':' && gnu_ext
) {
4026 sv
= *vtop
; /* save value to handle it later */
4027 vtop
--; /* no vpop so that FP stack is not flushed */
4035 bt1
= t1
& VT_BTYPE
;
4037 bt2
= t2
& VT_BTYPE
;
4038 /* cast operands to correct type according to ISOC rules */
4039 if (is_float(bt1
) || is_float(bt2
)) {
4040 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4041 type
.t
= VT_LDOUBLE
;
4042 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4047 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4048 /* cast to biggest op */
4050 /* convert to unsigned if it does not fit in a long long */
4051 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4052 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4053 type
.t
|= VT_UNSIGNED
;
4054 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4055 /* XXX: test pointer compatibility */
4057 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4058 /* XXX: test function pointer compatibility */
4060 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4061 /* XXX: test structure compatibility */
4063 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4064 /* NOTE: as an extension, we accept void on only one side */
4067 /* integer operations */
4069 /* convert to unsigned if it does not fit in an integer */
4070 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4071 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4072 type
.t
|= VT_UNSIGNED
;
4075 /* now we convert second operand */
4077 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4080 if (is_float(type
.t
)) {
4082 #ifdef TCC_TARGET_X86_64
4083 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4087 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4088 /* for long longs, we use fixed registers to avoid having
4089 to handle a complicated move */
4094 /* this is horrible, but we must also convert first
4098 /* put again first value and cast it */
4101 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4111 static void expr_eq(void)
4117 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4118 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4119 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4134 ST_FUNC
void gexpr(void)
4145 /* parse an expression and return its type without any side effect. */
4146 static void expr_type(CType
*type
)
4148 int saved_nocode_wanted
;
4150 saved_nocode_wanted
= nocode_wanted
;
4155 nocode_wanted
= saved_nocode_wanted
;
4158 /* parse a unary expression and return its type without any side
4160 static void unary_type(CType
*type
)
4172 /* parse a constant expression and return value in vtop. */
4173 static void expr_const1(void)
4182 /* parse an integer constant and return its value. */
4183 ST_FUNC
int expr_const(void)
4187 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4188 expect("constant expression");
4194 /* return the label token if current token is a label, otherwise
4196 static int is_label(void)
4200 /* fast test first */
4201 if (tok
< TOK_UIDENT
)
4203 /* no need to save tokc because tok is an identifier */
4210 unget_tok(last_tok
);
4215 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4216 int case_reg
, int is_expr
)
4221 /* generate line number info */
4222 if (tcc_state
->do_debug
&&
4223 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4224 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4226 last_line_num
= file
->line_num
;
4230 /* default return value is (void) */
4232 vtop
->type
.t
= VT_VOID
;
4235 if (tok
== TOK_IF
) {
4242 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4244 if (c
== TOK_ELSE
) {
4248 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4249 gsym(d
); /* patch else jmp */
4252 } else if (tok
== TOK_WHILE
) {
4260 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4264 } else if (tok
== '{') {
4268 /* record local declaration stack position */
4270 llabel
= local_label_stack
;
4271 /* handle local labels declarations */
4272 if (tok
== TOK_LABEL
) {
4275 if (tok
< TOK_UIDENT
)
4276 expect("label identifier");
4277 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4287 while (tok
!= '}') {
4292 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4295 /* pop locally defined labels */
4296 label_pop(&local_label_stack
, llabel
);
4297 /* pop locally defined symbols */
4299 /* XXX: this solution makes only valgrind happy...
4300 triggered by gcc.c-torture/execute/20000917-1.c */
4302 switch(vtop
->type
.t
& VT_BTYPE
) {
4307 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4309 error("unsupported expression type");
4312 sym_pop(&local_stack
, s
);
4314 } else if (tok
== TOK_RETURN
) {
4318 gen_assign_cast(&func_vt
);
4319 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4321 /* if returning structure, must copy it to implicit
4322 first pointer arg location */
4325 size
= type_size(&func_vt
,&align
);
4328 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4332 loc
= (loc
- size
) & -4;
4335 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4338 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4340 vtop
->type
= int_type
;
4346 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4349 /* copy structure value to pointer */
4354 } else if (is_float(func_vt
.t
)) {
4355 gv(rc_fret(func_vt
.t
));
4359 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4362 rsym
= gjmp(rsym
); /* jmp */
4363 } else if (tok
== TOK_BREAK
) {
4366 error("cannot break");
4367 *bsym
= gjmp(*bsym
);
4370 } else if (tok
== TOK_CONTINUE
) {
4373 error("cannot continue");
4374 *csym
= gjmp(*csym
);
4377 } else if (tok
== TOK_FOR
) {
4403 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4408 if (tok
== TOK_DO
) {
4413 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4424 if (tok
== TOK_SWITCH
) {
4428 /* XXX: other types than integer */
4429 case_reg
= gv(RC_INT
);
4433 b
= gjmp(0); /* jump to first case */
4435 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4436 /* if no default, jmp after switch */
4444 if (tok
== TOK_CASE
) {
4451 if (gnu_ext
&& tok
== TOK_DOTS
) {
4455 warning("empty case range");
4457 /* since a case is like a label, we must skip it with a jmp */
4464 *case_sym
= gtst(1, 0);
4467 *case_sym
= gtst(1, 0);
4471 *case_sym
= gtst(1, *case_sym
);
4476 goto block_after_label
;
4478 if (tok
== TOK_DEFAULT
) {
4484 error("too many 'default'");
4487 goto block_after_label
;
4489 if (tok
== TOK_GOTO
) {
4491 if (tok
== '*' && gnu_ext
) {
4495 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4498 } else if (tok
>= TOK_UIDENT
) {
4499 s
= label_find(tok
);
4500 /* put forward definition if needed */
4502 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4504 if (s
->r
== LABEL_DECLARED
)
4505 s
->r
= LABEL_FORWARD
;
4507 /* label already defined */
4508 if (s
->r
& LABEL_FORWARD
)
4509 s
->jnext
= gjmp(s
->jnext
);
4511 gjmp_addr(s
->jnext
);
4514 expect("label identifier");
4517 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4525 if (s
->r
== LABEL_DEFINED
)
4526 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4528 s
->r
= LABEL_DEFINED
;
4530 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4533 /* we accept this, but it is a mistake */
4536 warning("deprecated use of label at end of compound statement");
4540 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4543 /* expression case */
4558 /* t is the array or struct type. c is the array or struct
4559 address. cur_index/cur_field is the pointer to the current
4560 value. 'size_only' is true if only size info is needed (only used
4562 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4563 int *cur_index
, Sym
**cur_field
,
4567 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4573 if (gnu_ext
&& (l
= is_label()) != 0)
4575 while (tok
== '[' || tok
== '.') {
4577 if (!(type
->t
& VT_ARRAY
))
4578 expect("array type");
4581 index
= expr_const();
4582 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4583 expect("invalid index");
4584 if (tok
== TOK_DOTS
&& gnu_ext
) {
4586 index_last
= expr_const();
4587 if (index_last
< 0 ||
4588 (s
->c
>= 0 && index_last
>= s
->c
) ||
4590 expect("invalid index");
4596 *cur_index
= index_last
;
4597 type
= pointed_type(type
);
4598 elem_size
= type_size(type
, &align
);
4599 c
+= index
* elem_size
;
4600 /* NOTE: we only support ranges for last designator */
4601 nb_elems
= index_last
- index
+ 1;
4602 if (nb_elems
!= 1) {
4611 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4612 expect("struct/union type");
4625 /* XXX: fix this mess by using explicit storage field */
4627 type1
.t
|= (type
->t
& ~VT_TYPE
);
4641 if (type
->t
& VT_ARRAY
) {
4643 type
= pointed_type(type
);
4644 c
+= index
* type_size(type
, &align
);
4648 error("too many field init");
4649 /* XXX: fix this mess by using explicit storage field */
4651 type1
.t
|= (type
->t
& ~VT_TYPE
);
4656 decl_initializer(type
, sec
, c
, 0, size_only
);
4658 /* XXX: make it more general */
4659 if (!size_only
&& nb_elems
> 1) {
4660 unsigned long c_end
;
4665 error("range init not supported yet for dynamic storage");
4666 c_end
= c
+ nb_elems
* elem_size
;
4667 if (c_end
> sec
->data_allocated
)
4668 section_realloc(sec
, c_end
);
4669 src
= sec
->data
+ c
;
4671 for(i
= 1; i
< nb_elems
; i
++) {
4673 memcpy(dst
, src
, elem_size
);
4679 #define EXPR_CONST 1
4682 /* store a value or an expression directly in global data or in local array */
4683 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4684 int v
, int expr_type
)
4686 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4688 unsigned long long bit_mask
;
4696 /* compound literals must be allocated globally in this case */
4697 saved_global_expr
= global_expr
;
4700 global_expr
= saved_global_expr
;
4701 /* NOTE: symbols are accepted */
4702 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4703 error("initializer element is not constant");
4711 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4714 /* XXX: not portable */
4715 /* XXX: generate error if incorrect relocation */
4716 gen_assign_cast(&dtype
);
4717 bt
= type
->t
& VT_BTYPE
;
4718 /* we'll write at most 12 bytes */
4719 if (c
+ 12 > sec
->data_allocated
) {
4720 section_realloc(sec
, c
+ 12);
4722 ptr
= sec
->data
+ c
;
4723 /* XXX: make code faster ? */
4724 if (!(type
->t
& VT_BITFIELD
)) {
4729 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4730 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4731 bit_mask
= (1LL << bit_size
) - 1;
4733 if ((vtop
->r
& VT_SYM
) &&
4739 (bt
== VT_INT
&& bit_size
!= 32)))
4740 error("initializer element is not computable at load time");
4743 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4745 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4748 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4751 *(double *)ptr
= vtop
->c
.d
;
4754 *(long double *)ptr
= vtop
->c
.ld
;
4757 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4760 if (vtop
->r
& VT_SYM
) {
4761 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4763 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4768 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4775 /* put zeros for variable based init */
4776 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4779 /* nothing to do because globals are already set to zero */
4781 vpush_global_sym(&func_old_type
, TOK_memset
);
4789 /* 't' contains the type and storage info. 'c' is the offset of the
4790 object in section 'sec'. If 'sec' is NULL, it means stack based
4791 allocation. 'first' is true if array '{' must be read (multi
4792 dimension implicit array init handling). 'size_only' is true if
4793 size only evaluation is wanted (only for arrays). */
4794 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4795 int first
, int size_only
)
4797 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4798 int size1
, align1
, expr_type
;
4802 if (type
->t
& VT_ARRAY
) {
4806 t1
= pointed_type(type
);
4807 size1
= type_size(t1
, &align1
);
4810 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4813 error("character array initializer must be a literal,"
4814 " optionally enclosed in braces");
4819 /* only parse strings here if correct type (otherwise: handle
4820 them as ((w)char *) expressions */
4821 if ((tok
== TOK_LSTR
&&
4822 #ifdef TCC_TARGET_PE
4823 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4825 (t1
->t
& VT_BTYPE
) == VT_INT
4827 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4828 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4833 /* compute maximum number of chars wanted */
4835 cstr_len
= cstr
->size
;
4837 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4840 if (n
>= 0 && nb
> (n
- array_length
))
4841 nb
= n
- array_length
;
4844 warning("initializer-string for array is too long");
4845 /* in order to go faster for common case (char
4846 string in global variable, we handle it
4848 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4849 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4853 ch
= ((unsigned char *)cstr
->data
)[i
];
4855 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4856 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4864 /* only add trailing zero if enough storage (no
4865 warning in this case since it is standard) */
4866 if (n
< 0 || array_length
< n
) {
4868 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4874 if (ARRAY_RESIZE(s
->r
))
4876 while (tok
!= '}') {
4877 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4878 if (n
>= 0 && index
>= n
)
4879 error("index too large");
4880 /* must put zero in holes (note that doing it that way
4881 ensures that it even works with designators) */
4882 if (!size_only
&& array_length
< index
) {
4883 init_putz(t1
, sec
, c
+ array_length
* size1
,
4884 (index
- array_length
) * size1
);
4887 if (index
> array_length
)
4888 array_length
= index
;
4889 /* special test for multi dimensional arrays (may not
4890 be strictly correct if designators are used at the
4892 if (index
>= n
&& no_oblock
)
4901 /* put zeros at the end */
4902 if (!size_only
&& n
>= 0 && array_length
< n
) {
4903 init_putz(t1
, sec
, c
+ array_length
* size1
,
4904 (n
- array_length
) * size1
);
4906 /* patch type size if needed */
4908 s
->c
= array_length
;
4909 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4910 (sec
|| !first
|| tok
== '{')) {
4913 /* NOTE: the previous test is a specific case for automatic
4914 struct/union init */
4915 /* XXX: union needs only one init */
4917 /* XXX: this test is incorrect for local initializers
4918 beginning with ( without {. It would be much more difficult
4919 to do it correctly (ideally, the expression parser should
4920 be used in all cases) */
4926 while (tok
== '(') {
4930 if (!parse_btype(&type1
, &ad1
))
4932 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4934 if (!is_assignable_types(type
, &type1
))
4935 error("invalid type for cast");
4940 if (first
|| tok
== '{') {
4951 while (tok
!= '}') {
4952 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4954 if (!size_only
&& array_length
< index
) {
4955 init_putz(type
, sec
, c
+ array_length
,
4956 index
- array_length
);
4958 index
= index
+ type_size(&f
->type
, &align1
);
4959 if (index
> array_length
)
4960 array_length
= index
;
4962 /* gr: skip fields from same union - ugly. */
4964 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4965 /* test for same offset */
4966 if (f
->next
->c
!= f
->c
)
4968 /* if yes, test for bitfield shift */
4969 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4970 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4971 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4972 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4973 if (bit_pos_1
!= bit_pos_2
)
4980 if (no_oblock
&& f
== NULL
)
4986 /* put zeros at the end */
4987 if (!size_only
&& n
>= 0 && array_length
< n
) {
4988 init_putz(type
, sec
, c
+ array_length
,
4998 s
->c
= array_length
;
4999 } else if (tok
== '{') {
5001 decl_initializer(type
, sec
, c
, first
, size_only
);
5003 } else if (size_only
) {
5004 /* just skip expression */
5005 parlevel
= parlevel1
= 0;
5006 while ((parlevel
> 0 || parlevel1
> 0 ||
5007 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5010 else if (tok
== ')')
5012 else if (tok
== '{')
5014 else if (tok
== '}')
5019 /* currently, we always use constant expression for globals
5020 (may change for scripting case) */
5021 expr_type
= EXPR_CONST
;
5023 expr_type
= EXPR_ANY
;
5024 init_putv(type
, sec
, c
, 0, expr_type
);
5028 /* parse an initializer for type 't' if 'has_init' is non zero, and
5029 allocate space in local or global data space ('r' is either
5030 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5031 variable 'v' with an associated name represented by 'asm_label' of
5032 scope 'scope' is declared before initializers are parsed. If 'v' is
5033 zero, then a reference to the new object is put in the value stack.
5034 If 'has_init' is 2, a special parsing is done to handle string
5036 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5037 int has_init
, int v
, char *asm_label
,
5040 int size
, align
, addr
, data_offset
;
5042 ParseState saved_parse_state
= {0};
5043 TokenString init_str
;
5046 /* resize the struct */
5047 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&& (type
->ref
->r
& (1<<31)) != 0)
5049 size
= type_size(type
, &align
);
5050 /* If unknown size, we must evaluate it before
5051 evaluating initializers because
5052 initializers can generate global data too
5053 (e.g. string pointers or ISOC99 compound
5054 literals). It also simplifies local
5055 initializers handling */
5056 tok_str_new(&init_str
);
5059 error("unknown type size");
5060 /* get all init string */
5061 if (has_init
== 2) {
5062 /* only get strings */
5063 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5064 tok_str_add_tok(&init_str
);
5069 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5071 error("unexpected end of file in initializer");
5072 tok_str_add_tok(&init_str
);
5075 else if (tok
== '}') {
5085 tok_str_add(&init_str
, -1);
5086 tok_str_add(&init_str
, 0);
5089 save_parse_state(&saved_parse_state
);
5091 macro_ptr
= init_str
.str
;
5093 decl_initializer(type
, NULL
, 0, 1, 1);
5094 /* prepare second initializer parsing */
5095 macro_ptr
= init_str
.str
;
5098 /* if still unknown size, error */
5099 size
= type_size(type
, &align
);
5101 error("unknown type size");
5103 /* take into account specified alignment if bigger */
5105 if (ad
->aligned
> align
)
5106 align
= ad
->aligned
;
5107 } else if (ad
->packed
) {
5110 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5112 #ifdef CONFIG_TCC_BCHECK
5113 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
5116 loc
= (loc
- size
) & -align
;
5118 #ifdef CONFIG_TCC_BCHECK
5119 /* handles bounds */
5120 /* XXX: currently, since we do only one pass, we cannot track
5121 '&' operators, so we add only arrays */
5122 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5123 unsigned long *bounds_ptr
;
5124 /* add padding between regions */
5126 /* then add local bound info */
5127 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5128 bounds_ptr
[0] = addr
;
5129 bounds_ptr
[1] = size
;
5133 /* local variable */
5134 sym_push(v
, type
, r
, addr
);
5136 /* push local reference */
5137 vset(type
, r
, addr
);
5143 if (v
&& scope
== VT_CONST
) {
5144 /* see if the symbol was already defined */
5147 if (!is_compatible_types(&sym
->type
, type
))
5148 error("incompatible types for redefinition of '%s'",
5149 get_tok_str(v
, NULL
));
5150 if (sym
->type
.t
& VT_EXTERN
) {
5151 /* if the variable is extern, it was not allocated */
5152 sym
->type
.t
&= ~VT_EXTERN
;
5153 /* set array size if it was ommited in extern
5155 if ((sym
->type
.t
& VT_ARRAY
) &&
5156 sym
->type
.ref
->c
< 0 &&
5158 sym
->type
.ref
->c
= type
->ref
->c
;
5160 /* we accept several definitions of the same
5161 global variable. this is tricky, because we
5162 must play with the SHN_COMMON type of the symbol */
5163 /* XXX: should check if the variable was already
5164 initialized. It is incorrect to initialized it
5166 /* no init data, we won't add more to the symbol */
5173 /* allocate symbol in corresponding section */
5178 else if (tcc_state
->nocommon
)
5182 data_offset
= sec
->data_offset
;
5183 data_offset
= (data_offset
+ align
- 1) & -align
;
5185 /* very important to increment global pointer at this time
5186 because initializers themselves can create new initializers */
5187 data_offset
+= size
;
5188 #ifdef CONFIG_TCC_BCHECK
5189 /* add padding if bound check */
5190 if (tcc_state
->do_bounds_check
)
5193 sec
->data_offset
= data_offset
;
5194 /* allocate section space to put the data */
5195 if (sec
->sh_type
!= SHT_NOBITS
&&
5196 data_offset
> sec
->data_allocated
)
5197 section_realloc(sec
, data_offset
);
5198 /* align section if needed */
5199 if (align
> sec
->sh_addralign
)
5200 sec
->sh_addralign
= align
;
5202 addr
= 0; /* avoid warning */
5206 if (scope
!= VT_CONST
|| !sym
) {
5207 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5208 sym
->asm_label
= asm_label
;
5210 /* update symbol definition */
5212 put_extern_sym(sym
, sec
, addr
, size
);
5215 /* put a common area */
5216 put_extern_sym(sym
, NULL
, align
, size
);
5217 /* XXX: find a nicer way */
5218 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5219 esym
->st_shndx
= SHN_COMMON
;
5224 /* push global reference */
5225 sym
= get_sym_ref(type
, sec
, addr
, size
);
5227 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5230 /* patch symbol weakness */
5231 if (type
->t
& VT_WEAK
)
5233 #ifdef CONFIG_TCC_BCHECK
5234 /* handles bounds now because the symbol must be defined
5235 before for the relocation */
5236 if (tcc_state
->do_bounds_check
) {
5237 unsigned long *bounds_ptr
;
5239 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5240 /* then add global bound info */
5241 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5242 bounds_ptr
[0] = 0; /* relocated */
5243 bounds_ptr
[1] = size
;
5248 decl_initializer(type
, sec
, addr
, 1, 0);
5249 /* restore parse state if needed */
5251 tok_str_free(init_str
.str
);
5252 restore_parse_state(&saved_parse_state
);
5258 static void put_func_debug(Sym
*sym
)
5263 /* XXX: we put here a dummy type */
5264 snprintf(buf
, sizeof(buf
), "%s:%c1",
5265 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5266 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5267 cur_text_section
, sym
->c
);
5268 /* //gr gdb wants a line at the function */
5269 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5274 /* parse an old style function declaration list */
5275 /* XXX: check multiple parameter */
5276 static void func_decl_list(Sym
*func_sym
)
5283 /* parse each declaration */
5284 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5285 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5286 if (!parse_btype(&btype
, &ad
))
5287 expect("declaration list");
5288 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5289 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5291 /* we accept no variable after */
5295 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5296 /* find parameter in function parameter list */
5299 if ((s
->v
& ~SYM_FIELD
) == v
)
5303 error("declaration for parameter '%s' but no such parameter",
5304 get_tok_str(v
, NULL
));
5306 /* check that no storage specifier except 'register' was given */
5307 if (type
.t
& VT_STORAGE
)
5308 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5309 convert_parameter_type(&type
);
5310 /* we can add the type (NOTE: it could be local to the function) */
5312 /* accept other parameters */
5323 /* parse a function defined by symbol 'sym' and generate its code in
5324 'cur_text_section' */
5325 static void gen_function(Sym
*sym
)
5327 int saved_nocode_wanted
= nocode_wanted
;
5329 ind
= cur_text_section
->data_offset
;
5330 /* NOTE: we patch the symbol size later */
5331 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5332 funcname
= get_tok_str(sym
->v
, NULL
);
5334 /* put debug symbol */
5335 if (tcc_state
->do_debug
)
5336 put_func_debug(sym
);
5337 /* push a dummy symbol to enable local sym storage */
5338 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5339 gfunc_prolog(&sym
->type
);
5341 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5344 cur_text_section
->data_offset
= ind
;
5345 label_pop(&global_label_stack
, NULL
);
5346 sym_pop(&local_stack
, NULL
); /* reset local stack */
5347 /* end of function */
5348 /* patch symbol size */
5349 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5351 /* patch symbol weakness (this definition overrules any prototype) */
5352 if (sym
->type
.t
& VT_WEAK
)
5354 if (tcc_state
->do_debug
) {
5355 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5357 /* It's better to crash than to generate wrong code */
5358 cur_text_section
= NULL
;
5359 funcname
= ""; /* for safety */
5360 func_vt
.t
= VT_VOID
; /* for safety */
5361 ind
= 0; /* for safety */
5362 nocode_wanted
= saved_nocode_wanted
;
5365 ST_FUNC
void gen_inline_functions(void)
5368 int *str
, inline_generated
, i
;
5369 struct InlineFunc
*fn
;
5371 /* iterate while inline function are referenced */
5373 inline_generated
= 0;
5374 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5375 fn
= tcc_state
->inline_fns
[i
];
5377 if (sym
&& sym
->c
) {
5378 /* the function was used: generate its code and
5379 convert it to a normal function */
5380 str
= fn
->token_str
;
5383 strcpy(file
->filename
, fn
->filename
);
5384 sym
->r
= VT_SYM
| VT_CONST
;
5385 sym
->type
.t
&= ~VT_INLINE
;
5389 cur_text_section
= text_section
;
5391 macro_ptr
= NULL
; /* fail safe */
5393 inline_generated
= 1;
5396 if (!inline_generated
)
5399 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5400 fn
= tcc_state
->inline_fns
[i
];
5401 str
= fn
->token_str
;
5404 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5407 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5408 static void decl0(int l
, int is_for_loop_init
)
5416 if (!parse_btype(&btype
, &ad
)) {
5417 if (is_for_loop_init
) {
5422 /* skip redundant ';' */
5423 /* XXX: find more elegant solution */
5428 if (l
== VT_CONST
&&
5429 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5430 /* global asm block */
5434 /* special test for old K&R protos without explicit int
5435 type. Only accepted when defining global data */
5436 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5440 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5441 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5443 /* we accept no variable after */
5447 while (1) { /* iterate thru each declaration */
5448 char *asm_label
; // associated asm label
5450 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5454 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5455 printf("type = '%s'\n", buf
);
5458 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5459 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5460 error("function without file scope cannot be static");
5462 /* if old style function prototype, we accept a
5465 if (sym
->c
== FUNC_OLD
)
5466 func_decl_list(sym
);
5470 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5473 asm_label_instr(&astr
);
5474 asm_label
= tcc_strdup(astr
.data
);
5477 /* parse one last attribute list, after asm label */
5478 parse_attribute(&ad
);
5483 #ifdef TCC_TARGET_PE
5485 type
.t
|= VT_IMPORT
;
5487 type
.t
|= VT_EXPORT
;
5491 error("cannot use local functions");
5492 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5493 expect("function definition");
5495 /* reject abstract declarators in function definition */
5497 while ((sym
= sym
->next
) != NULL
)
5498 if (!(sym
->v
& ~SYM_FIELD
))
5499 expect("identifier");
5501 /* XXX: cannot do better now: convert extern line to static inline */
5502 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5503 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5507 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5510 r
= sym
->type
.ref
->r
;
5511 /* use func_call from prototype if not defined */
5512 if (FUNC_CALL(r
) != FUNC_CDECL
5513 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5514 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5516 /* use export from prototype */
5518 FUNC_EXPORT(type
.ref
->r
) = 1;
5520 /* use static from prototype */
5521 if (sym
->type
.t
& VT_STATIC
)
5522 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5524 if (!is_compatible_types(&sym
->type
, &type
)) {
5526 error("incompatible types for redefinition of '%s'",
5527 get_tok_str(v
, NULL
));
5529 /* if symbol is already defined, then put complete type */
5532 /* put function symbol */
5533 sym
= global_identifier_push(v
, type
.t
, 0);
5534 sym
->type
.ref
= type
.ref
;
5537 /* static inline functions are just recorded as a kind
5538 of macro. Their code will be emitted at the end of
5539 the compilation unit only if they are used */
5540 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5541 (VT_INLINE
| VT_STATIC
)) {
5542 TokenString func_str
;
5544 struct InlineFunc
*fn
;
5545 const char *filename
;
5547 tok_str_new(&func_str
);
5553 error("unexpected end of file");
5554 tok_str_add_tok(&func_str
);
5559 } else if (t
== '}') {
5561 if (block_level
== 0)
5565 tok_str_add(&func_str
, -1);
5566 tok_str_add(&func_str
, 0);
5567 filename
= file
? file
->filename
: "";
5568 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5569 strcpy(fn
->filename
, filename
);
5571 fn
->token_str
= func_str
.str
;
5572 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5575 /* compute text section */
5576 cur_text_section
= ad
.section
;
5577 if (!cur_text_section
)
5578 cur_text_section
= text_section
;
5579 sym
->r
= VT_SYM
| VT_CONST
;
5584 if (btype
.t
& VT_TYPEDEF
) {
5585 /* save typedefed type */
5586 /* XXX: test storage specifiers ? */
5587 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5588 sym
->type
.t
|= VT_TYPEDEF
;
5591 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5592 /* external function definition */
5593 /* specific case for func_call attribute */
5594 type
.ref
->r
= INT_ATTR(&ad
);
5595 } else if (!(type
.t
& VT_ARRAY
)) {
5596 /* not lvalue if array */
5597 r
|= lvalue_type(type
.t
);
5599 has_init
= (tok
== '=');
5600 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5601 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5602 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5603 /* external variable or function */
5604 /* NOTE: as GCC, uninitialized global static
5605 arrays of null size are considered as
5607 sym
= external_sym(v
, &type
, r
, asm_label
);
5609 if (type
.t
& VT_WEAK
)
5612 if (ad
.alias_target
) {
5617 alias_target
= sym_find(ad
.alias_target
);
5618 if (!alias_target
|| !alias_target
->c
)
5619 error("unsupported forward __alias__ attribute");
5620 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5621 tsec
.sh_num
= esym
->st_shndx
;
5622 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5625 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5626 if (type
.t
& VT_STATIC
)
5632 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5636 if (is_for_loop_init
)
5647 ST_FUNC
void for_loop_init(void)
5652 ST_FUNC
void decl(int l
)