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 integer constant */
288 ST_FUNC
void vpushi(int v
)
292 vsetc(&int_type
, VT_CONST
, &cval
);
295 /* push long long constant */
296 static void vpushll(long long v
)
303 vsetc(&ctype
, VT_CONST
, &cval
);
306 /* Return a static symbol pointing to a section */
307 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
313 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
314 sym
->type
.ref
= type
->ref
;
315 sym
->r
= VT_CONST
| VT_SYM
;
316 put_extern_sym(sym
, sec
, offset
, size
);
320 /* push a reference to a section offset by adding a dummy symbol */
321 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
326 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
327 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
330 /* define a new external reference to a symbol 'v' of type 'u' */
331 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
337 /* push forward reference */
338 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
339 s
->type
.ref
= type
->ref
;
340 s
->r
= r
| VT_CONST
| VT_SYM
;
345 /* define a new external reference to a symbol 'v' of type 'u' */
346 static Sym
*external_sym(int v
, CType
*type
, int r
)
352 /* push forward reference */
353 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
354 s
->type
.t
|= VT_EXTERN
;
355 } else if (s
->type
.ref
== func_old_type
.ref
) {
356 s
->type
.ref
= type
->ref
;
357 s
->r
= r
| VT_CONST
| VT_SYM
;
358 s
->type
.t
|= VT_EXTERN
;
359 } else if (!is_compatible_types(&s
->type
, type
)) {
360 error("incompatible types for redefinition of '%s'",
361 get_tok_str(v
, NULL
));
366 /* push a reference to global symbol v */
367 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
372 sym
= external_global_sym(v
, type
, 0);
374 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
378 ST_FUNC
void vset(CType
*type
, int r
, int v
)
383 vsetc(type
, r
, &cval
);
386 static void vseti(int r
, int v
)
394 ST_FUNC
void vswap(void)
403 ST_FUNC
void vpushv(SValue
*v
)
405 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
406 error("memory full");
411 static void vdup(void)
416 /* save r to the memory stack, and mark it as being free */
417 ST_FUNC
void save_reg(int r
)
419 int l
, saved
, size
, align
;
423 /* modify all stack values */
426 for(p
=vstack
;p
<=vtop
;p
++) {
427 if ((p
->r
& VT_VALMASK
) == r
||
428 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
429 /* must save value on stack if not already done */
431 /* NOTE: must reload 'r' because r might be equal to r2 */
432 r
= p
->r
& VT_VALMASK
;
433 /* store register in the stack */
435 if ((p
->r
& VT_LVAL
) ||
436 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
437 #ifdef TCC_TARGET_X86_64
438 type
= &char_pointer_type
;
442 size
= type_size(type
, &align
);
443 loc
= (loc
- size
) & -align
;
445 sv
.r
= VT_LOCAL
| VT_LVAL
;
448 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
449 /* x86 specific: need to pop fp register ST0 if saved */
451 o(0xd8dd); /* fstp %st(0) */
454 #ifndef TCC_TARGET_X86_64
455 /* special long long case */
456 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
464 /* mark that stack entry as being saved on the stack */
465 if (p
->r
& VT_LVAL
) {
466 /* also clear the bounded flag because the
467 relocation address of the function was stored in
469 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
471 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
479 #ifdef TCC_TARGET_ARM
480 /* find a register of class 'rc2' with at most one reference on stack.
481 * If none, call get_reg(rc) */
482 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
487 for(r
=0;r
<NB_REGS
;r
++) {
488 if (reg_classes
[r
] & rc2
) {
491 for(p
= vstack
; p
<= vtop
; p
++) {
492 if ((p
->r
& VT_VALMASK
) == r
||
493 (p
->r2
& VT_VALMASK
) == r
)
504 /* find a free register of class 'rc'. If none, save one register */
505 ST_FUNC
int get_reg(int rc
)
510 /* find a free register */
511 for(r
=0;r
<NB_REGS
;r
++) {
512 if (reg_classes
[r
] & rc
) {
513 for(p
=vstack
;p
<=vtop
;p
++) {
514 if ((p
->r
& VT_VALMASK
) == r
||
515 (p
->r2
& VT_VALMASK
) == r
)
523 /* no register left : free the first one on the stack (VERY
524 IMPORTANT to start from the bottom to ensure that we don't
525 spill registers used in gen_opi()) */
526 for(p
=vstack
;p
<=vtop
;p
++) {
527 r
= p
->r
& VT_VALMASK
;
528 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
530 /* also look at second register (if long long) */
531 r
= p
->r2
& VT_VALMASK
;
532 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
538 /* Should never comes here */
542 /* save registers up to (vtop - n) stack entry */
543 ST_FUNC
void save_regs(int n
)
548 for(p
= vstack
;p
<= p1
; p
++) {
549 r
= p
->r
& VT_VALMASK
;
556 /* move register 's' to 'r', and flush previous value of r to memory
558 static void move_reg(int r
, int s
)
571 /* get address of vtop (vtop MUST BE an lvalue) */
572 static void gaddrof(void)
575 /* tricky: if saved lvalue, then we can go back to lvalue */
576 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
577 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
580 #ifdef CONFIG_TCC_BCHECK
581 /* generate lvalue bound code */
582 static void gbound(void)
587 vtop
->r
&= ~VT_MUSTBOUND
;
588 /* if lvalue, then use checking code before dereferencing */
589 if (vtop
->r
& VT_LVAL
) {
590 /* if not VT_BOUNDED value, then make one */
591 if (!(vtop
->r
& VT_BOUNDED
)) {
592 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
593 /* must save type because we must set it to int to get pointer */
595 vtop
->type
.t
= VT_INT
;
598 gen_bounded_ptr_add();
599 vtop
->r
|= lval_type
;
602 /* then check for dereferencing */
603 gen_bounded_ptr_deref();
608 /* store vtop a register belonging to class 'rc'. lvalues are
609 converted to values. Cannot be used if cannot be converted to
610 register value (such as structures). */
611 ST_FUNC
int gv(int rc
)
613 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
615 /* NOTE: get_reg can modify vstack[] */
616 if (vtop
->type
.t
& VT_BITFIELD
) {
619 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
620 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
621 /* remove bit field info to avoid loops */
622 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
623 /* cast to int to propagate signedness in following ops */
624 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
629 if((vtop
->type
.t
& VT_UNSIGNED
) ||
630 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
631 type
.t
|= VT_UNSIGNED
;
633 /* generate shifts */
634 vpushi(bits
- (bit_pos
+ bit_size
));
636 vpushi(bits
- bit_size
);
637 /* NOTE: transformed to SHR if unsigned */
641 if (is_float(vtop
->type
.t
) &&
642 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
645 unsigned long offset
;
646 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
650 /* XXX: unify with initializers handling ? */
651 /* CPUs usually cannot use float constants, so we store them
652 generically in data segment */
653 size
= type_size(&vtop
->type
, &align
);
654 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
655 data_section
->data_offset
= offset
;
656 /* XXX: not portable yet */
657 #if defined(__i386__) || defined(__x86_64__)
658 /* Zero pad x87 tenbyte long doubles */
659 if (size
== LDOUBLE_SIZE
)
660 vtop
->c
.tab
[2] &= 0xffff;
662 ptr
= section_ptr_add(data_section
, size
);
664 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
668 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
672 ptr
[i
] = vtop
->c
.tab
[i
];
673 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
674 vtop
->r
|= VT_LVAL
| VT_SYM
;
678 #ifdef CONFIG_TCC_BCHECK
679 if (vtop
->r
& VT_MUSTBOUND
)
683 r
= vtop
->r
& VT_VALMASK
;
687 /* need to reload if:
689 - lvalue (need to dereference pointer)
690 - already a register, but not in the right class */
692 || (vtop
->r
& VT_LVAL
)
693 || !(reg_classes
[r
] & rc
)
694 #ifndef TCC_TARGET_X86_64
695 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
700 #ifndef TCC_TARGET_X86_64
701 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
703 unsigned long long ll
;
704 /* two register type load : expand to two words
706 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
709 vtop
->c
.ui
= ll
; /* first word */
711 vtop
->r
= r
; /* save register value */
712 vpushi(ll
>> 32); /* second word */
713 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
714 (vtop
->r
& VT_LVAL
)) {
715 /* We do not want to modifier the long long
716 pointer here, so the safest (and less
717 efficient) is to save all the other registers
718 in the stack. XXX: totally inefficient. */
720 /* load from memory */
723 vtop
[-1].r
= r
; /* save register value */
724 /* increment pointer to get second word */
725 vtop
->type
.t
= VT_INT
;
734 vtop
[-1].r
= r
; /* save register value */
735 vtop
->r
= vtop
[-1].r2
;
737 /* allocate second register */
741 /* write second register */
745 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
747 /* lvalue of scalar type : need to use lvalue type
748 because of possible cast */
751 /* compute memory access type */
752 if (vtop
->r
& VT_LVAL_BYTE
)
754 else if (vtop
->r
& VT_LVAL_SHORT
)
756 if (vtop
->r
& VT_LVAL_UNSIGNED
)
760 /* restore wanted type */
763 /* one register type load */
768 #ifdef TCC_TARGET_C67
769 /* uses register pairs for doubles */
770 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
777 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
778 ST_FUNC
void gv2(int rc1
, int rc2
)
782 /* generate more generic register first. But VT_JMP or VT_CMP
783 values must be generated first in all cases to avoid possible
785 v
= vtop
[0].r
& VT_VALMASK
;
786 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
791 /* test if reload is needed for first register */
792 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
802 /* test if reload is needed for first register */
803 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
809 /* wrapper around RC_FRET to return a register by type */
810 static int rc_fret(int t
)
812 #ifdef TCC_TARGET_X86_64
813 if (t
== VT_LDOUBLE
) {
820 /* wrapper around REG_FRET to return a register by type */
821 static int reg_fret(int t
)
823 #ifdef TCC_TARGET_X86_64
824 if (t
== VT_LDOUBLE
) {
831 /* expand long long on stack in two int registers */
832 static void lexpand(void)
836 u
= vtop
->type
.t
& VT_UNSIGNED
;
839 vtop
[0].r
= vtop
[-1].r2
;
840 vtop
[0].r2
= VT_CONST
;
841 vtop
[-1].r2
= VT_CONST
;
842 vtop
[0].type
.t
= VT_INT
| u
;
843 vtop
[-1].type
.t
= VT_INT
| u
;
846 #ifdef TCC_TARGET_ARM
847 /* expand long long on stack */
848 ST_FUNC
void lexpand_nr(void)
852 u
= vtop
->type
.t
& VT_UNSIGNED
;
855 vtop
->type
.t
= VT_INT
| u
;
856 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
858 vtop
[-1].c
.ui
= vtop
->c
.ull
;
859 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
861 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
863 vtop
->r
= vtop
[-1].r
;
864 } else if (v
> VT_CONST
) {
868 vtop
->r
= vtop
[-1].r2
;
869 vtop
[-1].r2
= VT_CONST
;
870 vtop
[-1].type
.t
= VT_INT
| u
;
874 /* build a long long from two ints */
875 static void lbuild(int t
)
878 vtop
[-1].r2
= vtop
[0].r
;
883 /* rotate n first stack elements to the bottom
884 I1 ... In -> I2 ... In I1 [top is right]
886 static void vrotb(int n
)
897 /* rotate n first stack elements to the top
898 I1 ... In -> In I1 ... I(n-1) [top is right]
900 ST_FUNC
void vrott(int n
)
906 for(i
= 0;i
< n
- 1; i
++)
907 vtop
[-i
] = vtop
[-i
- 1];
911 #ifdef TCC_TARGET_ARM
912 /* like vrott but in other direction
913 In ... I1 -> I(n-1) ... I1 In [top is right]
915 ST_FUNC
void vnrott(int n
)
921 for(i
= n
- 1; i
> 0; i
--)
922 vtop
[-i
] = vtop
[-i
+ 1];
927 /* pop stack value */
928 ST_FUNC
void vpop(void)
931 v
= vtop
->r
& VT_VALMASK
;
932 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
933 /* for x86, we need to pop the FP stack */
934 if (v
== TREG_ST0
&& !nocode_wanted
) {
935 o(0xd8dd); /* fstp %st(0) */
938 if (v
== VT_JMP
|| v
== VT_JMPI
) {
939 /* need to put correct jump if && or || without test */
945 /* convert stack entry to register and duplicate its value in another
947 static void gv_dup(void)
953 if ((t
& VT_BTYPE
) == VT_LLONG
) {
960 /* stack: H L L1 H1 */
968 /* duplicate value */
973 #ifdef TCC_TARGET_X86_64
974 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
984 load(r1
, &sv
); /* move r to r1 */
986 /* duplicates value */
992 #ifndef TCC_TARGET_X86_64
993 /* generate CPU independent (unsigned) long long operations */
994 static void gen_opl(int op
)
996 int t
, a
, b
, op1
, c
, i
;
998 unsigned short reg_iret
= REG_IRET
;
999 unsigned short reg_lret
= REG_LRET
;
1005 func
= TOK___divdi3
;
1008 func
= TOK___udivdi3
;
1011 func
= TOK___moddi3
;
1014 func
= TOK___umoddi3
;
1021 /* call generic long long function */
1022 vpush_global_sym(&func_old_type
, func
);
1027 vtop
->r2
= reg_lret
;
1040 /* stack: L1 H1 L2 H2 */
1045 vtop
[-2] = vtop
[-3];
1048 /* stack: H1 H2 L1 L2 */
1054 /* stack: H1 H2 L1 L2 ML MH */
1057 /* stack: ML MH H1 H2 L1 L2 */
1061 /* stack: ML MH H1 L2 H2 L1 */
1066 /* stack: ML MH M1 M2 */
1069 } else if (op
== '+' || op
== '-') {
1070 /* XXX: add non carry method too (for MIPS or alpha) */
1076 /* stack: H1 H2 (L1 op L2) */
1079 gen_op(op1
+ 1); /* TOK_xxxC2 */
1082 /* stack: H1 H2 (L1 op L2) */
1085 /* stack: (L1 op L2) H1 H2 */
1087 /* stack: (L1 op L2) (H1 op H2) */
1095 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1096 t
= vtop
[-1].type
.t
;
1100 /* stack: L H shift */
1102 /* constant: simpler */
1103 /* NOTE: all comments are for SHL. the other cases are
1104 done by swaping words */
1115 if (op
!= TOK_SAR
) {
1148 /* XXX: should provide a faster fallback on x86 ? */
1151 func
= TOK___ashrdi3
;
1154 func
= TOK___lshrdi3
;
1157 func
= TOK___ashldi3
;
1163 /* compare operations */
1169 /* stack: L1 H1 L2 H2 */
1171 vtop
[-1] = vtop
[-2];
1173 /* stack: L1 L2 H1 H2 */
1176 /* when values are equal, we need to compare low words. since
1177 the jump is inverted, we invert the test too. */
1180 else if (op1
== TOK_GT
)
1182 else if (op1
== TOK_ULT
)
1184 else if (op1
== TOK_UGT
)
1189 if (op1
!= TOK_NE
) {
1193 /* generate non equal test */
1194 /* XXX: NOT PORTABLE yet */
1198 #if defined(TCC_TARGET_I386)
1199 b
= psym(0x850f, 0);
1200 #elif defined(TCC_TARGET_ARM)
1202 o(0x1A000000 | encbranch(ind
, 0, 1));
1203 #elif defined(TCC_TARGET_C67)
1204 error("not implemented");
1206 #error not supported
1210 /* compare low. Always unsigned */
1214 else if (op1
== TOK_LE
)
1216 else if (op1
== TOK_GT
)
1218 else if (op1
== TOK_GE
)
1229 /* handle integer constant optimizations and various machine
1231 static void gen_opic(int op
)
1233 int c1
, c2
, t1
, t2
, n
;
1236 typedef unsigned long long U
;
1240 t1
= v1
->type
.t
& VT_BTYPE
;
1241 t2
= v2
->type
.t
& VT_BTYPE
;
1245 else if (v1
->type
.t
& VT_UNSIGNED
)
1252 else if (v2
->type
.t
& VT_UNSIGNED
)
1257 /* currently, we cannot do computations with forward symbols */
1258 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1259 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1262 case '+': l1
+= l2
; break;
1263 case '-': l1
-= l2
; break;
1264 case '&': l1
&= l2
; break;
1265 case '^': l1
^= l2
; break;
1266 case '|': l1
|= l2
; break;
1267 case '*': l1
*= l2
; break;
1274 /* if division by zero, generate explicit division */
1277 error("division by zero in constant");
1281 default: l1
/= l2
; break;
1282 case '%': l1
%= l2
; break;
1283 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1284 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1287 case TOK_SHL
: l1
<<= l2
; break;
1288 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1289 case TOK_SAR
: l1
>>= l2
; break;
1291 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1292 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1293 case TOK_EQ
: l1
= l1
== l2
; break;
1294 case TOK_NE
: l1
= l1
!= l2
; break;
1295 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1296 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1297 case TOK_LT
: l1
= l1
< l2
; break;
1298 case TOK_GE
: l1
= l1
>= l2
; break;
1299 case TOK_LE
: l1
= l1
<= l2
; break;
1300 case TOK_GT
: l1
= l1
> l2
; break;
1302 case TOK_LAND
: l1
= l1
&& l2
; break;
1303 case TOK_LOR
: l1
= l1
|| l2
; break;
1310 /* if commutative ops, put c2 as constant */
1311 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1312 op
== '|' || op
== '*')) {
1314 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1315 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1317 /* Filter out NOP operations like x*1, x-0, x&-1... */
1318 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1321 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1322 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1328 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1329 /* try to use shifts instead of muls or divs */
1330 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1339 else if (op
== TOK_PDIV
)
1345 } else if (c2
&& (op
== '+' || op
== '-') &&
1346 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1347 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1348 /* symbol + constant case */
1355 if (!nocode_wanted
) {
1356 /* call low level op generator */
1357 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1368 /* generate a floating point operation with constant propagation */
1369 static void gen_opif(int op
)
1377 /* currently, we cannot do computations with forward symbols */
1378 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1379 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1381 if (v1
->type
.t
== VT_FLOAT
) {
1384 } else if (v1
->type
.t
== VT_DOUBLE
) {
1392 /* NOTE: we only do constant propagation if finite number (not
1393 NaN or infinity) (ANSI spec) */
1394 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1398 case '+': f1
+= f2
; break;
1399 case '-': f1
-= f2
; break;
1400 case '*': f1
*= f2
; break;
1404 error("division by zero in constant");
1409 /* XXX: also handles tests ? */
1413 /* XXX: overflow test ? */
1414 if (v1
->type
.t
== VT_FLOAT
) {
1416 } else if (v1
->type
.t
== VT_DOUBLE
) {
1424 if (!nocode_wanted
) {
1432 static int pointed_size(CType
*type
)
1435 return type_size(pointed_type(type
), &align
);
1438 static inline int is_null_pointer(SValue
*p
)
1440 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1442 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1443 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1446 static inline int is_integer_btype(int bt
)
1448 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1449 bt
== VT_INT
|| bt
== VT_LLONG
);
1452 /* check types for comparison or substraction of pointers */
1453 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1455 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1458 /* null pointers are accepted for all comparisons as gcc */
1459 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1463 bt1
= type1
->t
& VT_BTYPE
;
1464 bt2
= type2
->t
& VT_BTYPE
;
1465 /* accept comparison between pointer and integer with a warning */
1466 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1467 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1468 warning("comparison between pointer and integer");
1472 /* both must be pointers or implicit function pointers */
1473 if (bt1
== VT_PTR
) {
1474 type1
= pointed_type(type1
);
1475 } else if (bt1
!= VT_FUNC
)
1476 goto invalid_operands
;
1478 if (bt2
== VT_PTR
) {
1479 type2
= pointed_type(type2
);
1480 } else if (bt2
!= VT_FUNC
) {
1482 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1484 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1485 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1489 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1490 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1491 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1492 /* gcc-like error if '-' is used */
1494 goto invalid_operands
;
1496 warning("comparison of distinct pointer types lacks a cast");
1500 /* generic gen_op: handles types problems */
1501 ST_FUNC
void gen_op(int op
)
1503 int u
, t1
, t2
, bt1
, bt2
, t
;
1506 t1
= vtop
[-1].type
.t
;
1507 t2
= vtop
[0].type
.t
;
1508 bt1
= t1
& VT_BTYPE
;
1509 bt2
= t2
& VT_BTYPE
;
1511 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1512 /* at least one operand is a pointer */
1513 /* relationnal op: must be both pointers */
1514 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1515 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1516 /* pointers are handled are unsigned */
1517 #ifdef TCC_TARGET_X86_64
1518 t
= VT_LLONG
| VT_UNSIGNED
;
1520 t
= VT_INT
| VT_UNSIGNED
;
1524 /* if both pointers, then it must be the '-' op */
1525 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1527 error("cannot use pointers here");
1528 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1529 /* XXX: check that types are compatible */
1530 u
= pointed_size(&vtop
[-1].type
);
1532 /* set to integer type */
1533 #ifdef TCC_TARGET_X86_64
1534 vtop
->type
.t
= VT_LLONG
;
1536 vtop
->type
.t
= VT_INT
;
1541 /* exactly one pointer : must be '+' or '-'. */
1542 if (op
!= '-' && op
!= '+')
1543 error("cannot use pointers here");
1544 /* Put pointer as first operand */
1545 if (bt2
== VT_PTR
) {
1549 type1
= vtop
[-1].type
;
1550 type1
.t
&= ~VT_ARRAY
;
1551 #ifdef TCC_TARGET_X86_64
1552 vpushll(pointed_size(&vtop
[-1].type
));
1554 /* XXX: cast to int ? (long long case) */
1555 vpushi(pointed_size(&vtop
[-1].type
));
1558 #ifdef CONFIG_TCC_BCHECK
1559 /* if evaluating constant expression, no code should be
1560 generated, so no bound check */
1561 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1562 /* if bounded pointers, we generate a special code to
1569 gen_bounded_ptr_add();
1575 /* put again type if gen_opic() swaped operands */
1578 } else if (is_float(bt1
) || is_float(bt2
)) {
1579 /* compute bigger type and do implicit casts */
1580 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1582 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1587 /* floats can only be used for a few operations */
1588 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1589 (op
< TOK_ULT
|| op
> TOK_GT
))
1590 error("invalid operands for binary operation");
1592 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1593 /* cast to biggest op */
1595 /* convert to unsigned if it does not fit in a long long */
1596 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1597 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1601 /* integer operations */
1603 /* convert to unsigned if it does not fit in an integer */
1604 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1605 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1608 /* XXX: currently, some unsigned operations are explicit, so
1609 we modify them here */
1610 if (t
& VT_UNSIGNED
) {
1617 else if (op
== TOK_LT
)
1619 else if (op
== TOK_GT
)
1621 else if (op
== TOK_LE
)
1623 else if (op
== TOK_GE
)
1630 /* special case for shifts and long long: we keep the shift as
1632 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1639 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1640 /* relationnal op: the result is an int */
1641 vtop
->type
.t
= VT_INT
;
1648 #ifndef TCC_TARGET_ARM
1649 /* generic itof for unsigned long long case */
1650 static void gen_cvt_itof1(int t
)
1652 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1653 (VT_LLONG
| VT_UNSIGNED
)) {
1656 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1657 #if LDOUBLE_SIZE != 8
1658 else if (t
== VT_LDOUBLE
)
1659 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1662 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1666 vtop
->r
= reg_fret(t
);
1673 /* generic ftoi for unsigned long long case */
1674 static void gen_cvt_ftoi1(int t
)
1678 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1679 /* not handled natively */
1680 st
= vtop
->type
.t
& VT_BTYPE
;
1682 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1683 #if LDOUBLE_SIZE != 8
1684 else if (st
== VT_LDOUBLE
)
1685 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1688 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1693 vtop
->r2
= REG_LRET
;
1699 /* force char or short cast */
1700 static void force_charshort_cast(int t
)
1704 /* XXX: add optimization if lvalue : just change type and offset */
1709 if (t
& VT_UNSIGNED
) {
1710 vpushi((1 << bits
) - 1);
1716 /* result must be signed or the SAR is converted to an SHL
1717 This was not the case when "t" was a signed short
1718 and the last value on the stack was an unsigned int */
1719 vtop
->type
.t
&= ~VT_UNSIGNED
;
1725 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1726 static void gen_cast(CType
*type
)
1728 int sbt
, dbt
, sf
, df
, c
, p
;
1730 /* special delayed cast for char/short */
1731 /* XXX: in some cases (multiple cascaded casts), it may still
1733 if (vtop
->r
& VT_MUSTCAST
) {
1734 vtop
->r
&= ~VT_MUSTCAST
;
1735 force_charshort_cast(vtop
->type
.t
);
1738 /* bitfields first get cast to ints */
1739 if (vtop
->type
.t
& VT_BITFIELD
) {
1743 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1744 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1749 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1750 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1752 /* constant case: we can do it now */
1753 /* XXX: in ISOC, cannot do it if error in convert */
1754 if (sbt
== VT_FLOAT
)
1755 vtop
->c
.ld
= vtop
->c
.f
;
1756 else if (sbt
== VT_DOUBLE
)
1757 vtop
->c
.ld
= vtop
->c
.d
;
1760 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1761 if (sbt
& VT_UNSIGNED
)
1762 vtop
->c
.ld
= vtop
->c
.ull
;
1764 vtop
->c
.ld
= vtop
->c
.ll
;
1766 if (sbt
& VT_UNSIGNED
)
1767 vtop
->c
.ld
= vtop
->c
.ui
;
1769 vtop
->c
.ld
= vtop
->c
.i
;
1772 if (dbt
== VT_FLOAT
)
1773 vtop
->c
.f
= (float)vtop
->c
.ld
;
1774 else if (dbt
== VT_DOUBLE
)
1775 vtop
->c
.d
= (double)vtop
->c
.ld
;
1776 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1777 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1778 } else if (sf
&& dbt
== VT_BOOL
) {
1779 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1782 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1783 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1784 vtop
->c
.ll
= vtop
->c
.ull
;
1785 else if (sbt
& VT_UNSIGNED
)
1786 vtop
->c
.ll
= vtop
->c
.ui
;
1787 #ifdef TCC_TARGET_X86_64
1788 else if (sbt
== VT_PTR
)
1791 else if (sbt
!= VT_LLONG
)
1792 vtop
->c
.ll
= vtop
->c
.i
;
1794 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1795 vtop
->c
.ull
= vtop
->c
.ll
;
1796 else if (dbt
== VT_BOOL
)
1797 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1798 else if (dbt
!= VT_LLONG
) {
1800 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1802 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1805 if(dbt
& VT_UNSIGNED
)
1806 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1808 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1811 } else if (p
&& dbt
== VT_BOOL
) {
1814 } else if (!nocode_wanted
) {
1815 /* non constant case: generate code */
1817 /* convert from fp to fp */
1820 /* convert int to fp */
1823 /* convert fp to int */
1824 if (dbt
== VT_BOOL
) {
1828 /* we handle char/short/etc... with generic code */
1829 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1830 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1834 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1835 /* additional cast for char/short... */
1840 #ifndef TCC_TARGET_X86_64
1841 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1842 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1843 /* scalar to long long */
1844 /* machine independent conversion */
1846 /* generate high word */
1847 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1851 if (sbt
== VT_PTR
) {
1852 /* cast from pointer to int before we apply
1853 shift operation, which pointers don't support*/
1854 gen_cast(&int_type
);
1860 /* patch second register */
1861 vtop
[-1].r2
= vtop
->r
;
1865 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1866 (dbt
& VT_BTYPE
) == VT_PTR
) {
1867 /* XXX: not sure if this is perfect... need more tests */
1868 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1870 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1871 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1872 /* x86_64 specific: movslq */
1874 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1878 } else if (dbt
== VT_BOOL
) {
1879 /* scalar to bool */
1882 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1883 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1884 if (sbt
== VT_PTR
) {
1885 vtop
->type
.t
= VT_INT
;
1886 warning("nonportable conversion from pointer to char/short");
1888 force_charshort_cast(dbt
);
1889 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1891 if (sbt
== VT_LLONG
) {
1892 /* from long long: just take low order word */
1896 /* if lvalue and single word type, nothing to do because
1897 the lvalue already contains the real type size (see
1898 VT_LVAL_xxx constants) */
1901 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1902 /* if we are casting between pointer types,
1903 we must update the VT_LVAL_xxx size */
1904 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1905 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1910 /* return type size. Put alignment at 'a' */
1911 ST_FUNC
int type_size(CType
*type
, int *a
)
1916 bt
= type
->t
& VT_BTYPE
;
1917 if (bt
== VT_STRUCT
) {
1922 } else if (bt
== VT_PTR
) {
1923 if (type
->t
& VT_ARRAY
) {
1927 ts
= type_size(&s
->type
, a
);
1929 if (ts
< 0 && s
->c
< 0)
1937 } else if (bt
== VT_LDOUBLE
) {
1939 return LDOUBLE_SIZE
;
1940 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1941 #ifdef TCC_TARGET_I386
1942 #ifdef TCC_TARGET_PE
1947 #elif defined(TCC_TARGET_ARM)
1957 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1960 } else if (bt
== VT_SHORT
) {
1964 /* char, void, function, _Bool */
1970 /* return the pointed type of t */
1971 static inline CType
*pointed_type(CType
*type
)
1973 return &type
->ref
->type
;
1976 /* modify type so that its it is a pointer to type. */
1977 ST_FUNC
void mk_pointer(CType
*type
)
1980 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1981 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1985 /* compare function types. OLD functions match any new functions */
1986 static int is_compatible_func(CType
*type1
, CType
*type2
)
1992 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1994 /* check func_call */
1995 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1997 /* XXX: not complete */
1998 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2002 while (s1
!= NULL
) {
2005 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2015 /* return true if type1 and type2 are the same. If unqualified is
2016 true, qualifiers on the types are ignored.
2018 - enums are not checked as gcc __builtin_types_compatible_p ()
2020 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2024 t1
= type1
->t
& VT_TYPE
;
2025 t2
= type2
->t
& VT_TYPE
;
2027 /* strip qualifiers before comparing */
2028 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2029 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2031 /* XXX: bitfields ? */
2034 /* test more complicated cases */
2035 bt1
= t1
& VT_BTYPE
;
2036 if (bt1
== VT_PTR
) {
2037 type1
= pointed_type(type1
);
2038 type2
= pointed_type(type2
);
2039 return is_compatible_types(type1
, type2
);
2040 } else if (bt1
== VT_STRUCT
) {
2041 return (type1
->ref
== type2
->ref
);
2042 } else if (bt1
== VT_FUNC
) {
2043 return is_compatible_func(type1
, type2
);
2049 /* return true if type1 and type2 are exactly the same (including
2052 static int is_compatible_types(CType
*type1
, CType
*type2
)
2054 return compare_types(type1
,type2
,0);
2057 /* return true if type1 and type2 are the same (ignoring qualifiers).
2059 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2061 return compare_types(type1
,type2
,1);
2064 /* print a type. If 'varstr' is not NULL, then the variable is also
2065 printed in the type */
2067 /* XXX: add array and function pointers */
2068 static void type_to_str(char *buf
, int buf_size
,
2069 CType
*type
, const char *varstr
)
2076 t
= type
->t
& VT_TYPE
;
2079 if (t
& VT_CONSTANT
)
2080 pstrcat(buf
, buf_size
, "const ");
2081 if (t
& VT_VOLATILE
)
2082 pstrcat(buf
, buf_size
, "volatile ");
2083 if (t
& VT_UNSIGNED
)
2084 pstrcat(buf
, buf_size
, "unsigned ");
2114 tstr
= "long double";
2116 pstrcat(buf
, buf_size
, tstr
);
2120 if (bt
== VT_STRUCT
)
2124 pstrcat(buf
, buf_size
, tstr
);
2125 v
= type
->ref
->v
& ~SYM_STRUCT
;
2126 if (v
>= SYM_FIRST_ANOM
)
2127 pstrcat(buf
, buf_size
, "<anonymous>");
2129 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2133 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2134 pstrcat(buf
, buf_size
, "(");
2136 while (sa
!= NULL
) {
2137 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2138 pstrcat(buf
, buf_size
, buf1
);
2141 pstrcat(buf
, buf_size
, ", ");
2143 pstrcat(buf
, buf_size
, ")");
2147 pstrcpy(buf1
, sizeof(buf1
), "*");
2149 pstrcat(buf1
, sizeof(buf1
), varstr
);
2150 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2154 pstrcat(buf
, buf_size
, " ");
2155 pstrcat(buf
, buf_size
, varstr
);
2160 /* verify type compatibility to store vtop in 'dt' type, and generate
2162 static void gen_assign_cast(CType
*dt
)
2164 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2165 char buf1
[256], buf2
[256];
2168 st
= &vtop
->type
; /* source type */
2169 dbt
= dt
->t
& VT_BTYPE
;
2170 sbt
= st
->t
& VT_BTYPE
;
2171 if (dt
->t
& VT_CONSTANT
)
2172 warning("assignment of read-only location");
2175 /* special cases for pointers */
2176 /* '0' can also be a pointer */
2177 if (is_null_pointer(vtop
))
2179 /* accept implicit pointer to integer cast with warning */
2180 if (is_integer_btype(sbt
)) {
2181 warning("assignment makes pointer from integer without a cast");
2184 type1
= pointed_type(dt
);
2185 /* a function is implicitely a function pointer */
2186 if (sbt
== VT_FUNC
) {
2187 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2188 !is_compatible_types(pointed_type(dt
), st
))
2189 warning("assignment from incompatible pointer type");
2194 type2
= pointed_type(st
);
2195 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2196 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2197 /* void * can match anything */
2199 /* exact type match, except for unsigned */
2202 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2203 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2204 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2205 warning("assignment from incompatible pointer type");
2207 /* check const and volatile */
2208 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2209 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2210 warning("assignment discards qualifiers from pointer target type");
2216 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2217 warning("assignment makes integer from pointer without a cast");
2219 /* XXX: more tests */
2224 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2225 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2226 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2228 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2229 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2230 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2238 /* store vtop in lvalue pushed on stack */
2239 ST_FUNC
void vstore(void)
2241 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2243 ft
= vtop
[-1].type
.t
;
2244 sbt
= vtop
->type
.t
& VT_BTYPE
;
2245 dbt
= ft
& VT_BTYPE
;
2246 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2247 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2248 /* optimize char/short casts */
2249 delayed_cast
= VT_MUSTCAST
;
2250 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2251 /* XXX: factorize */
2252 if (ft
& VT_CONSTANT
)
2253 warning("assignment of read-only location");
2256 if (!(ft
& VT_BITFIELD
))
2257 gen_assign_cast(&vtop
[-1].type
);
2260 if (sbt
== VT_STRUCT
) {
2261 /* if structure, only generate pointer */
2262 /* structure assignment : generate memcpy */
2263 /* XXX: optimize if small size */
2264 if (!nocode_wanted
) {
2265 size
= type_size(&vtop
->type
, &align
);
2269 vtop
->type
.t
= VT_PTR
;
2272 /* address of memcpy() */
2275 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2276 else if(!(align
& 3))
2277 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2280 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2285 vtop
->type
.t
= VT_PTR
;
2294 /* leave source on stack */
2295 } else if (ft
& VT_BITFIELD
) {
2296 /* bitfield store handling */
2297 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2298 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2299 /* remove bit field info to avoid loops */
2300 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2302 /* duplicate source into other register */
2307 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2308 gen_cast(&vtop
[-1].type
);
2309 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2312 /* duplicate destination */
2314 vtop
[-1] = vtop
[-2];
2316 /* mask and shift source */
2317 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2318 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2319 vpushll((1ULL << bit_size
) - 1ULL);
2321 vpushi((1 << bit_size
) - 1);
2327 /* load destination, mask and or with source */
2329 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2330 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2332 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2339 /* pop off shifted source from "duplicate source..." above */
2343 #ifdef CONFIG_TCC_BCHECK
2344 /* bound check case */
2345 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2351 if (!nocode_wanted
) {
2355 #ifdef TCC_TARGET_X86_64
2356 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2361 r
= gv(rc
); /* generate value */
2362 /* if lvalue was saved on stack, must read it */
2363 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2365 t
= get_reg(RC_INT
);
2366 #ifdef TCC_TARGET_X86_64
2371 sv
.r
= VT_LOCAL
| VT_LVAL
;
2372 sv
.c
.ul
= vtop
[-1].c
.ul
;
2374 vtop
[-1].r
= t
| VT_LVAL
;
2377 #ifndef TCC_TARGET_X86_64
2378 /* two word case handling : store second register at word + 4 */
2379 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2381 /* convert to int to increment easily */
2382 vtop
->type
.t
= VT_INT
;
2388 /* XXX: it works because r2 is spilled last ! */
2389 store(vtop
->r2
, vtop
- 1);
2394 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2395 vtop
->r
|= delayed_cast
;
2399 /* post defines POST/PRE add. c is the token ++ or -- */
2400 ST_FUNC
void inc(int post
, int c
)
2403 vdup(); /* save lvalue */
2405 gv_dup(); /* duplicate value */
2410 vpushi(c
- TOK_MID
);
2412 vstore(); /* store value */
2414 vpop(); /* if post op, return saved value */
2417 /* Parse GNUC __attribute__ extension. Currently, the following
2418 extensions are recognized:
2419 - aligned(n) : set data/function alignment.
2420 - packed : force data alignment to 1
2421 - section(x) : generate data/code in this section.
2422 - unused : currently ignored, but may be used someday.
2423 - regparm(n) : pass function parameters in registers (i386 only)
2425 static void parse_attribute(AttributeDef
*ad
)
2429 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2433 while (tok
!= ')') {
2434 if (tok
< TOK_IDENT
)
2435 expect("attribute name");
2443 expect("section name");
2444 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2453 if (n
<= 0 || (n
& (n
- 1)) != 0)
2454 error("alignment must be a positive power of two");
2467 /* currently, no need to handle it because tcc does not
2468 track unused objects */
2472 /* currently, no need to handle it because tcc does not
2473 track unused objects */
2478 ad
->func_call
= FUNC_CDECL
;
2483 ad
->func_call
= FUNC_STDCALL
;
2485 #ifdef TCC_TARGET_I386
2495 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2501 ad
->func_call
= FUNC_FASTCALLW
;
2508 ad
->mode
= VT_LLONG
+ 1;
2511 ad
->mode
= VT_SHORT
+ 1;
2514 ad
->mode
= VT_INT
+ 1;
2517 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2524 ad
->func_export
= 1;
2527 ad
->func_import
= 1;
2530 if (tcc_state
->warn_unsupported
)
2531 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2532 /* skip parameters */
2534 int parenthesis
= 0;
2538 else if (tok
== ')')
2541 } while (parenthesis
&& tok
!= -1);
2554 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2555 static void struct_decl(CType
*type
, int u
)
2557 int a
, v
, size
, align
, maxalign
, c
, offset
;
2558 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2559 Sym
*s
, *ss
, *ass
, **ps
;
2563 a
= tok
; /* save decl type */
2568 /* struct already defined ? return it */
2570 expect("struct/union/enum name");
2574 error("invalid type");
2581 /* we put an undefined size for struct/union */
2582 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2583 s
->r
= 0; /* default alignment is zero as gcc */
2584 /* put struct/union/enum name in type */
2592 error("struct/union/enum already defined");
2593 /* cannot be empty */
2595 /* non empty enums are not allowed */
2596 if (a
== TOK_ENUM
) {
2600 expect("identifier");
2606 /* enum symbols have static storage */
2607 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2608 ss
->type
.t
|= VT_STATIC
;
2613 /* NOTE: we accept a trailing comma */
2624 while (tok
!= '}') {
2625 parse_btype(&btype
, &ad
);
2631 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2632 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2633 expect("identifier");
2634 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2635 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2636 error("invalid type for '%s'",
2637 get_tok_str(v
, NULL
));
2641 bit_size
= expr_const();
2642 /* XXX: handle v = 0 case for messages */
2644 error("negative width in bit-field '%s'",
2645 get_tok_str(v
, NULL
));
2646 if (v
&& bit_size
== 0)
2647 error("zero width for bit-field '%s'",
2648 get_tok_str(v
, NULL
));
2650 size
= type_size(&type1
, &align
);
2652 if (align
< ad
.aligned
)
2654 } else if (ad
.packed
) {
2656 } else if (*tcc_state
->pack_stack_ptr
) {
2657 if (align
> *tcc_state
->pack_stack_ptr
)
2658 align
= *tcc_state
->pack_stack_ptr
;
2661 if (bit_size
>= 0) {
2662 bt
= type1
.t
& VT_BTYPE
;
2669 error("bitfields must have scalar type");
2671 if (bit_size
> bsize
) {
2672 error("width of '%s' exceeds its type",
2673 get_tok_str(v
, NULL
));
2674 } else if (bit_size
== bsize
) {
2675 /* no need for bit fields */
2677 } else if (bit_size
== 0) {
2678 /* XXX: what to do if only padding in a
2680 /* zero size: means to pad */
2683 /* we do not have enough room ?
2684 did the type change?
2686 if ((bit_pos
+ bit_size
) > bsize
||
2687 bt
!= prevbt
|| a
== TOK_UNION
)
2690 /* XXX: handle LSB first */
2691 type1
.t
|= VT_BITFIELD
|
2692 (bit_pos
<< VT_STRUCT_SHIFT
) |
2693 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2694 bit_pos
+= bit_size
;
2700 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2701 /* add new memory data only if starting
2703 if (lbit_pos
== 0) {
2704 if (a
== TOK_STRUCT
) {
2705 c
= (c
+ align
- 1) & -align
;
2714 if (align
> maxalign
)
2718 printf("add field %s offset=%d",
2719 get_tok_str(v
, NULL
), offset
);
2720 if (type1
.t
& VT_BITFIELD
) {
2721 printf(" pos=%d size=%d",
2722 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2723 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2728 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2730 while ((ass
= ass
->next
) != NULL
) {
2731 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2736 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2740 if (tok
== ';' || tok
== TOK_EOF
)
2747 /* store size and alignment */
2748 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2754 /* return 0 if no type declaration. otherwise, return the basic type
2757 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2759 int t
, u
, type_found
, typespec_found
, typedef_found
;
2763 memset(ad
, 0, sizeof(AttributeDef
));
2771 /* currently, we really ignore extension */
2781 if ((t
& VT_BTYPE
) != 0)
2782 error("too many basic types");
2798 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2799 #ifndef TCC_TARGET_PE
2800 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2802 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2803 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2817 if ((t
& VT_BTYPE
) == VT_LONG
) {
2818 #ifdef TCC_TARGET_PE
2819 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2821 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2829 struct_decl(&type1
, VT_ENUM
);
2832 type
->ref
= type1
.ref
;
2836 struct_decl(&type1
, VT_STRUCT
);
2839 /* type modifiers */
2892 /* GNUC attribute */
2893 case TOK_ATTRIBUTE1
:
2894 case TOK_ATTRIBUTE2
:
2895 parse_attribute(ad
);
2896 if (ATTR_MODE(ad
)) {
2897 u
= ATTR_MODE(ad
) -1;
2898 t
= (t
& ~VT_BTYPE
) | u
;
2906 parse_expr_type(&type1
);
2909 if (typespec_found
|| typedef_found
)
2912 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2915 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2916 type
->ref
= s
->type
.ref
;
2918 /* get attributes from typedef */
2919 if (0 == ad
->aligned
)
2920 ad
->aligned
= FUNC_ALIGN(s
->r
);
2921 if (0 == ad
->func_call
)
2922 ad
->func_call
= FUNC_CALL(s
->r
);
2923 ad
->packed
|= FUNC_PACKED(s
->r
);
2932 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2933 error("signed and unsigned modifier");
2934 if (tcc_state
->char_is_unsigned
) {
2935 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2940 /* long is never used as type */
2941 if ((t
& VT_BTYPE
) == VT_LONG
)
2942 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2943 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2945 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2951 /* convert a function parameter type (array to pointer and function to
2952 function pointer) */
2953 static inline void convert_parameter_type(CType
*pt
)
2955 /* remove const and volatile qualifiers (XXX: const could be used
2956 to indicate a const function parameter */
2957 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2958 /* array must be transformed to pointer according to ANSI C */
2960 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2965 static void post_type(CType
*type
, AttributeDef
*ad
)
2967 int n
, l
, t1
, arg_size
, align
;
2968 Sym
**plast
, *s
, *first
;
2973 /* function declaration */
2981 /* read param name and compute offset */
2982 if (l
!= FUNC_OLD
) {
2983 if (!parse_btype(&pt
, &ad1
)) {
2985 error("invalid type");
2992 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2994 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2995 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2996 error("parameter declared as void");
2997 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3002 expect("identifier");
3006 convert_parameter_type(&pt
);
3007 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3013 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3020 /* if no parameters, then old type prototype */
3024 t1
= type
->t
& VT_STORAGE
;
3025 /* NOTE: const is ignored in returned type as it has a special
3026 meaning in gcc / C++ */
3027 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3028 post_type(type
, ad
);
3029 /* we push a anonymous symbol which will contain the function prototype */
3030 ad
->func_args
= arg_size
;
3031 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3033 type
->t
= t1
| VT_FUNC
;
3035 } else if (tok
== '[') {
3036 /* array definition */
3038 if (tok
== TOK_RESTRICT1
)
3044 error("invalid array size");
3047 /* parse next post type */
3048 t1
= type
->t
& VT_STORAGE
;
3049 type
->t
&= ~VT_STORAGE
;
3050 post_type(type
, ad
);
3052 /* we push a anonymous symbol which will contain the array
3054 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3055 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3060 /* Parse a type declaration (except basic type), and return the type
3061 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3062 expected. 'type' should contain the basic type. 'ad' is the
3063 attribute definition of the basic type. It can be modified by
3066 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3069 CType type1
, *type2
;
3072 while (tok
== '*') {
3080 qualifiers
|= VT_CONSTANT
;
3085 qualifiers
|= VT_VOLATILE
;
3093 type
->t
|= qualifiers
;
3096 /* XXX: clarify attribute handling */
3097 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3098 parse_attribute(ad
);
3100 /* recursive type */
3101 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3102 type1
.t
= 0; /* XXX: same as int */
3105 /* XXX: this is not correct to modify 'ad' at this point, but
3106 the syntax is not clear */
3107 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3108 parse_attribute(ad
);
3109 type_decl(&type1
, ad
, v
, td
);
3112 /* type identifier */
3113 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3117 if (!(td
& TYPE_ABSTRACT
))
3118 expect("identifier");
3122 post_type(type
, ad
);
3123 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3124 parse_attribute(ad
);
3127 /* append type at the end of type1 */
3140 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3141 ST_FUNC
int lvalue_type(int t
)
3146 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3148 else if (bt
== VT_SHORT
)
3152 if (t
& VT_UNSIGNED
)
3153 r
|= VT_LVAL_UNSIGNED
;
3157 /* indirection with full error checking and bound check */
3158 ST_FUNC
void indir(void)
3160 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3161 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3165 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3167 vtop
->type
= *pointed_type(&vtop
->type
);
3168 /* Arrays and functions are never lvalues */
3169 if (!(vtop
->type
.t
& VT_ARRAY
)
3170 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3171 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3172 /* if bound checking, the referenced pointer must be checked */
3173 #ifdef CONFIG_TCC_BCHECK
3174 if (tcc_state
->do_bounds_check
)
3175 vtop
->r
|= VT_MUSTBOUND
;
3180 /* pass a parameter to a function and do type checking and casting */
3181 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3186 func_type
= func
->c
;
3187 if (func_type
== FUNC_OLD
||
3188 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3189 /* default casting : only need to convert float to double */
3190 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3194 } else if (arg
== NULL
) {
3195 error("too many arguments to function");
3198 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3199 gen_assign_cast(&type
);
3203 /* parse an expression of the form '(type)' or '(expr)' and return its
3205 static void parse_expr_type(CType
*type
)
3211 if (parse_btype(type
, &ad
)) {
3212 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3219 static void parse_type(CType
*type
)
3224 if (!parse_btype(type
, &ad
)) {
3227 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3230 static void vpush_tokc(int t
)
3235 vsetc(&type
, VT_CONST
, &tokc
);
3238 ST_FUNC
void unary(void)
3240 int n
, t
, align
, size
, r
;
3245 /* XXX: GCC 2.95.3 does not generate a table although it should be
3259 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3263 vpush_tokc(VT_LLONG
);
3267 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3271 vpush_tokc(VT_FLOAT
);
3275 vpush_tokc(VT_DOUBLE
);
3279 vpush_tokc(VT_LDOUBLE
);
3282 case TOK___FUNCTION__
:
3284 goto tok_identifier
;
3290 /* special function name identifier */
3291 len
= strlen(funcname
) + 1;
3292 /* generate char[len] type */
3297 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3298 ptr
= section_ptr_add(data_section
, len
);
3299 memcpy(ptr
, funcname
, len
);
3304 #ifdef TCC_TARGET_PE
3305 t
= VT_SHORT
| VT_UNSIGNED
;
3311 /* string parsing */
3314 if (tcc_state
->warn_write_strings
)
3319 memset(&ad
, 0, sizeof(AttributeDef
));
3320 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3325 if (parse_btype(&type
, &ad
)) {
3326 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3328 /* check ISOC99 compound literal */
3330 /* data is allocated locally by default */
3335 /* all except arrays are lvalues */
3336 if (!(type
.t
& VT_ARRAY
))
3337 r
|= lvalue_type(type
.t
);
3338 memset(&ad
, 0, sizeof(AttributeDef
));
3339 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3344 } else if (tok
== '{') {
3345 /* save all registers */
3347 /* statement expression : we do not accept break/continue
3348 inside as GCC does */
3349 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3364 /* functions names must be treated as function pointers,
3365 except for unary '&' and sizeof. Since we consider that
3366 functions are not lvalues, we only have to handle it
3367 there and in function calls. */
3368 /* arrays can also be used although they are not lvalues */
3369 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3370 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3372 mk_pointer(&vtop
->type
);
3378 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3380 boolean
.t
= VT_BOOL
;
3382 vtop
->c
.i
= !vtop
->c
.i
;
3383 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3384 vtop
->c
.i
= vtop
->c
.i
^ 1;
3387 vseti(VT_JMP
, gtst(1, 0));
3398 /* in order to force cast, we add zero */
3400 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3401 error("pointer not accepted for unary plus");
3411 parse_expr_type(&type
);
3415 size
= type_size(&type
, &align
);
3416 if (t
== TOK_SIZEOF
) {
3418 error("sizeof applied to an incomplete type");
3423 vtop
->type
.t
|= VT_UNSIGNED
;
3426 case TOK_builtin_types_compatible_p
:
3435 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3436 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3437 vpushi(is_compatible_types(&type1
, &type2
));
3440 case TOK_builtin_constant_p
:
3442 int saved_nocode_wanted
, res
;
3445 saved_nocode_wanted
= nocode_wanted
;
3448 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3450 nocode_wanted
= saved_nocode_wanted
;
3455 case TOK_builtin_frame_address
:
3460 if (tok
!= TOK_CINT
) {
3461 error("__builtin_frame_address only takes integers");
3464 error("TCC only supports __builtin_frame_address(0)");
3470 vset(&type
, VT_LOCAL
, 0);
3473 #ifdef TCC_TARGET_X86_64
3474 case TOK_builtin_malloc
:
3476 goto tok_identifier
;
3477 case TOK_builtin_free
:
3479 goto tok_identifier
;
3496 goto tok_identifier
;
3498 /* allow to take the address of a label */
3499 if (tok
< TOK_UIDENT
)
3500 expect("label identifier");
3501 s
= label_find(tok
);
3503 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3505 if (s
->r
== LABEL_DECLARED
)
3506 s
->r
= LABEL_FORWARD
;
3509 s
->type
.t
= VT_VOID
;
3510 mk_pointer(&s
->type
);
3511 s
->type
.t
|= VT_STATIC
;
3513 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3522 expect("identifier");
3526 error("'%s' undeclared", get_tok_str(t
, NULL
));
3527 /* for simple function calls, we tolerate undeclared
3528 external reference to int() function */
3529 if (tcc_state
->warn_implicit_function_declaration
)
3530 warning("implicit declaration of function '%s'",
3531 get_tok_str(t
, NULL
));
3532 s
= external_global_sym(t
, &func_old_type
, 0);
3534 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3535 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3536 /* if referencing an inline function, then we generate a
3537 symbol to it if not already done. It will have the
3538 effect to generate code for it at the end of the
3539 compilation unit. Inline function as always
3540 generated in the text section. */
3542 put_extern_sym(s
, text_section
, 0, 0);
3543 r
= VT_SYM
| VT_CONST
;
3547 vset(&s
->type
, r
, s
->c
);
3548 /* if forward reference, we must point to s */
3549 if (vtop
->r
& VT_SYM
) {
3556 /* post operations */
3558 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3561 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3564 if (tok
== TOK_ARROW
)
3566 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3570 /* expect pointer on structure */
3571 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3572 expect("struct or union");
3576 while ((s
= s
->next
) != NULL
) {
3581 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3582 /* add field offset to pointer */
3583 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3586 /* change type to field type, and set to lvalue */
3587 vtop
->type
= s
->type
;
3588 vtop
->type
.t
|= qualifiers
;
3589 /* an array is never an lvalue */
3590 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3591 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3592 #ifdef CONFIG_TCC_BCHECK
3593 /* if bound checking, the referenced pointer must be checked */
3594 if (tcc_state
->do_bounds_check
)
3595 vtop
->r
|= VT_MUSTBOUND
;
3599 } else if (tok
== '[') {
3605 } else if (tok
== '(') {
3611 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3612 /* pointer test (no array accepted) */
3613 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3614 vtop
->type
= *pointed_type(&vtop
->type
);
3615 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3619 expect("function pointer");
3622 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3624 /* get return type */
3627 sa
= s
->next
; /* first parameter */
3630 /* compute first implicit argument if a structure is returned */
3631 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3632 /* get some space for the returned structure */
3633 size
= type_size(&s
->type
, &align
);
3634 loc
= (loc
- size
) & -align
;
3636 ret
.r
= VT_LOCAL
| VT_LVAL
;
3637 /* pass it as 'int' to avoid structure arg passing
3639 vseti(VT_LOCAL
, loc
);
3644 /* return in register */
3645 if (is_float(ret
.type
.t
)) {
3646 ret
.r
= reg_fret(ret
.type
.t
);
3648 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3657 gfunc_param_typed(s
, sa
);
3667 error("too few arguments to function");
3669 if (!nocode_wanted
) {
3670 gfunc_call(nb_args
);
3672 vtop
-= (nb_args
+ 1);
3675 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3683 static void uneq(void)
3689 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3690 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3691 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3706 ST_FUNC
void expr_prod(void)
3711 while (tok
== '*' || tok
== '/' || tok
== '%') {
3719 ST_FUNC
void expr_sum(void)
3724 while (tok
== '+' || tok
== '-') {
3732 static void expr_shift(void)
3737 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3745 static void expr_cmp(void)
3750 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3751 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3759 static void expr_cmpeq(void)
3764 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3772 static void expr_and(void)
3775 while (tok
== '&') {
3782 static void expr_xor(void)
3785 while (tok
== '^') {
3792 static void expr_or(void)
3795 while (tok
== '|') {
3802 /* XXX: fix this mess */
3803 static void expr_land_const(void)
3806 while (tok
== TOK_LAND
) {
3813 /* XXX: fix this mess */
3814 static void expr_lor_const(void)
3817 while (tok
== TOK_LOR
) {
3824 /* only used if non constant */
3825 static void expr_land(void)
3830 if (tok
== TOK_LAND
) {
3835 if (tok
!= TOK_LAND
) {
3845 static void expr_lor(void)
3850 if (tok
== TOK_LOR
) {
3855 if (tok
!= TOK_LOR
) {
3865 /* XXX: better constant handling */
3866 static void expr_eq(void)
3868 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3870 CType type
, type1
, type2
;
3877 boolean
.t
= VT_BOOL
;
3883 if (tok
!= ':' || !gnu_ext
) {
3898 if (vtop
!= vstack
) {
3899 /* needed to avoid having different registers saved in
3901 if (is_float(vtop
->type
.t
)) {
3903 #ifdef TCC_TARGET_X86_64
3904 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3914 if (tok
== ':' && gnu_ext
) {
3922 sv
= *vtop
; /* save value to handle it later */
3923 vtop
--; /* no vpop so that FP stack is not flushed */
3931 bt1
= t1
& VT_BTYPE
;
3933 bt2
= t2
& VT_BTYPE
;
3934 /* cast operands to correct type according to ISOC rules */
3935 if (is_float(bt1
) || is_float(bt2
)) {
3936 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3937 type
.t
= VT_LDOUBLE
;
3938 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3943 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3944 /* cast to biggest op */
3946 /* convert to unsigned if it does not fit in a long long */
3947 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3948 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3949 type
.t
|= VT_UNSIGNED
;
3950 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3951 /* XXX: test pointer compatibility */
3953 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3954 /* XXX: test function pointer compatibility */
3956 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3957 /* XXX: test structure compatibility */
3959 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3960 /* NOTE: as an extension, we accept void on only one side */
3963 /* integer operations */
3965 /* convert to unsigned if it does not fit in an integer */
3966 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3967 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3968 type
.t
|= VT_UNSIGNED
;
3971 /* now we convert second operand */
3973 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3976 if (is_float(type
.t
)) {
3978 #ifdef TCC_TARGET_X86_64
3979 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3983 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3984 /* for long longs, we use fixed registers to avoid having
3985 to handle a complicated move */
3990 /* this is horrible, but we must also convert first
3994 /* put again first value and cast it */
3997 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4007 ST_FUNC
void gexpr(void)
4018 /* parse an expression and return its type without any side effect. */
4019 static void expr_type(CType
*type
)
4021 int saved_nocode_wanted
;
4023 saved_nocode_wanted
= nocode_wanted
;
4028 nocode_wanted
= saved_nocode_wanted
;
4031 /* parse a unary expression and return its type without any side
4033 static void unary_type(CType
*type
)
4045 /* parse a constant expression and return value in vtop. */
4046 static void expr_const1(void)
4055 /* parse an integer constant and return its value. */
4056 ST_FUNC
int expr_const(void)
4060 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4061 expect("constant expression");
4067 /* return the label token if current token is a label, otherwise
4069 static int is_label(void)
4073 /* fast test first */
4074 if (tok
< TOK_UIDENT
)
4076 /* no need to save tokc because tok is an identifier */
4083 unget_tok(last_tok
);
4088 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4089 int case_reg
, int is_expr
)
4094 /* generate line number info */
4095 if (tcc_state
->do_debug
&&
4096 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4097 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4099 last_line_num
= file
->line_num
;
4103 /* default return value is (void) */
4105 vtop
->type
.t
= VT_VOID
;
4108 if (tok
== TOK_IF
) {
4115 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4117 if (c
== TOK_ELSE
) {
4121 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4122 gsym(d
); /* patch else jmp */
4125 } else if (tok
== TOK_WHILE
) {
4133 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4137 } else if (tok
== '{') {
4141 /* record local declaration stack position */
4143 llabel
= local_label_stack
;
4144 /* handle local labels declarations */
4145 if (tok
== TOK_LABEL
) {
4148 if (tok
< TOK_UIDENT
)
4149 expect("label identifier");
4150 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4160 while (tok
!= '}') {
4165 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4168 /* pop locally defined labels */
4169 label_pop(&local_label_stack
, llabel
);
4170 /* pop locally defined symbols */
4172 /* XXX: this solution makes only valgrind happy...
4173 triggered by gcc.c-torture/execute/20000917-1.c */
4175 switch(vtop
->type
.t
& VT_BTYPE
) {
4180 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4182 error("unsupported expression type");
4185 sym_pop(&local_stack
, s
);
4187 } else if (tok
== TOK_RETURN
) {
4191 gen_assign_cast(&func_vt
);
4192 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4194 /* if returning structure, must copy it to implicit
4195 first pointer arg location */
4198 size
= type_size(&func_vt
,&align
);
4201 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4205 loc
= (loc
- size
) & -4;
4208 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4211 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4213 vtop
->type
= int_type
;
4219 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4222 /* copy structure value to pointer */
4227 } else if (is_float(func_vt
.t
)) {
4228 gv(rc_fret(func_vt
.t
));
4232 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4235 rsym
= gjmp(rsym
); /* jmp */
4236 } else if (tok
== TOK_BREAK
) {
4239 error("cannot break");
4240 *bsym
= gjmp(*bsym
);
4243 } else if (tok
== TOK_CONTINUE
) {
4246 error("cannot continue");
4247 *csym
= gjmp(*csym
);
4250 } else if (tok
== TOK_FOR
) {
4277 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4282 if (tok
== TOK_DO
) {
4287 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4298 if (tok
== TOK_SWITCH
) {
4302 /* XXX: other types than integer */
4303 case_reg
= gv(RC_INT
);
4307 b
= gjmp(0); /* jump to first case */
4309 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4310 /* if no default, jmp after switch */
4318 if (tok
== TOK_CASE
) {
4325 if (gnu_ext
&& tok
== TOK_DOTS
) {
4329 warning("empty case range");
4331 /* since a case is like a label, we must skip it with a jmp */
4338 *case_sym
= gtst(1, 0);
4341 *case_sym
= gtst(1, 0);
4345 *case_sym
= gtst(1, *case_sym
);
4350 goto block_after_label
;
4352 if (tok
== TOK_DEFAULT
) {
4358 error("too many 'default'");
4361 goto block_after_label
;
4363 if (tok
== TOK_GOTO
) {
4365 if (tok
== '*' && gnu_ext
) {
4369 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4372 } else if (tok
>= TOK_UIDENT
) {
4373 s
= label_find(tok
);
4374 /* put forward definition if needed */
4376 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4378 if (s
->r
== LABEL_DECLARED
)
4379 s
->r
= LABEL_FORWARD
;
4381 /* label already defined */
4382 if (s
->r
& LABEL_FORWARD
)
4383 s
->jnext
= gjmp(s
->jnext
);
4385 gjmp_addr(s
->jnext
);
4388 expect("label identifier");
4391 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4399 if (s
->r
== LABEL_DEFINED
)
4400 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4402 s
->r
= LABEL_DEFINED
;
4404 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4407 /* we accept this, but it is a mistake */
4410 warning("deprecated use of label at end of compound statement");
4414 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4417 /* expression case */
4432 /* t is the array or struct type. c is the array or struct
4433 address. cur_index/cur_field is the pointer to the current
4434 value. 'size_only' is true if only size info is needed (only used
4436 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4437 int *cur_index
, Sym
**cur_field
,
4441 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4447 if (gnu_ext
&& (l
= is_label()) != 0)
4449 while (tok
== '[' || tok
== '.') {
4451 if (!(type
->t
& VT_ARRAY
))
4452 expect("array type");
4455 index
= expr_const();
4456 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4457 expect("invalid index");
4458 if (tok
== TOK_DOTS
&& gnu_ext
) {
4460 index_last
= expr_const();
4461 if (index_last
< 0 ||
4462 (s
->c
>= 0 && index_last
>= s
->c
) ||
4464 expect("invalid index");
4470 *cur_index
= index_last
;
4471 type
= pointed_type(type
);
4472 elem_size
= type_size(type
, &align
);
4473 c
+= index
* elem_size
;
4474 /* NOTE: we only support ranges for last designator */
4475 nb_elems
= index_last
- index
+ 1;
4476 if (nb_elems
!= 1) {
4485 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4486 expect("struct/union type");
4499 /* XXX: fix this mess by using explicit storage field */
4501 type1
.t
|= (type
->t
& ~VT_TYPE
);
4515 if (type
->t
& VT_ARRAY
) {
4517 type
= pointed_type(type
);
4518 c
+= index
* type_size(type
, &align
);
4522 error("too many field init");
4523 /* XXX: fix this mess by using explicit storage field */
4525 type1
.t
|= (type
->t
& ~VT_TYPE
);
4530 decl_initializer(type
, sec
, c
, 0, size_only
);
4532 /* XXX: make it more general */
4533 if (!size_only
&& nb_elems
> 1) {
4534 unsigned long c_end
;
4539 error("range init not supported yet for dynamic storage");
4540 c_end
= c
+ nb_elems
* elem_size
;
4541 if (c_end
> sec
->data_allocated
)
4542 section_realloc(sec
, c_end
);
4543 src
= sec
->data
+ c
;
4545 for(i
= 1; i
< nb_elems
; i
++) {
4547 memcpy(dst
, src
, elem_size
);
4553 #define EXPR_CONST 1
4556 /* store a value or an expression directly in global data or in local array */
4557 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4558 int v
, int expr_type
)
4560 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4562 unsigned long long bit_mask
;
4570 /* compound literals must be allocated globally in this case */
4571 saved_global_expr
= global_expr
;
4574 global_expr
= saved_global_expr
;
4575 /* NOTE: symbols are accepted */
4576 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4577 error("initializer element is not constant");
4585 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4588 /* XXX: not portable */
4589 /* XXX: generate error if incorrect relocation */
4590 gen_assign_cast(&dtype
);
4591 bt
= type
->t
& VT_BTYPE
;
4592 /* we'll write at most 12 bytes */
4593 if (c
+ 12 > sec
->data_allocated
) {
4594 section_realloc(sec
, c
+ 12);
4596 ptr
= sec
->data
+ c
;
4597 /* XXX: make code faster ? */
4598 if (!(type
->t
& VT_BITFIELD
)) {
4603 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4604 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4605 bit_mask
= (1LL << bit_size
) - 1;
4607 if ((vtop
->r
& VT_SYM
) &&
4613 (bt
== VT_INT
&& bit_size
!= 32)))
4614 error("initializer element is not computable at load time");
4617 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4619 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4622 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4625 *(double *)ptr
= vtop
->c
.d
;
4628 *(long double *)ptr
= vtop
->c
.ld
;
4631 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4634 if (vtop
->r
& VT_SYM
) {
4635 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4637 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4642 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4649 /* put zeros for variable based init */
4650 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4653 /* nothing to do because globals are already set to zero */
4655 vpush_global_sym(&func_old_type
, TOK_memset
);
4663 /* 't' contains the type and storage info. 'c' is the offset of the
4664 object in section 'sec'. If 'sec' is NULL, it means stack based
4665 allocation. 'first' is true if array '{' must be read (multi
4666 dimension implicit array init handling). 'size_only' is true if
4667 size only evaluation is wanted (only for arrays). */
4668 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4669 int first
, int size_only
)
4671 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4672 int size1
, align1
, expr_type
;
4676 if (type
->t
& VT_ARRAY
) {
4680 t1
= pointed_type(type
);
4681 size1
= type_size(t1
, &align1
);
4684 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4690 /* only parse strings here if correct type (otherwise: handle
4691 them as ((w)char *) expressions */
4692 if ((tok
== TOK_LSTR
&&
4693 #ifdef TCC_TARGET_PE
4694 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4696 (t1
->t
& VT_BTYPE
) == VT_INT
4698 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4699 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4704 /* compute maximum number of chars wanted */
4706 cstr_len
= cstr
->size
;
4708 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4711 if (n
>= 0 && nb
> (n
- array_length
))
4712 nb
= n
- array_length
;
4715 warning("initializer-string for array is too long");
4716 /* in order to go faster for common case (char
4717 string in global variable, we handle it
4719 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4720 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4724 ch
= ((unsigned char *)cstr
->data
)[i
];
4726 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4727 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4735 /* only add trailing zero if enough storage (no
4736 warning in this case since it is standard) */
4737 if (n
< 0 || array_length
< n
) {
4739 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4745 while (tok
!= '}') {
4746 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4747 if (n
>= 0 && index
>= n
)
4748 error("index too large");
4749 /* must put zero in holes (note that doing it that way
4750 ensures that it even works with designators) */
4751 if (!size_only
&& array_length
< index
) {
4752 init_putz(t1
, sec
, c
+ array_length
* size1
,
4753 (index
- array_length
) * size1
);
4756 if (index
> array_length
)
4757 array_length
= index
;
4758 /* special test for multi dimensional arrays (may not
4759 be strictly correct if designators are used at the
4761 if (index
>= n
&& no_oblock
)
4770 /* put zeros at the end */
4771 if (!size_only
&& n
>= 0 && array_length
< n
) {
4772 init_putz(t1
, sec
, c
+ array_length
* size1
,
4773 (n
- array_length
) * size1
);
4775 /* patch type size if needed */
4777 s
->c
= array_length
;
4778 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4779 (sec
|| !first
|| tok
== '{')) {
4782 /* NOTE: the previous test is a specific case for automatic
4783 struct/union init */
4784 /* XXX: union needs only one init */
4786 /* XXX: this test is incorrect for local initializers
4787 beginning with ( without {. It would be much more difficult
4788 to do it correctly (ideally, the expression parser should
4789 be used in all cases) */
4795 while (tok
== '(') {
4799 if (!parse_btype(&type1
, &ad1
))
4801 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4803 if (!is_assignable_types(type
, &type1
))
4804 error("invalid type for cast");
4809 if (first
|| tok
== '{') {
4818 while (tok
!= '}') {
4819 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4821 if (!size_only
&& array_length
< index
) {
4822 init_putz(type
, sec
, c
+ array_length
,
4823 index
- array_length
);
4825 index
= index
+ type_size(&f
->type
, &align1
);
4826 if (index
> array_length
)
4827 array_length
= index
;
4829 /* gr: skip fields from same union - ugly. */
4831 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4832 /* test for same offset */
4833 if (f
->next
->c
!= f
->c
)
4835 /* if yes, test for bitfield shift */
4836 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4837 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4838 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4839 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4840 if (bit_pos_1
!= bit_pos_2
)
4847 if (no_oblock
&& f
== NULL
)
4853 /* put zeros at the end */
4854 if (!size_only
&& array_length
< n
) {
4855 init_putz(type
, sec
, c
+ array_length
,
4864 } else if (tok
== '{') {
4866 decl_initializer(type
, sec
, c
, first
, size_only
);
4868 } else if (size_only
) {
4869 /* just skip expression */
4871 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4875 else if (tok
== ')')
4880 /* currently, we always use constant expression for globals
4881 (may change for scripting case) */
4882 expr_type
= EXPR_CONST
;
4884 expr_type
= EXPR_ANY
;
4885 init_putv(type
, sec
, c
, 0, expr_type
);
4889 /* parse an initializer for type 't' if 'has_init' is non zero, and
4890 allocate space in local or global data space ('r' is either
4891 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4892 variable 'v' of scope 'scope' is declared before initializers are
4893 parsed. If 'v' is zero, then a reference to the new object is put
4894 in the value stack. If 'has_init' is 2, a special parsing is done
4895 to handle string constants. */
4896 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4897 int has_init
, int v
, int scope
)
4899 int size
, align
, addr
, data_offset
;
4901 ParseState saved_parse_state
= {0};
4902 TokenString init_str
;
4905 size
= type_size(type
, &align
);
4906 /* If unknown size, we must evaluate it before
4907 evaluating initializers because
4908 initializers can generate global data too
4909 (e.g. string pointers or ISOC99 compound
4910 literals). It also simplifies local
4911 initializers handling */
4912 tok_str_new(&init_str
);
4915 error("unknown type size");
4916 /* get all init string */
4917 if (has_init
== 2) {
4918 /* only get strings */
4919 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4920 tok_str_add_tok(&init_str
);
4925 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4927 error("unexpected end of file in initializer");
4928 tok_str_add_tok(&init_str
);
4931 else if (tok
== '}') {
4941 tok_str_add(&init_str
, -1);
4942 tok_str_add(&init_str
, 0);
4945 save_parse_state(&saved_parse_state
);
4947 macro_ptr
= init_str
.str
;
4949 decl_initializer(type
, NULL
, 0, 1, 1);
4950 /* prepare second initializer parsing */
4951 macro_ptr
= init_str
.str
;
4954 /* if still unknown size, error */
4955 size
= type_size(type
, &align
);
4957 error("unknown type size");
4959 /* take into account specified alignment if bigger */
4961 if (ad
->aligned
> align
)
4962 align
= ad
->aligned
;
4963 } else if (ad
->packed
) {
4966 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4968 #ifdef CONFIG_TCC_BCHECK
4969 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4972 loc
= (loc
- size
) & -align
;
4974 #ifdef CONFIG_TCC_BCHECK
4975 /* handles bounds */
4976 /* XXX: currently, since we do only one pass, we cannot track
4977 '&' operators, so we add only arrays */
4978 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4979 unsigned long *bounds_ptr
;
4980 /* add padding between regions */
4982 /* then add local bound info */
4983 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4984 bounds_ptr
[0] = addr
;
4985 bounds_ptr
[1] = size
;
4989 /* local variable */
4990 sym_push(v
, type
, r
, addr
);
4992 /* push local reference */
4993 vset(type
, r
, addr
);
4999 if (v
&& scope
== VT_CONST
) {
5000 /* see if the symbol was already defined */
5003 if (!is_compatible_types(&sym
->type
, type
))
5004 error("incompatible types for redefinition of '%s'",
5005 get_tok_str(v
, NULL
));
5006 if (sym
->type
.t
& VT_EXTERN
) {
5007 /* if the variable is extern, it was not allocated */
5008 sym
->type
.t
&= ~VT_EXTERN
;
5009 /* set array size if it was ommited in extern
5011 if ((sym
->type
.t
& VT_ARRAY
) &&
5012 sym
->type
.ref
->c
< 0 &&
5014 sym
->type
.ref
->c
= type
->ref
->c
;
5016 /* we accept several definitions of the same
5017 global variable. this is tricky, because we
5018 must play with the SHN_COMMON type of the symbol */
5019 /* XXX: should check if the variable was already
5020 initialized. It is incorrect to initialized it
5022 /* no init data, we won't add more to the symbol */
5029 /* allocate symbol in corresponding section */
5034 else if (tcc_state
->nocommon
)
5038 data_offset
= sec
->data_offset
;
5039 data_offset
= (data_offset
+ align
- 1) & -align
;
5041 /* very important to increment global pointer at this time
5042 because initializers themselves can create new initializers */
5043 data_offset
+= size
;
5044 #ifdef CONFIG_TCC_BCHECK
5045 /* add padding if bound check */
5046 if (tcc_state
->do_bounds_check
)
5049 sec
->data_offset
= data_offset
;
5050 /* allocate section space to put the data */
5051 if (sec
->sh_type
!= SHT_NOBITS
&&
5052 data_offset
> sec
->data_allocated
)
5053 section_realloc(sec
, data_offset
);
5054 /* align section if needed */
5055 if (align
> sec
->sh_addralign
)
5056 sec
->sh_addralign
= align
;
5058 addr
= 0; /* avoid warning */
5062 if (scope
!= VT_CONST
|| !sym
) {
5063 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5065 /* update symbol definition */
5067 put_extern_sym(sym
, sec
, addr
, size
);
5070 /* put a common area */
5071 put_extern_sym(sym
, NULL
, align
, size
);
5072 /* XXX: find a nicer way */
5073 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5074 esym
->st_shndx
= SHN_COMMON
;
5079 /* push global reference */
5080 sym
= get_sym_ref(type
, sec
, addr
, size
);
5082 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5085 #ifdef CONFIG_TCC_BCHECK
5086 /* handles bounds now because the symbol must be defined
5087 before for the relocation */
5088 if (tcc_state
->do_bounds_check
) {
5089 unsigned long *bounds_ptr
;
5091 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5092 /* then add global bound info */
5093 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5094 bounds_ptr
[0] = 0; /* relocated */
5095 bounds_ptr
[1] = size
;
5100 decl_initializer(type
, sec
, addr
, 1, 0);
5101 /* restore parse state if needed */
5103 tok_str_free(init_str
.str
);
5104 restore_parse_state(&saved_parse_state
);
5110 static void put_func_debug(Sym
*sym
)
5115 /* XXX: we put here a dummy type */
5116 snprintf(buf
, sizeof(buf
), "%s:%c1",
5117 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5118 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5119 cur_text_section
, sym
->c
);
5120 /* //gr gdb wants a line at the function */
5121 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5126 /* parse an old style function declaration list */
5127 /* XXX: check multiple parameter */
5128 static void func_decl_list(Sym
*func_sym
)
5135 /* parse each declaration */
5136 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
5137 if (!parse_btype(&btype
, &ad
))
5138 expect("declaration list");
5139 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5140 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5142 /* we accept no variable after */
5146 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5147 /* find parameter in function parameter list */
5150 if ((s
->v
& ~SYM_FIELD
) == v
)
5154 error("declaration for parameter '%s' but no such parameter",
5155 get_tok_str(v
, NULL
));
5157 /* check that no storage specifier except 'register' was given */
5158 if (type
.t
& VT_STORAGE
)
5159 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5160 convert_parameter_type(&type
);
5161 /* we can add the type (NOTE: it could be local to the function) */
5163 /* accept other parameters */
5174 /* parse a function defined by symbol 'sym' and generate its code in
5175 'cur_text_section' */
5176 static void gen_function(Sym
*sym
)
5178 int saved_nocode_wanted
= nocode_wanted
;
5180 ind
= cur_text_section
->data_offset
;
5181 /* NOTE: we patch the symbol size later */
5182 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5183 funcname
= get_tok_str(sym
->v
, NULL
);
5185 /* put debug symbol */
5186 if (tcc_state
->do_debug
)
5187 put_func_debug(sym
);
5188 /* push a dummy symbol to enable local sym storage */
5189 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5190 gfunc_prolog(&sym
->type
);
5192 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5195 cur_text_section
->data_offset
= ind
;
5196 label_pop(&global_label_stack
, NULL
);
5197 sym_pop(&local_stack
, NULL
); /* reset local stack */
5198 /* end of function */
5199 /* patch symbol size */
5200 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5202 if (tcc_state
->do_debug
) {
5203 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5205 /* It's better to crash than to generate wrong code */
5206 cur_text_section
= NULL
;
5207 funcname
= ""; /* for safety */
5208 func_vt
.t
= VT_VOID
; /* for safety */
5209 ind
= 0; /* for safety */
5210 nocode_wanted
= saved_nocode_wanted
;
5213 ST_FUNC
void gen_inline_functions(void)
5216 int *str
, inline_generated
, i
;
5217 struct InlineFunc
*fn
;
5219 /* iterate while inline function are referenced */
5221 inline_generated
= 0;
5222 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5223 fn
= tcc_state
->inline_fns
[i
];
5225 if (sym
&& sym
->c
) {
5226 /* the function was used: generate its code and
5227 convert it to a normal function */
5228 str
= fn
->token_str
;
5231 strcpy(file
->filename
, fn
->filename
);
5232 sym
->r
= VT_SYM
| VT_CONST
;
5233 sym
->type
.t
&= ~VT_INLINE
;
5237 cur_text_section
= text_section
;
5239 macro_ptr
= NULL
; /* fail safe */
5241 inline_generated
= 1;
5244 if (!inline_generated
)
5247 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5248 fn
= tcc_state
->inline_fns
[i
];
5249 str
= fn
->token_str
;
5252 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5255 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5256 ST_FUNC
void decl(int l
)
5264 if (!parse_btype(&btype
, &ad
)) {
5265 /* skip redundant ';' */
5266 /* XXX: find more elegant solution */
5271 if (l
== VT_CONST
&&
5272 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5273 /* global asm block */
5277 /* special test for old K&R protos without explicit int
5278 type. Only accepted when defining global data */
5279 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5283 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5284 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5286 /* we accept no variable after */
5290 while (1) { /* iterate thru each declaration */
5292 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5296 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5297 printf("type = '%s'\n", buf
);
5300 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5301 /* if old style function prototype, we accept a
5304 if (sym
->c
== FUNC_OLD
)
5305 func_decl_list(sym
);
5308 #ifdef TCC_TARGET_PE
5310 type
.t
|= VT_IMPORT
;
5312 type
.t
|= VT_EXPORT
;
5316 error("cannot use local functions");
5317 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5318 expect("function definition");
5320 /* reject abstract declarators in function definition */
5322 while ((sym
= sym
->next
) != NULL
)
5323 if (!(sym
->v
& ~SYM_FIELD
))
5324 expect("identifier");
5326 /* XXX: cannot do better now: convert extern line to static inline */
5327 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5328 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5332 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5335 r
= sym
->type
.ref
->r
;
5336 /* use func_call from prototype if not defined */
5337 if (FUNC_CALL(r
) != FUNC_CDECL
5338 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5339 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5341 /* use export from prototype */
5343 FUNC_EXPORT(type
.ref
->r
) = 1;
5345 /* use static from prototype */
5346 if (sym
->type
.t
& VT_STATIC
)
5347 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5349 if (!is_compatible_types(&sym
->type
, &type
)) {
5351 error("incompatible types for redefinition of '%s'",
5352 get_tok_str(v
, NULL
));
5354 /* if symbol is already defined, then put complete type */
5357 /* put function symbol */
5358 sym
= global_identifier_push(v
, type
.t
, 0);
5359 sym
->type
.ref
= type
.ref
;
5362 /* static inline functions are just recorded as a kind
5363 of macro. Their code will be emitted at the end of
5364 the compilation unit only if they are used */
5365 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5366 (VT_INLINE
| VT_STATIC
)) {
5367 TokenString func_str
;
5369 struct InlineFunc
*fn
;
5370 const char *filename
;
5372 tok_str_new(&func_str
);
5378 error("unexpected end of file");
5379 tok_str_add_tok(&func_str
);
5384 } else if (t
== '}') {
5386 if (block_level
== 0)
5390 tok_str_add(&func_str
, -1);
5391 tok_str_add(&func_str
, 0);
5392 filename
= file
? file
->filename
: "";
5393 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5394 strcpy(fn
->filename
, filename
);
5396 fn
->token_str
= func_str
.str
;
5397 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5400 /* compute text section */
5401 cur_text_section
= ad
.section
;
5402 if (!cur_text_section
)
5403 cur_text_section
= text_section
;
5404 sym
->r
= VT_SYM
| VT_CONST
;
5409 if (btype
.t
& VT_TYPEDEF
) {
5410 /* save typedefed type */
5411 /* XXX: test storage specifiers ? */
5412 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5413 sym
->type
.t
|= VT_TYPEDEF
;
5414 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5415 /* external function definition */
5416 /* specific case for func_call attribute */
5417 type
.ref
->r
= INT_ATTR(&ad
);
5418 external_sym(v
, &type
, 0);
5420 /* not lvalue if array */
5422 if (!(type
.t
& VT_ARRAY
))
5423 r
|= lvalue_type(type
.t
);
5424 has_init
= (tok
== '=');
5425 if ((btype
.t
& VT_EXTERN
) ||
5426 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5427 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5428 /* external variable */
5429 /* NOTE: as GCC, uninitialized global static
5430 arrays of null size are considered as
5432 external_sym(v
, &type
, r
);
5434 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5435 if (type
.t
& VT_STATIC
)
5441 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);