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 int decl0(int l
, int is_for_loop_init
);
80 static void expr_eq(void);
81 static void unary_type(CType
*type
);
82 static void vla_runtime_type_size(CType
*type
, int *a
);
83 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
84 static void expr_type(CType
*type
);
86 ST_INLN
int is_float(int t
)
90 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
93 ST_FUNC
void test_lvalue(void)
95 if (!(vtop
->r
& VT_LVAL
))
99 /* ------------------------------------------------------------------------- */
100 /* symbol allocator */
101 static Sym
*__sym_malloc(void)
103 Sym
*sym_pool
, *sym
, *last_sym
;
106 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
107 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
109 last_sym
= sym_free_first
;
111 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
112 sym
->next
= last_sym
;
116 sym_free_first
= last_sym
;
120 static inline Sym
*sym_malloc(void)
123 sym
= sym_free_first
;
125 sym
= __sym_malloc();
126 sym_free_first
= sym
->next
;
130 ST_INLN
void sym_free(Sym
*sym
)
132 sym
->next
= sym_free_first
;
133 tcc_free(sym
->asm_label
);
134 sym_free_first
= sym
;
137 /* push, without hashing */
138 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
157 /* find a symbol and return its associated structure. 's' is the top
158 of the symbol stack */
159 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
169 /* structure lookup */
170 ST_INLN Sym
*struct_find(int v
)
173 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
175 return table_ident
[v
]->sym_struct
;
178 /* find an identifier */
179 ST_INLN Sym
*sym_find(int v
)
182 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
184 return table_ident
[v
]->sym_identifier
;
187 /* push a given symbol on the symbol stack */
188 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
197 s
= sym_push2(ps
, v
, type
->t
, c
);
198 s
->type
.ref
= type
->ref
;
200 /* don't record fields or anonymous symbols */
202 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
203 /* record symbol in token array */
204 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
206 ps
= &ts
->sym_struct
;
208 ps
= &ts
->sym_identifier
;
215 /* push a global identifier */
216 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
219 s
= sym_push2(&global_stack
, v
, t
, c
);
220 /* don't record anonymous symbol */
221 if (v
< SYM_FIRST_ANOM
) {
222 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
223 /* modify the top most local identifier, so that
224 sym_identifier will point to 's' when popped */
226 ps
= &(*ps
)->prev_tok
;
233 /* pop symbols until top reaches 'b' */
234 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
244 /* remove symbol in token array */
246 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
247 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
249 ps
= &ts
->sym_struct
;
251 ps
= &ts
->sym_identifier
;
260 static void weaken_symbol(Sym
*sym
)
262 sym
->type
.t
|= VT_WEAK
;
267 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
268 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
269 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
273 /* ------------------------------------------------------------------------- */
275 ST_FUNC
void swap(int *p
, int *q
)
283 static void vsetc(CType
*type
, int r
, CValue
*vc
)
287 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
288 error("memory full");
289 /* cannot let cpu flags if other instruction are generated. Also
290 avoid leaving VT_JMP anywhere except on the top of the stack
291 because it would complicate the code generator. */
292 if (vtop
>= vstack
) {
293 v
= vtop
->r
& VT_VALMASK
;
294 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
304 /* push constant of type "type" with useless value */
305 void vpush(CType
*type
)
308 vsetc(type
, VT_CONST
, &cval
);
311 /* push integer constant */
312 ST_FUNC
void vpushi(int v
)
316 vsetc(&int_type
, VT_CONST
, &cval
);
319 /* push long long constant */
320 static void vpushll(long long v
)
327 vsetc(&ctype
, VT_CONST
, &cval
);
330 /* push arbitrary 64bit constant */
331 void vpush64(int ty
, unsigned long long v
)
337 vsetc(&ctype
, VT_CONST
, &cval
);
340 /* Return a static symbol pointing to a section */
341 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
347 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
348 sym
->type
.ref
= type
->ref
;
349 sym
->r
= VT_CONST
| VT_SYM
;
350 put_extern_sym(sym
, sec
, offset
, size
);
354 /* push a reference to a section offset by adding a dummy symbol */
355 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
360 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
361 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
364 /* define a new external reference to a symbol 'v' of type 'u' */
365 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
371 /* push forward reference */
372 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
373 s
->type
.ref
= type
->ref
;
374 s
->r
= r
| VT_CONST
| VT_SYM
;
379 /* define a new external reference to a symbol 'v' with alternate asm
380 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
381 is no alternate name (most cases) */
382 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
388 /* push forward reference */
389 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
390 s
->asm_label
= asm_label
;
391 s
->type
.t
|= VT_EXTERN
;
392 } else if (s
->type
.ref
== func_old_type
.ref
) {
393 s
->type
.ref
= type
->ref
;
394 s
->r
= r
| VT_CONST
| VT_SYM
;
395 s
->type
.t
|= VT_EXTERN
;
396 } else if (!is_compatible_types(&s
->type
, type
)) {
397 error("incompatible types for redefinition of '%s'",
398 get_tok_str(v
, NULL
));
403 /* push a reference to global symbol v */
404 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
409 sym
= external_global_sym(v
, type
, 0);
411 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
415 ST_FUNC
void vset(CType
*type
, int r
, int v
)
420 vsetc(type
, r
, &cval
);
423 static void vseti(int r
, int v
)
431 ST_FUNC
void vswap(void)
440 ST_FUNC
void vpushv(SValue
*v
)
442 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
443 error("memory full");
448 static void vdup(void)
453 /* save r to the memory stack, and mark it as being free */
454 ST_FUNC
void save_reg(int r
)
456 int l
, saved
, size
, align
;
460 /* modify all stack values */
463 for(p
=vstack
;p
<=vtop
;p
++) {
464 if ((p
->r
& VT_VALMASK
) == r
||
465 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
466 /* must save value on stack if not already done */
468 /* NOTE: must reload 'r' because r might be equal to r2 */
469 r
= p
->r
& VT_VALMASK
;
470 /* store register in the stack */
472 if ((p
->r
& VT_LVAL
) ||
473 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
474 #ifdef TCC_TARGET_X86_64
475 type
= &char_pointer_type
;
479 size
= type_size(type
, &align
);
480 loc
= (loc
- size
) & -align
;
482 sv
.r
= VT_LOCAL
| VT_LVAL
;
485 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
486 /* x86 specific: need to pop fp register ST0 if saved */
488 o(0xd8dd); /* fstp %st(0) */
491 #ifndef TCC_TARGET_X86_64
492 /* special long long case */
493 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
501 /* mark that stack entry as being saved on the stack */
502 if (p
->r
& VT_LVAL
) {
503 /* also clear the bounded flag because the
504 relocation address of the function was stored in
506 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
508 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
516 #ifdef TCC_TARGET_ARM
517 /* find a register of class 'rc2' with at most one reference on stack.
518 * If none, call get_reg(rc) */
519 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
524 for(r
=0;r
<NB_REGS
;r
++) {
525 if (reg_classes
[r
] & rc2
) {
528 for(p
= vstack
; p
<= vtop
; p
++) {
529 if ((p
->r
& VT_VALMASK
) == r
||
530 (p
->r2
& VT_VALMASK
) == r
)
541 /* find a free register of class 'rc'. If none, save one register */
542 ST_FUNC
int get_reg(int rc
)
547 /* find a free register */
548 for(r
=0;r
<NB_REGS
;r
++) {
549 if (reg_classes
[r
] & rc
) {
550 for(p
=vstack
;p
<=vtop
;p
++) {
551 if ((p
->r
& VT_VALMASK
) == r
||
552 (p
->r2
& VT_VALMASK
) == r
)
560 /* no register left : free the first one on the stack (VERY
561 IMPORTANT to start from the bottom to ensure that we don't
562 spill registers used in gen_opi()) */
563 for(p
=vstack
;p
<=vtop
;p
++) {
564 r
= p
->r
& VT_VALMASK
;
565 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
567 /* also look at second register (if long long) */
568 r
= p
->r2
& VT_VALMASK
;
569 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
575 /* Should never comes here */
579 /* save registers up to (vtop - n) stack entry */
580 ST_FUNC
void save_regs(int n
)
585 for(p
= vstack
;p
<= p1
; p
++) {
586 r
= p
->r
& VT_VALMASK
;
593 /* move register 's' to 'r', and flush previous value of r to memory
595 static void move_reg(int r
, int s
)
608 /* get address of vtop (vtop MUST BE an lvalue) */
609 static void gaddrof(void)
612 /* tricky: if saved lvalue, then we can go back to lvalue */
613 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
614 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
617 #ifdef CONFIG_TCC_BCHECK
618 /* generate lvalue bound code */
619 static void gbound(void)
624 vtop
->r
&= ~VT_MUSTBOUND
;
625 /* if lvalue, then use checking code before dereferencing */
626 if (vtop
->r
& VT_LVAL
) {
627 /* if not VT_BOUNDED value, then make one */
628 if (!(vtop
->r
& VT_BOUNDED
)) {
629 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
630 /* must save type because we must set it to int to get pointer */
632 vtop
->type
.t
= VT_INT
;
635 gen_bounded_ptr_add();
636 vtop
->r
|= lval_type
;
639 /* then check for dereferencing */
640 gen_bounded_ptr_deref();
645 /* store vtop a register belonging to class 'rc'. lvalues are
646 converted to values. Cannot be used if cannot be converted to
647 register value (such as structures). */
648 ST_FUNC
int gv(int rc
)
650 int r
, bit_pos
, bit_size
, size
, align
, i
;
651 #ifndef TCC_TARGET_X86_64
655 /* NOTE: get_reg can modify vstack[] */
656 if (vtop
->type
.t
& VT_BITFIELD
) {
659 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
660 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
661 /* remove bit field info to avoid loops */
662 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
663 /* cast to int to propagate signedness in following ops */
664 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
669 if((vtop
->type
.t
& VT_UNSIGNED
) ||
670 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
671 type
.t
|= VT_UNSIGNED
;
673 /* generate shifts */
674 vpushi(bits
- (bit_pos
+ bit_size
));
676 vpushi(bits
- bit_size
);
677 /* NOTE: transformed to SHR if unsigned */
681 if (is_float(vtop
->type
.t
) &&
682 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
685 unsigned long offset
;
686 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
690 /* XXX: unify with initializers handling ? */
691 /* CPUs usually cannot use float constants, so we store them
692 generically in data segment */
693 size
= type_size(&vtop
->type
, &align
);
694 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
695 data_section
->data_offset
= offset
;
696 /* XXX: not portable yet */
697 #if defined(__i386__) || defined(__x86_64__)
698 /* Zero pad x87 tenbyte long doubles */
699 if (size
== LDOUBLE_SIZE
)
700 vtop
->c
.tab
[2] &= 0xffff;
702 ptr
= section_ptr_add(data_section
, size
);
704 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
708 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
712 ptr
[i
] = vtop
->c
.tab
[i
];
713 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
714 vtop
->r
|= VT_LVAL
| VT_SYM
;
718 #ifdef CONFIG_TCC_BCHECK
719 if (vtop
->r
& VT_MUSTBOUND
)
723 r
= vtop
->r
& VT_VALMASK
;
724 #ifndef TCC_TARGET_X86_64
729 /* need to reload if:
731 - lvalue (need to dereference pointer)
732 - already a register, but not in the right class */
734 || (vtop
->r
& VT_LVAL
)
735 || !(reg_classes
[r
] & rc
)
736 #ifndef TCC_TARGET_X86_64
737 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
742 #ifndef TCC_TARGET_X86_64
743 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
745 unsigned long long ll
;
746 /* two register type load : expand to two words
748 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
751 vtop
->c
.ui
= ll
; /* first word */
753 vtop
->r
= r
; /* save register value */
754 vpushi(ll
>> 32); /* second word */
755 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
756 (vtop
->r
& VT_LVAL
)) {
757 /* We do not want to modifier the long long
758 pointer here, so the safest (and less
759 efficient) is to save all the other registers
760 in the stack. XXX: totally inefficient. */
762 /* load from memory */
765 vtop
[-1].r
= r
; /* save register value */
766 /* increment pointer to get second word */
767 vtop
->type
.t
= VT_INT
;
776 vtop
[-1].r
= r
; /* save register value */
777 vtop
->r
= vtop
[-1].r2
;
779 /* allocate second register */
783 /* write second register */
787 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
789 /* lvalue of scalar type : need to use lvalue type
790 because of possible cast */
793 /* compute memory access type */
794 if (vtop
->r
& VT_LVAL_BYTE
)
796 else if (vtop
->r
& VT_LVAL_SHORT
)
798 if (vtop
->r
& VT_LVAL_UNSIGNED
)
802 /* restore wanted type */
805 /* one register type load */
810 #ifdef TCC_TARGET_C67
811 /* uses register pairs for doubles */
812 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
819 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
820 ST_FUNC
void gv2(int rc1
, int rc2
)
824 /* generate more generic register first. But VT_JMP or VT_CMP
825 values must be generated first in all cases to avoid possible
827 v
= vtop
[0].r
& VT_VALMASK
;
828 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
833 /* test if reload is needed for first register */
834 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
844 /* test if reload is needed for first register */
845 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
851 /* wrapper around RC_FRET to return a register by type */
852 static int rc_fret(int t
)
854 #ifdef TCC_TARGET_X86_64
855 if (t
== VT_LDOUBLE
) {
862 /* wrapper around REG_FRET to return a register by type */
863 static int reg_fret(int t
)
865 #ifdef TCC_TARGET_X86_64
866 if (t
== VT_LDOUBLE
) {
873 /* expand long long on stack in two int registers */
874 static void lexpand(void)
878 u
= vtop
->type
.t
& VT_UNSIGNED
;
881 vtop
[0].r
= vtop
[-1].r2
;
882 vtop
[0].r2
= VT_CONST
;
883 vtop
[-1].r2
= VT_CONST
;
884 vtop
[0].type
.t
= VT_INT
| u
;
885 vtop
[-1].type
.t
= VT_INT
| u
;
888 #ifdef TCC_TARGET_ARM
889 /* expand long long on stack */
890 ST_FUNC
void lexpand_nr(void)
894 u
= vtop
->type
.t
& VT_UNSIGNED
;
897 vtop
->type
.t
= VT_INT
| u
;
898 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
900 vtop
[-1].c
.ui
= vtop
->c
.ull
;
901 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
903 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
905 vtop
->r
= vtop
[-1].r
;
906 } else if (v
> VT_CONST
) {
910 vtop
->r
= vtop
[-1].r2
;
911 vtop
[-1].r2
= VT_CONST
;
912 vtop
[-1].type
.t
= VT_INT
| u
;
916 /* build a long long from two ints */
917 static void lbuild(int t
)
920 vtop
[-1].r2
= vtop
[0].r
;
925 /* rotate n first stack elements to the bottom
926 I1 ... In -> I2 ... In I1 [top is right]
928 static void vrotb(int n
)
939 /* rotate n first stack elements to the top
940 I1 ... In -> In I1 ... I(n-1) [top is right]
942 ST_FUNC
void vrott(int n
)
948 for(i
= 0;i
< n
- 1; i
++)
949 vtop
[-i
] = vtop
[-i
- 1];
953 #ifdef TCC_TARGET_ARM
954 /* like vrott but in other direction
955 In ... I1 -> I(n-1) ... I1 In [top is right]
957 ST_FUNC
void vnrott(int n
)
963 for(i
= n
- 1; i
> 0; i
--)
964 vtop
[-i
] = vtop
[-i
+ 1];
969 /* pop stack value */
970 ST_FUNC
void vpop(void)
973 v
= vtop
->r
& VT_VALMASK
;
974 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
975 /* for x86, we need to pop the FP stack */
976 if (v
== TREG_ST0
&& !nocode_wanted
) {
977 o(0xd8dd); /* fstp %st(0) */
980 if (v
== VT_JMP
|| v
== VT_JMPI
) {
981 /* need to put correct jump if && or || without test */
987 /* convert stack entry to register and duplicate its value in another
989 static void gv_dup(void)
995 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1002 /* stack: H L L1 H1 */
1010 /* duplicate value */
1015 #ifdef TCC_TARGET_X86_64
1016 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1026 load(r1
, &sv
); /* move r to r1 */
1028 /* duplicates value */
1034 #ifndef TCC_TARGET_X86_64
1035 /* generate CPU independent (unsigned) long long operations */
1036 static void gen_opl(int op
)
1038 int t
, a
, b
, op1
, c
, i
;
1040 unsigned short reg_iret
= REG_IRET
;
1041 unsigned short reg_lret
= REG_LRET
;
1047 func
= TOK___divdi3
;
1050 func
= TOK___udivdi3
;
1053 func
= TOK___moddi3
;
1056 func
= TOK___umoddi3
;
1063 /* call generic long long function */
1064 vpush_global_sym(&func_old_type
, func
);
1069 vtop
->r2
= reg_lret
;
1082 /* stack: L1 H1 L2 H2 */
1087 vtop
[-2] = vtop
[-3];
1090 /* stack: H1 H2 L1 L2 */
1096 /* stack: H1 H2 L1 L2 ML MH */
1099 /* stack: ML MH H1 H2 L1 L2 */
1103 /* stack: ML MH H1 L2 H2 L1 */
1108 /* stack: ML MH M1 M2 */
1111 } else if (op
== '+' || op
== '-') {
1112 /* XXX: add non carry method too (for MIPS or alpha) */
1118 /* stack: H1 H2 (L1 op L2) */
1121 gen_op(op1
+ 1); /* TOK_xxxC2 */
1124 /* stack: H1 H2 (L1 op L2) */
1127 /* stack: (L1 op L2) H1 H2 */
1129 /* stack: (L1 op L2) (H1 op H2) */
1137 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1138 t
= vtop
[-1].type
.t
;
1142 /* stack: L H shift */
1144 /* constant: simpler */
1145 /* NOTE: all comments are for SHL. the other cases are
1146 done by swaping words */
1157 if (op
!= TOK_SAR
) {
1190 /* XXX: should provide a faster fallback on x86 ? */
1193 func
= TOK___ashrdi3
;
1196 func
= TOK___lshrdi3
;
1199 func
= TOK___ashldi3
;
1205 /* compare operations */
1211 /* stack: L1 H1 L2 H2 */
1213 vtop
[-1] = vtop
[-2];
1215 /* stack: L1 L2 H1 H2 */
1218 /* when values are equal, we need to compare low words. since
1219 the jump is inverted, we invert the test too. */
1222 else if (op1
== TOK_GT
)
1224 else if (op1
== TOK_ULT
)
1226 else if (op1
== TOK_UGT
)
1231 if (op1
!= TOK_NE
) {
1235 /* generate non equal test */
1236 /* XXX: NOT PORTABLE yet */
1240 #if defined(TCC_TARGET_I386)
1241 b
= psym(0x850f, 0);
1242 #elif defined(TCC_TARGET_ARM)
1244 o(0x1A000000 | encbranch(ind
, 0, 1));
1245 #elif defined(TCC_TARGET_C67)
1246 error("not implemented");
1248 #error not supported
1252 /* compare low. Always unsigned */
1256 else if (op1
== TOK_LE
)
1258 else if (op1
== TOK_GT
)
1260 else if (op1
== TOK_GE
)
1271 /* handle integer constant optimizations and various machine
1273 static void gen_opic(int op
)
1275 int c1
, c2
, t1
, t2
, n
;
1278 typedef unsigned long long U
;
1282 t1
= v1
->type
.t
& VT_BTYPE
;
1283 t2
= v2
->type
.t
& VT_BTYPE
;
1287 else if (v1
->type
.t
& VT_UNSIGNED
)
1294 else if (v2
->type
.t
& VT_UNSIGNED
)
1299 /* currently, we cannot do computations with forward symbols */
1300 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1301 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1304 case '+': l1
+= l2
; break;
1305 case '-': l1
-= l2
; break;
1306 case '&': l1
&= l2
; break;
1307 case '^': l1
^= l2
; break;
1308 case '|': l1
|= l2
; break;
1309 case '*': l1
*= l2
; break;
1316 /* if division by zero, generate explicit division */
1319 error("division by zero in constant");
1323 default: l1
/= l2
; break;
1324 case '%': l1
%= l2
; break;
1325 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1326 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1329 case TOK_SHL
: l1
<<= l2
; break;
1330 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1331 case TOK_SAR
: l1
>>= l2
; break;
1333 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1334 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1335 case TOK_EQ
: l1
= l1
== l2
; break;
1336 case TOK_NE
: l1
= l1
!= l2
; break;
1337 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1338 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1339 case TOK_LT
: l1
= l1
< l2
; break;
1340 case TOK_GE
: l1
= l1
>= l2
; break;
1341 case TOK_LE
: l1
= l1
<= l2
; break;
1342 case TOK_GT
: l1
= l1
> l2
; break;
1344 case TOK_LAND
: l1
= l1
&& l2
; break;
1345 case TOK_LOR
: l1
= l1
|| l2
; break;
1352 /* if commutative ops, put c2 as constant */
1353 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1354 op
== '|' || op
== '*')) {
1356 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1357 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1359 /* Filter out NOP operations like x*1, x-0, x&-1... */
1360 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1363 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1364 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1370 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1371 /* try to use shifts instead of muls or divs */
1372 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1381 else if (op
== TOK_PDIV
)
1387 } else if (c2
&& (op
== '+' || op
== '-') &&
1388 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1389 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1390 /* symbol + constant case */
1397 if (!nocode_wanted
) {
1398 /* call low level op generator */
1399 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1410 /* generate a floating point operation with constant propagation */
1411 static void gen_opif(int op
)
1419 /* currently, we cannot do computations with forward symbols */
1420 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1421 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1423 if (v1
->type
.t
== VT_FLOAT
) {
1426 } else if (v1
->type
.t
== VT_DOUBLE
) {
1434 /* NOTE: we only do constant propagation if finite number (not
1435 NaN or infinity) (ANSI spec) */
1436 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1440 case '+': f1
+= f2
; break;
1441 case '-': f1
-= f2
; break;
1442 case '*': f1
*= f2
; break;
1446 error("division by zero in constant");
1451 /* XXX: also handles tests ? */
1455 /* XXX: overflow test ? */
1456 if (v1
->type
.t
== VT_FLOAT
) {
1458 } else if (v1
->type
.t
== VT_DOUBLE
) {
1466 if (!nocode_wanted
) {
1474 static int pointed_size(CType
*type
)
1477 return type_size(pointed_type(type
), &align
);
1480 static void vla_runtime_pointed_size(CType
*type
)
1483 vla_runtime_type_size(pointed_type(type
), &align
);
1486 static inline int is_null_pointer(SValue
*p
)
1488 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1490 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1491 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1494 static inline int is_integer_btype(int bt
)
1496 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1497 bt
== VT_INT
|| bt
== VT_LLONG
);
1500 /* check types for comparison or substraction of pointers */
1501 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1503 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1506 /* null pointers are accepted for all comparisons as gcc */
1507 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1511 bt1
= type1
->t
& VT_BTYPE
;
1512 bt2
= type2
->t
& VT_BTYPE
;
1513 /* accept comparison between pointer and integer with a warning */
1514 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1515 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1516 warning("comparison between pointer and integer");
1520 /* both must be pointers or implicit function pointers */
1521 if (bt1
== VT_PTR
) {
1522 type1
= pointed_type(type1
);
1523 } else if (bt1
!= VT_FUNC
)
1524 goto invalid_operands
;
1526 if (bt2
== VT_PTR
) {
1527 type2
= pointed_type(type2
);
1528 } else if (bt2
!= VT_FUNC
) {
1530 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1532 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1533 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1537 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1538 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1539 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1540 /* gcc-like error if '-' is used */
1542 goto invalid_operands
;
1544 warning("comparison of distinct pointer types lacks a cast");
1548 /* generic gen_op: handles types problems */
1549 ST_FUNC
void gen_op(int op
)
1551 int u
, t1
, t2
, bt1
, bt2
, t
;
1554 t1
= vtop
[-1].type
.t
;
1555 t2
= vtop
[0].type
.t
;
1556 bt1
= t1
& VT_BTYPE
;
1557 bt2
= t2
& VT_BTYPE
;
1559 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1560 /* at least one operand is a pointer */
1561 /* relationnal op: must be both pointers */
1562 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1563 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1564 /* pointers are handled are unsigned */
1565 #ifdef TCC_TARGET_X86_64
1566 t
= VT_LLONG
| VT_UNSIGNED
;
1568 t
= VT_INT
| VT_UNSIGNED
;
1572 /* if both pointers, then it must be the '-' op */
1573 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1575 error("cannot use pointers here");
1576 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1577 /* XXX: check that types are compatible */
1578 if (vtop
[-1].type
.t
& VT_VLA
) {
1579 vla_runtime_pointed_size(&vtop
[-1].type
);
1581 vpushi(pointed_size(&vtop
[-1].type
));
1585 /* set to integer type */
1586 #ifdef TCC_TARGET_X86_64
1587 vtop
->type
.t
= VT_LLONG
;
1589 vtop
->type
.t
= VT_INT
;
1594 /* exactly one pointer : must be '+' or '-'. */
1595 if (op
!= '-' && op
!= '+')
1596 error("cannot use pointers here");
1597 /* Put pointer as first operand */
1598 if (bt2
== VT_PTR
) {
1602 type1
= vtop
[-1].type
;
1603 type1
.t
&= ~VT_ARRAY
;
1604 if (vtop
[-1].type
.t
& VT_VLA
)
1605 vla_runtime_pointed_size(&vtop
[-1].type
);
1607 u
= pointed_size(&vtop
[-1].type
);
1609 error("unknown array element size");
1610 #ifdef TCC_TARGET_X86_64
1613 /* XXX: cast to int ? (long long case) */
1618 #ifdef CONFIG_TCC_BCHECK
1619 /* if evaluating constant expression, no code should be
1620 generated, so no bound check */
1621 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1622 /* if bounded pointers, we generate a special code to
1629 gen_bounded_ptr_add();
1635 /* put again type if gen_opic() swaped operands */
1638 } else if (is_float(bt1
) || is_float(bt2
)) {
1639 /* compute bigger type and do implicit casts */
1640 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1642 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1647 /* floats can only be used for a few operations */
1648 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1649 (op
< TOK_ULT
|| op
> TOK_GT
))
1650 error("invalid operands for binary operation");
1652 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1653 /* cast to biggest op */
1655 /* convert to unsigned if it does not fit in a long long */
1656 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1657 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1661 /* integer operations */
1663 /* convert to unsigned if it does not fit in an integer */
1664 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1665 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1668 /* XXX: currently, some unsigned operations are explicit, so
1669 we modify them here */
1670 if (t
& VT_UNSIGNED
) {
1677 else if (op
== TOK_LT
)
1679 else if (op
== TOK_GT
)
1681 else if (op
== TOK_LE
)
1683 else if (op
== TOK_GE
)
1690 /* special case for shifts and long long: we keep the shift as
1692 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1699 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1700 /* relationnal op: the result is an int */
1701 vtop
->type
.t
= VT_INT
;
1708 #ifndef TCC_TARGET_ARM
1709 /* generic itof for unsigned long long case */
1710 static void gen_cvt_itof1(int t
)
1712 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1713 (VT_LLONG
| VT_UNSIGNED
)) {
1716 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1717 #if LDOUBLE_SIZE != 8
1718 else if (t
== VT_LDOUBLE
)
1719 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1722 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1726 vtop
->r
= reg_fret(t
);
1733 /* generic ftoi for unsigned long long case */
1734 static void gen_cvt_ftoi1(int t
)
1738 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1739 /* not handled natively */
1740 st
= vtop
->type
.t
& VT_BTYPE
;
1742 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1743 #if LDOUBLE_SIZE != 8
1744 else if (st
== VT_LDOUBLE
)
1745 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1748 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1753 vtop
->r2
= REG_LRET
;
1759 /* force char or short cast */
1760 static void force_charshort_cast(int t
)
1764 /* XXX: add optimization if lvalue : just change type and offset */
1769 if (t
& VT_UNSIGNED
) {
1770 vpushi((1 << bits
) - 1);
1776 /* result must be signed or the SAR is converted to an SHL
1777 This was not the case when "t" was a signed short
1778 and the last value on the stack was an unsigned int */
1779 vtop
->type
.t
&= ~VT_UNSIGNED
;
1785 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1786 static void gen_cast(CType
*type
)
1788 int sbt
, dbt
, sf
, df
, c
, p
;
1790 /* special delayed cast for char/short */
1791 /* XXX: in some cases (multiple cascaded casts), it may still
1793 if (vtop
->r
& VT_MUSTCAST
) {
1794 vtop
->r
&= ~VT_MUSTCAST
;
1795 force_charshort_cast(vtop
->type
.t
);
1798 /* bitfields first get cast to ints */
1799 if (vtop
->type
.t
& VT_BITFIELD
) {
1803 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1804 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1809 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1810 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1812 /* constant case: we can do it now */
1813 /* XXX: in ISOC, cannot do it if error in convert */
1814 if (sbt
== VT_FLOAT
)
1815 vtop
->c
.ld
= vtop
->c
.f
;
1816 else if (sbt
== VT_DOUBLE
)
1817 vtop
->c
.ld
= vtop
->c
.d
;
1820 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1821 if (sbt
& VT_UNSIGNED
)
1822 vtop
->c
.ld
= vtop
->c
.ull
;
1824 vtop
->c
.ld
= vtop
->c
.ll
;
1826 if (sbt
& VT_UNSIGNED
)
1827 vtop
->c
.ld
= vtop
->c
.ui
;
1829 vtop
->c
.ld
= vtop
->c
.i
;
1832 if (dbt
== VT_FLOAT
)
1833 vtop
->c
.f
= (float)vtop
->c
.ld
;
1834 else if (dbt
== VT_DOUBLE
)
1835 vtop
->c
.d
= (double)vtop
->c
.ld
;
1836 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1837 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1838 } else if (sf
&& dbt
== VT_BOOL
) {
1839 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1842 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1843 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1844 vtop
->c
.ll
= vtop
->c
.ull
;
1845 else if (sbt
& VT_UNSIGNED
)
1846 vtop
->c
.ll
= vtop
->c
.ui
;
1847 #ifdef TCC_TARGET_X86_64
1848 else if (sbt
== VT_PTR
)
1851 else if (sbt
!= VT_LLONG
)
1852 vtop
->c
.ll
= vtop
->c
.i
;
1854 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1855 vtop
->c
.ull
= vtop
->c
.ll
;
1856 else if (dbt
== VT_BOOL
)
1857 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1858 else if (dbt
!= VT_LLONG
) {
1860 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1862 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1865 if(dbt
& VT_UNSIGNED
)
1866 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1868 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1871 } else if (p
&& dbt
== VT_BOOL
) {
1874 } else if (!nocode_wanted
) {
1875 /* non constant case: generate code */
1877 /* convert from fp to fp */
1880 /* convert int to fp */
1883 /* convert fp to int */
1884 if (dbt
== VT_BOOL
) {
1888 /* we handle char/short/etc... with generic code */
1889 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1890 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1894 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1895 /* additional cast for char/short... */
1900 #ifndef TCC_TARGET_X86_64
1901 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1902 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1903 /* scalar to long long */
1904 /* machine independent conversion */
1906 /* generate high word */
1907 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1911 if (sbt
== VT_PTR
) {
1912 /* cast from pointer to int before we apply
1913 shift operation, which pointers don't support*/
1914 gen_cast(&int_type
);
1920 /* patch second register */
1921 vtop
[-1].r2
= vtop
->r
;
1925 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1926 (dbt
& VT_BTYPE
) == VT_PTR
||
1927 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1928 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1929 (sbt
& VT_BTYPE
) != VT_PTR
&&
1930 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1931 /* need to convert from 32bit to 64bit */
1933 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1934 /* x86_64 specific: movslq */
1936 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1940 } else if (dbt
== VT_BOOL
) {
1941 /* scalar to bool */
1944 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1945 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1946 if (sbt
== VT_PTR
) {
1947 vtop
->type
.t
= VT_INT
;
1948 warning("nonportable conversion from pointer to char/short");
1950 force_charshort_cast(dbt
);
1951 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1953 if (sbt
== VT_LLONG
) {
1954 /* from long long: just take low order word */
1958 /* if lvalue and single word type, nothing to do because
1959 the lvalue already contains the real type size (see
1960 VT_LVAL_xxx constants) */
1963 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1964 /* if we are casting between pointer types,
1965 we must update the VT_LVAL_xxx size */
1966 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1967 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1972 /* return type size as known at compile time. Put alignment at 'a' */
1973 ST_FUNC
int type_size(CType
*type
, int *a
)
1978 bt
= type
->t
& VT_BTYPE
;
1979 if (bt
== VT_STRUCT
) {
1984 } else if (bt
== VT_PTR
) {
1985 if (type
->t
& VT_ARRAY
) {
1989 ts
= type_size(&s
->type
, a
);
1991 if (ts
< 0 && s
->c
< 0)
1999 } else if (bt
== VT_LDOUBLE
) {
2001 return LDOUBLE_SIZE
;
2002 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2003 #ifdef TCC_TARGET_I386
2004 #ifdef TCC_TARGET_PE
2009 #elif defined(TCC_TARGET_ARM)
2019 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2022 } else if (bt
== VT_SHORT
) {
2026 /* char, void, function, _Bool */
2032 /* push type size as known at runtime time on top of value stack. Put
2034 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2036 if (type
->t
& VT_VLA
) {
2037 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2039 vpushi(type_size(type
, a
));
2043 /* return the pointed type of t */
2044 static inline CType
*pointed_type(CType
*type
)
2046 return &type
->ref
->type
;
2049 /* modify type so that its it is a pointer to type. */
2050 ST_FUNC
void mk_pointer(CType
*type
)
2053 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2054 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2058 /* compare function types. OLD functions match any new functions */
2059 static int is_compatible_func(CType
*type1
, CType
*type2
)
2065 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2067 /* check func_call */
2068 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2070 /* XXX: not complete */
2071 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2075 while (s1
!= NULL
) {
2078 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2088 /* return true if type1 and type2 are the same. If unqualified is
2089 true, qualifiers on the types are ignored.
2091 - enums are not checked as gcc __builtin_types_compatible_p ()
2093 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2097 t1
= type1
->t
& VT_TYPE
;
2098 t2
= type2
->t
& VT_TYPE
;
2100 /* strip qualifiers before comparing */
2101 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2102 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2104 /* XXX: bitfields ? */
2107 /* test more complicated cases */
2108 bt1
= t1
& VT_BTYPE
;
2109 if (bt1
== VT_PTR
) {
2110 type1
= pointed_type(type1
);
2111 type2
= pointed_type(type2
);
2112 return is_compatible_types(type1
, type2
);
2113 } else if (bt1
== VT_STRUCT
) {
2114 return (type1
->ref
== type2
->ref
);
2115 } else if (bt1
== VT_FUNC
) {
2116 return is_compatible_func(type1
, type2
);
2122 /* return true if type1 and type2 are exactly the same (including
2125 static int is_compatible_types(CType
*type1
, CType
*type2
)
2127 return compare_types(type1
,type2
,0);
2130 /* return true if type1 and type2 are the same (ignoring qualifiers).
2132 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2134 return compare_types(type1
,type2
,1);
2137 /* print a type. If 'varstr' is not NULL, then the variable is also
2138 printed in the type */
2140 /* XXX: add array and function pointers */
2141 static void type_to_str(char *buf
, int buf_size
,
2142 CType
*type
, const char *varstr
)
2149 t
= type
->t
& VT_TYPE
;
2152 if (t
& VT_CONSTANT
)
2153 pstrcat(buf
, buf_size
, "const ");
2154 if (t
& VT_VOLATILE
)
2155 pstrcat(buf
, buf_size
, "volatile ");
2156 if (t
& VT_UNSIGNED
)
2157 pstrcat(buf
, buf_size
, "unsigned ");
2187 tstr
= "long double";
2189 pstrcat(buf
, buf_size
, tstr
);
2193 if (bt
== VT_STRUCT
)
2197 pstrcat(buf
, buf_size
, tstr
);
2198 v
= type
->ref
->v
& ~SYM_STRUCT
;
2199 if (v
>= SYM_FIRST_ANOM
)
2200 pstrcat(buf
, buf_size
, "<anonymous>");
2202 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2206 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2207 pstrcat(buf
, buf_size
, "(");
2209 while (sa
!= NULL
) {
2210 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2211 pstrcat(buf
, buf_size
, buf1
);
2214 pstrcat(buf
, buf_size
, ", ");
2216 pstrcat(buf
, buf_size
, ")");
2220 pstrcpy(buf1
, sizeof(buf1
), "*");
2222 pstrcat(buf1
, sizeof(buf1
), varstr
);
2223 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2227 pstrcat(buf
, buf_size
, " ");
2228 pstrcat(buf
, buf_size
, varstr
);
2233 /* verify type compatibility to store vtop in 'dt' type, and generate
2235 static void gen_assign_cast(CType
*dt
)
2237 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2238 char buf1
[256], buf2
[256];
2241 st
= &vtop
->type
; /* source type */
2242 dbt
= dt
->t
& VT_BTYPE
;
2243 sbt
= st
->t
& VT_BTYPE
;
2244 if (dt
->t
& VT_CONSTANT
)
2245 warning("assignment of read-only location");
2248 /* special cases for pointers */
2249 /* '0' can also be a pointer */
2250 if (is_null_pointer(vtop
))
2252 /* accept implicit pointer to integer cast with warning */
2253 if (is_integer_btype(sbt
)) {
2254 warning("assignment makes pointer from integer without a cast");
2257 type1
= pointed_type(dt
);
2258 /* a function is implicitely a function pointer */
2259 if (sbt
== VT_FUNC
) {
2260 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2261 !is_compatible_types(pointed_type(dt
), st
))
2262 warning("assignment from incompatible pointer type");
2267 type2
= pointed_type(st
);
2268 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2269 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2270 /* void * can match anything */
2272 /* exact type match, except for unsigned */
2275 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2276 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2277 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2278 warning("assignment from incompatible pointer type");
2280 /* check const and volatile */
2281 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2282 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2283 warning("assignment discards qualifiers from pointer target type");
2289 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2290 warning("assignment makes integer from pointer without a cast");
2292 /* XXX: more tests */
2297 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2298 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2299 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2301 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2302 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2303 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2311 /* store vtop in lvalue pushed on stack */
2312 ST_FUNC
void vstore(void)
2314 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2316 ft
= vtop
[-1].type
.t
;
2317 sbt
= vtop
->type
.t
& VT_BTYPE
;
2318 dbt
= ft
& VT_BTYPE
;
2319 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2320 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2321 /* optimize char/short casts */
2322 delayed_cast
= VT_MUSTCAST
;
2323 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2324 /* XXX: factorize */
2325 if (ft
& VT_CONSTANT
)
2326 warning("assignment of read-only location");
2329 if (!(ft
& VT_BITFIELD
))
2330 gen_assign_cast(&vtop
[-1].type
);
2333 if (sbt
== VT_STRUCT
) {
2334 /* if structure, only generate pointer */
2335 /* structure assignment : generate memcpy */
2336 /* XXX: optimize if small size */
2337 if (!nocode_wanted
) {
2338 size
= type_size(&vtop
->type
, &align
);
2342 vtop
->type
.t
= VT_PTR
;
2345 /* address of memcpy() */
2348 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2349 else if(!(align
& 3))
2350 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2353 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2358 vtop
->type
.t
= VT_PTR
;
2367 /* leave source on stack */
2368 } else if (ft
& VT_BITFIELD
) {
2369 /* bitfield store handling */
2370 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2371 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2372 /* remove bit field info to avoid loops */
2373 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2375 /* duplicate source into other register */
2380 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2381 gen_cast(&vtop
[-1].type
);
2382 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2385 /* duplicate destination */
2387 vtop
[-1] = vtop
[-2];
2389 /* mask and shift source */
2390 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2391 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2392 vpushll((1ULL << bit_size
) - 1ULL);
2394 vpushi((1 << bit_size
) - 1);
2400 /* load destination, mask and or with source */
2402 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2403 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2405 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2412 /* pop off shifted source from "duplicate source..." above */
2416 #ifdef CONFIG_TCC_BCHECK
2417 /* bound check case */
2418 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2424 if (!nocode_wanted
) {
2428 #ifdef TCC_TARGET_X86_64
2429 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2434 r
= gv(rc
); /* generate value */
2435 /* if lvalue was saved on stack, must read it */
2436 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2438 t
= get_reg(RC_INT
);
2439 #ifdef TCC_TARGET_X86_64
2444 sv
.r
= VT_LOCAL
| VT_LVAL
;
2445 sv
.c
.ul
= vtop
[-1].c
.ul
;
2447 vtop
[-1].r
= t
| VT_LVAL
;
2450 #ifndef TCC_TARGET_X86_64
2451 /* two word case handling : store second register at word + 4 */
2452 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2454 /* convert to int to increment easily */
2455 vtop
->type
.t
= VT_INT
;
2461 /* XXX: it works because r2 is spilled last ! */
2462 store(vtop
->r2
, vtop
- 1);
2467 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2468 vtop
->r
|= delayed_cast
;
2472 /* post defines POST/PRE add. c is the token ++ or -- */
2473 ST_FUNC
void inc(int post
, int c
)
2476 vdup(); /* save lvalue */
2478 gv_dup(); /* duplicate value */
2483 vpushi(c
- TOK_MID
);
2485 vstore(); /* store value */
2487 vpop(); /* if post op, return saved value */
2490 /* Parse GNUC __attribute__ extension. Currently, the following
2491 extensions are recognized:
2492 - aligned(n) : set data/function alignment.
2493 - packed : force data alignment to 1
2494 - section(x) : generate data/code in this section.
2495 - unused : currently ignored, but may be used someday.
2496 - regparm(n) : pass function parameters in registers (i386 only)
2498 static void parse_attribute(AttributeDef
*ad
)
2502 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2506 while (tok
!= ')') {
2507 if (tok
< TOK_IDENT
)
2508 expect("attribute name");
2516 expect("section name");
2517 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2525 expect("alias(\"target\")");
2526 ad
->alias_target
= /* save string as token, for later */
2527 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2536 if (n
<= 0 || (n
& (n
- 1)) != 0)
2537 error("alignment must be a positive power of two");
2554 /* currently, no need to handle it because tcc does not
2555 track unused objects */
2559 /* currently, no need to handle it because tcc does not
2560 track unused objects */
2565 ad
->func_call
= FUNC_CDECL
;
2570 ad
->func_call
= FUNC_STDCALL
;
2572 #ifdef TCC_TARGET_I386
2582 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2588 ad
->func_call
= FUNC_FASTCALLW
;
2595 ad
->mode
= VT_LLONG
+ 1;
2598 ad
->mode
= VT_SHORT
+ 1;
2601 ad
->mode
= VT_INT
+ 1;
2604 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2611 ad
->func_export
= 1;
2614 ad
->func_import
= 1;
2617 if (tcc_state
->warn_unsupported
)
2618 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2619 /* skip parameters */
2621 int parenthesis
= 0;
2625 else if (tok
== ')')
2628 } while (parenthesis
&& tok
!= -1);
2641 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2642 static void struct_decl(CType
*type
, int u
)
2644 int a
, v
, size
, align
, maxalign
, c
, offset
;
2645 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2646 Sym
*s
, *ss
, *ass
, **ps
;
2650 a
= tok
; /* save decl type */
2655 /* struct already defined ? return it */
2657 expect("struct/union/enum name");
2661 error("invalid type");
2668 /* we put an undefined size for struct/union */
2669 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2670 s
->r
= 0; /* default alignment is zero as gcc */
2671 /* put struct/union/enum name in type */
2679 error("struct/union/enum already defined");
2680 /* cannot be empty */
2682 /* non empty enums are not allowed */
2683 if (a
== TOK_ENUM
) {
2687 expect("identifier");
2693 /* enum symbols have static storage */
2694 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2695 ss
->type
.t
|= VT_STATIC
;
2700 /* NOTE: we accept a trailing comma */
2711 while (tok
!= '}') {
2712 parse_btype(&btype
, &ad
);
2718 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2719 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2720 expect("identifier");
2721 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2722 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2723 error("invalid type for '%s'",
2724 get_tok_str(v
, NULL
));
2728 bit_size
= expr_const();
2729 /* XXX: handle v = 0 case for messages */
2731 error("negative width in bit-field '%s'",
2732 get_tok_str(v
, NULL
));
2733 if (v
&& bit_size
== 0)
2734 error("zero width for bit-field '%s'",
2735 get_tok_str(v
, NULL
));
2737 size
= type_size(&type1
, &align
);
2739 if (align
< ad
.aligned
)
2741 } else if (ad
.packed
) {
2743 } else if (*tcc_state
->pack_stack_ptr
) {
2744 if (align
> *tcc_state
->pack_stack_ptr
)
2745 align
= *tcc_state
->pack_stack_ptr
;
2748 if (bit_size
>= 0) {
2749 bt
= type1
.t
& VT_BTYPE
;
2756 error("bitfields must have scalar type");
2758 if (bit_size
> bsize
) {
2759 error("width of '%s' exceeds its type",
2760 get_tok_str(v
, NULL
));
2761 } else if (bit_size
== bsize
) {
2762 /* no need for bit fields */
2764 } else if (bit_size
== 0) {
2765 /* XXX: what to do if only padding in a
2767 /* zero size: means to pad */
2770 /* we do not have enough room ?
2771 did the type change?
2773 if ((bit_pos
+ bit_size
) > bsize
||
2774 bt
!= prevbt
|| a
== TOK_UNION
)
2777 /* XXX: handle LSB first */
2778 type1
.t
|= VT_BITFIELD
|
2779 (bit_pos
<< VT_STRUCT_SHIFT
) |
2780 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2781 bit_pos
+= bit_size
;
2787 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2788 /* add new memory data only if starting
2790 if (lbit_pos
== 0) {
2791 if (a
== TOK_STRUCT
) {
2792 c
= (c
+ align
- 1) & -align
;
2801 if (align
> maxalign
)
2805 printf("add field %s offset=%d",
2806 get_tok_str(v
, NULL
), offset
);
2807 if (type1
.t
& VT_BITFIELD
) {
2808 printf(" pos=%d size=%d",
2809 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2810 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2815 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2817 while ((ass
= ass
->next
) != NULL
) {
2818 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2823 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2827 if (tok
== ';' || tok
== TOK_EOF
)
2834 /* store size and alignment */
2835 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2841 /* return 0 if no type declaration. otherwise, return the basic type
2844 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2846 int t
, u
, type_found
, typespec_found
, typedef_found
;
2850 memset(ad
, 0, sizeof(AttributeDef
));
2858 /* currently, we really ignore extension */
2868 if ((t
& VT_BTYPE
) != 0)
2869 error("too many basic types");
2885 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2886 #ifndef TCC_TARGET_PE
2887 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2889 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2890 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2904 if ((t
& VT_BTYPE
) == VT_LONG
) {
2905 #ifdef TCC_TARGET_PE
2906 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2908 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2916 struct_decl(&type1
, VT_ENUM
);
2919 type
->ref
= type1
.ref
;
2923 struct_decl(&type1
, VT_STRUCT
);
2926 /* type modifiers */
2979 /* GNUC attribute */
2980 case TOK_ATTRIBUTE1
:
2981 case TOK_ATTRIBUTE2
:
2982 parse_attribute(ad
);
2985 t
= (t
& ~VT_BTYPE
) | u
;
2993 parse_expr_type(&type1
);
2994 /* remove all storage modifiers except typedef */
2995 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
2998 if (typespec_found
|| typedef_found
)
3001 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3004 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3005 type
->ref
= s
->type
.ref
;
3007 /* get attributes from typedef */
3008 if (0 == ad
->aligned
)
3009 ad
->aligned
= FUNC_ALIGN(s
->r
);
3010 if (0 == ad
->func_call
)
3011 ad
->func_call
= FUNC_CALL(s
->r
);
3012 ad
->packed
|= FUNC_PACKED(s
->r
);
3021 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3022 error("signed and unsigned modifier");
3023 if (tcc_state
->char_is_unsigned
) {
3024 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3029 /* long is never used as type */
3030 if ((t
& VT_BTYPE
) == VT_LONG
)
3031 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3032 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3034 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3040 /* convert a function parameter type (array to pointer and function to
3041 function pointer) */
3042 static inline void convert_parameter_type(CType
*pt
)
3044 /* remove const and volatile qualifiers (XXX: const could be used
3045 to indicate a const function parameter */
3046 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3047 /* array must be transformed to pointer according to ANSI C */
3049 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3054 ST_FUNC
void parse_asm_str(CString
*astr
)
3057 /* read the string */
3059 expect("string constant");
3061 while (tok
== TOK_STR
) {
3062 /* XXX: add \0 handling too ? */
3063 cstr_cat(astr
, tokc
.cstr
->data
);
3066 cstr_ccat(astr
, '\0');
3069 /* Parse an asm label and return the label
3070 * Don't forget to free the CString in the caller! */
3071 static void asm_label_instr(CString
*astr
)
3074 parse_asm_str(astr
);
3077 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3081 static void post_type(CType
*type
, AttributeDef
*ad
)
3083 int n
, l
, t1
, arg_size
, align
;
3084 Sym
**plast
, *s
, *first
;
3089 /* function declaration */
3097 /* read param name and compute offset */
3098 if (l
!= FUNC_OLD
) {
3099 if (!parse_btype(&pt
, &ad1
)) {
3101 error("invalid type");
3108 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3110 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3111 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3112 error("parameter declared as void");
3113 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3118 expect("identifier");
3122 convert_parameter_type(&pt
);
3123 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3129 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3136 /* if no parameters, then old type prototype */
3140 /* NOTE: const is ignored in returned type as it has a special
3141 meaning in gcc / C++ */
3142 type
->t
&= ~VT_CONSTANT
;
3143 /* some ancient pre-K&R C allows a function to return an array
3144 and the array brackets to be put after the arguments, such
3145 that "int c()[]" means something like "int[] c()" */
3148 skip(']'); /* only handle simple "[]" */
3151 /* we push a anonymous symbol which will contain the function prototype */
3152 ad
->func_args
= arg_size
;
3153 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3157 } else if (tok
== '[') {
3158 /* array definition */
3160 if (tok
== TOK_RESTRICT1
)
3166 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3169 error("invalid array size");
3170 } else if (!local_stack
) {
3171 error("expected constant expression (variably modified array at file scope)");
3173 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3174 error("size of variable length array should be an integer");
3179 /* parse next post type */
3180 post_type(type
, ad
);
3181 t1
|= type
->t
& VT_VLA
;
3184 loc
-= type_size(&int_type
, &align
);
3188 vla_runtime_type_size(type
, &align
);
3190 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3197 /* we push an anonymous symbol which will contain the array
3199 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3200 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3205 /* Parse a type declaration (except basic type), and return the type
3206 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3207 expected. 'type' should contain the basic type. 'ad' is the
3208 attribute definition of the basic type. It can be modified by
3211 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3214 CType type1
, *type2
;
3215 int qualifiers
, storage
;
3217 while (tok
== '*') {
3225 qualifiers
|= VT_CONSTANT
;
3230 qualifiers
|= VT_VOLATILE
;
3238 type
->t
|= qualifiers
;
3241 /* XXX: clarify attribute handling */
3242 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3243 parse_attribute(ad
);
3245 /* recursive type */
3246 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3247 type1
.t
= 0; /* XXX: same as int */
3250 /* XXX: this is not correct to modify 'ad' at this point, but
3251 the syntax is not clear */
3252 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3253 parse_attribute(ad
);
3254 type_decl(&type1
, ad
, v
, td
);
3257 /* type identifier */
3258 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3262 if (!(td
& TYPE_ABSTRACT
))
3263 expect("identifier");
3267 storage
= type
->t
& VT_STORAGE
;
3268 type
->t
&= ~VT_STORAGE
;
3269 post_type(type
, ad
);
3271 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3272 parse_attribute(ad
);
3276 /* append type at the end of type1 */
3289 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3290 ST_FUNC
int lvalue_type(int t
)
3295 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3297 else if (bt
== VT_SHORT
)
3301 if (t
& VT_UNSIGNED
)
3302 r
|= VT_LVAL_UNSIGNED
;
3306 /* indirection with full error checking and bound check */
3307 ST_FUNC
void indir(void)
3309 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3310 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3314 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3316 vtop
->type
= *pointed_type(&vtop
->type
);
3317 /* Arrays and functions are never lvalues */
3318 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3319 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3320 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3321 /* if bound checking, the referenced pointer must be checked */
3322 #ifdef CONFIG_TCC_BCHECK
3323 if (tcc_state
->do_bounds_check
)
3324 vtop
->r
|= VT_MUSTBOUND
;
3329 /* pass a parameter to a function and do type checking and casting */
3330 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3335 func_type
= func
->c
;
3336 if (func_type
== FUNC_OLD
||
3337 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3338 /* default casting : only need to convert float to double */
3339 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3343 } else if (arg
== NULL
) {
3344 error("too many arguments to function");
3347 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3348 gen_assign_cast(&type
);
3352 /* parse an expression of the form '(type)' or '(expr)' and return its
3354 static void parse_expr_type(CType
*type
)
3360 if (parse_btype(type
, &ad
)) {
3361 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3368 static void parse_type(CType
*type
)
3373 if (!parse_btype(type
, &ad
)) {
3376 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3379 static void vpush_tokc(int t
)
3384 vsetc(&type
, VT_CONST
, &tokc
);
3387 ST_FUNC
void unary(void)
3389 int n
, t
, align
, size
, r
, sizeof_caller
;
3393 static int in_sizeof
= 0;
3395 sizeof_caller
= in_sizeof
;
3397 /* XXX: GCC 2.95.3 does not generate a table although it should be
3411 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3415 vpush_tokc(VT_LLONG
);
3419 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3423 vpush_tokc(VT_FLOAT
);
3427 vpush_tokc(VT_DOUBLE
);
3431 vpush_tokc(VT_LDOUBLE
);
3434 case TOK___FUNCTION__
:
3436 goto tok_identifier
;
3442 /* special function name identifier */
3443 len
= strlen(funcname
) + 1;
3444 /* generate char[len] type */
3449 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3450 ptr
= section_ptr_add(data_section
, len
);
3451 memcpy(ptr
, funcname
, len
);
3456 #ifdef TCC_TARGET_PE
3457 t
= VT_SHORT
| VT_UNSIGNED
;
3463 /* string parsing */
3466 if (tcc_state
->warn_write_strings
)
3471 memset(&ad
, 0, sizeof(AttributeDef
));
3472 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3477 if (parse_btype(&type
, &ad
)) {
3478 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3480 /* check ISOC99 compound literal */
3482 /* data is allocated locally by default */
3487 /* all except arrays are lvalues */
3488 if (!(type
.t
& VT_ARRAY
))
3489 r
|= lvalue_type(type
.t
);
3490 memset(&ad
, 0, sizeof(AttributeDef
));
3491 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3493 if (sizeof_caller
) {
3500 } else if (tok
== '{') {
3501 /* save all registers */
3503 /* statement expression : we do not accept break/continue
3504 inside as GCC does */
3505 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3520 /* functions names must be treated as function pointers,
3521 except for unary '&' and sizeof. Since we consider that
3522 functions are not lvalues, we only have to handle it
3523 there and in function calls. */
3524 /* arrays can also be used although they are not lvalues */
3525 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3526 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3528 mk_pointer(&vtop
->type
);
3534 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3536 boolean
.t
= VT_BOOL
;
3538 vtop
->c
.i
= !vtop
->c
.i
;
3539 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3540 vtop
->c
.i
= vtop
->c
.i
^ 1;
3543 vseti(VT_JMP
, gtst(1, 0));
3554 /* in order to force cast, we add zero */
3556 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3557 error("pointer not accepted for unary plus");
3567 unary_type(&type
); // Perform a in_sizeof = 0;
3568 size
= type_size(&type
, &align
);
3569 if (t
== TOK_SIZEOF
) {
3570 if (!(type
.t
& VT_VLA
)) {
3572 error("sizeof applied to an incomplete type");
3575 vla_runtime_type_size(&type
, &align
);
3580 vtop
->type
.t
|= VT_UNSIGNED
;
3583 case TOK_builtin_types_compatible_p
:
3592 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3593 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3594 vpushi(is_compatible_types(&type1
, &type2
));
3597 case TOK_builtin_constant_p
:
3599 int saved_nocode_wanted
, res
;
3602 saved_nocode_wanted
= nocode_wanted
;
3605 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3607 nocode_wanted
= saved_nocode_wanted
;
3612 case TOK_builtin_frame_address
:
3617 if (tok
!= TOK_CINT
) {
3618 error("__builtin_frame_address only takes integers");
3621 error("TCC only supports __builtin_frame_address(0)");
3627 vset(&type
, VT_LOCAL
, 0);
3630 #ifdef TCC_TARGET_X86_64
3631 case TOK_builtin_va_arg_types
:
3633 /* This definition must be synced with stdarg.h */
3634 enum __va_arg_type
{
3635 __va_gen_reg
, __va_float_reg
, __va_stack
3643 bt
= type
.t
& VT_BTYPE
;
3644 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3646 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3647 vpushi(__va_float_reg
);
3649 vpushi(__va_gen_reg
);
3669 goto tok_identifier
;
3671 /* allow to take the address of a label */
3672 if (tok
< TOK_UIDENT
)
3673 expect("label identifier");
3674 s
= label_find(tok
);
3676 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3678 if (s
->r
== LABEL_DECLARED
)
3679 s
->r
= LABEL_FORWARD
;
3682 s
->type
.t
= VT_VOID
;
3683 mk_pointer(&s
->type
);
3684 s
->type
.t
|= VT_STATIC
;
3686 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3691 // special qnan , snan and infinity values
3693 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3697 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3701 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3710 expect("identifier");
3714 error("'%s' undeclared", get_tok_str(t
, NULL
));
3715 /* for simple function calls, we tolerate undeclared
3716 external reference to int() function */
3717 if (tcc_state
->warn_implicit_function_declaration
)
3718 warning("implicit declaration of function '%s'",
3719 get_tok_str(t
, NULL
));
3720 s
= external_global_sym(t
, &func_old_type
, 0);
3722 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3723 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3724 /* if referencing an inline function, then we generate a
3725 symbol to it if not already done. It will have the
3726 effect to generate code for it at the end of the
3727 compilation unit. Inline function as always
3728 generated in the text section. */
3730 put_extern_sym(s
, text_section
, 0, 0);
3731 r
= VT_SYM
| VT_CONST
;
3735 vset(&s
->type
, r
, s
->c
);
3736 /* if forward reference, we must point to s */
3737 if (vtop
->r
& VT_SYM
) {
3744 /* post operations */
3746 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3749 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3752 if (tok
== TOK_ARROW
)
3754 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3758 /* expect pointer on structure */
3759 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3760 expect("struct or union");
3764 while ((s
= s
->next
) != NULL
) {
3769 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3770 /* add field offset to pointer */
3771 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3774 /* change type to field type, and set to lvalue */
3775 vtop
->type
= s
->type
;
3776 vtop
->type
.t
|= qualifiers
;
3777 /* an array is never an lvalue */
3778 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3779 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3780 #ifdef CONFIG_TCC_BCHECK
3781 /* if bound checking, the referenced pointer must be checked */
3782 if (tcc_state
->do_bounds_check
)
3783 vtop
->r
|= VT_MUSTBOUND
;
3787 } else if (tok
== '[') {
3793 } else if (tok
== '(') {
3799 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3800 /* pointer test (no array accepted) */
3801 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3802 vtop
->type
= *pointed_type(&vtop
->type
);
3803 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3807 expect("function pointer");
3810 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3812 /* get return type */
3815 sa
= s
->next
; /* first parameter */
3818 /* compute first implicit argument if a structure is returned */
3819 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3820 /* get some space for the returned structure */
3821 size
= type_size(&s
->type
, &align
);
3822 loc
= (loc
- size
) & -align
;
3824 ret
.r
= VT_LOCAL
| VT_LVAL
;
3825 /* pass it as 'int' to avoid structure arg passing
3827 vseti(VT_LOCAL
, loc
);
3832 /* return in register */
3833 if (is_float(ret
.type
.t
)) {
3834 ret
.r
= reg_fret(ret
.type
.t
);
3836 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3845 gfunc_param_typed(s
, sa
);
3855 error("too few arguments to function");
3857 if (!nocode_wanted
) {
3858 gfunc_call(nb_args
);
3860 vtop
-= (nb_args
+ 1);
3863 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3871 ST_FUNC
void expr_prod(void)
3876 while (tok
== '*' || tok
== '/' || tok
== '%') {
3884 ST_FUNC
void expr_sum(void)
3889 while (tok
== '+' || tok
== '-') {
3897 static void expr_shift(void)
3902 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3910 static void expr_cmp(void)
3915 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3916 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3924 static void expr_cmpeq(void)
3929 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3937 static void expr_and(void)
3940 while (tok
== '&') {
3947 static void expr_xor(void)
3950 while (tok
== '^') {
3957 static void expr_or(void)
3960 while (tok
== '|') {
3967 /* XXX: fix this mess */
3968 static void expr_land_const(void)
3971 while (tok
== TOK_LAND
) {
3978 /* XXX: fix this mess */
3979 static void expr_lor_const(void)
3982 while (tok
== TOK_LOR
) {
3989 /* only used if non constant */
3990 static void expr_land(void)
3995 if (tok
== TOK_LAND
) {
4000 if (tok
!= TOK_LAND
) {
4010 static void expr_lor(void)
4015 if (tok
== TOK_LOR
) {
4020 if (tok
!= TOK_LOR
) {
4030 /* XXX: better constant handling */
4031 static void expr_cond(void)
4033 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4035 CType type
, type1
, type2
;
4042 boolean
.t
= VT_BOOL
;
4048 if (tok
!= ':' || !gnu_ext
) {
4063 if (vtop
!= vstack
) {
4064 /* needed to avoid having different registers saved in
4066 if (is_float(vtop
->type
.t
)) {
4068 #ifdef TCC_TARGET_X86_64
4069 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4079 if (tok
== ':' && gnu_ext
) {
4087 sv
= *vtop
; /* save value to handle it later */
4088 vtop
--; /* no vpop so that FP stack is not flushed */
4096 bt1
= t1
& VT_BTYPE
;
4098 bt2
= t2
& VT_BTYPE
;
4099 /* cast operands to correct type according to ISOC rules */
4100 if (is_float(bt1
) || is_float(bt2
)) {
4101 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4102 type
.t
= VT_LDOUBLE
;
4103 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4108 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4109 /* cast to biggest op */
4111 /* convert to unsigned if it does not fit in a long long */
4112 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4113 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4114 type
.t
|= VT_UNSIGNED
;
4115 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4116 /* XXX: test pointer compatibility */
4118 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4119 /* XXX: test function pointer compatibility */
4121 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4122 /* XXX: test structure compatibility */
4124 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4125 /* NOTE: as an extension, we accept void on only one side */
4128 /* integer operations */
4130 /* convert to unsigned if it does not fit in an integer */
4131 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4132 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4133 type
.t
|= VT_UNSIGNED
;
4136 /* now we convert second operand */
4138 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4141 if (is_float(type
.t
)) {
4143 #ifdef TCC_TARGET_X86_64
4144 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4148 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4149 /* for long longs, we use fixed registers to avoid having
4150 to handle a complicated move */
4155 /* this is horrible, but we must also convert first
4159 /* put again first value and cast it */
4162 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4172 static void expr_eq(void)
4178 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4179 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4180 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4195 ST_FUNC
void gexpr(void)
4206 /* parse an expression and return its type without any side effect. */
4207 static void expr_type(CType
*type
)
4209 int saved_nocode_wanted
;
4211 saved_nocode_wanted
= nocode_wanted
;
4216 nocode_wanted
= saved_nocode_wanted
;
4219 /* parse a unary expression and return its type without any side
4221 static void unary_type(CType
*type
)
4233 /* parse a constant expression and return value in vtop. */
4234 static void expr_const1(void)
4243 /* parse an integer constant and return its value. */
4244 ST_FUNC
int expr_const(void)
4248 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4249 expect("constant expression");
4255 /* return the label token if current token is a label, otherwise
4257 static int is_label(void)
4261 /* fast test first */
4262 if (tok
< TOK_UIDENT
)
4264 /* no need to save tokc because tok is an identifier */
4271 unget_tok(last_tok
);
4276 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4277 int case_reg
, int is_expr
)
4282 /* generate line number info */
4283 if (tcc_state
->do_debug
&&
4284 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4285 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4287 last_line_num
= file
->line_num
;
4291 /* default return value is (void) */
4293 vtop
->type
.t
= VT_VOID
;
4296 if (tok
== TOK_IF
) {
4303 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4305 if (c
== TOK_ELSE
) {
4309 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4310 gsym(d
); /* patch else jmp */
4313 } else if (tok
== TOK_WHILE
) {
4321 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4325 } else if (tok
== '{') {
4329 /* record local declaration stack position */
4331 llabel
= local_label_stack
;
4332 /* handle local labels declarations */
4333 if (tok
== TOK_LABEL
) {
4336 if (tok
< TOK_UIDENT
)
4337 expect("label identifier");
4338 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4348 while (tok
!= '}') {
4353 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4356 /* pop locally defined labels */
4357 label_pop(&local_label_stack
, llabel
);
4359 /* XXX: this solution makes only valgrind happy...
4360 triggered by gcc.c-torture/execute/20000917-1.c */
4362 switch(vtop
->type
.t
& VT_BTYPE
) {
4367 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4369 error("unsupported expression type");
4372 /* pop locally defined symbols */
4373 sym_pop(&local_stack
, s
);
4375 } else if (tok
== TOK_RETURN
) {
4379 gen_assign_cast(&func_vt
);
4380 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4382 /* if returning structure, must copy it to implicit
4383 first pointer arg location */
4386 size
= type_size(&func_vt
,&align
);
4389 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4393 loc
= (loc
- size
) & -4;
4396 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4399 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4401 vtop
->type
= int_type
;
4407 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4410 /* copy structure value to pointer */
4415 } else if (is_float(func_vt
.t
)) {
4416 gv(rc_fret(func_vt
.t
));
4420 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4423 rsym
= gjmp(rsym
); /* jmp */
4424 } else if (tok
== TOK_BREAK
) {
4427 error("cannot break");
4428 *bsym
= gjmp(*bsym
);
4431 } else if (tok
== TOK_CONTINUE
) {
4434 error("cannot continue");
4435 *csym
= gjmp(*csym
);
4438 } else if (tok
== TOK_FOR
) {
4444 /* c99 for-loop init decl? */
4445 if (!decl0(VT_LOCAL
, 1)) {
4446 /* no, regular for-loop init expr */
4470 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4474 sym_pop(&local_stack
, s
);
4476 if (tok
== TOK_DO
) {
4481 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4492 if (tok
== TOK_SWITCH
) {
4496 /* XXX: other types than integer */
4497 case_reg
= gv(RC_INT
);
4501 b
= gjmp(0); /* jump to first case */
4503 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4504 /* if no default, jmp after switch */
4512 if (tok
== TOK_CASE
) {
4519 if (gnu_ext
&& tok
== TOK_DOTS
) {
4523 warning("empty case range");
4525 /* since a case is like a label, we must skip it with a jmp */
4532 *case_sym
= gtst(1, 0);
4535 *case_sym
= gtst(1, 0);
4539 *case_sym
= gtst(1, *case_sym
);
4544 goto block_after_label
;
4546 if (tok
== TOK_DEFAULT
) {
4552 error("too many 'default'");
4555 goto block_after_label
;
4557 if (tok
== TOK_GOTO
) {
4559 if (tok
== '*' && gnu_ext
) {
4563 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4566 } else if (tok
>= TOK_UIDENT
) {
4567 s
= label_find(tok
);
4568 /* put forward definition if needed */
4570 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4572 if (s
->r
== LABEL_DECLARED
)
4573 s
->r
= LABEL_FORWARD
;
4575 /* label already defined */
4576 if (s
->r
& LABEL_FORWARD
)
4577 s
->jnext
= gjmp(s
->jnext
);
4579 gjmp_addr(s
->jnext
);
4582 expect("label identifier");
4585 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4593 if (s
->r
== LABEL_DEFINED
)
4594 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4596 s
->r
= LABEL_DEFINED
;
4598 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4601 /* we accept this, but it is a mistake */
4604 warning("deprecated use of label at end of compound statement");
4608 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4611 /* expression case */
4626 /* t is the array or struct type. c is the array or struct
4627 address. cur_index/cur_field is the pointer to the current
4628 value. 'size_only' is true if only size info is needed (only used
4630 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4631 int *cur_index
, Sym
**cur_field
,
4635 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4641 if (gnu_ext
&& (l
= is_label()) != 0)
4643 while (tok
== '[' || tok
== '.') {
4645 if (!(type
->t
& VT_ARRAY
))
4646 expect("array type");
4649 index
= expr_const();
4650 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4651 expect("invalid index");
4652 if (tok
== TOK_DOTS
&& gnu_ext
) {
4654 index_last
= expr_const();
4655 if (index_last
< 0 ||
4656 (s
->c
>= 0 && index_last
>= s
->c
) ||
4658 expect("invalid index");
4664 *cur_index
= index_last
;
4665 type
= pointed_type(type
);
4666 elem_size
= type_size(type
, &align
);
4667 c
+= index
* elem_size
;
4668 /* NOTE: we only support ranges for last designator */
4669 nb_elems
= index_last
- index
+ 1;
4670 if (nb_elems
!= 1) {
4679 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4680 expect("struct/union type");
4693 /* XXX: fix this mess by using explicit storage field */
4695 type1
.t
|= (type
->t
& ~VT_TYPE
);
4709 if (type
->t
& VT_ARRAY
) {
4711 type
= pointed_type(type
);
4712 c
+= index
* type_size(type
, &align
);
4716 error("too many field init");
4717 /* XXX: fix this mess by using explicit storage field */
4719 type1
.t
|= (type
->t
& ~VT_TYPE
);
4724 decl_initializer(type
, sec
, c
, 0, size_only
);
4726 /* XXX: make it more general */
4727 if (!size_only
&& nb_elems
> 1) {
4728 unsigned long c_end
;
4733 error("range init not supported yet for dynamic storage");
4734 c_end
= c
+ nb_elems
* elem_size
;
4735 if (c_end
> sec
->data_allocated
)
4736 section_realloc(sec
, c_end
);
4737 src
= sec
->data
+ c
;
4739 for(i
= 1; i
< nb_elems
; i
++) {
4741 memcpy(dst
, src
, elem_size
);
4747 #define EXPR_CONST 1
4750 /* store a value or an expression directly in global data or in local array */
4751 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4752 int v
, int expr_type
)
4754 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4756 unsigned long long bit_mask
;
4764 /* compound literals must be allocated globally in this case */
4765 saved_global_expr
= global_expr
;
4768 global_expr
= saved_global_expr
;
4769 /* NOTE: symbols are accepted */
4770 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4771 error("initializer element is not constant");
4779 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4782 /* XXX: not portable */
4783 /* XXX: generate error if incorrect relocation */
4784 gen_assign_cast(&dtype
);
4785 bt
= type
->t
& VT_BTYPE
;
4786 /* we'll write at most 12 bytes */
4787 if (c
+ 12 > sec
->data_allocated
) {
4788 section_realloc(sec
, c
+ 12);
4790 ptr
= sec
->data
+ c
;
4791 /* XXX: make code faster ? */
4792 if (!(type
->t
& VT_BITFIELD
)) {
4797 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4798 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4799 bit_mask
= (1LL << bit_size
) - 1;
4801 if ((vtop
->r
& VT_SYM
) &&
4807 (bt
== VT_INT
&& bit_size
!= 32)))
4808 error("initializer element is not computable at load time");
4811 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4813 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4816 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4819 *(double *)ptr
= vtop
->c
.d
;
4822 *(long double *)ptr
= vtop
->c
.ld
;
4825 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4828 if (vtop
->r
& VT_SYM
) {
4829 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4831 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4836 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4843 /* put zeros for variable based init */
4844 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4847 /* nothing to do because globals are already set to zero */
4849 vpush_global_sym(&func_old_type
, TOK_memset
);
4857 /* 't' contains the type and storage info. 'c' is the offset of the
4858 object in section 'sec'. If 'sec' is NULL, it means stack based
4859 allocation. 'first' is true if array '{' must be read (multi
4860 dimension implicit array init handling). 'size_only' is true if
4861 size only evaluation is wanted (only for arrays). */
4862 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4863 int first
, int size_only
)
4865 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4866 int size1
, align1
, expr_type
;
4870 if (type
->t
& VT_VLA
) {
4871 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4875 vpush_global_sym(&func_old_type
, TOK_alloca
);
4876 vla_runtime_type_size(type
, &a
);
4881 vsetc(type
, REG_IRET
, &retcval
);
4882 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
4887 error("variable length arrays unsupported for this target");
4889 } else if (type
->t
& VT_ARRAY
) {
4893 t1
= pointed_type(type
);
4894 size1
= type_size(t1
, &align1
);
4897 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4900 error("character array initializer must be a literal,"
4901 " optionally enclosed in braces");
4906 /* only parse strings here if correct type (otherwise: handle
4907 them as ((w)char *) expressions */
4908 if ((tok
== TOK_LSTR
&&
4909 #ifdef TCC_TARGET_PE
4910 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4912 (t1
->t
& VT_BTYPE
) == VT_INT
4914 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4915 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4920 /* compute maximum number of chars wanted */
4922 cstr_len
= cstr
->size
;
4924 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4927 if (n
>= 0 && nb
> (n
- array_length
))
4928 nb
= n
- array_length
;
4931 warning("initializer-string for array is too long");
4932 /* in order to go faster for common case (char
4933 string in global variable, we handle it
4935 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4936 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4940 ch
= ((unsigned char *)cstr
->data
)[i
];
4942 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4943 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4951 /* only add trailing zero if enough storage (no
4952 warning in this case since it is standard) */
4953 if (n
< 0 || array_length
< n
) {
4955 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4961 while (tok
!= '}') {
4962 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4963 if (n
>= 0 && index
>= n
)
4964 error("index too large");
4965 /* must put zero in holes (note that doing it that way
4966 ensures that it even works with designators) */
4967 if (!size_only
&& array_length
< index
) {
4968 init_putz(t1
, sec
, c
+ array_length
* size1
,
4969 (index
- array_length
) * size1
);
4972 if (index
> array_length
)
4973 array_length
= index
;
4974 /* special test for multi dimensional arrays (may not
4975 be strictly correct if designators are used at the
4977 if (index
>= n
&& no_oblock
)
4986 /* put zeros at the end */
4987 if (!size_only
&& n
>= 0 && array_length
< n
) {
4988 init_putz(t1
, sec
, c
+ array_length
* size1
,
4989 (n
- array_length
) * size1
);
4991 /* patch type size if needed */
4993 s
->c
= array_length
;
4994 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4995 (sec
|| !first
|| tok
== '{')) {
4998 /* NOTE: the previous test is a specific case for automatic
4999 struct/union init */
5000 /* XXX: union needs only one init */
5002 /* XXX: this test is incorrect for local initializers
5003 beginning with ( without {. It would be much more difficult
5004 to do it correctly (ideally, the expression parser should
5005 be used in all cases) */
5011 while (tok
== '(') {
5015 if (!parse_btype(&type1
, &ad1
))
5017 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5019 if (!is_assignable_types(type
, &type1
))
5020 error("invalid type for cast");
5025 if (first
|| tok
== '{') {
5034 while (tok
!= '}') {
5035 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5037 if (!size_only
&& array_length
< index
) {
5038 init_putz(type
, sec
, c
+ array_length
,
5039 index
- array_length
);
5041 index
= index
+ type_size(&f
->type
, &align1
);
5042 if (index
> array_length
)
5043 array_length
= index
;
5045 /* gr: skip fields from same union - ugly. */
5047 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5048 /* test for same offset */
5049 if (f
->next
->c
!= f
->c
)
5051 /* if yes, test for bitfield shift */
5052 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5053 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5054 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5055 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5056 if (bit_pos_1
!= bit_pos_2
)
5063 if (no_oblock
&& f
== NULL
)
5069 /* put zeros at the end */
5070 if (!size_only
&& array_length
< n
) {
5071 init_putz(type
, sec
, c
+ array_length
,
5080 } else if (tok
== '{') {
5082 decl_initializer(type
, sec
, c
, first
, size_only
);
5084 } else if (size_only
) {
5085 /* just skip expression */
5086 parlevel
= parlevel1
= 0;
5087 while ((parlevel
> 0 || parlevel1
> 0 ||
5088 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5091 else if (tok
== ')')
5093 else if (tok
== '{')
5095 else if (tok
== '}')
5100 /* currently, we always use constant expression for globals
5101 (may change for scripting case) */
5102 expr_type
= EXPR_CONST
;
5104 expr_type
= EXPR_ANY
;
5105 init_putv(type
, sec
, c
, 0, expr_type
);
5109 /* parse an initializer for type 't' if 'has_init' is non zero, and
5110 allocate space in local or global data space ('r' is either
5111 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5112 variable 'v' with an associated name represented by 'asm_label' of
5113 scope 'scope' is declared before initializers are parsed. If 'v' is
5114 zero, then a reference to the new object is put in the value stack.
5115 If 'has_init' is 2, a special parsing is done to handle string
5117 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5118 int has_init
, int v
, char *asm_label
,
5121 int size
, align
, addr
, data_offset
;
5123 ParseState saved_parse_state
= {0};
5124 TokenString init_str
;
5126 Sym
*flexible_array
;
5128 flexible_array
= NULL
;
5129 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5132 while (field
&& field
->next
)
5133 field
= field
->next
;
5134 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5135 flexible_array
= field
;
5138 size
= type_size(type
, &align
);
5139 /* If unknown size, we must evaluate it before
5140 evaluating initializers because
5141 initializers can generate global data too
5142 (e.g. string pointers or ISOC99 compound
5143 literals). It also simplifies local
5144 initializers handling */
5145 tok_str_new(&init_str
);
5146 if (size
< 0 || flexible_array
) {
5148 error("unknown type size");
5149 /* get all init string */
5150 if (has_init
== 2) {
5151 /* only get strings */
5152 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5153 tok_str_add_tok(&init_str
);
5158 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5160 error("unexpected end of file in initializer");
5161 tok_str_add_tok(&init_str
);
5164 else if (tok
== '}') {
5174 tok_str_add(&init_str
, -1);
5175 tok_str_add(&init_str
, 0);
5178 save_parse_state(&saved_parse_state
);
5180 macro_ptr
= init_str
.str
;
5182 decl_initializer(type
, NULL
, 0, 1, 1);
5183 /* prepare second initializer parsing */
5184 macro_ptr
= init_str
.str
;
5187 /* if still unknown size, error */
5188 size
= type_size(type
, &align
);
5190 error("unknown type size");
5193 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5194 /* take into account specified alignment if bigger */
5196 if (ad
->aligned
> align
)
5197 align
= ad
->aligned
;
5198 } else if (ad
->packed
) {
5201 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5203 #ifdef CONFIG_TCC_BCHECK
5204 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5208 loc
= (loc
- size
) & -align
;
5210 #ifdef CONFIG_TCC_BCHECK
5211 /* handles bounds */
5212 /* XXX: currently, since we do only one pass, we cannot track
5213 '&' operators, so we add only arrays */
5214 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5215 unsigned long *bounds_ptr
;
5216 /* add padding between regions */
5218 /* then add local bound info */
5219 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5220 bounds_ptr
[0] = addr
;
5221 bounds_ptr
[1] = size
;
5225 /* local variable */
5226 sym_push(v
, type
, r
, addr
);
5228 /* push local reference */
5229 vset(type
, r
, addr
);
5235 if (v
&& scope
== VT_CONST
) {
5236 /* see if the symbol was already defined */
5239 if (!is_compatible_types(&sym
->type
, type
))
5240 error("incompatible types for redefinition of '%s'",
5241 get_tok_str(v
, NULL
));
5242 if (sym
->type
.t
& VT_EXTERN
) {
5243 /* if the variable is extern, it was not allocated */
5244 sym
->type
.t
&= ~VT_EXTERN
;
5245 /* set array size if it was ommited in extern
5247 if ((sym
->type
.t
& VT_ARRAY
) &&
5248 sym
->type
.ref
->c
< 0 &&
5250 sym
->type
.ref
->c
= type
->ref
->c
;
5252 /* we accept several definitions of the same
5253 global variable. this is tricky, because we
5254 must play with the SHN_COMMON type of the symbol */
5255 /* XXX: should check if the variable was already
5256 initialized. It is incorrect to initialized it
5258 /* no init data, we won't add more to the symbol */
5265 /* allocate symbol in corresponding section */
5270 else if (tcc_state
->nocommon
)
5274 data_offset
= sec
->data_offset
;
5275 data_offset
= (data_offset
+ align
- 1) & -align
;
5277 /* very important to increment global pointer at this time
5278 because initializers themselves can create new initializers */
5279 data_offset
+= size
;
5280 #ifdef CONFIG_TCC_BCHECK
5281 /* add padding if bound check */
5282 if (tcc_state
->do_bounds_check
)
5285 sec
->data_offset
= data_offset
;
5286 /* allocate section space to put the data */
5287 if (sec
->sh_type
!= SHT_NOBITS
&&
5288 data_offset
> sec
->data_allocated
)
5289 section_realloc(sec
, data_offset
);
5290 /* align section if needed */
5291 if (align
> sec
->sh_addralign
)
5292 sec
->sh_addralign
= align
;
5294 addr
= 0; /* avoid warning */
5298 if (scope
!= VT_CONST
|| !sym
) {
5299 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5300 sym
->asm_label
= asm_label
;
5302 /* update symbol definition */
5304 put_extern_sym(sym
, sec
, addr
, size
);
5307 /* put a common area */
5308 put_extern_sym(sym
, NULL
, align
, size
);
5309 /* XXX: find a nicer way */
5310 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5311 esym
->st_shndx
= SHN_COMMON
;
5316 /* push global reference */
5317 sym
= get_sym_ref(type
, sec
, addr
, size
);
5319 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5322 /* patch symbol weakness */
5323 if (type
->t
& VT_WEAK
)
5325 #ifdef CONFIG_TCC_BCHECK
5326 /* handles bounds now because the symbol must be defined
5327 before for the relocation */
5328 if (tcc_state
->do_bounds_check
) {
5329 unsigned long *bounds_ptr
;
5331 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5332 /* then add global bound info */
5333 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5334 bounds_ptr
[0] = 0; /* relocated */
5335 bounds_ptr
[1] = size
;
5339 if (has_init
|| (type
->t
& VT_VLA
)) {
5340 decl_initializer(type
, sec
, addr
, 1, 0);
5341 /* restore parse state if needed */
5343 tok_str_free(init_str
.str
);
5344 restore_parse_state(&saved_parse_state
);
5346 /* patch flexible array member size back to -1, */
5347 /* for possible subsequent similar declarations */
5349 flexible_array
->type
.ref
->c
= -1;
5354 static void put_func_debug(Sym
*sym
)
5359 /* XXX: we put here a dummy type */
5360 snprintf(buf
, sizeof(buf
), "%s:%c1",
5361 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5362 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5363 cur_text_section
, sym
->c
);
5364 /* //gr gdb wants a line at the function */
5365 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5370 /* parse an old style function declaration list */
5371 /* XXX: check multiple parameter */
5372 static void func_decl_list(Sym
*func_sym
)
5379 /* parse each declaration */
5380 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5381 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5382 if (!parse_btype(&btype
, &ad
))
5383 expect("declaration list");
5384 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5385 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5387 /* we accept no variable after */
5391 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5392 /* find parameter in function parameter list */
5395 if ((s
->v
& ~SYM_FIELD
) == v
)
5399 error("declaration for parameter '%s' but no such parameter",
5400 get_tok_str(v
, NULL
));
5402 /* check that no storage specifier except 'register' was given */
5403 if (type
.t
& VT_STORAGE
)
5404 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5405 convert_parameter_type(&type
);
5406 /* we can add the type (NOTE: it could be local to the function) */
5408 /* accept other parameters */
5419 /* parse a function defined by symbol 'sym' and generate its code in
5420 'cur_text_section' */
5421 static void gen_function(Sym
*sym
)
5423 int saved_nocode_wanted
= nocode_wanted
;
5425 ind
= cur_text_section
->data_offset
;
5426 /* NOTE: we patch the symbol size later */
5427 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5428 funcname
= get_tok_str(sym
->v
, NULL
);
5430 /* put debug symbol */
5431 if (tcc_state
->do_debug
)
5432 put_func_debug(sym
);
5433 /* push a dummy symbol to enable local sym storage */
5434 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5435 gfunc_prolog(&sym
->type
);
5437 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5440 cur_text_section
->data_offset
= ind
;
5441 label_pop(&global_label_stack
, NULL
);
5442 sym_pop(&local_stack
, NULL
); /* reset local stack */
5443 /* end of function */
5444 /* patch symbol size */
5445 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5447 /* patch symbol weakness (this definition overrules any prototype) */
5448 if (sym
->type
.t
& VT_WEAK
)
5450 if (tcc_state
->do_debug
) {
5451 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5453 /* It's better to crash than to generate wrong code */
5454 cur_text_section
= NULL
;
5455 funcname
= ""; /* for safety */
5456 func_vt
.t
= VT_VOID
; /* for safety */
5457 ind
= 0; /* for safety */
5458 nocode_wanted
= saved_nocode_wanted
;
5461 ST_FUNC
void gen_inline_functions(void)
5464 int *str
, inline_generated
, i
;
5465 struct InlineFunc
*fn
;
5467 /* iterate while inline function are referenced */
5469 inline_generated
= 0;
5470 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5471 fn
= tcc_state
->inline_fns
[i
];
5473 if (sym
&& sym
->c
) {
5474 /* the function was used: generate its code and
5475 convert it to a normal function */
5476 str
= fn
->token_str
;
5479 strcpy(file
->filename
, fn
->filename
);
5480 sym
->r
= VT_SYM
| VT_CONST
;
5481 sym
->type
.t
&= ~VT_INLINE
;
5485 cur_text_section
= text_section
;
5487 macro_ptr
= NULL
; /* fail safe */
5489 inline_generated
= 1;
5492 if (!inline_generated
)
5495 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5496 fn
= tcc_state
->inline_fns
[i
];
5497 str
= fn
->token_str
;
5500 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5503 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5504 static int decl0(int l
, int is_for_loop_init
)
5512 if (!parse_btype(&btype
, &ad
)) {
5513 if (is_for_loop_init
)
5515 /* skip redundant ';' */
5516 /* XXX: find more elegant solution */
5521 if (l
== VT_CONST
&&
5522 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5523 /* global asm block */
5527 /* special test for old K&R protos without explicit int
5528 type. Only accepted when defining global data */
5529 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5533 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5534 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5536 /* we accept no variable after */
5540 while (1) { /* iterate thru each declaration */
5541 char *asm_label
; // associated asm label
5543 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5547 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5548 printf("type = '%s'\n", buf
);
5551 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5552 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5553 error("function without file scope cannot be static");
5555 /* if old style function prototype, we accept a
5558 if (sym
->c
== FUNC_OLD
)
5559 func_decl_list(sym
);
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 /* parse one last attribute list, after asm label */
5571 parse_attribute(&ad
);
5576 #ifdef TCC_TARGET_PE
5578 type
.t
|= VT_IMPORT
;
5580 type
.t
|= VT_EXPORT
;
5584 error("cannot use local functions");
5585 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5586 expect("function definition");
5588 /* reject abstract declarators in function definition */
5590 while ((sym
= sym
->next
) != NULL
)
5591 if (!(sym
->v
& ~SYM_FIELD
))
5592 expect("identifier");
5594 /* XXX: cannot do better now: convert extern line to static inline */
5595 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5596 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5600 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5603 r
= sym
->type
.ref
->r
;
5604 /* use func_call from prototype if not defined */
5605 if (FUNC_CALL(r
) != FUNC_CDECL
5606 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5607 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5609 /* use export from prototype */
5611 FUNC_EXPORT(type
.ref
->r
) = 1;
5613 /* use static from prototype */
5614 if (sym
->type
.t
& VT_STATIC
)
5615 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5617 if (!is_compatible_types(&sym
->type
, &type
)) {
5619 error("incompatible types for redefinition of '%s'",
5620 get_tok_str(v
, NULL
));
5622 /* if symbol is already defined, then put complete type */
5625 /* put function symbol */
5626 sym
= global_identifier_push(v
, type
.t
, 0);
5627 sym
->type
.ref
= type
.ref
;
5630 /* static inline functions are just recorded as a kind
5631 of macro. Their code will be emitted at the end of
5632 the compilation unit only if they are used */
5633 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5634 (VT_INLINE
| VT_STATIC
)) {
5635 TokenString func_str
;
5637 struct InlineFunc
*fn
;
5638 const char *filename
;
5640 tok_str_new(&func_str
);
5646 error("unexpected end of file");
5647 tok_str_add_tok(&func_str
);
5652 } else if (t
== '}') {
5654 if (block_level
== 0)
5658 tok_str_add(&func_str
, -1);
5659 tok_str_add(&func_str
, 0);
5660 filename
= file
? file
->filename
: "";
5661 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5662 strcpy(fn
->filename
, filename
);
5664 fn
->token_str
= func_str
.str
;
5665 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5668 /* compute text section */
5669 cur_text_section
= ad
.section
;
5670 if (!cur_text_section
)
5671 cur_text_section
= text_section
;
5672 sym
->r
= VT_SYM
| VT_CONST
;
5677 if (btype
.t
& VT_TYPEDEF
) {
5678 /* save typedefed type */
5679 /* XXX: test storage specifiers ? */
5680 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5681 sym
->type
.t
|= VT_TYPEDEF
;
5684 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5685 /* external function definition */
5686 /* specific case for func_call attribute */
5687 type
.ref
->r
= INT_ATTR(&ad
);
5688 } else if (!(type
.t
& VT_ARRAY
)) {
5689 /* not lvalue if array */
5690 r
|= lvalue_type(type
.t
);
5692 has_init
= (tok
== '=');
5693 if (has_init
&& (type
.t
& VT_VLA
))
5694 error("Variable length array cannot be initialized");
5695 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5696 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5697 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5698 /* external variable or function */
5699 /* NOTE: as GCC, uninitialized global static
5700 arrays of null size are considered as
5702 sym
= external_sym(v
, &type
, r
, asm_label
);
5704 if (type
.t
& VT_WEAK
)
5707 if (ad
.alias_target
) {
5712 alias_target
= sym_find(ad
.alias_target
);
5713 if (!alias_target
|| !alias_target
->c
)
5714 error("unsupported forward __alias__ attribute");
5715 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5716 tsec
.sh_num
= esym
->st_shndx
;
5717 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5720 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5721 if (type
.t
& VT_STATIC
)
5727 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5731 if (is_for_loop_init
)
5743 ST_FUNC
void decl(int l
)