2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
57 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
59 ST_DATA
int const_wanted
; /* true if constant wanted */
60 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
61 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
64 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
65 ST_DATA
char *funcname
;
67 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType
*type
);
71 static inline CType
*pointed_type(CType
*type
);
72 static int is_compatible_types(CType
*type1
, CType
*type2
);
73 static int parse_btype(CType
*type
, AttributeDef
*ad
);
74 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
75 static void parse_expr_type(CType
*type
);
76 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
77 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
78 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
79 static void expr_eq(void);
80 static void unary_type(CType
*type
);
81 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
82 static void expr_type(CType
*type
);
84 ST_INLN
int is_float(int t
)
88 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
91 ST_FUNC
void test_lvalue(void)
93 if (!(vtop
->r
& VT_LVAL
))
97 /* ------------------------------------------------------------------------- */
98 /* symbol allocator */
99 static Sym
*__sym_malloc(void)
101 Sym
*sym_pool
, *sym
, *last_sym
;
104 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
105 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
107 last_sym
= sym_free_first
;
109 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
110 sym
->next
= last_sym
;
114 sym_free_first
= last_sym
;
118 static inline Sym
*sym_malloc(void)
121 sym
= sym_free_first
;
123 sym
= __sym_malloc();
124 sym_free_first
= sym
->next
;
128 ST_INLN
void sym_free(Sym
*sym
)
130 sym
->next
= sym_free_first
;
131 sym_free_first
= sym
;
134 /* push, without hashing */
135 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
153 /* find a symbol and return its associated structure. 's' is the top
154 of the symbol stack */
155 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
165 /* structure lookup */
166 ST_INLN Sym
*struct_find(int v
)
169 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
171 return table_ident
[v
]->sym_struct
;
174 /* find an identifier */
175 ST_INLN Sym
*sym_find(int v
)
178 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
180 return table_ident
[v
]->sym_identifier
;
183 /* push a given symbol on the symbol stack */
184 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
193 s
= sym_push2(ps
, v
, type
->t
, c
);
194 s
->type
.ref
= type
->ref
;
196 /* don't record fields or anonymous symbols */
198 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
199 /* record symbol in token array */
200 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
202 ps
= &ts
->sym_struct
;
204 ps
= &ts
->sym_identifier
;
211 /* push a global identifier */
212 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
215 s
= sym_push2(&global_stack
, v
, t
, c
);
216 /* don't record anonymous symbol */
217 if (v
< SYM_FIRST_ANOM
) {
218 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
219 /* modify the top most local identifier, so that
220 sym_identifier will point to 's' when popped */
222 ps
= &(*ps
)->prev_tok
;
229 /* pop symbols until top reaches 'b' */
230 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
240 /* remove symbol in token array */
242 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
243 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
245 ps
= &ts
->sym_struct
;
247 ps
= &ts
->sym_identifier
;
256 /* ------------------------------------------------------------------------- */
258 ST_FUNC
void swap(int *p
, int *q
)
266 static void vsetc(CType
*type
, int r
, CValue
*vc
)
270 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
271 error("memory full");
272 /* cannot let cpu flags if other instruction are generated. Also
273 avoid leaving VT_JMP anywhere except on the top of the stack
274 because it would complicate the code generator. */
275 if (vtop
>= vstack
) {
276 v
= vtop
->r
& VT_VALMASK
;
277 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
287 /* push constant of type "type" with useless value */
288 void vpush(CType
*type
)
291 vsetc(type
, VT_CONST
, &cval
);
294 /* push integer constant */
295 ST_FUNC
void vpushi(int v
)
299 vsetc(&int_type
, VT_CONST
, &cval
);
302 /* push long long constant */
303 static void vpushll(long long v
)
310 vsetc(&ctype
, VT_CONST
, &cval
);
313 /* Return a static symbol pointing to a section */
314 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
320 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
321 sym
->type
.ref
= type
->ref
;
322 sym
->r
= VT_CONST
| VT_SYM
;
323 put_extern_sym(sym
, sec
, offset
, size
);
327 /* push a reference to a section offset by adding a dummy symbol */
328 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
333 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
334 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
337 /* define a new external reference to a symbol 'v' of type 'u' */
338 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
344 /* push forward reference */
345 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
346 s
->type
.ref
= type
->ref
;
347 s
->r
= r
| VT_CONST
| VT_SYM
;
352 /* define a new external reference to a symbol 'v' of type 'u' */
353 static Sym
*external_sym(int v
, CType
*type
, int r
)
359 /* push forward reference */
360 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
361 s
->type
.t
|= VT_EXTERN
;
362 } else if (s
->type
.ref
== func_old_type
.ref
) {
363 s
->type
.ref
= type
->ref
;
364 s
->r
= r
| VT_CONST
| VT_SYM
;
365 s
->type
.t
|= VT_EXTERN
;
366 } else if (!is_compatible_types(&s
->type
, type
)) {
367 error("incompatible types for redefinition of '%s'",
368 get_tok_str(v
, NULL
));
373 /* push a reference to global symbol v */
374 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
379 sym
= external_global_sym(v
, type
, 0);
381 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
385 ST_FUNC
void vset(CType
*type
, int r
, int v
)
390 vsetc(type
, r
, &cval
);
393 static void vseti(int r
, int v
)
401 ST_FUNC
void vswap(void)
410 ST_FUNC
void vpushv(SValue
*v
)
412 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
413 error("memory full");
418 static void vdup(void)
423 /* save r to the memory stack, and mark it as being free */
424 ST_FUNC
void save_reg(int r
)
426 int l
, saved
, size
, align
;
430 /* modify all stack values */
433 for(p
=vstack
;p
<=vtop
;p
++) {
434 if ((p
->r
& VT_VALMASK
) == r
||
435 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
436 /* must save value on stack if not already done */
438 /* NOTE: must reload 'r' because r might be equal to r2 */
439 r
= p
->r
& VT_VALMASK
;
440 /* store register in the stack */
442 if ((p
->r
& VT_LVAL
) ||
443 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
444 #ifdef TCC_TARGET_X86_64
445 type
= &char_pointer_type
;
449 size
= type_size(type
, &align
);
450 loc
= (loc
- size
) & -align
;
452 sv
.r
= VT_LOCAL
| VT_LVAL
;
455 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
456 /* x86 specific: need to pop fp register ST0 if saved */
458 o(0xd8dd); /* fstp %st(0) */
461 #ifndef TCC_TARGET_X86_64
462 /* special long long case */
463 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
471 /* mark that stack entry as being saved on the stack */
472 if (p
->r
& VT_LVAL
) {
473 /* also clear the bounded flag because the
474 relocation address of the function was stored in
476 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
478 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
486 #ifdef TCC_TARGET_ARM
487 /* find a register of class 'rc2' with at most one reference on stack.
488 * If none, call get_reg(rc) */
489 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
494 for(r
=0;r
<NB_REGS
;r
++) {
495 if (reg_classes
[r
] & rc2
) {
498 for(p
= vstack
; p
<= vtop
; p
++) {
499 if ((p
->r
& VT_VALMASK
) == r
||
500 (p
->r2
& VT_VALMASK
) == r
)
511 /* find a free register of class 'rc'. If none, save one register */
512 ST_FUNC
int get_reg(int rc
)
517 /* find a free register */
518 for(r
=0;r
<NB_REGS
;r
++) {
519 if (reg_classes
[r
] & rc
) {
520 for(p
=vstack
;p
<=vtop
;p
++) {
521 if ((p
->r
& VT_VALMASK
) == r
||
522 (p
->r2
& VT_VALMASK
) == r
)
530 /* no register left : free the first one on the stack (VERY
531 IMPORTANT to start from the bottom to ensure that we don't
532 spill registers used in gen_opi()) */
533 for(p
=vstack
;p
<=vtop
;p
++) {
534 r
= p
->r
& VT_VALMASK
;
535 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
537 /* also look at second register (if long long) */
538 r
= p
->r2
& VT_VALMASK
;
539 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
545 /* Should never comes here */
549 /* save registers up to (vtop - n) stack entry */
550 ST_FUNC
void save_regs(int n
)
555 for(p
= vstack
;p
<= p1
; p
++) {
556 r
= p
->r
& VT_VALMASK
;
563 /* move register 's' to 'r', and flush previous value of r to memory
565 static void move_reg(int r
, int s
)
578 /* get address of vtop (vtop MUST BE an lvalue) */
579 static void gaddrof(void)
582 /* tricky: if saved lvalue, then we can go back to lvalue */
583 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
584 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
587 #ifdef CONFIG_TCC_BCHECK
588 /* generate lvalue bound code */
589 static void gbound(void)
594 vtop
->r
&= ~VT_MUSTBOUND
;
595 /* if lvalue, then use checking code before dereferencing */
596 if (vtop
->r
& VT_LVAL
) {
597 /* if not VT_BOUNDED value, then make one */
598 if (!(vtop
->r
& VT_BOUNDED
)) {
599 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
600 /* must save type because we must set it to int to get pointer */
602 vtop
->type
.t
= VT_INT
;
605 gen_bounded_ptr_add();
606 vtop
->r
|= lval_type
;
609 /* then check for dereferencing */
610 gen_bounded_ptr_deref();
615 /* store vtop a register belonging to class 'rc'. lvalues are
616 converted to values. Cannot be used if cannot be converted to
617 register value (such as structures). */
618 ST_FUNC
int gv(int rc
)
620 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
622 /* NOTE: get_reg can modify vstack[] */
623 if (vtop
->type
.t
& VT_BITFIELD
) {
626 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
627 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
628 /* remove bit field info to avoid loops */
629 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
630 /* cast to int to propagate signedness in following ops */
631 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
636 if((vtop
->type
.t
& VT_UNSIGNED
) ||
637 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
638 type
.t
|= VT_UNSIGNED
;
640 /* generate shifts */
641 vpushi(bits
- (bit_pos
+ bit_size
));
643 vpushi(bits
- bit_size
);
644 /* NOTE: transformed to SHR if unsigned */
648 if (is_float(vtop
->type
.t
) &&
649 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
652 unsigned long offset
;
653 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
657 /* XXX: unify with initializers handling ? */
658 /* CPUs usually cannot use float constants, so we store them
659 generically in data segment */
660 size
= type_size(&vtop
->type
, &align
);
661 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
662 data_section
->data_offset
= offset
;
663 /* XXX: not portable yet */
664 #if defined(__i386__) || defined(__x86_64__)
665 /* Zero pad x87 tenbyte long doubles */
666 if (size
== LDOUBLE_SIZE
)
667 vtop
->c
.tab
[2] &= 0xffff;
669 ptr
= section_ptr_add(data_section
, size
);
671 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
675 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
679 ptr
[i
] = vtop
->c
.tab
[i
];
680 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
681 vtop
->r
|= VT_LVAL
| VT_SYM
;
685 #ifdef CONFIG_TCC_BCHECK
686 if (vtop
->r
& VT_MUSTBOUND
)
690 r
= vtop
->r
& VT_VALMASK
;
694 /* need to reload if:
696 - lvalue (need to dereference pointer)
697 - already a register, but not in the right class */
699 || (vtop
->r
& VT_LVAL
)
700 || !(reg_classes
[r
] & rc
)
701 #ifndef TCC_TARGET_X86_64
702 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
707 #ifndef TCC_TARGET_X86_64
708 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
710 unsigned long long ll
;
711 /* two register type load : expand to two words
713 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
716 vtop
->c
.ui
= ll
; /* first word */
718 vtop
->r
= r
; /* save register value */
719 vpushi(ll
>> 32); /* second word */
720 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
721 (vtop
->r
& VT_LVAL
)) {
722 /* We do not want to modifier the long long
723 pointer here, so the safest (and less
724 efficient) is to save all the other registers
725 in the stack. XXX: totally inefficient. */
727 /* load from memory */
730 vtop
[-1].r
= r
; /* save register value */
731 /* increment pointer to get second word */
732 vtop
->type
.t
= VT_INT
;
741 vtop
[-1].r
= r
; /* save register value */
742 vtop
->r
= vtop
[-1].r2
;
744 /* allocate second register */
748 /* write second register */
752 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
754 /* lvalue of scalar type : need to use lvalue type
755 because of possible cast */
758 /* compute memory access type */
759 if (vtop
->r
& VT_LVAL_BYTE
)
761 else if (vtop
->r
& VT_LVAL_SHORT
)
763 if (vtop
->r
& VT_LVAL_UNSIGNED
)
767 /* restore wanted type */
770 /* one register type load */
775 #ifdef TCC_TARGET_C67
776 /* uses register pairs for doubles */
777 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
784 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
785 ST_FUNC
void gv2(int rc1
, int rc2
)
789 /* generate more generic register first. But VT_JMP or VT_CMP
790 values must be generated first in all cases to avoid possible
792 v
= vtop
[0].r
& VT_VALMASK
;
793 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
798 /* test if reload is needed for first register */
799 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
809 /* test if reload is needed for first register */
810 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
816 /* wrapper around RC_FRET to return a register by type */
817 static int rc_fret(int t
)
819 #ifdef TCC_TARGET_X86_64
820 if (t
== VT_LDOUBLE
) {
827 /* wrapper around REG_FRET to return a register by type */
828 static int reg_fret(int t
)
830 #ifdef TCC_TARGET_X86_64
831 if (t
== VT_LDOUBLE
) {
838 /* expand long long on stack in two int registers */
839 static void lexpand(void)
843 u
= vtop
->type
.t
& VT_UNSIGNED
;
846 vtop
[0].r
= vtop
[-1].r2
;
847 vtop
[0].r2
= VT_CONST
;
848 vtop
[-1].r2
= VT_CONST
;
849 vtop
[0].type
.t
= VT_INT
| u
;
850 vtop
[-1].type
.t
= VT_INT
| u
;
853 #ifdef TCC_TARGET_ARM
854 /* expand long long on stack */
855 ST_FUNC
void lexpand_nr(void)
859 u
= vtop
->type
.t
& VT_UNSIGNED
;
862 vtop
->type
.t
= VT_INT
| u
;
863 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
865 vtop
[-1].c
.ui
= vtop
->c
.ull
;
866 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
868 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
870 vtop
->r
= vtop
[-1].r
;
871 } else if (v
> VT_CONST
) {
875 vtop
->r
= vtop
[-1].r2
;
876 vtop
[-1].r2
= VT_CONST
;
877 vtop
[-1].type
.t
= VT_INT
| u
;
881 /* build a long long from two ints */
882 static void lbuild(int t
)
885 vtop
[-1].r2
= vtop
[0].r
;
890 /* rotate n first stack elements to the bottom
891 I1 ... In -> I2 ... In I1 [top is right]
893 static void vrotb(int n
)
904 /* rotate n first stack elements to the top
905 I1 ... In -> In I1 ... I(n-1) [top is right]
907 ST_FUNC
void vrott(int n
)
913 for(i
= 0;i
< n
- 1; i
++)
914 vtop
[-i
] = vtop
[-i
- 1];
918 #ifdef TCC_TARGET_ARM
919 /* like vrott but in other direction
920 In ... I1 -> I(n-1) ... I1 In [top is right]
922 ST_FUNC
void vnrott(int n
)
928 for(i
= n
- 1; i
> 0; i
--)
929 vtop
[-i
] = vtop
[-i
+ 1];
934 /* pop stack value */
935 ST_FUNC
void vpop(void)
938 v
= vtop
->r
& VT_VALMASK
;
939 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
940 /* for x86, we need to pop the FP stack */
941 if (v
== TREG_ST0
&& !nocode_wanted
) {
942 o(0xd8dd); /* fstp %st(0) */
945 if (v
== VT_JMP
|| v
== VT_JMPI
) {
946 /* need to put correct jump if && or || without test */
952 /* convert stack entry to register and duplicate its value in another
954 static void gv_dup(void)
960 if ((t
& VT_BTYPE
) == VT_LLONG
) {
967 /* stack: H L L1 H1 */
975 /* duplicate value */
980 #ifdef TCC_TARGET_X86_64
981 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
991 load(r1
, &sv
); /* move r to r1 */
993 /* duplicates value */
999 #ifndef TCC_TARGET_X86_64
1000 /* generate CPU independent (unsigned) long long operations */
1001 static void gen_opl(int op
)
1003 int t
, a
, b
, op1
, c
, i
;
1005 unsigned short reg_iret
= REG_IRET
;
1006 unsigned short reg_lret
= REG_LRET
;
1012 func
= TOK___divdi3
;
1015 func
= TOK___udivdi3
;
1018 func
= TOK___moddi3
;
1021 func
= TOK___umoddi3
;
1028 /* call generic long long function */
1029 vpush_global_sym(&func_old_type
, func
);
1034 vtop
->r2
= reg_lret
;
1047 /* stack: L1 H1 L2 H2 */
1052 vtop
[-2] = vtop
[-3];
1055 /* stack: H1 H2 L1 L2 */
1061 /* stack: H1 H2 L1 L2 ML MH */
1064 /* stack: ML MH H1 H2 L1 L2 */
1068 /* stack: ML MH H1 L2 H2 L1 */
1073 /* stack: ML MH M1 M2 */
1076 } else if (op
== '+' || op
== '-') {
1077 /* XXX: add non carry method too (for MIPS or alpha) */
1083 /* stack: H1 H2 (L1 op L2) */
1086 gen_op(op1
+ 1); /* TOK_xxxC2 */
1089 /* stack: H1 H2 (L1 op L2) */
1092 /* stack: (L1 op L2) H1 H2 */
1094 /* stack: (L1 op L2) (H1 op H2) */
1102 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1103 t
= vtop
[-1].type
.t
;
1107 /* stack: L H shift */
1109 /* constant: simpler */
1110 /* NOTE: all comments are for SHL. the other cases are
1111 done by swaping words */
1122 if (op
!= TOK_SAR
) {
1155 /* XXX: should provide a faster fallback on x86 ? */
1158 func
= TOK___ashrdi3
;
1161 func
= TOK___lshrdi3
;
1164 func
= TOK___ashldi3
;
1170 /* compare operations */
1176 /* stack: L1 H1 L2 H2 */
1178 vtop
[-1] = vtop
[-2];
1180 /* stack: L1 L2 H1 H2 */
1183 /* when values are equal, we need to compare low words. since
1184 the jump is inverted, we invert the test too. */
1187 else if (op1
== TOK_GT
)
1189 else if (op1
== TOK_ULT
)
1191 else if (op1
== TOK_UGT
)
1196 if (op1
!= TOK_NE
) {
1200 /* generate non equal test */
1201 /* XXX: NOT PORTABLE yet */
1205 #if defined(TCC_TARGET_I386)
1206 b
= psym(0x850f, 0);
1207 #elif defined(TCC_TARGET_ARM)
1209 o(0x1A000000 | encbranch(ind
, 0, 1));
1210 #elif defined(TCC_TARGET_C67)
1211 error("not implemented");
1213 #error not supported
1217 /* compare low. Always unsigned */
1221 else if (op1
== TOK_LE
)
1223 else if (op1
== TOK_GT
)
1225 else if (op1
== TOK_GE
)
1236 /* handle integer constant optimizations and various machine
1238 static void gen_opic(int op
)
1240 int c1
, c2
, t1
, t2
, n
;
1243 typedef unsigned long long U
;
1247 t1
= v1
->type
.t
& VT_BTYPE
;
1248 t2
= v2
->type
.t
& VT_BTYPE
;
1252 else if (v1
->type
.t
& VT_UNSIGNED
)
1259 else if (v2
->type
.t
& VT_UNSIGNED
)
1264 /* currently, we cannot do computations with forward symbols */
1265 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1266 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1269 case '+': l1
+= l2
; break;
1270 case '-': l1
-= l2
; break;
1271 case '&': l1
&= l2
; break;
1272 case '^': l1
^= l2
; break;
1273 case '|': l1
|= l2
; break;
1274 case '*': l1
*= l2
; break;
1281 /* if division by zero, generate explicit division */
1284 error("division by zero in constant");
1288 default: l1
/= l2
; break;
1289 case '%': l1
%= l2
; break;
1290 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1291 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1294 case TOK_SHL
: l1
<<= l2
; break;
1295 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1296 case TOK_SAR
: l1
>>= l2
; break;
1298 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1299 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1300 case TOK_EQ
: l1
= l1
== l2
; break;
1301 case TOK_NE
: l1
= l1
!= l2
; break;
1302 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1303 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1304 case TOK_LT
: l1
= l1
< l2
; break;
1305 case TOK_GE
: l1
= l1
>= l2
; break;
1306 case TOK_LE
: l1
= l1
<= l2
; break;
1307 case TOK_GT
: l1
= l1
> l2
; break;
1309 case TOK_LAND
: l1
= l1
&& l2
; break;
1310 case TOK_LOR
: l1
= l1
|| l2
; break;
1317 /* if commutative ops, put c2 as constant */
1318 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1319 op
== '|' || op
== '*')) {
1321 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1322 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1324 /* Filter out NOP operations like x*1, x-0, x&-1... */
1325 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1328 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1329 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1335 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1336 /* try to use shifts instead of muls or divs */
1337 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1346 else if (op
== TOK_PDIV
)
1352 } else if (c2
&& (op
== '+' || op
== '-') &&
1353 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1354 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1355 /* symbol + constant case */
1362 if (!nocode_wanted
) {
1363 /* call low level op generator */
1364 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1375 /* generate a floating point operation with constant propagation */
1376 static void gen_opif(int op
)
1384 /* currently, we cannot do computations with forward symbols */
1385 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1386 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1388 if (v1
->type
.t
== VT_FLOAT
) {
1391 } else if (v1
->type
.t
== VT_DOUBLE
) {
1399 /* NOTE: we only do constant propagation if finite number (not
1400 NaN or infinity) (ANSI spec) */
1401 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1405 case '+': f1
+= f2
; break;
1406 case '-': f1
-= f2
; break;
1407 case '*': f1
*= f2
; break;
1411 error("division by zero in constant");
1416 /* XXX: also handles tests ? */
1420 /* XXX: overflow test ? */
1421 if (v1
->type
.t
== VT_FLOAT
) {
1423 } else if (v1
->type
.t
== VT_DOUBLE
) {
1431 if (!nocode_wanted
) {
1439 static int pointed_size(CType
*type
)
1442 return type_size(pointed_type(type
), &align
);
1445 static inline int is_null_pointer(SValue
*p
)
1447 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1449 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1450 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1453 static inline int is_integer_btype(int bt
)
1455 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1456 bt
== VT_INT
|| bt
== VT_LLONG
);
1459 /* check types for comparison or substraction of pointers */
1460 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1462 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1465 /* null pointers are accepted for all comparisons as gcc */
1466 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1470 bt1
= type1
->t
& VT_BTYPE
;
1471 bt2
= type2
->t
& VT_BTYPE
;
1472 /* accept comparison between pointer and integer with a warning */
1473 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1474 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1475 warning("comparison between pointer and integer");
1479 /* both must be pointers or implicit function pointers */
1480 if (bt1
== VT_PTR
) {
1481 type1
= pointed_type(type1
);
1482 } else if (bt1
!= VT_FUNC
)
1483 goto invalid_operands
;
1485 if (bt2
== VT_PTR
) {
1486 type2
= pointed_type(type2
);
1487 } else if (bt2
!= VT_FUNC
) {
1489 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1491 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1492 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1496 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1497 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1498 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1499 /* gcc-like error if '-' is used */
1501 goto invalid_operands
;
1503 warning("comparison of distinct pointer types lacks a cast");
1507 /* generic gen_op: handles types problems */
1508 ST_FUNC
void gen_op(int op
)
1510 int u
, t1
, t2
, bt1
, bt2
, t
;
1513 t1
= vtop
[-1].type
.t
;
1514 t2
= vtop
[0].type
.t
;
1515 bt1
= t1
& VT_BTYPE
;
1516 bt2
= t2
& VT_BTYPE
;
1518 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1519 /* at least one operand is a pointer */
1520 /* relationnal op: must be both pointers */
1521 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1522 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1523 /* pointers are handled are unsigned */
1524 #ifdef TCC_TARGET_X86_64
1525 t
= VT_LLONG
| VT_UNSIGNED
;
1527 t
= VT_INT
| VT_UNSIGNED
;
1531 /* if both pointers, then it must be the '-' op */
1532 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1534 error("cannot use pointers here");
1535 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1536 /* XXX: check that types are compatible */
1537 u
= pointed_size(&vtop
[-1].type
);
1539 /* set to integer type */
1540 #ifdef TCC_TARGET_X86_64
1541 vtop
->type
.t
= VT_LLONG
;
1543 vtop
->type
.t
= VT_INT
;
1548 /* exactly one pointer : must be '+' or '-'. */
1549 if (op
!= '-' && op
!= '+')
1550 error("cannot use pointers here");
1551 /* Put pointer as first operand */
1552 if (bt2
== VT_PTR
) {
1556 type1
= vtop
[-1].type
;
1557 type1
.t
&= ~VT_ARRAY
;
1558 u
= pointed_size(&vtop
[-1].type
);
1560 error("unknown array element size");
1561 #ifdef TCC_TARGET_X86_64
1564 /* XXX: cast to int ? (long long case) */
1568 #ifdef CONFIG_TCC_BCHECK
1569 /* if evaluating constant expression, no code should be
1570 generated, so no bound check */
1571 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1572 /* if bounded pointers, we generate a special code to
1579 gen_bounded_ptr_add();
1585 /* put again type if gen_opic() swaped operands */
1588 } else if (is_float(bt1
) || is_float(bt2
)) {
1589 /* compute bigger type and do implicit casts */
1590 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1592 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1597 /* floats can only be used for a few operations */
1598 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1599 (op
< TOK_ULT
|| op
> TOK_GT
))
1600 error("invalid operands for binary operation");
1602 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1603 /* cast to biggest op */
1605 /* convert to unsigned if it does not fit in a long long */
1606 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1607 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1611 /* integer operations */
1613 /* convert to unsigned if it does not fit in an integer */
1614 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1615 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1618 /* XXX: currently, some unsigned operations are explicit, so
1619 we modify them here */
1620 if (t
& VT_UNSIGNED
) {
1627 else if (op
== TOK_LT
)
1629 else if (op
== TOK_GT
)
1631 else if (op
== TOK_LE
)
1633 else if (op
== TOK_GE
)
1640 /* special case for shifts and long long: we keep the shift as
1642 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1649 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1650 /* relationnal op: the result is an int */
1651 vtop
->type
.t
= VT_INT
;
1658 #ifndef TCC_TARGET_ARM
1659 /* generic itof for unsigned long long case */
1660 static void gen_cvt_itof1(int t
)
1662 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1663 (VT_LLONG
| VT_UNSIGNED
)) {
1666 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1667 #if LDOUBLE_SIZE != 8
1668 else if (t
== VT_LDOUBLE
)
1669 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1672 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1676 vtop
->r
= reg_fret(t
);
1683 /* generic ftoi for unsigned long long case */
1684 static void gen_cvt_ftoi1(int t
)
1688 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1689 /* not handled natively */
1690 st
= vtop
->type
.t
& VT_BTYPE
;
1692 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1693 #if LDOUBLE_SIZE != 8
1694 else if (st
== VT_LDOUBLE
)
1695 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1698 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1703 vtop
->r2
= REG_LRET
;
1709 /* force char or short cast */
1710 static void force_charshort_cast(int t
)
1714 /* XXX: add optimization if lvalue : just change type and offset */
1719 if (t
& VT_UNSIGNED
) {
1720 vpushi((1 << bits
) - 1);
1726 /* result must be signed or the SAR is converted to an SHL
1727 This was not the case when "t" was a signed short
1728 and the last value on the stack was an unsigned int */
1729 vtop
->type
.t
&= ~VT_UNSIGNED
;
1735 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1736 static void gen_cast(CType
*type
)
1738 int sbt
, dbt
, sf
, df
, c
, p
;
1740 /* special delayed cast for char/short */
1741 /* XXX: in some cases (multiple cascaded casts), it may still
1743 if (vtop
->r
& VT_MUSTCAST
) {
1744 vtop
->r
&= ~VT_MUSTCAST
;
1745 force_charshort_cast(vtop
->type
.t
);
1748 /* bitfields first get cast to ints */
1749 if (vtop
->type
.t
& VT_BITFIELD
) {
1753 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1754 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1759 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1760 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1762 /* constant case: we can do it now */
1763 /* XXX: in ISOC, cannot do it if error in convert */
1764 if (sbt
== VT_FLOAT
)
1765 vtop
->c
.ld
= vtop
->c
.f
;
1766 else if (sbt
== VT_DOUBLE
)
1767 vtop
->c
.ld
= vtop
->c
.d
;
1770 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1771 if (sbt
& VT_UNSIGNED
)
1772 vtop
->c
.ld
= vtop
->c
.ull
;
1774 vtop
->c
.ld
= vtop
->c
.ll
;
1776 if (sbt
& VT_UNSIGNED
)
1777 vtop
->c
.ld
= vtop
->c
.ui
;
1779 vtop
->c
.ld
= vtop
->c
.i
;
1782 if (dbt
== VT_FLOAT
)
1783 vtop
->c
.f
= (float)vtop
->c
.ld
;
1784 else if (dbt
== VT_DOUBLE
)
1785 vtop
->c
.d
= (double)vtop
->c
.ld
;
1786 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1787 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1788 } else if (sf
&& dbt
== VT_BOOL
) {
1789 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1792 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1793 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1794 vtop
->c
.ll
= vtop
->c
.ull
;
1795 else if (sbt
& VT_UNSIGNED
)
1796 vtop
->c
.ll
= vtop
->c
.ui
;
1797 #ifdef TCC_TARGET_X86_64
1798 else if (sbt
== VT_PTR
)
1801 else if (sbt
!= VT_LLONG
)
1802 vtop
->c
.ll
= vtop
->c
.i
;
1804 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1805 vtop
->c
.ull
= vtop
->c
.ll
;
1806 else if (dbt
== VT_BOOL
)
1807 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1808 else if (dbt
!= VT_LLONG
) {
1810 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1812 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1815 if(dbt
& VT_UNSIGNED
)
1816 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1818 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1821 } else if (p
&& dbt
== VT_BOOL
) {
1824 } else if (!nocode_wanted
) {
1825 /* non constant case: generate code */
1827 /* convert from fp to fp */
1830 /* convert int to fp */
1833 /* convert fp to int */
1834 if (dbt
== VT_BOOL
) {
1838 /* we handle char/short/etc... with generic code */
1839 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1840 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1844 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1845 /* additional cast for char/short... */
1850 #ifndef TCC_TARGET_X86_64
1851 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1852 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1853 /* scalar to long long */
1854 /* machine independent conversion */
1856 /* generate high word */
1857 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1861 if (sbt
== VT_PTR
) {
1862 /* cast from pointer to int before we apply
1863 shift operation, which pointers don't support*/
1864 gen_cast(&int_type
);
1870 /* patch second register */
1871 vtop
[-1].r2
= vtop
->r
;
1875 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1876 (dbt
& VT_BTYPE
) == VT_PTR
) {
1877 /* XXX: not sure if this is perfect... need more tests */
1878 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1880 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1881 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1882 /* x86_64 specific: movslq */
1884 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1888 } else if (dbt
== VT_BOOL
) {
1889 /* scalar to bool */
1892 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1893 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1894 if (sbt
== VT_PTR
) {
1895 vtop
->type
.t
= VT_INT
;
1896 warning("nonportable conversion from pointer to char/short");
1898 force_charshort_cast(dbt
);
1899 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1901 if (sbt
== VT_LLONG
) {
1902 /* from long long: just take low order word */
1906 /* if lvalue and single word type, nothing to do because
1907 the lvalue already contains the real type size (see
1908 VT_LVAL_xxx constants) */
1911 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1912 /* if we are casting between pointer types,
1913 we must update the VT_LVAL_xxx size */
1914 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1915 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1920 /* return type size. Put alignment at 'a' */
1921 ST_FUNC
int type_size(CType
*type
, int *a
)
1926 bt
= type
->t
& VT_BTYPE
;
1927 if (bt
== VT_STRUCT
) {
1932 } else if (bt
== VT_PTR
) {
1933 if (type
->t
& VT_ARRAY
) {
1937 ts
= type_size(&s
->type
, a
);
1939 if (ts
< 0 && s
->c
< 0)
1947 } else if (bt
== VT_LDOUBLE
) {
1949 return LDOUBLE_SIZE
;
1950 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1951 #ifdef TCC_TARGET_I386
1952 #ifdef TCC_TARGET_PE
1957 #elif defined(TCC_TARGET_ARM)
1967 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1970 } else if (bt
== VT_SHORT
) {
1974 /* char, void, function, _Bool */
1980 /* return the pointed type of t */
1981 static inline CType
*pointed_type(CType
*type
)
1983 return &type
->ref
->type
;
1986 /* modify type so that its it is a pointer to type. */
1987 ST_FUNC
void mk_pointer(CType
*type
)
1990 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1991 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1995 /* compare function types. OLD functions match any new functions */
1996 static int is_compatible_func(CType
*type1
, CType
*type2
)
2002 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2004 /* check func_call */
2005 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2007 /* XXX: not complete */
2008 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2012 while (s1
!= NULL
) {
2015 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2025 /* return true if type1 and type2 are the same. If unqualified is
2026 true, qualifiers on the types are ignored.
2028 - enums are not checked as gcc __builtin_types_compatible_p ()
2030 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2034 t1
= type1
->t
& VT_TYPE
;
2035 t2
= type2
->t
& VT_TYPE
;
2037 /* strip qualifiers before comparing */
2038 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2039 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2041 /* XXX: bitfields ? */
2044 /* test more complicated cases */
2045 bt1
= t1
& VT_BTYPE
;
2046 if (bt1
== VT_PTR
) {
2047 type1
= pointed_type(type1
);
2048 type2
= pointed_type(type2
);
2049 return is_compatible_types(type1
, type2
);
2050 } else if (bt1
== VT_STRUCT
) {
2051 return (type1
->ref
== type2
->ref
);
2052 } else if (bt1
== VT_FUNC
) {
2053 return is_compatible_func(type1
, type2
);
2059 /* return true if type1 and type2 are exactly the same (including
2062 static int is_compatible_types(CType
*type1
, CType
*type2
)
2064 return compare_types(type1
,type2
,0);
2067 /* return true if type1 and type2 are the same (ignoring qualifiers).
2069 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2071 return compare_types(type1
,type2
,1);
2074 /* print a type. If 'varstr' is not NULL, then the variable is also
2075 printed in the type */
2077 /* XXX: add array and function pointers */
2078 static void type_to_str(char *buf
, int buf_size
,
2079 CType
*type
, const char *varstr
)
2086 t
= type
->t
& VT_TYPE
;
2089 if (t
& VT_CONSTANT
)
2090 pstrcat(buf
, buf_size
, "const ");
2091 if (t
& VT_VOLATILE
)
2092 pstrcat(buf
, buf_size
, "volatile ");
2093 if (t
& VT_UNSIGNED
)
2094 pstrcat(buf
, buf_size
, "unsigned ");
2124 tstr
= "long double";
2126 pstrcat(buf
, buf_size
, tstr
);
2130 if (bt
== VT_STRUCT
)
2134 pstrcat(buf
, buf_size
, tstr
);
2135 v
= type
->ref
->v
& ~SYM_STRUCT
;
2136 if (v
>= SYM_FIRST_ANOM
)
2137 pstrcat(buf
, buf_size
, "<anonymous>");
2139 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2143 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2144 pstrcat(buf
, buf_size
, "(");
2146 while (sa
!= NULL
) {
2147 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2148 pstrcat(buf
, buf_size
, buf1
);
2151 pstrcat(buf
, buf_size
, ", ");
2153 pstrcat(buf
, buf_size
, ")");
2157 pstrcpy(buf1
, sizeof(buf1
), "*");
2159 pstrcat(buf1
, sizeof(buf1
), varstr
);
2160 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2164 pstrcat(buf
, buf_size
, " ");
2165 pstrcat(buf
, buf_size
, varstr
);
2170 /* verify type compatibility to store vtop in 'dt' type, and generate
2172 static void gen_assign_cast(CType
*dt
)
2174 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2175 char buf1
[256], buf2
[256];
2178 st
= &vtop
->type
; /* source type */
2179 dbt
= dt
->t
& VT_BTYPE
;
2180 sbt
= st
->t
& VT_BTYPE
;
2181 if (dt
->t
& VT_CONSTANT
)
2182 warning("assignment of read-only location");
2185 /* special cases for pointers */
2186 /* '0' can also be a pointer */
2187 if (is_null_pointer(vtop
))
2189 /* accept implicit pointer to integer cast with warning */
2190 if (is_integer_btype(sbt
)) {
2191 warning("assignment makes pointer from integer without a cast");
2194 type1
= pointed_type(dt
);
2195 /* a function is implicitely a function pointer */
2196 if (sbt
== VT_FUNC
) {
2197 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2198 !is_compatible_types(pointed_type(dt
), st
))
2199 warning("assignment from incompatible pointer type");
2204 type2
= pointed_type(st
);
2205 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2206 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2207 /* void * can match anything */
2209 /* exact type match, except for unsigned */
2212 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2213 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2214 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2215 warning("assignment from incompatible pointer type");
2217 /* check const and volatile */
2218 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2219 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2220 warning("assignment discards qualifiers from pointer target type");
2226 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2227 warning("assignment makes integer from pointer without a cast");
2229 /* XXX: more tests */
2234 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2235 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2236 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2238 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2239 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2240 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2248 /* store vtop in lvalue pushed on stack */
2249 ST_FUNC
void vstore(void)
2251 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2253 ft
= vtop
[-1].type
.t
;
2254 sbt
= vtop
->type
.t
& VT_BTYPE
;
2255 dbt
= ft
& VT_BTYPE
;
2256 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2257 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2258 /* optimize char/short casts */
2259 delayed_cast
= VT_MUSTCAST
;
2260 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2261 /* XXX: factorize */
2262 if (ft
& VT_CONSTANT
)
2263 warning("assignment of read-only location");
2266 if (!(ft
& VT_BITFIELD
))
2267 gen_assign_cast(&vtop
[-1].type
);
2270 if (sbt
== VT_STRUCT
) {
2271 /* if structure, only generate pointer */
2272 /* structure assignment : generate memcpy */
2273 /* XXX: optimize if small size */
2274 if (!nocode_wanted
) {
2275 size
= type_size(&vtop
->type
, &align
);
2279 vtop
->type
.t
= VT_PTR
;
2282 /* address of memcpy() */
2285 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2286 else if(!(align
& 3))
2287 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2290 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2295 vtop
->type
.t
= VT_PTR
;
2304 /* leave source on stack */
2305 } else if (ft
& VT_BITFIELD
) {
2306 /* bitfield store handling */
2307 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2308 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2309 /* remove bit field info to avoid loops */
2310 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2312 /* duplicate source into other register */
2317 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2318 gen_cast(&vtop
[-1].type
);
2319 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2322 /* duplicate destination */
2324 vtop
[-1] = vtop
[-2];
2326 /* mask and shift source */
2327 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2328 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2329 vpushll((1ULL << bit_size
) - 1ULL);
2331 vpushi((1 << bit_size
) - 1);
2337 /* load destination, mask and or with source */
2339 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2340 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2342 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2349 /* pop off shifted source from "duplicate source..." above */
2353 #ifdef CONFIG_TCC_BCHECK
2354 /* bound check case */
2355 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2361 if (!nocode_wanted
) {
2365 #ifdef TCC_TARGET_X86_64
2366 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2371 r
= gv(rc
); /* generate value */
2372 /* if lvalue was saved on stack, must read it */
2373 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2375 t
= get_reg(RC_INT
);
2376 #ifdef TCC_TARGET_X86_64
2381 sv
.r
= VT_LOCAL
| VT_LVAL
;
2382 sv
.c
.ul
= vtop
[-1].c
.ul
;
2384 vtop
[-1].r
= t
| VT_LVAL
;
2387 #ifndef TCC_TARGET_X86_64
2388 /* two word case handling : store second register at word + 4 */
2389 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2391 /* convert to int to increment easily */
2392 vtop
->type
.t
= VT_INT
;
2398 /* XXX: it works because r2 is spilled last ! */
2399 store(vtop
->r2
, vtop
- 1);
2404 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2405 vtop
->r
|= delayed_cast
;
2409 /* post defines POST/PRE add. c is the token ++ or -- */
2410 ST_FUNC
void inc(int post
, int c
)
2413 vdup(); /* save lvalue */
2415 gv_dup(); /* duplicate value */
2420 vpushi(c
- TOK_MID
);
2422 vstore(); /* store value */
2424 vpop(); /* if post op, return saved value */
2427 /* Parse GNUC __attribute__ extension. Currently, the following
2428 extensions are recognized:
2429 - aligned(n) : set data/function alignment.
2430 - packed : force data alignment to 1
2431 - section(x) : generate data/code in this section.
2432 - unused : currently ignored, but may be used someday.
2433 - regparm(n) : pass function parameters in registers (i386 only)
2435 static void parse_attribute(AttributeDef
*ad
)
2439 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2443 while (tok
!= ')') {
2444 if (tok
< TOK_IDENT
)
2445 expect("attribute name");
2453 expect("section name");
2454 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2463 if (n
<= 0 || (n
& (n
- 1)) != 0)
2464 error("alignment must be a positive power of two");
2481 /* currently, no need to handle it because tcc does not
2482 track unused objects */
2486 /* currently, no need to handle it because tcc does not
2487 track unused objects */
2492 ad
->func_call
= FUNC_CDECL
;
2497 ad
->func_call
= FUNC_STDCALL
;
2499 #ifdef TCC_TARGET_I386
2509 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2515 ad
->func_call
= FUNC_FASTCALLW
;
2522 ad
->mode
= VT_LLONG
+ 1;
2525 ad
->mode
= VT_SHORT
+ 1;
2528 ad
->mode
= VT_INT
+ 1;
2531 warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2538 ad
->func_export
= 1;
2541 ad
->func_import
= 1;
2544 if (tcc_state
->warn_unsupported
)
2545 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2546 /* skip parameters */
2548 int parenthesis
= 0;
2552 else if (tok
== ')')
2555 } while (parenthesis
&& tok
!= -1);
2568 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2569 static void struct_decl(CType
*type
, int u
)
2571 int a
, v
, size
, align
, maxalign
, c
, offset
;
2572 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2573 Sym
*s
, *ss
, *ass
, **ps
;
2577 a
= tok
; /* save decl type */
2582 /* struct already defined ? return it */
2584 expect("struct/union/enum name");
2588 error("invalid type");
2595 /* we put an undefined size for struct/union */
2596 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2597 s
->r
= 0; /* default alignment is zero as gcc */
2598 /* put struct/union/enum name in type */
2606 error("struct/union/enum already defined");
2607 /* cannot be empty */
2609 /* non empty enums are not allowed */
2610 if (a
== TOK_ENUM
) {
2614 expect("identifier");
2620 /* enum symbols have static storage */
2621 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2622 ss
->type
.t
|= VT_STATIC
;
2627 /* NOTE: we accept a trailing comma */
2638 while (tok
!= '}') {
2639 parse_btype(&btype
, &ad
);
2645 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2646 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2647 expect("identifier");
2648 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2649 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2650 error("invalid type for '%s'",
2651 get_tok_str(v
, NULL
));
2655 bit_size
= expr_const();
2656 /* XXX: handle v = 0 case for messages */
2658 error("negative width in bit-field '%s'",
2659 get_tok_str(v
, NULL
));
2660 if (v
&& bit_size
== 0)
2661 error("zero width for bit-field '%s'",
2662 get_tok_str(v
, NULL
));
2664 size
= type_size(&type1
, &align
);
2666 if (align
< ad
.aligned
)
2668 } else if (ad
.packed
) {
2670 } else if (*tcc_state
->pack_stack_ptr
) {
2671 if (align
> *tcc_state
->pack_stack_ptr
)
2672 align
= *tcc_state
->pack_stack_ptr
;
2675 if (bit_size
>= 0) {
2676 bt
= type1
.t
& VT_BTYPE
;
2683 error("bitfields must have scalar type");
2685 if (bit_size
> bsize
) {
2686 error("width of '%s' exceeds its type",
2687 get_tok_str(v
, NULL
));
2688 } else if (bit_size
== bsize
) {
2689 /* no need for bit fields */
2691 } else if (bit_size
== 0) {
2692 /* XXX: what to do if only padding in a
2694 /* zero size: means to pad */
2697 /* we do not have enough room ?
2698 did the type change?
2700 if ((bit_pos
+ bit_size
) > bsize
||
2701 bt
!= prevbt
|| a
== TOK_UNION
)
2704 /* XXX: handle LSB first */
2705 type1
.t
|= VT_BITFIELD
|
2706 (bit_pos
<< VT_STRUCT_SHIFT
) |
2707 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2708 bit_pos
+= bit_size
;
2714 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2715 /* add new memory data only if starting
2717 if (lbit_pos
== 0) {
2718 if (a
== TOK_STRUCT
) {
2719 c
= (c
+ align
- 1) & -align
;
2728 if (align
> maxalign
)
2732 printf("add field %s offset=%d",
2733 get_tok_str(v
, NULL
), offset
);
2734 if (type1
.t
& VT_BITFIELD
) {
2735 printf(" pos=%d size=%d",
2736 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2737 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2742 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2744 while ((ass
= ass
->next
) != NULL
) {
2745 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2750 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2754 if (tok
== ';' || tok
== TOK_EOF
)
2761 /* store size and alignment */
2762 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2768 /* return 0 if no type declaration. otherwise, return the basic type
2771 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2773 int t
, u
, type_found
, typespec_found
, typedef_found
;
2777 memset(ad
, 0, sizeof(AttributeDef
));
2785 /* currently, we really ignore extension */
2795 if ((t
& VT_BTYPE
) != 0)
2796 error("too many basic types");
2812 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2813 #ifndef TCC_TARGET_PE
2814 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2816 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2817 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2831 if ((t
& VT_BTYPE
) == VT_LONG
) {
2832 #ifdef TCC_TARGET_PE
2833 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2835 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2843 struct_decl(&type1
, VT_ENUM
);
2846 type
->ref
= type1
.ref
;
2850 struct_decl(&type1
, VT_STRUCT
);
2853 /* type modifiers */
2906 /* GNUC attribute */
2907 case TOK_ATTRIBUTE1
:
2908 case TOK_ATTRIBUTE2
:
2909 parse_attribute(ad
);
2912 t
= (t
& ~VT_BTYPE
) | u
;
2920 parse_expr_type(&type1
);
2923 if (typespec_found
|| typedef_found
)
2926 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2929 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2930 type
->ref
= s
->type
.ref
;
2932 /* get attributes from typedef */
2933 if (0 == ad
->aligned
)
2934 ad
->aligned
= FUNC_ALIGN(s
->r
);
2935 if (0 == ad
->func_call
)
2936 ad
->func_call
= FUNC_CALL(s
->r
);
2937 ad
->packed
|= FUNC_PACKED(s
->r
);
2946 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2947 error("signed and unsigned modifier");
2948 if (tcc_state
->char_is_unsigned
) {
2949 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2954 /* long is never used as type */
2955 if ((t
& VT_BTYPE
) == VT_LONG
)
2956 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2957 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2959 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2965 /* convert a function parameter type (array to pointer and function to
2966 function pointer) */
2967 static inline void convert_parameter_type(CType
*pt
)
2969 /* remove const and volatile qualifiers (XXX: const could be used
2970 to indicate a const function parameter */
2971 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2972 /* array must be transformed to pointer according to ANSI C */
2974 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2979 static void post_type(CType
*type
, AttributeDef
*ad
)
2981 int n
, l
, t1
, arg_size
, align
;
2982 Sym
**plast
, *s
, *first
;
2987 /* function declaration */
2995 /* read param name and compute offset */
2996 if (l
!= FUNC_OLD
) {
2997 if (!parse_btype(&pt
, &ad1
)) {
2999 error("invalid type");
3006 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3008 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3009 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3010 error("parameter declared as void");
3011 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3016 expect("identifier");
3020 convert_parameter_type(&pt
);
3021 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3027 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3034 /* if no parameters, then old type prototype */
3038 t1
= type
->t
& VT_STORAGE
;
3039 /* NOTE: const is ignored in returned type as it has a special
3040 meaning in gcc / C++ */
3041 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3042 post_type(type
, ad
);
3043 /* we push a anonymous symbol which will contain the function prototype */
3044 ad
->func_args
= arg_size
;
3045 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3047 type
->t
= t1
| VT_FUNC
;
3049 } else if (tok
== '[') {
3050 /* array definition */
3052 if (tok
== TOK_RESTRICT1
)
3058 error("invalid array size");
3061 /* parse next post type */
3062 t1
= type
->t
& VT_STORAGE
;
3063 type
->t
&= ~VT_STORAGE
;
3064 post_type(type
, ad
);
3066 /* we push a anonymous symbol which will contain the array
3068 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3069 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3074 /* Parse a type declaration (except basic type), and return the type
3075 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3076 expected. 'type' should contain the basic type. 'ad' is the
3077 attribute definition of the basic type. It can be modified by
3080 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3083 CType type1
, *type2
;
3086 while (tok
== '*') {
3094 qualifiers
|= VT_CONSTANT
;
3099 qualifiers
|= VT_VOLATILE
;
3107 type
->t
|= qualifiers
;
3110 /* XXX: clarify attribute handling */
3111 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3112 parse_attribute(ad
);
3114 /* recursive type */
3115 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3116 type1
.t
= 0; /* XXX: same as int */
3119 /* XXX: this is not correct to modify 'ad' at this point, but
3120 the syntax is not clear */
3121 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3122 parse_attribute(ad
);
3123 type_decl(&type1
, ad
, v
, td
);
3126 /* type identifier */
3127 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3131 if (!(td
& TYPE_ABSTRACT
))
3132 expect("identifier");
3136 post_type(type
, ad
);
3137 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3138 parse_attribute(ad
);
3141 /* append type at the end of type1 */
3154 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3155 ST_FUNC
int lvalue_type(int t
)
3160 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3162 else if (bt
== VT_SHORT
)
3166 if (t
& VT_UNSIGNED
)
3167 r
|= VT_LVAL_UNSIGNED
;
3171 /* indirection with full error checking and bound check */
3172 ST_FUNC
void indir(void)
3174 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3175 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3179 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3181 vtop
->type
= *pointed_type(&vtop
->type
);
3182 /* Arrays and functions are never lvalues */
3183 if (!(vtop
->type
.t
& VT_ARRAY
)
3184 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3185 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3186 /* if bound checking, the referenced pointer must be checked */
3187 #ifdef CONFIG_TCC_BCHECK
3188 if (tcc_state
->do_bounds_check
)
3189 vtop
->r
|= VT_MUSTBOUND
;
3194 /* pass a parameter to a function and do type checking and casting */
3195 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3200 func_type
= func
->c
;
3201 if (func_type
== FUNC_OLD
||
3202 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3203 /* default casting : only need to convert float to double */
3204 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3208 } else if (arg
== NULL
) {
3209 error("too many arguments to function");
3212 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3213 gen_assign_cast(&type
);
3217 /* parse an expression of the form '(type)' or '(expr)' and return its
3219 static void parse_expr_type(CType
*type
)
3225 if (parse_btype(type
, &ad
)) {
3226 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3233 static void parse_type(CType
*type
)
3238 if (!parse_btype(type
, &ad
)) {
3241 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3244 static void vpush_tokc(int t
)
3249 vsetc(&type
, VT_CONST
, &tokc
);
3252 ST_FUNC
void unary(void)
3254 int n
, t
, align
, size
, r
, sizeof_caller
;
3258 static int in_sizeof
= 0;
3260 sizeof_caller
= in_sizeof
;
3262 /* XXX: GCC 2.95.3 does not generate a table although it should be
3276 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3280 vpush_tokc(VT_LLONG
);
3284 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3288 vpush_tokc(VT_FLOAT
);
3292 vpush_tokc(VT_DOUBLE
);
3296 vpush_tokc(VT_LDOUBLE
);
3299 case TOK___FUNCTION__
:
3301 goto tok_identifier
;
3307 /* special function name identifier */
3308 len
= strlen(funcname
) + 1;
3309 /* generate char[len] type */
3314 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3315 ptr
= section_ptr_add(data_section
, len
);
3316 memcpy(ptr
, funcname
, len
);
3321 #ifdef TCC_TARGET_PE
3322 t
= VT_SHORT
| VT_UNSIGNED
;
3328 /* string parsing */
3331 if (tcc_state
->warn_write_strings
)
3336 memset(&ad
, 0, sizeof(AttributeDef
));
3337 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3342 if (parse_btype(&type
, &ad
)) {
3343 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3345 /* check ISOC99 compound literal */
3347 /* data is allocated locally by default */
3352 /* all except arrays are lvalues */
3353 if (!(type
.t
& VT_ARRAY
))
3354 r
|= lvalue_type(type
.t
);
3355 memset(&ad
, 0, sizeof(AttributeDef
));
3356 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3358 if (sizeof_caller
) {
3365 } else if (tok
== '{') {
3366 /* save all registers */
3368 /* statement expression : we do not accept break/continue
3369 inside as GCC does */
3370 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3385 /* functions names must be treated as function pointers,
3386 except for unary '&' and sizeof. Since we consider that
3387 functions are not lvalues, we only have to handle it
3388 there and in function calls. */
3389 /* arrays can also be used although they are not lvalues */
3390 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3391 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3393 mk_pointer(&vtop
->type
);
3399 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3401 boolean
.t
= VT_BOOL
;
3403 vtop
->c
.i
= !vtop
->c
.i
;
3404 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3405 vtop
->c
.i
= vtop
->c
.i
^ 1;
3408 vseti(VT_JMP
, gtst(1, 0));
3419 /* in order to force cast, we add zero */
3421 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3422 error("pointer not accepted for unary plus");
3432 unary_type(&type
); // Perform a in_sizeof = 0;
3433 size
= type_size(&type
, &align
);
3434 if (t
== TOK_SIZEOF
) {
3436 error("sizeof applied to an incomplete type");
3441 vtop
->type
.t
|= VT_UNSIGNED
;
3444 case TOK_builtin_types_compatible_p
:
3453 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3454 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3455 vpushi(is_compatible_types(&type1
, &type2
));
3458 case TOK_builtin_constant_p
:
3460 int saved_nocode_wanted
, res
;
3463 saved_nocode_wanted
= nocode_wanted
;
3466 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3468 nocode_wanted
= saved_nocode_wanted
;
3473 case TOK_builtin_frame_address
:
3478 if (tok
!= TOK_CINT
) {
3479 error("__builtin_frame_address only takes integers");
3482 error("TCC only supports __builtin_frame_address(0)");
3488 vset(&type
, VT_LOCAL
, 0);
3491 #ifdef TCC_TARGET_X86_64
3492 case TOK_builtin_malloc
:
3494 goto tok_identifier
;
3495 case TOK_builtin_free
:
3497 goto tok_identifier
;
3514 goto tok_identifier
;
3516 /* allow to take the address of a label */
3517 if (tok
< TOK_UIDENT
)
3518 expect("label identifier");
3519 s
= label_find(tok
);
3521 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3523 if (s
->r
== LABEL_DECLARED
)
3524 s
->r
= LABEL_FORWARD
;
3527 s
->type
.t
= VT_VOID
;
3528 mk_pointer(&s
->type
);
3529 s
->type
.t
|= VT_STATIC
;
3531 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3540 expect("identifier");
3544 error("'%s' undeclared", get_tok_str(t
, NULL
));
3545 /* for simple function calls, we tolerate undeclared
3546 external reference to int() function */
3547 if (tcc_state
->warn_implicit_function_declaration
)
3548 warning("implicit declaration of function '%s'",
3549 get_tok_str(t
, NULL
));
3550 s
= external_global_sym(t
, &func_old_type
, 0);
3552 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3553 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3554 /* if referencing an inline function, then we generate a
3555 symbol to it if not already done. It will have the
3556 effect to generate code for it at the end of the
3557 compilation unit. Inline function as always
3558 generated in the text section. */
3560 put_extern_sym(s
, text_section
, 0, 0);
3561 r
= VT_SYM
| VT_CONST
;
3565 vset(&s
->type
, r
, s
->c
);
3566 /* if forward reference, we must point to s */
3567 if (vtop
->r
& VT_SYM
) {
3574 /* post operations */
3576 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3579 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3582 if (tok
== TOK_ARROW
)
3584 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3588 /* expect pointer on structure */
3589 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3590 expect("struct or union");
3594 while ((s
= s
->next
) != NULL
) {
3599 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3600 /* add field offset to pointer */
3601 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3604 /* change type to field type, and set to lvalue */
3605 vtop
->type
= s
->type
;
3606 vtop
->type
.t
|= qualifiers
;
3607 /* an array is never an lvalue */
3608 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3609 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3610 #ifdef CONFIG_TCC_BCHECK
3611 /* if bound checking, the referenced pointer must be checked */
3612 if (tcc_state
->do_bounds_check
)
3613 vtop
->r
|= VT_MUSTBOUND
;
3617 } else if (tok
== '[') {
3623 } else if (tok
== '(') {
3629 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3630 /* pointer test (no array accepted) */
3631 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3632 vtop
->type
= *pointed_type(&vtop
->type
);
3633 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3637 expect("function pointer");
3640 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3642 /* get return type */
3645 sa
= s
->next
; /* first parameter */
3648 /* compute first implicit argument if a structure is returned */
3649 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3650 /* get some space for the returned structure */
3651 size
= type_size(&s
->type
, &align
);
3652 loc
= (loc
- size
) & -align
;
3654 ret
.r
= VT_LOCAL
| VT_LVAL
;
3655 /* pass it as 'int' to avoid structure arg passing
3657 vseti(VT_LOCAL
, loc
);
3662 /* return in register */
3663 if (is_float(ret
.type
.t
)) {
3664 ret
.r
= reg_fret(ret
.type
.t
);
3666 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3675 gfunc_param_typed(s
, sa
);
3685 error("too few arguments to function");
3687 if (!nocode_wanted
) {
3688 gfunc_call(nb_args
);
3690 vtop
-= (nb_args
+ 1);
3693 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3701 static void uneq(void)
3707 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3708 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3709 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3724 ST_FUNC
void expr_prod(void)
3729 while (tok
== '*' || tok
== '/' || tok
== '%') {
3737 ST_FUNC
void expr_sum(void)
3742 while (tok
== '+' || tok
== '-') {
3750 static void expr_shift(void)
3755 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3763 static void expr_cmp(void)
3768 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3769 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3777 static void expr_cmpeq(void)
3782 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3790 static void expr_and(void)
3793 while (tok
== '&') {
3800 static void expr_xor(void)
3803 while (tok
== '^') {
3810 static void expr_or(void)
3813 while (tok
== '|') {
3820 /* XXX: fix this mess */
3821 static void expr_land_const(void)
3824 while (tok
== TOK_LAND
) {
3831 /* XXX: fix this mess */
3832 static void expr_lor_const(void)
3835 while (tok
== TOK_LOR
) {
3842 /* only used if non constant */
3843 static void expr_land(void)
3848 if (tok
== TOK_LAND
) {
3853 if (tok
!= TOK_LAND
) {
3863 static void expr_lor(void)
3868 if (tok
== TOK_LOR
) {
3873 if (tok
!= TOK_LOR
) {
3883 /* XXX: better constant handling */
3884 static void expr_eq(void)
3886 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3888 CType type
, type1
, type2
;
3895 boolean
.t
= VT_BOOL
;
3901 if (tok
!= ':' || !gnu_ext
) {
3916 if (vtop
!= vstack
) {
3917 /* needed to avoid having different registers saved in
3919 if (is_float(vtop
->type
.t
)) {
3921 #ifdef TCC_TARGET_X86_64
3922 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3932 if (tok
== ':' && gnu_ext
) {
3940 sv
= *vtop
; /* save value to handle it later */
3941 vtop
--; /* no vpop so that FP stack is not flushed */
3949 bt1
= t1
& VT_BTYPE
;
3951 bt2
= t2
& VT_BTYPE
;
3952 /* cast operands to correct type according to ISOC rules */
3953 if (is_float(bt1
) || is_float(bt2
)) {
3954 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3955 type
.t
= VT_LDOUBLE
;
3956 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3961 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3962 /* cast to biggest op */
3964 /* convert to unsigned if it does not fit in a long long */
3965 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3966 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3967 type
.t
|= VT_UNSIGNED
;
3968 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3969 /* XXX: test pointer compatibility */
3971 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3972 /* XXX: test function pointer compatibility */
3974 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3975 /* XXX: test structure compatibility */
3977 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3978 /* NOTE: as an extension, we accept void on only one side */
3981 /* integer operations */
3983 /* convert to unsigned if it does not fit in an integer */
3984 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3985 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3986 type
.t
|= VT_UNSIGNED
;
3989 /* now we convert second operand */
3991 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3994 if (is_float(type
.t
)) {
3996 #ifdef TCC_TARGET_X86_64
3997 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4001 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4002 /* for long longs, we use fixed registers to avoid having
4003 to handle a complicated move */
4008 /* this is horrible, but we must also convert first
4012 /* put again first value and cast it */
4015 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4025 ST_FUNC
void gexpr(void)
4036 /* parse an expression and return its type without any side effect. */
4037 static void expr_type(CType
*type
)
4039 int saved_nocode_wanted
;
4041 saved_nocode_wanted
= nocode_wanted
;
4046 nocode_wanted
= saved_nocode_wanted
;
4049 /* parse a unary expression and return its type without any side
4051 static void unary_type(CType
*type
)
4065 /* parse a constant expression and return value in vtop. */
4066 static void expr_const1(void)
4075 /* parse an integer constant and return its value. */
4076 ST_FUNC
int expr_const(void)
4080 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4081 expect("constant expression");
4087 /* return the label token if current token is a label, otherwise
4089 static int is_label(void)
4093 /* fast test first */
4094 if (tok
< TOK_UIDENT
)
4096 /* no need to save tokc because tok is an identifier */
4103 unget_tok(last_tok
);
4108 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4109 int case_reg
, int is_expr
)
4114 /* generate line number info */
4115 if (tcc_state
->do_debug
&&
4116 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4117 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4119 last_line_num
= file
->line_num
;
4123 /* default return value is (void) */
4125 vtop
->type
.t
= VT_VOID
;
4128 if (tok
== TOK_IF
) {
4135 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4137 if (c
== TOK_ELSE
) {
4141 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4142 gsym(d
); /* patch else jmp */
4145 } else if (tok
== TOK_WHILE
) {
4153 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4157 } else if (tok
== '{') {
4161 /* record local declaration stack position */
4163 llabel
= local_label_stack
;
4164 /* handle local labels declarations */
4165 if (tok
== TOK_LABEL
) {
4168 if (tok
< TOK_UIDENT
)
4169 expect("label identifier");
4170 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4180 while (tok
!= '}') {
4185 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4188 /* pop locally defined labels */
4189 label_pop(&local_label_stack
, llabel
);
4190 /* pop locally defined symbols */
4192 /* XXX: this solution makes only valgrind happy...
4193 triggered by gcc.c-torture/execute/20000917-1.c */
4195 switch(vtop
->type
.t
& VT_BTYPE
) {
4200 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4202 error("unsupported expression type");
4205 sym_pop(&local_stack
, s
);
4207 } else if (tok
== TOK_RETURN
) {
4211 gen_assign_cast(&func_vt
);
4212 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4214 /* if returning structure, must copy it to implicit
4215 first pointer arg location */
4218 size
= type_size(&func_vt
,&align
);
4221 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4225 loc
= (loc
- size
) & -4;
4228 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4231 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4233 vtop
->type
= int_type
;
4239 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4242 /* copy structure value to pointer */
4247 } else if (is_float(func_vt
.t
)) {
4248 gv(rc_fret(func_vt
.t
));
4252 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4255 rsym
= gjmp(rsym
); /* jmp */
4256 } else if (tok
== TOK_BREAK
) {
4259 error("cannot break");
4260 *bsym
= gjmp(*bsym
);
4263 } else if (tok
== TOK_CONTINUE
) {
4266 error("cannot continue");
4267 *csym
= gjmp(*csym
);
4270 } else if (tok
== TOK_FOR
) {
4297 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4302 if (tok
== TOK_DO
) {
4307 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4318 if (tok
== TOK_SWITCH
) {
4322 /* XXX: other types than integer */
4323 case_reg
= gv(RC_INT
);
4327 b
= gjmp(0); /* jump to first case */
4329 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4330 /* if no default, jmp after switch */
4338 if (tok
== TOK_CASE
) {
4345 if (gnu_ext
&& tok
== TOK_DOTS
) {
4349 warning("empty case range");
4351 /* since a case is like a label, we must skip it with a jmp */
4358 *case_sym
= gtst(1, 0);
4361 *case_sym
= gtst(1, 0);
4365 *case_sym
= gtst(1, *case_sym
);
4370 goto block_after_label
;
4372 if (tok
== TOK_DEFAULT
) {
4378 error("too many 'default'");
4381 goto block_after_label
;
4383 if (tok
== TOK_GOTO
) {
4385 if (tok
== '*' && gnu_ext
) {
4389 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4392 } else if (tok
>= TOK_UIDENT
) {
4393 s
= label_find(tok
);
4394 /* put forward definition if needed */
4396 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4398 if (s
->r
== LABEL_DECLARED
)
4399 s
->r
= LABEL_FORWARD
;
4401 /* label already defined */
4402 if (s
->r
& LABEL_FORWARD
)
4403 s
->jnext
= gjmp(s
->jnext
);
4405 gjmp_addr(s
->jnext
);
4408 expect("label identifier");
4411 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4419 if (s
->r
== LABEL_DEFINED
)
4420 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4422 s
->r
= LABEL_DEFINED
;
4424 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4427 /* we accept this, but it is a mistake */
4430 warning("deprecated use of label at end of compound statement");
4434 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4437 /* expression case */
4452 /* t is the array or struct type. c is the array or struct
4453 address. cur_index/cur_field is the pointer to the current
4454 value. 'size_only' is true if only size info is needed (only used
4456 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4457 int *cur_index
, Sym
**cur_field
,
4461 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4467 if (gnu_ext
&& (l
= is_label()) != 0)
4469 while (tok
== '[' || tok
== '.') {
4471 if (!(type
->t
& VT_ARRAY
))
4472 expect("array type");
4475 index
= expr_const();
4476 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4477 expect("invalid index");
4478 if (tok
== TOK_DOTS
&& gnu_ext
) {
4480 index_last
= expr_const();
4481 if (index_last
< 0 ||
4482 (s
->c
>= 0 && index_last
>= s
->c
) ||
4484 expect("invalid index");
4490 *cur_index
= index_last
;
4491 type
= pointed_type(type
);
4492 elem_size
= type_size(type
, &align
);
4493 c
+= index
* elem_size
;
4494 /* NOTE: we only support ranges for last designator */
4495 nb_elems
= index_last
- index
+ 1;
4496 if (nb_elems
!= 1) {
4505 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4506 expect("struct/union type");
4519 /* XXX: fix this mess by using explicit storage field */
4521 type1
.t
|= (type
->t
& ~VT_TYPE
);
4535 if (type
->t
& VT_ARRAY
) {
4537 type
= pointed_type(type
);
4538 c
+= index
* type_size(type
, &align
);
4542 error("too many field init");
4543 /* XXX: fix this mess by using explicit storage field */
4545 type1
.t
|= (type
->t
& ~VT_TYPE
);
4550 decl_initializer(type
, sec
, c
, 0, size_only
);
4552 /* XXX: make it more general */
4553 if (!size_only
&& nb_elems
> 1) {
4554 unsigned long c_end
;
4559 error("range init not supported yet for dynamic storage");
4560 c_end
= c
+ nb_elems
* elem_size
;
4561 if (c_end
> sec
->data_allocated
)
4562 section_realloc(sec
, c_end
);
4563 src
= sec
->data
+ c
;
4565 for(i
= 1; i
< nb_elems
; i
++) {
4567 memcpy(dst
, src
, elem_size
);
4573 #define EXPR_CONST 1
4576 /* store a value or an expression directly in global data or in local array */
4577 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4578 int v
, int expr_type
)
4580 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4582 unsigned long long bit_mask
;
4590 /* compound literals must be allocated globally in this case */
4591 saved_global_expr
= global_expr
;
4594 global_expr
= saved_global_expr
;
4595 /* NOTE: symbols are accepted */
4596 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4597 error("initializer element is not constant");
4605 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4608 /* XXX: not portable */
4609 /* XXX: generate error if incorrect relocation */
4610 gen_assign_cast(&dtype
);
4611 bt
= type
->t
& VT_BTYPE
;
4612 /* we'll write at most 12 bytes */
4613 if (c
+ 12 > sec
->data_allocated
) {
4614 section_realloc(sec
, c
+ 12);
4616 ptr
= sec
->data
+ c
;
4617 /* XXX: make code faster ? */
4618 if (!(type
->t
& VT_BITFIELD
)) {
4623 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4624 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4625 bit_mask
= (1LL << bit_size
) - 1;
4627 if ((vtop
->r
& VT_SYM
) &&
4633 (bt
== VT_INT
&& bit_size
!= 32)))
4634 error("initializer element is not computable at load time");
4637 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4639 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4642 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4645 *(double *)ptr
= vtop
->c
.d
;
4648 *(long double *)ptr
= vtop
->c
.ld
;
4651 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4654 if (vtop
->r
& VT_SYM
) {
4655 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4657 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4662 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4669 /* put zeros for variable based init */
4670 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4673 /* nothing to do because globals are already set to zero */
4675 vpush_global_sym(&func_old_type
, TOK_memset
);
4683 /* 't' contains the type and storage info. 'c' is the offset of the
4684 object in section 'sec'. If 'sec' is NULL, it means stack based
4685 allocation. 'first' is true if array '{' must be read (multi
4686 dimension implicit array init handling). 'size_only' is true if
4687 size only evaluation is wanted (only for arrays). */
4688 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4689 int first
, int size_only
)
4691 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4692 int size1
, align1
, expr_type
;
4696 if (type
->t
& VT_ARRAY
) {
4700 t1
= pointed_type(type
);
4701 size1
= type_size(t1
, &align1
);
4704 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4707 error("character array initializer must be a literal,"
4708 " optionally enclosed in braces");
4713 /* only parse strings here if correct type (otherwise: handle
4714 them as ((w)char *) expressions */
4715 if ((tok
== TOK_LSTR
&&
4716 #ifdef TCC_TARGET_PE
4717 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4719 (t1
->t
& VT_BTYPE
) == VT_INT
4721 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4722 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4727 /* compute maximum number of chars wanted */
4729 cstr_len
= cstr
->size
;
4731 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4734 if (n
>= 0 && nb
> (n
- array_length
))
4735 nb
= n
- array_length
;
4738 warning("initializer-string for array is too long");
4739 /* in order to go faster for common case (char
4740 string in global variable, we handle it
4742 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4743 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4747 ch
= ((unsigned char *)cstr
->data
)[i
];
4749 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4750 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4758 /* only add trailing zero if enough storage (no
4759 warning in this case since it is standard) */
4760 if (n
< 0 || array_length
< n
) {
4762 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4768 while (tok
!= '}') {
4769 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4770 if (n
>= 0 && index
>= n
)
4771 error("index too large");
4772 /* must put zero in holes (note that doing it that way
4773 ensures that it even works with designators) */
4774 if (!size_only
&& array_length
< index
) {
4775 init_putz(t1
, sec
, c
+ array_length
* size1
,
4776 (index
- array_length
) * size1
);
4779 if (index
> array_length
)
4780 array_length
= index
;
4781 /* special test for multi dimensional arrays (may not
4782 be strictly correct if designators are used at the
4784 if (index
>= n
&& no_oblock
)
4793 /* put zeros at the end */
4794 if (!size_only
&& n
>= 0 && array_length
< n
) {
4795 init_putz(t1
, sec
, c
+ array_length
* size1
,
4796 (n
- array_length
) * size1
);
4798 /* patch type size if needed */
4800 s
->c
= array_length
;
4801 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4802 (sec
|| !first
|| tok
== '{')) {
4805 /* NOTE: the previous test is a specific case for automatic
4806 struct/union init */
4807 /* XXX: union needs only one init */
4809 /* XXX: this test is incorrect for local initializers
4810 beginning with ( without {. It would be much more difficult
4811 to do it correctly (ideally, the expression parser should
4812 be used in all cases) */
4818 while (tok
== '(') {
4822 if (!parse_btype(&type1
, &ad1
))
4824 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4826 if (!is_assignable_types(type
, &type1
))
4827 error("invalid type for cast");
4832 if (first
|| tok
== '{') {
4841 while (tok
!= '}') {
4842 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4844 if (!size_only
&& array_length
< index
) {
4845 init_putz(type
, sec
, c
+ array_length
,
4846 index
- array_length
);
4848 index
= index
+ type_size(&f
->type
, &align1
);
4849 if (index
> array_length
)
4850 array_length
= index
;
4852 /* gr: skip fields from same union - ugly. */
4854 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4855 /* test for same offset */
4856 if (f
->next
->c
!= f
->c
)
4858 /* if yes, test for bitfield shift */
4859 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4860 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4861 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4862 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4863 if (bit_pos_1
!= bit_pos_2
)
4870 if (no_oblock
&& f
== NULL
)
4876 /* put zeros at the end */
4877 if (!size_only
&& array_length
< n
) {
4878 init_putz(type
, sec
, c
+ array_length
,
4887 } else if (tok
== '{') {
4889 decl_initializer(type
, sec
, c
, first
, size_only
);
4891 } else if (size_only
) {
4892 /* just skip expression */
4894 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4898 else if (tok
== ')')
4903 /* currently, we always use constant expression for globals
4904 (may change for scripting case) */
4905 expr_type
= EXPR_CONST
;
4907 expr_type
= EXPR_ANY
;
4908 init_putv(type
, sec
, c
, 0, expr_type
);
4912 /* parse an initializer for type 't' if 'has_init' is non zero, and
4913 allocate space in local or global data space ('r' is either
4914 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4915 variable 'v' of scope 'scope' is declared before initializers are
4916 parsed. If 'v' is zero, then a reference to the new object is put
4917 in the value stack. If 'has_init' is 2, a special parsing is done
4918 to handle string constants. */
4919 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4920 int has_init
, int v
, int scope
)
4922 int size
, align
, addr
, data_offset
;
4924 ParseState saved_parse_state
= {0};
4925 TokenString init_str
;
4928 size
= type_size(type
, &align
);
4929 /* If unknown size, we must evaluate it before
4930 evaluating initializers because
4931 initializers can generate global data too
4932 (e.g. string pointers or ISOC99 compound
4933 literals). It also simplifies local
4934 initializers handling */
4935 tok_str_new(&init_str
);
4938 error("unknown type size");
4939 /* get all init string */
4940 if (has_init
== 2) {
4941 /* only get strings */
4942 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4943 tok_str_add_tok(&init_str
);
4948 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4950 error("unexpected end of file in initializer");
4951 tok_str_add_tok(&init_str
);
4954 else if (tok
== '}') {
4964 tok_str_add(&init_str
, -1);
4965 tok_str_add(&init_str
, 0);
4968 save_parse_state(&saved_parse_state
);
4970 macro_ptr
= init_str
.str
;
4972 decl_initializer(type
, NULL
, 0, 1, 1);
4973 /* prepare second initializer parsing */
4974 macro_ptr
= init_str
.str
;
4977 /* if still unknown size, error */
4978 size
= type_size(type
, &align
);
4980 error("unknown type size");
4982 /* take into account specified alignment if bigger */
4984 if (ad
->aligned
> align
)
4985 align
= ad
->aligned
;
4986 } else if (ad
->packed
) {
4989 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4991 #ifdef CONFIG_TCC_BCHECK
4992 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4995 loc
= (loc
- size
) & -align
;
4997 #ifdef CONFIG_TCC_BCHECK
4998 /* handles bounds */
4999 /* XXX: currently, since we do only one pass, we cannot track
5000 '&' operators, so we add only arrays */
5001 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5002 unsigned long *bounds_ptr
;
5003 /* add padding between regions */
5005 /* then add local bound info */
5006 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5007 bounds_ptr
[0] = addr
;
5008 bounds_ptr
[1] = size
;
5012 /* local variable */
5013 sym_push(v
, type
, r
, addr
);
5015 /* push local reference */
5016 vset(type
, r
, addr
);
5022 if (v
&& scope
== VT_CONST
) {
5023 /* see if the symbol was already defined */
5026 if (!is_compatible_types(&sym
->type
, type
))
5027 error("incompatible types for redefinition of '%s'",
5028 get_tok_str(v
, NULL
));
5029 if (sym
->type
.t
& VT_EXTERN
) {
5030 /* if the variable is extern, it was not allocated */
5031 sym
->type
.t
&= ~VT_EXTERN
;
5032 /* set array size if it was ommited in extern
5034 if ((sym
->type
.t
& VT_ARRAY
) &&
5035 sym
->type
.ref
->c
< 0 &&
5037 sym
->type
.ref
->c
= type
->ref
->c
;
5039 /* we accept several definitions of the same
5040 global variable. this is tricky, because we
5041 must play with the SHN_COMMON type of the symbol */
5042 /* XXX: should check if the variable was already
5043 initialized. It is incorrect to initialized it
5045 /* no init data, we won't add more to the symbol */
5052 /* allocate symbol in corresponding section */
5057 else if (tcc_state
->nocommon
)
5061 data_offset
= sec
->data_offset
;
5062 data_offset
= (data_offset
+ align
- 1) & -align
;
5064 /* very important to increment global pointer at this time
5065 because initializers themselves can create new initializers */
5066 data_offset
+= size
;
5067 #ifdef CONFIG_TCC_BCHECK
5068 /* add padding if bound check */
5069 if (tcc_state
->do_bounds_check
)
5072 sec
->data_offset
= data_offset
;
5073 /* allocate section space to put the data */
5074 if (sec
->sh_type
!= SHT_NOBITS
&&
5075 data_offset
> sec
->data_allocated
)
5076 section_realloc(sec
, data_offset
);
5077 /* align section if needed */
5078 if (align
> sec
->sh_addralign
)
5079 sec
->sh_addralign
= align
;
5081 addr
= 0; /* avoid warning */
5085 if (scope
!= VT_CONST
|| !sym
) {
5086 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5088 /* update symbol definition */
5090 put_extern_sym(sym
, sec
, addr
, size
);
5093 /* put a common area */
5094 put_extern_sym(sym
, NULL
, align
, size
);
5095 /* XXX: find a nicer way */
5096 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5097 esym
->st_shndx
= SHN_COMMON
;
5102 /* push global reference */
5103 sym
= get_sym_ref(type
, sec
, addr
, size
);
5105 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5108 #ifdef CONFIG_TCC_BCHECK
5109 /* handles bounds now because the symbol must be defined
5110 before for the relocation */
5111 if (tcc_state
->do_bounds_check
) {
5112 unsigned long *bounds_ptr
;
5114 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5115 /* then add global bound info */
5116 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5117 bounds_ptr
[0] = 0; /* relocated */
5118 bounds_ptr
[1] = size
;
5123 decl_initializer(type
, sec
, addr
, 1, 0);
5124 /* restore parse state if needed */
5126 tok_str_free(init_str
.str
);
5127 restore_parse_state(&saved_parse_state
);
5133 static void put_func_debug(Sym
*sym
)
5138 /* XXX: we put here a dummy type */
5139 snprintf(buf
, sizeof(buf
), "%s:%c1",
5140 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5141 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5142 cur_text_section
, sym
->c
);
5143 /* //gr gdb wants a line at the function */
5144 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5149 /* parse an old style function declaration list */
5150 /* XXX: check multiple parameter */
5151 static void func_decl_list(Sym
*func_sym
)
5158 /* parse each declaration */
5159 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
5160 if (!parse_btype(&btype
, &ad
))
5161 expect("declaration list");
5162 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5163 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5165 /* we accept no variable after */
5169 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5170 /* find parameter in function parameter list */
5173 if ((s
->v
& ~SYM_FIELD
) == v
)
5177 error("declaration for parameter '%s' but no such parameter",
5178 get_tok_str(v
, NULL
));
5180 /* check that no storage specifier except 'register' was given */
5181 if (type
.t
& VT_STORAGE
)
5182 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5183 convert_parameter_type(&type
);
5184 /* we can add the type (NOTE: it could be local to the function) */
5186 /* accept other parameters */
5197 /* parse a function defined by symbol 'sym' and generate its code in
5198 'cur_text_section' */
5199 static void gen_function(Sym
*sym
)
5201 int saved_nocode_wanted
= nocode_wanted
;
5203 ind
= cur_text_section
->data_offset
;
5204 /* NOTE: we patch the symbol size later */
5205 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5206 funcname
= get_tok_str(sym
->v
, NULL
);
5208 /* put debug symbol */
5209 if (tcc_state
->do_debug
)
5210 put_func_debug(sym
);
5211 /* push a dummy symbol to enable local sym storage */
5212 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5213 gfunc_prolog(&sym
->type
);
5215 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5218 cur_text_section
->data_offset
= ind
;
5219 label_pop(&global_label_stack
, NULL
);
5220 sym_pop(&local_stack
, NULL
); /* reset local stack */
5221 /* end of function */
5222 /* patch symbol size */
5223 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5225 if (tcc_state
->do_debug
) {
5226 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5228 /* It's better to crash than to generate wrong code */
5229 cur_text_section
= NULL
;
5230 funcname
= ""; /* for safety */
5231 func_vt
.t
= VT_VOID
; /* for safety */
5232 ind
= 0; /* for safety */
5233 nocode_wanted
= saved_nocode_wanted
;
5236 ST_FUNC
void gen_inline_functions(void)
5239 int *str
, inline_generated
, i
;
5240 struct InlineFunc
*fn
;
5242 /* iterate while inline function are referenced */
5244 inline_generated
= 0;
5245 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5246 fn
= tcc_state
->inline_fns
[i
];
5248 if (sym
&& sym
->c
) {
5249 /* the function was used: generate its code and
5250 convert it to a normal function */
5251 str
= fn
->token_str
;
5254 strcpy(file
->filename
, fn
->filename
);
5255 sym
->r
= VT_SYM
| VT_CONST
;
5256 sym
->type
.t
&= ~VT_INLINE
;
5260 cur_text_section
= text_section
;
5262 macro_ptr
= NULL
; /* fail safe */
5264 inline_generated
= 1;
5267 if (!inline_generated
)
5270 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5271 fn
= tcc_state
->inline_fns
[i
];
5272 str
= fn
->token_str
;
5275 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5278 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5279 ST_FUNC
void decl(int l
)
5287 if (!parse_btype(&btype
, &ad
)) {
5288 /* skip redundant ';' */
5289 /* XXX: find more elegant solution */
5294 if (l
== VT_CONST
&&
5295 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5296 /* global asm block */
5300 /* special test for old K&R protos without explicit int
5301 type. Only accepted when defining global data */
5302 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5306 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5307 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5309 /* we accept no variable after */
5313 while (1) { /* iterate thru each declaration */
5315 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5319 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5320 printf("type = '%s'\n", buf
);
5323 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5324 /* if old style function prototype, we accept a
5327 if (sym
->c
== FUNC_OLD
)
5328 func_decl_list(sym
);
5331 #ifdef TCC_TARGET_PE
5333 type
.t
|= VT_IMPORT
;
5335 type
.t
|= VT_EXPORT
;
5339 error("cannot use local functions");
5340 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5341 expect("function definition");
5343 /* reject abstract declarators in function definition */
5345 while ((sym
= sym
->next
) != NULL
)
5346 if (!(sym
->v
& ~SYM_FIELD
))
5347 expect("identifier");
5349 /* XXX: cannot do better now: convert extern line to static inline */
5350 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5351 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5355 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5358 r
= sym
->type
.ref
->r
;
5359 /* use func_call from prototype if not defined */
5360 if (FUNC_CALL(r
) != FUNC_CDECL
5361 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5362 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5364 /* use export from prototype */
5366 FUNC_EXPORT(type
.ref
->r
) = 1;
5368 /* use static from prototype */
5369 if (sym
->type
.t
& VT_STATIC
)
5370 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5372 if (!is_compatible_types(&sym
->type
, &type
)) {
5374 error("incompatible types for redefinition of '%s'",
5375 get_tok_str(v
, NULL
));
5377 /* if symbol is already defined, then put complete type */
5380 /* put function symbol */
5381 sym
= global_identifier_push(v
, type
.t
, 0);
5382 sym
->type
.ref
= type
.ref
;
5385 /* static inline functions are just recorded as a kind
5386 of macro. Their code will be emitted at the end of
5387 the compilation unit only if they are used */
5388 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5389 (VT_INLINE
| VT_STATIC
)) {
5390 TokenString func_str
;
5392 struct InlineFunc
*fn
;
5393 const char *filename
;
5395 tok_str_new(&func_str
);
5401 error("unexpected end of file");
5402 tok_str_add_tok(&func_str
);
5407 } else if (t
== '}') {
5409 if (block_level
== 0)
5413 tok_str_add(&func_str
, -1);
5414 tok_str_add(&func_str
, 0);
5415 filename
= file
? file
->filename
: "";
5416 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5417 strcpy(fn
->filename
, filename
);
5419 fn
->token_str
= func_str
.str
;
5420 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5423 /* compute text section */
5424 cur_text_section
= ad
.section
;
5425 if (!cur_text_section
)
5426 cur_text_section
= text_section
;
5427 sym
->r
= VT_SYM
| VT_CONST
;
5432 if (btype
.t
& VT_TYPEDEF
) {
5433 /* save typedefed type */
5434 /* XXX: test storage specifiers ? */
5435 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5436 sym
->type
.t
|= VT_TYPEDEF
;
5437 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5439 /* external function definition */
5440 /* specific case for func_call attribute */
5441 type
.ref
->r
= INT_ATTR(&ad
);
5442 fn
= external_sym(v
, &type
, 0);
5444 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5450 /* Part 1: __USER_LABEL_PREFIX__ (user defined) */
5452 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5454 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5457 /* Part 2: api name */
5459 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5461 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5465 if (tcc_state
->warn_unsupported
)
5466 warning("ignoring redirection from %s to %s\n", get_tok_str(v
, NULL
), target
);
5468 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
5469 parse_attribute((AttributeDef
*) &fn
->type
.ref
->r
);
5472 /* not lvalue if array */
5474 if (!(type
.t
& VT_ARRAY
))
5475 r
|= lvalue_type(type
.t
);
5476 has_init
= (tok
== '=');
5477 if ((btype
.t
& VT_EXTERN
) ||
5478 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5479 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5480 /* external variable */
5481 /* NOTE: as GCC, uninitialized global static
5482 arrays of null size are considered as
5484 external_sym(v
, &type
, r
);
5486 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5487 if (type
.t
& VT_STATIC
)
5493 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);