2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
57 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
59 ST_DATA
int const_wanted
; /* true if constant wanted */
60 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
61 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
64 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
65 ST_DATA
char *funcname
;
67 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType
*type
);
71 static inline CType
*pointed_type(CType
*type
);
72 static int is_compatible_types(CType
*type1
, CType
*type2
);
73 static int parse_btype(CType
*type
, AttributeDef
*ad
);
74 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
75 static void parse_expr_type(CType
*type
);
76 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
77 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
78 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
79 static void expr_eq(void);
80 static void unary_type(CType
*type
);
81 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
82 static void expr_type(CType
*type
);
84 ST_INLN
int is_float(int t
)
88 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
91 ST_FUNC
void test_lvalue(void)
93 if (!(vtop
->r
& VT_LVAL
))
97 /* ------------------------------------------------------------------------- */
98 /* symbol allocator */
99 static Sym
*__sym_malloc(void)
101 Sym
*sym_pool
, *sym
, *last_sym
;
104 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
105 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
107 last_sym
= sym_free_first
;
109 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
110 sym
->next
= last_sym
;
114 sym_free_first
= last_sym
;
118 static inline Sym
*sym_malloc(void)
121 sym
= sym_free_first
;
123 sym
= __sym_malloc();
124 sym_free_first
= sym
->next
;
128 ST_INLN
void sym_free(Sym
*sym
)
130 sym
->next
= sym_free_first
;
131 sym_free_first
= sym
;
134 /* push, without hashing */
135 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
153 /* find a symbol and return its associated structure. 's' is the top
154 of the symbol stack */
155 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
165 /* structure lookup */
166 ST_INLN Sym
*struct_find(int v
)
169 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
171 return table_ident
[v
]->sym_struct
;
174 /* find an identifier */
175 ST_INLN Sym
*sym_find(int v
)
178 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
180 return table_ident
[v
]->sym_identifier
;
183 /* push a given symbol on the symbol stack */
184 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
193 s
= sym_push2(ps
, v
, type
->t
, c
);
194 s
->type
.ref
= type
->ref
;
196 /* don't record fields or anonymous symbols */
198 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
199 /* record symbol in token array */
200 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
202 ps
= &ts
->sym_struct
;
204 ps
= &ts
->sym_identifier
;
211 /* push a global identifier */
212 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
215 s
= sym_push2(&global_stack
, v
, t
, c
);
216 /* don't record anonymous symbol */
217 if (v
< SYM_FIRST_ANOM
) {
218 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
219 /* modify the top most local identifier, so that
220 sym_identifier will point to 's' when popped */
222 ps
= &(*ps
)->prev_tok
;
229 /* pop symbols until top reaches 'b' */
230 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
240 /* remove symbol in token array */
242 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
243 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
245 ps
= &ts
->sym_struct
;
247 ps
= &ts
->sym_identifier
;
256 /* ------------------------------------------------------------------------- */
258 ST_FUNC
void swap(int *p
, int *q
)
266 static void vsetc(CType
*type
, int r
, CValue
*vc
)
270 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
271 error("memory full");
272 /* cannot let cpu flags if other instruction are generated. Also
273 avoid leaving VT_JMP anywhere except on the top of the stack
274 because it would complicate the code generator. */
275 if (vtop
>= vstack
) {
276 v
= vtop
->r
& VT_VALMASK
;
277 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
287 /* push constant of type "type" with useless value */
288 void vpush(CType
*type
)
291 vsetc(type
, VT_CONST
, &cval
);
294 /* push integer constant */
295 ST_FUNC
void vpushi(int v
)
299 vsetc(&int_type
, VT_CONST
, &cval
);
302 /* push long long constant */
303 static void vpushll(long long v
)
310 vsetc(&ctype
, VT_CONST
, &cval
);
313 /* push arbitrary 64bit constant */
314 void vpush64(int ty
, unsigned long long v
)
320 vsetc(&ctype
, VT_CONST
, &cval
);
323 /* Return a static symbol pointing to a section */
324 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
330 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
331 sym
->type
.ref
= type
->ref
;
332 sym
->r
= VT_CONST
| VT_SYM
;
333 put_extern_sym(sym
, sec
, offset
, size
);
337 /* push a reference to a section offset by adding a dummy symbol */
338 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
343 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
344 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
347 /* define a new external reference to a symbol 'v' of type 'u' */
348 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
354 /* push forward reference */
355 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
356 s
->type
.ref
= type
->ref
;
357 s
->r
= r
| VT_CONST
| VT_SYM
;
362 /* define a new external reference to a symbol 'v' of type 'u' */
363 static Sym
*external_sym(int v
, CType
*type
, int r
)
369 /* push forward reference */
370 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
371 s
->type
.t
|= VT_EXTERN
;
372 } else if (s
->type
.ref
== func_old_type
.ref
) {
373 s
->type
.ref
= type
->ref
;
374 s
->r
= r
| VT_CONST
| VT_SYM
;
375 s
->type
.t
|= VT_EXTERN
;
376 } else if (!is_compatible_types(&s
->type
, type
)) {
377 error("incompatible types for redefinition of '%s'",
378 get_tok_str(v
, NULL
));
383 /* push a reference to global symbol v */
384 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
389 sym
= external_global_sym(v
, type
, 0);
391 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
395 ST_FUNC
void vset(CType
*type
, int r
, int v
)
400 vsetc(type
, r
, &cval
);
403 static void vseti(int r
, int v
)
411 ST_FUNC
void vswap(void)
420 ST_FUNC
void vpushv(SValue
*v
)
422 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
423 error("memory full");
428 static void vdup(void)
433 /* save r to the memory stack, and mark it as being free */
434 ST_FUNC
void save_reg(int r
)
436 int l
, saved
, size
, align
;
440 /* modify all stack values */
443 for(p
=vstack
;p
<=vtop
;p
++) {
444 if ((p
->r
& VT_VALMASK
) == r
||
445 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
446 /* must save value on stack if not already done */
448 /* NOTE: must reload 'r' because r might be equal to r2 */
449 r
= p
->r
& VT_VALMASK
;
450 /* store register in the stack */
452 if ((p
->r
& VT_LVAL
) ||
453 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
454 #ifdef TCC_TARGET_X86_64
455 type
= &char_pointer_type
;
459 size
= type_size(type
, &align
);
460 loc
= (loc
- size
) & -align
;
462 sv
.r
= VT_LOCAL
| VT_LVAL
;
465 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
466 /* x86 specific: need to pop fp register ST0 if saved */
468 o(0xd8dd); /* fstp %st(0) */
471 #ifndef TCC_TARGET_X86_64
472 /* special long long case */
473 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
481 /* mark that stack entry as being saved on the stack */
482 if (p
->r
& VT_LVAL
) {
483 /* also clear the bounded flag because the
484 relocation address of the function was stored in
486 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
488 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
496 #ifdef TCC_TARGET_ARM
497 /* find a register of class 'rc2' with at most one reference on stack.
498 * If none, call get_reg(rc) */
499 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
504 for(r
=0;r
<NB_REGS
;r
++) {
505 if (reg_classes
[r
] & rc2
) {
508 for(p
= vstack
; p
<= vtop
; p
++) {
509 if ((p
->r
& VT_VALMASK
) == r
||
510 (p
->r2
& VT_VALMASK
) == r
)
521 /* find a free register of class 'rc'. If none, save one register */
522 ST_FUNC
int get_reg(int rc
)
527 /* find a free register */
528 for(r
=0;r
<NB_REGS
;r
++) {
529 if (reg_classes
[r
] & rc
) {
530 for(p
=vstack
;p
<=vtop
;p
++) {
531 if ((p
->r
& VT_VALMASK
) == r
||
532 (p
->r2
& VT_VALMASK
) == r
)
540 /* no register left : free the first one on the stack (VERY
541 IMPORTANT to start from the bottom to ensure that we don't
542 spill registers used in gen_opi()) */
543 for(p
=vstack
;p
<=vtop
;p
++) {
544 r
= p
->r
& VT_VALMASK
;
545 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
547 /* also look at second register (if long long) */
548 r
= p
->r2
& VT_VALMASK
;
549 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
555 /* Should never comes here */
559 /* save registers up to (vtop - n) stack entry */
560 ST_FUNC
void save_regs(int n
)
565 for(p
= vstack
;p
<= p1
; p
++) {
566 r
= p
->r
& VT_VALMASK
;
573 /* move register 's' to 'r', and flush previous value of r to memory
575 static void move_reg(int r
, int s
)
588 /* get address of vtop (vtop MUST BE an lvalue) */
589 static void gaddrof(void)
592 /* tricky: if saved lvalue, then we can go back to lvalue */
593 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
594 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
597 #ifdef CONFIG_TCC_BCHECK
598 /* generate lvalue bound code */
599 static void gbound(void)
604 vtop
->r
&= ~VT_MUSTBOUND
;
605 /* if lvalue, then use checking code before dereferencing */
606 if (vtop
->r
& VT_LVAL
) {
607 /* if not VT_BOUNDED value, then make one */
608 if (!(vtop
->r
& VT_BOUNDED
)) {
609 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
610 /* must save type because we must set it to int to get pointer */
612 vtop
->type
.t
= VT_INT
;
615 gen_bounded_ptr_add();
616 vtop
->r
|= lval_type
;
619 /* then check for dereferencing */
620 gen_bounded_ptr_deref();
625 /* store vtop a register belonging to class 'rc'. lvalues are
626 converted to values. Cannot be used if cannot be converted to
627 register value (such as structures). */
628 ST_FUNC
int gv(int rc
)
630 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
632 /* NOTE: get_reg can modify vstack[] */
633 if (vtop
->type
.t
& VT_BITFIELD
) {
636 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
637 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
638 /* remove bit field info to avoid loops */
639 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
640 /* cast to int to propagate signedness in following ops */
641 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
646 if((vtop
->type
.t
& VT_UNSIGNED
) ||
647 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
648 type
.t
|= VT_UNSIGNED
;
650 /* generate shifts */
651 vpushi(bits
- (bit_pos
+ bit_size
));
653 vpushi(bits
- bit_size
);
654 /* NOTE: transformed to SHR if unsigned */
658 if (is_float(vtop
->type
.t
) &&
659 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
662 unsigned long offset
;
663 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
667 /* XXX: unify with initializers handling ? */
668 /* CPUs usually cannot use float constants, so we store them
669 generically in data segment */
670 size
= type_size(&vtop
->type
, &align
);
671 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
672 data_section
->data_offset
= offset
;
673 /* XXX: not portable yet */
674 #if defined(__i386__) || defined(__x86_64__)
675 /* Zero pad x87 tenbyte long doubles */
676 if (size
== LDOUBLE_SIZE
)
677 vtop
->c
.tab
[2] &= 0xffff;
679 ptr
= section_ptr_add(data_section
, size
);
681 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
685 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
689 ptr
[i
] = vtop
->c
.tab
[i
];
690 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
691 vtop
->r
|= VT_LVAL
| VT_SYM
;
695 #ifdef CONFIG_TCC_BCHECK
696 if (vtop
->r
& VT_MUSTBOUND
)
700 r
= vtop
->r
& VT_VALMASK
;
704 /* need to reload if:
706 - lvalue (need to dereference pointer)
707 - already a register, but not in the right class */
709 || (vtop
->r
& VT_LVAL
)
710 || !(reg_classes
[r
] & rc
)
711 #ifndef TCC_TARGET_X86_64
712 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
717 #ifndef TCC_TARGET_X86_64
718 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
720 unsigned long long ll
;
721 /* two register type load : expand to two words
723 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
726 vtop
->c
.ui
= ll
; /* first word */
728 vtop
->r
= r
; /* save register value */
729 vpushi(ll
>> 32); /* second word */
730 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
731 (vtop
->r
& VT_LVAL
)) {
732 /* We do not want to modifier the long long
733 pointer here, so the safest (and less
734 efficient) is to save all the other registers
735 in the stack. XXX: totally inefficient. */
737 /* load from memory */
740 vtop
[-1].r
= r
; /* save register value */
741 /* increment pointer to get second word */
742 vtop
->type
.t
= VT_INT
;
751 vtop
[-1].r
= r
; /* save register value */
752 vtop
->r
= vtop
[-1].r2
;
754 /* allocate second register */
758 /* write second register */
762 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
764 /* lvalue of scalar type : need to use lvalue type
765 because of possible cast */
768 /* compute memory access type */
769 if (vtop
->r
& VT_LVAL_BYTE
)
771 else if (vtop
->r
& VT_LVAL_SHORT
)
773 if (vtop
->r
& VT_LVAL_UNSIGNED
)
777 /* restore wanted type */
780 /* one register type load */
785 #ifdef TCC_TARGET_C67
786 /* uses register pairs for doubles */
787 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
794 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
795 ST_FUNC
void gv2(int rc1
, int rc2
)
799 /* generate more generic register first. But VT_JMP or VT_CMP
800 values must be generated first in all cases to avoid possible
802 v
= vtop
[0].r
& VT_VALMASK
;
803 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
808 /* test if reload is needed for first register */
809 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
819 /* test if reload is needed for first register */
820 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
826 /* wrapper around RC_FRET to return a register by type */
827 static int rc_fret(int t
)
829 #ifdef TCC_TARGET_X86_64
830 if (t
== VT_LDOUBLE
) {
837 /* wrapper around REG_FRET to return a register by type */
838 static int reg_fret(int t
)
840 #ifdef TCC_TARGET_X86_64
841 if (t
== VT_LDOUBLE
) {
848 /* expand long long on stack in two int registers */
849 static void lexpand(void)
853 u
= vtop
->type
.t
& VT_UNSIGNED
;
856 vtop
[0].r
= vtop
[-1].r2
;
857 vtop
[0].r2
= VT_CONST
;
858 vtop
[-1].r2
= VT_CONST
;
859 vtop
[0].type
.t
= VT_INT
| u
;
860 vtop
[-1].type
.t
= VT_INT
| u
;
863 #ifdef TCC_TARGET_ARM
864 /* expand long long on stack */
865 ST_FUNC
void lexpand_nr(void)
869 u
= vtop
->type
.t
& VT_UNSIGNED
;
872 vtop
->type
.t
= VT_INT
| u
;
873 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
875 vtop
[-1].c
.ui
= vtop
->c
.ull
;
876 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
878 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
880 vtop
->r
= vtop
[-1].r
;
881 } else if (v
> VT_CONST
) {
885 vtop
->r
= vtop
[-1].r2
;
886 vtop
[-1].r2
= VT_CONST
;
887 vtop
[-1].type
.t
= VT_INT
| u
;
891 /* build a long long from two ints */
892 static void lbuild(int t
)
895 vtop
[-1].r2
= vtop
[0].r
;
900 /* rotate n first stack elements to the bottom
901 I1 ... In -> I2 ... In I1 [top is right]
903 static void vrotb(int n
)
914 /* rotate n first stack elements to the top
915 I1 ... In -> In I1 ... I(n-1) [top is right]
917 ST_FUNC
void vrott(int n
)
923 for(i
= 0;i
< n
- 1; i
++)
924 vtop
[-i
] = vtop
[-i
- 1];
928 #ifdef TCC_TARGET_ARM
929 /* like vrott but in other direction
930 In ... I1 -> I(n-1) ... I1 In [top is right]
932 ST_FUNC
void vnrott(int n
)
938 for(i
= n
- 1; i
> 0; i
--)
939 vtop
[-i
] = vtop
[-i
+ 1];
944 /* pop stack value */
945 ST_FUNC
void vpop(void)
948 v
= vtop
->r
& VT_VALMASK
;
949 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
950 /* for x86, we need to pop the FP stack */
951 if (v
== TREG_ST0
&& !nocode_wanted
) {
952 o(0xd8dd); /* fstp %st(0) */
955 if (v
== VT_JMP
|| v
== VT_JMPI
) {
956 /* need to put correct jump if && or || without test */
962 /* convert stack entry to register and duplicate its value in another
964 static void gv_dup(void)
970 if ((t
& VT_BTYPE
) == VT_LLONG
) {
977 /* stack: H L L1 H1 */
985 /* duplicate value */
990 #ifdef TCC_TARGET_X86_64
991 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1001 load(r1
, &sv
); /* move r to r1 */
1003 /* duplicates value */
1009 #ifndef TCC_TARGET_X86_64
1010 /* generate CPU independent (unsigned) long long operations */
1011 static void gen_opl(int op
)
1013 int t
, a
, b
, op1
, c
, i
;
1015 unsigned short reg_iret
= REG_IRET
;
1016 unsigned short reg_lret
= REG_LRET
;
1022 func
= TOK___divdi3
;
1025 func
= TOK___udivdi3
;
1028 func
= TOK___moddi3
;
1031 func
= TOK___umoddi3
;
1038 /* call generic long long function */
1039 vpush_global_sym(&func_old_type
, func
);
1044 vtop
->r2
= reg_lret
;
1057 /* stack: L1 H1 L2 H2 */
1062 vtop
[-2] = vtop
[-3];
1065 /* stack: H1 H2 L1 L2 */
1071 /* stack: H1 H2 L1 L2 ML MH */
1074 /* stack: ML MH H1 H2 L1 L2 */
1078 /* stack: ML MH H1 L2 H2 L1 */
1083 /* stack: ML MH M1 M2 */
1086 } else if (op
== '+' || op
== '-') {
1087 /* XXX: add non carry method too (for MIPS or alpha) */
1093 /* stack: H1 H2 (L1 op L2) */
1096 gen_op(op1
+ 1); /* TOK_xxxC2 */
1099 /* stack: H1 H2 (L1 op L2) */
1102 /* stack: (L1 op L2) H1 H2 */
1104 /* stack: (L1 op L2) (H1 op H2) */
1112 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1113 t
= vtop
[-1].type
.t
;
1117 /* stack: L H shift */
1119 /* constant: simpler */
1120 /* NOTE: all comments are for SHL. the other cases are
1121 done by swaping words */
1132 if (op
!= TOK_SAR
) {
1165 /* XXX: should provide a faster fallback on x86 ? */
1168 func
= TOK___ashrdi3
;
1171 func
= TOK___lshrdi3
;
1174 func
= TOK___ashldi3
;
1180 /* compare operations */
1186 /* stack: L1 H1 L2 H2 */
1188 vtop
[-1] = vtop
[-2];
1190 /* stack: L1 L2 H1 H2 */
1193 /* when values are equal, we need to compare low words. since
1194 the jump is inverted, we invert the test too. */
1197 else if (op1
== TOK_GT
)
1199 else if (op1
== TOK_ULT
)
1201 else if (op1
== TOK_UGT
)
1206 if (op1
!= TOK_NE
) {
1210 /* generate non equal test */
1211 /* XXX: NOT PORTABLE yet */
1215 #if defined(TCC_TARGET_I386)
1216 b
= psym(0x850f, 0);
1217 #elif defined(TCC_TARGET_ARM)
1219 o(0x1A000000 | encbranch(ind
, 0, 1));
1220 #elif defined(TCC_TARGET_C67)
1221 error("not implemented");
1223 #error not supported
1227 /* compare low. Always unsigned */
1231 else if (op1
== TOK_LE
)
1233 else if (op1
== TOK_GT
)
1235 else if (op1
== TOK_GE
)
1246 /* handle integer constant optimizations and various machine
1248 static void gen_opic(int op
)
1250 int c1
, c2
, t1
, t2
, n
;
1253 typedef unsigned long long U
;
1257 t1
= v1
->type
.t
& VT_BTYPE
;
1258 t2
= v2
->type
.t
& VT_BTYPE
;
1262 else if (v1
->type
.t
& VT_UNSIGNED
)
1269 else if (v2
->type
.t
& VT_UNSIGNED
)
1274 /* currently, we cannot do computations with forward symbols */
1275 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1276 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1279 case '+': l1
+= l2
; break;
1280 case '-': l1
-= l2
; break;
1281 case '&': l1
&= l2
; break;
1282 case '^': l1
^= l2
; break;
1283 case '|': l1
|= l2
; break;
1284 case '*': l1
*= l2
; break;
1291 /* if division by zero, generate explicit division */
1294 error("division by zero in constant");
1298 default: l1
/= l2
; break;
1299 case '%': l1
%= l2
; break;
1300 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1301 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1304 case TOK_SHL
: l1
<<= l2
; break;
1305 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1306 case TOK_SAR
: l1
>>= l2
; break;
1308 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1309 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1310 case TOK_EQ
: l1
= l1
== l2
; break;
1311 case TOK_NE
: l1
= l1
!= l2
; break;
1312 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1313 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1314 case TOK_LT
: l1
= l1
< l2
; break;
1315 case TOK_GE
: l1
= l1
>= l2
; break;
1316 case TOK_LE
: l1
= l1
<= l2
; break;
1317 case TOK_GT
: l1
= l1
> l2
; break;
1319 case TOK_LAND
: l1
= l1
&& l2
; break;
1320 case TOK_LOR
: l1
= l1
|| l2
; break;
1327 /* if commutative ops, put c2 as constant */
1328 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1329 op
== '|' || op
== '*')) {
1331 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1332 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1334 /* Filter out NOP operations like x*1, x-0, x&-1... */
1335 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1338 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1339 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1345 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1346 /* try to use shifts instead of muls or divs */
1347 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1356 else if (op
== TOK_PDIV
)
1362 } else if (c2
&& (op
== '+' || op
== '-') &&
1363 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1364 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1365 /* symbol + constant case */
1372 if (!nocode_wanted
) {
1373 /* call low level op generator */
1374 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1385 /* generate a floating point operation with constant propagation */
1386 static void gen_opif(int op
)
1394 /* currently, we cannot do computations with forward symbols */
1395 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1396 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1398 if (v1
->type
.t
== VT_FLOAT
) {
1401 } else if (v1
->type
.t
== VT_DOUBLE
) {
1409 /* NOTE: we only do constant propagation if finite number (not
1410 NaN or infinity) (ANSI spec) */
1411 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1415 case '+': f1
+= f2
; break;
1416 case '-': f1
-= f2
; break;
1417 case '*': f1
*= f2
; break;
1421 error("division by zero in constant");
1426 /* XXX: also handles tests ? */
1430 /* XXX: overflow test ? */
1431 if (v1
->type
.t
== VT_FLOAT
) {
1433 } else if (v1
->type
.t
== VT_DOUBLE
) {
1441 if (!nocode_wanted
) {
1449 static int pointed_size(CType
*type
)
1452 return type_size(pointed_type(type
), &align
);
1455 static inline int is_null_pointer(SValue
*p
)
1457 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1459 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1460 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1463 static inline int is_integer_btype(int bt
)
1465 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1466 bt
== VT_INT
|| bt
== VT_LLONG
);
1469 /* check types for comparison or substraction of pointers */
1470 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1472 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1475 /* null pointers are accepted for all comparisons as gcc */
1476 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1480 bt1
= type1
->t
& VT_BTYPE
;
1481 bt2
= type2
->t
& VT_BTYPE
;
1482 /* accept comparison between pointer and integer with a warning */
1483 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1484 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1485 warning("comparison between pointer and integer");
1489 /* both must be pointers or implicit function pointers */
1490 if (bt1
== VT_PTR
) {
1491 type1
= pointed_type(type1
);
1492 } else if (bt1
!= VT_FUNC
)
1493 goto invalid_operands
;
1495 if (bt2
== VT_PTR
) {
1496 type2
= pointed_type(type2
);
1497 } else if (bt2
!= VT_FUNC
) {
1499 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1501 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1502 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1506 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1507 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1508 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1509 /* gcc-like error if '-' is used */
1511 goto invalid_operands
;
1513 warning("comparison of distinct pointer types lacks a cast");
1517 /* generic gen_op: handles types problems */
1518 ST_FUNC
void gen_op(int op
)
1520 int u
, t1
, t2
, bt1
, bt2
, t
;
1523 t1
= vtop
[-1].type
.t
;
1524 t2
= vtop
[0].type
.t
;
1525 bt1
= t1
& VT_BTYPE
;
1526 bt2
= t2
& VT_BTYPE
;
1528 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1529 /* at least one operand is a pointer */
1530 /* relationnal op: must be both pointers */
1531 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1532 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1533 /* pointers are handled are unsigned */
1534 #ifdef TCC_TARGET_X86_64
1535 t
= VT_LLONG
| VT_UNSIGNED
;
1537 t
= VT_INT
| VT_UNSIGNED
;
1541 /* if both pointers, then it must be the '-' op */
1542 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1544 error("cannot use pointers here");
1545 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1546 /* XXX: check that types are compatible */
1547 u
= pointed_size(&vtop
[-1].type
);
1549 /* set to integer type */
1550 #ifdef TCC_TARGET_X86_64
1551 vtop
->type
.t
= VT_LLONG
;
1553 vtop
->type
.t
= VT_INT
;
1558 /* exactly one pointer : must be '+' or '-'. */
1559 if (op
!= '-' && op
!= '+')
1560 error("cannot use pointers here");
1561 /* Put pointer as first operand */
1562 if (bt2
== VT_PTR
) {
1566 type1
= vtop
[-1].type
;
1567 type1
.t
&= ~VT_ARRAY
;
1568 u
= pointed_size(&vtop
[-1].type
);
1570 error("unknown array element size");
1571 #ifdef TCC_TARGET_X86_64
1574 /* XXX: cast to int ? (long long case) */
1578 #ifdef CONFIG_TCC_BCHECK
1579 /* if evaluating constant expression, no code should be
1580 generated, so no bound check */
1581 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1582 /* if bounded pointers, we generate a special code to
1589 gen_bounded_ptr_add();
1595 /* put again type if gen_opic() swaped operands */
1598 } else if (is_float(bt1
) || is_float(bt2
)) {
1599 /* compute bigger type and do implicit casts */
1600 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1602 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1607 /* floats can only be used for a few operations */
1608 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1609 (op
< TOK_ULT
|| op
> TOK_GT
))
1610 error("invalid operands for binary operation");
1612 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1613 /* cast to biggest op */
1615 /* convert to unsigned if it does not fit in a long long */
1616 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1617 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1621 /* integer operations */
1623 /* convert to unsigned if it does not fit in an integer */
1624 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1625 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1628 /* XXX: currently, some unsigned operations are explicit, so
1629 we modify them here */
1630 if (t
& VT_UNSIGNED
) {
1637 else if (op
== TOK_LT
)
1639 else if (op
== TOK_GT
)
1641 else if (op
== TOK_LE
)
1643 else if (op
== TOK_GE
)
1650 /* special case for shifts and long long: we keep the shift as
1652 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1659 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1660 /* relationnal op: the result is an int */
1661 vtop
->type
.t
= VT_INT
;
1668 #ifndef TCC_TARGET_ARM
1669 /* generic itof for unsigned long long case */
1670 static void gen_cvt_itof1(int t
)
1672 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1673 (VT_LLONG
| VT_UNSIGNED
)) {
1676 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1677 #if LDOUBLE_SIZE != 8
1678 else if (t
== VT_LDOUBLE
)
1679 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1682 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1686 vtop
->r
= reg_fret(t
);
1693 /* generic ftoi for unsigned long long case */
1694 static void gen_cvt_ftoi1(int t
)
1698 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1699 /* not handled natively */
1700 st
= vtop
->type
.t
& VT_BTYPE
;
1702 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1703 #if LDOUBLE_SIZE != 8
1704 else if (st
== VT_LDOUBLE
)
1705 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1708 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1713 vtop
->r2
= REG_LRET
;
1719 /* force char or short cast */
1720 static void force_charshort_cast(int t
)
1724 /* XXX: add optimization if lvalue : just change type and offset */
1729 if (t
& VT_UNSIGNED
) {
1730 vpushi((1 << bits
) - 1);
1736 /* result must be signed or the SAR is converted to an SHL
1737 This was not the case when "t" was a signed short
1738 and the last value on the stack was an unsigned int */
1739 vtop
->type
.t
&= ~VT_UNSIGNED
;
1745 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1746 static void gen_cast(CType
*type
)
1748 int sbt
, dbt
, sf
, df
, c
, p
;
1750 /* special delayed cast for char/short */
1751 /* XXX: in some cases (multiple cascaded casts), it may still
1753 if (vtop
->r
& VT_MUSTCAST
) {
1754 vtop
->r
&= ~VT_MUSTCAST
;
1755 force_charshort_cast(vtop
->type
.t
);
1758 /* bitfields first get cast to ints */
1759 if (vtop
->type
.t
& VT_BITFIELD
) {
1763 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1764 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1769 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1770 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1772 /* constant case: we can do it now */
1773 /* XXX: in ISOC, cannot do it if error in convert */
1774 if (sbt
== VT_FLOAT
)
1775 vtop
->c
.ld
= vtop
->c
.f
;
1776 else if (sbt
== VT_DOUBLE
)
1777 vtop
->c
.ld
= vtop
->c
.d
;
1780 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1781 if (sbt
& VT_UNSIGNED
)
1782 vtop
->c
.ld
= vtop
->c
.ull
;
1784 vtop
->c
.ld
= vtop
->c
.ll
;
1786 if (sbt
& VT_UNSIGNED
)
1787 vtop
->c
.ld
= vtop
->c
.ui
;
1789 vtop
->c
.ld
= vtop
->c
.i
;
1792 if (dbt
== VT_FLOAT
)
1793 vtop
->c
.f
= (float)vtop
->c
.ld
;
1794 else if (dbt
== VT_DOUBLE
)
1795 vtop
->c
.d
= (double)vtop
->c
.ld
;
1796 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1797 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1798 } else if (sf
&& dbt
== VT_BOOL
) {
1799 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1802 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1803 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1804 vtop
->c
.ll
= vtop
->c
.ull
;
1805 else if (sbt
& VT_UNSIGNED
)
1806 vtop
->c
.ll
= vtop
->c
.ui
;
1807 #ifdef TCC_TARGET_X86_64
1808 else if (sbt
== VT_PTR
)
1811 else if (sbt
!= VT_LLONG
)
1812 vtop
->c
.ll
= vtop
->c
.i
;
1814 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1815 vtop
->c
.ull
= vtop
->c
.ll
;
1816 else if (dbt
== VT_BOOL
)
1817 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1818 else if (dbt
!= VT_LLONG
) {
1820 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1822 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1825 if(dbt
& VT_UNSIGNED
)
1826 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1828 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1831 } else if (p
&& dbt
== VT_BOOL
) {
1834 } else if (!nocode_wanted
) {
1835 /* non constant case: generate code */
1837 /* convert from fp to fp */
1840 /* convert int to fp */
1843 /* convert fp to int */
1844 if (dbt
== VT_BOOL
) {
1848 /* we handle char/short/etc... with generic code */
1849 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1850 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1854 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1855 /* additional cast for char/short... */
1860 #ifndef TCC_TARGET_X86_64
1861 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1862 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1863 /* scalar to long long */
1864 /* machine independent conversion */
1866 /* generate high word */
1867 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1871 if (sbt
== VT_PTR
) {
1872 /* cast from pointer to int before we apply
1873 shift operation, which pointers don't support*/
1874 gen_cast(&int_type
);
1880 /* patch second register */
1881 vtop
[-1].r2
= vtop
->r
;
1885 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1886 (dbt
& VT_BTYPE
) == VT_PTR
||
1887 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1888 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1889 (sbt
& VT_BTYPE
) != VT_PTR
&&
1890 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1891 /* need to convert from 32bit to 64bit */
1893 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1894 /* x86_64 specific: movslq */
1896 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1900 } else if (dbt
== VT_BOOL
) {
1901 /* scalar to bool */
1904 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1905 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1906 if (sbt
== VT_PTR
) {
1907 vtop
->type
.t
= VT_INT
;
1908 warning("nonportable conversion from pointer to char/short");
1910 force_charshort_cast(dbt
);
1911 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1913 if (sbt
== VT_LLONG
) {
1914 /* from long long: just take low order word */
1918 /* if lvalue and single word type, nothing to do because
1919 the lvalue already contains the real type size (see
1920 VT_LVAL_xxx constants) */
1923 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1924 /* if we are casting between pointer types,
1925 we must update the VT_LVAL_xxx size */
1926 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1927 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1932 /* return type size. Put alignment at 'a' */
1933 ST_FUNC
int type_size(CType
*type
, int *a
)
1938 bt
= type
->t
& VT_BTYPE
;
1939 if (bt
== VT_STRUCT
) {
1944 } else if (bt
== VT_PTR
) {
1945 if (type
->t
& VT_ARRAY
) {
1949 ts
= type_size(&s
->type
, a
);
1951 if (ts
< 0 && s
->c
< 0)
1959 } else if (bt
== VT_LDOUBLE
) {
1961 return LDOUBLE_SIZE
;
1962 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1963 #ifdef TCC_TARGET_I386
1964 #ifdef TCC_TARGET_PE
1969 #elif defined(TCC_TARGET_ARM)
1979 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1982 } else if (bt
== VT_SHORT
) {
1986 /* char, void, function, _Bool */
1992 /* return the pointed type of t */
1993 static inline CType
*pointed_type(CType
*type
)
1995 return &type
->ref
->type
;
1998 /* modify type so that its it is a pointer to type. */
1999 ST_FUNC
void mk_pointer(CType
*type
)
2002 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2003 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2007 /* compare function types. OLD functions match any new functions */
2008 static int is_compatible_func(CType
*type1
, CType
*type2
)
2014 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2016 /* check func_call */
2017 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2019 /* XXX: not complete */
2020 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2024 while (s1
!= NULL
) {
2027 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2037 /* return true if type1 and type2 are the same. If unqualified is
2038 true, qualifiers on the types are ignored.
2040 - enums are not checked as gcc __builtin_types_compatible_p ()
2042 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2046 t1
= type1
->t
& VT_TYPE
;
2047 t2
= type2
->t
& VT_TYPE
;
2049 /* strip qualifiers before comparing */
2050 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2051 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2053 /* XXX: bitfields ? */
2056 /* test more complicated cases */
2057 bt1
= t1
& VT_BTYPE
;
2058 if (bt1
== VT_PTR
) {
2059 type1
= pointed_type(type1
);
2060 type2
= pointed_type(type2
);
2061 return is_compatible_types(type1
, type2
);
2062 } else if (bt1
== VT_STRUCT
) {
2063 return (type1
->ref
== type2
->ref
);
2064 } else if (bt1
== VT_FUNC
) {
2065 return is_compatible_func(type1
, type2
);
2071 /* return true if type1 and type2 are exactly the same (including
2074 static int is_compatible_types(CType
*type1
, CType
*type2
)
2076 return compare_types(type1
,type2
,0);
2079 /* return true if type1 and type2 are the same (ignoring qualifiers).
2081 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2083 return compare_types(type1
,type2
,1);
2086 /* print a type. If 'varstr' is not NULL, then the variable is also
2087 printed in the type */
2089 /* XXX: add array and function pointers */
2090 static void type_to_str(char *buf
, int buf_size
,
2091 CType
*type
, const char *varstr
)
2098 t
= type
->t
& VT_TYPE
;
2101 if (t
& VT_CONSTANT
)
2102 pstrcat(buf
, buf_size
, "const ");
2103 if (t
& VT_VOLATILE
)
2104 pstrcat(buf
, buf_size
, "volatile ");
2105 if (t
& VT_UNSIGNED
)
2106 pstrcat(buf
, buf_size
, "unsigned ");
2136 tstr
= "long double";
2138 pstrcat(buf
, buf_size
, tstr
);
2142 if (bt
== VT_STRUCT
)
2146 pstrcat(buf
, buf_size
, tstr
);
2147 v
= type
->ref
->v
& ~SYM_STRUCT
;
2148 if (v
>= SYM_FIRST_ANOM
)
2149 pstrcat(buf
, buf_size
, "<anonymous>");
2151 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2155 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2156 pstrcat(buf
, buf_size
, "(");
2158 while (sa
!= NULL
) {
2159 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2160 pstrcat(buf
, buf_size
, buf1
);
2163 pstrcat(buf
, buf_size
, ", ");
2165 pstrcat(buf
, buf_size
, ")");
2169 pstrcpy(buf1
, sizeof(buf1
), "*");
2171 pstrcat(buf1
, sizeof(buf1
), varstr
);
2172 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2176 pstrcat(buf
, buf_size
, " ");
2177 pstrcat(buf
, buf_size
, varstr
);
2182 /* verify type compatibility to store vtop in 'dt' type, and generate
2184 static void gen_assign_cast(CType
*dt
)
2186 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2187 char buf1
[256], buf2
[256];
2190 st
= &vtop
->type
; /* source type */
2191 dbt
= dt
->t
& VT_BTYPE
;
2192 sbt
= st
->t
& VT_BTYPE
;
2193 if (dt
->t
& VT_CONSTANT
)
2194 warning("assignment of read-only location");
2197 /* special cases for pointers */
2198 /* '0' can also be a pointer */
2199 if (is_null_pointer(vtop
))
2201 /* accept implicit pointer to integer cast with warning */
2202 if (is_integer_btype(sbt
)) {
2203 warning("assignment makes pointer from integer without a cast");
2206 type1
= pointed_type(dt
);
2207 /* a function is implicitely a function pointer */
2208 if (sbt
== VT_FUNC
) {
2209 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2210 !is_compatible_types(pointed_type(dt
), st
))
2211 warning("assignment from incompatible pointer type");
2216 type2
= pointed_type(st
);
2217 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2218 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2219 /* void * can match anything */
2221 /* exact type match, except for unsigned */
2224 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2225 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2226 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2227 warning("assignment from incompatible pointer type");
2229 /* check const and volatile */
2230 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2231 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2232 warning("assignment discards qualifiers from pointer target type");
2238 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2239 warning("assignment makes integer from pointer without a cast");
2241 /* XXX: more tests */
2246 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2247 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2248 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2250 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2251 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2252 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2260 /* store vtop in lvalue pushed on stack */
2261 ST_FUNC
void vstore(void)
2263 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2265 ft
= vtop
[-1].type
.t
;
2266 sbt
= vtop
->type
.t
& VT_BTYPE
;
2267 dbt
= ft
& VT_BTYPE
;
2268 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2269 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2270 /* optimize char/short casts */
2271 delayed_cast
= VT_MUSTCAST
;
2272 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2273 /* XXX: factorize */
2274 if (ft
& VT_CONSTANT
)
2275 warning("assignment of read-only location");
2278 if (!(ft
& VT_BITFIELD
))
2279 gen_assign_cast(&vtop
[-1].type
);
2282 if (sbt
== VT_STRUCT
) {
2283 /* if structure, only generate pointer */
2284 /* structure assignment : generate memcpy */
2285 /* XXX: optimize if small size */
2286 if (!nocode_wanted
) {
2287 size
= type_size(&vtop
->type
, &align
);
2291 vtop
->type
.t
= VT_PTR
;
2294 /* address of memcpy() */
2297 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2298 else if(!(align
& 3))
2299 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2302 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2307 vtop
->type
.t
= VT_PTR
;
2316 /* leave source on stack */
2317 } else if (ft
& VT_BITFIELD
) {
2318 /* bitfield store handling */
2319 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2320 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2321 /* remove bit field info to avoid loops */
2322 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2324 /* duplicate source into other register */
2329 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2330 gen_cast(&vtop
[-1].type
);
2331 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2334 /* duplicate destination */
2336 vtop
[-1] = vtop
[-2];
2338 /* mask and shift source */
2339 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2340 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2341 vpushll((1ULL << bit_size
) - 1ULL);
2343 vpushi((1 << bit_size
) - 1);
2349 /* load destination, mask and or with source */
2351 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2352 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2354 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2361 /* pop off shifted source from "duplicate source..." above */
2365 #ifdef CONFIG_TCC_BCHECK
2366 /* bound check case */
2367 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2373 if (!nocode_wanted
) {
2377 #ifdef TCC_TARGET_X86_64
2378 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2383 r
= gv(rc
); /* generate value */
2384 /* if lvalue was saved on stack, must read it */
2385 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2387 t
= get_reg(RC_INT
);
2388 #ifdef TCC_TARGET_X86_64
2393 sv
.r
= VT_LOCAL
| VT_LVAL
;
2394 sv
.c
.ul
= vtop
[-1].c
.ul
;
2396 vtop
[-1].r
= t
| VT_LVAL
;
2399 #ifndef TCC_TARGET_X86_64
2400 /* two word case handling : store second register at word + 4 */
2401 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2403 /* convert to int to increment easily */
2404 vtop
->type
.t
= VT_INT
;
2410 /* XXX: it works because r2 is spilled last ! */
2411 store(vtop
->r2
, vtop
- 1);
2416 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2417 vtop
->r
|= delayed_cast
;
2421 /* post defines POST/PRE add. c is the token ++ or -- */
2422 ST_FUNC
void inc(int post
, int c
)
2425 vdup(); /* save lvalue */
2427 gv_dup(); /* duplicate value */
2432 vpushi(c
- TOK_MID
);
2434 vstore(); /* store value */
2436 vpop(); /* if post op, return saved value */
2439 /* Parse GNUC __attribute__ extension. Currently, the following
2440 extensions are recognized:
2441 - aligned(n) : set data/function alignment.
2442 - packed : force data alignment to 1
2443 - section(x) : generate data/code in this section.
2444 - unused : currently ignored, but may be used someday.
2445 - regparm(n) : pass function parameters in registers (i386 only)
2447 static void parse_attribute(AttributeDef
*ad
)
2451 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2455 while (tok
!= ')') {
2456 if (tok
< TOK_IDENT
)
2457 expect("attribute name");
2465 expect("section name");
2466 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2475 if (n
<= 0 || (n
& (n
- 1)) != 0)
2476 error("alignment must be a positive power of two");
2493 /* currently, no need to handle it because tcc does not
2494 track unused objects */
2498 /* currently, no need to handle it because tcc does not
2499 track unused objects */
2504 ad
->func_call
= FUNC_CDECL
;
2509 ad
->func_call
= FUNC_STDCALL
;
2511 #ifdef TCC_TARGET_I386
2521 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2527 ad
->func_call
= FUNC_FASTCALLW
;
2534 ad
->mode
= VT_LLONG
+ 1;
2537 ad
->mode
= VT_SHORT
+ 1;
2540 ad
->mode
= VT_INT
+ 1;
2543 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2550 ad
->func_export
= 1;
2553 ad
->func_import
= 1;
2556 if (tcc_state
->warn_unsupported
)
2557 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2558 /* skip parameters */
2560 int parenthesis
= 0;
2564 else if (tok
== ')')
2567 } while (parenthesis
&& tok
!= -1);
2580 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2581 static void struct_decl(CType
*type
, int u
)
2583 int a
, v
, size
, align
, maxalign
, c
, offset
;
2584 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2585 Sym
*s
, *ss
, *ass
, **ps
;
2589 a
= tok
; /* save decl type */
2594 /* struct already defined ? return it */
2596 expect("struct/union/enum name");
2600 error("invalid type");
2607 /* we put an undefined size for struct/union */
2608 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2609 s
->r
= 0; /* default alignment is zero as gcc */
2610 /* put struct/union/enum name in type */
2618 error("struct/union/enum already defined");
2619 /* cannot be empty */
2621 /* non empty enums are not allowed */
2622 if (a
== TOK_ENUM
) {
2626 expect("identifier");
2632 /* enum symbols have static storage */
2633 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2634 ss
->type
.t
|= VT_STATIC
;
2639 /* NOTE: we accept a trailing comma */
2650 while (tok
!= '}') {
2651 parse_btype(&btype
, &ad
);
2657 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2658 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2659 expect("identifier");
2660 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2661 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2662 error("invalid type for '%s'",
2663 get_tok_str(v
, NULL
));
2667 bit_size
= expr_const();
2668 /* XXX: handle v = 0 case for messages */
2670 error("negative width in bit-field '%s'",
2671 get_tok_str(v
, NULL
));
2672 if (v
&& bit_size
== 0)
2673 error("zero width for bit-field '%s'",
2674 get_tok_str(v
, NULL
));
2676 size
= type_size(&type1
, &align
);
2678 if (align
< ad
.aligned
)
2680 } else if (ad
.packed
) {
2682 } else if (*tcc_state
->pack_stack_ptr
) {
2683 if (align
> *tcc_state
->pack_stack_ptr
)
2684 align
= *tcc_state
->pack_stack_ptr
;
2687 if (bit_size
>= 0) {
2688 bt
= type1
.t
& VT_BTYPE
;
2695 error("bitfields must have scalar type");
2697 if (bit_size
> bsize
) {
2698 error("width of '%s' exceeds its type",
2699 get_tok_str(v
, NULL
));
2700 } else if (bit_size
== bsize
) {
2701 /* no need for bit fields */
2703 } else if (bit_size
== 0) {
2704 /* XXX: what to do if only padding in a
2706 /* zero size: means to pad */
2709 /* we do not have enough room ?
2710 did the type change?
2712 if ((bit_pos
+ bit_size
) > bsize
||
2713 bt
!= prevbt
|| a
== TOK_UNION
)
2716 /* XXX: handle LSB first */
2717 type1
.t
|= VT_BITFIELD
|
2718 (bit_pos
<< VT_STRUCT_SHIFT
) |
2719 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2720 bit_pos
+= bit_size
;
2726 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2727 /* add new memory data only if starting
2729 if (lbit_pos
== 0) {
2730 if (a
== TOK_STRUCT
) {
2731 c
= (c
+ align
- 1) & -align
;
2740 if (align
> maxalign
)
2744 printf("add field %s offset=%d",
2745 get_tok_str(v
, NULL
), offset
);
2746 if (type1
.t
& VT_BITFIELD
) {
2747 printf(" pos=%d size=%d",
2748 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2749 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2754 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2756 while ((ass
= ass
->next
) != NULL
) {
2757 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2762 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2766 if (tok
== ';' || tok
== TOK_EOF
)
2773 /* store size and alignment */
2774 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2780 /* return 0 if no type declaration. otherwise, return the basic type
2783 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2785 int t
, u
, type_found
, typespec_found
, typedef_found
;
2789 memset(ad
, 0, sizeof(AttributeDef
));
2797 /* currently, we really ignore extension */
2807 if ((t
& VT_BTYPE
) != 0)
2808 error("too many basic types");
2824 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2825 #ifndef TCC_TARGET_PE
2826 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2828 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2829 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2843 if ((t
& VT_BTYPE
) == VT_LONG
) {
2844 #ifdef TCC_TARGET_PE
2845 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2847 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2855 struct_decl(&type1
, VT_ENUM
);
2858 type
->ref
= type1
.ref
;
2862 struct_decl(&type1
, VT_STRUCT
);
2865 /* type modifiers */
2918 /* GNUC attribute */
2919 case TOK_ATTRIBUTE1
:
2920 case TOK_ATTRIBUTE2
:
2921 parse_attribute(ad
);
2924 t
= (t
& ~VT_BTYPE
) | u
;
2932 parse_expr_type(&type1
);
2935 if (typespec_found
|| typedef_found
)
2938 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2941 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2942 type
->ref
= s
->type
.ref
;
2944 /* get attributes from typedef */
2945 if (0 == ad
->aligned
)
2946 ad
->aligned
= FUNC_ALIGN(s
->r
);
2947 if (0 == ad
->func_call
)
2948 ad
->func_call
= FUNC_CALL(s
->r
);
2949 ad
->packed
|= FUNC_PACKED(s
->r
);
2958 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2959 error("signed and unsigned modifier");
2960 if (tcc_state
->char_is_unsigned
) {
2961 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2966 /* long is never used as type */
2967 if ((t
& VT_BTYPE
) == VT_LONG
)
2968 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2969 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2971 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2977 /* convert a function parameter type (array to pointer and function to
2978 function pointer) */
2979 static inline void convert_parameter_type(CType
*pt
)
2981 /* remove const and volatile qualifiers (XXX: const could be used
2982 to indicate a const function parameter */
2983 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2984 /* array must be transformed to pointer according to ANSI C */
2986 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2991 static void post_type(CType
*type
, AttributeDef
*ad
)
2993 int n
, l
, t1
, arg_size
, align
;
2994 Sym
**plast
, *s
, *first
;
2999 /* function declaration */
3007 /* read param name and compute offset */
3008 if (l
!= FUNC_OLD
) {
3009 if (!parse_btype(&pt
, &ad1
)) {
3011 error("invalid type");
3018 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3020 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3021 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3022 error("parameter declared as void");
3023 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3028 expect("identifier");
3032 convert_parameter_type(&pt
);
3033 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3039 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3046 /* if no parameters, then old type prototype */
3050 t1
= type
->t
& VT_STORAGE
;
3051 /* NOTE: const is ignored in returned type as it has a special
3052 meaning in gcc / C++ */
3053 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3054 post_type(type
, ad
);
3055 /* we push a anonymous symbol which will contain the function prototype */
3056 ad
->func_args
= arg_size
;
3057 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3059 type
->t
= t1
| VT_FUNC
;
3061 } else if (tok
== '[') {
3062 /* array definition */
3064 if (tok
== TOK_RESTRICT1
)
3070 error("invalid array size");
3073 /* parse next post type */
3074 t1
= type
->t
& VT_STORAGE
;
3075 type
->t
&= ~VT_STORAGE
;
3076 post_type(type
, ad
);
3078 /* we push a anonymous symbol which will contain the array
3080 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3081 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3086 /* Parse a type declaration (except basic type), and return the type
3087 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3088 expected. 'type' should contain the basic type. 'ad' is the
3089 attribute definition of the basic type. It can be modified by
3092 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3095 CType type1
, *type2
;
3098 while (tok
== '*') {
3106 qualifiers
|= VT_CONSTANT
;
3111 qualifiers
|= VT_VOLATILE
;
3119 type
->t
|= qualifiers
;
3122 /* XXX: clarify attribute handling */
3123 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3124 parse_attribute(ad
);
3126 /* recursive type */
3127 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3128 type1
.t
= 0; /* XXX: same as int */
3131 /* XXX: this is not correct to modify 'ad' at this point, but
3132 the syntax is not clear */
3133 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3134 parse_attribute(ad
);
3135 type_decl(&type1
, ad
, v
, td
);
3138 /* type identifier */
3139 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3143 if (!(td
& TYPE_ABSTRACT
))
3144 expect("identifier");
3148 post_type(type
, ad
);
3149 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3150 parse_attribute(ad
);
3153 /* append type at the end of type1 */
3166 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3167 ST_FUNC
int lvalue_type(int t
)
3172 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3174 else if (bt
== VT_SHORT
)
3178 if (t
& VT_UNSIGNED
)
3179 r
|= VT_LVAL_UNSIGNED
;
3183 /* indirection with full error checking and bound check */
3184 ST_FUNC
void indir(void)
3186 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3187 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3191 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3193 vtop
->type
= *pointed_type(&vtop
->type
);
3194 /* Arrays and functions are never lvalues */
3195 if (!(vtop
->type
.t
& VT_ARRAY
)
3196 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3197 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3198 /* if bound checking, the referenced pointer must be checked */
3199 #ifdef CONFIG_TCC_BCHECK
3200 if (tcc_state
->do_bounds_check
)
3201 vtop
->r
|= VT_MUSTBOUND
;
3206 /* pass a parameter to a function and do type checking and casting */
3207 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3212 func_type
= func
->c
;
3213 if (func_type
== FUNC_OLD
||
3214 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3215 /* default casting : only need to convert float to double */
3216 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3220 } else if (arg
== NULL
) {
3221 error("too many arguments to function");
3224 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3225 gen_assign_cast(&type
);
3229 /* parse an expression of the form '(type)' or '(expr)' and return its
3231 static void parse_expr_type(CType
*type
)
3237 if (parse_btype(type
, &ad
)) {
3238 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3245 static void parse_type(CType
*type
)
3250 if (!parse_btype(type
, &ad
)) {
3253 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3256 static void vpush_tokc(int t
)
3261 vsetc(&type
, VT_CONST
, &tokc
);
3264 ST_FUNC
void unary(void)
3266 int n
, t
, align
, size
, r
, sizeof_caller
;
3270 static int in_sizeof
= 0;
3272 sizeof_caller
= in_sizeof
;
3274 /* XXX: GCC 2.95.3 does not generate a table although it should be
3288 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3292 vpush_tokc(VT_LLONG
);
3296 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3300 vpush_tokc(VT_FLOAT
);
3304 vpush_tokc(VT_DOUBLE
);
3308 vpush_tokc(VT_LDOUBLE
);
3311 case TOK___FUNCTION__
:
3313 goto tok_identifier
;
3319 /* special function name identifier */
3320 len
= strlen(funcname
) + 1;
3321 /* generate char[len] type */
3326 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3327 ptr
= section_ptr_add(data_section
, len
);
3328 memcpy(ptr
, funcname
, len
);
3333 #ifdef TCC_TARGET_PE
3334 t
= VT_SHORT
| VT_UNSIGNED
;
3340 /* string parsing */
3343 if (tcc_state
->warn_write_strings
)
3348 memset(&ad
, 0, sizeof(AttributeDef
));
3349 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3354 if (parse_btype(&type
, &ad
)) {
3355 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3357 /* check ISOC99 compound literal */
3359 /* data is allocated locally by default */
3364 /* all except arrays are lvalues */
3365 if (!(type
.t
& VT_ARRAY
))
3366 r
|= lvalue_type(type
.t
);
3367 memset(&ad
, 0, sizeof(AttributeDef
));
3368 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3370 if (sizeof_caller
) {
3377 } else if (tok
== '{') {
3378 /* save all registers */
3380 /* statement expression : we do not accept break/continue
3381 inside as GCC does */
3382 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3397 /* functions names must be treated as function pointers,
3398 except for unary '&' and sizeof. Since we consider that
3399 functions are not lvalues, we only have to handle it
3400 there and in function calls. */
3401 /* arrays can also be used although they are not lvalues */
3402 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3403 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3405 mk_pointer(&vtop
->type
);
3411 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3413 boolean
.t
= VT_BOOL
;
3415 vtop
->c
.i
= !vtop
->c
.i
;
3416 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3417 vtop
->c
.i
= vtop
->c
.i
^ 1;
3420 vseti(VT_JMP
, gtst(1, 0));
3431 /* in order to force cast, we add zero */
3433 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3434 error("pointer not accepted for unary plus");
3444 unary_type(&type
); // Perform a in_sizeof = 0;
3445 size
= type_size(&type
, &align
);
3446 if (t
== TOK_SIZEOF
) {
3448 error("sizeof applied to an incomplete type");
3453 vtop
->type
.t
|= VT_UNSIGNED
;
3456 case TOK_builtin_types_compatible_p
:
3465 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3466 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3467 vpushi(is_compatible_types(&type1
, &type2
));
3470 case TOK_builtin_constant_p
:
3472 int saved_nocode_wanted
, res
;
3475 saved_nocode_wanted
= nocode_wanted
;
3478 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3480 nocode_wanted
= saved_nocode_wanted
;
3485 case TOK_builtin_frame_address
:
3490 if (tok
!= TOK_CINT
) {
3491 error("__builtin_frame_address only takes integers");
3494 error("TCC only supports __builtin_frame_address(0)");
3500 vset(&type
, VT_LOCAL
, 0);
3503 #ifdef TCC_TARGET_X86_64
3504 case TOK_builtin_malloc
:
3506 goto tok_identifier
;
3507 case TOK_builtin_free
:
3509 goto tok_identifier
;
3526 goto tok_identifier
;
3528 /* allow to take the address of a label */
3529 if (tok
< TOK_UIDENT
)
3530 expect("label identifier");
3531 s
= label_find(tok
);
3533 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3535 if (s
->r
== LABEL_DECLARED
)
3536 s
->r
= LABEL_FORWARD
;
3539 s
->type
.t
= VT_VOID
;
3540 mk_pointer(&s
->type
);
3541 s
->type
.t
|= VT_STATIC
;
3543 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3548 // special qnan , snan and infinity values
3550 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3554 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3558 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3567 expect("identifier");
3571 error("'%s' undeclared", get_tok_str(t
, NULL
));
3572 /* for simple function calls, we tolerate undeclared
3573 external reference to int() function */
3574 if (tcc_state
->warn_implicit_function_declaration
)
3575 warning("implicit declaration of function '%s'",
3576 get_tok_str(t
, NULL
));
3577 s
= external_global_sym(t
, &func_old_type
, 0);
3579 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3580 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3581 /* if referencing an inline function, then we generate a
3582 symbol to it if not already done. It will have the
3583 effect to generate code for it at the end of the
3584 compilation unit. Inline function as always
3585 generated in the text section. */
3587 put_extern_sym(s
, text_section
, 0, 0);
3588 r
= VT_SYM
| VT_CONST
;
3592 vset(&s
->type
, r
, s
->c
);
3593 /* if forward reference, we must point to s */
3594 if (vtop
->r
& VT_SYM
) {
3601 /* post operations */
3603 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3606 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3609 if (tok
== TOK_ARROW
)
3611 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3615 /* expect pointer on structure */
3616 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3617 expect("struct or union");
3621 while ((s
= s
->next
) != NULL
) {
3626 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3627 /* add field offset to pointer */
3628 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3631 /* change type to field type, and set to lvalue */
3632 vtop
->type
= s
->type
;
3633 vtop
->type
.t
|= qualifiers
;
3634 /* an array is never an lvalue */
3635 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3636 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3637 #ifdef CONFIG_TCC_BCHECK
3638 /* if bound checking, the referenced pointer must be checked */
3639 if (tcc_state
->do_bounds_check
)
3640 vtop
->r
|= VT_MUSTBOUND
;
3644 } else if (tok
== '[') {
3650 } else if (tok
== '(') {
3656 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3657 /* pointer test (no array accepted) */
3658 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3659 vtop
->type
= *pointed_type(&vtop
->type
);
3660 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3664 expect("function pointer");
3667 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3669 /* get return type */
3672 sa
= s
->next
; /* first parameter */
3675 /* compute first implicit argument if a structure is returned */
3676 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3677 /* get some space for the returned structure */
3678 size
= type_size(&s
->type
, &align
);
3679 loc
= (loc
- size
) & -align
;
3681 ret
.r
= VT_LOCAL
| VT_LVAL
;
3682 /* pass it as 'int' to avoid structure arg passing
3684 vseti(VT_LOCAL
, loc
);
3689 /* return in register */
3690 if (is_float(ret
.type
.t
)) {
3691 ret
.r
= reg_fret(ret
.type
.t
);
3693 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3702 gfunc_param_typed(s
, sa
);
3712 error("too few arguments to function");
3714 if (!nocode_wanted
) {
3715 gfunc_call(nb_args
);
3717 vtop
-= (nb_args
+ 1);
3720 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3728 static void uneq(void)
3734 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3735 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3736 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3751 ST_FUNC
void expr_prod(void)
3756 while (tok
== '*' || tok
== '/' || tok
== '%') {
3764 ST_FUNC
void expr_sum(void)
3769 while (tok
== '+' || tok
== '-') {
3777 static void expr_shift(void)
3782 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3790 static void expr_cmp(void)
3795 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3796 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3804 static void expr_cmpeq(void)
3809 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3817 static void expr_and(void)
3820 while (tok
== '&') {
3827 static void expr_xor(void)
3830 while (tok
== '^') {
3837 static void expr_or(void)
3840 while (tok
== '|') {
3847 /* XXX: fix this mess */
3848 static void expr_land_const(void)
3851 while (tok
== TOK_LAND
) {
3858 /* XXX: fix this mess */
3859 static void expr_lor_const(void)
3862 while (tok
== TOK_LOR
) {
3869 /* only used if non constant */
3870 static void expr_land(void)
3875 if (tok
== TOK_LAND
) {
3880 if (tok
!= TOK_LAND
) {
3890 static void expr_lor(void)
3895 if (tok
== TOK_LOR
) {
3900 if (tok
!= TOK_LOR
) {
3910 /* XXX: better constant handling */
3911 static void expr_eq(void)
3913 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3915 CType type
, type1
, type2
;
3922 boolean
.t
= VT_BOOL
;
3928 if (tok
!= ':' || !gnu_ext
) {
3943 if (vtop
!= vstack
) {
3944 /* needed to avoid having different registers saved in
3946 if (is_float(vtop
->type
.t
)) {
3948 #ifdef TCC_TARGET_X86_64
3949 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3959 if (tok
== ':' && gnu_ext
) {
3967 sv
= *vtop
; /* save value to handle it later */
3968 vtop
--; /* no vpop so that FP stack is not flushed */
3976 bt1
= t1
& VT_BTYPE
;
3978 bt2
= t2
& VT_BTYPE
;
3979 /* cast operands to correct type according to ISOC rules */
3980 if (is_float(bt1
) || is_float(bt2
)) {
3981 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3982 type
.t
= VT_LDOUBLE
;
3983 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3988 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3989 /* cast to biggest op */
3991 /* convert to unsigned if it does not fit in a long long */
3992 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3993 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3994 type
.t
|= VT_UNSIGNED
;
3995 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3996 /* XXX: test pointer compatibility */
3998 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3999 /* XXX: test function pointer compatibility */
4001 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4002 /* XXX: test structure compatibility */
4004 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4005 /* NOTE: as an extension, we accept void on only one side */
4008 /* integer operations */
4010 /* convert to unsigned if it does not fit in an integer */
4011 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4012 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4013 type
.t
|= VT_UNSIGNED
;
4016 /* now we convert second operand */
4018 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4021 if (is_float(type
.t
)) {
4023 #ifdef TCC_TARGET_X86_64
4024 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4028 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4029 /* for long longs, we use fixed registers to avoid having
4030 to handle a complicated move */
4035 /* this is horrible, but we must also convert first
4039 /* put again first value and cast it */
4042 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4052 ST_FUNC
void gexpr(void)
4063 /* parse an expression and return its type without any side effect. */
4064 static void expr_type(CType
*type
)
4066 int saved_nocode_wanted
;
4068 saved_nocode_wanted
= nocode_wanted
;
4073 nocode_wanted
= saved_nocode_wanted
;
4076 /* parse a unary expression and return its type without any side
4078 static void unary_type(CType
*type
)
4090 /* parse a constant expression and return value in vtop. */
4091 static void expr_const1(void)
4100 /* parse an integer constant and return its value. */
4101 ST_FUNC
int expr_const(void)
4105 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4106 expect("constant expression");
4112 /* return the label token if current token is a label, otherwise
4114 static int is_label(void)
4118 /* fast test first */
4119 if (tok
< TOK_UIDENT
)
4121 /* no need to save tokc because tok is an identifier */
4128 unget_tok(last_tok
);
4133 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4134 int case_reg
, int is_expr
)
4139 /* generate line number info */
4140 if (tcc_state
->do_debug
&&
4141 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4142 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4144 last_line_num
= file
->line_num
;
4148 /* default return value is (void) */
4150 vtop
->type
.t
= VT_VOID
;
4153 if (tok
== TOK_IF
) {
4160 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4162 if (c
== TOK_ELSE
) {
4166 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4167 gsym(d
); /* patch else jmp */
4170 } else if (tok
== TOK_WHILE
) {
4178 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4182 } else if (tok
== '{') {
4186 /* record local declaration stack position */
4188 llabel
= local_label_stack
;
4189 /* handle local labels declarations */
4190 if (tok
== TOK_LABEL
) {
4193 if (tok
< TOK_UIDENT
)
4194 expect("label identifier");
4195 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4205 while (tok
!= '}') {
4210 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4213 /* pop locally defined labels */
4214 label_pop(&local_label_stack
, llabel
);
4215 /* pop locally defined symbols */
4217 /* XXX: this solution makes only valgrind happy...
4218 triggered by gcc.c-torture/execute/20000917-1.c */
4220 switch(vtop
->type
.t
& VT_BTYPE
) {
4225 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4227 error("unsupported expression type");
4230 sym_pop(&local_stack
, s
);
4232 } else if (tok
== TOK_RETURN
) {
4236 gen_assign_cast(&func_vt
);
4237 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4239 /* if returning structure, must copy it to implicit
4240 first pointer arg location */
4243 size
= type_size(&func_vt
,&align
);
4246 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4250 loc
= (loc
- size
) & -4;
4253 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4256 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4258 vtop
->type
= int_type
;
4264 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4267 /* copy structure value to pointer */
4272 } else if (is_float(func_vt
.t
)) {
4273 gv(rc_fret(func_vt
.t
));
4277 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4280 rsym
= gjmp(rsym
); /* jmp */
4281 } else if (tok
== TOK_BREAK
) {
4284 error("cannot break");
4285 *bsym
= gjmp(*bsym
);
4288 } else if (tok
== TOK_CONTINUE
) {
4291 error("cannot continue");
4292 *csym
= gjmp(*csym
);
4295 } else if (tok
== TOK_FOR
) {
4322 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4327 if (tok
== TOK_DO
) {
4332 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4343 if (tok
== TOK_SWITCH
) {
4347 /* XXX: other types than integer */
4348 case_reg
= gv(RC_INT
);
4352 b
= gjmp(0); /* jump to first case */
4354 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4355 /* if no default, jmp after switch */
4363 if (tok
== TOK_CASE
) {
4370 if (gnu_ext
&& tok
== TOK_DOTS
) {
4374 warning("empty case range");
4376 /* since a case is like a label, we must skip it with a jmp */
4383 *case_sym
= gtst(1, 0);
4386 *case_sym
= gtst(1, 0);
4390 *case_sym
= gtst(1, *case_sym
);
4395 goto block_after_label
;
4397 if (tok
== TOK_DEFAULT
) {
4403 error("too many 'default'");
4406 goto block_after_label
;
4408 if (tok
== TOK_GOTO
) {
4410 if (tok
== '*' && gnu_ext
) {
4414 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4417 } else if (tok
>= TOK_UIDENT
) {
4418 s
= label_find(tok
);
4419 /* put forward definition if needed */
4421 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4423 if (s
->r
== LABEL_DECLARED
)
4424 s
->r
= LABEL_FORWARD
;
4426 /* label already defined */
4427 if (s
->r
& LABEL_FORWARD
)
4428 s
->jnext
= gjmp(s
->jnext
);
4430 gjmp_addr(s
->jnext
);
4433 expect("label identifier");
4436 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4444 if (s
->r
== LABEL_DEFINED
)
4445 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4447 s
->r
= LABEL_DEFINED
;
4449 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4452 /* we accept this, but it is a mistake */
4455 warning("deprecated use of label at end of compound statement");
4459 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4462 /* expression case */
4477 /* t is the array or struct type. c is the array or struct
4478 address. cur_index/cur_field is the pointer to the current
4479 value. 'size_only' is true if only size info is needed (only used
4481 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4482 int *cur_index
, Sym
**cur_field
,
4486 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4492 if (gnu_ext
&& (l
= is_label()) != 0)
4494 while (tok
== '[' || tok
== '.') {
4496 if (!(type
->t
& VT_ARRAY
))
4497 expect("array type");
4500 index
= expr_const();
4501 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4502 expect("invalid index");
4503 if (tok
== TOK_DOTS
&& gnu_ext
) {
4505 index_last
= expr_const();
4506 if (index_last
< 0 ||
4507 (s
->c
>= 0 && index_last
>= s
->c
) ||
4509 expect("invalid index");
4515 *cur_index
= index_last
;
4516 type
= pointed_type(type
);
4517 elem_size
= type_size(type
, &align
);
4518 c
+= index
* elem_size
;
4519 /* NOTE: we only support ranges for last designator */
4520 nb_elems
= index_last
- index
+ 1;
4521 if (nb_elems
!= 1) {
4530 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4531 expect("struct/union type");
4544 /* XXX: fix this mess by using explicit storage field */
4546 type1
.t
|= (type
->t
& ~VT_TYPE
);
4560 if (type
->t
& VT_ARRAY
) {
4562 type
= pointed_type(type
);
4563 c
+= index
* type_size(type
, &align
);
4567 error("too many field init");
4568 /* XXX: fix this mess by using explicit storage field */
4570 type1
.t
|= (type
->t
& ~VT_TYPE
);
4575 decl_initializer(type
, sec
, c
, 0, size_only
);
4577 /* XXX: make it more general */
4578 if (!size_only
&& nb_elems
> 1) {
4579 unsigned long c_end
;
4584 error("range init not supported yet for dynamic storage");
4585 c_end
= c
+ nb_elems
* elem_size
;
4586 if (c_end
> sec
->data_allocated
)
4587 section_realloc(sec
, c_end
);
4588 src
= sec
->data
+ c
;
4590 for(i
= 1; i
< nb_elems
; i
++) {
4592 memcpy(dst
, src
, elem_size
);
4598 #define EXPR_CONST 1
4601 /* store a value or an expression directly in global data or in local array */
4602 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4603 int v
, int expr_type
)
4605 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4607 unsigned long long bit_mask
;
4615 /* compound literals must be allocated globally in this case */
4616 saved_global_expr
= global_expr
;
4619 global_expr
= saved_global_expr
;
4620 /* NOTE: symbols are accepted */
4621 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4622 error("initializer element is not constant");
4630 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4633 /* XXX: not portable */
4634 /* XXX: generate error if incorrect relocation */
4635 gen_assign_cast(&dtype
);
4636 bt
= type
->t
& VT_BTYPE
;
4637 /* we'll write at most 12 bytes */
4638 if (c
+ 12 > sec
->data_allocated
) {
4639 section_realloc(sec
, c
+ 12);
4641 ptr
= sec
->data
+ c
;
4642 /* XXX: make code faster ? */
4643 if (!(type
->t
& VT_BITFIELD
)) {
4648 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4649 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4650 bit_mask
= (1LL << bit_size
) - 1;
4652 if ((vtop
->r
& VT_SYM
) &&
4658 (bt
== VT_INT
&& bit_size
!= 32)))
4659 error("initializer element is not computable at load time");
4662 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4664 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4667 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4670 *(double *)ptr
= vtop
->c
.d
;
4673 *(long double *)ptr
= vtop
->c
.ld
;
4676 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4679 if (vtop
->r
& VT_SYM
) {
4680 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4682 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4687 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4694 /* put zeros for variable based init */
4695 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4698 /* nothing to do because globals are already set to zero */
4700 vpush_global_sym(&func_old_type
, TOK_memset
);
4708 /* 't' contains the type and storage info. 'c' is the offset of the
4709 object in section 'sec'. If 'sec' is NULL, it means stack based
4710 allocation. 'first' is true if array '{' must be read (multi
4711 dimension implicit array init handling). 'size_only' is true if
4712 size only evaluation is wanted (only for arrays). */
4713 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4714 int first
, int size_only
)
4716 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4717 int size1
, align1
, expr_type
;
4721 if (type
->t
& VT_ARRAY
) {
4725 t1
= pointed_type(type
);
4726 size1
= type_size(t1
, &align1
);
4729 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4732 error("character array initializer must be a literal,"
4733 " optionally enclosed in braces");
4738 /* only parse strings here if correct type (otherwise: handle
4739 them as ((w)char *) expressions */
4740 if ((tok
== TOK_LSTR
&&
4741 #ifdef TCC_TARGET_PE
4742 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4744 (t1
->t
& VT_BTYPE
) == VT_INT
4746 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4747 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4752 /* compute maximum number of chars wanted */
4754 cstr_len
= cstr
->size
;
4756 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4759 if (n
>= 0 && nb
> (n
- array_length
))
4760 nb
= n
- array_length
;
4763 warning("initializer-string for array is too long");
4764 /* in order to go faster for common case (char
4765 string in global variable, we handle it
4767 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4768 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4772 ch
= ((unsigned char *)cstr
->data
)[i
];
4774 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4775 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4783 /* only add trailing zero if enough storage (no
4784 warning in this case since it is standard) */
4785 if (n
< 0 || array_length
< n
) {
4787 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4793 while (tok
!= '}') {
4794 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4795 if (n
>= 0 && index
>= n
)
4796 error("index too large");
4797 /* must put zero in holes (note that doing it that way
4798 ensures that it even works with designators) */
4799 if (!size_only
&& array_length
< index
) {
4800 init_putz(t1
, sec
, c
+ array_length
* size1
,
4801 (index
- array_length
) * size1
);
4804 if (index
> array_length
)
4805 array_length
= index
;
4806 /* special test for multi dimensional arrays (may not
4807 be strictly correct if designators are used at the
4809 if (index
>= n
&& no_oblock
)
4818 /* put zeros at the end */
4819 if (!size_only
&& n
>= 0 && array_length
< n
) {
4820 init_putz(t1
, sec
, c
+ array_length
* size1
,
4821 (n
- array_length
) * size1
);
4823 /* patch type size if needed */
4825 s
->c
= array_length
;
4826 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4827 (sec
|| !first
|| tok
== '{')) {
4830 /* NOTE: the previous test is a specific case for automatic
4831 struct/union init */
4832 /* XXX: union needs only one init */
4834 /* XXX: this test is incorrect for local initializers
4835 beginning with ( without {. It would be much more difficult
4836 to do it correctly (ideally, the expression parser should
4837 be used in all cases) */
4843 while (tok
== '(') {
4847 if (!parse_btype(&type1
, &ad1
))
4849 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4851 if (!is_assignable_types(type
, &type1
))
4852 error("invalid type for cast");
4857 if (first
|| tok
== '{') {
4866 while (tok
!= '}') {
4867 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4869 if (!size_only
&& array_length
< index
) {
4870 init_putz(type
, sec
, c
+ array_length
,
4871 index
- array_length
);
4873 index
= index
+ type_size(&f
->type
, &align1
);
4874 if (index
> array_length
)
4875 array_length
= index
;
4877 /* gr: skip fields from same union - ugly. */
4879 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4880 /* test for same offset */
4881 if (f
->next
->c
!= f
->c
)
4883 /* if yes, test for bitfield shift */
4884 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4885 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4886 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4887 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4888 if (bit_pos_1
!= bit_pos_2
)
4895 if (no_oblock
&& f
== NULL
)
4901 /* put zeros at the end */
4902 if (!size_only
&& array_length
< n
) {
4903 init_putz(type
, sec
, c
+ array_length
,
4912 } else if (tok
== '{') {
4914 decl_initializer(type
, sec
, c
, first
, size_only
);
4916 } else if (size_only
) {
4917 /* just skip expression */
4919 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4923 else if (tok
== ')')
4928 /* currently, we always use constant expression for globals
4929 (may change for scripting case) */
4930 expr_type
= EXPR_CONST
;
4932 expr_type
= EXPR_ANY
;
4933 init_putv(type
, sec
, c
, 0, expr_type
);
4937 /* parse an initializer for type 't' if 'has_init' is non zero, and
4938 allocate space in local or global data space ('r' is either
4939 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4940 variable 'v' of scope 'scope' is declared before initializers are
4941 parsed. If 'v' is zero, then a reference to the new object is put
4942 in the value stack. If 'has_init' is 2, a special parsing is done
4943 to handle string constants. */
4944 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4945 int has_init
, int v
, int scope
)
4947 int size
, align
, addr
, data_offset
;
4949 ParseState saved_parse_state
= {0};
4950 TokenString init_str
;
4953 size
= type_size(type
, &align
);
4954 /* If unknown size, we must evaluate it before
4955 evaluating initializers because
4956 initializers can generate global data too
4957 (e.g. string pointers or ISOC99 compound
4958 literals). It also simplifies local
4959 initializers handling */
4960 tok_str_new(&init_str
);
4963 error("unknown type size");
4964 /* get all init string */
4965 if (has_init
== 2) {
4966 /* only get strings */
4967 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4968 tok_str_add_tok(&init_str
);
4973 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4975 error("unexpected end of file in initializer");
4976 tok_str_add_tok(&init_str
);
4979 else if (tok
== '}') {
4989 tok_str_add(&init_str
, -1);
4990 tok_str_add(&init_str
, 0);
4993 save_parse_state(&saved_parse_state
);
4995 macro_ptr
= init_str
.str
;
4997 decl_initializer(type
, NULL
, 0, 1, 1);
4998 /* prepare second initializer parsing */
4999 macro_ptr
= init_str
.str
;
5002 /* if still unknown size, error */
5003 size
= type_size(type
, &align
);
5005 error("unknown type size");
5007 /* take into account specified alignment if bigger */
5009 if (ad
->aligned
> align
)
5010 align
= ad
->aligned
;
5011 } else if (ad
->packed
) {
5014 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5016 #ifdef CONFIG_TCC_BCHECK
5017 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
5020 loc
= (loc
- size
) & -align
;
5022 #ifdef CONFIG_TCC_BCHECK
5023 /* handles bounds */
5024 /* XXX: currently, since we do only one pass, we cannot track
5025 '&' operators, so we add only arrays */
5026 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5027 unsigned long *bounds_ptr
;
5028 /* add padding between regions */
5030 /* then add local bound info */
5031 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5032 bounds_ptr
[0] = addr
;
5033 bounds_ptr
[1] = size
;
5037 /* local variable */
5038 sym_push(v
, type
, r
, addr
);
5040 /* push local reference */
5041 vset(type
, r
, addr
);
5047 if (v
&& scope
== VT_CONST
) {
5048 /* see if the symbol was already defined */
5051 if (!is_compatible_types(&sym
->type
, type
))
5052 error("incompatible types for redefinition of '%s'",
5053 get_tok_str(v
, NULL
));
5054 if (sym
->type
.t
& VT_EXTERN
) {
5055 /* if the variable is extern, it was not allocated */
5056 sym
->type
.t
&= ~VT_EXTERN
;
5057 /* set array size if it was ommited in extern
5059 if ((sym
->type
.t
& VT_ARRAY
) &&
5060 sym
->type
.ref
->c
< 0 &&
5062 sym
->type
.ref
->c
= type
->ref
->c
;
5064 /* we accept several definitions of the same
5065 global variable. this is tricky, because we
5066 must play with the SHN_COMMON type of the symbol */
5067 /* XXX: should check if the variable was already
5068 initialized. It is incorrect to initialized it
5070 /* no init data, we won't add more to the symbol */
5077 /* allocate symbol in corresponding section */
5082 else if (tcc_state
->nocommon
)
5086 data_offset
= sec
->data_offset
;
5087 data_offset
= (data_offset
+ align
- 1) & -align
;
5089 /* very important to increment global pointer at this time
5090 because initializers themselves can create new initializers */
5091 data_offset
+= size
;
5092 #ifdef CONFIG_TCC_BCHECK
5093 /* add padding if bound check */
5094 if (tcc_state
->do_bounds_check
)
5097 sec
->data_offset
= data_offset
;
5098 /* allocate section space to put the data */
5099 if (sec
->sh_type
!= SHT_NOBITS
&&
5100 data_offset
> sec
->data_allocated
)
5101 section_realloc(sec
, data_offset
);
5102 /* align section if needed */
5103 if (align
> sec
->sh_addralign
)
5104 sec
->sh_addralign
= align
;
5106 addr
= 0; /* avoid warning */
5110 if (scope
!= VT_CONST
|| !sym
) {
5111 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5113 /* update symbol definition */
5115 put_extern_sym(sym
, sec
, addr
, size
);
5118 /* put a common area */
5119 put_extern_sym(sym
, NULL
, align
, size
);
5120 /* XXX: find a nicer way */
5121 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5122 esym
->st_shndx
= SHN_COMMON
;
5127 /* push global reference */
5128 sym
= get_sym_ref(type
, sec
, addr
, size
);
5130 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5133 #ifdef CONFIG_TCC_BCHECK
5134 /* handles bounds now because the symbol must be defined
5135 before for the relocation */
5136 if (tcc_state
->do_bounds_check
) {
5137 unsigned long *bounds_ptr
;
5139 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5140 /* then add global bound info */
5141 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5142 bounds_ptr
[0] = 0; /* relocated */
5143 bounds_ptr
[1] = size
;
5148 decl_initializer(type
, sec
, addr
, 1, 0);
5149 /* restore parse state if needed */
5151 tok_str_free(init_str
.str
);
5152 restore_parse_state(&saved_parse_state
);
5158 static void put_func_debug(Sym
*sym
)
5163 /* XXX: we put here a dummy type */
5164 snprintf(buf
, sizeof(buf
), "%s:%c1",
5165 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5166 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5167 cur_text_section
, sym
->c
);
5168 /* //gr gdb wants a line at the function */
5169 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5174 /* parse an old style function declaration list */
5175 /* XXX: check multiple parameter */
5176 static void func_decl_list(Sym
*func_sym
)
5183 /* parse each declaration */
5184 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
5185 if (!parse_btype(&btype
, &ad
))
5186 expect("declaration list");
5187 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5188 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5190 /* we accept no variable after */
5194 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5195 /* find parameter in function parameter list */
5198 if ((s
->v
& ~SYM_FIELD
) == v
)
5202 error("declaration for parameter '%s' but no such parameter",
5203 get_tok_str(v
, NULL
));
5205 /* check that no storage specifier except 'register' was given */
5206 if (type
.t
& VT_STORAGE
)
5207 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5208 convert_parameter_type(&type
);
5209 /* we can add the type (NOTE: it could be local to the function) */
5211 /* accept other parameters */
5222 /* parse a function defined by symbol 'sym' and generate its code in
5223 'cur_text_section' */
5224 static void gen_function(Sym
*sym
)
5226 int saved_nocode_wanted
= nocode_wanted
;
5228 ind
= cur_text_section
->data_offset
;
5229 /* NOTE: we patch the symbol size later */
5230 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5231 funcname
= get_tok_str(sym
->v
, NULL
);
5233 /* put debug symbol */
5234 if (tcc_state
->do_debug
)
5235 put_func_debug(sym
);
5236 /* push a dummy symbol to enable local sym storage */
5237 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5238 gfunc_prolog(&sym
->type
);
5240 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5243 cur_text_section
->data_offset
= ind
;
5244 label_pop(&global_label_stack
, NULL
);
5245 sym_pop(&local_stack
, NULL
); /* reset local stack */
5246 /* end of function */
5247 /* patch symbol size */
5248 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5250 if (tcc_state
->do_debug
) {
5251 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5253 /* It's better to crash than to generate wrong code */
5254 cur_text_section
= NULL
;
5255 funcname
= ""; /* for safety */
5256 func_vt
.t
= VT_VOID
; /* for safety */
5257 ind
= 0; /* for safety */
5258 nocode_wanted
= saved_nocode_wanted
;
5261 ST_FUNC
void gen_inline_functions(void)
5264 int *str
, inline_generated
, i
;
5265 struct InlineFunc
*fn
;
5267 /* iterate while inline function are referenced */
5269 inline_generated
= 0;
5270 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5271 fn
= tcc_state
->inline_fns
[i
];
5273 if (sym
&& sym
->c
) {
5274 /* the function was used: generate its code and
5275 convert it to a normal function */
5276 str
= fn
->token_str
;
5279 strcpy(file
->filename
, fn
->filename
);
5280 sym
->r
= VT_SYM
| VT_CONST
;
5281 sym
->type
.t
&= ~VT_INLINE
;
5285 cur_text_section
= text_section
;
5287 macro_ptr
= NULL
; /* fail safe */
5289 inline_generated
= 1;
5292 if (!inline_generated
)
5295 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5296 fn
= tcc_state
->inline_fns
[i
];
5297 str
= fn
->token_str
;
5300 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5303 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5304 ST_FUNC
void decl(int l
)
5312 if (!parse_btype(&btype
, &ad
)) {
5313 /* skip redundant ';' */
5314 /* XXX: find more elegant solution */
5319 if (l
== VT_CONST
&&
5320 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5321 /* global asm block */
5325 /* special test for old K&R protos without explicit int
5326 type. Only accepted when defining global data */
5327 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5331 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5332 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5334 /* we accept no variable after */
5338 while (1) { /* iterate thru each declaration */
5340 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5344 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5345 printf("type = '%s'\n", buf
);
5348 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5349 /* if old style function prototype, we accept a
5352 if (sym
->c
== FUNC_OLD
)
5353 func_decl_list(sym
);
5356 #ifdef TCC_TARGET_PE
5358 type
.t
|= VT_IMPORT
;
5360 type
.t
|= VT_EXPORT
;
5364 error("cannot use local functions");
5365 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5366 expect("function definition");
5368 /* reject abstract declarators in function definition */
5370 while ((sym
= sym
->next
) != NULL
)
5371 if (!(sym
->v
& ~SYM_FIELD
))
5372 expect("identifier");
5374 /* XXX: cannot do better now: convert extern line to static inline */
5375 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5376 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5380 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5383 r
= sym
->type
.ref
->r
;
5384 /* use func_call from prototype if not defined */
5385 if (FUNC_CALL(r
) != FUNC_CDECL
5386 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5387 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5389 /* use export from prototype */
5391 FUNC_EXPORT(type
.ref
->r
) = 1;
5393 /* use static from prototype */
5394 if (sym
->type
.t
& VT_STATIC
)
5395 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5397 if (!is_compatible_types(&sym
->type
, &type
)) {
5399 error("incompatible types for redefinition of '%s'",
5400 get_tok_str(v
, NULL
));
5402 /* if symbol is already defined, then put complete type */
5405 /* put function symbol */
5406 sym
= global_identifier_push(v
, type
.t
, 0);
5407 sym
->type
.ref
= type
.ref
;
5410 /* static inline functions are just recorded as a kind
5411 of macro. Their code will be emitted at the end of
5412 the compilation unit only if they are used */
5413 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5414 (VT_INLINE
| VT_STATIC
)) {
5415 TokenString func_str
;
5417 struct InlineFunc
*fn
;
5418 const char *filename
;
5420 tok_str_new(&func_str
);
5426 error("unexpected end of file");
5427 tok_str_add_tok(&func_str
);
5432 } else if (t
== '}') {
5434 if (block_level
== 0)
5438 tok_str_add(&func_str
, -1);
5439 tok_str_add(&func_str
, 0);
5440 filename
= file
? file
->filename
: "";
5441 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5442 strcpy(fn
->filename
, filename
);
5444 fn
->token_str
= func_str
.str
;
5445 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5448 /* compute text section */
5449 cur_text_section
= ad
.section
;
5450 if (!cur_text_section
)
5451 cur_text_section
= text_section
;
5452 sym
->r
= VT_SYM
| VT_CONST
;
5457 if (btype
.t
& VT_TYPEDEF
) {
5458 /* save typedefed type */
5459 /* XXX: test storage specifiers ? */
5460 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5461 sym
->type
.t
|= VT_TYPEDEF
;
5462 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5464 /* external function definition */
5465 /* specific case for func_call attribute */
5466 type
.ref
->r
= INT_ATTR(&ad
);
5467 fn
= external_sym(v
, &type
, 0);
5469 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5475 /* Part 1: __USER_LABEL_PREFIX__ (user defined) */
5477 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5479 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5482 /* Part 2: api name */
5484 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5486 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5490 if (tcc_state
->warn_unsupported
)
5491 warning("ignoring redirection from %s to %s\n", get_tok_str(v
, NULL
), target
);
5493 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
5494 parse_attribute((AttributeDef
*) &fn
->type
.ref
->r
);
5497 /* not lvalue if array */
5499 if (!(type
.t
& VT_ARRAY
))
5500 r
|= lvalue_type(type
.t
);
5501 has_init
= (tok
== '=');
5502 if ((btype
.t
& VT_EXTERN
) ||
5503 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5504 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5505 /* external variable */
5506 /* NOTE: as GCC, uninitialized global static
5507 arrays of null size are considered as
5509 external_sym(v
, &type
, r
);
5511 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5512 if (type
.t
& VT_STATIC
)
5518 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);