2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
57 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
59 ST_DATA
int const_wanted
; /* true if constant wanted */
60 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
61 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
64 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
65 ST_DATA
char *funcname
;
67 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType
*type
);
71 static inline CType
*pointed_type(CType
*type
);
72 static int is_compatible_types(CType
*type1
, CType
*type2
);
73 static int parse_btype(CType
*type
, AttributeDef
*ad
);
74 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
75 static void parse_expr_type(CType
*type
);
76 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
77 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
78 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
79 static void expr_eq(void);
80 static void unary_type(CType
*type
);
81 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
82 static void expr_type(CType
*type
);
84 ST_INLN
int is_float(int t
)
88 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
91 ST_FUNC
void test_lvalue(void)
93 if (!(vtop
->r
& VT_LVAL
))
97 /* ------------------------------------------------------------------------- */
98 /* symbol allocator */
99 static Sym
*__sym_malloc(void)
101 Sym
*sym_pool
, *sym
, *last_sym
;
104 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
105 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
107 last_sym
= sym_free_first
;
109 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
110 sym
->next
= last_sym
;
114 sym_free_first
= last_sym
;
118 static inline Sym
*sym_malloc(void)
121 sym
= sym_free_first
;
123 sym
= __sym_malloc();
124 sym_free_first
= sym
->next
;
128 ST_INLN
void sym_free(Sym
*sym
)
130 sym
->next
= sym_free_first
;
131 tcc_free(sym
->asm_label
);
132 sym_free_first
= sym
;
135 /* push, without hashing */
136 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
155 /* find a symbol and return its associated structure. 's' is the top
156 of the symbol stack */
157 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
167 /* structure lookup */
168 ST_INLN Sym
*struct_find(int v
)
171 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
173 return table_ident
[v
]->sym_struct
;
176 /* find an identifier */
177 ST_INLN Sym
*sym_find(int v
)
180 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
182 return table_ident
[v
]->sym_identifier
;
185 /* push a given symbol on the symbol stack */
186 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
195 s
= sym_push2(ps
, v
, type
->t
, c
);
196 s
->type
.ref
= type
->ref
;
198 /* don't record fields or anonymous symbols */
200 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
201 /* record symbol in token array */
202 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
204 ps
= &ts
->sym_struct
;
206 ps
= &ts
->sym_identifier
;
213 /* push a global identifier */
214 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
217 s
= sym_push2(&global_stack
, v
, t
, c
);
218 /* don't record anonymous symbol */
219 if (v
< SYM_FIRST_ANOM
) {
220 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
221 /* modify the top most local identifier, so that
222 sym_identifier will point to 's' when popped */
224 ps
= &(*ps
)->prev_tok
;
231 /* pop symbols until top reaches 'b' */
232 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
242 /* remove symbol in token array */
244 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
245 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
247 ps
= &ts
->sym_struct
;
249 ps
= &ts
->sym_identifier
;
258 /* ------------------------------------------------------------------------- */
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' with alternate asm
365 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
366 is no alternate name (most cases) */
367 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
373 /* push forward reference */
374 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
375 s
->asm_label
= asm_label
;
376 s
->type
.t
|= VT_EXTERN
;
377 } else if (s
->type
.ref
== func_old_type
.ref
) {
378 s
->type
.ref
= type
->ref
;
379 s
->r
= r
| VT_CONST
| VT_SYM
;
380 s
->type
.t
|= VT_EXTERN
;
381 } else if (!is_compatible_types(&s
->type
, type
)) {
382 error("incompatible types for redefinition of '%s'",
383 get_tok_str(v
, NULL
));
388 /* push a reference to global symbol v */
389 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
394 sym
= external_global_sym(v
, type
, 0);
396 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
400 ST_FUNC
void vset(CType
*type
, int r
, int v
)
405 vsetc(type
, r
, &cval
);
408 static void vseti(int r
, int v
)
416 ST_FUNC
void vswap(void)
425 ST_FUNC
void vpushv(SValue
*v
)
427 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
428 error("memory full");
433 static void vdup(void)
438 /* save r to the memory stack, and mark it as being free */
439 ST_FUNC
void save_reg(int r
)
441 int l
, saved
, size
, align
;
445 /* modify all stack values */
448 for(p
=vstack
;p
<=vtop
;p
++) {
449 if ((p
->r
& VT_VALMASK
) == r
||
450 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
451 /* must save value on stack if not already done */
453 /* NOTE: must reload 'r' because r might be equal to r2 */
454 r
= p
->r
& VT_VALMASK
;
455 /* store register in the stack */
457 if ((p
->r
& VT_LVAL
) ||
458 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
459 #ifdef TCC_TARGET_X86_64
460 type
= &char_pointer_type
;
464 size
= type_size(type
, &align
);
465 loc
= (loc
- size
) & -align
;
467 sv
.r
= VT_LOCAL
| VT_LVAL
;
470 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
471 /* x86 specific: need to pop fp register ST0 if saved */
473 o(0xd8dd); /* fstp %st(0) */
476 #ifndef TCC_TARGET_X86_64
477 /* special long long case */
478 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
486 /* mark that stack entry as being saved on the stack */
487 if (p
->r
& VT_LVAL
) {
488 /* also clear the bounded flag because the
489 relocation address of the function was stored in
491 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
493 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
501 #ifdef TCC_TARGET_ARM
502 /* find a register of class 'rc2' with at most one reference on stack.
503 * If none, call get_reg(rc) */
504 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
509 for(r
=0;r
<NB_REGS
;r
++) {
510 if (reg_classes
[r
] & rc2
) {
513 for(p
= vstack
; p
<= vtop
; p
++) {
514 if ((p
->r
& VT_VALMASK
) == r
||
515 (p
->r2
& VT_VALMASK
) == r
)
526 /* find a free register of class 'rc'. If none, save one register */
527 ST_FUNC
int get_reg(int rc
)
532 /* find a free register */
533 for(r
=0;r
<NB_REGS
;r
++) {
534 if (reg_classes
[r
] & rc
) {
535 for(p
=vstack
;p
<=vtop
;p
++) {
536 if ((p
->r
& VT_VALMASK
) == r
||
537 (p
->r2
& VT_VALMASK
) == r
)
545 /* no register left : free the first one on the stack (VERY
546 IMPORTANT to start from the bottom to ensure that we don't
547 spill registers used in gen_opi()) */
548 for(p
=vstack
;p
<=vtop
;p
++) {
549 r
= p
->r
& VT_VALMASK
;
550 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
552 /* also look at second register (if long long) */
553 r
= p
->r2
& VT_VALMASK
;
554 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
560 /* Should never comes here */
564 /* save registers up to (vtop - n) stack entry */
565 ST_FUNC
void save_regs(int n
)
570 for(p
= vstack
;p
<= p1
; p
++) {
571 r
= p
->r
& VT_VALMASK
;
578 /* move register 's' to 'r', and flush previous value of r to memory
580 static void move_reg(int r
, int s
)
593 /* get address of vtop (vtop MUST BE an lvalue) */
594 static void gaddrof(void)
597 /* tricky: if saved lvalue, then we can go back to lvalue */
598 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
599 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
602 #ifdef CONFIG_TCC_BCHECK
603 /* generate lvalue bound code */
604 static void gbound(void)
609 vtop
->r
&= ~VT_MUSTBOUND
;
610 /* if lvalue, then use checking code before dereferencing */
611 if (vtop
->r
& VT_LVAL
) {
612 /* if not VT_BOUNDED value, then make one */
613 if (!(vtop
->r
& VT_BOUNDED
)) {
614 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
615 /* must save type because we must set it to int to get pointer */
617 vtop
->type
.t
= VT_INT
;
620 gen_bounded_ptr_add();
621 vtop
->r
|= lval_type
;
624 /* then check for dereferencing */
625 gen_bounded_ptr_deref();
630 /* store vtop a register belonging to class 'rc'. lvalues are
631 converted to values. Cannot be used if cannot be converted to
632 register value (such as structures). */
633 ST_FUNC
int gv(int rc
)
635 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
637 /* NOTE: get_reg can modify vstack[] */
638 if (vtop
->type
.t
& VT_BITFIELD
) {
641 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
642 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
643 /* remove bit field info to avoid loops */
644 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
645 /* cast to int to propagate signedness in following ops */
646 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
651 if((vtop
->type
.t
& VT_UNSIGNED
) ||
652 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
653 type
.t
|= VT_UNSIGNED
;
655 /* generate shifts */
656 vpushi(bits
- (bit_pos
+ bit_size
));
658 vpushi(bits
- bit_size
);
659 /* NOTE: transformed to SHR if unsigned */
663 if (is_float(vtop
->type
.t
) &&
664 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
667 unsigned long offset
;
668 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
672 /* XXX: unify with initializers handling ? */
673 /* CPUs usually cannot use float constants, so we store them
674 generically in data segment */
675 size
= type_size(&vtop
->type
, &align
);
676 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
677 data_section
->data_offset
= offset
;
678 /* XXX: not portable yet */
679 #if defined(__i386__) || defined(__x86_64__)
680 /* Zero pad x87 tenbyte long doubles */
681 if (size
== LDOUBLE_SIZE
)
682 vtop
->c
.tab
[2] &= 0xffff;
684 ptr
= section_ptr_add(data_section
, size
);
686 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
690 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
694 ptr
[i
] = vtop
->c
.tab
[i
];
695 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
696 vtop
->r
|= VT_LVAL
| VT_SYM
;
700 #ifdef CONFIG_TCC_BCHECK
701 if (vtop
->r
& VT_MUSTBOUND
)
705 r
= vtop
->r
& VT_VALMASK
;
709 /* need to reload if:
711 - lvalue (need to dereference pointer)
712 - already a register, but not in the right class */
714 || (vtop
->r
& VT_LVAL
)
715 || !(reg_classes
[r
] & rc
)
716 #ifndef TCC_TARGET_X86_64
717 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
722 #ifndef TCC_TARGET_X86_64
723 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
725 unsigned long long ll
;
726 /* two register type load : expand to two words
728 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
731 vtop
->c
.ui
= ll
; /* first word */
733 vtop
->r
= r
; /* save register value */
734 vpushi(ll
>> 32); /* second word */
735 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
736 (vtop
->r
& VT_LVAL
)) {
737 /* We do not want to modifier the long long
738 pointer here, so the safest (and less
739 efficient) is to save all the other registers
740 in the stack. XXX: totally inefficient. */
742 /* load from memory */
745 vtop
[-1].r
= r
; /* save register value */
746 /* increment pointer to get second word */
747 vtop
->type
.t
= VT_INT
;
756 vtop
[-1].r
= r
; /* save register value */
757 vtop
->r
= vtop
[-1].r2
;
759 /* allocate second register */
763 /* write second register */
767 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
769 /* lvalue of scalar type : need to use lvalue type
770 because of possible cast */
773 /* compute memory access type */
774 if (vtop
->r
& VT_LVAL_BYTE
)
776 else if (vtop
->r
& VT_LVAL_SHORT
)
778 if (vtop
->r
& VT_LVAL_UNSIGNED
)
782 /* restore wanted type */
785 /* one register type load */
790 #ifdef TCC_TARGET_C67
791 /* uses register pairs for doubles */
792 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
799 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
800 ST_FUNC
void gv2(int rc1
, int rc2
)
804 /* generate more generic register first. But VT_JMP or VT_CMP
805 values must be generated first in all cases to avoid possible
807 v
= vtop
[0].r
& VT_VALMASK
;
808 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
813 /* test if reload is needed for first register */
814 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
824 /* test if reload is needed for first register */
825 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
831 /* wrapper around RC_FRET to return a register by type */
832 static int rc_fret(int t
)
834 #ifdef TCC_TARGET_X86_64
835 if (t
== VT_LDOUBLE
) {
842 /* wrapper around REG_FRET to return a register by type */
843 static int reg_fret(int t
)
845 #ifdef TCC_TARGET_X86_64
846 if (t
== VT_LDOUBLE
) {
853 /* expand long long on stack in two int registers */
854 static void lexpand(void)
858 u
= vtop
->type
.t
& VT_UNSIGNED
;
861 vtop
[0].r
= vtop
[-1].r2
;
862 vtop
[0].r2
= VT_CONST
;
863 vtop
[-1].r2
= VT_CONST
;
864 vtop
[0].type
.t
= VT_INT
| u
;
865 vtop
[-1].type
.t
= VT_INT
| u
;
868 #ifdef TCC_TARGET_ARM
869 /* expand long long on stack */
870 ST_FUNC
void lexpand_nr(void)
874 u
= vtop
->type
.t
& VT_UNSIGNED
;
877 vtop
->type
.t
= VT_INT
| u
;
878 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
880 vtop
[-1].c
.ui
= vtop
->c
.ull
;
881 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
883 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
885 vtop
->r
= vtop
[-1].r
;
886 } else if (v
> VT_CONST
) {
890 vtop
->r
= vtop
[-1].r2
;
891 vtop
[-1].r2
= VT_CONST
;
892 vtop
[-1].type
.t
= VT_INT
| u
;
896 /* build a long long from two ints */
897 static void lbuild(int t
)
900 vtop
[-1].r2
= vtop
[0].r
;
905 /* rotate n first stack elements to the bottom
906 I1 ... In -> I2 ... In I1 [top is right]
908 static void vrotb(int n
)
919 /* rotate n first stack elements to the top
920 I1 ... In -> In I1 ... I(n-1) [top is right]
922 ST_FUNC
void vrott(int n
)
928 for(i
= 0;i
< n
- 1; i
++)
929 vtop
[-i
] = vtop
[-i
- 1];
933 #ifdef TCC_TARGET_ARM
934 /* like vrott but in other direction
935 In ... I1 -> I(n-1) ... I1 In [top is right]
937 ST_FUNC
void vnrott(int n
)
943 for(i
= n
- 1; i
> 0; i
--)
944 vtop
[-i
] = vtop
[-i
+ 1];
949 /* pop stack value */
950 ST_FUNC
void vpop(void)
953 v
= vtop
->r
& VT_VALMASK
;
954 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
955 /* for x86, we need to pop the FP stack */
956 if (v
== TREG_ST0
&& !nocode_wanted
) {
957 o(0xd8dd); /* fstp %st(0) */
960 if (v
== VT_JMP
|| v
== VT_JMPI
) {
961 /* need to put correct jump if && or || without test */
967 /* convert stack entry to register and duplicate its value in another
969 static void gv_dup(void)
975 if ((t
& VT_BTYPE
) == VT_LLONG
) {
982 /* stack: H L L1 H1 */
990 /* duplicate value */
995 #ifdef TCC_TARGET_X86_64
996 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1006 load(r1
, &sv
); /* move r to r1 */
1008 /* duplicates value */
1014 #ifndef TCC_TARGET_X86_64
1015 /* generate CPU independent (unsigned) long long operations */
1016 static void gen_opl(int op
)
1018 int t
, a
, b
, op1
, c
, i
;
1020 unsigned short reg_iret
= REG_IRET
;
1021 unsigned short reg_lret
= REG_LRET
;
1027 func
= TOK___divdi3
;
1030 func
= TOK___udivdi3
;
1033 func
= TOK___moddi3
;
1036 func
= TOK___umoddi3
;
1043 /* call generic long long function */
1044 vpush_global_sym(&func_old_type
, func
);
1049 vtop
->r2
= reg_lret
;
1062 /* stack: L1 H1 L2 H2 */
1067 vtop
[-2] = vtop
[-3];
1070 /* stack: H1 H2 L1 L2 */
1076 /* stack: H1 H2 L1 L2 ML MH */
1079 /* stack: ML MH H1 H2 L1 L2 */
1083 /* stack: ML MH H1 L2 H2 L1 */
1088 /* stack: ML MH M1 M2 */
1091 } else if (op
== '+' || op
== '-') {
1092 /* XXX: add non carry method too (for MIPS or alpha) */
1098 /* stack: H1 H2 (L1 op L2) */
1101 gen_op(op1
+ 1); /* TOK_xxxC2 */
1104 /* stack: H1 H2 (L1 op L2) */
1107 /* stack: (L1 op L2) H1 H2 */
1109 /* stack: (L1 op L2) (H1 op H2) */
1117 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1118 t
= vtop
[-1].type
.t
;
1122 /* stack: L H shift */
1124 /* constant: simpler */
1125 /* NOTE: all comments are for SHL. the other cases are
1126 done by swaping words */
1137 if (op
!= TOK_SAR
) {
1170 /* XXX: should provide a faster fallback on x86 ? */
1173 func
= TOK___ashrdi3
;
1176 func
= TOK___lshrdi3
;
1179 func
= TOK___ashldi3
;
1185 /* compare operations */
1191 /* stack: L1 H1 L2 H2 */
1193 vtop
[-1] = vtop
[-2];
1195 /* stack: L1 L2 H1 H2 */
1198 /* when values are equal, we need to compare low words. since
1199 the jump is inverted, we invert the test too. */
1202 else if (op1
== TOK_GT
)
1204 else if (op1
== TOK_ULT
)
1206 else if (op1
== TOK_UGT
)
1211 if (op1
!= TOK_NE
) {
1215 /* generate non equal test */
1216 /* XXX: NOT PORTABLE yet */
1220 #if defined(TCC_TARGET_I386)
1221 b
= psym(0x850f, 0);
1222 #elif defined(TCC_TARGET_ARM)
1224 o(0x1A000000 | encbranch(ind
, 0, 1));
1225 #elif defined(TCC_TARGET_C67)
1226 error("not implemented");
1228 #error not supported
1232 /* compare low. Always unsigned */
1236 else if (op1
== TOK_LE
)
1238 else if (op1
== TOK_GT
)
1240 else if (op1
== TOK_GE
)
1251 /* handle integer constant optimizations and various machine
1253 static void gen_opic(int op
)
1255 int c1
, c2
, t1
, t2
, n
;
1258 typedef unsigned long long U
;
1262 t1
= v1
->type
.t
& VT_BTYPE
;
1263 t2
= v2
->type
.t
& VT_BTYPE
;
1267 else if (v1
->type
.t
& VT_UNSIGNED
)
1274 else if (v2
->type
.t
& VT_UNSIGNED
)
1279 /* currently, we cannot do computations with forward symbols */
1280 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1281 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
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;
1289 case '*': l1
*= l2
; break;
1296 /* if division by zero, generate explicit division */
1299 error("division by zero in constant");
1303 default: l1
/= l2
; break;
1304 case '%': l1
%= l2
; break;
1305 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1306 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1309 case TOK_SHL
: l1
<<= l2
; break;
1310 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1311 case TOK_SAR
: l1
>>= l2
; break;
1313 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1314 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1315 case TOK_EQ
: l1
= l1
== l2
; break;
1316 case TOK_NE
: l1
= l1
!= l2
; break;
1317 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1318 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1319 case TOK_LT
: l1
= l1
< l2
; break;
1320 case TOK_GE
: l1
= l1
>= l2
; break;
1321 case TOK_LE
: l1
= l1
<= l2
; break;
1322 case TOK_GT
: l1
= l1
> l2
; break;
1324 case TOK_LAND
: l1
= l1
&& l2
; break;
1325 case TOK_LOR
: l1
= l1
|| l2
; break;
1332 /* if commutative ops, put c2 as constant */
1333 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1334 op
== '|' || op
== '*')) {
1336 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1337 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1339 /* Filter out NOP operations like x*1, x-0, x&-1... */
1340 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1343 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1344 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1350 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1351 /* try to use shifts instead of muls or divs */
1352 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1361 else if (op
== TOK_PDIV
)
1367 } else if (c2
&& (op
== '+' || op
== '-') &&
1368 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1369 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1370 /* symbol + constant case */
1377 if (!nocode_wanted
) {
1378 /* call low level op generator */
1379 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1390 /* generate a floating point operation with constant propagation */
1391 static void gen_opif(int op
)
1399 /* currently, we cannot do computations with forward symbols */
1400 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1401 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1403 if (v1
->type
.t
== VT_FLOAT
) {
1406 } else if (v1
->type
.t
== VT_DOUBLE
) {
1414 /* NOTE: we only do constant propagation if finite number (not
1415 NaN or infinity) (ANSI spec) */
1416 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1420 case '+': f1
+= f2
; break;
1421 case '-': f1
-= f2
; break;
1422 case '*': f1
*= f2
; break;
1426 error("division by zero in constant");
1431 /* XXX: also handles tests ? */
1435 /* XXX: overflow test ? */
1436 if (v1
->type
.t
== VT_FLOAT
) {
1438 } else if (v1
->type
.t
== VT_DOUBLE
) {
1446 if (!nocode_wanted
) {
1454 static int pointed_size(CType
*type
)
1457 return type_size(pointed_type(type
), &align
);
1460 static inline int is_null_pointer(SValue
*p
)
1462 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1464 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1465 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1468 static inline int is_integer_btype(int bt
)
1470 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1471 bt
== VT_INT
|| bt
== VT_LLONG
);
1474 /* check types for comparison or substraction of pointers */
1475 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1477 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1480 /* null pointers are accepted for all comparisons as gcc */
1481 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1485 bt1
= type1
->t
& VT_BTYPE
;
1486 bt2
= type2
->t
& VT_BTYPE
;
1487 /* accept comparison between pointer and integer with a warning */
1488 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1489 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1490 warning("comparison between pointer and integer");
1494 /* both must be pointers or implicit function pointers */
1495 if (bt1
== VT_PTR
) {
1496 type1
= pointed_type(type1
);
1497 } else if (bt1
!= VT_FUNC
)
1498 goto invalid_operands
;
1500 if (bt2
== VT_PTR
) {
1501 type2
= pointed_type(type2
);
1502 } else if (bt2
!= VT_FUNC
) {
1504 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1506 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1507 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1511 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1512 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1513 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1514 /* gcc-like error if '-' is used */
1516 goto invalid_operands
;
1518 warning("comparison of distinct pointer types lacks a cast");
1522 /* generic gen_op: handles types problems */
1523 ST_FUNC
void gen_op(int op
)
1525 int u
, t1
, t2
, bt1
, bt2
, t
;
1528 t1
= vtop
[-1].type
.t
;
1529 t2
= vtop
[0].type
.t
;
1530 bt1
= t1
& VT_BTYPE
;
1531 bt2
= t2
& VT_BTYPE
;
1533 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1534 /* at least one operand is a pointer */
1535 /* relationnal op: must be both pointers */
1536 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1537 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1538 /* pointers are handled are unsigned */
1539 #ifdef TCC_TARGET_X86_64
1540 t
= VT_LLONG
| VT_UNSIGNED
;
1542 t
= VT_INT
| VT_UNSIGNED
;
1546 /* if both pointers, then it must be the '-' op */
1547 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1549 error("cannot use pointers here");
1550 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1551 /* XXX: check that types are compatible */
1552 u
= pointed_size(&vtop
[-1].type
);
1554 /* set to integer type */
1555 #ifdef TCC_TARGET_X86_64
1556 vtop
->type
.t
= VT_LLONG
;
1558 vtop
->type
.t
= VT_INT
;
1563 /* exactly one pointer : must be '+' or '-'. */
1564 if (op
!= '-' && op
!= '+')
1565 error("cannot use pointers here");
1566 /* Put pointer as first operand */
1567 if (bt2
== VT_PTR
) {
1571 type1
= vtop
[-1].type
;
1572 type1
.t
&= ~VT_ARRAY
;
1573 u
= pointed_size(&vtop
[-1].type
);
1575 error("unknown array element size");
1576 #ifdef TCC_TARGET_X86_64
1579 /* XXX: cast to int ? (long long case) */
1583 #ifdef CONFIG_TCC_BCHECK
1584 /* if evaluating constant expression, no code should be
1585 generated, so no bound check */
1586 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1587 /* if bounded pointers, we generate a special code to
1594 gen_bounded_ptr_add();
1600 /* put again type if gen_opic() swaped operands */
1603 } else if (is_float(bt1
) || is_float(bt2
)) {
1604 /* compute bigger type and do implicit casts */
1605 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1607 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1612 /* floats can only be used for a few operations */
1613 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1614 (op
< TOK_ULT
|| op
> TOK_GT
))
1615 error("invalid operands for binary operation");
1617 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1618 /* cast to biggest op */
1620 /* convert to unsigned if it does not fit in a long long */
1621 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1622 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1626 /* integer operations */
1628 /* convert to unsigned if it does not fit in an integer */
1629 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1630 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1633 /* XXX: currently, some unsigned operations are explicit, so
1634 we modify them here */
1635 if (t
& VT_UNSIGNED
) {
1642 else if (op
== TOK_LT
)
1644 else if (op
== TOK_GT
)
1646 else if (op
== TOK_LE
)
1648 else if (op
== TOK_GE
)
1655 /* special case for shifts and long long: we keep the shift as
1657 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1664 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1665 /* relationnal op: the result is an int */
1666 vtop
->type
.t
= VT_INT
;
1673 #ifndef TCC_TARGET_ARM
1674 /* generic itof for unsigned long long case */
1675 static void gen_cvt_itof1(int t
)
1677 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1678 (VT_LLONG
| VT_UNSIGNED
)) {
1681 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1682 #if LDOUBLE_SIZE != 8
1683 else if (t
== VT_LDOUBLE
)
1684 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1687 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1691 vtop
->r
= reg_fret(t
);
1698 /* generic ftoi for unsigned long long case */
1699 static void gen_cvt_ftoi1(int t
)
1703 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1704 /* not handled natively */
1705 st
= vtop
->type
.t
& VT_BTYPE
;
1707 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1708 #if LDOUBLE_SIZE != 8
1709 else if (st
== VT_LDOUBLE
)
1710 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1713 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1718 vtop
->r2
= REG_LRET
;
1724 /* force char or short cast */
1725 static void force_charshort_cast(int t
)
1729 /* XXX: add optimization if lvalue : just change type and offset */
1734 if (t
& VT_UNSIGNED
) {
1735 vpushi((1 << bits
) - 1);
1741 /* result must be signed or the SAR is converted to an SHL
1742 This was not the case when "t" was a signed short
1743 and the last value on the stack was an unsigned int */
1744 vtop
->type
.t
&= ~VT_UNSIGNED
;
1750 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1751 static void gen_cast(CType
*type
)
1753 int sbt
, dbt
, sf
, df
, c
, p
;
1755 /* special delayed cast for char/short */
1756 /* XXX: in some cases (multiple cascaded casts), it may still
1758 if (vtop
->r
& VT_MUSTCAST
) {
1759 vtop
->r
&= ~VT_MUSTCAST
;
1760 force_charshort_cast(vtop
->type
.t
);
1763 /* bitfields first get cast to ints */
1764 if (vtop
->type
.t
& VT_BITFIELD
) {
1768 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1769 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1774 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1775 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1777 /* constant case: we can do it now */
1778 /* XXX: in ISOC, cannot do it if error in convert */
1779 if (sbt
== VT_FLOAT
)
1780 vtop
->c
.ld
= vtop
->c
.f
;
1781 else if (sbt
== VT_DOUBLE
)
1782 vtop
->c
.ld
= vtop
->c
.d
;
1785 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1786 if (sbt
& VT_UNSIGNED
)
1787 vtop
->c
.ld
= vtop
->c
.ull
;
1789 vtop
->c
.ld
= vtop
->c
.ll
;
1791 if (sbt
& VT_UNSIGNED
)
1792 vtop
->c
.ld
= vtop
->c
.ui
;
1794 vtop
->c
.ld
= vtop
->c
.i
;
1797 if (dbt
== VT_FLOAT
)
1798 vtop
->c
.f
= (float)vtop
->c
.ld
;
1799 else if (dbt
== VT_DOUBLE
)
1800 vtop
->c
.d
= (double)vtop
->c
.ld
;
1801 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1802 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1803 } else if (sf
&& dbt
== VT_BOOL
) {
1804 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1807 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1808 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1809 vtop
->c
.ll
= vtop
->c
.ull
;
1810 else if (sbt
& VT_UNSIGNED
)
1811 vtop
->c
.ll
= vtop
->c
.ui
;
1812 #ifdef TCC_TARGET_X86_64
1813 else if (sbt
== VT_PTR
)
1816 else if (sbt
!= VT_LLONG
)
1817 vtop
->c
.ll
= vtop
->c
.i
;
1819 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1820 vtop
->c
.ull
= vtop
->c
.ll
;
1821 else if (dbt
== VT_BOOL
)
1822 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1823 else if (dbt
!= VT_LLONG
) {
1825 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1827 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1830 if(dbt
& VT_UNSIGNED
)
1831 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1833 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1836 } else if (p
&& dbt
== VT_BOOL
) {
1839 } else if (!nocode_wanted
) {
1840 /* non constant case: generate code */
1842 /* convert from fp to fp */
1845 /* convert int to fp */
1848 /* convert fp to int */
1849 if (dbt
== VT_BOOL
) {
1853 /* we handle char/short/etc... with generic code */
1854 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1855 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1859 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1860 /* additional cast for char/short... */
1865 #ifndef TCC_TARGET_X86_64
1866 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1867 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1868 /* scalar to long long */
1869 /* machine independent conversion */
1871 /* generate high word */
1872 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1876 if (sbt
== VT_PTR
) {
1877 /* cast from pointer to int before we apply
1878 shift operation, which pointers don't support*/
1879 gen_cast(&int_type
);
1885 /* patch second register */
1886 vtop
[-1].r2
= vtop
->r
;
1890 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1891 (dbt
& VT_BTYPE
) == VT_PTR
||
1892 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1893 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1894 (sbt
& VT_BTYPE
) != VT_PTR
&&
1895 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1896 /* need to convert from 32bit to 64bit */
1898 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1899 /* x86_64 specific: movslq */
1901 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1905 } else if (dbt
== VT_BOOL
) {
1906 /* scalar to bool */
1909 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1910 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1911 if (sbt
== VT_PTR
) {
1912 vtop
->type
.t
= VT_INT
;
1913 warning("nonportable conversion from pointer to char/short");
1915 force_charshort_cast(dbt
);
1916 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1918 if (sbt
== VT_LLONG
) {
1919 /* from long long: just take low order word */
1923 /* if lvalue and single word type, nothing to do because
1924 the lvalue already contains the real type size (see
1925 VT_LVAL_xxx constants) */
1928 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1929 /* if we are casting between pointer types,
1930 we must update the VT_LVAL_xxx size */
1931 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1932 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1937 /* return type size. Put alignment at 'a' */
1938 ST_FUNC
int type_size(CType
*type
, int *a
)
1943 bt
= type
->t
& VT_BTYPE
;
1944 if (bt
== VT_STRUCT
) {
1947 *a
= s
->r
& 0xffffff;
1949 } else if (bt
== VT_PTR
) {
1950 if (type
->t
& VT_ARRAY
) {
1954 ts
= type_size(&s
->type
, a
);
1956 if (ts
< 0 && s
->c
< 0)
1964 } else if (bt
== VT_LDOUBLE
) {
1966 return LDOUBLE_SIZE
;
1967 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1968 #ifdef TCC_TARGET_I386
1969 #ifdef TCC_TARGET_PE
1974 #elif defined(TCC_TARGET_ARM)
1984 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1987 } else if (bt
== VT_SHORT
) {
1991 /* char, void, function, _Bool */
1997 /* return the pointed type of t */
1998 static inline CType
*pointed_type(CType
*type
)
2000 return &type
->ref
->type
;
2003 /* modify type so that its it is a pointer to type. */
2004 ST_FUNC
void mk_pointer(CType
*type
)
2007 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2008 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2012 /* compare function types. OLD functions match any new functions */
2013 static int is_compatible_func(CType
*type1
, CType
*type2
)
2019 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2021 /* check func_call */
2022 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2024 /* XXX: not complete */
2025 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2029 while (s1
!= NULL
) {
2032 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2042 /* return true if type1 and type2 are the same. If unqualified is
2043 true, qualifiers on the types are ignored.
2045 - enums are not checked as gcc __builtin_types_compatible_p ()
2047 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2051 t1
= type1
->t
& VT_TYPE
;
2052 t2
= type2
->t
& VT_TYPE
;
2054 /* strip qualifiers before comparing */
2055 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2056 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2058 /* XXX: bitfields ? */
2061 /* test more complicated cases */
2062 bt1
= t1
& VT_BTYPE
;
2063 if (bt1
== VT_PTR
) {
2064 type1
= pointed_type(type1
);
2065 type2
= pointed_type(type2
);
2066 return is_compatible_types(type1
, type2
);
2067 } else if (bt1
== VT_STRUCT
) {
2068 return (type1
->ref
== type2
->ref
);
2069 } else if (bt1
== VT_FUNC
) {
2070 return is_compatible_func(type1
, type2
);
2076 /* return true if type1 and type2 are exactly the same (including
2079 static int is_compatible_types(CType
*type1
, CType
*type2
)
2081 return compare_types(type1
,type2
,0);
2084 /* return true if type1 and type2 are the same (ignoring qualifiers).
2086 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2088 return compare_types(type1
,type2
,1);
2091 /* print a type. If 'varstr' is not NULL, then the variable is also
2092 printed in the type */
2094 /* XXX: add array and function pointers */
2095 static void type_to_str(char *buf
, int buf_size
,
2096 CType
*type
, const char *varstr
)
2103 t
= type
->t
& VT_TYPE
;
2106 if (t
& VT_CONSTANT
)
2107 pstrcat(buf
, buf_size
, "const ");
2108 if (t
& VT_VOLATILE
)
2109 pstrcat(buf
, buf_size
, "volatile ");
2110 if (t
& VT_UNSIGNED
)
2111 pstrcat(buf
, buf_size
, "unsigned ");
2141 tstr
= "long double";
2143 pstrcat(buf
, buf_size
, tstr
);
2147 if (bt
== VT_STRUCT
)
2151 pstrcat(buf
, buf_size
, tstr
);
2152 v
= type
->ref
->v
& ~SYM_STRUCT
;
2153 if (v
>= SYM_FIRST_ANOM
)
2154 pstrcat(buf
, buf_size
, "<anonymous>");
2156 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2160 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2161 pstrcat(buf
, buf_size
, "(");
2163 while (sa
!= NULL
) {
2164 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2165 pstrcat(buf
, buf_size
, buf1
);
2168 pstrcat(buf
, buf_size
, ", ");
2170 pstrcat(buf
, buf_size
, ")");
2174 pstrcpy(buf1
, sizeof(buf1
), "*");
2176 pstrcat(buf1
, sizeof(buf1
), varstr
);
2177 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2181 pstrcat(buf
, buf_size
, " ");
2182 pstrcat(buf
, buf_size
, varstr
);
2187 /* verify type compatibility to store vtop in 'dt' type, and generate
2189 static void gen_assign_cast(CType
*dt
)
2191 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2192 char buf1
[256], buf2
[256];
2195 st
= &vtop
->type
; /* source type */
2196 dbt
= dt
->t
& VT_BTYPE
;
2197 sbt
= st
->t
& VT_BTYPE
;
2198 if (dt
->t
& VT_CONSTANT
)
2199 warning("assignment of read-only location");
2202 /* special cases for pointers */
2203 /* '0' can also be a pointer */
2204 if (is_null_pointer(vtop
))
2206 /* accept implicit pointer to integer cast with warning */
2207 if (is_integer_btype(sbt
)) {
2208 warning("assignment makes pointer from integer without a cast");
2211 type1
= pointed_type(dt
);
2212 /* a function is implicitely a function pointer */
2213 if (sbt
== VT_FUNC
) {
2214 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2215 !is_compatible_types(pointed_type(dt
), st
))
2216 warning("assignment from incompatible pointer type");
2221 type2
= pointed_type(st
);
2222 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2223 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2224 /* void * can match anything */
2226 /* exact type match, except for unsigned */
2229 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2230 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2231 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2232 warning("assignment from incompatible pointer type");
2234 /* check const and volatile */
2235 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2236 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2237 warning("assignment discards qualifiers from pointer target type");
2243 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2244 warning("assignment makes integer from pointer without a cast");
2246 /* XXX: more tests */
2251 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2252 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2253 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2255 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2256 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2257 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2265 /* store vtop in lvalue pushed on stack */
2266 ST_FUNC
void vstore(void)
2268 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2270 ft
= vtop
[-1].type
.t
;
2271 sbt
= vtop
->type
.t
& VT_BTYPE
;
2272 dbt
= ft
& VT_BTYPE
;
2273 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2274 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2275 /* optimize char/short casts */
2276 delayed_cast
= VT_MUSTCAST
;
2277 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2278 /* XXX: factorize */
2279 if (ft
& VT_CONSTANT
)
2280 warning("assignment of read-only location");
2283 if (!(ft
& VT_BITFIELD
))
2284 gen_assign_cast(&vtop
[-1].type
);
2287 if (sbt
== VT_STRUCT
) {
2288 /* if structure, only generate pointer */
2289 /* structure assignment : generate memcpy */
2290 /* XXX: optimize if small size */
2291 if (!nocode_wanted
) {
2292 size
= type_size(&vtop
->type
, &align
);
2296 vtop
->type
.t
= VT_PTR
;
2299 /* address of memcpy() */
2302 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2303 else if(!(align
& 3))
2304 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2307 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2312 vtop
->type
.t
= VT_PTR
;
2321 /* leave source on stack */
2322 } else if (ft
& VT_BITFIELD
) {
2323 /* bitfield store handling */
2324 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2325 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2326 /* remove bit field info to avoid loops */
2327 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2329 /* duplicate source into other register */
2334 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2335 gen_cast(&vtop
[-1].type
);
2336 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2339 /* duplicate destination */
2341 vtop
[-1] = vtop
[-2];
2343 /* mask and shift source */
2344 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2345 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2346 vpushll((1ULL << bit_size
) - 1ULL);
2348 vpushi((1 << bit_size
) - 1);
2354 /* load destination, mask and or with source */
2356 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2357 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2359 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2366 /* pop off shifted source from "duplicate source..." above */
2370 #ifdef CONFIG_TCC_BCHECK
2371 /* bound check case */
2372 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2378 if (!nocode_wanted
) {
2382 #ifdef TCC_TARGET_X86_64
2383 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2388 r
= gv(rc
); /* generate value */
2389 /* if lvalue was saved on stack, must read it */
2390 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2392 t
= get_reg(RC_INT
);
2393 #ifdef TCC_TARGET_X86_64
2398 sv
.r
= VT_LOCAL
| VT_LVAL
;
2399 sv
.c
.ul
= vtop
[-1].c
.ul
;
2401 vtop
[-1].r
= t
| VT_LVAL
;
2404 #ifndef TCC_TARGET_X86_64
2405 /* two word case handling : store second register at word + 4 */
2406 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2408 /* convert to int to increment easily */
2409 vtop
->type
.t
= VT_INT
;
2415 /* XXX: it works because r2 is spilled last ! */
2416 store(vtop
->r2
, vtop
- 1);
2421 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2422 vtop
->r
|= delayed_cast
;
2426 /* post defines POST/PRE add. c is the token ++ or -- */
2427 ST_FUNC
void inc(int post
, int c
)
2430 vdup(); /* save lvalue */
2432 gv_dup(); /* duplicate value */
2437 vpushi(c
- TOK_MID
);
2439 vstore(); /* store value */
2441 vpop(); /* if post op, return saved value */
2444 /* Parse GNUC __attribute__ extension. Currently, the following
2445 extensions are recognized:
2446 - aligned(n) : set data/function alignment.
2447 - packed : force data alignment to 1
2448 - section(x) : generate data/code in this section.
2449 - unused : currently ignored, but may be used someday.
2450 - regparm(n) : pass function parameters in registers (i386 only)
2452 static void parse_attribute(AttributeDef
*ad
)
2456 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2460 while (tok
!= ')') {
2461 if (tok
< TOK_IDENT
)
2462 expect("attribute name");
2470 expect("section name");
2471 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2480 if (n
<= 0 || (n
& (n
- 1)) != 0)
2481 error("alignment must be a positive power of two");
2498 /* currently, no need to handle it because tcc does not
2499 track unused objects */
2503 /* currently, no need to handle it because tcc does not
2504 track unused objects */
2509 ad
->func_call
= FUNC_CDECL
;
2514 ad
->func_call
= FUNC_STDCALL
;
2516 #ifdef TCC_TARGET_I386
2526 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2532 ad
->func_call
= FUNC_FASTCALLW
;
2539 ad
->mode
= VT_LLONG
+ 1;
2542 ad
->mode
= VT_SHORT
+ 1;
2545 ad
->mode
= VT_INT
+ 1;
2548 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2555 ad
->func_export
= 1;
2558 ad
->func_import
= 1;
2561 if (tcc_state
->warn_unsupported
)
2562 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2563 /* skip parameters */
2565 int parenthesis
= 0;
2569 else if (tok
== ')')
2572 } while (parenthesis
&& tok
!= -1);
2585 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2586 static void struct_decl(CType
*type
, int u
)
2588 int a
, v
, size
, align
, maxalign
, c
, offset
;
2589 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
, resize
;
2590 Sym
*s
, *ss
, *ass
, **ps
;
2594 a
= tok
; /* save decl type */
2599 /* struct already defined ? return it */
2601 expect("struct/union/enum name");
2605 error("invalid type");
2612 /* we put an undefined size for struct/union */
2613 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2614 s
->r
= 0; /* default alignment is zero as gcc */
2615 /* put struct/union/enum name in type */
2623 error("struct/union/enum already defined");
2624 /* cannot be empty */
2626 /* non empty enums are not allowed */
2627 if (a
== TOK_ENUM
) {
2631 expect("identifier");
2637 /* enum symbols have static storage */
2638 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2639 ss
->type
.t
|= VT_STATIC
;
2644 /* NOTE: we accept a trailing comma */
2656 while (tok
!= '}') {
2657 parse_btype(&btype
, &ad
);
2663 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2664 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2665 expect("identifier");
2666 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2667 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2668 error("invalid type for '%s'",
2669 get_tok_str(v
, NULL
));
2673 bit_size
= expr_const();
2674 /* XXX: handle v = 0 case for messages */
2676 error("negative width in bit-field '%s'",
2677 get_tok_str(v
, NULL
));
2678 if (v
&& bit_size
== 0)
2679 error("zero width for bit-field '%s'",
2680 get_tok_str(v
, NULL
));
2682 size
= type_size(&type1
, &align
);
2684 if (align
< ad
.aligned
)
2686 } else if (ad
.packed
) {
2688 } else if (*tcc_state
->pack_stack_ptr
) {
2689 if (align
> *tcc_state
->pack_stack_ptr
)
2690 align
= *tcc_state
->pack_stack_ptr
;
2693 if (bit_size
>= 0) {
2694 bt
= type1
.t
& VT_BTYPE
;
2701 error("bitfields must have scalar type");
2703 if (bit_size
> bsize
) {
2704 error("width of '%s' exceeds its type",
2705 get_tok_str(v
, NULL
));
2706 } else if (bit_size
== bsize
) {
2707 /* no need for bit fields */
2709 } else if (bit_size
== 0) {
2710 /* XXX: what to do if only padding in a
2712 /* zero size: means to pad */
2715 /* we do not have enough room ?
2716 did the type change?
2718 if ((bit_pos
+ bit_size
) > bsize
||
2719 bt
!= prevbt
|| a
== TOK_UNION
)
2722 /* XXX: handle LSB first */
2723 type1
.t
|= VT_BITFIELD
|
2724 (bit_pos
<< VT_STRUCT_SHIFT
) |
2725 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2726 bit_pos
+= bit_size
;
2732 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2733 /* add new memory data only if starting
2735 if (lbit_pos
== 0) {
2736 if (a
== TOK_STRUCT
) {
2737 c
= (c
+ align
- 1) & -align
;
2748 if (align
> maxalign
)
2752 printf("add field %s offset=%d",
2753 get_tok_str(v
, NULL
), offset
);
2754 if (type1
.t
& VT_BITFIELD
) {
2755 printf(" pos=%d size=%d",
2756 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2757 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2762 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2764 while ((ass
= ass
->next
) != NULL
) {
2765 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2770 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2774 if (tok
== ';' || tok
== TOK_EOF
)
2781 /* store size and alignment */
2782 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2783 s
->r
= maxalign
| (resize
? (1 << 31) : 0);
2788 /* return 0 if no type declaration. otherwise, return the basic type
2791 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2793 int t
, u
, type_found
, typespec_found
, typedef_found
;
2797 memset(ad
, 0, sizeof(AttributeDef
));
2805 /* currently, we really ignore extension */
2815 if ((t
& VT_BTYPE
) != 0)
2816 error("too many basic types");
2832 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2833 #ifndef TCC_TARGET_PE
2834 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2836 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2837 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2851 if ((t
& VT_BTYPE
) == VT_LONG
) {
2852 #ifdef TCC_TARGET_PE
2853 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2855 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2863 struct_decl(&type1
, VT_ENUM
);
2866 type
->ref
= type1
.ref
;
2870 struct_decl(&type1
, VT_STRUCT
);
2873 /* type modifiers */
2926 /* GNUC attribute */
2927 case TOK_ATTRIBUTE1
:
2928 case TOK_ATTRIBUTE2
:
2929 parse_attribute(ad
);
2935 t
= (t
& ~VT_BTYPE
) | u
;
2943 parse_expr_type(&type1
);
2946 if (typespec_found
|| typedef_found
)
2949 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2952 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2953 type
->ref
= s
->type
.ref
;
2955 /* get attributes from typedef */
2956 if (0 == ad
->aligned
)
2957 ad
->aligned
= FUNC_ALIGN(s
->r
);
2958 if (0 == ad
->func_call
)
2959 ad
->func_call
= FUNC_CALL(s
->r
);
2960 ad
->packed
|= FUNC_PACKED(s
->r
);
2969 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2970 error("signed and unsigned modifier");
2971 if (tcc_state
->char_is_unsigned
) {
2972 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2977 /* long is never used as type */
2978 if ((t
& VT_BTYPE
) == VT_LONG
)
2979 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2980 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2982 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2988 /* convert a function parameter type (array to pointer and function to
2989 function pointer) */
2990 static inline void convert_parameter_type(CType
*pt
)
2992 /* remove const and volatile qualifiers (XXX: const could be used
2993 to indicate a const function parameter */
2994 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2995 /* array must be transformed to pointer according to ANSI C */
2997 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3002 ST_FUNC
void parse_asm_str(CString
*astr
)
3005 /* read the string */
3007 expect("string constant");
3009 while (tok
== TOK_STR
) {
3010 /* XXX: add \0 handling too ? */
3011 cstr_cat(astr
, tokc
.cstr
->data
);
3014 cstr_ccat(astr
, '\0');
3017 /* Parse an asm label and return the label
3018 * Don't forget to free the CString in the caller! */
3019 static void asm_label_instr(CString
*astr
)
3022 parse_asm_str(astr
);
3025 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3029 static void post_type(CType
*type
, AttributeDef
*ad
)
3031 int n
, l
, t1
, arg_size
, align
;
3032 Sym
**plast
, *s
, *first
;
3037 /* function declaration */
3045 /* read param name and compute offset */
3046 if (l
!= FUNC_OLD
) {
3047 if (!parse_btype(&pt
, &ad1
)) {
3049 error("invalid type");
3056 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3058 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3059 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3060 error("parameter declared as void");
3061 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3066 expect("identifier");
3070 convert_parameter_type(&pt
);
3071 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3077 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3084 /* if no parameters, then old type prototype */
3088 t1
= type
->t
& VT_STORAGE
;
3089 /* NOTE: const is ignored in returned type as it has a special
3090 meaning in gcc / C++ */
3091 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3092 post_type(type
, ad
);
3093 /* we push a anonymous symbol which will contain the function prototype */
3094 ad
->func_args
= arg_size
;
3095 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3097 type
->t
= t1
| VT_FUNC
;
3099 } else if (tok
== '[') {
3100 /* array definition */
3102 if (tok
== TOK_RESTRICT1
)
3108 error("invalid array size");
3111 /* parse next post type */
3112 t1
= type
->t
& VT_STORAGE
;
3113 type
->t
&= ~VT_STORAGE
;
3114 post_type(type
, ad
);
3116 /* we push a anonymous symbol which will contain the array
3118 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3120 ARRAY_RESIZE(s
->r
) = 1;
3121 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3126 /* Parse a type declaration (except basic type), and return the type
3127 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3128 expected. 'type' should contain the basic type. 'ad' is the
3129 attribute definition of the basic type. It can be modified by
3132 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3135 CType type1
, *type2
;
3138 while (tok
== '*') {
3146 qualifiers
|= VT_CONSTANT
;
3151 qualifiers
|= VT_VOLATILE
;
3159 type
->t
|= qualifiers
;
3162 /* XXX: clarify attribute handling */
3163 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3164 parse_attribute(ad
);
3166 /* recursive type */
3167 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3168 type1
.t
= 0; /* XXX: same as int */
3171 /* XXX: this is not correct to modify 'ad' at this point, but
3172 the syntax is not clear */
3173 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3174 parse_attribute(ad
);
3175 type_decl(&type1
, ad
, v
, td
);
3178 /* type identifier */
3179 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3183 if (!(td
& TYPE_ABSTRACT
))
3184 expect("identifier");
3188 post_type(type
, ad
);
3189 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3190 parse_attribute(ad
);
3197 /* append type at the end of type1 */
3210 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3211 ST_FUNC
int lvalue_type(int t
)
3216 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3218 else if (bt
== VT_SHORT
)
3222 if (t
& VT_UNSIGNED
)
3223 r
|= VT_LVAL_UNSIGNED
;
3227 /* indirection with full error checking and bound check */
3228 ST_FUNC
void indir(void)
3230 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3231 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3235 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3237 vtop
->type
= *pointed_type(&vtop
->type
);
3238 /* Arrays and functions are never lvalues */
3239 if (!(vtop
->type
.t
& VT_ARRAY
)
3240 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3241 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3242 /* if bound checking, the referenced pointer must be checked */
3243 #ifdef CONFIG_TCC_BCHECK
3244 if (tcc_state
->do_bounds_check
)
3245 vtop
->r
|= VT_MUSTBOUND
;
3250 /* pass a parameter to a function and do type checking and casting */
3251 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3256 func_type
= func
->c
;
3257 if (func_type
== FUNC_OLD
||
3258 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3259 /* default casting : only need to convert float to double */
3260 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3264 } else if (arg
== NULL
) {
3265 error("too many arguments to function");
3268 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3269 gen_assign_cast(&type
);
3273 /* parse an expression of the form '(type)' or '(expr)' and return its
3275 static void parse_expr_type(CType
*type
)
3281 if (parse_btype(type
, &ad
)) {
3282 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3289 static void parse_type(CType
*type
)
3294 if (!parse_btype(type
, &ad
)) {
3297 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3300 static void vpush_tokc(int t
)
3305 vsetc(&type
, VT_CONST
, &tokc
);
3308 ST_FUNC
void unary(void)
3310 int n
, t
, align
, size
, r
, sizeof_caller
;
3314 static int in_sizeof
= 0;
3316 sizeof_caller
= in_sizeof
;
3318 /* XXX: GCC 2.95.3 does not generate a table although it should be
3332 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3336 vpush_tokc(VT_LLONG
);
3340 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3344 vpush_tokc(VT_FLOAT
);
3348 vpush_tokc(VT_DOUBLE
);
3352 vpush_tokc(VT_LDOUBLE
);
3355 case TOK___FUNCTION__
:
3357 goto tok_identifier
;
3363 /* special function name identifier */
3364 len
= strlen(funcname
) + 1;
3365 /* generate char[len] type */
3370 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3371 ptr
= section_ptr_add(data_section
, len
);
3372 memcpy(ptr
, funcname
, len
);
3377 #ifdef TCC_TARGET_PE
3378 t
= VT_SHORT
| VT_UNSIGNED
;
3384 /* string parsing */
3387 if (tcc_state
->warn_write_strings
)
3392 memset(&ad
, 0, sizeof(AttributeDef
));
3393 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3398 if (parse_btype(&type
, &ad
)) {
3399 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3401 /* check ISOC99 compound literal */
3403 /* data is allocated locally by default */
3408 /* all except arrays are lvalues */
3409 if (!(type
.t
& VT_ARRAY
))
3410 r
|= lvalue_type(type
.t
);
3411 memset(&ad
, 0, sizeof(AttributeDef
));
3412 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3414 if (sizeof_caller
) {
3421 } else if (tok
== '{') {
3422 /* save all registers */
3424 /* statement expression : we do not accept break/continue
3425 inside as GCC does */
3426 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3441 /* functions names must be treated as function pointers,
3442 except for unary '&' and sizeof. Since we consider that
3443 functions are not lvalues, we only have to handle it
3444 there and in function calls. */
3445 /* arrays can also be used although they are not lvalues */
3446 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3447 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3449 mk_pointer(&vtop
->type
);
3455 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3457 boolean
.t
= VT_BOOL
;
3459 vtop
->c
.i
= !vtop
->c
.i
;
3460 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3461 vtop
->c
.i
= vtop
->c
.i
^ 1;
3464 vseti(VT_JMP
, gtst(1, 0));
3475 /* in order to force cast, we add zero */
3477 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3478 error("pointer not accepted for unary plus");
3488 unary_type(&type
); // Perform a in_sizeof = 0;
3489 size
= type_size(&type
, &align
);
3490 if (t
== TOK_SIZEOF
) {
3492 error("sizeof applied to an incomplete type");
3497 vtop
->type
.t
|= VT_UNSIGNED
;
3500 case TOK_builtin_types_compatible_p
:
3509 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3510 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3511 vpushi(is_compatible_types(&type1
, &type2
));
3514 case TOK_builtin_constant_p
:
3516 int saved_nocode_wanted
, res
;
3519 saved_nocode_wanted
= nocode_wanted
;
3522 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3524 nocode_wanted
= saved_nocode_wanted
;
3529 case TOK_builtin_frame_address
:
3534 if (tok
!= TOK_CINT
) {
3535 error("__builtin_frame_address only takes integers");
3538 error("TCC only supports __builtin_frame_address(0)");
3544 vset(&type
, VT_LOCAL
, 0);
3547 #ifdef TCC_TARGET_X86_64
3548 case TOK_builtin_va_arg_types
:
3550 /* This definition must be synced with stdarg.h */
3551 enum __va_arg_type
{
3552 __va_gen_reg
, __va_float_reg
, __va_stack
3560 bt
= type
.t
& VT_BTYPE
;
3561 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3563 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3564 vpushi(__va_float_reg
);
3566 vpushi(__va_gen_reg
);
3586 goto tok_identifier
;
3588 /* allow to take the address of a label */
3589 if (tok
< TOK_UIDENT
)
3590 expect("label identifier");
3591 s
= label_find(tok
);
3593 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3595 if (s
->r
== LABEL_DECLARED
)
3596 s
->r
= LABEL_FORWARD
;
3599 s
->type
.t
= VT_VOID
;
3600 mk_pointer(&s
->type
);
3601 s
->type
.t
|= VT_STATIC
;
3603 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3608 // special qnan , snan and infinity values
3610 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3614 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3618 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3627 expect("identifier");
3631 error("'%s' undeclared", get_tok_str(t
, NULL
));
3632 /* for simple function calls, we tolerate undeclared
3633 external reference to int() function */
3634 if (tcc_state
->warn_implicit_function_declaration
)
3635 warning("implicit declaration of function '%s'",
3636 get_tok_str(t
, NULL
));
3637 s
= external_global_sym(t
, &func_old_type
, 0);
3639 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3640 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3641 /* if referencing an inline function, then we generate a
3642 symbol to it if not already done. It will have the
3643 effect to generate code for it at the end of the
3644 compilation unit. Inline function as always
3645 generated in the text section. */
3647 put_extern_sym(s
, text_section
, 0, 0);
3648 r
= VT_SYM
| VT_CONST
;
3652 vset(&s
->type
, r
, s
->c
);
3653 /* if forward reference, we must point to s */
3654 if (vtop
->r
& VT_SYM
) {
3661 /* post operations */
3663 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3666 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3669 if (tok
== TOK_ARROW
)
3671 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3675 /* expect pointer on structure */
3676 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3677 expect("struct or union");
3681 while ((s
= s
->next
) != NULL
) {
3686 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3687 /* add field offset to pointer */
3688 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3691 /* change type to field type, and set to lvalue */
3692 vtop
->type
= s
->type
;
3693 vtop
->type
.t
|= qualifiers
;
3694 /* an array is never an lvalue */
3695 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3696 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3697 #ifdef CONFIG_TCC_BCHECK
3698 /* if bound checking, the referenced pointer must be checked */
3699 if (tcc_state
->do_bounds_check
)
3700 vtop
->r
|= VT_MUSTBOUND
;
3704 } else if (tok
== '[') {
3710 } else if (tok
== '(') {
3716 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3717 /* pointer test (no array accepted) */
3718 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3719 vtop
->type
= *pointed_type(&vtop
->type
);
3720 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3724 expect("function pointer");
3727 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3729 /* get return type */
3732 sa
= s
->next
; /* first parameter */
3735 /* compute first implicit argument if a structure is returned */
3736 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3737 /* get some space for the returned structure */
3738 size
= type_size(&s
->type
, &align
);
3739 loc
= (loc
- size
) & -align
;
3741 ret
.r
= VT_LOCAL
| VT_LVAL
;
3742 /* pass it as 'int' to avoid structure arg passing
3744 vseti(VT_LOCAL
, loc
);
3749 /* return in register */
3750 if (is_float(ret
.type
.t
)) {
3751 ret
.r
= reg_fret(ret
.type
.t
);
3753 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3762 gfunc_param_typed(s
, sa
);
3772 error("too few arguments to function");
3774 if (!nocode_wanted
) {
3775 gfunc_call(nb_args
);
3777 vtop
-= (nb_args
+ 1);
3780 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3788 ST_FUNC
void expr_prod(void)
3793 while (tok
== '*' || tok
== '/' || tok
== '%') {
3801 ST_FUNC
void expr_sum(void)
3806 while (tok
== '+' || tok
== '-') {
3814 static void expr_shift(void)
3819 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3827 static void expr_cmp(void)
3832 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3833 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3841 static void expr_cmpeq(void)
3846 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3854 static void expr_and(void)
3857 while (tok
== '&') {
3864 static void expr_xor(void)
3867 while (tok
== '^') {
3874 static void expr_or(void)
3877 while (tok
== '|') {
3884 /* XXX: fix this mess */
3885 static void expr_land_const(void)
3888 while (tok
== TOK_LAND
) {
3895 /* XXX: fix this mess */
3896 static void expr_lor_const(void)
3899 while (tok
== TOK_LOR
) {
3906 /* only used if non constant */
3907 static void expr_land(void)
3912 if (tok
== TOK_LAND
) {
3917 if (tok
!= TOK_LAND
) {
3927 static void expr_lor(void)
3932 if (tok
== TOK_LOR
) {
3937 if (tok
!= TOK_LOR
) {
3947 /* XXX: better constant handling */
3948 static void expr_cond(void)
3950 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3952 CType type
, type1
, type2
;
3959 boolean
.t
= VT_BOOL
;
3965 if (tok
!= ':' || !gnu_ext
) {
3980 if (vtop
!= vstack
) {
3981 /* needed to avoid having different registers saved in
3983 if (is_float(vtop
->type
.t
)) {
3985 #ifdef TCC_TARGET_X86_64
3986 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3996 if (tok
== ':' && gnu_ext
) {
4004 sv
= *vtop
; /* save value to handle it later */
4005 vtop
--; /* no vpop so that FP stack is not flushed */
4013 bt1
= t1
& VT_BTYPE
;
4015 bt2
= t2
& VT_BTYPE
;
4016 /* cast operands to correct type according to ISOC rules */
4017 if (is_float(bt1
) || is_float(bt2
)) {
4018 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4019 type
.t
= VT_LDOUBLE
;
4020 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4025 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4026 /* cast to biggest op */
4028 /* convert to unsigned if it does not fit in a long long */
4029 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4030 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4031 type
.t
|= VT_UNSIGNED
;
4032 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4033 /* XXX: test pointer compatibility */
4035 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4036 /* XXX: test function pointer compatibility */
4038 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4039 /* XXX: test structure compatibility */
4041 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4042 /* NOTE: as an extension, we accept void on only one side */
4045 /* integer operations */
4047 /* convert to unsigned if it does not fit in an integer */
4048 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4049 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4050 type
.t
|= VT_UNSIGNED
;
4053 /* now we convert second operand */
4055 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4058 if (is_float(type
.t
)) {
4060 #ifdef TCC_TARGET_X86_64
4061 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4065 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4066 /* for long longs, we use fixed registers to avoid having
4067 to handle a complicated move */
4072 /* this is horrible, but we must also convert first
4076 /* put again first value and cast it */
4079 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4089 static void expr_eq(void)
4095 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4096 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4097 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4112 ST_FUNC
void gexpr(void)
4123 /* parse an expression and return its type without any side effect. */
4124 static void expr_type(CType
*type
)
4126 int saved_nocode_wanted
;
4128 saved_nocode_wanted
= nocode_wanted
;
4133 nocode_wanted
= saved_nocode_wanted
;
4136 /* parse a unary expression and return its type without any side
4138 static void unary_type(CType
*type
)
4150 /* parse a constant expression and return value in vtop. */
4151 static void expr_const1(void)
4160 /* parse an integer constant and return its value. */
4161 ST_FUNC
int expr_const(void)
4165 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4166 expect("constant expression");
4172 /* return the label token if current token is a label, otherwise
4174 static int is_label(void)
4178 /* fast test first */
4179 if (tok
< TOK_UIDENT
)
4181 /* no need to save tokc because tok is an identifier */
4188 unget_tok(last_tok
);
4193 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4194 int case_reg
, int is_expr
)
4199 /* generate line number info */
4200 if (tcc_state
->do_debug
&&
4201 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4202 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4204 last_line_num
= file
->line_num
;
4208 /* default return value is (void) */
4210 vtop
->type
.t
= VT_VOID
;
4213 if (tok
== TOK_IF
) {
4220 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4222 if (c
== TOK_ELSE
) {
4226 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4227 gsym(d
); /* patch else jmp */
4230 } else if (tok
== TOK_WHILE
) {
4238 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4242 } else if (tok
== '{') {
4246 /* record local declaration stack position */
4248 llabel
= local_label_stack
;
4249 /* handle local labels declarations */
4250 if (tok
== TOK_LABEL
) {
4253 if (tok
< TOK_UIDENT
)
4254 expect("label identifier");
4255 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4265 while (tok
!= '}') {
4270 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4273 /* pop locally defined labels */
4274 label_pop(&local_label_stack
, llabel
);
4275 /* pop locally defined symbols */
4277 /* XXX: this solution makes only valgrind happy...
4278 triggered by gcc.c-torture/execute/20000917-1.c */
4280 switch(vtop
->type
.t
& VT_BTYPE
) {
4285 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4287 error("unsupported expression type");
4290 sym_pop(&local_stack
, s
);
4292 } else if (tok
== TOK_RETURN
) {
4296 gen_assign_cast(&func_vt
);
4297 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4299 /* if returning structure, must copy it to implicit
4300 first pointer arg location */
4303 size
= type_size(&func_vt
,&align
);
4306 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4310 loc
= (loc
- size
) & -4;
4313 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4316 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4318 vtop
->type
= int_type
;
4324 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4327 /* copy structure value to pointer */
4332 } else if (is_float(func_vt
.t
)) {
4333 gv(rc_fret(func_vt
.t
));
4337 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4340 rsym
= gjmp(rsym
); /* jmp */
4341 } else if (tok
== TOK_BREAK
) {
4344 error("cannot break");
4345 *bsym
= gjmp(*bsym
);
4348 } else if (tok
== TOK_CONTINUE
) {
4351 error("cannot continue");
4352 *csym
= gjmp(*csym
);
4355 } else if (tok
== TOK_FOR
) {
4382 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4387 if (tok
== TOK_DO
) {
4392 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4403 if (tok
== TOK_SWITCH
) {
4407 /* XXX: other types than integer */
4408 case_reg
= gv(RC_INT
);
4412 b
= gjmp(0); /* jump to first case */
4414 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4415 /* if no default, jmp after switch */
4423 if (tok
== TOK_CASE
) {
4430 if (gnu_ext
&& tok
== TOK_DOTS
) {
4434 warning("empty case range");
4436 /* since a case is like a label, we must skip it with a jmp */
4443 *case_sym
= gtst(1, 0);
4446 *case_sym
= gtst(1, 0);
4450 *case_sym
= gtst(1, *case_sym
);
4455 goto block_after_label
;
4457 if (tok
== TOK_DEFAULT
) {
4463 error("too many 'default'");
4466 goto block_after_label
;
4468 if (tok
== TOK_GOTO
) {
4470 if (tok
== '*' && gnu_ext
) {
4474 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4477 } else if (tok
>= TOK_UIDENT
) {
4478 s
= label_find(tok
);
4479 /* put forward definition if needed */
4481 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4483 if (s
->r
== LABEL_DECLARED
)
4484 s
->r
= LABEL_FORWARD
;
4486 /* label already defined */
4487 if (s
->r
& LABEL_FORWARD
)
4488 s
->jnext
= gjmp(s
->jnext
);
4490 gjmp_addr(s
->jnext
);
4493 expect("label identifier");
4496 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4504 if (s
->r
== LABEL_DEFINED
)
4505 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4507 s
->r
= LABEL_DEFINED
;
4509 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4512 /* we accept this, but it is a mistake */
4515 warning("deprecated use of label at end of compound statement");
4519 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4522 /* expression case */
4537 /* t is the array or struct type. c is the array or struct
4538 address. cur_index/cur_field is the pointer to the current
4539 value. 'size_only' is true if only size info is needed (only used
4541 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4542 int *cur_index
, Sym
**cur_field
,
4546 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4552 if (gnu_ext
&& (l
= is_label()) != 0)
4554 while (tok
== '[' || tok
== '.') {
4556 if (!(type
->t
& VT_ARRAY
))
4557 expect("array type");
4560 index
= expr_const();
4561 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4562 expect("invalid index");
4563 if (tok
== TOK_DOTS
&& gnu_ext
) {
4565 index_last
= expr_const();
4566 if (index_last
< 0 ||
4567 (s
->c
>= 0 && index_last
>= s
->c
) ||
4569 expect("invalid index");
4575 *cur_index
= index_last
;
4576 type
= pointed_type(type
);
4577 elem_size
= type_size(type
, &align
);
4578 c
+= index
* elem_size
;
4579 /* NOTE: we only support ranges for last designator */
4580 nb_elems
= index_last
- index
+ 1;
4581 if (nb_elems
!= 1) {
4590 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4591 expect("struct/union type");
4604 /* XXX: fix this mess by using explicit storage field */
4606 type1
.t
|= (type
->t
& ~VT_TYPE
);
4620 if (type
->t
& VT_ARRAY
) {
4622 type
= pointed_type(type
);
4623 c
+= index
* type_size(type
, &align
);
4627 error("too many field init");
4628 /* XXX: fix this mess by using explicit storage field */
4630 type1
.t
|= (type
->t
& ~VT_TYPE
);
4635 decl_initializer(type
, sec
, c
, 0, size_only
);
4637 /* XXX: make it more general */
4638 if (!size_only
&& nb_elems
> 1) {
4639 unsigned long c_end
;
4644 error("range init not supported yet for dynamic storage");
4645 c_end
= c
+ nb_elems
* elem_size
;
4646 if (c_end
> sec
->data_allocated
)
4647 section_realloc(sec
, c_end
);
4648 src
= sec
->data
+ c
;
4650 for(i
= 1; i
< nb_elems
; i
++) {
4652 memcpy(dst
, src
, elem_size
);
4658 #define EXPR_CONST 1
4661 /* store a value or an expression directly in global data or in local array */
4662 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4663 int v
, int expr_type
)
4665 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4667 unsigned long long bit_mask
;
4675 /* compound literals must be allocated globally in this case */
4676 saved_global_expr
= global_expr
;
4679 global_expr
= saved_global_expr
;
4680 /* NOTE: symbols are accepted */
4681 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4682 error("initializer element is not constant");
4690 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4693 /* XXX: not portable */
4694 /* XXX: generate error if incorrect relocation */
4695 gen_assign_cast(&dtype
);
4696 bt
= type
->t
& VT_BTYPE
;
4697 /* we'll write at most 12 bytes */
4698 if (c
+ 12 > sec
->data_allocated
) {
4699 section_realloc(sec
, c
+ 12);
4701 ptr
= sec
->data
+ c
;
4702 /* XXX: make code faster ? */
4703 if (!(type
->t
& VT_BITFIELD
)) {
4708 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4709 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4710 bit_mask
= (1LL << bit_size
) - 1;
4712 if ((vtop
->r
& VT_SYM
) &&
4718 (bt
== VT_INT
&& bit_size
!= 32)))
4719 error("initializer element is not computable at load time");
4722 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4724 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4727 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4730 *(double *)ptr
= vtop
->c
.d
;
4733 *(long double *)ptr
= vtop
->c
.ld
;
4736 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4739 if (vtop
->r
& VT_SYM
) {
4740 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4742 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4747 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4754 /* put zeros for variable based init */
4755 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4758 /* nothing to do because globals are already set to zero */
4760 vpush_global_sym(&func_old_type
, TOK_memset
);
4768 /* 't' contains the type and storage info. 'c' is the offset of the
4769 object in section 'sec'. If 'sec' is NULL, it means stack based
4770 allocation. 'first' is true if array '{' must be read (multi
4771 dimension implicit array init handling). 'size_only' is true if
4772 size only evaluation is wanted (only for arrays). */
4773 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4774 int first
, int size_only
)
4776 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4777 int size1
, align1
, expr_type
;
4781 if (type
->t
& VT_ARRAY
) {
4785 t1
= pointed_type(type
);
4786 size1
= type_size(t1
, &align1
);
4789 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4792 error("character array initializer must be a literal,"
4793 " optionally enclosed in braces");
4798 /* only parse strings here if correct type (otherwise: handle
4799 them as ((w)char *) expressions */
4800 if ((tok
== TOK_LSTR
&&
4801 #ifdef TCC_TARGET_PE
4802 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4804 (t1
->t
& VT_BTYPE
) == VT_INT
4806 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4807 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4812 /* compute maximum number of chars wanted */
4814 cstr_len
= cstr
->size
;
4816 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4819 if (n
>= 0 && nb
> (n
- array_length
))
4820 nb
= n
- array_length
;
4823 warning("initializer-string for array is too long");
4824 /* in order to go faster for common case (char
4825 string in global variable, we handle it
4827 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4828 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4832 ch
= ((unsigned char *)cstr
->data
)[i
];
4834 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4835 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4843 /* only add trailing zero if enough storage (no
4844 warning in this case since it is standard) */
4845 if (n
< 0 || array_length
< n
) {
4847 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4853 if (ARRAY_RESIZE(s
->r
))
4855 while (tok
!= '}') {
4856 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4857 if (n
>= 0 && index
>= n
)
4858 error("index too large");
4859 /* must put zero in holes (note that doing it that way
4860 ensures that it even works with designators) */
4861 if (!size_only
&& array_length
< index
) {
4862 init_putz(t1
, sec
, c
+ array_length
* size1
,
4863 (index
- array_length
) * size1
);
4866 if (index
> array_length
)
4867 array_length
= index
;
4868 /* special test for multi dimensional arrays (may not
4869 be strictly correct if designators are used at the
4871 if (index
>= n
&& no_oblock
)
4880 /* put zeros at the end */
4881 if (!size_only
&& n
>= 0 && array_length
< n
) {
4882 init_putz(t1
, sec
, c
+ array_length
* size1
,
4883 (n
- array_length
) * size1
);
4885 /* patch type size if needed */
4887 s
->c
= array_length
;
4888 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4889 (sec
|| !first
|| tok
== '{')) {
4892 /* NOTE: the previous test is a specific case for automatic
4893 struct/union init */
4894 /* XXX: union needs only one init */
4896 /* XXX: this test is incorrect for local initializers
4897 beginning with ( without {. It would be much more difficult
4898 to do it correctly (ideally, the expression parser should
4899 be used in all cases) */
4905 while (tok
== '(') {
4909 if (!parse_btype(&type1
, &ad1
))
4911 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4913 if (!is_assignable_types(type
, &type1
))
4914 error("invalid type for cast");
4919 if (first
|| tok
== '{') {
4930 while (tok
!= '}') {
4931 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4933 if (!size_only
&& array_length
< index
) {
4934 init_putz(type
, sec
, c
+ array_length
,
4935 index
- array_length
);
4937 index
= index
+ type_size(&f
->type
, &align1
);
4938 if (index
> array_length
)
4939 array_length
= index
;
4941 /* gr: skip fields from same union - ugly. */
4943 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4944 /* test for same offset */
4945 if (f
->next
->c
!= f
->c
)
4947 /* if yes, test for bitfield shift */
4948 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4949 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4950 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4951 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4952 if (bit_pos_1
!= bit_pos_2
)
4959 if (no_oblock
&& f
== NULL
)
4965 /* put zeros at the end */
4966 if (!size_only
&& n
>= 0 && array_length
< n
) {
4967 init_putz(type
, sec
, c
+ array_length
,
4977 s
->c
= array_length
;
4978 } else if (tok
== '{') {
4980 decl_initializer(type
, sec
, c
, first
, size_only
);
4982 } else if (size_only
) {
4983 /* just skip expression */
4984 parlevel
= parlevel1
= 0;
4985 while ((parlevel
> 0 || parlevel1
> 0 ||
4986 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
4989 else if (tok
== ')')
4991 else if (tok
== '{')
4993 else if (tok
== '}')
4998 /* currently, we always use constant expression for globals
4999 (may change for scripting case) */
5000 expr_type
= EXPR_CONST
;
5002 expr_type
= EXPR_ANY
;
5003 init_putv(type
, sec
, c
, 0, expr_type
);
5007 /* parse an initializer for type 't' if 'has_init' is non zero, and
5008 allocate space in local or global data space ('r' is either
5009 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5010 variable 'v' with an associated name represented by 'asm_label' of
5011 scope 'scope' is declared before initializers are parsed. If 'v' is
5012 zero, then a reference to the new object is put in the value stack.
5013 If 'has_init' is 2, a special parsing is done to handle string
5015 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5016 int has_init
, int v
, char *asm_label
,
5019 int size
, align
, addr
, data_offset
;
5021 ParseState saved_parse_state
= {0};
5022 TokenString init_str
;
5025 /* resize the struct */
5026 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&& (type
->ref
->r
& (1<<31)) != 0)
5028 size
= type_size(type
, &align
);
5029 /* If unknown size, we must evaluate it before
5030 evaluating initializers because
5031 initializers can generate global data too
5032 (e.g. string pointers or ISOC99 compound
5033 literals). It also simplifies local
5034 initializers handling */
5035 tok_str_new(&init_str
);
5038 error("unknown type size");
5039 /* get all init string */
5040 if (has_init
== 2) {
5041 /* only get strings */
5042 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5043 tok_str_add_tok(&init_str
);
5048 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5050 error("unexpected end of file in initializer");
5051 tok_str_add_tok(&init_str
);
5054 else if (tok
== '}') {
5064 tok_str_add(&init_str
, -1);
5065 tok_str_add(&init_str
, 0);
5068 save_parse_state(&saved_parse_state
);
5070 macro_ptr
= init_str
.str
;
5072 decl_initializer(type
, NULL
, 0, 1, 1);
5073 /* prepare second initializer parsing */
5074 macro_ptr
= init_str
.str
;
5077 /* if still unknown size, error */
5078 size
= type_size(type
, &align
);
5080 error("unknown type size");
5082 /* take into account specified alignment if bigger */
5084 if (ad
->aligned
> align
)
5085 align
= ad
->aligned
;
5086 } else if (ad
->packed
) {
5089 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5091 #ifdef CONFIG_TCC_BCHECK
5092 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
5095 loc
= (loc
- size
) & -align
;
5097 #ifdef CONFIG_TCC_BCHECK
5098 /* handles bounds */
5099 /* XXX: currently, since we do only one pass, we cannot track
5100 '&' operators, so we add only arrays */
5101 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5102 unsigned long *bounds_ptr
;
5103 /* add padding between regions */
5105 /* then add local bound info */
5106 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5107 bounds_ptr
[0] = addr
;
5108 bounds_ptr
[1] = size
;
5112 /* local variable */
5113 sym_push(v
, type
, r
, addr
);
5115 /* push local reference */
5116 vset(type
, r
, addr
);
5122 if (v
&& scope
== VT_CONST
) {
5123 /* see if the symbol was already defined */
5126 if (!is_compatible_types(&sym
->type
, type
))
5127 error("incompatible types for redefinition of '%s'",
5128 get_tok_str(v
, NULL
));
5129 if (sym
->type
.t
& VT_EXTERN
) {
5130 /* if the variable is extern, it was not allocated */
5131 sym
->type
.t
&= ~VT_EXTERN
;
5132 /* set array size if it was ommited in extern
5134 if ((sym
->type
.t
& VT_ARRAY
) &&
5135 sym
->type
.ref
->c
< 0 &&
5137 sym
->type
.ref
->c
= type
->ref
->c
;
5139 /* we accept several definitions of the same
5140 global variable. this is tricky, because we
5141 must play with the SHN_COMMON type of the symbol */
5142 /* XXX: should check if the variable was already
5143 initialized. It is incorrect to initialized it
5145 /* no init data, we won't add more to the symbol */
5152 /* allocate symbol in corresponding section */
5157 else if (tcc_state
->nocommon
)
5161 data_offset
= sec
->data_offset
;
5162 data_offset
= (data_offset
+ align
- 1) & -align
;
5164 /* very important to increment global pointer at this time
5165 because initializers themselves can create new initializers */
5166 data_offset
+= size
;
5167 #ifdef CONFIG_TCC_BCHECK
5168 /* add padding if bound check */
5169 if (tcc_state
->do_bounds_check
)
5172 sec
->data_offset
= data_offset
;
5173 /* allocate section space to put the data */
5174 if (sec
->sh_type
!= SHT_NOBITS
&&
5175 data_offset
> sec
->data_allocated
)
5176 section_realloc(sec
, data_offset
);
5177 /* align section if needed */
5178 if (align
> sec
->sh_addralign
)
5179 sec
->sh_addralign
= align
;
5181 addr
= 0; /* avoid warning */
5185 if (scope
!= VT_CONST
|| !sym
) {
5186 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5187 sym
->asm_label
= asm_label
;
5189 /* update symbol definition */
5191 put_extern_sym(sym
, sec
, addr
, size
);
5194 /* put a common area */
5195 put_extern_sym(sym
, NULL
, align
, size
);
5196 /* XXX: find a nicer way */
5197 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5198 esym
->st_shndx
= SHN_COMMON
;
5203 /* push global reference */
5204 sym
= get_sym_ref(type
, sec
, addr
, size
);
5206 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5209 /* patch symbol weakness */
5210 if (type
->t
& VT_WEAK
) {
5211 unsigned char *st_info
=
5212 &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_info
;
5213 *st_info
= ELF32_ST_INFO(STB_WEAK
, ELF32_ST_TYPE(*st_info
));
5215 #ifdef CONFIG_TCC_BCHECK
5216 /* handles bounds now because the symbol must be defined
5217 before for the relocation */
5218 if (tcc_state
->do_bounds_check
) {
5219 unsigned long *bounds_ptr
;
5221 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5222 /* then add global bound info */
5223 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5224 bounds_ptr
[0] = 0; /* relocated */
5225 bounds_ptr
[1] = size
;
5230 decl_initializer(type
, sec
, addr
, 1, 0);
5231 /* restore parse state if needed */
5233 tok_str_free(init_str
.str
);
5234 restore_parse_state(&saved_parse_state
);
5240 static void put_func_debug(Sym
*sym
)
5245 /* XXX: we put here a dummy type */
5246 snprintf(buf
, sizeof(buf
), "%s:%c1",
5247 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5248 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5249 cur_text_section
, sym
->c
);
5250 /* //gr gdb wants a line at the function */
5251 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5256 /* parse an old style function declaration list */
5257 /* XXX: check multiple parameter */
5258 static void func_decl_list(Sym
*func_sym
)
5265 /* parse each declaration */
5266 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5267 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5268 if (!parse_btype(&btype
, &ad
))
5269 expect("declaration list");
5270 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5271 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5273 /* we accept no variable after */
5277 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5278 /* find parameter in function parameter list */
5281 if ((s
->v
& ~SYM_FIELD
) == v
)
5285 error("declaration for parameter '%s' but no such parameter",
5286 get_tok_str(v
, NULL
));
5288 /* check that no storage specifier except 'register' was given */
5289 if (type
.t
& VT_STORAGE
)
5290 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5291 convert_parameter_type(&type
);
5292 /* we can add the type (NOTE: it could be local to the function) */
5294 /* accept other parameters */
5305 /* parse a function defined by symbol 'sym' and generate its code in
5306 'cur_text_section' */
5307 static void gen_function(Sym
*sym
)
5309 int saved_nocode_wanted
= nocode_wanted
;
5311 ind
= cur_text_section
->data_offset
;
5312 /* NOTE: we patch the symbol size later */
5313 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5314 funcname
= get_tok_str(sym
->v
, NULL
);
5316 /* put debug symbol */
5317 if (tcc_state
->do_debug
)
5318 put_func_debug(sym
);
5319 /* push a dummy symbol to enable local sym storage */
5320 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5321 gfunc_prolog(&sym
->type
);
5323 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5326 cur_text_section
->data_offset
= ind
;
5327 label_pop(&global_label_stack
, NULL
);
5328 sym_pop(&local_stack
, NULL
); /* reset local stack */
5329 /* end of function */
5330 /* patch symbol size */
5331 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5333 /* patch symbol weakness (this definition overrules any prototype) */
5334 if (sym
->type
.t
& VT_WEAK
) {
5335 unsigned char *st_info
=
5336 &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_info
;
5337 *st_info
= ELF32_ST_INFO(STB_WEAK
, ELF32_ST_TYPE(*st_info
));
5339 if (tcc_state
->do_debug
) {
5340 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5342 /* It's better to crash than to generate wrong code */
5343 cur_text_section
= NULL
;
5344 funcname
= ""; /* for safety */
5345 func_vt
.t
= VT_VOID
; /* for safety */
5346 ind
= 0; /* for safety */
5347 nocode_wanted
= saved_nocode_wanted
;
5350 ST_FUNC
void gen_inline_functions(void)
5353 int *str
, inline_generated
, i
;
5354 struct InlineFunc
*fn
;
5356 /* iterate while inline function are referenced */
5358 inline_generated
= 0;
5359 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5360 fn
= tcc_state
->inline_fns
[i
];
5362 if (sym
&& sym
->c
) {
5363 /* the function was used: generate its code and
5364 convert it to a normal function */
5365 str
= fn
->token_str
;
5368 strcpy(file
->filename
, fn
->filename
);
5369 sym
->r
= VT_SYM
| VT_CONST
;
5370 sym
->type
.t
&= ~VT_INLINE
;
5374 cur_text_section
= text_section
;
5376 macro_ptr
= NULL
; /* fail safe */
5378 inline_generated
= 1;
5381 if (!inline_generated
)
5384 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5385 fn
= tcc_state
->inline_fns
[i
];
5386 str
= fn
->token_str
;
5389 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5392 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5393 ST_FUNC
void decl(int l
)
5401 if (!parse_btype(&btype
, &ad
)) {
5402 /* skip redundant ';' */
5403 /* XXX: find more elegant solution */
5408 if (l
== VT_CONST
&&
5409 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5410 /* global asm block */
5414 /* special test for old K&R protos without explicit int
5415 type. Only accepted when defining global data */
5416 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5420 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5421 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5423 /* we accept no variable after */
5427 while (1) { /* iterate thru each declaration */
5429 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5433 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5434 printf("type = '%s'\n", buf
);
5437 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5438 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5439 error("function without file scope cannot be static");
5441 /* if old style function prototype, we accept a
5444 if (sym
->c
== FUNC_OLD
)
5445 func_decl_list(sym
);
5448 #ifdef TCC_TARGET_PE
5450 type
.t
|= VT_IMPORT
;
5452 type
.t
|= VT_EXPORT
;
5456 error("cannot use local functions");
5457 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5458 expect("function definition");
5460 /* reject abstract declarators in function definition */
5462 while ((sym
= sym
->next
) != NULL
)
5463 if (!(sym
->v
& ~SYM_FIELD
))
5464 expect("identifier");
5466 /* XXX: cannot do better now: convert extern line to static inline */
5467 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5468 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5472 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5475 r
= sym
->type
.ref
->r
;
5476 /* use func_call from prototype if not defined */
5477 if (FUNC_CALL(r
) != FUNC_CDECL
5478 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5479 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5481 /* use export from prototype */
5483 FUNC_EXPORT(type
.ref
->r
) = 1;
5485 /* use static from prototype */
5486 if (sym
->type
.t
& VT_STATIC
)
5487 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5489 if (!is_compatible_types(&sym
->type
, &type
)) {
5491 error("incompatible types for redefinition of '%s'",
5492 get_tok_str(v
, NULL
));
5494 /* if symbol is already defined, then put complete type */
5497 /* put function symbol */
5498 sym
= global_identifier_push(v
, type
.t
, 0);
5499 sym
->type
.ref
= type
.ref
;
5502 /* static inline functions are just recorded as a kind
5503 of macro. Their code will be emitted at the end of
5504 the compilation unit only if they are used */
5505 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5506 (VT_INLINE
| VT_STATIC
)) {
5507 TokenString func_str
;
5509 struct InlineFunc
*fn
;
5510 const char *filename
;
5512 tok_str_new(&func_str
);
5518 error("unexpected end of file");
5519 tok_str_add_tok(&func_str
);
5524 } else if (t
== '}') {
5526 if (block_level
== 0)
5530 tok_str_add(&func_str
, -1);
5531 tok_str_add(&func_str
, 0);
5532 filename
= file
? file
->filename
: "";
5533 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5534 strcpy(fn
->filename
, filename
);
5536 fn
->token_str
= func_str
.str
;
5537 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5540 /* compute text section */
5541 cur_text_section
= ad
.section
;
5542 if (!cur_text_section
)
5543 cur_text_section
= text_section
;
5544 sym
->r
= VT_SYM
| VT_CONST
;
5549 if (btype
.t
& VT_TYPEDEF
) {
5550 /* save typedefed type */
5551 /* XXX: test storage specifiers ? */
5552 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5553 sym
->type
.t
|= VT_TYPEDEF
;
5554 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5555 char *asm_label
; // associated asm label
5559 /* external function definition */
5560 /* specific case for func_call attribute */
5561 type
.ref
->r
= INT_ATTR(&ad
);
5563 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5566 asm_label_instr(&astr
);
5567 asm_label
= tcc_strdup(astr
.data
);
5570 fn
= external_sym(v
, &type
, 0, asm_label
);
5571 if (gnu_ext
&& (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
))
5572 parse_attribute((AttributeDef
*) &fn
->type
.ref
->r
);
5574 char *asm_label
; // associated asm label
5576 /* not lvalue if array */
5579 if (!(type
.t
& VT_ARRAY
))
5580 r
|= lvalue_type(type
.t
);
5581 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5584 asm_label_instr(&astr
);
5585 asm_label
= tcc_strdup(astr
.data
);
5588 has_init
= (tok
== '=');
5589 if ((btype
.t
& VT_EXTERN
) ||
5590 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5591 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5592 /* external variable */
5593 /* NOTE: as GCC, uninitialized global static
5594 arrays of null size are considered as
5596 external_sym(v
, &type
, r
, asm_label
);
5598 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5599 if (type
.t
& VT_STATIC
)
5605 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);