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
, int scope
);
79 static void expr_eq(void);
80 static void unary_type(CType
*type
);
81 static void vla_runtime_type_size(CType
*type
, int *a
);
82 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
83 static void expr_type(CType
*type
);
85 ST_INLN
int is_float(int t
)
89 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
92 ST_FUNC
void test_lvalue(void)
94 if (!(vtop
->r
& VT_LVAL
))
98 /* ------------------------------------------------------------------------- */
99 /* symbol allocator */
100 static Sym
*__sym_malloc(void)
102 Sym
*sym_pool
, *sym
, *last_sym
;
105 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
106 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
108 last_sym
= sym_free_first
;
110 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
111 sym
->next
= last_sym
;
115 sym_free_first
= last_sym
;
119 static inline Sym
*sym_malloc(void)
122 sym
= sym_free_first
;
124 sym
= __sym_malloc();
125 sym_free_first
= sym
->next
;
129 ST_INLN
void sym_free(Sym
*sym
)
131 sym
->next
= sym_free_first
;
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 /* ------------------------------------------------------------------------- */
260 ST_FUNC
void swap(int *p
, int *q
)
268 static void vsetc(CType
*type
, int r
, CValue
*vc
)
272 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
273 error("memory full");
274 /* cannot let cpu flags if other instruction are generated. Also
275 avoid leaving VT_JMP anywhere except on the top of the stack
276 because it would complicate the code generator. */
277 if (vtop
>= vstack
) {
278 v
= vtop
->r
& VT_VALMASK
;
279 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
289 /* push constant of type "type" with useless value */
290 void vpush(CType
*type
)
293 vsetc(type
, VT_CONST
, &cval
);
296 /* push integer constant */
297 ST_FUNC
void vpushi(int v
)
301 vsetc(&int_type
, VT_CONST
, &cval
);
304 /* push long long constant */
305 static void vpushll(long long v
)
312 vsetc(&ctype
, VT_CONST
, &cval
);
315 /* push arbitrary 64bit constant */
316 void vpush64(int ty
, unsigned long long v
)
322 vsetc(&ctype
, VT_CONST
, &cval
);
325 /* Return a static symbol pointing to a section */
326 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
332 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
333 sym
->type
.ref
= type
->ref
;
334 sym
->r
= VT_CONST
| VT_SYM
;
335 put_extern_sym(sym
, sec
, offset
, size
);
339 /* push a reference to a section offset by adding a dummy symbol */
340 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
345 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
346 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
349 /* define a new external reference to a symbol 'v' of type 'u' */
350 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
356 /* push forward reference */
357 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
358 s
->type
.ref
= type
->ref
;
359 s
->r
= r
| VT_CONST
| VT_SYM
;
364 /* define a new external reference to a symbol 'v' of type 'u' */
365 static Sym
*external_sym(int v
, CType
*type
, int r
)
371 /* push forward reference */
372 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
373 if (type
&& type
->ref
&& type
->ref
->a
)
375 s
->type
.t
|= VT_EXTERN
;
376 } else if (s
->type
.ref
== func_old_type
.ref
) {
377 s
->type
.ref
= type
->ref
;
378 s
->r
= r
| VT_CONST
| VT_SYM
;
379 s
->type
.t
|= VT_EXTERN
;
380 } else if (!is_compatible_types(&s
->type
, type
)) {
381 error("incompatible types for redefinition of '%s'",
382 get_tok_str(v
, NULL
));
387 /* push a reference to global symbol v */
388 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
393 sym
= external_global_sym(v
, type
, 0);
395 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
399 ST_FUNC
void vset(CType
*type
, int r
, int v
)
404 vsetc(type
, r
, &cval
);
407 static void vseti(int r
, int v
)
415 ST_FUNC
void vswap(void)
424 ST_FUNC
void vpushv(SValue
*v
)
426 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
427 error("memory full");
432 static void vdup(void)
437 /* save r to the memory stack, and mark it as being free */
438 ST_FUNC
void save_reg(int r
)
440 int l
, saved
, size
, align
;
444 /* modify all stack values */
447 for(p
=vstack
;p
<=vtop
;p
++) {
448 if ((p
->r
& VT_VALMASK
) == r
||
449 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
450 /* must save value on stack if not already done */
452 /* NOTE: must reload 'r' because r might be equal to r2 */
453 r
= p
->r
& VT_VALMASK
;
454 /* store register in the stack */
456 if ((p
->r
& VT_LVAL
) ||
457 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
458 #ifdef TCC_TARGET_X86_64
459 type
= &char_pointer_type
;
463 size
= type_size(type
, &align
);
464 loc
= (loc
- size
) & -align
;
466 sv
.r
= VT_LOCAL
| VT_LVAL
;
469 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
470 /* x86 specific: need to pop fp register ST0 if saved */
472 o(0xd8dd); /* fstp %st(0) */
475 #ifndef TCC_TARGET_X86_64
476 /* special long long case */
477 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
485 /* mark that stack entry as being saved on the stack */
486 if (p
->r
& VT_LVAL
) {
487 /* also clear the bounded flag because the
488 relocation address of the function was stored in
490 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
492 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
500 #ifdef TCC_TARGET_ARM
501 /* find a register of class 'rc2' with at most one reference on stack.
502 * If none, call get_reg(rc) */
503 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
508 for(r
=0;r
<NB_REGS
;r
++) {
509 if (reg_classes
[r
] & rc2
) {
512 for(p
= vstack
; p
<= vtop
; p
++) {
513 if ((p
->r
& VT_VALMASK
) == r
||
514 (p
->r2
& VT_VALMASK
) == r
)
525 /* find a free register of class 'rc'. If none, save one register */
526 ST_FUNC
int get_reg(int rc
)
531 /* find a free register */
532 for(r
=0;r
<NB_REGS
;r
++) {
533 if (reg_classes
[r
] & rc
) {
534 for(p
=vstack
;p
<=vtop
;p
++) {
535 if ((p
->r
& VT_VALMASK
) == r
||
536 (p
->r2
& VT_VALMASK
) == r
)
544 /* no register left : free the first one on the stack (VERY
545 IMPORTANT to start from the bottom to ensure that we don't
546 spill registers used in gen_opi()) */
547 for(p
=vstack
;p
<=vtop
;p
++) {
548 r
= p
->r
& VT_VALMASK
;
549 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
551 /* also look at second register (if long long) */
552 r
= p
->r2
& VT_VALMASK
;
553 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
559 /* Should never comes here */
563 /* save registers up to (vtop - n) stack entry */
564 ST_FUNC
void save_regs(int n
)
569 for(p
= vstack
;p
<= p1
; p
++) {
570 r
= p
->r
& VT_VALMASK
;
577 /* move register 's' to 'r', and flush previous value of r to memory
579 static void move_reg(int r
, int s
)
592 /* get address of vtop (vtop MUST BE an lvalue) */
593 static void gaddrof(void)
596 /* tricky: if saved lvalue, then we can go back to lvalue */
597 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
598 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
601 #ifdef CONFIG_TCC_BCHECK
602 /* generate lvalue bound code */
603 static void gbound(void)
608 vtop
->r
&= ~VT_MUSTBOUND
;
609 /* if lvalue, then use checking code before dereferencing */
610 if (vtop
->r
& VT_LVAL
) {
611 /* if not VT_BOUNDED value, then make one */
612 if (!(vtop
->r
& VT_BOUNDED
)) {
613 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
614 /* must save type because we must set it to int to get pointer */
616 vtop
->type
.t
= VT_INT
;
619 gen_bounded_ptr_add();
620 vtop
->r
|= lval_type
;
623 /* then check for dereferencing */
624 gen_bounded_ptr_deref();
629 /* store vtop a register belonging to class 'rc'. lvalues are
630 converted to values. Cannot be used if cannot be converted to
631 register value (such as structures). */
632 ST_FUNC
int gv(int rc
)
634 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
636 /* NOTE: get_reg can modify vstack[] */
637 if (vtop
->type
.t
& VT_BITFIELD
) {
640 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
641 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
642 /* remove bit field info to avoid loops */
643 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
644 /* cast to int to propagate signedness in following ops */
645 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
650 if((vtop
->type
.t
& VT_UNSIGNED
) ||
651 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
652 type
.t
|= VT_UNSIGNED
;
654 /* generate shifts */
655 vpushi(bits
- (bit_pos
+ bit_size
));
657 vpushi(bits
- bit_size
);
658 /* NOTE: transformed to SHR if unsigned */
662 if (is_float(vtop
->type
.t
) &&
663 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
666 unsigned long offset
;
667 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
671 /* XXX: unify with initializers handling ? */
672 /* CPUs usually cannot use float constants, so we store them
673 generically in data segment */
674 size
= type_size(&vtop
->type
, &align
);
675 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
676 data_section
->data_offset
= offset
;
677 /* XXX: not portable yet */
678 #if defined(__i386__) || defined(__x86_64__)
679 /* Zero pad x87 tenbyte long doubles */
680 if (size
== LDOUBLE_SIZE
)
681 vtop
->c
.tab
[2] &= 0xffff;
683 ptr
= section_ptr_add(data_section
, size
);
685 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
689 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
693 ptr
[i
] = vtop
->c
.tab
[i
];
694 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
695 vtop
->r
|= VT_LVAL
| VT_SYM
;
699 #ifdef CONFIG_TCC_BCHECK
700 if (vtop
->r
& VT_MUSTBOUND
)
704 r
= vtop
->r
& VT_VALMASK
;
708 /* need to reload if:
710 - lvalue (need to dereference pointer)
711 - already a register, but not in the right class */
713 || (vtop
->r
& VT_LVAL
)
714 || !(reg_classes
[r
] & rc
)
715 #ifndef TCC_TARGET_X86_64
716 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
721 #ifndef TCC_TARGET_X86_64
722 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
724 unsigned long long ll
;
725 /* two register type load : expand to two words
727 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
730 vtop
->c
.ui
= ll
; /* first word */
732 vtop
->r
= r
; /* save register value */
733 vpushi(ll
>> 32); /* second word */
734 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
735 (vtop
->r
& VT_LVAL
)) {
736 /* We do not want to modifier the long long
737 pointer here, so the safest (and less
738 efficient) is to save all the other registers
739 in the stack. XXX: totally inefficient. */
741 /* load from memory */
744 vtop
[-1].r
= r
; /* save register value */
745 /* increment pointer to get second word */
746 vtop
->type
.t
= VT_INT
;
755 vtop
[-1].r
= r
; /* save register value */
756 vtop
->r
= vtop
[-1].r2
;
758 /* allocate second register */
762 /* write second register */
766 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
768 /* lvalue of scalar type : need to use lvalue type
769 because of possible cast */
772 /* compute memory access type */
773 if (vtop
->r
& VT_LVAL_BYTE
)
775 else if (vtop
->r
& VT_LVAL_SHORT
)
777 if (vtop
->r
& VT_LVAL_UNSIGNED
)
781 /* restore wanted type */
784 /* one register type load */
789 #ifdef TCC_TARGET_C67
790 /* uses register pairs for doubles */
791 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
798 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
799 ST_FUNC
void gv2(int rc1
, int rc2
)
803 /* generate more generic register first. But VT_JMP or VT_CMP
804 values must be generated first in all cases to avoid possible
806 v
= vtop
[0].r
& VT_VALMASK
;
807 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
812 /* test if reload is needed for first register */
813 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
823 /* test if reload is needed for first register */
824 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
830 /* wrapper around RC_FRET to return a register by type */
831 static int rc_fret(int t
)
833 #ifdef TCC_TARGET_X86_64
834 if (t
== VT_LDOUBLE
) {
841 /* wrapper around REG_FRET to return a register by type */
842 static int reg_fret(int t
)
844 #ifdef TCC_TARGET_X86_64
845 if (t
== VT_LDOUBLE
) {
852 /* expand long long on stack in two int registers */
853 static void lexpand(void)
857 u
= vtop
->type
.t
& VT_UNSIGNED
;
860 vtop
[0].r
= vtop
[-1].r2
;
861 vtop
[0].r2
= VT_CONST
;
862 vtop
[-1].r2
= VT_CONST
;
863 vtop
[0].type
.t
= VT_INT
| u
;
864 vtop
[-1].type
.t
= VT_INT
| u
;
867 #ifdef TCC_TARGET_ARM
868 /* expand long long on stack */
869 ST_FUNC
void lexpand_nr(void)
873 u
= vtop
->type
.t
& VT_UNSIGNED
;
876 vtop
->type
.t
= VT_INT
| u
;
877 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
879 vtop
[-1].c
.ui
= vtop
->c
.ull
;
880 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
882 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
884 vtop
->r
= vtop
[-1].r
;
885 } else if (v
> VT_CONST
) {
889 vtop
->r
= vtop
[-1].r2
;
890 vtop
[-1].r2
= VT_CONST
;
891 vtop
[-1].type
.t
= VT_INT
| u
;
895 /* build a long long from two ints */
896 static void lbuild(int t
)
899 vtop
[-1].r2
= vtop
[0].r
;
904 /* rotate n first stack elements to the bottom
905 I1 ... In -> I2 ... In I1 [top is right]
907 static void vrotb(int n
)
918 /* rotate n first stack elements to the top
919 I1 ... In -> In I1 ... I(n-1) [top is right]
921 ST_FUNC
void vrott(int n
)
927 for(i
= 0;i
< n
- 1; i
++)
928 vtop
[-i
] = vtop
[-i
- 1];
932 #ifdef TCC_TARGET_ARM
933 /* like vrott but in other direction
934 In ... I1 -> I(n-1) ... I1 In [top is right]
936 ST_FUNC
void vnrott(int n
)
942 for(i
= n
- 1; i
> 0; i
--)
943 vtop
[-i
] = vtop
[-i
+ 1];
948 /* pop stack value */
949 ST_FUNC
void vpop(void)
952 v
= vtop
->r
& VT_VALMASK
;
953 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
954 /* for x86, we need to pop the FP stack */
955 if (v
== TREG_ST0
&& !nocode_wanted
) {
956 o(0xd8dd); /* fstp %st(0) */
959 if (v
== VT_JMP
|| v
== VT_JMPI
) {
960 /* need to put correct jump if && or || without test */
966 /* convert stack entry to register and duplicate its value in another
968 static void gv_dup(void)
974 if ((t
& VT_BTYPE
) == VT_LLONG
) {
981 /* stack: H L L1 H1 */
989 /* duplicate value */
994 #ifdef TCC_TARGET_X86_64
995 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1005 load(r1
, &sv
); /* move r to r1 */
1007 /* duplicates value */
1013 #ifndef TCC_TARGET_X86_64
1014 /* generate CPU independent (unsigned) long long operations */
1015 static void gen_opl(int op
)
1017 int t
, a
, b
, op1
, c
, i
;
1019 unsigned short reg_iret
= REG_IRET
;
1020 unsigned short reg_lret
= REG_LRET
;
1026 func
= TOK___divdi3
;
1029 func
= TOK___udivdi3
;
1032 func
= TOK___moddi3
;
1035 func
= TOK___umoddi3
;
1042 /* call generic long long function */
1043 vpush_global_sym(&func_old_type
, func
);
1048 vtop
->r2
= reg_lret
;
1061 /* stack: L1 H1 L2 H2 */
1066 vtop
[-2] = vtop
[-3];
1069 /* stack: H1 H2 L1 L2 */
1075 /* stack: H1 H2 L1 L2 ML MH */
1078 /* stack: ML MH H1 H2 L1 L2 */
1082 /* stack: ML MH H1 L2 H2 L1 */
1087 /* stack: ML MH M1 M2 */
1090 } else if (op
== '+' || op
== '-') {
1091 /* XXX: add non carry method too (for MIPS or alpha) */
1097 /* stack: H1 H2 (L1 op L2) */
1100 gen_op(op1
+ 1); /* TOK_xxxC2 */
1103 /* stack: H1 H2 (L1 op L2) */
1106 /* stack: (L1 op L2) H1 H2 */
1108 /* stack: (L1 op L2) (H1 op H2) */
1116 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1117 t
= vtop
[-1].type
.t
;
1121 /* stack: L H shift */
1123 /* constant: simpler */
1124 /* NOTE: all comments are for SHL. the other cases are
1125 done by swaping words */
1136 if (op
!= TOK_SAR
) {
1169 /* XXX: should provide a faster fallback on x86 ? */
1172 func
= TOK___ashrdi3
;
1175 func
= TOK___lshrdi3
;
1178 func
= TOK___ashldi3
;
1184 /* compare operations */
1190 /* stack: L1 H1 L2 H2 */
1192 vtop
[-1] = vtop
[-2];
1194 /* stack: L1 L2 H1 H2 */
1197 /* when values are equal, we need to compare low words. since
1198 the jump is inverted, we invert the test too. */
1201 else if (op1
== TOK_GT
)
1203 else if (op1
== TOK_ULT
)
1205 else if (op1
== TOK_UGT
)
1210 if (op1
!= TOK_NE
) {
1214 /* generate non equal test */
1215 /* XXX: NOT PORTABLE yet */
1219 #if defined(TCC_TARGET_I386)
1220 b
= psym(0x850f, 0);
1221 #elif defined(TCC_TARGET_ARM)
1223 o(0x1A000000 | encbranch(ind
, 0, 1));
1224 #elif defined(TCC_TARGET_C67)
1225 error("not implemented");
1227 #error not supported
1231 /* compare low. Always unsigned */
1235 else if (op1
== TOK_LE
)
1237 else if (op1
== TOK_GT
)
1239 else if (op1
== TOK_GE
)
1250 /* handle integer constant optimizations and various machine
1252 static void gen_opic(int op
)
1254 int c1
, c2
, t1
, t2
, n
;
1257 typedef unsigned long long U
;
1261 t1
= v1
->type
.t
& VT_BTYPE
;
1262 t2
= v2
->type
.t
& VT_BTYPE
;
1266 else if (v1
->type
.t
& VT_UNSIGNED
)
1273 else if (v2
->type
.t
& VT_UNSIGNED
)
1278 /* currently, we cannot do computations with forward symbols */
1279 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1280 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1283 case '+': l1
+= l2
; break;
1284 case '-': l1
-= l2
; break;
1285 case '&': l1
&= l2
; break;
1286 case '^': l1
^= l2
; break;
1287 case '|': l1
|= l2
; break;
1288 case '*': l1
*= l2
; break;
1295 /* if division by zero, generate explicit division */
1298 error("division by zero in constant");
1302 default: l1
/= l2
; break;
1303 case '%': l1
%= l2
; break;
1304 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1305 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1308 case TOK_SHL
: l1
<<= l2
; break;
1309 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1310 case TOK_SAR
: l1
>>= l2
; break;
1312 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1313 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1314 case TOK_EQ
: l1
= l1
== l2
; break;
1315 case TOK_NE
: l1
= l1
!= l2
; break;
1316 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1317 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1318 case TOK_LT
: l1
= l1
< l2
; break;
1319 case TOK_GE
: l1
= l1
>= l2
; break;
1320 case TOK_LE
: l1
= l1
<= l2
; break;
1321 case TOK_GT
: l1
= l1
> l2
; break;
1323 case TOK_LAND
: l1
= l1
&& l2
; break;
1324 case TOK_LOR
: l1
= l1
|| l2
; break;
1331 /* if commutative ops, put c2 as constant */
1332 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1333 op
== '|' || op
== '*')) {
1335 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1336 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1338 /* Filter out NOP operations like x*1, x-0, x&-1... */
1339 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1342 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1343 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1349 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1350 /* try to use shifts instead of muls or divs */
1351 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1360 else if (op
== TOK_PDIV
)
1366 } else if (c2
&& (op
== '+' || op
== '-') &&
1367 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1368 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1369 /* symbol + constant case */
1376 if (!nocode_wanted
) {
1377 /* call low level op generator */
1378 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1389 /* generate a floating point operation with constant propagation */
1390 static void gen_opif(int op
)
1398 /* currently, we cannot do computations with forward symbols */
1399 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1400 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1402 if (v1
->type
.t
== VT_FLOAT
) {
1405 } else if (v1
->type
.t
== VT_DOUBLE
) {
1413 /* NOTE: we only do constant propagation if finite number (not
1414 NaN or infinity) (ANSI spec) */
1415 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1419 case '+': f1
+= f2
; break;
1420 case '-': f1
-= f2
; break;
1421 case '*': f1
*= f2
; break;
1425 error("division by zero in constant");
1430 /* XXX: also handles tests ? */
1434 /* XXX: overflow test ? */
1435 if (v1
->type
.t
== VT_FLOAT
) {
1437 } else if (v1
->type
.t
== VT_DOUBLE
) {
1445 if (!nocode_wanted
) {
1453 static int pointed_size(CType
*type
)
1456 return type_size(pointed_type(type
), &align
);
1459 static void vla_runtime_pointed_size(CType
*type
)
1462 vla_runtime_type_size(pointed_type(type
), &align
);
1465 static inline int is_null_pointer(SValue
*p
)
1467 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1469 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1470 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1473 static inline int is_integer_btype(int bt
)
1475 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1476 bt
== VT_INT
|| bt
== VT_LLONG
);
1479 /* check types for comparison or substraction of pointers */
1480 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1482 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1485 /* null pointers are accepted for all comparisons as gcc */
1486 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1490 bt1
= type1
->t
& VT_BTYPE
;
1491 bt2
= type2
->t
& VT_BTYPE
;
1492 /* accept comparison between pointer and integer with a warning */
1493 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1494 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1495 warning("comparison between pointer and integer");
1499 /* both must be pointers or implicit function pointers */
1500 if (bt1
== VT_PTR
) {
1501 type1
= pointed_type(type1
);
1502 } else if (bt1
!= VT_FUNC
)
1503 goto invalid_operands
;
1505 if (bt2
== VT_PTR
) {
1506 type2
= pointed_type(type2
);
1507 } else if (bt2
!= VT_FUNC
) {
1509 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1511 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1512 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1516 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1517 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1518 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1519 /* gcc-like error if '-' is used */
1521 goto invalid_operands
;
1523 warning("comparison of distinct pointer types lacks a cast");
1527 /* generic gen_op: handles types problems */
1528 ST_FUNC
void gen_op(int op
)
1530 int u
, t1
, t2
, bt1
, bt2
, t
;
1533 t1
= vtop
[-1].type
.t
;
1534 t2
= vtop
[0].type
.t
;
1535 bt1
= t1
& VT_BTYPE
;
1536 bt2
= t2
& VT_BTYPE
;
1538 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1539 /* at least one operand is a pointer */
1540 /* relationnal op: must be both pointers */
1541 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1542 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1543 /* pointers are handled are unsigned */
1544 #ifdef TCC_TARGET_X86_64
1545 t
= VT_LLONG
| VT_UNSIGNED
;
1547 t
= VT_INT
| VT_UNSIGNED
;
1551 /* if both pointers, then it must be the '-' op */
1552 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1557 error("cannot use pointers here");
1558 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1559 /* XXX: check that types are compatible */
1560 if (vtop
[-1].type
.t
& VT_VLA
) {
1561 vla_runtime_pointed_size(&vtop
[-1].type
);
1565 u
= pointed_size(&vtop
[-1].type
);
1568 /* set to integer type */
1569 #ifdef TCC_TARGET_X86_64
1570 vtop
->type
.t
= VT_LLONG
;
1572 vtop
->type
.t
= VT_INT
;
1580 /* exactly one pointer : must be '+' or '-'. */
1581 if (op
!= '-' && op
!= '+')
1582 error("cannot use pointers here");
1583 /* Put pointer as first operand */
1584 if (bt2
== VT_PTR
) {
1588 type1
= vtop
[-1].type
;
1589 type1
.t
&= ~(VT_ARRAY
|VT_VLA
);
1590 if (vtop
[-1].type
.t
& VT_VLA
)
1591 vla_runtime_pointed_size(&vtop
[-1].type
);
1593 u
= pointed_size(&vtop
[-1].type
);
1595 error("unknown array element size");
1596 #ifdef TCC_TARGET_X86_64
1599 /* XXX: cast to int ? (long long case) */
1604 #ifdef CONFIG_TCC_BCHECK
1605 /* if evaluating constant expression, no code should be
1606 generated, so no bound check */
1607 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1608 /* if bounded pointers, we generate a special code to
1615 gen_bounded_ptr_add();
1621 /* put again type if gen_opic() swaped operands */
1624 } else if (is_float(bt1
) || is_float(bt2
)) {
1625 /* compute bigger type and do implicit casts */
1626 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1628 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1633 /* floats can only be used for a few operations */
1634 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1635 (op
< TOK_ULT
|| op
> TOK_GT
))
1636 error("invalid operands for binary operation");
1638 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1639 /* cast to biggest op */
1641 /* convert to unsigned if it does not fit in a long long */
1642 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1643 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1647 /* integer operations */
1649 /* convert to unsigned if it does not fit in an integer */
1650 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1651 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1654 /* XXX: currently, some unsigned operations are explicit, so
1655 we modify them here */
1656 if (t
& VT_UNSIGNED
) {
1663 else if (op
== TOK_LT
)
1665 else if (op
== TOK_GT
)
1667 else if (op
== TOK_LE
)
1669 else if (op
== TOK_GE
)
1676 /* special case for shifts and long long: we keep the shift as
1678 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1685 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1686 /* relationnal op: the result is an int */
1687 vtop
->type
.t
= VT_INT
;
1694 #ifndef TCC_TARGET_ARM
1695 /* generic itof for unsigned long long case */
1696 static void gen_cvt_itof1(int t
)
1698 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1699 (VT_LLONG
| VT_UNSIGNED
)) {
1702 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1703 #if LDOUBLE_SIZE != 8
1704 else if (t
== VT_LDOUBLE
)
1705 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1708 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1712 vtop
->r
= reg_fret(t
);
1719 /* generic ftoi for unsigned long long case */
1720 static void gen_cvt_ftoi1(int t
)
1724 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1725 /* not handled natively */
1726 st
= vtop
->type
.t
& VT_BTYPE
;
1728 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1729 #if LDOUBLE_SIZE != 8
1730 else if (st
== VT_LDOUBLE
)
1731 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1734 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1739 vtop
->r2
= REG_LRET
;
1745 /* force char or short cast */
1746 static void force_charshort_cast(int t
)
1750 /* XXX: add optimization if lvalue : just change type and offset */
1755 if (t
& VT_UNSIGNED
) {
1756 vpushi((1 << bits
) - 1);
1762 /* result must be signed or the SAR is converted to an SHL
1763 This was not the case when "t" was a signed short
1764 and the last value on the stack was an unsigned int */
1765 vtop
->type
.t
&= ~VT_UNSIGNED
;
1771 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1772 static void gen_cast(CType
*type
)
1774 int sbt
, dbt
, sf
, df
, c
, p
;
1776 /* special delayed cast for char/short */
1777 /* XXX: in some cases (multiple cascaded casts), it may still
1779 if (vtop
->r
& VT_MUSTCAST
) {
1780 vtop
->r
&= ~VT_MUSTCAST
;
1781 force_charshort_cast(vtop
->type
.t
);
1784 /* bitfields first get cast to ints */
1785 if (vtop
->type
.t
& VT_BITFIELD
) {
1789 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1790 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1795 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1796 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1798 /* constant case: we can do it now */
1799 /* XXX: in ISOC, cannot do it if error in convert */
1800 if (sbt
== VT_FLOAT
)
1801 vtop
->c
.ld
= vtop
->c
.f
;
1802 else if (sbt
== VT_DOUBLE
)
1803 vtop
->c
.ld
= vtop
->c
.d
;
1806 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1807 if (sbt
& VT_UNSIGNED
)
1808 vtop
->c
.ld
= vtop
->c
.ull
;
1810 vtop
->c
.ld
= vtop
->c
.ll
;
1812 if (sbt
& VT_UNSIGNED
)
1813 vtop
->c
.ld
= vtop
->c
.ui
;
1815 vtop
->c
.ld
= vtop
->c
.i
;
1818 if (dbt
== VT_FLOAT
)
1819 vtop
->c
.f
= (float)vtop
->c
.ld
;
1820 else if (dbt
== VT_DOUBLE
)
1821 vtop
->c
.d
= (double)vtop
->c
.ld
;
1822 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1823 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1824 } else if (sf
&& dbt
== VT_BOOL
) {
1825 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1828 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1829 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1830 vtop
->c
.ll
= vtop
->c
.ull
;
1831 else if (sbt
& VT_UNSIGNED
)
1832 vtop
->c
.ll
= vtop
->c
.ui
;
1833 #ifdef TCC_TARGET_X86_64
1834 else if (sbt
== VT_PTR
)
1837 else if (sbt
!= VT_LLONG
)
1838 vtop
->c
.ll
= vtop
->c
.i
;
1840 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1841 vtop
->c
.ull
= vtop
->c
.ll
;
1842 else if (dbt
== VT_BOOL
)
1843 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1844 else if (dbt
!= VT_LLONG
) {
1846 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1848 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1851 if(dbt
& VT_UNSIGNED
)
1852 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1854 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1857 } else if (p
&& dbt
== VT_BOOL
) {
1860 } else if (!nocode_wanted
) {
1861 /* non constant case: generate code */
1863 /* convert from fp to fp */
1866 /* convert int to fp */
1869 /* convert fp to int */
1870 if (dbt
== VT_BOOL
) {
1874 /* we handle char/short/etc... with generic code */
1875 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1876 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1880 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1881 /* additional cast for char/short... */
1886 #ifndef TCC_TARGET_X86_64
1887 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1888 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1889 /* scalar to long long */
1890 /* machine independent conversion */
1892 /* generate high word */
1893 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1897 if (sbt
== VT_PTR
) {
1898 /* cast from pointer to int before we apply
1899 shift operation, which pointers don't support*/
1900 gen_cast(&int_type
);
1906 /* patch second register */
1907 vtop
[-1].r2
= vtop
->r
;
1911 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1912 (dbt
& VT_BTYPE
) == VT_PTR
||
1913 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1914 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1915 (sbt
& VT_BTYPE
) != VT_PTR
&&
1916 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1917 /* need to convert from 32bit to 64bit */
1919 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1920 /* x86_64 specific: movslq */
1922 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1926 } else if (dbt
== VT_BOOL
) {
1927 /* scalar to bool */
1930 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1931 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1932 if (sbt
== VT_PTR
) {
1933 vtop
->type
.t
= VT_INT
;
1934 warning("nonportable conversion from pointer to char/short");
1936 force_charshort_cast(dbt
);
1937 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1939 if (sbt
== VT_LLONG
) {
1940 /* from long long: just take low order word */
1944 /* if lvalue and single word type, nothing to do because
1945 the lvalue already contains the real type size (see
1946 VT_LVAL_xxx constants) */
1949 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1950 /* if we are casting between pointer types,
1951 we must update the VT_LVAL_xxx size */
1952 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1953 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1958 /* return type size as known at compile time. Put alignment at 'a' */
1959 ST_FUNC
int type_size(CType
*type
, int *a
)
1964 bt
= type
->t
& VT_BTYPE
;
1965 if (bt
== VT_STRUCT
) {
1970 } else if (bt
== VT_PTR
) {
1971 if (type
->t
& VT_VLA
) {
1974 } else if (type
->t
& VT_ARRAY
) {
1978 ts
= type_size(&s
->type
, a
);
1980 if (ts
< 0 && s
->c
< 0)
1988 } else if (bt
== VT_LDOUBLE
) {
1990 return LDOUBLE_SIZE
;
1991 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1992 #ifdef TCC_TARGET_I386
1993 #ifdef TCC_TARGET_PE
1998 #elif defined(TCC_TARGET_ARM)
2008 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2011 } else if (bt
== VT_SHORT
) {
2015 /* char, void, function, _Bool */
2021 /* push type size as known at runtime time on top of value stack. Put
2023 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2025 if (type
->t
& VT_VLA
) {
2029 vla_runtime_type_size(&s
->type
, a
);
2031 if ((vtop
->r
& (VT_SYM
|VT_LVAL
|VT_VALMASK
)) != VT_CONST
) {
2040 size
= type_size(type
, a
);
2045 /* return the pointed type of t */
2046 static inline CType
*pointed_type(CType
*type
)
2048 return &type
->ref
->type
;
2051 /* modify type so that its it is a pointer to type. */
2052 ST_FUNC
void mk_pointer(CType
*type
)
2055 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2056 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2060 /* compare function types. OLD functions match any new functions */
2061 static int is_compatible_func(CType
*type1
, CType
*type2
)
2067 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2069 /* check func_call */
2070 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2072 /* XXX: not complete */
2073 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2077 while (s1
!= NULL
) {
2080 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2090 /* return true if type1 and type2 are the same. If unqualified is
2091 true, qualifiers on the types are ignored.
2093 - enums are not checked as gcc __builtin_types_compatible_p ()
2095 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2099 t1
= type1
->t
& VT_TYPE
;
2100 t2
= type2
->t
& VT_TYPE
;
2102 /* strip qualifiers before comparing */
2103 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2104 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2106 /* XXX: bitfields ? */
2109 /* test more complicated cases */
2110 bt1
= t1
& VT_BTYPE
;
2111 if (bt1
== VT_PTR
) {
2112 type1
= pointed_type(type1
);
2113 type2
= pointed_type(type2
);
2114 return is_compatible_types(type1
, type2
);
2115 } else if (bt1
== VT_STRUCT
) {
2116 return (type1
->ref
== type2
->ref
);
2117 } else if (bt1
== VT_FUNC
) {
2118 return is_compatible_func(type1
, type2
);
2124 /* return true if type1 and type2 are exactly the same (including
2127 static int is_compatible_types(CType
*type1
, CType
*type2
)
2129 return compare_types(type1
,type2
,0);
2132 /* return true if type1 and type2 are the same (ignoring qualifiers).
2134 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2136 return compare_types(type1
,type2
,1);
2139 /* print a type. If 'varstr' is not NULL, then the variable is also
2140 printed in the type */
2142 /* XXX: add array and function pointers */
2143 static void type_to_str(char *buf
, int buf_size
,
2144 CType
*type
, const char *varstr
)
2151 t
= type
->t
& VT_TYPE
;
2154 if (t
& VT_CONSTANT
)
2155 pstrcat(buf
, buf_size
, "const ");
2156 if (t
& VT_VOLATILE
)
2157 pstrcat(buf
, buf_size
, "volatile ");
2158 if (t
& VT_UNSIGNED
)
2159 pstrcat(buf
, buf_size
, "unsigned ");
2189 tstr
= "long double";
2191 pstrcat(buf
, buf_size
, tstr
);
2195 if (bt
== VT_STRUCT
)
2199 pstrcat(buf
, buf_size
, tstr
);
2200 v
= type
->ref
->v
& ~SYM_STRUCT
;
2201 if (v
>= SYM_FIRST_ANOM
)
2202 pstrcat(buf
, buf_size
, "<anonymous>");
2204 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2208 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2209 pstrcat(buf
, buf_size
, "(");
2211 while (sa
!= NULL
) {
2212 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2213 pstrcat(buf
, buf_size
, buf1
);
2216 pstrcat(buf
, buf_size
, ", ");
2218 pstrcat(buf
, buf_size
, ")");
2222 pstrcpy(buf1
, sizeof(buf1
), "*");
2224 pstrcat(buf1
, sizeof(buf1
), varstr
);
2225 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2229 pstrcat(buf
, buf_size
, " ");
2230 pstrcat(buf
, buf_size
, varstr
);
2235 /* verify type compatibility to store vtop in 'dt' type, and generate
2237 static void gen_assign_cast(CType
*dt
)
2239 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2240 char buf1
[256], buf2
[256];
2243 st
= &vtop
->type
; /* source type */
2244 dbt
= dt
->t
& VT_BTYPE
;
2245 sbt
= st
->t
& VT_BTYPE
;
2246 if (dt
->t
& VT_CONSTANT
)
2247 warning("assignment of read-only location");
2250 /* special cases for pointers */
2251 /* '0' can also be a pointer */
2252 if (is_null_pointer(vtop
))
2254 /* accept implicit pointer to integer cast with warning */
2255 if (is_integer_btype(sbt
)) {
2256 warning("assignment makes pointer from integer without a cast");
2259 type1
= pointed_type(dt
);
2260 /* a function is implicitely a function pointer */
2261 if (sbt
== VT_FUNC
) {
2262 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2263 !is_compatible_types(pointed_type(dt
), st
))
2264 warning("assignment from incompatible pointer type");
2269 type2
= pointed_type(st
);
2270 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2271 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2272 /* void * can match anything */
2274 /* exact type match, except for unsigned */
2277 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2278 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2279 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2280 warning("assignment from incompatible pointer type");
2282 /* check const and volatile */
2283 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2284 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2285 warning("assignment discards qualifiers from pointer target type");
2291 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2292 warning("assignment makes integer from pointer without a cast");
2294 /* XXX: more tests */
2299 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2300 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2301 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2303 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2304 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2305 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2313 /* store vtop in lvalue pushed on stack */
2314 ST_FUNC
void vstore(void)
2316 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2318 ft
= vtop
[-1].type
.t
;
2319 sbt
= vtop
->type
.t
& VT_BTYPE
;
2320 dbt
= ft
& VT_BTYPE
;
2321 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2322 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2323 /* optimize char/short casts */
2324 delayed_cast
= VT_MUSTCAST
;
2325 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2326 /* XXX: factorize */
2327 if (ft
& VT_CONSTANT
)
2328 warning("assignment of read-only location");
2331 if (!(ft
& VT_BITFIELD
))
2332 gen_assign_cast(&vtop
[-1].type
);
2335 if (sbt
== VT_STRUCT
) {
2336 /* if structure, only generate pointer */
2337 /* structure assignment : generate memcpy */
2338 /* XXX: optimize if small size */
2339 if (!nocode_wanted
) {
2340 size
= type_size(&vtop
->type
, &align
);
2344 vtop
->type
.t
= VT_PTR
;
2347 /* address of memcpy() */
2350 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2351 else if(!(align
& 3))
2352 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2355 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2360 vtop
->type
.t
= VT_PTR
;
2369 /* leave source on stack */
2370 } else if (ft
& VT_BITFIELD
) {
2371 /* bitfield store handling */
2372 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2373 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2374 /* remove bit field info to avoid loops */
2375 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2377 /* duplicate source into other register */
2382 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2383 gen_cast(&vtop
[-1].type
);
2384 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2387 /* duplicate destination */
2389 vtop
[-1] = vtop
[-2];
2391 /* mask and shift source */
2392 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2393 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2394 vpushll((1ULL << bit_size
) - 1ULL);
2396 vpushi((1 << bit_size
) - 1);
2402 /* load destination, mask and or with source */
2404 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2405 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2407 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2414 /* pop off shifted source from "duplicate source..." above */
2418 #ifdef CONFIG_TCC_BCHECK
2419 /* bound check case */
2420 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2426 if (!nocode_wanted
) {
2430 #ifdef TCC_TARGET_X86_64
2431 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2436 r
= gv(rc
); /* generate value */
2437 /* if lvalue was saved on stack, must read it */
2438 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2440 t
= get_reg(RC_INT
);
2441 #ifdef TCC_TARGET_X86_64
2446 sv
.r
= VT_LOCAL
| VT_LVAL
;
2447 sv
.c
.ul
= vtop
[-1].c
.ul
;
2449 vtop
[-1].r
= t
| VT_LVAL
;
2452 #ifndef TCC_TARGET_X86_64
2453 /* two word case handling : store second register at word + 4 */
2454 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2456 /* convert to int to increment easily */
2457 vtop
->type
.t
= VT_INT
;
2463 /* XXX: it works because r2 is spilled last ! */
2464 store(vtop
->r2
, vtop
- 1);
2469 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2470 vtop
->r
|= delayed_cast
;
2474 /* post defines POST/PRE add. c is the token ++ or -- */
2475 ST_FUNC
void inc(int post
, int c
)
2478 vdup(); /* save lvalue */
2480 gv_dup(); /* duplicate value */
2485 vpushi(c
- TOK_MID
);
2487 vstore(); /* store value */
2489 vpop(); /* if post op, return saved value */
2492 /* Parse GNUC __attribute__ extension. Currently, the following
2493 extensions are recognized:
2494 - aligned(n) : set data/function alignment.
2495 - packed : force data alignment to 1
2496 - section(x) : generate data/code in this section.
2497 - unused : currently ignored, but may be used someday.
2498 - regparm(n) : pass function parameters in registers (i386 only)
2500 static void parse_attribute(AttributeDef
*ad
)
2504 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2508 while (tok
!= ')') {
2509 if (tok
< TOK_IDENT
)
2510 expect("attribute name");
2518 expect("section name");
2519 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2528 if (n
<= 0 || (n
& (n
- 1)) != 0)
2529 error("alignment must be a positive power of two");
2546 /* currently, no need to handle it because tcc does not
2547 track unused objects */
2551 /* currently, no need to handle it because tcc does not
2552 track unused objects */
2557 ad
->func_call
= FUNC_CDECL
;
2562 ad
->func_call
= FUNC_STDCALL
;
2564 #ifdef TCC_TARGET_I386
2574 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2580 ad
->func_call
= FUNC_FASTCALLW
;
2587 ad
->mode
= VT_LLONG
+ 1;
2590 ad
->mode
= VT_SHORT
+ 1;
2593 ad
->mode
= VT_INT
+ 1;
2596 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2603 ad
->func_export
= 1;
2606 ad
->func_import
= 1;
2609 if (tcc_state
->warn_unsupported
)
2610 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2611 /* skip parameters */
2613 int parenthesis
= 0;
2617 else if (tok
== ')')
2620 } while (parenthesis
&& tok
!= -1);
2633 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2634 static void struct_decl(CType
*type
, int u
)
2636 int a
, v
, size
, align
, maxalign
, c
, offset
;
2637 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2638 Sym
*s
, *ss
, *ass
, **ps
;
2642 a
= tok
; /* save decl type */
2647 /* struct already defined ? return it */
2649 expect("struct/union/enum name");
2653 error("invalid type");
2660 /* we put an undefined size for struct/union */
2661 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2662 s
->r
= 0; /* default alignment is zero as gcc */
2663 /* put struct/union/enum name in type */
2671 error("struct/union/enum already defined");
2672 /* cannot be empty */
2674 /* non empty enums are not allowed */
2675 if (a
== TOK_ENUM
) {
2679 expect("identifier");
2685 /* enum symbols have static storage */
2686 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2687 ss
->type
.t
|= VT_STATIC
;
2692 /* NOTE: we accept a trailing comma */
2703 while (tok
!= '}') {
2704 parse_btype(&btype
, &ad
);
2710 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2711 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2712 expect("identifier");
2713 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2714 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2715 error("invalid type for '%s'",
2716 get_tok_str(v
, NULL
));
2720 bit_size
= expr_const();
2721 /* XXX: handle v = 0 case for messages */
2723 error("negative width in bit-field '%s'",
2724 get_tok_str(v
, NULL
));
2725 if (v
&& bit_size
== 0)
2726 error("zero width for bit-field '%s'",
2727 get_tok_str(v
, NULL
));
2729 size
= type_size(&type1
, &align
);
2731 if (align
< ad
.aligned
)
2733 } else if (ad
.packed
) {
2735 } else if (*tcc_state
->pack_stack_ptr
) {
2736 if (align
> *tcc_state
->pack_stack_ptr
)
2737 align
= *tcc_state
->pack_stack_ptr
;
2740 if (bit_size
>= 0) {
2741 bt
= type1
.t
& VT_BTYPE
;
2748 error("bitfields must have scalar type");
2750 if (bit_size
> bsize
) {
2751 error("width of '%s' exceeds its type",
2752 get_tok_str(v
, NULL
));
2753 } else if (bit_size
== bsize
) {
2754 /* no need for bit fields */
2756 } else if (bit_size
== 0) {
2757 /* XXX: what to do if only padding in a
2759 /* zero size: means to pad */
2762 /* we do not have enough room ?
2763 did the type change?
2765 if ((bit_pos
+ bit_size
) > bsize
||
2766 bt
!= prevbt
|| a
== TOK_UNION
)
2769 /* XXX: handle LSB first */
2770 type1
.t
|= VT_BITFIELD
|
2771 (bit_pos
<< VT_STRUCT_SHIFT
) |
2772 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2773 bit_pos
+= bit_size
;
2779 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2780 /* add new memory data only if starting
2782 if (lbit_pos
== 0) {
2783 if (a
== TOK_STRUCT
) {
2784 c
= (c
+ align
- 1) & -align
;
2793 if (align
> maxalign
)
2797 printf("add field %s offset=%d",
2798 get_tok_str(v
, NULL
), offset
);
2799 if (type1
.t
& VT_BITFIELD
) {
2800 printf(" pos=%d size=%d",
2801 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2802 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2807 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2809 while ((ass
= ass
->next
) != NULL
) {
2810 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2815 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2819 if (tok
== ';' || tok
== TOK_EOF
)
2826 /* store size and alignment */
2827 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2833 /* return 0 if no type declaration. otherwise, return the basic type
2836 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2838 int t
, u
, type_found
, typespec_found
, typedef_found
;
2842 memset(ad
, 0, sizeof(AttributeDef
));
2850 /* currently, we really ignore extension */
2860 if ((t
& VT_BTYPE
) != 0)
2861 error("too many basic types");
2877 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2878 #ifndef TCC_TARGET_PE
2879 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2881 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2882 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2896 if ((t
& VT_BTYPE
) == VT_LONG
) {
2897 #ifdef TCC_TARGET_PE
2898 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2900 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2908 struct_decl(&type1
, VT_ENUM
);
2911 type
->ref
= type1
.ref
;
2915 struct_decl(&type1
, VT_STRUCT
);
2918 /* type modifiers */
2971 /* GNUC attribute */
2972 case TOK_ATTRIBUTE1
:
2973 case TOK_ATTRIBUTE2
:
2974 parse_attribute(ad
);
2980 t
= (t
& ~VT_BTYPE
) | u
;
2988 parse_expr_type(&type1
);
2991 if (typespec_found
|| typedef_found
)
2994 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2997 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2998 type
->ref
= s
->type
.ref
;
3000 /* get attributes from typedef */
3001 if (0 == ad
->aligned
)
3002 ad
->aligned
= FUNC_ALIGN(s
->r
);
3003 if (0 == ad
->func_call
)
3004 ad
->func_call
= FUNC_CALL(s
->r
);
3005 ad
->packed
|= FUNC_PACKED(s
->r
);
3014 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3015 error("signed and unsigned modifier");
3016 if (tcc_state
->char_is_unsigned
) {
3017 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3022 /* long is never used as type */
3023 if ((t
& VT_BTYPE
) == VT_LONG
)
3024 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3025 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3027 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3033 /* convert a function parameter type (array to pointer and function to
3034 function pointer) */
3035 static inline void convert_parameter_type(CType
*pt
)
3037 /* remove const and volatile qualifiers (XXX: const could be used
3038 to indicate a const function parameter */
3039 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3040 /* array must be transformed to pointer according to ANSI C */
3042 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3047 ST_FUNC
void parse_asm_str(CString
*astr
)
3050 /* read the string */
3052 expect("string constant");
3054 while (tok
== TOK_STR
) {
3055 /* XXX: add \0 handling too ? */
3056 cstr_cat(astr
, tokc
.cstr
->data
);
3059 cstr_ccat(astr
, '\0');
3062 /* Parse an asm label and return the label
3063 * Don't forget to free the CString in the caller! */
3064 static void asm_label_instr(CString
*astr
)
3067 parse_asm_str(astr
);
3070 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3074 static void post_type(CType
*type
, AttributeDef
*ad
)
3076 int n
, l
, t1
, arg_size
, align
;
3077 Sym
**plast
, *s
, *first
;
3082 TokenSym
*ts
= NULL
;
3084 /* function declaration */
3092 /* read param name and compute offset */
3093 if (l
!= FUNC_OLD
) {
3094 if (!parse_btype(&pt
, &ad1
)) {
3096 error("invalid type");
3103 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3105 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3106 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3107 error("parameter declared as void");
3108 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3113 expect("identifier");
3117 convert_parameter_type(&pt
);
3118 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3124 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3131 /* if no parameters, then old type prototype */
3135 t1
= type
->t
& VT_STORAGE
;
3136 /* NOTE: const is ignored in returned type as it has a special
3137 meaning in gcc / C++ */
3138 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3139 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
3142 asm_label_instr(&astr
);
3143 ts
= tok_alloc(astr
.data
, strlen(astr
.data
));
3146 post_type(type
, ad
);
3147 /* we push a anonymous symbol which will contain the function prototype */
3148 ad
->func_args
= arg_size
;
3149 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3153 type
->t
= t1
| VT_FUNC
;
3155 } else if (tok
== '[') {
3156 SValue
*last_vtop
= NULL
;
3158 /* array definition */
3160 if (tok
== TOK_RESTRICT1
)
3165 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3169 error("invalid array size");
3171 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3172 error("size of variable length array should be an integer");
3178 /* parse next post type */
3179 t1
= type
->t
& (VT_STORAGE
|VT_VLA
);
3180 type
->t
&= ~(VT_STORAGE
|VT_VLA
);
3181 post_type(type
, ad
);
3182 t1
|= type
->t
& VT_VLA
;
3184 /* we push an anonymous symbol which will contain the array
3186 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3188 s
->s
= last_vtop
; // That's ok, we don't need n with VLA
3193 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3198 /* Parse a type declaration (except basic type), and return the type
3199 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3200 expected. 'type' should contain the basic type. 'ad' is the
3201 attribute definition of the basic type. It can be modified by
3204 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3207 CType type1
, *type2
;
3210 while (tok
== '*') {
3218 qualifiers
|= VT_CONSTANT
;
3223 qualifiers
|= VT_VOLATILE
;
3231 type
->t
|= qualifiers
;
3234 /* XXX: clarify attribute handling */
3235 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3236 parse_attribute(ad
);
3238 /* recursive type */
3239 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3240 type1
.t
= 0; /* XXX: same as int */
3243 /* XXX: this is not correct to modify 'ad' at this point, but
3244 the syntax is not clear */
3245 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3246 parse_attribute(ad
);
3247 type_decl(&type1
, ad
, v
, td
);
3250 /* type identifier */
3251 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3255 if (!(td
& TYPE_ABSTRACT
))
3256 expect("identifier");
3260 post_type(type
, ad
);
3261 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3262 parse_attribute(ad
);
3269 /* append type at the end of type1 */
3282 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3283 ST_FUNC
int lvalue_type(int t
)
3288 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3290 else if (bt
== VT_SHORT
)
3294 if (t
& VT_UNSIGNED
)
3295 r
|= VT_LVAL_UNSIGNED
;
3299 /* indirection with full error checking and bound check */
3300 ST_FUNC
void indir(void)
3302 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3303 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3307 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3309 vtop
->type
= *pointed_type(&vtop
->type
);
3310 /* Arrays and functions are never lvalues */
3311 if (!(vtop
->type
.t
& VT_ARRAY
)
3312 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3313 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3314 /* if bound checking, the referenced pointer must be checked */
3315 #ifdef CONFIG_TCC_BCHECK
3316 if (tcc_state
->do_bounds_check
)
3317 vtop
->r
|= VT_MUSTBOUND
;
3322 /* pass a parameter to a function and do type checking and casting */
3323 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3328 func_type
= func
->c
;
3329 if (func_type
== FUNC_OLD
||
3330 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3331 /* default casting : only need to convert float to double */
3332 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3336 } else if (arg
== NULL
) {
3337 error("too many arguments to function");
3340 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3341 gen_assign_cast(&type
);
3345 /* parse an expression of the form '(type)' or '(expr)' and return its
3347 static void parse_expr_type(CType
*type
)
3353 if (parse_btype(type
, &ad
)) {
3354 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3361 static void parse_type(CType
*type
)
3366 if (!parse_btype(type
, &ad
)) {
3369 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3372 static void vpush_tokc(int t
)
3377 vsetc(&type
, VT_CONST
, &tokc
);
3380 ST_FUNC
void unary(void)
3382 int n
, t
, align
, size
, r
, sizeof_caller
;
3386 static int in_sizeof
= 0;
3388 sizeof_caller
= in_sizeof
;
3390 /* XXX: GCC 2.95.3 does not generate a table although it should be
3404 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3408 vpush_tokc(VT_LLONG
);
3412 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3416 vpush_tokc(VT_FLOAT
);
3420 vpush_tokc(VT_DOUBLE
);
3424 vpush_tokc(VT_LDOUBLE
);
3427 case TOK___FUNCTION__
:
3429 goto tok_identifier
;
3435 /* special function name identifier */
3436 len
= strlen(funcname
) + 1;
3437 /* generate char[len] type */
3442 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3443 ptr
= section_ptr_add(data_section
, len
);
3444 memcpy(ptr
, funcname
, len
);
3449 #ifdef TCC_TARGET_PE
3450 t
= VT_SHORT
| VT_UNSIGNED
;
3456 /* string parsing */
3459 if (tcc_state
->warn_write_strings
)
3464 memset(&ad
, 0, sizeof(AttributeDef
));
3465 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3470 if (parse_btype(&type
, &ad
)) {
3471 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3473 /* check ISOC99 compound literal */
3475 /* data is allocated locally by default */
3480 /* all except arrays are lvalues */
3481 if (!(type
.t
& VT_ARRAY
))
3482 r
|= lvalue_type(type
.t
);
3483 memset(&ad
, 0, sizeof(AttributeDef
));
3484 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3486 if (sizeof_caller
) {
3493 } else if (tok
== '{') {
3494 /* save all registers */
3496 /* statement expression : we do not accept break/continue
3497 inside as GCC does */
3498 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3513 /* functions names must be treated as function pointers,
3514 except for unary '&' and sizeof. Since we consider that
3515 functions are not lvalues, we only have to handle it
3516 there and in function calls. */
3517 /* arrays can also be used although they are not lvalues */
3518 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3519 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3521 mk_pointer(&vtop
->type
);
3527 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3529 boolean
.t
= VT_BOOL
;
3531 vtop
->c
.i
= !vtop
->c
.i
;
3532 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3533 vtop
->c
.i
= vtop
->c
.i
^ 1;
3536 vseti(VT_JMP
, gtst(1, 0));
3547 /* in order to force cast, we add zero */
3549 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3550 error("pointer not accepted for unary plus");
3560 unary_type(&type
); // Perform a in_sizeof = 0;
3561 if ((t
== TOK_SIZEOF
) && (type
.t
& VT_VLA
)) {
3562 vla_runtime_type_size(&type
, &align
);
3565 size
= type_size(&type
, &align
);
3567 if (t
== TOK_SIZEOF
) {
3568 if (!(type
.t
& VT_VLA
)) {
3570 error("sizeof applied to an incomplete type");
3576 vtop
->type
.t
|= VT_UNSIGNED
;
3579 case TOK_builtin_types_compatible_p
:
3588 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3589 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3590 vpushi(is_compatible_types(&type1
, &type2
));
3593 case TOK_builtin_constant_p
:
3595 int saved_nocode_wanted
, res
;
3598 saved_nocode_wanted
= nocode_wanted
;
3601 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3603 nocode_wanted
= saved_nocode_wanted
;
3608 case TOK_builtin_frame_address
:
3613 if (tok
!= TOK_CINT
) {
3614 error("__builtin_frame_address only takes integers");
3617 error("TCC only supports __builtin_frame_address(0)");
3623 vset(&type
, VT_LOCAL
, 0);
3626 #ifdef TCC_TARGET_X86_64
3627 case TOK_builtin_va_arg_types
:
3629 /* This definition must be synced with stdarg.h */
3630 enum __va_arg_type
{
3631 __va_gen_reg
, __va_float_reg
, __va_stack
3639 bt
= type
.t
& VT_BTYPE
;
3640 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3642 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3643 vpushi(__va_float_reg
);
3645 vpushi(__va_gen_reg
);
3665 goto tok_identifier
;
3667 /* allow to take the address of a label */
3668 if (tok
< TOK_UIDENT
)
3669 expect("label identifier");
3670 s
= label_find(tok
);
3672 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3674 if (s
->r
== LABEL_DECLARED
)
3675 s
->r
= LABEL_FORWARD
;
3678 s
->type
.t
= VT_VOID
;
3679 mk_pointer(&s
->type
);
3680 s
->type
.t
|= VT_STATIC
;
3682 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3687 // special qnan , snan and infinity values
3689 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3693 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3697 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3706 expect("identifier");
3710 error("'%s' undeclared", get_tok_str(t
, NULL
));
3711 /* for simple function calls, we tolerate undeclared
3712 external reference to int() function */
3713 if (tcc_state
->warn_implicit_function_declaration
)
3714 warning("implicit declaration of function '%s'",
3715 get_tok_str(t
, NULL
));
3716 s
= external_global_sym(t
, &func_old_type
, 0);
3718 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3719 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3720 /* if referencing an inline function, then we generate a
3721 symbol to it if not already done. It will have the
3722 effect to generate code for it at the end of the
3723 compilation unit. Inline function as always
3724 generated in the text section. */
3726 put_extern_sym(s
, text_section
, 0, 0);
3727 r
= VT_SYM
| VT_CONST
;
3731 if (s
->type
.t
& VT_VLA
)
3734 vset(&s
->type
, r
, s
->c
);
3735 /* if forward reference, we must point to s */
3736 if (vtop
->r
& VT_SYM
) {
3743 /* post operations */
3745 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3748 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3751 if (tok
== TOK_ARROW
)
3753 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3757 /* expect pointer on structure */
3758 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3759 expect("struct or union");
3763 while ((s
= s
->next
) != NULL
) {
3768 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3769 /* add field offset to pointer */
3770 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3773 /* change type to field type, and set to lvalue */
3774 vtop
->type
= s
->type
;
3775 vtop
->type
.t
|= qualifiers
;
3776 /* an array is never an lvalue */
3777 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3778 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3779 #ifdef CONFIG_TCC_BCHECK
3780 /* if bound checking, the referenced pointer must be checked */
3781 if (tcc_state
->do_bounds_check
)
3782 vtop
->r
|= VT_MUSTBOUND
;
3786 } else if (tok
== '[') {
3792 } else if (tok
== '(') {
3798 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3799 /* pointer test (no array accepted) */
3800 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3801 vtop
->type
= *pointed_type(&vtop
->type
);
3802 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3806 expect("function pointer");
3809 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3811 /* get return type */
3814 sa
= s
->next
; /* first parameter */
3817 /* compute first implicit argument if a structure is returned */
3818 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3819 /* get some space for the returned structure */
3820 size
= type_size(&s
->type
, &align
);
3821 loc
= (loc
- size
) & -align
;
3823 ret
.r
= VT_LOCAL
| VT_LVAL
;
3824 /* pass it as 'int' to avoid structure arg passing
3826 vseti(VT_LOCAL
, loc
);
3831 /* return in register */
3832 if (is_float(ret
.type
.t
)) {
3833 ret
.r
= reg_fret(ret
.type
.t
);
3835 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3844 gfunc_param_typed(s
, sa
);
3854 error("too few arguments to function");
3856 if (!nocode_wanted
) {
3857 gfunc_call(nb_args
);
3859 vtop
-= (nb_args
+ 1);
3862 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3870 ST_FUNC
void expr_prod(void)
3875 while (tok
== '*' || tok
== '/' || tok
== '%') {
3883 ST_FUNC
void expr_sum(void)
3888 while (tok
== '+' || tok
== '-') {
3896 static void expr_shift(void)
3901 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3909 static void expr_cmp(void)
3914 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3915 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3923 static void expr_cmpeq(void)
3928 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3936 static void expr_and(void)
3939 while (tok
== '&') {
3946 static void expr_xor(void)
3949 while (tok
== '^') {
3956 static void expr_or(void)
3959 while (tok
== '|') {
3966 /* XXX: fix this mess */
3967 static void expr_land_const(void)
3970 while (tok
== TOK_LAND
) {
3977 /* XXX: fix this mess */
3978 static void expr_lor_const(void)
3981 while (tok
== TOK_LOR
) {
3988 /* only used if non constant */
3989 static void expr_land(void)
3994 if (tok
== TOK_LAND
) {
3999 if (tok
!= TOK_LAND
) {
4009 static void expr_lor(void)
4014 if (tok
== TOK_LOR
) {
4019 if (tok
!= TOK_LOR
) {
4029 /* XXX: better constant handling */
4030 static void expr_cond(void)
4032 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4034 CType type
, type1
, type2
;
4041 boolean
.t
= VT_BOOL
;
4047 if (tok
!= ':' || !gnu_ext
) {
4062 if (vtop
!= vstack
) {
4063 /* needed to avoid having different registers saved in
4065 if (is_float(vtop
->type
.t
)) {
4067 #ifdef TCC_TARGET_X86_64
4068 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4078 if (tok
== ':' && gnu_ext
) {
4086 sv
= *vtop
; /* save value to handle it later */
4087 vtop
--; /* no vpop so that FP stack is not flushed */
4095 bt1
= t1
& VT_BTYPE
;
4097 bt2
= t2
& VT_BTYPE
;
4098 /* cast operands to correct type according to ISOC rules */
4099 if (is_float(bt1
) || is_float(bt2
)) {
4100 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4101 type
.t
= VT_LDOUBLE
;
4102 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4107 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4108 /* cast to biggest op */
4110 /* convert to unsigned if it does not fit in a long long */
4111 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4112 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4113 type
.t
|= VT_UNSIGNED
;
4114 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4115 /* XXX: test pointer compatibility */
4117 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4118 /* XXX: test function pointer compatibility */
4120 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4121 /* XXX: test structure compatibility */
4123 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4124 /* NOTE: as an extension, we accept void on only one side */
4127 /* integer operations */
4129 /* convert to unsigned if it does not fit in an integer */
4130 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4131 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4132 type
.t
|= VT_UNSIGNED
;
4135 /* now we convert second operand */
4137 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4140 if (is_float(type
.t
)) {
4142 #ifdef TCC_TARGET_X86_64
4143 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4147 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4148 /* for long longs, we use fixed registers to avoid having
4149 to handle a complicated move */
4154 /* this is horrible, but we must also convert first
4158 /* put again first value and cast it */
4161 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4171 static void expr_eq(void)
4177 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4178 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4179 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4194 ST_FUNC
void gexpr(void)
4205 /* parse an expression and return its type without any side effect. */
4206 static void expr_type(CType
*type
)
4208 int saved_nocode_wanted
;
4210 saved_nocode_wanted
= nocode_wanted
;
4215 nocode_wanted
= saved_nocode_wanted
;
4218 /* parse a unary expression and return its type without any side
4220 static void unary_type(CType
*type
)
4232 /* parse a constant expression and return value in vtop. */
4233 static void expr_const1(void)
4242 /* parse an integer constant and return its value. */
4243 ST_FUNC
int expr_const(void)
4247 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4248 expect("constant expression");
4254 /* return the label token if current token is a label, otherwise
4256 static int is_label(void)
4260 /* fast test first */
4261 if (tok
< TOK_UIDENT
)
4263 /* no need to save tokc because tok is an identifier */
4270 unget_tok(last_tok
);
4275 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4276 int case_reg
, int is_expr
)
4281 /* generate line number info */
4282 if (tcc_state
->do_debug
&&
4283 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4284 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4286 last_line_num
= file
->line_num
;
4290 /* default return value is (void) */
4292 vtop
->type
.t
= VT_VOID
;
4295 if (tok
== TOK_IF
) {
4302 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4304 if (c
== TOK_ELSE
) {
4308 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4309 gsym(d
); /* patch else jmp */
4312 } else if (tok
== TOK_WHILE
) {
4320 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4324 } else if (tok
== '{') {
4328 /* record local declaration stack position */
4330 llabel
= local_label_stack
;
4331 /* handle local labels declarations */
4332 if (tok
== TOK_LABEL
) {
4335 if (tok
< TOK_UIDENT
)
4336 expect("label identifier");
4337 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4347 while (tok
!= '}') {
4352 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4355 /* pop locally defined labels */
4356 label_pop(&local_label_stack
, llabel
);
4357 /* pop locally defined symbols */
4359 /* XXX: this solution makes only valgrind happy...
4360 triggered by gcc.c-torture/execute/20000917-1.c */
4362 switch(vtop
->type
.t
& VT_BTYPE
) {
4367 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4369 error("unsupported expression type");
4372 sym_pop(&local_stack
, s
);
4374 } else if (tok
== TOK_RETURN
) {
4378 gen_assign_cast(&func_vt
);
4379 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4381 /* if returning structure, must copy it to implicit
4382 first pointer arg location */
4385 size
= type_size(&func_vt
,&align
);
4388 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4392 loc
= (loc
- size
) & -4;
4395 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4398 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4400 vtop
->type
= int_type
;
4406 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4409 /* copy structure value to pointer */
4414 } else if (is_float(func_vt
.t
)) {
4415 gv(rc_fret(func_vt
.t
));
4419 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4422 rsym
= gjmp(rsym
); /* jmp */
4423 } else if (tok
== TOK_BREAK
) {
4426 error("cannot break");
4427 *bsym
= gjmp(*bsym
);
4430 } else if (tok
== TOK_CONTINUE
) {
4433 error("cannot continue");
4434 *csym
= gjmp(*csym
);
4437 } else if (tok
== TOK_FOR
) {
4464 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4469 if (tok
== TOK_DO
) {
4474 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4485 if (tok
== TOK_SWITCH
) {
4489 /* XXX: other types than integer */
4490 case_reg
= gv(RC_INT
);
4494 b
= gjmp(0); /* jump to first case */
4496 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4497 /* if no default, jmp after switch */
4505 if (tok
== TOK_CASE
) {
4512 if (gnu_ext
&& tok
== TOK_DOTS
) {
4516 warning("empty case range");
4518 /* since a case is like a label, we must skip it with a jmp */
4525 *case_sym
= gtst(1, 0);
4528 *case_sym
= gtst(1, 0);
4532 *case_sym
= gtst(1, *case_sym
);
4537 goto block_after_label
;
4539 if (tok
== TOK_DEFAULT
) {
4545 error("too many 'default'");
4548 goto block_after_label
;
4550 if (tok
== TOK_GOTO
) {
4552 if (tok
== '*' && gnu_ext
) {
4556 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4559 } else if (tok
>= TOK_UIDENT
) {
4560 s
= label_find(tok
);
4561 /* put forward definition if needed */
4563 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4565 if (s
->r
== LABEL_DECLARED
)
4566 s
->r
= LABEL_FORWARD
;
4568 /* label already defined */
4569 if (s
->r
& LABEL_FORWARD
)
4570 s
->jnext
= gjmp(s
->jnext
);
4572 gjmp_addr(s
->jnext
);
4575 expect("label identifier");
4578 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4586 if (s
->r
== LABEL_DEFINED
)
4587 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4589 s
->r
= LABEL_DEFINED
;
4591 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4594 /* we accept this, but it is a mistake */
4597 warning("deprecated use of label at end of compound statement");
4601 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4604 /* expression case */
4619 /* t is the array or struct type. c is the array or struct
4620 address. cur_index/cur_field is the pointer to the current
4621 value. 'size_only' is true if only size info is needed (only used
4623 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4624 int *cur_index
, Sym
**cur_field
,
4628 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4634 if (gnu_ext
&& (l
= is_label()) != 0)
4636 while (tok
== '[' || tok
== '.') {
4638 if (!(type
->t
& VT_ARRAY
))
4639 expect("array type");
4642 index
= expr_const();
4643 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4644 expect("invalid index");
4645 if (tok
== TOK_DOTS
&& gnu_ext
) {
4647 index_last
= expr_const();
4648 if (index_last
< 0 ||
4649 (s
->c
>= 0 && index_last
>= s
->c
) ||
4651 expect("invalid index");
4657 *cur_index
= index_last
;
4658 type
= pointed_type(type
);
4659 elem_size
= type_size(type
, &align
);
4660 c
+= index
* elem_size
;
4661 /* NOTE: we only support ranges for last designator */
4662 nb_elems
= index_last
- index
+ 1;
4663 if (nb_elems
!= 1) {
4672 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4673 expect("struct/union type");
4686 /* XXX: fix this mess by using explicit storage field */
4688 type1
.t
|= (type
->t
& ~VT_TYPE
);
4702 if (type
->t
& VT_ARRAY
) {
4704 type
= pointed_type(type
);
4705 c
+= index
* type_size(type
, &align
);
4709 error("too many field init");
4710 /* XXX: fix this mess by using explicit storage field */
4712 type1
.t
|= (type
->t
& ~VT_TYPE
);
4717 decl_initializer(type
, sec
, c
, 0, size_only
);
4719 /* XXX: make it more general */
4720 if (!size_only
&& nb_elems
> 1) {
4721 unsigned long c_end
;
4726 error("range init not supported yet for dynamic storage");
4727 c_end
= c
+ nb_elems
* elem_size
;
4728 if (c_end
> sec
->data_allocated
)
4729 section_realloc(sec
, c_end
);
4730 src
= sec
->data
+ c
;
4732 for(i
= 1; i
< nb_elems
; i
++) {
4734 memcpy(dst
, src
, elem_size
);
4740 #define EXPR_CONST 1
4743 /* store a value or an expression directly in global data or in local array */
4744 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4745 int v
, int expr_type
)
4747 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4749 unsigned long long bit_mask
;
4757 /* compound literals must be allocated globally in this case */
4758 saved_global_expr
= global_expr
;
4761 global_expr
= saved_global_expr
;
4762 /* NOTE: symbols are accepted */
4763 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4764 error("initializer element is not constant");
4772 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4775 /* XXX: not portable */
4776 /* XXX: generate error if incorrect relocation */
4777 gen_assign_cast(&dtype
);
4778 bt
= type
->t
& VT_BTYPE
;
4779 /* we'll write at most 12 bytes */
4780 if (c
+ 12 > sec
->data_allocated
) {
4781 section_realloc(sec
, c
+ 12);
4783 ptr
= sec
->data
+ c
;
4784 /* XXX: make code faster ? */
4785 if (!(type
->t
& VT_BITFIELD
)) {
4790 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4791 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4792 bit_mask
= (1LL << bit_size
) - 1;
4794 if ((vtop
->r
& VT_SYM
) &&
4800 (bt
== VT_INT
&& bit_size
!= 32)))
4801 error("initializer element is not computable at load time");
4804 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4806 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4809 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4812 *(double *)ptr
= vtop
->c
.d
;
4815 *(long double *)ptr
= vtop
->c
.ld
;
4818 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4821 if (vtop
->r
& VT_SYM
) {
4822 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4824 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4829 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4836 /* put zeros for variable based init */
4837 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4840 /* nothing to do because globals are already set to zero */
4842 vpush_global_sym(&func_old_type
, TOK_memset
);
4850 /* 't' contains the type and storage info. 'c' is the offset of the
4851 object in section 'sec'. If 'sec' is NULL, it means stack based
4852 allocation. 'first' is true if array '{' must be read (multi
4853 dimension implicit array init handling). 'size_only' is true if
4854 size only evaluation is wanted (only for arrays). */
4855 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4856 int first
, int size_only
)
4858 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4859 int size1
, align1
, expr_type
;
4863 if (type
->t
& VT_VLA
) {
4867 vpush_global_sym(&func_old_type
, TOK_alloca
);
4868 vla_runtime_type_size(type
, &a
);
4873 vsetc(type
, REG_IRET
, &retcval
);
4874 } else if (type
->t
& VT_ARRAY
) {
4878 t1
= pointed_type(type
);
4879 size1
= type_size(t1
, &align1
);
4882 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4885 error("character array initializer must be a literal,"
4886 " optionally enclosed in braces");
4891 /* only parse strings here if correct type (otherwise: handle
4892 them as ((w)char *) expressions */
4893 if ((tok
== TOK_LSTR
&&
4894 #ifdef TCC_TARGET_PE
4895 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4897 (t1
->t
& VT_BTYPE
) == VT_INT
4899 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4900 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4905 /* compute maximum number of chars wanted */
4907 cstr_len
= cstr
->size
;
4909 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4912 if (n
>= 0 && nb
> (n
- array_length
))
4913 nb
= n
- array_length
;
4916 warning("initializer-string for array is too long");
4917 /* in order to go faster for common case (char
4918 string in global variable, we handle it
4920 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4921 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4925 ch
= ((unsigned char *)cstr
->data
)[i
];
4927 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4928 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4936 /* only add trailing zero if enough storage (no
4937 warning in this case since it is standard) */
4938 if (n
< 0 || array_length
< n
) {
4940 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4946 while (tok
!= '}') {
4947 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4948 if (n
>= 0 && index
>= n
)
4949 error("index too large");
4950 /* must put zero in holes (note that doing it that way
4951 ensures that it even works with designators) */
4952 if (!size_only
&& array_length
< index
) {
4953 init_putz(t1
, sec
, c
+ array_length
* size1
,
4954 (index
- array_length
) * size1
);
4957 if (index
> array_length
)
4958 array_length
= index
;
4959 /* special test for multi dimensional arrays (may not
4960 be strictly correct if designators are used at the
4962 if (index
>= n
&& no_oblock
)
4971 /* put zeros at the end */
4972 if (!size_only
&& n
>= 0 && array_length
< n
) {
4973 init_putz(t1
, sec
, c
+ array_length
* size1
,
4974 (n
- array_length
) * size1
);
4976 /* patch type size if needed */
4978 s
->c
= array_length
;
4979 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4980 (sec
|| !first
|| tok
== '{')) {
4983 /* NOTE: the previous test is a specific case for automatic
4984 struct/union init */
4985 /* XXX: union needs only one init */
4987 /* XXX: this test is incorrect for local initializers
4988 beginning with ( without {. It would be much more difficult
4989 to do it correctly (ideally, the expression parser should
4990 be used in all cases) */
4996 while (tok
== '(') {
5000 if (!parse_btype(&type1
, &ad1
))
5002 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5004 if (!is_assignable_types(type
, &type1
))
5005 error("invalid type for cast");
5010 if (first
|| tok
== '{') {
5019 while (tok
!= '}') {
5020 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5022 if (!size_only
&& array_length
< index
) {
5023 init_putz(type
, sec
, c
+ array_length
,
5024 index
- array_length
);
5026 index
= index
+ type_size(&f
->type
, &align1
);
5027 if (index
> array_length
)
5028 array_length
= index
;
5030 /* gr: skip fields from same union - ugly. */
5032 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5033 /* test for same offset */
5034 if (f
->next
->c
!= f
->c
)
5036 /* if yes, test for bitfield shift */
5037 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5038 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5039 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5040 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5041 if (bit_pos_1
!= bit_pos_2
)
5048 if (no_oblock
&& f
== NULL
)
5054 /* put zeros at the end */
5055 if (!size_only
&& array_length
< n
) {
5056 init_putz(type
, sec
, c
+ array_length
,
5065 } else if (tok
== '{') {
5067 decl_initializer(type
, sec
, c
, first
, size_only
);
5069 } else if (size_only
) {
5070 /* just skip expression */
5072 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
5076 else if (tok
== ')')
5081 /* currently, we always use constant expression for globals
5082 (may change for scripting case) */
5083 expr_type
= EXPR_CONST
;
5085 expr_type
= EXPR_ANY
;
5086 init_putv(type
, sec
, c
, 0, expr_type
);
5090 /* parse an initializer for type 't' if 'has_init' is non zero, and
5091 allocate space in local or global data space ('r' is either
5092 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5093 variable 'v' of scope 'scope' is declared before initializers are
5094 parsed. If 'v' is zero, then a reference to the new object is put
5095 in the value stack. If 'has_init' is 2, a special parsing is done
5096 to handle string constants. */
5097 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5098 int has_init
, int v
, int scope
)
5100 int size
, align
, addr
, data_offset
;
5102 ParseState saved_parse_state
= {0};
5103 TokenString init_str
;
5107 size
= type_size(type
, &align
);
5108 /* If unknown size, we must evaluate it before
5109 evaluating initializers because
5110 initializers can generate global data too
5111 (e.g. string pointers or ISOC99 compound
5112 literals). It also simplifies local
5113 initializers handling */
5114 tok_str_new(&init_str
);
5117 error("unknown type size");
5118 /* get all init string */
5119 if (has_init
== 2) {
5120 /* only get strings */
5121 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5122 tok_str_add_tok(&init_str
);
5127 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5129 error("unexpected end of file in initializer");
5130 tok_str_add_tok(&init_str
);
5133 else if (tok
== '}') {
5143 tok_str_add(&init_str
, -1);
5144 tok_str_add(&init_str
, 0);
5147 save_parse_state(&saved_parse_state
);
5149 macro_ptr
= init_str
.str
;
5151 decl_initializer(type
, NULL
, 0, 1, 1);
5152 /* prepare second initializer parsing */
5153 macro_ptr
= init_str
.str
;
5156 /* if still unknown size, error */
5157 size
= type_size(type
, &align
);
5159 error("unknown type size");
5161 /* take into account specified alignment if bigger */
5163 if (ad
->aligned
> align
)
5164 align
= ad
->aligned
;
5165 } else if (ad
->packed
) {
5168 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5170 #ifdef CONFIG_TCC_BCHECK
5171 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5172 if (type
->t
& VT_VLA
)
5173 warning("Array bound check don't work for VLA");
5178 loc
= (loc
- size
) & -align
;
5180 #ifdef CONFIG_TCC_BCHECK
5181 /* handles bounds */
5182 /* XXX: currently, since we do only one pass, we cannot track
5183 '&' operators, so we add only arrays */
5184 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
) &&
5185 !(type
->t
& VT_VLA
)) {
5186 unsigned long *bounds_ptr
;
5187 /* add padding between regions */
5189 /* then add local bound info */
5190 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5191 bounds_ptr
[0] = addr
;
5192 bounds_ptr
[1] = size
;
5196 /* local variable */
5197 vla
= sym_push(v
, type
, r
, addr
);
5199 /* push local reference */
5200 vset(type
, r
, addr
);
5206 if (v
&& scope
== VT_CONST
) {
5207 /* see if the symbol was already defined */
5210 if (!is_compatible_types(&sym
->type
, type
))
5211 error("incompatible types for redefinition of '%s'",
5212 get_tok_str(v
, NULL
));
5213 if (sym
->type
.t
& VT_EXTERN
) {
5214 /* if the variable is extern, it was not allocated */
5215 sym
->type
.t
&= ~VT_EXTERN
;
5216 /* set array size if it was ommited in extern
5218 if ((sym
->type
.t
& VT_ARRAY
) &&
5219 sym
->type
.ref
->c
< 0 &&
5221 sym
->type
.ref
->c
= type
->ref
->c
;
5223 /* we accept several definitions of the same
5224 global variable. this is tricky, because we
5225 must play with the SHN_COMMON type of the symbol */
5226 /* XXX: should check if the variable was already
5227 initialized. It is incorrect to initialized it
5229 /* no init data, we won't add more to the symbol */
5236 /* allocate symbol in corresponding section */
5241 else if (tcc_state
->nocommon
)
5245 data_offset
= sec
->data_offset
;
5246 data_offset
= (data_offset
+ align
- 1) & -align
;
5248 /* very important to increment global pointer at this time
5249 because initializers themselves can create new initializers */
5250 data_offset
+= size
;
5251 #ifdef CONFIG_TCC_BCHECK
5252 /* add padding if bound check */
5253 if (tcc_state
->do_bounds_check
)
5256 sec
->data_offset
= data_offset
;
5257 /* allocate section space to put the data */
5258 if (sec
->sh_type
!= SHT_NOBITS
&&
5259 data_offset
> sec
->data_allocated
)
5260 section_realloc(sec
, data_offset
);
5261 /* align section if needed */
5262 if (align
> sec
->sh_addralign
)
5263 sec
->sh_addralign
= align
;
5265 addr
= 0; /* avoid warning */
5269 if (scope
!= VT_CONST
|| !sym
) {
5270 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5272 /* update symbol definition */
5274 put_extern_sym(sym
, sec
, addr
, size
);
5277 /* put a common area */
5278 put_extern_sym(sym
, NULL
, align
, size
);
5279 /* XXX: find a nicer way */
5280 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5281 esym
->st_shndx
= SHN_COMMON
;
5286 /* push global reference */
5287 sym
= get_sym_ref(type
, sec
, addr
, size
);
5289 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5292 /* patch symbol weakness */
5293 if (type
->t
& VT_WEAK
) {
5294 unsigned char *st_info
=
5295 &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_info
;
5296 *st_info
= ELF32_ST_INFO(STB_WEAK
, ELF32_ST_TYPE(*st_info
));
5298 #ifdef CONFIG_TCC_BCHECK
5299 /* handles bounds now because the symbol must be defined
5300 before for the relocation */
5301 if (tcc_state
->do_bounds_check
) {
5302 unsigned long *bounds_ptr
;
5304 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5305 /* then add global bound info */
5306 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5307 bounds_ptr
[0] = 0; /* relocated */
5308 bounds_ptr
[1] = size
;
5312 if (has_init
|| (type
->t
& VT_VLA
)) {
5313 decl_initializer(type
, sec
, addr
, 1, 0);
5314 if (type
->t
& VT_VLA
)
5316 /* restore parse state if needed */
5318 tok_str_free(init_str
.str
);
5319 restore_parse_state(&saved_parse_state
);
5325 static void put_func_debug(Sym
*sym
)
5330 /* XXX: we put here a dummy type */
5331 snprintf(buf
, sizeof(buf
), "%s:%c1",
5332 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5333 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5334 cur_text_section
, sym
->c
);
5335 /* //gr gdb wants a line at the function */
5336 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5341 /* parse an old style function declaration list */
5342 /* XXX: check multiple parameter */
5343 static void func_decl_list(Sym
*func_sym
)
5350 /* parse each declaration */
5351 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
5352 if (!parse_btype(&btype
, &ad
))
5353 expect("declaration list");
5354 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5355 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5357 /* we accept no variable after */
5361 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5362 /* find parameter in function parameter list */
5365 if ((s
->v
& ~SYM_FIELD
) == v
)
5369 error("declaration for parameter '%s' but no such parameter",
5370 get_tok_str(v
, NULL
));
5372 /* check that no storage specifier except 'register' was given */
5373 if (type
.t
& VT_STORAGE
)
5374 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5375 convert_parameter_type(&type
);
5376 /* we can add the type (NOTE: it could be local to the function) */
5378 /* accept other parameters */
5389 /* parse a function defined by symbol 'sym' and generate its code in
5390 'cur_text_section' */
5391 static void gen_function(Sym
*sym
)
5393 int saved_nocode_wanted
= nocode_wanted
;
5395 ind
= cur_text_section
->data_offset
;
5396 /* NOTE: we patch the symbol size later */
5397 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5399 funcname
= get_tok_str(sym
->a
, NULL
);
5401 funcname
= get_tok_str(sym
->v
, NULL
);
5403 /* put debug symbol */
5404 if (tcc_state
->do_debug
)
5405 put_func_debug(sym
);
5406 /* push a dummy symbol to enable local sym storage */
5407 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5408 gfunc_prolog(&sym
->type
);
5410 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5413 cur_text_section
->data_offset
= ind
;
5414 label_pop(&global_label_stack
, NULL
);
5415 sym_pop(&local_stack
, NULL
); /* reset local stack */
5416 /* end of function */
5417 /* patch symbol size */
5418 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5420 /* patch symbol weakness (this definition overrules any prototype) */
5421 if (sym
->type
.t
& VT_WEAK
) {
5422 unsigned char *st_info
=
5423 &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_info
;
5424 *st_info
= ELF32_ST_INFO(STB_WEAK
, ELF32_ST_TYPE(*st_info
));
5426 if (tcc_state
->do_debug
) {
5427 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5429 /* It's better to crash than to generate wrong code */
5430 cur_text_section
= NULL
;
5431 funcname
= ""; /* for safety */
5432 func_vt
.t
= VT_VOID
; /* for safety */
5433 ind
= 0; /* for safety */
5434 nocode_wanted
= saved_nocode_wanted
;
5437 ST_FUNC
void gen_inline_functions(void)
5440 int *str
, inline_generated
, i
;
5441 struct InlineFunc
*fn
;
5443 /* iterate while inline function are referenced */
5445 inline_generated
= 0;
5446 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5447 fn
= tcc_state
->inline_fns
[i
];
5449 if (sym
&& sym
->c
) {
5450 /* the function was used: generate its code and
5451 convert it to a normal function */
5452 str
= fn
->token_str
;
5455 strcpy(file
->filename
, fn
->filename
);
5456 sym
->r
= VT_SYM
| VT_CONST
;
5457 sym
->type
.t
&= ~VT_INLINE
;
5461 cur_text_section
= text_section
;
5463 macro_ptr
= NULL
; /* fail safe */
5465 inline_generated
= 1;
5468 if (!inline_generated
)
5471 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5472 fn
= tcc_state
->inline_fns
[i
];
5473 str
= fn
->token_str
;
5476 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5479 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5480 ST_FUNC
void decl(int l
)
5488 * type.ref must be either a valid reference or NULL for external_sym to
5489 * work. As type = btype is executed before external_sym is call, setting
5490 * btype.ref to 0 is enough.
5494 if (!parse_btype(&btype
, &ad
)) {
5495 /* skip redundant ';' */
5496 /* XXX: find more elegant solution */
5501 if (l
== VT_CONST
&&
5502 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5503 /* global asm block */
5507 /* special test for old K&R protos without explicit int
5508 type. Only accepted when defining global data */
5509 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5513 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5514 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5516 /* we accept no variable after */
5520 while (1) { /* iterate thru each declaration */
5522 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5526 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5527 printf("type = '%s'\n", buf
);
5530 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5531 /* if old style function prototype, we accept a
5534 if (sym
->c
== FUNC_OLD
)
5535 func_decl_list(sym
);
5538 #ifdef TCC_TARGET_PE
5540 type
.t
|= VT_IMPORT
;
5542 type
.t
|= VT_EXPORT
;
5546 error("cannot use local functions");
5547 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5548 expect("function definition");
5550 /* reject abstract declarators in function definition */
5552 while ((sym
= sym
->next
) != NULL
)
5553 if (!(sym
->v
& ~SYM_FIELD
))
5554 expect("identifier");
5556 /* XXX: cannot do better now: convert extern line to static inline */
5557 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5558 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5562 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5565 r
= sym
->type
.ref
->r
;
5566 /* use func_call from prototype if not defined */
5567 if (FUNC_CALL(r
) != FUNC_CDECL
5568 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5569 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5571 /* use export from prototype */
5573 FUNC_EXPORT(type
.ref
->r
) = 1;
5575 /* use static from prototype */
5576 if (sym
->type
.t
& VT_STATIC
)
5577 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5579 if (!is_compatible_types(&sym
->type
, &type
)) {
5581 error("incompatible types for redefinition of '%s'",
5582 get_tok_str(v
, NULL
));
5584 /* if symbol is already defined, then put complete type */
5587 /* put function symbol */
5588 sym
= global_identifier_push(v
, type
.t
, 0);
5589 sym
->type
.ref
= type
.ref
;
5592 /* static inline functions are just recorded as a kind
5593 of macro. Their code will be emitted at the end of
5594 the compilation unit only if they are used */
5595 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5596 (VT_INLINE
| VT_STATIC
)) {
5597 TokenString func_str
;
5599 struct InlineFunc
*fn
;
5600 const char *filename
;
5602 tok_str_new(&func_str
);
5608 error("unexpected end of file");
5609 tok_str_add_tok(&func_str
);
5614 } else if (t
== '}') {
5616 if (block_level
== 0)
5620 tok_str_add(&func_str
, -1);
5621 tok_str_add(&func_str
, 0);
5622 filename
= file
? file
->filename
: "";
5623 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5624 strcpy(fn
->filename
, filename
);
5626 fn
->token_str
= func_str
.str
;
5627 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5630 /* compute text section */
5631 cur_text_section
= ad
.section
;
5632 if (!cur_text_section
)
5633 cur_text_section
= text_section
;
5634 sym
->r
= VT_SYM
| VT_CONST
;
5639 if (btype
.t
& VT_TYPEDEF
) {
5640 /* save typedefed type */
5641 /* XXX: test storage specifiers ? */
5642 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5643 sym
->type
.t
|= VT_TYPEDEF
;
5644 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5646 /* external function definition */
5647 /* specific case for func_call attribute */
5648 type
.ref
->r
= INT_ATTR(&ad
);
5649 fn
= external_sym(v
, &type
, 0);
5651 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5657 /* Part 1: __USER_LABEL_PREFIX__ (user defined) */
5659 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5661 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5664 /* Part 2: api name */
5666 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5668 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5672 if (tcc_state
->warn_unsupported
)
5673 warning("ignoring redirection from %s to %s\n", get_tok_str(v
, NULL
), target
);
5675 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
5676 parse_attribute((AttributeDef
*) &fn
->type
.ref
->r
);
5679 /* not lvalue if array */
5681 if (!(type
.t
& VT_ARRAY
))
5682 r
|= lvalue_type(type
.t
);
5683 has_init
= (tok
== '=');
5684 if (has_init
&& (type
.t
& VT_VLA
))
5685 error("Variable length array cannot be initialized");
5686 if ((btype
.t
& VT_EXTERN
) ||
5687 ((type
.t
& VT_ARRAY
) && (!(type
.t
& VT_VLA
)) &&
5688 (type
.t
& VT_STATIC
) && !has_init
&& l
== VT_CONST
&&
5690 /* external variable */
5691 /* NOTE: as GCC, uninitialized global static
5692 arrays of null size are considered as
5694 external_sym(v
, &type
, r
);
5696 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5697 if (type
.t
& VT_STATIC
)
5703 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);