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
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
652 /* NOTE: get_reg can modify vstack[] */
653 if (vtop
->type
.t
& VT_BITFIELD
) {
656 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
657 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
658 /* remove bit field info to avoid loops */
659 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
660 /* cast to int to propagate signedness in following ops */
661 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
666 if((vtop
->type
.t
& VT_UNSIGNED
) ||
667 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
668 type
.t
|= VT_UNSIGNED
;
670 /* generate shifts */
671 vpushi(bits
- (bit_pos
+ bit_size
));
673 vpushi(bits
- bit_size
);
674 /* NOTE: transformed to SHR if unsigned */
678 if (is_float(vtop
->type
.t
) &&
679 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
682 unsigned long offset
;
683 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
687 /* XXX: unify with initializers handling ? */
688 /* CPUs usually cannot use float constants, so we store them
689 generically in data segment */
690 size
= type_size(&vtop
->type
, &align
);
691 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
692 data_section
->data_offset
= offset
;
693 /* XXX: not portable yet */
694 #if defined(__i386__) || defined(__x86_64__)
695 /* Zero pad x87 tenbyte long doubles */
696 if (size
== LDOUBLE_SIZE
)
697 vtop
->c
.tab
[2] &= 0xffff;
699 ptr
= section_ptr_add(data_section
, size
);
701 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
705 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
709 ptr
[i
] = vtop
->c
.tab
[i
];
710 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
711 vtop
->r
|= VT_LVAL
| VT_SYM
;
715 #ifdef CONFIG_TCC_BCHECK
716 if (vtop
->r
& VT_MUSTBOUND
)
720 r
= vtop
->r
& VT_VALMASK
;
724 /* need to reload if:
726 - lvalue (need to dereference pointer)
727 - already a register, but not in the right class */
729 || (vtop
->r
& VT_LVAL
)
730 || !(reg_classes
[r
] & rc
)
731 #ifndef TCC_TARGET_X86_64
732 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
737 #ifndef TCC_TARGET_X86_64
738 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
740 unsigned long long ll
;
741 /* two register type load : expand to two words
743 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
746 vtop
->c
.ui
= ll
; /* first word */
748 vtop
->r
= r
; /* save register value */
749 vpushi(ll
>> 32); /* second word */
750 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
751 (vtop
->r
& VT_LVAL
)) {
752 /* We do not want to modifier the long long
753 pointer here, so the safest (and less
754 efficient) is to save all the other registers
755 in the stack. XXX: totally inefficient. */
757 /* load from memory */
760 vtop
[-1].r
= r
; /* save register value */
761 /* increment pointer to get second word */
762 vtop
->type
.t
= VT_INT
;
771 vtop
[-1].r
= r
; /* save register value */
772 vtop
->r
= vtop
[-1].r2
;
774 /* allocate second register */
778 /* write second register */
782 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
784 /* lvalue of scalar type : need to use lvalue type
785 because of possible cast */
788 /* compute memory access type */
789 if (vtop
->r
& VT_LVAL_BYTE
)
791 else if (vtop
->r
& VT_LVAL_SHORT
)
793 if (vtop
->r
& VT_LVAL_UNSIGNED
)
797 /* restore wanted type */
800 /* one register type load */
805 #ifdef TCC_TARGET_C67
806 /* uses register pairs for doubles */
807 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
814 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
815 ST_FUNC
void gv2(int rc1
, int rc2
)
819 /* generate more generic register first. But VT_JMP or VT_CMP
820 values must be generated first in all cases to avoid possible
822 v
= vtop
[0].r
& VT_VALMASK
;
823 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
828 /* test if reload is needed for first register */
829 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
839 /* test if reload is needed for first register */
840 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
846 /* wrapper around RC_FRET to return a register by type */
847 static int rc_fret(int t
)
849 #ifdef TCC_TARGET_X86_64
850 if (t
== VT_LDOUBLE
) {
857 /* wrapper around REG_FRET to return a register by type */
858 static int reg_fret(int t
)
860 #ifdef TCC_TARGET_X86_64
861 if (t
== VT_LDOUBLE
) {
868 /* expand long long on stack in two int registers */
869 static void lexpand(void)
873 u
= vtop
->type
.t
& VT_UNSIGNED
;
876 vtop
[0].r
= vtop
[-1].r2
;
877 vtop
[0].r2
= VT_CONST
;
878 vtop
[-1].r2
= VT_CONST
;
879 vtop
[0].type
.t
= VT_INT
| u
;
880 vtop
[-1].type
.t
= VT_INT
| u
;
883 #ifdef TCC_TARGET_ARM
884 /* expand long long on stack */
885 ST_FUNC
void lexpand_nr(void)
889 u
= vtop
->type
.t
& VT_UNSIGNED
;
892 vtop
->type
.t
= VT_INT
| u
;
893 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
895 vtop
[-1].c
.ui
= vtop
->c
.ull
;
896 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
898 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
900 vtop
->r
= vtop
[-1].r
;
901 } else if (v
> VT_CONST
) {
905 vtop
->r
= vtop
[-1].r2
;
906 vtop
[-1].r2
= VT_CONST
;
907 vtop
[-1].type
.t
= VT_INT
| u
;
911 /* build a long long from two ints */
912 static void lbuild(int t
)
915 vtop
[-1].r2
= vtop
[0].r
;
920 /* rotate n first stack elements to the bottom
921 I1 ... In -> I2 ... In I1 [top is right]
923 static void vrotb(int n
)
934 /* rotate n first stack elements to the top
935 I1 ... In -> In I1 ... I(n-1) [top is right]
937 ST_FUNC
void vrott(int n
)
943 for(i
= 0;i
< n
- 1; i
++)
944 vtop
[-i
] = vtop
[-i
- 1];
948 #ifdef TCC_TARGET_ARM
949 /* like vrott but in other direction
950 In ... I1 -> I(n-1) ... I1 In [top is right]
952 ST_FUNC
void vnrott(int n
)
958 for(i
= n
- 1; i
> 0; i
--)
959 vtop
[-i
] = vtop
[-i
+ 1];
964 /* pop stack value */
965 ST_FUNC
void vpop(void)
968 v
= vtop
->r
& VT_VALMASK
;
969 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
970 /* for x86, we need to pop the FP stack */
971 if (v
== TREG_ST0
&& !nocode_wanted
) {
972 o(0xd8dd); /* fstp %st(0) */
975 if (v
== VT_JMP
|| v
== VT_JMPI
) {
976 /* need to put correct jump if && or || without test */
982 /* convert stack entry to register and duplicate its value in another
984 static void gv_dup(void)
990 if ((t
& VT_BTYPE
) == VT_LLONG
) {
997 /* stack: H L L1 H1 */
1005 /* duplicate value */
1010 #ifdef TCC_TARGET_X86_64
1011 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1021 load(r1
, &sv
); /* move r to r1 */
1023 /* duplicates value */
1029 #ifndef TCC_TARGET_X86_64
1030 /* generate CPU independent (unsigned) long long operations */
1031 static void gen_opl(int op
)
1033 int t
, a
, b
, op1
, c
, i
;
1035 unsigned short reg_iret
= REG_IRET
;
1036 unsigned short reg_lret
= REG_LRET
;
1042 func
= TOK___divdi3
;
1045 func
= TOK___udivdi3
;
1048 func
= TOK___moddi3
;
1051 func
= TOK___umoddi3
;
1058 /* call generic long long function */
1059 vpush_global_sym(&func_old_type
, func
);
1064 vtop
->r2
= reg_lret
;
1077 /* stack: L1 H1 L2 H2 */
1082 vtop
[-2] = vtop
[-3];
1085 /* stack: H1 H2 L1 L2 */
1091 /* stack: H1 H2 L1 L2 ML MH */
1094 /* stack: ML MH H1 H2 L1 L2 */
1098 /* stack: ML MH H1 L2 H2 L1 */
1103 /* stack: ML MH M1 M2 */
1106 } else if (op
== '+' || op
== '-') {
1107 /* XXX: add non carry method too (for MIPS or alpha) */
1113 /* stack: H1 H2 (L1 op L2) */
1116 gen_op(op1
+ 1); /* TOK_xxxC2 */
1119 /* stack: H1 H2 (L1 op L2) */
1122 /* stack: (L1 op L2) H1 H2 */
1124 /* stack: (L1 op L2) (H1 op H2) */
1132 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1133 t
= vtop
[-1].type
.t
;
1137 /* stack: L H shift */
1139 /* constant: simpler */
1140 /* NOTE: all comments are for SHL. the other cases are
1141 done by swaping words */
1152 if (op
!= TOK_SAR
) {
1185 /* XXX: should provide a faster fallback on x86 ? */
1188 func
= TOK___ashrdi3
;
1191 func
= TOK___lshrdi3
;
1194 func
= TOK___ashldi3
;
1200 /* compare operations */
1206 /* stack: L1 H1 L2 H2 */
1208 vtop
[-1] = vtop
[-2];
1210 /* stack: L1 L2 H1 H2 */
1213 /* when values are equal, we need to compare low words. since
1214 the jump is inverted, we invert the test too. */
1217 else if (op1
== TOK_GT
)
1219 else if (op1
== TOK_ULT
)
1221 else if (op1
== TOK_UGT
)
1226 if (op1
!= TOK_NE
) {
1230 /* generate non equal test */
1231 /* XXX: NOT PORTABLE yet */
1235 #if defined(TCC_TARGET_I386)
1236 b
= psym(0x850f, 0);
1237 #elif defined(TCC_TARGET_ARM)
1239 o(0x1A000000 | encbranch(ind
, 0, 1));
1240 #elif defined(TCC_TARGET_C67)
1241 error("not implemented");
1243 #error not supported
1247 /* compare low. Always unsigned */
1251 else if (op1
== TOK_LE
)
1253 else if (op1
== TOK_GT
)
1255 else if (op1
== TOK_GE
)
1266 /* handle integer constant optimizations and various machine
1268 static void gen_opic(int op
)
1270 int c1
, c2
, t1
, t2
, n
;
1273 typedef unsigned long long U
;
1277 t1
= v1
->type
.t
& VT_BTYPE
;
1278 t2
= v2
->type
.t
& VT_BTYPE
;
1282 else if (v1
->type
.t
& VT_UNSIGNED
)
1289 else if (v2
->type
.t
& VT_UNSIGNED
)
1294 /* currently, we cannot do computations with forward symbols */
1295 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1296 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1299 case '+': l1
+= l2
; break;
1300 case '-': l1
-= l2
; break;
1301 case '&': l1
&= l2
; break;
1302 case '^': l1
^= l2
; break;
1303 case '|': l1
|= l2
; break;
1304 case '*': l1
*= l2
; break;
1311 /* if division by zero, generate explicit division */
1314 error("division by zero in constant");
1318 default: l1
/= l2
; break;
1319 case '%': l1
%= l2
; break;
1320 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1321 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1324 case TOK_SHL
: l1
<<= l2
; break;
1325 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1326 case TOK_SAR
: l1
>>= l2
; break;
1328 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1329 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1330 case TOK_EQ
: l1
= l1
== l2
; break;
1331 case TOK_NE
: l1
= l1
!= l2
; break;
1332 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1333 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1334 case TOK_LT
: l1
= l1
< l2
; break;
1335 case TOK_GE
: l1
= l1
>= l2
; break;
1336 case TOK_LE
: l1
= l1
<= l2
; break;
1337 case TOK_GT
: l1
= l1
> l2
; break;
1339 case TOK_LAND
: l1
= l1
&& l2
; break;
1340 case TOK_LOR
: l1
= l1
|| l2
; break;
1347 /* if commutative ops, put c2 as constant */
1348 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1349 op
== '|' || op
== '*')) {
1351 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1352 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1354 /* Filter out NOP operations like x*1, x-0, x&-1... */
1355 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1358 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1359 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1365 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1366 /* try to use shifts instead of muls or divs */
1367 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1376 else if (op
== TOK_PDIV
)
1382 } else if (c2
&& (op
== '+' || op
== '-') &&
1383 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1384 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1385 /* symbol + constant case */
1392 if (!nocode_wanted
) {
1393 /* call low level op generator */
1394 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1405 /* generate a floating point operation with constant propagation */
1406 static void gen_opif(int op
)
1414 /* currently, we cannot do computations with forward symbols */
1415 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1416 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1418 if (v1
->type
.t
== VT_FLOAT
) {
1421 } else if (v1
->type
.t
== VT_DOUBLE
) {
1429 /* NOTE: we only do constant propagation if finite number (not
1430 NaN or infinity) (ANSI spec) */
1431 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1435 case '+': f1
+= f2
; break;
1436 case '-': f1
-= f2
; break;
1437 case '*': f1
*= f2
; break;
1441 error("division by zero in constant");
1446 /* XXX: also handles tests ? */
1450 /* XXX: overflow test ? */
1451 if (v1
->type
.t
== VT_FLOAT
) {
1453 } else if (v1
->type
.t
== VT_DOUBLE
) {
1461 if (!nocode_wanted
) {
1469 static int pointed_size(CType
*type
)
1472 return type_size(pointed_type(type
), &align
);
1475 static void vla_runtime_pointed_size(CType
*type
)
1478 vla_runtime_type_size(pointed_type(type
), &align
);
1481 static inline int is_null_pointer(SValue
*p
)
1483 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1485 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1486 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1489 static inline int is_integer_btype(int bt
)
1491 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1492 bt
== VT_INT
|| bt
== VT_LLONG
);
1495 /* check types for comparison or substraction of pointers */
1496 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1498 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1501 /* null pointers are accepted for all comparisons as gcc */
1502 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1506 bt1
= type1
->t
& VT_BTYPE
;
1507 bt2
= type2
->t
& VT_BTYPE
;
1508 /* accept comparison between pointer and integer with a warning */
1509 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1510 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1511 warning("comparison between pointer and integer");
1515 /* both must be pointers or implicit function pointers */
1516 if (bt1
== VT_PTR
) {
1517 type1
= pointed_type(type1
);
1518 } else if (bt1
!= VT_FUNC
)
1519 goto invalid_operands
;
1521 if (bt2
== VT_PTR
) {
1522 type2
= pointed_type(type2
);
1523 } else if (bt2
!= VT_FUNC
) {
1525 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1527 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1528 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1532 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1533 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1534 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1535 /* gcc-like error if '-' is used */
1537 goto invalid_operands
;
1539 warning("comparison of distinct pointer types lacks a cast");
1543 /* generic gen_op: handles types problems */
1544 ST_FUNC
void gen_op(int op
)
1546 int u
, t1
, t2
, bt1
, bt2
, t
;
1549 t1
= vtop
[-1].type
.t
;
1550 t2
= vtop
[0].type
.t
;
1551 bt1
= t1
& VT_BTYPE
;
1552 bt2
= t2
& VT_BTYPE
;
1554 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1555 /* at least one operand is a pointer */
1556 /* relationnal op: must be both pointers */
1557 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1558 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1559 /* pointers are handled are unsigned */
1560 #ifdef TCC_TARGET_X86_64
1561 t
= VT_LLONG
| VT_UNSIGNED
;
1563 t
= VT_INT
| VT_UNSIGNED
;
1567 /* if both pointers, then it must be the '-' op */
1568 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1573 error("cannot use pointers here");
1574 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1575 /* XXX: check that types are compatible */
1576 if (vtop
[-1].type
.t
& VT_VLA
) {
1577 vla_runtime_pointed_size(&vtop
[-1].type
);
1581 u
= pointed_size(&vtop
[-1].type
);
1584 /* set to integer type */
1585 #ifdef TCC_TARGET_X86_64
1586 vtop
->type
.t
= VT_LLONG
;
1588 vtop
->type
.t
= VT_INT
;
1596 /* exactly one pointer : must be '+' or '-'. */
1597 if (op
!= '-' && op
!= '+')
1598 error("cannot use pointers here");
1599 /* Put pointer as first operand */
1600 if (bt2
== VT_PTR
) {
1604 type1
= vtop
[-1].type
;
1605 type1
.t
&= ~VT_ARRAY
;
1606 if (vtop
[-1].type
.t
& VT_VLA
)
1607 vla_runtime_pointed_size(&vtop
[-1].type
);
1609 u
= pointed_size(&vtop
[-1].type
);
1611 error("unknown array element size");
1612 #ifdef TCC_TARGET_X86_64
1615 /* XXX: cast to int ? (long long case) */
1620 #ifdef CONFIG_TCC_BCHECK
1621 /* if evaluating constant expression, no code should be
1622 generated, so no bound check */
1623 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1624 /* if bounded pointers, we generate a special code to
1631 gen_bounded_ptr_add();
1637 /* put again type if gen_opic() swaped operands */
1640 } else if (is_float(bt1
) || is_float(bt2
)) {
1641 /* compute bigger type and do implicit casts */
1642 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1644 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1649 /* floats can only be used for a few operations */
1650 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1651 (op
< TOK_ULT
|| op
> TOK_GT
))
1652 error("invalid operands for binary operation");
1654 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1655 /* cast to biggest op */
1657 /* convert to unsigned if it does not fit in a long long */
1658 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1659 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1663 /* integer operations */
1665 /* convert to unsigned if it does not fit in an integer */
1666 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1667 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1670 /* XXX: currently, some unsigned operations are explicit, so
1671 we modify them here */
1672 if (t
& VT_UNSIGNED
) {
1679 else if (op
== TOK_LT
)
1681 else if (op
== TOK_GT
)
1683 else if (op
== TOK_LE
)
1685 else if (op
== TOK_GE
)
1692 /* special case for shifts and long long: we keep the shift as
1694 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1701 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1702 /* relationnal op: the result is an int */
1703 vtop
->type
.t
= VT_INT
;
1710 #ifndef TCC_TARGET_ARM
1711 /* generic itof for unsigned long long case */
1712 static void gen_cvt_itof1(int t
)
1714 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1715 (VT_LLONG
| VT_UNSIGNED
)) {
1718 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1719 #if LDOUBLE_SIZE != 8
1720 else if (t
== VT_LDOUBLE
)
1721 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1724 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1728 vtop
->r
= reg_fret(t
);
1735 /* generic ftoi for unsigned long long case */
1736 static void gen_cvt_ftoi1(int t
)
1740 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1741 /* not handled natively */
1742 st
= vtop
->type
.t
& VT_BTYPE
;
1744 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1745 #if LDOUBLE_SIZE != 8
1746 else if (st
== VT_LDOUBLE
)
1747 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1750 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1755 vtop
->r2
= REG_LRET
;
1761 /* force char or short cast */
1762 static void force_charshort_cast(int t
)
1766 /* XXX: add optimization if lvalue : just change type and offset */
1771 if (t
& VT_UNSIGNED
) {
1772 vpushi((1 << bits
) - 1);
1778 /* result must be signed or the SAR is converted to an SHL
1779 This was not the case when "t" was a signed short
1780 and the last value on the stack was an unsigned int */
1781 vtop
->type
.t
&= ~VT_UNSIGNED
;
1787 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1788 static void gen_cast(CType
*type
)
1790 int sbt
, dbt
, sf
, df
, c
, p
;
1792 /* special delayed cast for char/short */
1793 /* XXX: in some cases (multiple cascaded casts), it may still
1795 if (vtop
->r
& VT_MUSTCAST
) {
1796 vtop
->r
&= ~VT_MUSTCAST
;
1797 force_charshort_cast(vtop
->type
.t
);
1800 /* bitfields first get cast to ints */
1801 if (vtop
->type
.t
& VT_BITFIELD
) {
1805 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1806 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1811 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1812 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1814 /* constant case: we can do it now */
1815 /* XXX: in ISOC, cannot do it if error in convert */
1816 if (sbt
== VT_FLOAT
)
1817 vtop
->c
.ld
= vtop
->c
.f
;
1818 else if (sbt
== VT_DOUBLE
)
1819 vtop
->c
.ld
= vtop
->c
.d
;
1822 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1823 if (sbt
& VT_UNSIGNED
)
1824 vtop
->c
.ld
= vtop
->c
.ull
;
1826 vtop
->c
.ld
= vtop
->c
.ll
;
1828 if (sbt
& VT_UNSIGNED
)
1829 vtop
->c
.ld
= vtop
->c
.ui
;
1831 vtop
->c
.ld
= vtop
->c
.i
;
1834 if (dbt
== VT_FLOAT
)
1835 vtop
->c
.f
= (float)vtop
->c
.ld
;
1836 else if (dbt
== VT_DOUBLE
)
1837 vtop
->c
.d
= (double)vtop
->c
.ld
;
1838 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1839 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1840 } else if (sf
&& dbt
== VT_BOOL
) {
1841 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1844 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1845 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1846 vtop
->c
.ll
= vtop
->c
.ull
;
1847 else if (sbt
& VT_UNSIGNED
)
1848 vtop
->c
.ll
= vtop
->c
.ui
;
1849 #ifdef TCC_TARGET_X86_64
1850 else if (sbt
== VT_PTR
)
1853 else if (sbt
!= VT_LLONG
)
1854 vtop
->c
.ll
= vtop
->c
.i
;
1856 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1857 vtop
->c
.ull
= vtop
->c
.ll
;
1858 else if (dbt
== VT_BOOL
)
1859 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1860 else if (dbt
!= VT_LLONG
) {
1862 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1864 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1867 if(dbt
& VT_UNSIGNED
)
1868 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1870 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1873 } else if (p
&& dbt
== VT_BOOL
) {
1876 } else if (!nocode_wanted
) {
1877 /* non constant case: generate code */
1879 /* convert from fp to fp */
1882 /* convert int to fp */
1885 /* convert fp to int */
1886 if (dbt
== VT_BOOL
) {
1890 /* we handle char/short/etc... with generic code */
1891 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1892 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1896 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1897 /* additional cast for char/short... */
1902 #ifndef TCC_TARGET_X86_64
1903 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1904 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1905 /* scalar to long long */
1906 /* machine independent conversion */
1908 /* generate high word */
1909 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1913 if (sbt
== VT_PTR
) {
1914 /* cast from pointer to int before we apply
1915 shift operation, which pointers don't support*/
1916 gen_cast(&int_type
);
1922 /* patch second register */
1923 vtop
[-1].r2
= vtop
->r
;
1927 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1928 (dbt
& VT_BTYPE
) == VT_PTR
||
1929 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1930 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1931 (sbt
& VT_BTYPE
) != VT_PTR
&&
1932 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1933 /* need to convert from 32bit to 64bit */
1935 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1936 /* x86_64 specific: movslq */
1938 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1942 } else if (dbt
== VT_BOOL
) {
1943 /* scalar to bool */
1946 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1947 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1948 if (sbt
== VT_PTR
) {
1949 vtop
->type
.t
= VT_INT
;
1950 warning("nonportable conversion from pointer to char/short");
1952 force_charshort_cast(dbt
);
1953 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1955 if (sbt
== VT_LLONG
) {
1956 /* from long long: just take low order word */
1960 /* if lvalue and single word type, nothing to do because
1961 the lvalue already contains the real type size (see
1962 VT_LVAL_xxx constants) */
1965 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1966 /* if we are casting between pointer types,
1967 we must update the VT_LVAL_xxx size */
1968 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1969 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1974 /* return type size as known at compile time. Put alignment at 'a' */
1975 ST_FUNC
int type_size(CType
*type
, int *a
)
1980 bt
= type
->t
& VT_BTYPE
;
1981 if (bt
== VT_STRUCT
) {
1986 } else if (bt
== VT_PTR
) {
1987 if (type
->t
& VT_ARRAY
) {
1991 ts
= type_size(&s
->type
, a
);
1993 if (ts
< 0 && s
->c
< 0)
2001 } else if (bt
== VT_LDOUBLE
) {
2003 return LDOUBLE_SIZE
;
2004 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2005 #ifdef TCC_TARGET_I386
2006 #ifdef TCC_TARGET_PE
2011 #elif defined(TCC_TARGET_ARM)
2021 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2024 } else if (bt
== VT_SHORT
) {
2028 /* char, void, function, _Bool */
2034 /* push type size as known at runtime time on top of value stack. Put
2036 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2038 if (type
->t
& VT_VLA
) {
2042 vla_runtime_type_size(&s
->type
, a
);
2044 if ((vtop
->r
& (VT_SYM
|VT_LVAL
|VT_VALMASK
)) != VT_CONST
) {
2053 size
= type_size(type
, a
);
2058 /* return the pointed type of t */
2059 static inline CType
*pointed_type(CType
*type
)
2061 return &type
->ref
->type
;
2064 /* modify type so that its it is a pointer to type. */
2065 ST_FUNC
void mk_pointer(CType
*type
)
2068 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2069 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2073 /* compare function types. OLD functions match any new functions */
2074 static int is_compatible_func(CType
*type1
, CType
*type2
)
2080 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2082 /* check func_call */
2083 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2085 /* XXX: not complete */
2086 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2090 while (s1
!= NULL
) {
2093 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2103 /* return true if type1 and type2 are the same. If unqualified is
2104 true, qualifiers on the types are ignored.
2106 - enums are not checked as gcc __builtin_types_compatible_p ()
2108 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2112 t1
= type1
->t
& VT_TYPE
;
2113 t2
= type2
->t
& VT_TYPE
;
2115 /* strip qualifiers before comparing */
2116 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2117 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2119 /* XXX: bitfields ? */
2122 /* test more complicated cases */
2123 bt1
= t1
& VT_BTYPE
;
2124 if (bt1
== VT_PTR
) {
2125 type1
= pointed_type(type1
);
2126 type2
= pointed_type(type2
);
2127 return is_compatible_types(type1
, type2
);
2128 } else if (bt1
== VT_STRUCT
) {
2129 return (type1
->ref
== type2
->ref
);
2130 } else if (bt1
== VT_FUNC
) {
2131 return is_compatible_func(type1
, type2
);
2137 /* return true if type1 and type2 are exactly the same (including
2140 static int is_compatible_types(CType
*type1
, CType
*type2
)
2142 return compare_types(type1
,type2
,0);
2145 /* return true if type1 and type2 are the same (ignoring qualifiers).
2147 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2149 return compare_types(type1
,type2
,1);
2152 /* print a type. If 'varstr' is not NULL, then the variable is also
2153 printed in the type */
2155 /* XXX: add array and function pointers */
2156 static void type_to_str(char *buf
, int buf_size
,
2157 CType
*type
, const char *varstr
)
2164 t
= type
->t
& VT_TYPE
;
2167 if (t
& VT_CONSTANT
)
2168 pstrcat(buf
, buf_size
, "const ");
2169 if (t
& VT_VOLATILE
)
2170 pstrcat(buf
, buf_size
, "volatile ");
2171 if (t
& VT_UNSIGNED
)
2172 pstrcat(buf
, buf_size
, "unsigned ");
2202 tstr
= "long double";
2204 pstrcat(buf
, buf_size
, tstr
);
2208 if (bt
== VT_STRUCT
)
2212 pstrcat(buf
, buf_size
, tstr
);
2213 v
= type
->ref
->v
& ~SYM_STRUCT
;
2214 if (v
>= SYM_FIRST_ANOM
)
2215 pstrcat(buf
, buf_size
, "<anonymous>");
2217 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2221 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2222 pstrcat(buf
, buf_size
, "(");
2224 while (sa
!= NULL
) {
2225 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2226 pstrcat(buf
, buf_size
, buf1
);
2229 pstrcat(buf
, buf_size
, ", ");
2231 pstrcat(buf
, buf_size
, ")");
2235 pstrcpy(buf1
, sizeof(buf1
), "*");
2237 pstrcat(buf1
, sizeof(buf1
), varstr
);
2238 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2242 pstrcat(buf
, buf_size
, " ");
2243 pstrcat(buf
, buf_size
, varstr
);
2248 /* verify type compatibility to store vtop in 'dt' type, and generate
2250 static void gen_assign_cast(CType
*dt
)
2252 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2253 char buf1
[256], buf2
[256];
2256 st
= &vtop
->type
; /* source type */
2257 dbt
= dt
->t
& VT_BTYPE
;
2258 sbt
= st
->t
& VT_BTYPE
;
2259 if (dt
->t
& VT_CONSTANT
)
2260 warning("assignment of read-only location");
2263 /* special cases for pointers */
2264 /* '0' can also be a pointer */
2265 if (is_null_pointer(vtop
))
2267 /* accept implicit pointer to integer cast with warning */
2268 if (is_integer_btype(sbt
)) {
2269 warning("assignment makes pointer from integer without a cast");
2272 type1
= pointed_type(dt
);
2273 /* a function is implicitely a function pointer */
2274 if (sbt
== VT_FUNC
) {
2275 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2276 !is_compatible_types(pointed_type(dt
), st
))
2277 warning("assignment from incompatible pointer type");
2282 type2
= pointed_type(st
);
2283 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2284 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2285 /* void * can match anything */
2287 /* exact type match, except for unsigned */
2290 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2291 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2292 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2293 warning("assignment from incompatible pointer type");
2295 /* check const and volatile */
2296 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2297 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2298 warning("assignment discards qualifiers from pointer target type");
2304 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2305 warning("assignment makes integer from pointer without a cast");
2307 /* XXX: more tests */
2312 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2313 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2314 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2316 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2317 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2318 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2326 /* store vtop in lvalue pushed on stack */
2327 ST_FUNC
void vstore(void)
2329 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2331 ft
= vtop
[-1].type
.t
;
2332 sbt
= vtop
->type
.t
& VT_BTYPE
;
2333 dbt
= ft
& VT_BTYPE
;
2334 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2335 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2336 /* optimize char/short casts */
2337 delayed_cast
= VT_MUSTCAST
;
2338 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2339 /* XXX: factorize */
2340 if (ft
& VT_CONSTANT
)
2341 warning("assignment of read-only location");
2344 if (!(ft
& VT_BITFIELD
))
2345 gen_assign_cast(&vtop
[-1].type
);
2348 if (sbt
== VT_STRUCT
) {
2349 /* if structure, only generate pointer */
2350 /* structure assignment : generate memcpy */
2351 /* XXX: optimize if small size */
2352 if (!nocode_wanted
) {
2353 size
= type_size(&vtop
->type
, &align
);
2357 vtop
->type
.t
= VT_PTR
;
2360 /* address of memcpy() */
2363 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2364 else if(!(align
& 3))
2365 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2368 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2373 vtop
->type
.t
= VT_PTR
;
2382 /* leave source on stack */
2383 } else if (ft
& VT_BITFIELD
) {
2384 /* bitfield store handling */
2385 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2386 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2387 /* remove bit field info to avoid loops */
2388 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2390 /* duplicate source into other register */
2395 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2396 gen_cast(&vtop
[-1].type
);
2397 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2400 /* duplicate destination */
2402 vtop
[-1] = vtop
[-2];
2404 /* mask and shift source */
2405 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2406 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2407 vpushll((1ULL << bit_size
) - 1ULL);
2409 vpushi((1 << bit_size
) - 1);
2415 /* load destination, mask and or with source */
2417 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2418 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2420 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2427 /* pop off shifted source from "duplicate source..." above */
2431 #ifdef CONFIG_TCC_BCHECK
2432 /* bound check case */
2433 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2439 if (!nocode_wanted
) {
2443 #ifdef TCC_TARGET_X86_64
2444 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2449 r
= gv(rc
); /* generate value */
2450 /* if lvalue was saved on stack, must read it */
2451 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2453 t
= get_reg(RC_INT
);
2454 #ifdef TCC_TARGET_X86_64
2459 sv
.r
= VT_LOCAL
| VT_LVAL
;
2460 sv
.c
.ul
= vtop
[-1].c
.ul
;
2462 vtop
[-1].r
= t
| VT_LVAL
;
2465 #ifndef TCC_TARGET_X86_64
2466 /* two word case handling : store second register at word + 4 */
2467 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2469 /* convert to int to increment easily */
2470 vtop
->type
.t
= VT_INT
;
2476 /* XXX: it works because r2 is spilled last ! */
2477 store(vtop
->r2
, vtop
- 1);
2482 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2483 vtop
->r
|= delayed_cast
;
2487 /* post defines POST/PRE add. c is the token ++ or -- */
2488 ST_FUNC
void inc(int post
, int c
)
2491 vdup(); /* save lvalue */
2493 gv_dup(); /* duplicate value */
2498 vpushi(c
- TOK_MID
);
2500 vstore(); /* store value */
2502 vpop(); /* if post op, return saved value */
2505 /* Parse GNUC __attribute__ extension. Currently, the following
2506 extensions are recognized:
2507 - aligned(n) : set data/function alignment.
2508 - packed : force data alignment to 1
2509 - section(x) : generate data/code in this section.
2510 - unused : currently ignored, but may be used someday.
2511 - regparm(n) : pass function parameters in registers (i386 only)
2513 static void parse_attribute(AttributeDef
*ad
)
2517 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2521 while (tok
!= ')') {
2522 if (tok
< TOK_IDENT
)
2523 expect("attribute name");
2531 expect("section name");
2532 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2540 expect("alias(\"target\")");
2541 ad
->alias_target
= /* save string as token, for later */
2542 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2551 if (n
<= 0 || (n
& (n
- 1)) != 0)
2552 error("alignment must be a positive power of two");
2569 /* currently, no need to handle it because tcc does not
2570 track unused objects */
2574 /* currently, no need to handle it because tcc does not
2575 track unused objects */
2580 ad
->func_call
= FUNC_CDECL
;
2585 ad
->func_call
= FUNC_STDCALL
;
2587 #ifdef TCC_TARGET_I386
2597 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2603 ad
->func_call
= FUNC_FASTCALLW
;
2610 ad
->mode
= VT_LLONG
+ 1;
2613 ad
->mode
= VT_SHORT
+ 1;
2616 ad
->mode
= VT_INT
+ 1;
2619 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2626 ad
->func_export
= 1;
2629 ad
->func_import
= 1;
2632 if (tcc_state
->warn_unsupported
)
2633 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2634 /* skip parameters */
2636 int parenthesis
= 0;
2640 else if (tok
== ')')
2643 } while (parenthesis
&& tok
!= -1);
2656 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2657 static void struct_decl(CType
*type
, int u
)
2659 int a
, v
, size
, align
, maxalign
, c
, offset
;
2660 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2661 Sym
*s
, *ss
, *ass
, **ps
;
2665 a
= tok
; /* save decl type */
2670 /* struct already defined ? return it */
2672 expect("struct/union/enum name");
2676 error("invalid type");
2683 /* we put an undefined size for struct/union */
2684 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2685 s
->r
= 0; /* default alignment is zero as gcc */
2686 /* put struct/union/enum name in type */
2694 error("struct/union/enum already defined");
2695 /* cannot be empty */
2697 /* non empty enums are not allowed */
2698 if (a
== TOK_ENUM
) {
2702 expect("identifier");
2708 /* enum symbols have static storage */
2709 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2710 ss
->type
.t
|= VT_STATIC
;
2715 /* NOTE: we accept a trailing comma */
2726 while (tok
!= '}') {
2727 parse_btype(&btype
, &ad
);
2733 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2734 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2735 expect("identifier");
2736 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2737 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2738 error("invalid type for '%s'",
2739 get_tok_str(v
, NULL
));
2743 bit_size
= expr_const();
2744 /* XXX: handle v = 0 case for messages */
2746 error("negative width in bit-field '%s'",
2747 get_tok_str(v
, NULL
));
2748 if (v
&& bit_size
== 0)
2749 error("zero width for bit-field '%s'",
2750 get_tok_str(v
, NULL
));
2752 size
= type_size(&type1
, &align
);
2754 if (align
< ad
.aligned
)
2756 } else if (ad
.packed
) {
2758 } else if (*tcc_state
->pack_stack_ptr
) {
2759 if (align
> *tcc_state
->pack_stack_ptr
)
2760 align
= *tcc_state
->pack_stack_ptr
;
2763 if (bit_size
>= 0) {
2764 bt
= type1
.t
& VT_BTYPE
;
2771 error("bitfields must have scalar type");
2773 if (bit_size
> bsize
) {
2774 error("width of '%s' exceeds its type",
2775 get_tok_str(v
, NULL
));
2776 } else if (bit_size
== bsize
) {
2777 /* no need for bit fields */
2779 } else if (bit_size
== 0) {
2780 /* XXX: what to do if only padding in a
2782 /* zero size: means to pad */
2785 /* we do not have enough room ?
2786 did the type change?
2788 if ((bit_pos
+ bit_size
) > bsize
||
2789 bt
!= prevbt
|| a
== TOK_UNION
)
2792 /* XXX: handle LSB first */
2793 type1
.t
|= VT_BITFIELD
|
2794 (bit_pos
<< VT_STRUCT_SHIFT
) |
2795 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2796 bit_pos
+= bit_size
;
2802 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2803 /* add new memory data only if starting
2805 if (lbit_pos
== 0) {
2806 if (a
== TOK_STRUCT
) {
2807 c
= (c
+ align
- 1) & -align
;
2816 if (align
> maxalign
)
2820 printf("add field %s offset=%d",
2821 get_tok_str(v
, NULL
), offset
);
2822 if (type1
.t
& VT_BITFIELD
) {
2823 printf(" pos=%d size=%d",
2824 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2825 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2830 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2832 while ((ass
= ass
->next
) != NULL
) {
2833 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2838 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2842 if (tok
== ';' || tok
== TOK_EOF
)
2849 /* store size and alignment */
2850 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2856 /* return 0 if no type declaration. otherwise, return the basic type
2859 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2861 int t
, u
, type_found
, typespec_found
, typedef_found
;
2865 memset(ad
, 0, sizeof(AttributeDef
));
2873 /* currently, we really ignore extension */
2883 if ((t
& VT_BTYPE
) != 0)
2884 error("too many basic types");
2900 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2901 #ifndef TCC_TARGET_PE
2902 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2904 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2905 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2919 if ((t
& VT_BTYPE
) == VT_LONG
) {
2920 #ifdef TCC_TARGET_PE
2921 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2923 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2931 struct_decl(&type1
, VT_ENUM
);
2934 type
->ref
= type1
.ref
;
2938 struct_decl(&type1
, VT_STRUCT
);
2941 /* type modifiers */
2994 /* GNUC attribute */
2995 case TOK_ATTRIBUTE1
:
2996 case TOK_ATTRIBUTE2
:
2997 parse_attribute(ad
);
3000 t
= (t
& ~VT_BTYPE
) | u
;
3008 parse_expr_type(&type1
);
3009 /* remove all storage modifiers except typedef */
3010 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3013 if (typespec_found
|| typedef_found
)
3016 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3019 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3020 type
->ref
= s
->type
.ref
;
3022 /* get attributes from typedef */
3023 if (0 == ad
->aligned
)
3024 ad
->aligned
= FUNC_ALIGN(s
->r
);
3025 if (0 == ad
->func_call
)
3026 ad
->func_call
= FUNC_CALL(s
->r
);
3027 ad
->packed
|= FUNC_PACKED(s
->r
);
3036 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3037 error("signed and unsigned modifier");
3038 if (tcc_state
->char_is_unsigned
) {
3039 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3044 /* long is never used as type */
3045 if ((t
& VT_BTYPE
) == VT_LONG
)
3046 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3047 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3049 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3055 /* convert a function parameter type (array to pointer and function to
3056 function pointer) */
3057 static inline void convert_parameter_type(CType
*pt
)
3059 /* remove const and volatile qualifiers (XXX: const could be used
3060 to indicate a const function parameter */
3061 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3062 /* array must be transformed to pointer according to ANSI C */
3064 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3069 ST_FUNC
void parse_asm_str(CString
*astr
)
3072 /* read the string */
3074 expect("string constant");
3076 while (tok
== TOK_STR
) {
3077 /* XXX: add \0 handling too ? */
3078 cstr_cat(astr
, tokc
.cstr
->data
);
3081 cstr_ccat(astr
, '\0');
3084 /* Parse an asm label and return the label
3085 * Don't forget to free the CString in the caller! */
3086 static void asm_label_instr(CString
*astr
)
3089 parse_asm_str(astr
);
3092 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3096 static void post_type(CType
*type
, AttributeDef
*ad
)
3098 int n
, l
, t1
, arg_size
, align
;
3099 Sym
**plast
, *s
, *first
;
3104 /* function declaration */
3112 /* read param name and compute offset */
3113 if (l
!= FUNC_OLD
) {
3114 if (!parse_btype(&pt
, &ad1
)) {
3116 error("invalid type");
3123 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3125 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3126 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3127 error("parameter declared as void");
3128 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3133 expect("identifier");
3137 convert_parameter_type(&pt
);
3138 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3144 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3151 /* if no parameters, then old type prototype */
3155 /* NOTE: const is ignored in returned type as it has a special
3156 meaning in gcc / C++ */
3157 type
->t
&= ~VT_CONSTANT
;
3158 /* some ancient pre-K&R C allows a function to return an array
3159 and the array brackets to be put after the arguments, such
3160 that "int c()[]" means something like "int[] c()" */
3163 skip(']'); /* only handle simple "[]" */
3166 /* we push a anonymous symbol which will contain the function prototype */
3167 ad
->func_args
= arg_size
;
3168 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3172 } else if (tok
== '[') {
3173 SValue
*last_vtop
= NULL
;
3175 /* array definition */
3177 if (tok
== TOK_RESTRICT1
)
3183 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3187 error("invalid array size");
3189 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3190 error("size of variable length array should be an integer");
3196 /* parse next post type */
3197 post_type(type
, ad
);
3198 t1
|= type
->t
& VT_VLA
;
3200 /* we push an anonymous symbol which will contain the array
3202 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3204 s
->s
= last_vtop
; // That's ok, we don't need n with VLA
3209 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3214 /* Parse a type declaration (except basic type), and return the type
3215 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3216 expected. 'type' should contain the basic type. 'ad' is the
3217 attribute definition of the basic type. It can be modified by
3220 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3223 CType type1
, *type2
;
3224 int qualifiers
, storage
;
3226 while (tok
== '*') {
3234 qualifiers
|= VT_CONSTANT
;
3239 qualifiers
|= VT_VOLATILE
;
3247 type
->t
|= qualifiers
;
3250 /* XXX: clarify attribute handling */
3251 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3252 parse_attribute(ad
);
3254 /* recursive type */
3255 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3256 type1
.t
= 0; /* XXX: same as int */
3259 /* XXX: this is not correct to modify 'ad' at this point, but
3260 the syntax is not clear */
3261 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3262 parse_attribute(ad
);
3263 type_decl(&type1
, ad
, v
, td
);
3266 /* type identifier */
3267 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3271 if (!(td
& TYPE_ABSTRACT
))
3272 expect("identifier");
3276 storage
= type
->t
& VT_STORAGE
;
3277 type
->t
&= ~VT_STORAGE
;
3278 post_type(type
, ad
);
3280 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3281 parse_attribute(ad
);
3285 /* append type at the end of type1 */
3298 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3299 ST_FUNC
int lvalue_type(int t
)
3304 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3306 else if (bt
== VT_SHORT
)
3310 if (t
& VT_UNSIGNED
)
3311 r
|= VT_LVAL_UNSIGNED
;
3315 /* indirection with full error checking and bound check */
3316 ST_FUNC
void indir(void)
3318 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3319 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3323 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3325 vtop
->type
= *pointed_type(&vtop
->type
);
3326 /* Arrays and functions are never lvalues */
3327 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3328 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3329 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3330 /* if bound checking, the referenced pointer must be checked */
3331 #ifdef CONFIG_TCC_BCHECK
3332 if (tcc_state
->do_bounds_check
)
3333 vtop
->r
|= VT_MUSTBOUND
;
3338 /* pass a parameter to a function and do type checking and casting */
3339 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3344 func_type
= func
->c
;
3345 if (func_type
== FUNC_OLD
||
3346 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3347 /* default casting : only need to convert float to double */
3348 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3352 } else if (arg
== NULL
) {
3353 error("too many arguments to function");
3356 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3357 gen_assign_cast(&type
);
3361 /* parse an expression of the form '(type)' or '(expr)' and return its
3363 static void parse_expr_type(CType
*type
)
3369 if (parse_btype(type
, &ad
)) {
3370 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3377 static void parse_type(CType
*type
)
3382 if (!parse_btype(type
, &ad
)) {
3385 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3388 static void vpush_tokc(int t
)
3393 vsetc(&type
, VT_CONST
, &tokc
);
3396 ST_FUNC
void unary(void)
3398 int n
, t
, align
, size
, r
, sizeof_caller
;
3402 static int in_sizeof
= 0;
3404 sizeof_caller
= in_sizeof
;
3406 /* XXX: GCC 2.95.3 does not generate a table although it should be
3420 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3424 vpush_tokc(VT_LLONG
);
3428 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3432 vpush_tokc(VT_FLOAT
);
3436 vpush_tokc(VT_DOUBLE
);
3440 vpush_tokc(VT_LDOUBLE
);
3443 case TOK___FUNCTION__
:
3445 goto tok_identifier
;
3451 /* special function name identifier */
3452 len
= strlen(funcname
) + 1;
3453 /* generate char[len] type */
3458 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3459 ptr
= section_ptr_add(data_section
, len
);
3460 memcpy(ptr
, funcname
, len
);
3465 #ifdef TCC_TARGET_PE
3466 t
= VT_SHORT
| VT_UNSIGNED
;
3472 /* string parsing */
3475 if (tcc_state
->warn_write_strings
)
3480 memset(&ad
, 0, sizeof(AttributeDef
));
3481 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3486 if (parse_btype(&type
, &ad
)) {
3487 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3489 /* check ISOC99 compound literal */
3491 /* data is allocated locally by default */
3496 /* all except arrays are lvalues */
3497 if (!(type
.t
& VT_ARRAY
))
3498 r
|= lvalue_type(type
.t
);
3499 memset(&ad
, 0, sizeof(AttributeDef
));
3500 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3502 if (sizeof_caller
) {
3509 } else if (tok
== '{') {
3510 /* save all registers */
3512 /* statement expression : we do not accept break/continue
3513 inside as GCC does */
3514 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3529 /* functions names must be treated as function pointers,
3530 except for unary '&' and sizeof. Since we consider that
3531 functions are not lvalues, we only have to handle it
3532 there and in function calls. */
3533 /* arrays can also be used although they are not lvalues */
3534 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3535 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3537 mk_pointer(&vtop
->type
);
3543 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3545 boolean
.t
= VT_BOOL
;
3547 vtop
->c
.i
= !vtop
->c
.i
;
3548 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3549 vtop
->c
.i
= vtop
->c
.i
^ 1;
3552 vseti(VT_JMP
, gtst(1, 0));
3563 /* in order to force cast, we add zero */
3565 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3566 error("pointer not accepted for unary plus");
3576 unary_type(&type
); // Perform a in_sizeof = 0;
3577 size
= type_size(&type
, &align
);
3578 if (t
== TOK_SIZEOF
) {
3579 if (!(type
.t
& VT_VLA
)) {
3581 error("sizeof applied to an incomplete type");
3584 vla_runtime_type_size(&type
, &align
);
3589 vtop
->type
.t
|= VT_UNSIGNED
;
3592 case TOK_builtin_types_compatible_p
:
3601 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3602 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3603 vpushi(is_compatible_types(&type1
, &type2
));
3606 case TOK_builtin_constant_p
:
3608 int saved_nocode_wanted
, res
;
3611 saved_nocode_wanted
= nocode_wanted
;
3614 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3616 nocode_wanted
= saved_nocode_wanted
;
3621 case TOK_builtin_frame_address
:
3626 if (tok
!= TOK_CINT
) {
3627 error("__builtin_frame_address only takes integers");
3630 error("TCC only supports __builtin_frame_address(0)");
3636 vset(&type
, VT_LOCAL
, 0);
3639 #ifdef TCC_TARGET_X86_64
3640 case TOK_builtin_va_arg_types
:
3642 /* This definition must be synced with stdarg.h */
3643 enum __va_arg_type
{
3644 __va_gen_reg
, __va_float_reg
, __va_stack
3652 bt
= type
.t
& VT_BTYPE
;
3653 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3655 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3656 vpushi(__va_float_reg
);
3658 vpushi(__va_gen_reg
);
3678 goto tok_identifier
;
3680 /* allow to take the address of a label */
3681 if (tok
< TOK_UIDENT
)
3682 expect("label identifier");
3683 s
= label_find(tok
);
3685 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3687 if (s
->r
== LABEL_DECLARED
)
3688 s
->r
= LABEL_FORWARD
;
3691 s
->type
.t
= VT_VOID
;
3692 mk_pointer(&s
->type
);
3693 s
->type
.t
|= VT_STATIC
;
3695 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3700 // special qnan , snan and infinity values
3702 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3706 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3710 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3719 expect("identifier");
3723 error("'%s' undeclared", get_tok_str(t
, NULL
));
3724 /* for simple function calls, we tolerate undeclared
3725 external reference to int() function */
3726 if (tcc_state
->warn_implicit_function_declaration
)
3727 warning("implicit declaration of function '%s'",
3728 get_tok_str(t
, NULL
));
3729 s
= external_global_sym(t
, &func_old_type
, 0);
3731 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3732 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3733 /* if referencing an inline function, then we generate a
3734 symbol to it if not already done. It will have the
3735 effect to generate code for it at the end of the
3736 compilation unit. Inline function as always
3737 generated in the text section. */
3739 put_extern_sym(s
, text_section
, 0, 0);
3740 r
= VT_SYM
| VT_CONST
;
3744 if (s
->type
.t
& VT_VLA
)
3747 vset(&s
->type
, r
, s
->c
);
3748 /* if forward reference, we must point to s */
3749 if (vtop
->r
& VT_SYM
) {
3756 /* post operations */
3758 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3761 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3764 if (tok
== TOK_ARROW
)
3766 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3770 /* expect pointer on structure */
3771 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3772 expect("struct or union");
3776 while ((s
= s
->next
) != NULL
) {
3781 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3782 /* add field offset to pointer */
3783 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3786 /* change type to field type, and set to lvalue */
3787 vtop
->type
= s
->type
;
3788 vtop
->type
.t
|= qualifiers
;
3789 /* an array is never an lvalue */
3790 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3791 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3792 #ifdef CONFIG_TCC_BCHECK
3793 /* if bound checking, the referenced pointer must be checked */
3794 if (tcc_state
->do_bounds_check
)
3795 vtop
->r
|= VT_MUSTBOUND
;
3799 } else if (tok
== '[') {
3805 } else if (tok
== '(') {
3811 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3812 /* pointer test (no array accepted) */
3813 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3814 vtop
->type
= *pointed_type(&vtop
->type
);
3815 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3819 expect("function pointer");
3822 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3824 /* get return type */
3827 sa
= s
->next
; /* first parameter */
3830 /* compute first implicit argument if a structure is returned */
3831 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3832 /* get some space for the returned structure */
3833 size
= type_size(&s
->type
, &align
);
3834 loc
= (loc
- size
) & -align
;
3836 ret
.r
= VT_LOCAL
| VT_LVAL
;
3837 /* pass it as 'int' to avoid structure arg passing
3839 vseti(VT_LOCAL
, loc
);
3844 /* return in register */
3845 if (is_float(ret
.type
.t
)) {
3846 ret
.r
= reg_fret(ret
.type
.t
);
3848 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3857 gfunc_param_typed(s
, sa
);
3867 error("too few arguments to function");
3869 if (!nocode_wanted
) {
3870 gfunc_call(nb_args
);
3872 vtop
-= (nb_args
+ 1);
3875 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3883 ST_FUNC
void expr_prod(void)
3888 while (tok
== '*' || tok
== '/' || tok
== '%') {
3896 ST_FUNC
void expr_sum(void)
3901 while (tok
== '+' || tok
== '-') {
3909 static void expr_shift(void)
3914 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3922 static void expr_cmp(void)
3927 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3928 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3936 static void expr_cmpeq(void)
3941 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3949 static void expr_and(void)
3952 while (tok
== '&') {
3959 static void expr_xor(void)
3962 while (tok
== '^') {
3969 static void expr_or(void)
3972 while (tok
== '|') {
3979 /* XXX: fix this mess */
3980 static void expr_land_const(void)
3983 while (tok
== TOK_LAND
) {
3990 /* XXX: fix this mess */
3991 static void expr_lor_const(void)
3994 while (tok
== TOK_LOR
) {
4001 /* only used if non constant */
4002 static void expr_land(void)
4007 if (tok
== TOK_LAND
) {
4012 if (tok
!= TOK_LAND
) {
4022 static void expr_lor(void)
4027 if (tok
== TOK_LOR
) {
4032 if (tok
!= TOK_LOR
) {
4042 /* XXX: better constant handling */
4043 static void expr_cond(void)
4045 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4047 CType type
, type1
, type2
;
4054 boolean
.t
= VT_BOOL
;
4060 if (tok
!= ':' || !gnu_ext
) {
4075 if (vtop
!= vstack
) {
4076 /* needed to avoid having different registers saved in
4078 if (is_float(vtop
->type
.t
)) {
4080 #ifdef TCC_TARGET_X86_64
4081 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4091 if (tok
== ':' && gnu_ext
) {
4099 sv
= *vtop
; /* save value to handle it later */
4100 vtop
--; /* no vpop so that FP stack is not flushed */
4108 bt1
= t1
& VT_BTYPE
;
4110 bt2
= t2
& VT_BTYPE
;
4111 /* cast operands to correct type according to ISOC rules */
4112 if (is_float(bt1
) || is_float(bt2
)) {
4113 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4114 type
.t
= VT_LDOUBLE
;
4115 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4120 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4121 /* cast to biggest op */
4123 /* convert to unsigned if it does not fit in a long long */
4124 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4125 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4126 type
.t
|= VT_UNSIGNED
;
4127 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4128 /* XXX: test pointer compatibility */
4130 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4131 /* XXX: test function pointer compatibility */
4133 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4134 /* XXX: test structure compatibility */
4136 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4137 /* NOTE: as an extension, we accept void on only one side */
4140 /* integer operations */
4142 /* convert to unsigned if it does not fit in an integer */
4143 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4144 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4145 type
.t
|= VT_UNSIGNED
;
4148 /* now we convert second operand */
4150 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4153 if (is_float(type
.t
)) {
4155 #ifdef TCC_TARGET_X86_64
4156 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4160 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4161 /* for long longs, we use fixed registers to avoid having
4162 to handle a complicated move */
4167 /* this is horrible, but we must also convert first
4171 /* put again first value and cast it */
4174 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4184 static void expr_eq(void)
4190 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4191 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4192 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4207 ST_FUNC
void gexpr(void)
4218 /* parse an expression and return its type without any side effect. */
4219 static void expr_type(CType
*type
)
4221 int saved_nocode_wanted
;
4223 saved_nocode_wanted
= nocode_wanted
;
4228 nocode_wanted
= saved_nocode_wanted
;
4231 /* parse a unary expression and return its type without any side
4233 static void unary_type(CType
*type
)
4245 /* parse a constant expression and return value in vtop. */
4246 static void expr_const1(void)
4255 /* parse an integer constant and return its value. */
4256 ST_FUNC
int expr_const(void)
4260 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4261 expect("constant expression");
4267 /* return the label token if current token is a label, otherwise
4269 static int is_label(void)
4273 /* fast test first */
4274 if (tok
< TOK_UIDENT
)
4276 /* no need to save tokc because tok is an identifier */
4283 unget_tok(last_tok
);
4288 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4289 int case_reg
, int is_expr
)
4294 /* generate line number info */
4295 if (tcc_state
->do_debug
&&
4296 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4297 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4299 last_line_num
= file
->line_num
;
4303 /* default return value is (void) */
4305 vtop
->type
.t
= VT_VOID
;
4308 if (tok
== TOK_IF
) {
4315 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4317 if (c
== TOK_ELSE
) {
4321 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4322 gsym(d
); /* patch else jmp */
4325 } else if (tok
== TOK_WHILE
) {
4333 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4337 } else if (tok
== '{') {
4342 /* record local declaration stack position */
4344 llabel
= local_label_stack
;
4345 /* record vstack position */
4347 /* handle local labels declarations */
4348 if (tok
== TOK_LABEL
) {
4351 if (tok
< TOK_UIDENT
)
4352 expect("label identifier");
4353 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4363 while (tok
!= '}') {
4368 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4371 /* pop locally defined labels */
4372 label_pop(&local_label_stack
, llabel
);
4373 /* pop left-over VLA size expressions */
4376 /* XXX: this solution makes only valgrind happy...
4377 triggered by gcc.c-torture/execute/20000917-1.c */
4379 switch(vtop
->type
.t
& VT_BTYPE
) {
4384 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4386 error("unsupported expression type");
4389 /* pop locally defined symbols */
4390 sym_pop(&local_stack
, s
);
4392 } else if (tok
== TOK_RETURN
) {
4396 gen_assign_cast(&func_vt
);
4397 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4399 /* if returning structure, must copy it to implicit
4400 first pointer arg location */
4403 size
= type_size(&func_vt
,&align
);
4406 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4410 loc
= (loc
- size
) & -4;
4413 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4416 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4418 vtop
->type
= int_type
;
4424 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4427 /* copy structure value to pointer */
4432 } else if (is_float(func_vt
.t
)) {
4433 gv(rc_fret(func_vt
.t
));
4437 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4440 rsym
= gjmp(rsym
); /* jmp */
4441 } else if (tok
== TOK_BREAK
) {
4444 error("cannot break");
4445 *bsym
= gjmp(*bsym
);
4448 } else if (tok
== TOK_CONTINUE
) {
4451 error("cannot continue");
4452 *csym
= gjmp(*csym
);
4455 } else if (tok
== TOK_FOR
) {
4461 /* c99 for-loop init decl? */
4462 if (!decl0(VT_LOCAL
, 1)) {
4463 /* no, regular for-loop init expr */
4487 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4491 sym_pop(&local_stack
, s
);
4493 if (tok
== TOK_DO
) {
4498 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4509 if (tok
== TOK_SWITCH
) {
4513 /* XXX: other types than integer */
4514 case_reg
= gv(RC_INT
);
4518 b
= gjmp(0); /* jump to first case */
4520 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4521 /* if no default, jmp after switch */
4529 if (tok
== TOK_CASE
) {
4536 if (gnu_ext
&& tok
== TOK_DOTS
) {
4540 warning("empty case range");
4542 /* since a case is like a label, we must skip it with a jmp */
4549 *case_sym
= gtst(1, 0);
4552 *case_sym
= gtst(1, 0);
4556 *case_sym
= gtst(1, *case_sym
);
4561 goto block_after_label
;
4563 if (tok
== TOK_DEFAULT
) {
4569 error("too many 'default'");
4572 goto block_after_label
;
4574 if (tok
== TOK_GOTO
) {
4576 if (tok
== '*' && gnu_ext
) {
4580 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4583 } else if (tok
>= TOK_UIDENT
) {
4584 s
= label_find(tok
);
4585 /* put forward definition if needed */
4587 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4589 if (s
->r
== LABEL_DECLARED
)
4590 s
->r
= LABEL_FORWARD
;
4592 /* label already defined */
4593 if (s
->r
& LABEL_FORWARD
)
4594 s
->jnext
= gjmp(s
->jnext
);
4596 gjmp_addr(s
->jnext
);
4599 expect("label identifier");
4602 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4610 if (s
->r
== LABEL_DEFINED
)
4611 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4613 s
->r
= LABEL_DEFINED
;
4615 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4618 /* we accept this, but it is a mistake */
4621 warning("deprecated use of label at end of compound statement");
4625 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4628 /* expression case */
4643 /* t is the array or struct type. c is the array or struct
4644 address. cur_index/cur_field is the pointer to the current
4645 value. 'size_only' is true if only size info is needed (only used
4647 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4648 int *cur_index
, Sym
**cur_field
,
4652 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4658 if (gnu_ext
&& (l
= is_label()) != 0)
4660 while (tok
== '[' || tok
== '.') {
4662 if (!(type
->t
& VT_ARRAY
))
4663 expect("array type");
4666 index
= expr_const();
4667 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4668 expect("invalid index");
4669 if (tok
== TOK_DOTS
&& gnu_ext
) {
4671 index_last
= expr_const();
4672 if (index_last
< 0 ||
4673 (s
->c
>= 0 && index_last
>= s
->c
) ||
4675 expect("invalid index");
4681 *cur_index
= index_last
;
4682 type
= pointed_type(type
);
4683 elem_size
= type_size(type
, &align
);
4684 c
+= index
* elem_size
;
4685 /* NOTE: we only support ranges for last designator */
4686 nb_elems
= index_last
- index
+ 1;
4687 if (nb_elems
!= 1) {
4696 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4697 expect("struct/union type");
4710 /* XXX: fix this mess by using explicit storage field */
4712 type1
.t
|= (type
->t
& ~VT_TYPE
);
4726 if (type
->t
& VT_ARRAY
) {
4728 type
= pointed_type(type
);
4729 c
+= index
* type_size(type
, &align
);
4733 error("too many field init");
4734 /* XXX: fix this mess by using explicit storage field */
4736 type1
.t
|= (type
->t
& ~VT_TYPE
);
4741 decl_initializer(type
, sec
, c
, 0, size_only
);
4743 /* XXX: make it more general */
4744 if (!size_only
&& nb_elems
> 1) {
4745 unsigned long c_end
;
4750 error("range init not supported yet for dynamic storage");
4751 c_end
= c
+ nb_elems
* elem_size
;
4752 if (c_end
> sec
->data_allocated
)
4753 section_realloc(sec
, c_end
);
4754 src
= sec
->data
+ c
;
4756 for(i
= 1; i
< nb_elems
; i
++) {
4758 memcpy(dst
, src
, elem_size
);
4764 #define EXPR_CONST 1
4767 /* store a value or an expression directly in global data or in local array */
4768 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4769 int v
, int expr_type
)
4771 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4773 unsigned long long bit_mask
;
4781 /* compound literals must be allocated globally in this case */
4782 saved_global_expr
= global_expr
;
4785 global_expr
= saved_global_expr
;
4786 /* NOTE: symbols are accepted */
4787 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4788 error("initializer element is not constant");
4796 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4799 /* XXX: not portable */
4800 /* XXX: generate error if incorrect relocation */
4801 gen_assign_cast(&dtype
);
4802 bt
= type
->t
& VT_BTYPE
;
4803 /* we'll write at most 12 bytes */
4804 if (c
+ 12 > sec
->data_allocated
) {
4805 section_realloc(sec
, c
+ 12);
4807 ptr
= sec
->data
+ c
;
4808 /* XXX: make code faster ? */
4809 if (!(type
->t
& VT_BITFIELD
)) {
4814 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4815 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4816 bit_mask
= (1LL << bit_size
) - 1;
4818 if ((vtop
->r
& VT_SYM
) &&
4824 (bt
== VT_INT
&& bit_size
!= 32)))
4825 error("initializer element is not computable at load time");
4828 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4830 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4833 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4836 *(double *)ptr
= vtop
->c
.d
;
4839 *(long double *)ptr
= vtop
->c
.ld
;
4842 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4845 if (vtop
->r
& VT_SYM
) {
4846 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4848 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4853 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4860 /* put zeros for variable based init */
4861 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4864 /* nothing to do because globals are already set to zero */
4866 vpush_global_sym(&func_old_type
, TOK_memset
);
4874 /* 't' contains the type and storage info. 'c' is the offset of the
4875 object in section 'sec'. If 'sec' is NULL, it means stack based
4876 allocation. 'first' is true if array '{' must be read (multi
4877 dimension implicit array init handling). 'size_only' is true if
4878 size only evaluation is wanted (only for arrays). */
4879 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4880 int first
, int size_only
)
4882 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4883 int size1
, align1
, expr_type
;
4887 if (type
->t
& VT_VLA
) {
4891 vpush_global_sym(&func_old_type
, TOK_alloca
);
4892 vla_runtime_type_size(type
, &a
);
4897 vsetc(type
, REG_IRET
, &retcval
);
4898 } else if (type
->t
& VT_ARRAY
) {
4902 t1
= pointed_type(type
);
4903 size1
= type_size(t1
, &align1
);
4906 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4909 error("character array initializer must be a literal,"
4910 " optionally enclosed in braces");
4915 /* only parse strings here if correct type (otherwise: handle
4916 them as ((w)char *) expressions */
4917 if ((tok
== TOK_LSTR
&&
4918 #ifdef TCC_TARGET_PE
4919 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4921 (t1
->t
& VT_BTYPE
) == VT_INT
4923 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4924 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4929 /* compute maximum number of chars wanted */
4931 cstr_len
= cstr
->size
;
4933 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4936 if (n
>= 0 && nb
> (n
- array_length
))
4937 nb
= n
- array_length
;
4940 warning("initializer-string for array is too long");
4941 /* in order to go faster for common case (char
4942 string in global variable, we handle it
4944 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4945 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4949 ch
= ((unsigned char *)cstr
->data
)[i
];
4951 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4952 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4960 /* only add trailing zero if enough storage (no
4961 warning in this case since it is standard) */
4962 if (n
< 0 || array_length
< n
) {
4964 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4970 while (tok
!= '}') {
4971 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4972 if (n
>= 0 && index
>= n
)
4973 error("index too large");
4974 /* must put zero in holes (note that doing it that way
4975 ensures that it even works with designators) */
4976 if (!size_only
&& array_length
< index
) {
4977 init_putz(t1
, sec
, c
+ array_length
* size1
,
4978 (index
- array_length
) * size1
);
4981 if (index
> array_length
)
4982 array_length
= index
;
4983 /* special test for multi dimensional arrays (may not
4984 be strictly correct if designators are used at the
4986 if (index
>= n
&& no_oblock
)
4995 /* put zeros at the end */
4996 if (!size_only
&& n
>= 0 && array_length
< n
) {
4997 init_putz(t1
, sec
, c
+ array_length
* size1
,
4998 (n
- array_length
) * size1
);
5000 /* patch type size if needed */
5002 s
->c
= array_length
;
5003 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5004 (sec
|| !first
|| tok
== '{')) {
5007 /* NOTE: the previous test is a specific case for automatic
5008 struct/union init */
5009 /* XXX: union needs only one init */
5011 /* XXX: this test is incorrect for local initializers
5012 beginning with ( without {. It would be much more difficult
5013 to do it correctly (ideally, the expression parser should
5014 be used in all cases) */
5020 while (tok
== '(') {
5024 if (!parse_btype(&type1
, &ad1
))
5026 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5028 if (!is_assignable_types(type
, &type1
))
5029 error("invalid type for cast");
5034 if (first
|| tok
== '{') {
5043 while (tok
!= '}') {
5044 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5046 if (!size_only
&& array_length
< index
) {
5047 init_putz(type
, sec
, c
+ array_length
,
5048 index
- array_length
);
5050 index
= index
+ type_size(&f
->type
, &align1
);
5051 if (index
> array_length
)
5052 array_length
= index
;
5054 /* gr: skip fields from same union - ugly. */
5056 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5057 /* test for same offset */
5058 if (f
->next
->c
!= f
->c
)
5060 /* if yes, test for bitfield shift */
5061 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5062 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5063 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5064 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5065 if (bit_pos_1
!= bit_pos_2
)
5072 if (no_oblock
&& f
== NULL
)
5078 /* put zeros at the end */
5079 if (!size_only
&& array_length
< n
) {
5080 init_putz(type
, sec
, c
+ array_length
,
5089 } else if (tok
== '{') {
5091 decl_initializer(type
, sec
, c
, first
, size_only
);
5093 } else if (size_only
) {
5094 /* just skip expression */
5095 parlevel
= parlevel1
= 0;
5096 while ((parlevel
> 0 || parlevel1
> 0 ||
5097 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5100 else if (tok
== ')')
5102 else if (tok
== '{')
5104 else if (tok
== '}')
5109 /* currently, we always use constant expression for globals
5110 (may change for scripting case) */
5111 expr_type
= EXPR_CONST
;
5113 expr_type
= EXPR_ANY
;
5114 init_putv(type
, sec
, c
, 0, expr_type
);
5118 /* parse an initializer for type 't' if 'has_init' is non zero, and
5119 allocate space in local or global data space ('r' is either
5120 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5121 variable 'v' with an associated name represented by 'asm_label' of
5122 scope 'scope' is declared before initializers are parsed. If 'v' is
5123 zero, then a reference to the new object is put in the value stack.
5124 If 'has_init' is 2, a special parsing is done to handle string
5126 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5127 int has_init
, int v
, char *asm_label
,
5130 int size
, align
, addr
, data_offset
;
5132 ParseState saved_parse_state
= {0};
5133 TokenString init_str
;
5136 Sym
*flexible_array
;
5138 flexible_array
= NULL
;
5139 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5142 while (field
&& field
->next
)
5143 field
= field
->next
;
5144 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5145 flexible_array
= field
;
5148 size
= type_size(type
, &align
);
5149 /* If unknown size, we must evaluate it before
5150 evaluating initializers because
5151 initializers can generate global data too
5152 (e.g. string pointers or ISOC99 compound
5153 literals). It also simplifies local
5154 initializers handling */
5155 tok_str_new(&init_str
);
5156 if (size
< 0 || flexible_array
) {
5158 error("unknown type size");
5159 /* get all init string */
5160 if (has_init
== 2) {
5161 /* only get strings */
5162 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5163 tok_str_add_tok(&init_str
);
5168 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5170 error("unexpected end of file in initializer");
5171 tok_str_add_tok(&init_str
);
5174 else if (tok
== '}') {
5184 tok_str_add(&init_str
, -1);
5185 tok_str_add(&init_str
, 0);
5188 save_parse_state(&saved_parse_state
);
5190 macro_ptr
= init_str
.str
;
5192 decl_initializer(type
, NULL
, 0, 1, 1);
5193 /* prepare second initializer parsing */
5194 macro_ptr
= init_str
.str
;
5197 /* if still unknown size, error */
5198 size
= type_size(type
, &align
);
5200 error("unknown type size");
5203 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5204 /* take into account specified alignment if bigger */
5206 if (ad
->aligned
> align
)
5207 align
= ad
->aligned
;
5208 } else if (ad
->packed
) {
5211 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5213 #ifdef CONFIG_TCC_BCHECK
5214 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5218 loc
= (loc
- size
) & -align
;
5220 #ifdef CONFIG_TCC_BCHECK
5221 /* handles bounds */
5222 /* XXX: currently, since we do only one pass, we cannot track
5223 '&' operators, so we add only arrays */
5224 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5225 unsigned long *bounds_ptr
;
5226 /* add padding between regions */
5228 /* then add local bound info */
5229 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5230 bounds_ptr
[0] = addr
;
5231 bounds_ptr
[1] = size
;
5235 /* local variable */
5236 vla
= sym_push(v
, type
, r
, addr
);
5238 /* push local reference */
5239 vset(type
, r
, addr
);
5245 if (v
&& scope
== VT_CONST
) {
5246 /* see if the symbol was already defined */
5249 if (!is_compatible_types(&sym
->type
, type
))
5250 error("incompatible types for redefinition of '%s'",
5251 get_tok_str(v
, NULL
));
5252 if (sym
->type
.t
& VT_EXTERN
) {
5253 /* if the variable is extern, it was not allocated */
5254 sym
->type
.t
&= ~VT_EXTERN
;
5255 /* set array size if it was ommited in extern
5257 if ((sym
->type
.t
& VT_ARRAY
) &&
5258 sym
->type
.ref
->c
< 0 &&
5260 sym
->type
.ref
->c
= type
->ref
->c
;
5262 /* we accept several definitions of the same
5263 global variable. this is tricky, because we
5264 must play with the SHN_COMMON type of the symbol */
5265 /* XXX: should check if the variable was already
5266 initialized. It is incorrect to initialized it
5268 /* no init data, we won't add more to the symbol */
5275 /* allocate symbol in corresponding section */
5280 else if (tcc_state
->nocommon
)
5284 data_offset
= sec
->data_offset
;
5285 data_offset
= (data_offset
+ align
- 1) & -align
;
5287 /* very important to increment global pointer at this time
5288 because initializers themselves can create new initializers */
5289 data_offset
+= size
;
5290 #ifdef CONFIG_TCC_BCHECK
5291 /* add padding if bound check */
5292 if (tcc_state
->do_bounds_check
)
5295 sec
->data_offset
= data_offset
;
5296 /* allocate section space to put the data */
5297 if (sec
->sh_type
!= SHT_NOBITS
&&
5298 data_offset
> sec
->data_allocated
)
5299 section_realloc(sec
, data_offset
);
5300 /* align section if needed */
5301 if (align
> sec
->sh_addralign
)
5302 sec
->sh_addralign
= align
;
5304 addr
= 0; /* avoid warning */
5308 if (scope
!= VT_CONST
|| !sym
) {
5309 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5310 sym
->asm_label
= asm_label
;
5312 /* update symbol definition */
5314 put_extern_sym(sym
, sec
, addr
, size
);
5317 /* put a common area */
5318 put_extern_sym(sym
, NULL
, align
, size
);
5319 /* XXX: find a nicer way */
5320 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5321 esym
->st_shndx
= SHN_COMMON
;
5326 /* push global reference */
5327 sym
= get_sym_ref(type
, sec
, addr
, size
);
5329 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5332 /* patch symbol weakness */
5333 if (type
->t
& VT_WEAK
)
5335 #ifdef CONFIG_TCC_BCHECK
5336 /* handles bounds now because the symbol must be defined
5337 before for the relocation */
5338 if (tcc_state
->do_bounds_check
) {
5339 unsigned long *bounds_ptr
;
5341 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5342 /* then add global bound info */
5343 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5344 bounds_ptr
[0] = 0; /* relocated */
5345 bounds_ptr
[1] = size
;
5349 if (has_init
|| (type
->t
& VT_VLA
)) {
5350 decl_initializer(type
, sec
, addr
, 1, 0);
5351 if (type
->t
& VT_VLA
)
5353 /* restore parse state if needed */
5355 tok_str_free(init_str
.str
);
5356 restore_parse_state(&saved_parse_state
);
5358 /* patch flexible array member size back to -1, */
5359 /* for possible subsequent similar declarations */
5361 flexible_array
->type
.ref
->c
= -1;
5366 static void put_func_debug(Sym
*sym
)
5371 /* XXX: we put here a dummy type */
5372 snprintf(buf
, sizeof(buf
), "%s:%c1",
5373 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5374 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5375 cur_text_section
, sym
->c
);
5376 /* //gr gdb wants a line at the function */
5377 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5382 /* parse an old style function declaration list */
5383 /* XXX: check multiple parameter */
5384 static void func_decl_list(Sym
*func_sym
)
5391 /* parse each declaration */
5392 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5393 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5394 if (!parse_btype(&btype
, &ad
))
5395 expect("declaration list");
5396 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5397 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5399 /* we accept no variable after */
5403 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5404 /* find parameter in function parameter list */
5407 if ((s
->v
& ~SYM_FIELD
) == v
)
5411 error("declaration for parameter '%s' but no such parameter",
5412 get_tok_str(v
, NULL
));
5414 /* check that no storage specifier except 'register' was given */
5415 if (type
.t
& VT_STORAGE
)
5416 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5417 convert_parameter_type(&type
);
5418 /* we can add the type (NOTE: it could be local to the function) */
5420 /* accept other parameters */
5431 /* parse a function defined by symbol 'sym' and generate its code in
5432 'cur_text_section' */
5433 static void gen_function(Sym
*sym
)
5435 int saved_nocode_wanted
= nocode_wanted
;
5437 ind
= cur_text_section
->data_offset
;
5438 /* NOTE: we patch the symbol size later */
5439 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5440 funcname
= get_tok_str(sym
->v
, NULL
);
5442 /* put debug symbol */
5443 if (tcc_state
->do_debug
)
5444 put_func_debug(sym
);
5445 /* push a dummy symbol to enable local sym storage */
5446 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5447 gfunc_prolog(&sym
->type
);
5449 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5452 cur_text_section
->data_offset
= ind
;
5453 label_pop(&global_label_stack
, NULL
);
5454 sym_pop(&local_stack
, NULL
); /* reset local stack */
5455 /* end of function */
5456 /* patch symbol size */
5457 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5459 /* patch symbol weakness (this definition overrules any prototype) */
5460 if (sym
->type
.t
& VT_WEAK
)
5462 if (tcc_state
->do_debug
) {
5463 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5465 /* It's better to crash than to generate wrong code */
5466 cur_text_section
= NULL
;
5467 funcname
= ""; /* for safety */
5468 func_vt
.t
= VT_VOID
; /* for safety */
5469 ind
= 0; /* for safety */
5470 nocode_wanted
= saved_nocode_wanted
;
5473 ST_FUNC
void gen_inline_functions(void)
5476 int *str
, inline_generated
, i
;
5477 struct InlineFunc
*fn
;
5479 /* iterate while inline function are referenced */
5481 inline_generated
= 0;
5482 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5483 fn
= tcc_state
->inline_fns
[i
];
5485 if (sym
&& sym
->c
) {
5486 /* the function was used: generate its code and
5487 convert it to a normal function */
5488 str
= fn
->token_str
;
5491 strcpy(file
->filename
, fn
->filename
);
5492 sym
->r
= VT_SYM
| VT_CONST
;
5493 sym
->type
.t
&= ~VT_INLINE
;
5497 cur_text_section
= text_section
;
5499 macro_ptr
= NULL
; /* fail safe */
5501 inline_generated
= 1;
5504 if (!inline_generated
)
5507 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5508 fn
= tcc_state
->inline_fns
[i
];
5509 str
= fn
->token_str
;
5512 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5515 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5516 static int decl0(int l
, int is_for_loop_init
)
5524 if (!parse_btype(&btype
, &ad
)) {
5525 if (is_for_loop_init
)
5527 /* skip redundant ';' */
5528 /* XXX: find more elegant solution */
5533 if (l
== VT_CONST
&&
5534 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5535 /* global asm block */
5539 /* special test for old K&R protos without explicit int
5540 type. Only accepted when defining global data */
5541 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5545 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5546 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5548 /* we accept no variable after */
5552 while (1) { /* iterate thru each declaration */
5553 char *asm_label
; // associated asm label
5555 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5559 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5560 printf("type = '%s'\n", buf
);
5563 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5564 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5565 error("function without file scope cannot be static");
5567 /* if old style function prototype, we accept a
5570 if (sym
->c
== FUNC_OLD
)
5571 func_decl_list(sym
);
5575 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5578 asm_label_instr(&astr
);
5579 asm_label
= tcc_strdup(astr
.data
);
5582 /* parse one last attribute list, after asm label */
5583 parse_attribute(&ad
);
5588 #ifdef TCC_TARGET_PE
5590 type
.t
|= VT_IMPORT
;
5592 type
.t
|= VT_EXPORT
;
5596 error("cannot use local functions");
5597 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5598 expect("function definition");
5600 /* reject abstract declarators in function definition */
5602 while ((sym
= sym
->next
) != NULL
)
5603 if (!(sym
->v
& ~SYM_FIELD
))
5604 expect("identifier");
5606 /* XXX: cannot do better now: convert extern line to static inline */
5607 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5608 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5612 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5615 r
= sym
->type
.ref
->r
;
5616 /* use func_call from prototype if not defined */
5617 if (FUNC_CALL(r
) != FUNC_CDECL
5618 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5619 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5621 /* use export from prototype */
5623 FUNC_EXPORT(type
.ref
->r
) = 1;
5625 /* use static from prototype */
5626 if (sym
->type
.t
& VT_STATIC
)
5627 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5629 if (!is_compatible_types(&sym
->type
, &type
)) {
5631 error("incompatible types for redefinition of '%s'",
5632 get_tok_str(v
, NULL
));
5634 /* if symbol is already defined, then put complete type */
5637 /* put function symbol */
5638 sym
= global_identifier_push(v
, type
.t
, 0);
5639 sym
->type
.ref
= type
.ref
;
5642 /* static inline functions are just recorded as a kind
5643 of macro. Their code will be emitted at the end of
5644 the compilation unit only if they are used */
5645 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5646 (VT_INLINE
| VT_STATIC
)) {
5647 TokenString func_str
;
5649 struct InlineFunc
*fn
;
5650 const char *filename
;
5652 tok_str_new(&func_str
);
5658 error("unexpected end of file");
5659 tok_str_add_tok(&func_str
);
5664 } else if (t
== '}') {
5666 if (block_level
== 0)
5670 tok_str_add(&func_str
, -1);
5671 tok_str_add(&func_str
, 0);
5672 filename
= file
? file
->filename
: "";
5673 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5674 strcpy(fn
->filename
, filename
);
5676 fn
->token_str
= func_str
.str
;
5677 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5680 /* compute text section */
5681 cur_text_section
= ad
.section
;
5682 if (!cur_text_section
)
5683 cur_text_section
= text_section
;
5684 sym
->r
= VT_SYM
| VT_CONST
;
5689 if (btype
.t
& VT_TYPEDEF
) {
5690 /* save typedefed type */
5691 /* XXX: test storage specifiers ? */
5692 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5693 sym
->type
.t
|= VT_TYPEDEF
;
5696 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5697 /* external function definition */
5698 /* specific case for func_call attribute */
5699 type
.ref
->r
= INT_ATTR(&ad
);
5700 } else if (!(type
.t
& VT_ARRAY
)) {
5701 /* not lvalue if array */
5702 r
|= lvalue_type(type
.t
);
5704 has_init
= (tok
== '=');
5705 if (has_init
&& (type
.t
& VT_VLA
))
5706 error("Variable length array cannot be initialized");
5707 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5708 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5709 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5710 /* external variable or function */
5711 /* NOTE: as GCC, uninitialized global static
5712 arrays of null size are considered as
5714 sym
= external_sym(v
, &type
, r
, asm_label
);
5716 if (type
.t
& VT_WEAK
)
5719 if (ad
.alias_target
) {
5724 alias_target
= sym_find(ad
.alias_target
);
5725 if (!alias_target
|| !alias_target
->c
)
5726 error("unsupported forward __alias__ attribute");
5727 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5728 tsec
.sh_num
= esym
->st_shndx
;
5729 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5732 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5733 if (type
.t
& VT_STATIC
)
5739 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5743 if (is_for_loop_init
)
5755 ST_FUNC
void decl(int l
)