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 */
2988 if ((type
->t
& VT_STATIC
) && local_stack
) {
2989 error("Function without file scope cannot be static");
2998 /* read param name and compute offset */
2999 if (l
!= FUNC_OLD
) {
3000 if (!parse_btype(&pt
, &ad1
)) {
3002 error("invalid type");
3009 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3011 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3012 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3013 error("parameter declared as void");
3014 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3019 expect("identifier");
3023 convert_parameter_type(&pt
);
3024 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3030 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3037 /* if no parameters, then old type prototype */
3041 t1
= type
->t
& VT_STORAGE
;
3042 /* NOTE: const is ignored in returned type as it has a special
3043 meaning in gcc / C++ */
3044 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
3045 post_type(type
, ad
);
3046 /* we push a anonymous symbol which will contain the function prototype */
3047 ad
->func_args
= arg_size
;
3048 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3050 type
->t
= t1
| VT_FUNC
;
3052 } else if (tok
== '[') {
3053 /* array definition */
3055 if (tok
== TOK_RESTRICT1
)
3061 error("invalid array size");
3064 /* parse next post type */
3065 t1
= type
->t
& VT_STORAGE
;
3066 type
->t
&= ~VT_STORAGE
;
3067 post_type(type
, ad
);
3069 /* we push a anonymous symbol which will contain the array
3071 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3072 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
3077 /* Parse a type declaration (except basic type), and return the type
3078 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3079 expected. 'type' should contain the basic type. 'ad' is the
3080 attribute definition of the basic type. It can be modified by
3083 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3086 CType type1
, *type2
;
3089 while (tok
== '*') {
3097 qualifiers
|= VT_CONSTANT
;
3102 qualifiers
|= VT_VOLATILE
;
3110 type
->t
|= qualifiers
;
3113 /* XXX: clarify attribute handling */
3114 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3115 parse_attribute(ad
);
3117 /* recursive type */
3118 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3119 type1
.t
= 0; /* XXX: same as int */
3122 /* XXX: this is not correct to modify 'ad' at this point, but
3123 the syntax is not clear */
3124 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3125 parse_attribute(ad
);
3126 type_decl(&type1
, ad
, v
, td
);
3129 /* type identifier */
3130 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3134 if (!(td
& TYPE_ABSTRACT
))
3135 expect("identifier");
3139 post_type(type
, ad
);
3140 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3141 parse_attribute(ad
);
3144 /* append type at the end of type1 */
3157 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3158 ST_FUNC
int lvalue_type(int t
)
3163 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3165 else if (bt
== VT_SHORT
)
3169 if (t
& VT_UNSIGNED
)
3170 r
|= VT_LVAL_UNSIGNED
;
3174 /* indirection with full error checking and bound check */
3175 ST_FUNC
void indir(void)
3177 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3178 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3182 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3184 vtop
->type
= *pointed_type(&vtop
->type
);
3185 /* Arrays and functions are never lvalues */
3186 if (!(vtop
->type
.t
& VT_ARRAY
)
3187 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3188 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3189 /* if bound checking, the referenced pointer must be checked */
3190 #ifdef CONFIG_TCC_BCHECK
3191 if (tcc_state
->do_bounds_check
)
3192 vtop
->r
|= VT_MUSTBOUND
;
3197 /* pass a parameter to a function and do type checking and casting */
3198 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3203 func_type
= func
->c
;
3204 if (func_type
== FUNC_OLD
||
3205 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3206 /* default casting : only need to convert float to double */
3207 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3211 } else if (arg
== NULL
) {
3212 error("too many arguments to function");
3215 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3216 gen_assign_cast(&type
);
3220 /* parse an expression of the form '(type)' or '(expr)' and return its
3222 static void parse_expr_type(CType
*type
)
3228 if (parse_btype(type
, &ad
)) {
3229 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3236 static void parse_type(CType
*type
)
3241 if (!parse_btype(type
, &ad
)) {
3244 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3247 static void vpush_tokc(int t
)
3252 vsetc(&type
, VT_CONST
, &tokc
);
3255 ST_FUNC
void unary(void)
3257 int n
, t
, align
, size
, r
, sizeof_caller
;
3261 static int in_sizeof
= 0;
3263 sizeof_caller
= in_sizeof
;
3265 /* XXX: GCC 2.95.3 does not generate a table although it should be
3279 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3283 vpush_tokc(VT_LLONG
);
3287 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3291 vpush_tokc(VT_FLOAT
);
3295 vpush_tokc(VT_DOUBLE
);
3299 vpush_tokc(VT_LDOUBLE
);
3302 case TOK___FUNCTION__
:
3304 goto tok_identifier
;
3310 /* special function name identifier */
3311 len
= strlen(funcname
) + 1;
3312 /* generate char[len] type */
3317 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3318 ptr
= section_ptr_add(data_section
, len
);
3319 memcpy(ptr
, funcname
, len
);
3324 #ifdef TCC_TARGET_PE
3325 t
= VT_SHORT
| VT_UNSIGNED
;
3331 /* string parsing */
3334 if (tcc_state
->warn_write_strings
)
3339 memset(&ad
, 0, sizeof(AttributeDef
));
3340 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3345 if (parse_btype(&type
, &ad
)) {
3346 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3348 /* check ISOC99 compound literal */
3350 /* data is allocated locally by default */
3355 /* all except arrays are lvalues */
3356 if (!(type
.t
& VT_ARRAY
))
3357 r
|= lvalue_type(type
.t
);
3358 memset(&ad
, 0, sizeof(AttributeDef
));
3359 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3361 if (sizeof_caller
) {
3368 } else if (tok
== '{') {
3369 /* save all registers */
3371 /* statement expression : we do not accept break/continue
3372 inside as GCC does */
3373 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3388 /* functions names must be treated as function pointers,
3389 except for unary '&' and sizeof. Since we consider that
3390 functions are not lvalues, we only have to handle it
3391 there and in function calls. */
3392 /* arrays can also be used although they are not lvalues */
3393 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3394 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3396 mk_pointer(&vtop
->type
);
3402 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3404 boolean
.t
= VT_BOOL
;
3406 vtop
->c
.i
= !vtop
->c
.i
;
3407 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3408 vtop
->c
.i
= vtop
->c
.i
^ 1;
3411 vseti(VT_JMP
, gtst(1, 0));
3422 /* in order to force cast, we add zero */
3424 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3425 error("pointer not accepted for unary plus");
3435 unary_type(&type
); // Perform a in_sizeof = 0;
3436 size
= type_size(&type
, &align
);
3437 if (t
== TOK_SIZEOF
) {
3439 error("sizeof applied to an incomplete type");
3444 vtop
->type
.t
|= VT_UNSIGNED
;
3447 case TOK_builtin_types_compatible_p
:
3456 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3457 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3458 vpushi(is_compatible_types(&type1
, &type2
));
3461 case TOK_builtin_constant_p
:
3463 int saved_nocode_wanted
, res
;
3466 saved_nocode_wanted
= nocode_wanted
;
3469 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3471 nocode_wanted
= saved_nocode_wanted
;
3476 case TOK_builtin_frame_address
:
3481 if (tok
!= TOK_CINT
) {
3482 error("__builtin_frame_address only takes integers");
3485 error("TCC only supports __builtin_frame_address(0)");
3491 vset(&type
, VT_LOCAL
, 0);
3494 #ifdef TCC_TARGET_X86_64
3495 case TOK_builtin_malloc
:
3497 goto tok_identifier
;
3498 case TOK_builtin_free
:
3500 goto tok_identifier
;
3517 goto tok_identifier
;
3519 /* allow to take the address of a label */
3520 if (tok
< TOK_UIDENT
)
3521 expect("label identifier");
3522 s
= label_find(tok
);
3524 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3526 if (s
->r
== LABEL_DECLARED
)
3527 s
->r
= LABEL_FORWARD
;
3530 s
->type
.t
= VT_VOID
;
3531 mk_pointer(&s
->type
);
3532 s
->type
.t
|= VT_STATIC
;
3534 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3543 expect("identifier");
3547 error("'%s' undeclared", get_tok_str(t
, NULL
));
3548 /* for simple function calls, we tolerate undeclared
3549 external reference to int() function */
3550 if (tcc_state
->warn_implicit_function_declaration
)
3551 warning("implicit declaration of function '%s'",
3552 get_tok_str(t
, NULL
));
3553 s
= external_global_sym(t
, &func_old_type
, 0);
3555 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3556 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3557 /* if referencing an inline function, then we generate a
3558 symbol to it if not already done. It will have the
3559 effect to generate code for it at the end of the
3560 compilation unit. Inline function as always
3561 generated in the text section. */
3563 put_extern_sym(s
, text_section
, 0, 0);
3564 r
= VT_SYM
| VT_CONST
;
3568 vset(&s
->type
, r
, s
->c
);
3569 /* if forward reference, we must point to s */
3570 if (vtop
->r
& VT_SYM
) {
3577 /* post operations */
3579 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3582 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3585 if (tok
== TOK_ARROW
)
3587 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3591 /* expect pointer on structure */
3592 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3593 expect("struct or union");
3597 while ((s
= s
->next
) != NULL
) {
3602 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3603 /* add field offset to pointer */
3604 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3607 /* change type to field type, and set to lvalue */
3608 vtop
->type
= s
->type
;
3609 vtop
->type
.t
|= qualifiers
;
3610 /* an array is never an lvalue */
3611 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3612 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3613 #ifdef CONFIG_TCC_BCHECK
3614 /* if bound checking, the referenced pointer must be checked */
3615 if (tcc_state
->do_bounds_check
)
3616 vtop
->r
|= VT_MUSTBOUND
;
3620 } else if (tok
== '[') {
3626 } else if (tok
== '(') {
3632 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3633 /* pointer test (no array accepted) */
3634 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3635 vtop
->type
= *pointed_type(&vtop
->type
);
3636 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3640 expect("function pointer");
3643 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3645 /* get return type */
3648 sa
= s
->next
; /* first parameter */
3651 /* compute first implicit argument if a structure is returned */
3652 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3653 /* get some space for the returned structure */
3654 size
= type_size(&s
->type
, &align
);
3655 loc
= (loc
- size
) & -align
;
3657 ret
.r
= VT_LOCAL
| VT_LVAL
;
3658 /* pass it as 'int' to avoid structure arg passing
3660 vseti(VT_LOCAL
, loc
);
3665 /* return in register */
3666 if (is_float(ret
.type
.t
)) {
3667 ret
.r
= reg_fret(ret
.type
.t
);
3669 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3678 gfunc_param_typed(s
, sa
);
3688 error("too few arguments to function");
3690 if (!nocode_wanted
) {
3691 gfunc_call(nb_args
);
3693 vtop
-= (nb_args
+ 1);
3696 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3704 static void uneq(void)
3710 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3711 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3712 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3727 ST_FUNC
void expr_prod(void)
3732 while (tok
== '*' || tok
== '/' || tok
== '%') {
3740 ST_FUNC
void expr_sum(void)
3745 while (tok
== '+' || tok
== '-') {
3753 static void expr_shift(void)
3758 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3766 static void expr_cmp(void)
3771 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3772 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3780 static void expr_cmpeq(void)
3785 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3793 static void expr_and(void)
3796 while (tok
== '&') {
3803 static void expr_xor(void)
3806 while (tok
== '^') {
3813 static void expr_or(void)
3816 while (tok
== '|') {
3823 /* XXX: fix this mess */
3824 static void expr_land_const(void)
3827 while (tok
== TOK_LAND
) {
3834 /* XXX: fix this mess */
3835 static void expr_lor_const(void)
3838 while (tok
== TOK_LOR
) {
3845 /* only used if non constant */
3846 static void expr_land(void)
3851 if (tok
== TOK_LAND
) {
3856 if (tok
!= TOK_LAND
) {
3866 static void expr_lor(void)
3871 if (tok
== TOK_LOR
) {
3876 if (tok
!= TOK_LOR
) {
3886 /* XXX: better constant handling */
3887 static void expr_eq(void)
3889 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3891 CType type
, type1
, type2
;
3898 boolean
.t
= VT_BOOL
;
3904 if (tok
!= ':' || !gnu_ext
) {
3919 if (vtop
!= vstack
) {
3920 /* needed to avoid having different registers saved in
3922 if (is_float(vtop
->type
.t
)) {
3924 #ifdef TCC_TARGET_X86_64
3925 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3935 if (tok
== ':' && gnu_ext
) {
3943 sv
= *vtop
; /* save value to handle it later */
3944 vtop
--; /* no vpop so that FP stack is not flushed */
3952 bt1
= t1
& VT_BTYPE
;
3954 bt2
= t2
& VT_BTYPE
;
3955 /* cast operands to correct type according to ISOC rules */
3956 if (is_float(bt1
) || is_float(bt2
)) {
3957 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3958 type
.t
= VT_LDOUBLE
;
3959 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3964 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3965 /* cast to biggest op */
3967 /* convert to unsigned if it does not fit in a long long */
3968 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3969 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3970 type
.t
|= VT_UNSIGNED
;
3971 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3972 /* XXX: test pointer compatibility */
3974 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3975 /* XXX: test function pointer compatibility */
3977 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3978 /* XXX: test structure compatibility */
3980 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3981 /* NOTE: as an extension, we accept void on only one side */
3984 /* integer operations */
3986 /* convert to unsigned if it does not fit in an integer */
3987 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3988 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3989 type
.t
|= VT_UNSIGNED
;
3992 /* now we convert second operand */
3994 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3997 if (is_float(type
.t
)) {
3999 #ifdef TCC_TARGET_X86_64
4000 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4004 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4005 /* for long longs, we use fixed registers to avoid having
4006 to handle a complicated move */
4011 /* this is horrible, but we must also convert first
4015 /* put again first value and cast it */
4018 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4028 ST_FUNC
void gexpr(void)
4039 /* parse an expression and return its type without any side effect. */
4040 static void expr_type(CType
*type
)
4042 int saved_nocode_wanted
;
4044 saved_nocode_wanted
= nocode_wanted
;
4049 nocode_wanted
= saved_nocode_wanted
;
4052 /* parse a unary expression and return its type without any side
4054 static void unary_type(CType
*type
)
4066 /* parse a constant expression and return value in vtop. */
4067 static void expr_const1(void)
4076 /* parse an integer constant and return its value. */
4077 ST_FUNC
int expr_const(void)
4081 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4082 expect("constant expression");
4088 /* return the label token if current token is a label, otherwise
4090 static int is_label(void)
4094 /* fast test first */
4095 if (tok
< TOK_UIDENT
)
4097 /* no need to save tokc because tok is an identifier */
4104 unget_tok(last_tok
);
4109 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4110 int case_reg
, int is_expr
)
4115 /* generate line number info */
4116 if (tcc_state
->do_debug
&&
4117 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4118 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4120 last_line_num
= file
->line_num
;
4124 /* default return value is (void) */
4126 vtop
->type
.t
= VT_VOID
;
4129 if (tok
== TOK_IF
) {
4136 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4138 if (c
== TOK_ELSE
) {
4142 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4143 gsym(d
); /* patch else jmp */
4146 } else if (tok
== TOK_WHILE
) {
4154 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4158 } else if (tok
== '{') {
4162 /* record local declaration stack position */
4164 llabel
= local_label_stack
;
4165 /* handle local labels declarations */
4166 if (tok
== TOK_LABEL
) {
4169 if (tok
< TOK_UIDENT
)
4170 expect("label identifier");
4171 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4181 while (tok
!= '}') {
4186 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4189 /* pop locally defined labels */
4190 label_pop(&local_label_stack
, llabel
);
4191 /* pop locally defined symbols */
4193 /* XXX: this solution makes only valgrind happy...
4194 triggered by gcc.c-torture/execute/20000917-1.c */
4196 switch(vtop
->type
.t
& VT_BTYPE
) {
4201 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4203 error("unsupported expression type");
4206 sym_pop(&local_stack
, s
);
4208 } else if (tok
== TOK_RETURN
) {
4212 gen_assign_cast(&func_vt
);
4213 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4215 /* if returning structure, must copy it to implicit
4216 first pointer arg location */
4219 size
= type_size(&func_vt
,&align
);
4222 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4226 loc
= (loc
- size
) & -4;
4229 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4232 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4234 vtop
->type
= int_type
;
4240 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4243 /* copy structure value to pointer */
4248 } else if (is_float(func_vt
.t
)) {
4249 gv(rc_fret(func_vt
.t
));
4253 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4256 rsym
= gjmp(rsym
); /* jmp */
4257 } else if (tok
== TOK_BREAK
) {
4260 error("cannot break");
4261 *bsym
= gjmp(*bsym
);
4264 } else if (tok
== TOK_CONTINUE
) {
4267 error("cannot continue");
4268 *csym
= gjmp(*csym
);
4271 } else if (tok
== TOK_FOR
) {
4298 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4303 if (tok
== TOK_DO
) {
4308 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4319 if (tok
== TOK_SWITCH
) {
4323 /* XXX: other types than integer */
4324 case_reg
= gv(RC_INT
);
4328 b
= gjmp(0); /* jump to first case */
4330 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4331 /* if no default, jmp after switch */
4339 if (tok
== TOK_CASE
) {
4346 if (gnu_ext
&& tok
== TOK_DOTS
) {
4350 warning("empty case range");
4352 /* since a case is like a label, we must skip it with a jmp */
4359 *case_sym
= gtst(1, 0);
4362 *case_sym
= gtst(1, 0);
4366 *case_sym
= gtst(1, *case_sym
);
4371 goto block_after_label
;
4373 if (tok
== TOK_DEFAULT
) {
4379 error("too many 'default'");
4382 goto block_after_label
;
4384 if (tok
== TOK_GOTO
) {
4386 if (tok
== '*' && gnu_ext
) {
4390 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4393 } else if (tok
>= TOK_UIDENT
) {
4394 s
= label_find(tok
);
4395 /* put forward definition if needed */
4397 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4399 if (s
->r
== LABEL_DECLARED
)
4400 s
->r
= LABEL_FORWARD
;
4402 /* label already defined */
4403 if (s
->r
& LABEL_FORWARD
)
4404 s
->jnext
= gjmp(s
->jnext
);
4406 gjmp_addr(s
->jnext
);
4409 expect("label identifier");
4412 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4420 if (s
->r
== LABEL_DEFINED
)
4421 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4423 s
->r
= LABEL_DEFINED
;
4425 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4428 /* we accept this, but it is a mistake */
4431 warning("deprecated use of label at end of compound statement");
4435 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4438 /* expression case */
4453 /* t is the array or struct type. c is the array or struct
4454 address. cur_index/cur_field is the pointer to the current
4455 value. 'size_only' is true if only size info is needed (only used
4457 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4458 int *cur_index
, Sym
**cur_field
,
4462 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4468 if (gnu_ext
&& (l
= is_label()) != 0)
4470 while (tok
== '[' || tok
== '.') {
4472 if (!(type
->t
& VT_ARRAY
))
4473 expect("array type");
4476 index
= expr_const();
4477 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4478 expect("invalid index");
4479 if (tok
== TOK_DOTS
&& gnu_ext
) {
4481 index_last
= expr_const();
4482 if (index_last
< 0 ||
4483 (s
->c
>= 0 && index_last
>= s
->c
) ||
4485 expect("invalid index");
4491 *cur_index
= index_last
;
4492 type
= pointed_type(type
);
4493 elem_size
= type_size(type
, &align
);
4494 c
+= index
* elem_size
;
4495 /* NOTE: we only support ranges for last designator */
4496 nb_elems
= index_last
- index
+ 1;
4497 if (nb_elems
!= 1) {
4506 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4507 expect("struct/union type");
4520 /* XXX: fix this mess by using explicit storage field */
4522 type1
.t
|= (type
->t
& ~VT_TYPE
);
4536 if (type
->t
& VT_ARRAY
) {
4538 type
= pointed_type(type
);
4539 c
+= index
* type_size(type
, &align
);
4543 error("too many field init");
4544 /* XXX: fix this mess by using explicit storage field */
4546 type1
.t
|= (type
->t
& ~VT_TYPE
);
4551 decl_initializer(type
, sec
, c
, 0, size_only
);
4553 /* XXX: make it more general */
4554 if (!size_only
&& nb_elems
> 1) {
4555 unsigned long c_end
;
4560 error("range init not supported yet for dynamic storage");
4561 c_end
= c
+ nb_elems
* elem_size
;
4562 if (c_end
> sec
->data_allocated
)
4563 section_realloc(sec
, c_end
);
4564 src
= sec
->data
+ c
;
4566 for(i
= 1; i
< nb_elems
; i
++) {
4568 memcpy(dst
, src
, elem_size
);
4574 #define EXPR_CONST 1
4577 /* store a value or an expression directly in global data or in local array */
4578 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4579 int v
, int expr_type
)
4581 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4583 unsigned long long bit_mask
;
4591 /* compound literals must be allocated globally in this case */
4592 saved_global_expr
= global_expr
;
4595 global_expr
= saved_global_expr
;
4596 /* NOTE: symbols are accepted */
4597 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4598 error("initializer element is not constant");
4606 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4609 /* XXX: not portable */
4610 /* XXX: generate error if incorrect relocation */
4611 gen_assign_cast(&dtype
);
4612 bt
= type
->t
& VT_BTYPE
;
4613 /* we'll write at most 12 bytes */
4614 if (c
+ 12 > sec
->data_allocated
) {
4615 section_realloc(sec
, c
+ 12);
4617 ptr
= sec
->data
+ c
;
4618 /* XXX: make code faster ? */
4619 if (!(type
->t
& VT_BITFIELD
)) {
4624 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4625 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4626 bit_mask
= (1LL << bit_size
) - 1;
4628 if ((vtop
->r
& VT_SYM
) &&
4634 (bt
== VT_INT
&& bit_size
!= 32)))
4635 error("initializer element is not computable at load time");
4638 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4640 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4643 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4646 *(double *)ptr
= vtop
->c
.d
;
4649 *(long double *)ptr
= vtop
->c
.ld
;
4652 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4655 if (vtop
->r
& VT_SYM
) {
4656 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4658 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4663 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4670 /* put zeros for variable based init */
4671 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4674 /* nothing to do because globals are already set to zero */
4676 vpush_global_sym(&func_old_type
, TOK_memset
);
4684 /* 't' contains the type and storage info. 'c' is the offset of the
4685 object in section 'sec'. If 'sec' is NULL, it means stack based
4686 allocation. 'first' is true if array '{' must be read (multi
4687 dimension implicit array init handling). 'size_only' is true if
4688 size only evaluation is wanted (only for arrays). */
4689 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4690 int first
, int size_only
)
4692 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4693 int size1
, align1
, expr_type
;
4697 if (type
->t
& VT_ARRAY
) {
4701 t1
= pointed_type(type
);
4702 size1
= type_size(t1
, &align1
);
4705 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4708 error("character array initializer must be a literal,"
4709 " optionally enclosed in braces");
4714 /* only parse strings here if correct type (otherwise: handle
4715 them as ((w)char *) expressions */
4716 if ((tok
== TOK_LSTR
&&
4717 #ifdef TCC_TARGET_PE
4718 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4720 (t1
->t
& VT_BTYPE
) == VT_INT
4722 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4723 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4728 /* compute maximum number of chars wanted */
4730 cstr_len
= cstr
->size
;
4732 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4735 if (n
>= 0 && nb
> (n
- array_length
))
4736 nb
= n
- array_length
;
4739 warning("initializer-string for array is too long");
4740 /* in order to go faster for common case (char
4741 string in global variable, we handle it
4743 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4744 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4748 ch
= ((unsigned char *)cstr
->data
)[i
];
4750 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4751 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4759 /* only add trailing zero if enough storage (no
4760 warning in this case since it is standard) */
4761 if (n
< 0 || array_length
< n
) {
4763 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4769 while (tok
!= '}') {
4770 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4771 if (n
>= 0 && index
>= n
)
4772 error("index too large");
4773 /* must put zero in holes (note that doing it that way
4774 ensures that it even works with designators) */
4775 if (!size_only
&& array_length
< index
) {
4776 init_putz(t1
, sec
, c
+ array_length
* size1
,
4777 (index
- array_length
) * size1
);
4780 if (index
> array_length
)
4781 array_length
= index
;
4782 /* special test for multi dimensional arrays (may not
4783 be strictly correct if designators are used at the
4785 if (index
>= n
&& no_oblock
)
4794 /* put zeros at the end */
4795 if (!size_only
&& n
>= 0 && array_length
< n
) {
4796 init_putz(t1
, sec
, c
+ array_length
* size1
,
4797 (n
- array_length
) * size1
);
4799 /* patch type size if needed */
4801 s
->c
= array_length
;
4802 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4803 (sec
|| !first
|| tok
== '{')) {
4806 /* NOTE: the previous test is a specific case for automatic
4807 struct/union init */
4808 /* XXX: union needs only one init */
4810 /* XXX: this test is incorrect for local initializers
4811 beginning with ( without {. It would be much more difficult
4812 to do it correctly (ideally, the expression parser should
4813 be used in all cases) */
4819 while (tok
== '(') {
4823 if (!parse_btype(&type1
, &ad1
))
4825 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4827 if (!is_assignable_types(type
, &type1
))
4828 error("invalid type for cast");
4833 if (first
|| tok
== '{') {
4842 while (tok
!= '}') {
4843 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4845 if (!size_only
&& array_length
< index
) {
4846 init_putz(type
, sec
, c
+ array_length
,
4847 index
- array_length
);
4849 index
= index
+ type_size(&f
->type
, &align1
);
4850 if (index
> array_length
)
4851 array_length
= index
;
4853 /* gr: skip fields from same union - ugly. */
4855 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4856 /* test for same offset */
4857 if (f
->next
->c
!= f
->c
)
4859 /* if yes, test for bitfield shift */
4860 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4861 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4862 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4863 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4864 if (bit_pos_1
!= bit_pos_2
)
4871 if (no_oblock
&& f
== NULL
)
4877 /* put zeros at the end */
4878 if (!size_only
&& array_length
< n
) {
4879 init_putz(type
, sec
, c
+ array_length
,
4888 } else if (tok
== '{') {
4890 decl_initializer(type
, sec
, c
, first
, size_only
);
4892 } else if (size_only
) {
4893 /* just skip expression */
4895 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4899 else if (tok
== ')')
4904 /* currently, we always use constant expression for globals
4905 (may change for scripting case) */
4906 expr_type
= EXPR_CONST
;
4908 expr_type
= EXPR_ANY
;
4909 init_putv(type
, sec
, c
, 0, expr_type
);
4913 /* parse an initializer for type 't' if 'has_init' is non zero, and
4914 allocate space in local or global data space ('r' is either
4915 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4916 variable 'v' of scope 'scope' is declared before initializers are
4917 parsed. If 'v' is zero, then a reference to the new object is put
4918 in the value stack. If 'has_init' is 2, a special parsing is done
4919 to handle string constants. */
4920 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4921 int has_init
, int v
, int scope
)
4923 int size
, align
, addr
, data_offset
;
4925 ParseState saved_parse_state
= {0};
4926 TokenString init_str
;
4929 size
= type_size(type
, &align
);
4930 /* If unknown size, we must evaluate it before
4931 evaluating initializers because
4932 initializers can generate global data too
4933 (e.g. string pointers or ISOC99 compound
4934 literals). It also simplifies local
4935 initializers handling */
4936 tok_str_new(&init_str
);
4939 error("unknown type size");
4940 /* get all init string */
4941 if (has_init
== 2) {
4942 /* only get strings */
4943 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4944 tok_str_add_tok(&init_str
);
4949 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4951 error("unexpected end of file in initializer");
4952 tok_str_add_tok(&init_str
);
4955 else if (tok
== '}') {
4965 tok_str_add(&init_str
, -1);
4966 tok_str_add(&init_str
, 0);
4969 save_parse_state(&saved_parse_state
);
4971 macro_ptr
= init_str
.str
;
4973 decl_initializer(type
, NULL
, 0, 1, 1);
4974 /* prepare second initializer parsing */
4975 macro_ptr
= init_str
.str
;
4978 /* if still unknown size, error */
4979 size
= type_size(type
, &align
);
4981 error("unknown type size");
4983 /* take into account specified alignment if bigger */
4985 if (ad
->aligned
> align
)
4986 align
= ad
->aligned
;
4987 } else if (ad
->packed
) {
4990 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4992 #ifdef CONFIG_TCC_BCHECK
4993 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4996 loc
= (loc
- size
) & -align
;
4998 #ifdef CONFIG_TCC_BCHECK
4999 /* handles bounds */
5000 /* XXX: currently, since we do only one pass, we cannot track
5001 '&' operators, so we add only arrays */
5002 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5003 unsigned long *bounds_ptr
;
5004 /* add padding between regions */
5006 /* then add local bound info */
5007 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5008 bounds_ptr
[0] = addr
;
5009 bounds_ptr
[1] = size
;
5013 /* local variable */
5014 sym_push(v
, type
, r
, addr
);
5016 /* push local reference */
5017 vset(type
, r
, addr
);
5023 if (v
&& scope
== VT_CONST
) {
5024 /* see if the symbol was already defined */
5027 if (!is_compatible_types(&sym
->type
, type
))
5028 error("incompatible types for redefinition of '%s'",
5029 get_tok_str(v
, NULL
));
5030 if (sym
->type
.t
& VT_EXTERN
) {
5031 /* if the variable is extern, it was not allocated */
5032 sym
->type
.t
&= ~VT_EXTERN
;
5033 /* set array size if it was ommited in extern
5035 if ((sym
->type
.t
& VT_ARRAY
) &&
5036 sym
->type
.ref
->c
< 0 &&
5038 sym
->type
.ref
->c
= type
->ref
->c
;
5040 /* we accept several definitions of the same
5041 global variable. this is tricky, because we
5042 must play with the SHN_COMMON type of the symbol */
5043 /* XXX: should check if the variable was already
5044 initialized. It is incorrect to initialized it
5046 /* no init data, we won't add more to the symbol */
5053 /* allocate symbol in corresponding section */
5058 else if (tcc_state
->nocommon
)
5062 data_offset
= sec
->data_offset
;
5063 data_offset
= (data_offset
+ align
- 1) & -align
;
5065 /* very important to increment global pointer at this time
5066 because initializers themselves can create new initializers */
5067 data_offset
+= size
;
5068 #ifdef CONFIG_TCC_BCHECK
5069 /* add padding if bound check */
5070 if (tcc_state
->do_bounds_check
)
5073 sec
->data_offset
= data_offset
;
5074 /* allocate section space to put the data */
5075 if (sec
->sh_type
!= SHT_NOBITS
&&
5076 data_offset
> sec
->data_allocated
)
5077 section_realloc(sec
, data_offset
);
5078 /* align section if needed */
5079 if (align
> sec
->sh_addralign
)
5080 sec
->sh_addralign
= align
;
5082 addr
= 0; /* avoid warning */
5086 if (scope
!= VT_CONST
|| !sym
) {
5087 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5089 /* update symbol definition */
5091 put_extern_sym(sym
, sec
, addr
, size
);
5094 /* put a common area */
5095 put_extern_sym(sym
, NULL
, align
, size
);
5096 /* XXX: find a nicer way */
5097 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5098 esym
->st_shndx
= SHN_COMMON
;
5103 /* push global reference */
5104 sym
= get_sym_ref(type
, sec
, addr
, size
);
5106 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5109 #ifdef CONFIG_TCC_BCHECK
5110 /* handles bounds now because the symbol must be defined
5111 before for the relocation */
5112 if (tcc_state
->do_bounds_check
) {
5113 unsigned long *bounds_ptr
;
5115 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5116 /* then add global bound info */
5117 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5118 bounds_ptr
[0] = 0; /* relocated */
5119 bounds_ptr
[1] = size
;
5124 decl_initializer(type
, sec
, addr
, 1, 0);
5125 /* restore parse state if needed */
5127 tok_str_free(init_str
.str
);
5128 restore_parse_state(&saved_parse_state
);
5134 static void put_func_debug(Sym
*sym
)
5139 /* XXX: we put here a dummy type */
5140 snprintf(buf
, sizeof(buf
), "%s:%c1",
5141 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5142 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5143 cur_text_section
, sym
->c
);
5144 /* //gr gdb wants a line at the function */
5145 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5150 /* parse an old style function declaration list */
5151 /* XXX: check multiple parameter */
5152 static void func_decl_list(Sym
*func_sym
)
5159 /* parse each declaration */
5160 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
5161 if (!parse_btype(&btype
, &ad
))
5162 expect("declaration list");
5163 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5164 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5166 /* we accept no variable after */
5170 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5171 /* find parameter in function parameter list */
5174 if ((s
->v
& ~SYM_FIELD
) == v
)
5178 error("declaration for parameter '%s' but no such parameter",
5179 get_tok_str(v
, NULL
));
5181 /* check that no storage specifier except 'register' was given */
5182 if (type
.t
& VT_STORAGE
)
5183 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5184 convert_parameter_type(&type
);
5185 /* we can add the type (NOTE: it could be local to the function) */
5187 /* accept other parameters */
5198 /* parse a function defined by symbol 'sym' and generate its code in
5199 'cur_text_section' */
5200 static void gen_function(Sym
*sym
)
5202 int saved_nocode_wanted
= nocode_wanted
;
5204 ind
= cur_text_section
->data_offset
;
5205 /* NOTE: we patch the symbol size later */
5206 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5207 funcname
= get_tok_str(sym
->v
, NULL
);
5209 /* put debug symbol */
5210 if (tcc_state
->do_debug
)
5211 put_func_debug(sym
);
5212 /* push a dummy symbol to enable local sym storage */
5213 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5214 gfunc_prolog(&sym
->type
);
5216 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5219 cur_text_section
->data_offset
= ind
;
5220 label_pop(&global_label_stack
, NULL
);
5221 sym_pop(&local_stack
, NULL
); /* reset local stack */
5222 /* end of function */
5223 /* patch symbol size */
5224 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5226 if (tcc_state
->do_debug
) {
5227 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5229 /* It's better to crash than to generate wrong code */
5230 cur_text_section
= NULL
;
5231 funcname
= ""; /* for safety */
5232 func_vt
.t
= VT_VOID
; /* for safety */
5233 ind
= 0; /* for safety */
5234 nocode_wanted
= saved_nocode_wanted
;
5237 ST_FUNC
void gen_inline_functions(void)
5240 int *str
, inline_generated
, i
;
5241 struct InlineFunc
*fn
;
5243 /* iterate while inline function are referenced */
5245 inline_generated
= 0;
5246 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5247 fn
= tcc_state
->inline_fns
[i
];
5249 if (sym
&& sym
->c
) {
5250 /* the function was used: generate its code and
5251 convert it to a normal function */
5252 str
= fn
->token_str
;
5255 strcpy(file
->filename
, fn
->filename
);
5256 sym
->r
= VT_SYM
| VT_CONST
;
5257 sym
->type
.t
&= ~VT_INLINE
;
5261 cur_text_section
= text_section
;
5263 macro_ptr
= NULL
; /* fail safe */
5265 inline_generated
= 1;
5268 if (!inline_generated
)
5271 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5272 fn
= tcc_state
->inline_fns
[i
];
5273 str
= fn
->token_str
;
5276 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5279 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5280 ST_FUNC
void decl(int l
)
5288 if (!parse_btype(&btype
, &ad
)) {
5289 /* skip redundant ';' */
5290 /* XXX: find more elegant solution */
5295 if (l
== VT_CONST
&&
5296 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5297 /* global asm block */
5301 /* special test for old K&R protos without explicit int
5302 type. Only accepted when defining global data */
5303 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5307 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5308 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5310 /* we accept no variable after */
5314 while (1) { /* iterate thru each declaration */
5316 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5320 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5321 printf("type = '%s'\n", buf
);
5324 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5325 /* if old style function prototype, we accept a
5328 if (sym
->c
== FUNC_OLD
)
5329 func_decl_list(sym
);
5332 #ifdef TCC_TARGET_PE
5334 type
.t
|= VT_IMPORT
;
5336 type
.t
|= VT_EXPORT
;
5340 error("cannot use local functions");
5341 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5342 expect("function definition");
5344 /* reject abstract declarators in function definition */
5346 while ((sym
= sym
->next
) != NULL
)
5347 if (!(sym
->v
& ~SYM_FIELD
))
5348 expect("identifier");
5350 /* XXX: cannot do better now: convert extern line to static inline */
5351 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5352 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5356 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5359 r
= sym
->type
.ref
->r
;
5360 /* use func_call from prototype if not defined */
5361 if (FUNC_CALL(r
) != FUNC_CDECL
5362 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5363 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5365 /* use export from prototype */
5367 FUNC_EXPORT(type
.ref
->r
) = 1;
5369 /* use static from prototype */
5370 if (sym
->type
.t
& VT_STATIC
)
5371 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5373 if (!is_compatible_types(&sym
->type
, &type
)) {
5375 error("incompatible types for redefinition of '%s'",
5376 get_tok_str(v
, NULL
));
5378 /* if symbol is already defined, then put complete type */
5381 /* put function symbol */
5382 sym
= global_identifier_push(v
, type
.t
, 0);
5383 sym
->type
.ref
= type
.ref
;
5386 /* static inline functions are just recorded as a kind
5387 of macro. Their code will be emitted at the end of
5388 the compilation unit only if they are used */
5389 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5390 (VT_INLINE
| VT_STATIC
)) {
5391 TokenString func_str
;
5393 struct InlineFunc
*fn
;
5394 const char *filename
;
5396 tok_str_new(&func_str
);
5402 error("unexpected end of file");
5403 tok_str_add_tok(&func_str
);
5408 } else if (t
== '}') {
5410 if (block_level
== 0)
5414 tok_str_add(&func_str
, -1);
5415 tok_str_add(&func_str
, 0);
5416 filename
= file
? file
->filename
: "";
5417 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5418 strcpy(fn
->filename
, filename
);
5420 fn
->token_str
= func_str
.str
;
5421 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5424 /* compute text section */
5425 cur_text_section
= ad
.section
;
5426 if (!cur_text_section
)
5427 cur_text_section
= text_section
;
5428 sym
->r
= VT_SYM
| VT_CONST
;
5433 if (btype
.t
& VT_TYPEDEF
) {
5434 /* save typedefed type */
5435 /* XXX: test storage specifiers ? */
5436 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5437 sym
->type
.t
|= VT_TYPEDEF
;
5438 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5440 /* external function definition */
5441 /* specific case for func_call attribute */
5442 type
.ref
->r
= INT_ATTR(&ad
);
5443 fn
= external_sym(v
, &type
, 0);
5445 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5451 /* Part 1: __USER_LABEL_PREFIX__ (user defined) */
5453 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5455 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5458 /* Part 2: api name */
5460 pstrcat(target
, sizeof(target
), tokc
.cstr
->data
);
5462 pstrcat(target
, sizeof(target
), get_tok_str(tok
, NULL
));
5466 if (tcc_state
->warn_unsupported
)
5467 warning("ignoring redirection from %s to %s\n", get_tok_str(v
, NULL
), target
);
5469 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
5470 parse_attribute((AttributeDef
*) &fn
->type
.ref
->r
);
5473 /* not lvalue if array */
5475 if (!(type
.t
& VT_ARRAY
))
5476 r
|= lvalue_type(type
.t
);
5477 has_init
= (tok
== '=');
5478 if ((btype
.t
& VT_EXTERN
) ||
5479 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5480 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5481 /* external variable */
5482 /* NOTE: as GCC, uninitialized global static
5483 arrays of null size are considered as
5485 external_sym(v
, &type
, r
);
5487 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5488 if (type
.t
& VT_STATIC
)
5494 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);