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 void swap(int *p
, int *q
)
31 void vsetc(CType
*type
, int r
, CValue
*vc
)
35 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
37 /* cannot let cpu flags if other instruction are generated. Also
38 avoid leaving VT_JMP anywhere except on the top of the stack
39 because it would complicate the code generator. */
41 v
= vtop
->r
& VT_VALMASK
;
42 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
52 /* push integer constant */
57 vsetc(&int_type
, VT_CONST
, &cval
);
60 /* push long long constant */
61 void vpushll(long long v
)
67 vsetc(&ctype
, VT_CONST
, &cval
);
70 /* Return a static symbol pointing to a section */
71 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
72 unsigned long offset
, unsigned long size
)
78 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
79 sym
->type
.ref
= type
->ref
;
80 sym
->r
= VT_CONST
| VT_SYM
;
81 put_extern_sym(sym
, sec
, offset
, size
);
85 /* push a reference to a section offset by adding a dummy symbol */
86 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
91 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
92 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
95 /* define a new external reference to a symbol 'v' of type 'u' */
96 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
102 /* push forward reference */
103 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
104 s
->type
.ref
= type
->ref
;
105 s
->r
= r
| VT_CONST
| VT_SYM
;
110 /* define a new external reference to a symbol 'v' of type 'u' */
111 static Sym
*external_sym(int v
, CType
*type
, int r
)
117 /* push forward reference */
118 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
119 s
->type
.t
|= VT_EXTERN
;
120 } else if (s
->type
.ref
== func_old_type
.ref
) {
121 s
->type
.ref
= type
->ref
;
122 s
->r
= r
| VT_CONST
| VT_SYM
;
123 s
->type
.t
|= VT_EXTERN
;
124 } else if (!is_compatible_types(&s
->type
, type
)) {
125 error("incompatible types for redefinition of '%s'",
126 get_tok_str(v
, NULL
));
131 /* push a reference to global symbol v */
132 static void vpush_global_sym(CType
*type
, int v
)
137 sym
= external_global_sym(v
, type
, 0);
139 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
143 void vset(CType
*type
, int r
, int v
)
148 vsetc(type
, r
, &cval
);
151 void vseti(int r
, int v
)
167 void vpushv(SValue
*v
)
169 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
170 error("memory full");
180 /* save r to the memory stack, and mark it as being free */
183 int l
, saved
, size
, align
;
187 /* modify all stack values */
190 for(p
=vstack
;p
<=vtop
;p
++) {
191 if ((p
->r
& VT_VALMASK
) == r
||
192 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
193 /* must save value on stack if not already done */
195 /* NOTE: must reload 'r' because r might be equal to r2 */
196 r
= p
->r
& VT_VALMASK
;
197 /* store register in the stack */
199 if ((p
->r
& VT_LVAL
) ||
200 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
201 #ifdef TCC_TARGET_X86_64
202 type
= &char_pointer_type
;
206 size
= type_size(type
, &align
);
207 loc
= (loc
- size
) & -align
;
209 sv
.r
= VT_LOCAL
| VT_LVAL
;
212 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
213 /* x86 specific: need to pop fp register ST0 if saved */
215 o(0xd8dd); /* fstp %st(0) */
218 #ifndef TCC_TARGET_X86_64
219 /* special long long case */
220 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
228 /* mark that stack entry as being saved on the stack */
229 if (p
->r
& VT_LVAL
) {
230 /* also clear the bounded flag because the
231 relocation address of the function was stored in
233 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
235 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
243 /* find a register of class 'rc2' with at most one reference on stack.
244 * If none, call get_reg(rc) */
245 int get_reg_ex(int rc
, int rc2
)
250 for(r
=0;r
<NB_REGS
;r
++) {
251 if (reg_classes
[r
] & rc2
) {
254 for(p
= vstack
; p
<= vtop
; p
++) {
255 if ((p
->r
& VT_VALMASK
) == r
||
256 (p
->r2
& VT_VALMASK
) == r
)
266 /* find a free register of class 'rc'. If none, save one register */
272 /* find a free register */
273 for(r
=0;r
<NB_REGS
;r
++) {
274 if (reg_classes
[r
] & rc
) {
275 for(p
=vstack
;p
<=vtop
;p
++) {
276 if ((p
->r
& VT_VALMASK
) == r
||
277 (p
->r2
& VT_VALMASK
) == r
)
285 /* no register left : free the first one on the stack (VERY
286 IMPORTANT to start from the bottom to ensure that we don't
287 spill registers used in gen_opi()) */
288 for(p
=vstack
;p
<=vtop
;p
++) {
289 r
= p
->r
& VT_VALMASK
;
290 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
292 /* also look at second register (if long long) */
293 r
= p
->r2
& VT_VALMASK
;
294 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
300 /* Should never comes here */
304 /* save registers up to (vtop - n) stack entry */
305 void save_regs(int n
)
310 for(p
= vstack
;p
<= p1
; p
++) {
311 r
= p
->r
& VT_VALMASK
;
318 /* move register 's' to 'r', and flush previous value of r to memory
320 void move_reg(int r
, int s
)
333 /* get address of vtop (vtop MUST BE an lvalue) */
337 /* tricky: if saved lvalue, then we can go back to lvalue */
338 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
339 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
342 #ifdef CONFIG_TCC_BCHECK
343 /* generate lvalue bound code */
349 vtop
->r
&= ~VT_MUSTBOUND
;
350 /* if lvalue, then use checking code before dereferencing */
351 if (vtop
->r
& VT_LVAL
) {
352 /* if not VT_BOUNDED value, then make one */
353 if (!(vtop
->r
& VT_BOUNDED
)) {
354 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
355 /* must save type because we must set it to int to get pointer */
357 vtop
->type
.t
= VT_INT
;
360 gen_bounded_ptr_add();
361 vtop
->r
|= lval_type
;
364 /* then check for dereferencing */
365 gen_bounded_ptr_deref();
370 /* store vtop a register belonging to class 'rc'. lvalues are
371 converted to values. Cannot be used if cannot be converted to
372 register value (such as structures). */
375 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
377 /* NOTE: get_reg can modify vstack[] */
378 if (vtop
->type
.t
& VT_BITFIELD
) {
381 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
382 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
383 /* remove bit field info to avoid loops */
384 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
385 /* cast to int to propagate signedness in following ops */
386 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
391 if((vtop
->type
.t
& VT_UNSIGNED
) ||
392 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
393 type
.t
|= VT_UNSIGNED
;
395 /* generate shifts */
396 vpushi(bits
- (bit_pos
+ bit_size
));
398 vpushi(bits
- bit_size
);
399 /* NOTE: transformed to SHR if unsigned */
403 if (is_float(vtop
->type
.t
) &&
404 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
407 unsigned long offset
;
408 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
412 /* XXX: unify with initializers handling ? */
413 /* CPUs usually cannot use float constants, so we store them
414 generically in data segment */
415 size
= type_size(&vtop
->type
, &align
);
416 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
417 data_section
->data_offset
= offset
;
418 /* XXX: not portable yet */
419 #if defined(__i386__) || defined(__x86_64__)
420 /* Zero pad x87 tenbyte long doubles */
421 if (size
== LDOUBLE_SIZE
)
422 vtop
->c
.tab
[2] &= 0xffff;
424 ptr
= section_ptr_add(data_section
, size
);
426 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
430 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
434 ptr
[i
] = vtop
->c
.tab
[i
];
435 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
436 vtop
->r
|= VT_LVAL
| VT_SYM
;
440 #ifdef CONFIG_TCC_BCHECK
441 if (vtop
->r
& VT_MUSTBOUND
)
445 r
= vtop
->r
& VT_VALMASK
;
449 /* need to reload if:
451 - lvalue (need to dereference pointer)
452 - already a register, but not in the right class */
454 (vtop
->r
& VT_LVAL
) ||
455 !(reg_classes
[r
] & rc
) ||
456 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
457 !(reg_classes
[vtop
->r2
] & rc2
))) {
459 #ifndef TCC_TARGET_X86_64
460 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
462 unsigned long long ll
;
463 /* two register type load : expand to two words
465 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
468 vtop
->c
.ui
= ll
; /* first word */
470 vtop
->r
= r
; /* save register value */
471 vpushi(ll
>> 32); /* second word */
472 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
473 (vtop
->r
& VT_LVAL
)) {
474 /* We do not want to modifier the long long
475 pointer here, so the safest (and less
476 efficient) is to save all the other registers
477 in the stack. XXX: totally inefficient. */
479 /* load from memory */
482 vtop
[-1].r
= r
; /* save register value */
483 /* increment pointer to get second word */
484 vtop
->type
.t
= VT_INT
;
493 vtop
[-1].r
= r
; /* save register value */
494 vtop
->r
= vtop
[-1].r2
;
496 /* allocate second register */
500 /* write second register */
504 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
506 /* lvalue of scalar type : need to use lvalue type
507 because of possible cast */
510 /* compute memory access type */
511 if (vtop
->r
& VT_LVAL_BYTE
)
513 else if (vtop
->r
& VT_LVAL_SHORT
)
515 if (vtop
->r
& VT_LVAL_UNSIGNED
)
519 /* restore wanted type */
522 /* one register type load */
527 #ifdef TCC_TARGET_C67
528 /* uses register pairs for doubles */
529 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
536 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
537 void gv2(int rc1
, int rc2
)
541 /* generate more generic register first. But VT_JMP or VT_CMP
542 values must be generated first in all cases to avoid possible
544 v
= vtop
[0].r
& VT_VALMASK
;
545 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
550 /* test if reload is needed for first register */
551 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
561 /* test if reload is needed for first register */
562 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
568 /* wrapper around RC_FRET to return a register by type */
571 #ifdef TCC_TARGET_X86_64
572 if (t
== VT_LDOUBLE
) {
579 /* wrapper around REG_FRET to return a register by type */
582 #ifdef TCC_TARGET_X86_64
583 if (t
== VT_LDOUBLE
) {
590 /* expand long long on stack in two int registers */
595 u
= vtop
->type
.t
& VT_UNSIGNED
;
598 vtop
[0].r
= vtop
[-1].r2
;
599 vtop
[0].r2
= VT_CONST
;
600 vtop
[-1].r2
= VT_CONST
;
601 vtop
[0].type
.t
= VT_INT
| u
;
602 vtop
[-1].type
.t
= VT_INT
| u
;
605 #ifdef TCC_TARGET_ARM
606 /* expand long long on stack */
607 void lexpand_nr(void)
611 u
= vtop
->type
.t
& VT_UNSIGNED
;
614 vtop
->type
.t
= VT_INT
| u
;
615 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
617 vtop
[-1].c
.ui
= vtop
->c
.ull
;
618 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
620 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
622 vtop
->r
= vtop
[-1].r
;
623 } else if (v
> VT_CONST
) {
627 vtop
->r
= vtop
[-1].r2
;
628 vtop
[-1].r2
= VT_CONST
;
629 vtop
[-1].type
.t
= VT_INT
| u
;
633 /* build a long long from two ints */
637 vtop
[-1].r2
= vtop
[0].r
;
642 /* rotate n first stack elements to the bottom
643 I1 ... In -> I2 ... In I1 [top is right]
656 /* rotate n first stack elements to the top
657 I1 ... In -> In I1 ... I(n-1) [top is right]
665 for(i
= 0;i
< n
- 1; i
++)
666 vtop
[-i
] = vtop
[-i
- 1];
670 #ifdef TCC_TARGET_ARM
671 /* like vrott but in other direction
672 In ... I1 -> I(n-1) ... I1 In [top is right]
680 for(i
= n
- 1; i
> 0; i
--)
681 vtop
[-i
] = vtop
[-i
+ 1];
686 /* pop stack value */
690 v
= vtop
->r
& VT_VALMASK
;
691 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
692 /* for x86, we need to pop the FP stack */
693 if (v
== TREG_ST0
&& !nocode_wanted
) {
694 o(0xd8dd); /* fstp %st(0) */
697 if (v
== VT_JMP
|| v
== VT_JMPI
) {
698 /* need to put correct jump if && or || without test */
704 /* convert stack entry to register and duplicate its value in another
712 if ((t
& VT_BTYPE
) == VT_LLONG
) {
719 /* stack: H L L1 H1 */
727 /* duplicate value */
732 #ifdef TCC_TARGET_X86_64
733 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
743 load(r1
, &sv
); /* move r to r1 */
745 /* duplicates value */
751 #ifndef TCC_TARGET_X86_64
752 /* generate CPU independent (unsigned) long long operations */
755 int t
, a
, b
, op1
, c
, i
;
757 unsigned short reg_iret
= REG_IRET
;
758 unsigned short reg_lret
= REG_LRET
;
767 func
= TOK___udivdi3
;
773 func
= TOK___umoddi3
;
780 /* call generic long long function */
781 vpush_global_sym(&func_old_type
, func
);
799 /* stack: L1 H1 L2 H2 */
807 /* stack: H1 H2 L1 L2 */
813 /* stack: H1 H2 L1 L2 ML MH */
816 /* stack: ML MH H1 H2 L1 L2 */
820 /* stack: ML MH H1 L2 H2 L1 */
825 /* stack: ML MH M1 M2 */
828 } else if (op
== '+' || op
== '-') {
829 /* XXX: add non carry method too (for MIPS or alpha) */
835 /* stack: H1 H2 (L1 op L2) */
838 gen_op(op1
+ 1); /* TOK_xxxC2 */
841 /* stack: H1 H2 (L1 op L2) */
844 /* stack: (L1 op L2) H1 H2 */
846 /* stack: (L1 op L2) (H1 op H2) */
854 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
859 /* stack: L H shift */
861 /* constant: simpler */
862 /* NOTE: all comments are for SHL. the other cases are
863 done by swaping words */
907 /* XXX: should provide a faster fallback on x86 ? */
910 func
= TOK___ashrdi3
;
913 func
= TOK___lshrdi3
;
916 func
= TOK___ashldi3
;
922 /* compare operations */
928 /* stack: L1 H1 L2 H2 */
932 /* stack: L1 L2 H1 H2 */
935 /* when values are equal, we need to compare low words. since
936 the jump is inverted, we invert the test too. */
939 else if (op1
== TOK_GT
)
941 else if (op1
== TOK_ULT
)
943 else if (op1
== TOK_UGT
)
952 /* generate non equal test */
953 /* XXX: NOT PORTABLE yet */
957 #if defined(TCC_TARGET_I386)
959 #elif defined(TCC_TARGET_ARM)
961 o(0x1A000000 | encbranch(ind
, 0, 1));
962 #elif defined(TCC_TARGET_C67)
963 error("not implemented");
969 /* compare low. Always unsigned */
973 else if (op1
== TOK_LE
)
975 else if (op1
== TOK_GT
)
977 else if (op1
== TOK_GE
)
988 /* handle integer constant optimizations and various machine
990 void gen_opic(int op
)
992 int c1
, c2
, t1
, t2
, n
;
995 typedef unsigned long long U
;
999 t1
= v1
->type
.t
& VT_BTYPE
;
1000 t2
= v2
->type
.t
& VT_BTYPE
;
1004 else if (v1
->type
.t
& VT_UNSIGNED
)
1011 else if (v2
->type
.t
& VT_UNSIGNED
)
1016 /* currently, we cannot do computations with forward symbols */
1017 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1018 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1021 case '+': l1
+= l2
; break;
1022 case '-': l1
-= l2
; break;
1023 case '&': l1
&= l2
; break;
1024 case '^': l1
^= l2
; break;
1025 case '|': l1
|= l2
; break;
1026 case '*': l1
*= l2
; break;
1033 /* if division by zero, generate explicit division */
1036 error("division by zero in constant");
1040 default: l1
/= l2
; break;
1041 case '%': l1
%= l2
; break;
1042 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1043 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1046 case TOK_SHL
: l1
<<= l2
; break;
1047 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1048 case TOK_SAR
: l1
>>= l2
; break;
1050 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1051 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1052 case TOK_EQ
: l1
= l1
== l2
; break;
1053 case TOK_NE
: l1
= l1
!= l2
; break;
1054 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1055 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1056 case TOK_LT
: l1
= l1
< l2
; break;
1057 case TOK_GE
: l1
= l1
>= l2
; break;
1058 case TOK_LE
: l1
= l1
<= l2
; break;
1059 case TOK_GT
: l1
= l1
> l2
; break;
1061 case TOK_LAND
: l1
= l1
&& l2
; break;
1062 case TOK_LOR
: l1
= l1
|| l2
; break;
1069 /* if commutative ops, put c2 as constant */
1070 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1071 op
== '|' || op
== '*')) {
1073 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1074 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1076 /* Filter out NOP operations like x*1, x-0, x&-1... */
1077 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1080 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1081 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1087 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1088 /* try to use shifts instead of muls or divs */
1089 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1098 else if (op
== TOK_PDIV
)
1104 } else if (c2
&& (op
== '+' || op
== '-') &&
1105 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
)
1106 && !(vtop
[-1].sym
->type
.t
& VT_IMPORT
))
1108 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1109 /* symbol + constant case */
1116 if (!nocode_wanted
) {
1117 /* call low level op generator */
1118 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1129 /* generate a floating point operation with constant propagation */
1130 void gen_opif(int op
)
1138 /* currently, we cannot do computations with forward symbols */
1139 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1140 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1142 if (v1
->type
.t
== VT_FLOAT
) {
1145 } else if (v1
->type
.t
== VT_DOUBLE
) {
1153 /* NOTE: we only do constant propagation if finite number (not
1154 NaN or infinity) (ANSI spec) */
1155 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1159 case '+': f1
+= f2
; break;
1160 case '-': f1
-= f2
; break;
1161 case '*': f1
*= f2
; break;
1165 error("division by zero in constant");
1170 /* XXX: also handles tests ? */
1174 /* XXX: overflow test ? */
1175 if (v1
->type
.t
== VT_FLOAT
) {
1177 } else if (v1
->type
.t
== VT_DOUBLE
) {
1185 if (!nocode_wanted
) {
1193 static int pointed_size(CType
*type
)
1196 return type_size(pointed_type(type
), &align
);
1199 static inline int is_null_pointer(SValue
*p
)
1201 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1203 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1204 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1207 static inline int is_integer_btype(int bt
)
1209 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1210 bt
== VT_INT
|| bt
== VT_LLONG
);
1213 /* check types for comparison or substraction of pointers */
1214 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1216 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1219 /* null pointers are accepted for all comparisons as gcc */
1220 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1224 bt1
= type1
->t
& VT_BTYPE
;
1225 bt2
= type2
->t
& VT_BTYPE
;
1226 /* accept comparison between pointer and integer with a warning */
1227 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1228 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1229 warning("comparison between pointer and integer");
1233 /* both must be pointers or implicit function pointers */
1234 if (bt1
== VT_PTR
) {
1235 type1
= pointed_type(type1
);
1236 } else if (bt1
!= VT_FUNC
)
1237 goto invalid_operands
;
1239 if (bt2
== VT_PTR
) {
1240 type2
= pointed_type(type2
);
1241 } else if (bt2
!= VT_FUNC
) {
1243 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1245 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1246 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1250 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1251 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1252 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1253 /* gcc-like error if '-' is used */
1255 goto invalid_operands
;
1257 warning("comparison of distinct pointer types lacks a cast");
1261 /* generic gen_op: handles types problems */
1264 int u
, t1
, t2
, bt1
, bt2
, t
;
1267 t1
= vtop
[-1].type
.t
;
1268 t2
= vtop
[0].type
.t
;
1269 bt1
= t1
& VT_BTYPE
;
1270 bt2
= t2
& VT_BTYPE
;
1272 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1273 /* at least one operand is a pointer */
1274 /* relationnal op: must be both pointers */
1275 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1276 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1277 /* pointers are handled are unsigned */
1278 #ifdef TCC_TARGET_X86_64
1279 t
= VT_LLONG
| VT_UNSIGNED
;
1281 t
= VT_INT
| VT_UNSIGNED
;
1285 /* if both pointers, then it must be the '-' op */
1286 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1288 error("cannot use pointers here");
1289 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1290 /* XXX: check that types are compatible */
1291 u
= pointed_size(&vtop
[-1].type
);
1293 /* set to integer type */
1294 #ifdef TCC_TARGET_X86_64
1295 vtop
->type
.t
= VT_LLONG
;
1297 vtop
->type
.t
= VT_INT
;
1302 /* exactly one pointer : must be '+' or '-'. */
1303 if (op
!= '-' && op
!= '+')
1304 error("cannot use pointers here");
1305 /* Put pointer as first operand */
1306 if (bt2
== VT_PTR
) {
1310 type1
= vtop
[-1].type
;
1311 type1
.t
&= ~VT_ARRAY
;
1312 #ifdef TCC_TARGET_X86_64
1313 vpushll(pointed_size(&vtop
[-1].type
));
1315 /* XXX: cast to int ? (long long case) */
1316 vpushi(pointed_size(&vtop
[-1].type
));
1319 #ifdef CONFIG_TCC_BCHECK
1320 /* if evaluating constant expression, no code should be
1321 generated, so no bound check */
1322 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1323 /* if bounded pointers, we generate a special code to
1330 gen_bounded_ptr_add();
1336 /* put again type if gen_opic() swaped operands */
1339 } else if (is_float(bt1
) || is_float(bt2
)) {
1340 /* compute bigger type and do implicit casts */
1341 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1343 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1348 /* floats can only be used for a few operations */
1349 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1350 (op
< TOK_ULT
|| op
> TOK_GT
))
1351 error("invalid operands for binary operation");
1353 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1354 /* cast to biggest op */
1356 /* convert to unsigned if it does not fit in a long long */
1357 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1358 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1362 /* integer operations */
1364 /* convert to unsigned if it does not fit in an integer */
1365 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1366 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1369 /* XXX: currently, some unsigned operations are explicit, so
1370 we modify them here */
1371 if (t
& VT_UNSIGNED
) {
1378 else if (op
== TOK_LT
)
1380 else if (op
== TOK_GT
)
1382 else if (op
== TOK_LE
)
1384 else if (op
== TOK_GE
)
1391 /* special case for shifts and long long: we keep the shift as
1393 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1400 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1401 /* relationnal op: the result is an int */
1402 vtop
->type
.t
= VT_INT
;
1409 #ifndef TCC_TARGET_ARM
1410 /* generic itof for unsigned long long case */
1411 void gen_cvt_itof1(int t
)
1413 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1414 (VT_LLONG
| VT_UNSIGNED
)) {
1417 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1418 #if LDOUBLE_SIZE != 8
1419 else if (t
== VT_LDOUBLE
)
1420 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1423 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1427 vtop
->r
= reg_fret(t
);
1434 /* generic ftoi for unsigned long long case */
1435 void gen_cvt_ftoi1(int t
)
1439 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1440 /* not handled natively */
1441 st
= vtop
->type
.t
& VT_BTYPE
;
1443 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1444 #if LDOUBLE_SIZE != 8
1445 else if (st
== VT_LDOUBLE
)
1446 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1449 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1454 vtop
->r2
= REG_LRET
;
1460 /* force char or short cast */
1461 void force_charshort_cast(int t
)
1465 /* XXX: add optimization if lvalue : just change type and offset */
1470 if (t
& VT_UNSIGNED
) {
1471 vpushi((1 << bits
) - 1);
1477 /* result must be signed or the SAR is converted to an SHL
1478 This was not the case when "t" was a signed short
1479 and the last value on the stack was an unsigned int */
1480 vtop
->type
.t
&= ~VT_UNSIGNED
;
1486 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1487 static void gen_cast(CType
*type
)
1489 int sbt
, dbt
, sf
, df
, c
, p
;
1491 /* special delayed cast for char/short */
1492 /* XXX: in some cases (multiple cascaded casts), it may still
1494 if (vtop
->r
& VT_MUSTCAST
) {
1495 vtop
->r
&= ~VT_MUSTCAST
;
1496 force_charshort_cast(vtop
->type
.t
);
1499 /* bitfields first get cast to ints */
1500 if (vtop
->type
.t
& VT_BITFIELD
) {
1504 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1505 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1510 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1511 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1513 /* constant case: we can do it now */
1514 /* XXX: in ISOC, cannot do it if error in convert */
1515 if (sbt
== VT_FLOAT
)
1516 vtop
->c
.ld
= vtop
->c
.f
;
1517 else if (sbt
== VT_DOUBLE
)
1518 vtop
->c
.ld
= vtop
->c
.d
;
1521 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1522 if (sbt
& VT_UNSIGNED
)
1523 vtop
->c
.ld
= vtop
->c
.ull
;
1525 vtop
->c
.ld
= vtop
->c
.ll
;
1527 if (sbt
& VT_UNSIGNED
)
1528 vtop
->c
.ld
= vtop
->c
.ui
;
1530 vtop
->c
.ld
= vtop
->c
.i
;
1533 if (dbt
== VT_FLOAT
)
1534 vtop
->c
.f
= (float)vtop
->c
.ld
;
1535 else if (dbt
== VT_DOUBLE
)
1536 vtop
->c
.d
= (double)vtop
->c
.ld
;
1537 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1538 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1539 } else if (sf
&& dbt
== VT_BOOL
) {
1540 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1543 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1544 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1545 vtop
->c
.ll
= vtop
->c
.ull
;
1546 else if (sbt
& VT_UNSIGNED
)
1547 vtop
->c
.ll
= vtop
->c
.ui
;
1548 else if (sbt
!= VT_LLONG
)
1549 vtop
->c
.ll
= vtop
->c
.i
;
1551 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1552 vtop
->c
.ull
= vtop
->c
.ll
;
1553 else if (dbt
== VT_BOOL
)
1554 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1555 else if (dbt
!= VT_LLONG
) {
1557 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1559 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1562 if(dbt
& VT_UNSIGNED
)
1563 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1565 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1568 } else if (p
&& dbt
== VT_BOOL
) {
1571 } else if (!nocode_wanted
) {
1572 /* non constant case: generate code */
1574 /* convert from fp to fp */
1577 /* convert int to fp */
1580 /* convert fp to int */
1581 if (dbt
== VT_BOOL
) {
1585 /* we handle char/short/etc... with generic code */
1586 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1587 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1591 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1592 /* additional cast for char/short... */
1597 #ifndef TCC_TARGET_X86_64
1598 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1599 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1600 /* scalar to long long */
1601 /* machine independent conversion */
1603 /* generate high word */
1604 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1608 if (sbt
== VT_PTR
) {
1609 /* cast from pointer to int before we apply
1610 shift operation, which pointers don't support*/
1611 gen_cast(&int_type
);
1617 /* patch second register */
1618 vtop
[-1].r2
= vtop
->r
;
1622 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1623 (dbt
& VT_BTYPE
) == VT_PTR
) {
1624 /* XXX: not sure if this is perfect... need more tests */
1625 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1627 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1628 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1629 /* x86_64 specific: movslq */
1631 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1635 } else if (dbt
== VT_BOOL
) {
1636 /* scalar to bool */
1639 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1640 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1641 if (sbt
== VT_PTR
) {
1642 vtop
->type
.t
= VT_INT
;
1643 warning("nonportable conversion from pointer to char/short");
1645 force_charshort_cast(dbt
);
1646 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1648 if (sbt
== VT_LLONG
) {
1649 /* from long long: just take low order word */
1653 /* if lvalue and single word type, nothing to do because
1654 the lvalue already contains the real type size (see
1655 VT_LVAL_xxx constants) */
1658 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1659 /* if we are casting between pointer types,
1660 we must update the VT_LVAL_xxx size */
1661 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1662 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1667 /* return type size. Put alignment at 'a' */
1668 static int type_size(CType
*type
, int *a
)
1673 bt
= type
->t
& VT_BTYPE
;
1674 if (bt
== VT_STRUCT
) {
1679 } else if (bt
== VT_PTR
) {
1680 if (type
->t
& VT_ARRAY
) {
1684 ts
= type_size(&s
->type
, a
);
1686 if (ts
< 0 && s
->c
< 0)
1694 } else if (bt
== VT_LDOUBLE
) {
1696 return LDOUBLE_SIZE
;
1697 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1698 #ifdef TCC_TARGET_I386
1699 #ifdef TCC_TARGET_PE
1704 #elif defined(TCC_TARGET_ARM)
1714 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1717 } else if (bt
== VT_SHORT
) {
1721 /* char, void, function, _Bool */
1727 /* return the pointed type of t */
1728 static inline CType
*pointed_type(CType
*type
)
1730 return &type
->ref
->type
;
1733 /* modify type so that its it is a pointer to type. */
1734 void mk_pointer(CType
*type
)
1737 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1738 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1742 /* compare function types. OLD functions match any new functions */
1743 static int is_compatible_func(CType
*type1
, CType
*type2
)
1749 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1751 /* check func_call */
1752 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1754 /* XXX: not complete */
1755 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
1759 while (s1
!= NULL
) {
1762 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
1772 /* return true if type1 and type2 are the same. If unqualified is
1773 true, qualifiers on the types are ignored.
1775 - enums are not checked as gcc __builtin_types_compatible_p ()
1777 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
1781 t1
= type1
->t
& VT_TYPE
;
1782 t2
= type2
->t
& VT_TYPE
;
1784 /* strip qualifiers before comparing */
1785 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1786 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1788 /* XXX: bitfields ? */
1791 /* test more complicated cases */
1792 bt1
= t1
& VT_BTYPE
;
1793 if (bt1
== VT_PTR
) {
1794 type1
= pointed_type(type1
);
1795 type2
= pointed_type(type2
);
1796 return is_compatible_types(type1
, type2
);
1797 } else if (bt1
== VT_STRUCT
) {
1798 return (type1
->ref
== type2
->ref
);
1799 } else if (bt1
== VT_FUNC
) {
1800 return is_compatible_func(type1
, type2
);
1806 /* return true if type1 and type2 are exactly the same (including
1809 static int is_compatible_types(CType
*type1
, CType
*type2
)
1811 return compare_types(type1
,type2
,0);
1814 /* return true if type1 and type2 are the same (ignoring qualifiers).
1816 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
1818 return compare_types(type1
,type2
,1);
1821 /* print a type. If 'varstr' is not NULL, then the variable is also
1822 printed in the type */
1824 /* XXX: add array and function pointers */
1825 void type_to_str(char *buf
, int buf_size
,
1826 CType
*type
, const char *varstr
)
1833 t
= type
->t
& VT_TYPE
;
1836 if (t
& VT_CONSTANT
)
1837 pstrcat(buf
, buf_size
, "const ");
1838 if (t
& VT_VOLATILE
)
1839 pstrcat(buf
, buf_size
, "volatile ");
1840 if (t
& VT_UNSIGNED
)
1841 pstrcat(buf
, buf_size
, "unsigned ");
1871 tstr
= "long double";
1873 pstrcat(buf
, buf_size
, tstr
);
1877 if (bt
== VT_STRUCT
)
1881 pstrcat(buf
, buf_size
, tstr
);
1882 v
= type
->ref
->v
& ~SYM_STRUCT
;
1883 if (v
>= SYM_FIRST_ANOM
)
1884 pstrcat(buf
, buf_size
, "<anonymous>");
1886 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
1890 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
1891 pstrcat(buf
, buf_size
, "(");
1893 while (sa
!= NULL
) {
1894 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
1895 pstrcat(buf
, buf_size
, buf1
);
1898 pstrcat(buf
, buf_size
, ", ");
1900 pstrcat(buf
, buf_size
, ")");
1904 pstrcpy(buf1
, sizeof(buf1
), "*");
1906 pstrcat(buf1
, sizeof(buf1
), varstr
);
1907 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
1911 pstrcat(buf
, buf_size
, " ");
1912 pstrcat(buf
, buf_size
, varstr
);
1917 /* verify type compatibility to store vtop in 'dt' type, and generate
1919 static void gen_assign_cast(CType
*dt
)
1921 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
1922 char buf1
[256], buf2
[256];
1925 st
= &vtop
->type
; /* source type */
1926 dbt
= dt
->t
& VT_BTYPE
;
1927 sbt
= st
->t
& VT_BTYPE
;
1928 if (dt
->t
& VT_CONSTANT
)
1929 warning("assignment of read-only location");
1932 /* special cases for pointers */
1933 /* '0' can also be a pointer */
1934 if (is_null_pointer(vtop
))
1936 /* accept implicit pointer to integer cast with warning */
1937 if (is_integer_btype(sbt
)) {
1938 warning("assignment makes pointer from integer without a cast");
1941 type1
= pointed_type(dt
);
1942 /* a function is implicitely a function pointer */
1943 if (sbt
== VT_FUNC
) {
1944 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
1945 !is_compatible_types(pointed_type(dt
), st
))
1946 warning("assignment from incompatible pointer type");
1951 type2
= pointed_type(st
);
1952 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1953 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
1954 /* void * can match anything */
1956 /* exact type match, except for unsigned */
1959 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1960 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1961 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
1962 warning("assignment from incompatible pointer type");
1964 /* check const and volatile */
1965 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
1966 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
1967 warning("assignment discards qualifiers from pointer target type");
1973 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
1974 warning("assignment makes integer from pointer without a cast");
1976 /* XXX: more tests */
1981 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1982 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1983 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1985 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
1986 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
1987 error("cannot cast '%s' to '%s'", buf1
, buf2
);
1995 /* store vtop in lvalue pushed on stack */
1998 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2000 ft
= vtop
[-1].type
.t
;
2001 sbt
= vtop
->type
.t
& VT_BTYPE
;
2002 dbt
= ft
& VT_BTYPE
;
2003 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2004 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2005 /* optimize char/short casts */
2006 delayed_cast
= VT_MUSTCAST
;
2007 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2008 /* XXX: factorize */
2009 if (ft
& VT_CONSTANT
)
2010 warning("assignment of read-only location");
2013 if (!(ft
& VT_BITFIELD
))
2014 gen_assign_cast(&vtop
[-1].type
);
2017 if (sbt
== VT_STRUCT
) {
2018 /* if structure, only generate pointer */
2019 /* structure assignment : generate memcpy */
2020 /* XXX: optimize if small size */
2021 if (!nocode_wanted
) {
2022 size
= type_size(&vtop
->type
, &align
);
2026 vtop
->type
.t
= VT_PTR
;
2029 /* address of memcpy() */
2032 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2033 else if(!(align
& 3))
2034 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2037 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2042 vtop
->type
.t
= VT_PTR
;
2051 /* leave source on stack */
2052 } else if (ft
& VT_BITFIELD
) {
2053 /* bitfield store handling */
2054 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2055 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2056 /* remove bit field info to avoid loops */
2057 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2059 /* duplicate source into other register */
2064 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2065 gen_cast(&vtop
[-1].type
);
2066 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2069 /* duplicate destination */
2071 vtop
[-1] = vtop
[-2];
2073 /* mask and shift source */
2074 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2075 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2076 vpushll((1ULL << bit_size
) - 1ULL);
2078 vpushi((1 << bit_size
) - 1);
2084 /* load destination, mask and or with source */
2086 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2087 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2089 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2096 /* pop off shifted source from "duplicate source..." above */
2100 #ifdef CONFIG_TCC_BCHECK
2101 /* bound check case */
2102 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2108 if (!nocode_wanted
) {
2112 #ifdef TCC_TARGET_X86_64
2113 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2118 r
= gv(rc
); /* generate value */
2119 /* if lvalue was saved on stack, must read it */
2120 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2122 t
= get_reg(RC_INT
);
2123 #ifdef TCC_TARGET_X86_64
2128 sv
.r
= VT_LOCAL
| VT_LVAL
;
2129 sv
.c
.ul
= vtop
[-1].c
.ul
;
2131 vtop
[-1].r
= t
| VT_LVAL
;
2134 #ifndef TCC_TARGET_X86_64
2135 /* two word case handling : store second register at word + 4 */
2136 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2138 /* convert to int to increment easily */
2139 vtop
->type
.t
= VT_INT
;
2145 /* XXX: it works because r2 is spilled last ! */
2146 store(vtop
->r2
, vtop
- 1);
2151 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2152 vtop
->r
|= delayed_cast
;
2156 /* post defines POST/PRE add. c is the token ++ or -- */
2157 void inc(int post
, int c
)
2160 vdup(); /* save lvalue */
2162 gv_dup(); /* duplicate value */
2167 vpushi(c
- TOK_MID
);
2169 vstore(); /* store value */
2171 vpop(); /* if post op, return saved value */
2174 /* Parse GNUC __attribute__ extension. Currently, the following
2175 extensions are recognized:
2176 - aligned(n) : set data/function alignment.
2177 - packed : force data alignment to 1
2178 - section(x) : generate data/code in this section.
2179 - unused : currently ignored, but may be used someday.
2180 - regparm(n) : pass function parameters in registers (i386 only)
2182 static void parse_attribute(AttributeDef
*ad
)
2186 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2190 while (tok
!= ')') {
2191 if (tok
< TOK_IDENT
)
2192 expect("attribute name");
2200 expect("section name");
2201 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2210 if (n
<= 0 || (n
& (n
- 1)) != 0)
2211 error("alignment must be a positive power of two");
2224 /* currently, no need to handle it because tcc does not
2225 track unused objects */
2229 /* currently, no need to handle it because tcc does not
2230 track unused objects */
2235 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
2240 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
2242 #ifdef TCC_TARGET_I386
2252 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
2258 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
2262 FUNC_EXPORT(ad
->func_attr
) = 1;
2265 FUNC_IMPORT(ad
->func_attr
) = 1;
2268 if (tcc_state
->warn_unsupported
)
2269 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2270 /* skip parameters */
2272 int parenthesis
= 0;
2276 else if (tok
== ')')
2279 } while (parenthesis
&& tok
!= -1);
2292 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2293 static void struct_decl(CType
*type
, int u
)
2295 int a
, v
, size
, align
, maxalign
, c
, offset
;
2296 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2297 Sym
*s
, *ss
, *ass
, **ps
;
2301 a
= tok
; /* save decl type */
2306 /* struct already defined ? return it */
2308 expect("struct/union/enum name");
2312 error("invalid type");
2319 /* we put an undefined size for struct/union */
2320 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2321 s
->r
= 0; /* default alignment is zero as gcc */
2322 /* put struct/union/enum name in type */
2330 error("struct/union/enum already defined");
2331 /* cannot be empty */
2333 /* non empty enums are not allowed */
2334 if (a
== TOK_ENUM
) {
2338 expect("identifier");
2344 /* enum symbols have static storage */
2345 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2346 ss
->type
.t
|= VT_STATIC
;
2351 /* NOTE: we accept a trailing comma */
2362 while (tok
!= '}') {
2363 parse_btype(&btype
, &ad
);
2369 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2370 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2371 expect("identifier");
2372 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2373 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2374 error("invalid type for '%s'",
2375 get_tok_str(v
, NULL
));
2379 bit_size
= expr_const();
2380 /* XXX: handle v = 0 case for messages */
2382 error("negative width in bit-field '%s'",
2383 get_tok_str(v
, NULL
));
2384 if (v
&& bit_size
== 0)
2385 error("zero width for bit-field '%s'",
2386 get_tok_str(v
, NULL
));
2388 size
= type_size(&type1
, &align
);
2390 if (align
< ad
.aligned
)
2392 } else if (ad
.packed
) {
2394 } else if (*tcc_state
->pack_stack_ptr
) {
2395 if (align
> *tcc_state
->pack_stack_ptr
)
2396 align
= *tcc_state
->pack_stack_ptr
;
2399 if (bit_size
>= 0) {
2400 bt
= type1
.t
& VT_BTYPE
;
2407 error("bitfields must have scalar type");
2409 if (bit_size
> bsize
) {
2410 error("width of '%s' exceeds its type",
2411 get_tok_str(v
, NULL
));
2412 } else if (bit_size
== bsize
) {
2413 /* no need for bit fields */
2415 } else if (bit_size
== 0) {
2416 /* XXX: what to do if only padding in a
2418 /* zero size: means to pad */
2421 /* we do not have enough room ?
2422 did the type change?
2424 if ((bit_pos
+ bit_size
) > bsize
||
2425 bt
!= prevbt
|| a
== TOK_UNION
)
2428 /* XXX: handle LSB first */
2429 type1
.t
|= VT_BITFIELD
|
2430 (bit_pos
<< VT_STRUCT_SHIFT
) |
2431 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2432 bit_pos
+= bit_size
;
2438 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2439 /* add new memory data only if starting
2441 if (lbit_pos
== 0) {
2442 if (a
== TOK_STRUCT
) {
2443 c
= (c
+ align
- 1) & -align
;
2452 if (align
> maxalign
)
2456 printf("add field %s offset=%d",
2457 get_tok_str(v
, NULL
), offset
);
2458 if (type1
.t
& VT_BITFIELD
) {
2459 printf(" pos=%d size=%d",
2460 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2461 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2466 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2468 while ((ass
= ass
->next
) != NULL
) {
2469 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2474 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2478 if (tok
== ';' || tok
== TOK_EOF
)
2485 /* store size and alignment */
2486 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2492 /* return 0 if no type declaration. otherwise, return the basic type
2495 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2497 int t
, u
, type_found
, typespec_found
, typedef_found
;
2501 memset(ad
, 0, sizeof(AttributeDef
));
2509 /* currently, we really ignore extension */
2519 if ((t
& VT_BTYPE
) != 0)
2520 error("too many basic types");
2536 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2537 #ifndef TCC_TARGET_PE
2538 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2540 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2541 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2555 if ((t
& VT_BTYPE
) == VT_LONG
) {
2556 #ifdef TCC_TARGET_PE
2557 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2559 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2567 struct_decl(&type1
, VT_ENUM
);
2570 type
->ref
= type1
.ref
;
2574 struct_decl(&type1
, VT_STRUCT
);
2577 /* type modifiers */
2630 /* GNUC attribute */
2631 case TOK_ATTRIBUTE1
:
2632 case TOK_ATTRIBUTE2
:
2633 parse_attribute(ad
);
2640 parse_expr_type(&type1
);
2643 if (typespec_found
|| typedef_found
)
2646 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2649 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2650 type
->ref
= s
->type
.ref
;
2658 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2659 error("signed and unsigned modifier");
2660 if (tcc_state
->char_is_unsigned
) {
2661 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2666 /* long is never used as type */
2667 if ((t
& VT_BTYPE
) == VT_LONG
)
2668 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2669 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2671 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2677 /* convert a function parameter type (array to pointer and function to
2678 function pointer) */
2679 static inline void convert_parameter_type(CType
*pt
)
2681 /* remove const and volatile qualifiers (XXX: const could be used
2682 to indicate a const function parameter */
2683 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2684 /* array must be transformed to pointer according to ANSI C */
2686 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2691 static void post_type(CType
*type
, AttributeDef
*ad
)
2693 int n
, l
, t1
, arg_size
, align
;
2694 Sym
**plast
, *s
, *first
;
2699 /* function declaration */
2707 /* read param name and compute offset */
2708 if (l
!= FUNC_OLD
) {
2709 if (!parse_btype(&pt
, &ad1
)) {
2711 error("invalid type");
2718 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2720 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2721 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2722 error("parameter declared as void");
2723 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
2728 expect("identifier");
2732 convert_parameter_type(&pt
);
2733 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
2739 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
2746 /* if no parameters, then old type prototype */
2750 t1
= type
->t
& VT_STORAGE
;
2751 /* NOTE: const is ignored in returned type as it has a special
2752 meaning in gcc / C++ */
2753 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
2754 post_type(type
, ad
);
2755 /* we push a anonymous symbol which will contain the function prototype */
2756 FUNC_ARGS(ad
->func_attr
) = arg_size
;
2757 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
2759 type
->t
= t1
| VT_FUNC
;
2761 } else if (tok
== '[') {
2762 /* array definition */
2764 if (tok
== TOK_RESTRICT1
)
2770 error("invalid array size");
2773 /* parse next post type */
2774 t1
= type
->t
& VT_STORAGE
;
2775 type
->t
&= ~VT_STORAGE
;
2776 post_type(type
, ad
);
2778 /* we push a anonymous symbol which will contain the array
2780 s
= sym_push(SYM_FIELD
, type
, 0, n
);
2781 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
2786 /* Parse a type declaration (except basic type), and return the type
2787 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2788 expected. 'type' should contain the basic type. 'ad' is the
2789 attribute definition of the basic type. It can be modified by
2792 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
2795 CType type1
, *type2
;
2798 while (tok
== '*') {
2806 qualifiers
|= VT_CONSTANT
;
2811 qualifiers
|= VT_VOLATILE
;
2819 type
->t
|= qualifiers
;
2822 /* XXX: clarify attribute handling */
2823 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2824 parse_attribute(ad
);
2826 /* recursive type */
2827 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2828 type1
.t
= 0; /* XXX: same as int */
2831 /* XXX: this is not correct to modify 'ad' at this point, but
2832 the syntax is not clear */
2833 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2834 parse_attribute(ad
);
2835 type_decl(&type1
, ad
, v
, td
);
2838 /* type identifier */
2839 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
2843 if (!(td
& TYPE_ABSTRACT
))
2844 expect("identifier");
2848 post_type(type
, ad
);
2849 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2850 parse_attribute(ad
);
2853 /* append type at the end of type1 */
2866 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2867 static int lvalue_type(int t
)
2872 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
2874 else if (bt
== VT_SHORT
)
2878 if (t
& VT_UNSIGNED
)
2879 r
|= VT_LVAL_UNSIGNED
;
2883 /* indirection with full error checking and bound check */
2884 static void indir(void)
2886 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
2887 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
2891 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
2893 vtop
->type
= *pointed_type(&vtop
->type
);
2894 /* Arrays and functions are never lvalues */
2895 if (!(vtop
->type
.t
& VT_ARRAY
)
2896 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
2897 vtop
->r
|= lvalue_type(vtop
->type
.t
);
2898 /* if bound checking, the referenced pointer must be checked */
2899 if (tcc_state
->do_bounds_check
)
2900 vtop
->r
|= VT_MUSTBOUND
;
2904 /* pass a parameter to a function and do type checking and casting */
2905 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2910 func_type
= func
->c
;
2911 if (func_type
== FUNC_OLD
||
2912 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2913 /* default casting : only need to convert float to double */
2914 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2918 } else if (arg
== NULL
) {
2919 error("too many arguments to function");
2922 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2923 gen_assign_cast(&type
);
2927 /* parse an expression of the form '(type)' or '(expr)' and return its
2929 static void parse_expr_type(CType
*type
)
2935 if (parse_btype(type
, &ad
)) {
2936 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2943 static void parse_type(CType
*type
)
2948 if (!parse_btype(type
, &ad
)) {
2951 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2954 static void vpush_tokc(int t
)
2958 vsetc(&type
, VT_CONST
, &tokc
);
2961 static void unary(void)
2963 int n
, t
, align
, size
, r
;
2968 /* XXX: GCC 2.95.3 does not generate a table although it should be
2982 vpush_tokc(VT_INT
| VT_UNSIGNED
);
2986 vpush_tokc(VT_LLONG
);
2990 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
2994 vpush_tokc(VT_FLOAT
);
2998 vpush_tokc(VT_DOUBLE
);
3002 vpush_tokc(VT_LDOUBLE
);
3005 case TOK___FUNCTION__
:
3007 goto tok_identifier
;
3013 /* special function name identifier */
3014 len
= strlen(funcname
) + 1;
3015 /* generate char[len] type */
3020 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3021 ptr
= section_ptr_add(data_section
, len
);
3022 memcpy(ptr
, funcname
, len
);
3027 #ifdef TCC_TARGET_PE
3028 t
= VT_SHORT
| VT_UNSIGNED
;
3034 /* string parsing */
3037 if (tcc_state
->warn_write_strings
)
3042 memset(&ad
, 0, sizeof(AttributeDef
));
3043 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3048 if (parse_btype(&type
, &ad
)) {
3049 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3051 /* check ISOC99 compound literal */
3053 /* data is allocated locally by default */
3058 /* all except arrays are lvalues */
3059 if (!(type
.t
& VT_ARRAY
))
3060 r
|= lvalue_type(type
.t
);
3061 memset(&ad
, 0, sizeof(AttributeDef
));
3062 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3067 } else if (tok
== '{') {
3068 /* save all registers */
3070 /* statement expression : we do not accept break/continue
3071 inside as GCC does */
3072 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3087 /* functions names must be treated as function pointers,
3088 except for unary '&' and sizeof. Since we consider that
3089 functions are not lvalues, we only have to handle it
3090 there and in function calls. */
3091 /* arrays can also be used although they are not lvalues */
3092 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3093 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3095 mk_pointer(&vtop
->type
);
3101 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3103 boolean
.t
= VT_BOOL
;
3105 vtop
->c
.i
= !vtop
->c
.i
;
3106 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3107 vtop
->c
.i
= vtop
->c
.i
^ 1;
3110 vseti(VT_JMP
, gtst(1, 0));
3121 /* in order to force cast, we add zero */
3123 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3124 error("pointer not accepted for unary plus");
3134 parse_expr_type(&type
);
3138 size
= type_size(&type
, &align
);
3139 if (t
== TOK_SIZEOF
) {
3141 error("sizeof applied to an incomplete type");
3146 vtop
->type
.t
|= VT_UNSIGNED
;
3149 case TOK_builtin_types_compatible_p
:
3158 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3159 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3160 vpushi(is_compatible_types(&type1
, &type2
));
3163 case TOK_builtin_constant_p
:
3165 int saved_nocode_wanted
, res
;
3168 saved_nocode_wanted
= nocode_wanted
;
3171 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3173 nocode_wanted
= saved_nocode_wanted
;
3178 case TOK_builtin_frame_address
:
3183 if (tok
!= TOK_CINT
) {
3184 error("__builtin_frame_address only takes integers");
3187 error("TCC only supports __builtin_frame_address(0)");
3193 vset(&type
, VT_LOCAL
, 0);
3196 #ifdef TCC_TARGET_X86_64
3197 case TOK_builtin_malloc
:
3199 goto tok_identifier
;
3200 case TOK_builtin_free
:
3202 goto tok_identifier
;
3219 goto tok_identifier
;
3221 /* allow to take the address of a label */
3222 if (tok
< TOK_UIDENT
)
3223 expect("label identifier");
3224 s
= label_find(tok
);
3226 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3228 if (s
->r
== LABEL_DECLARED
)
3229 s
->r
= LABEL_FORWARD
;
3232 s
->type
.t
= VT_VOID
;
3233 mk_pointer(&s
->type
);
3234 s
->type
.t
|= VT_STATIC
;
3236 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3245 expect("identifier");
3249 error("'%s' undeclared", get_tok_str(t
, NULL
));
3250 /* for simple function calls, we tolerate undeclared
3251 external reference to int() function */
3252 if (tcc_state
->warn_implicit_function_declaration
)
3253 warning("implicit declaration of function '%s'",
3254 get_tok_str(t
, NULL
));
3255 s
= external_global_sym(t
, &func_old_type
, 0);
3257 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3258 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3259 /* if referencing an inline function, then we generate a
3260 symbol to it if not already done. It will have the
3261 effect to generate code for it at the end of the
3262 compilation unit. Inline function as always
3263 generated in the text section. */
3265 put_extern_sym(s
, text_section
, 0, 0);
3266 r
= VT_SYM
| VT_CONST
;
3270 vset(&s
->type
, r
, s
->c
);
3271 /* if forward reference, we must point to s */
3272 if (vtop
->r
& VT_SYM
) {
3279 /* post operations */
3281 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3284 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3287 if (tok
== TOK_ARROW
)
3289 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3293 /* expect pointer on structure */
3294 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3295 expect("struct or union");
3299 while ((s
= s
->next
) != NULL
) {
3304 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3305 /* add field offset to pointer */
3306 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3309 /* change type to field type, and set to lvalue */
3310 vtop
->type
= s
->type
;
3311 vtop
->type
.t
|= qualifiers
;
3312 /* an array is never an lvalue */
3313 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3314 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3315 /* if bound checking, the referenced pointer must be checked */
3316 if (tcc_state
->do_bounds_check
)
3317 vtop
->r
|= VT_MUSTBOUND
;
3320 } else if (tok
== '[') {
3326 } else if (tok
== '(') {
3332 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3333 /* pointer test (no array accepted) */
3334 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3335 vtop
->type
= *pointed_type(&vtop
->type
);
3336 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3340 expect("function pointer");
3343 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3345 /* get return type */
3348 sa
= s
->next
; /* first parameter */
3351 /* compute first implicit argument if a structure is returned */
3352 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3353 /* get some space for the returned structure */
3354 size
= type_size(&s
->type
, &align
);
3355 loc
= (loc
- size
) & -align
;
3357 ret
.r
= VT_LOCAL
| VT_LVAL
;
3358 /* pass it as 'int' to avoid structure arg passing
3360 vseti(VT_LOCAL
, loc
);
3365 /* return in register */
3366 if (is_float(ret
.type
.t
)) {
3367 ret
.r
= reg_fret(ret
.type
.t
);
3369 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3378 gfunc_param_typed(s
, sa
);
3388 error("too few arguments to function");
3390 if (!nocode_wanted
) {
3391 gfunc_call(nb_args
);
3393 vtop
-= (nb_args
+ 1);
3396 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3404 static void uneq(void)
3410 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3411 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3412 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3427 static void expr_prod(void)
3432 while (tok
== '*' || tok
== '/' || tok
== '%') {
3440 static void expr_sum(void)
3445 while (tok
== '+' || tok
== '-') {
3453 static void expr_shift(void)
3458 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3466 static void expr_cmp(void)
3471 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3472 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3480 static void expr_cmpeq(void)
3485 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3493 static void expr_and(void)
3496 while (tok
== '&') {
3503 static void expr_xor(void)
3506 while (tok
== '^') {
3513 static void expr_or(void)
3516 while (tok
== '|') {
3523 /* XXX: fix this mess */
3524 static void expr_land_const(void)
3527 while (tok
== TOK_LAND
) {
3534 /* XXX: fix this mess */
3535 static void expr_lor_const(void)
3538 while (tok
== TOK_LOR
) {
3545 /* only used if non constant */
3546 static void expr_land(void)
3551 if (tok
== TOK_LAND
) {
3556 if (tok
!= TOK_LAND
) {
3566 static void expr_lor(void)
3571 if (tok
== TOK_LOR
) {
3576 if (tok
!= TOK_LOR
) {
3586 /* XXX: better constant handling */
3587 static void expr_eq(void)
3589 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3591 CType type
, type1
, type2
;
3598 boolean
.t
= VT_BOOL
;
3604 if (tok
!= ':' || !gnu_ext
) {
3619 if (vtop
!= vstack
) {
3620 /* needed to avoid having different registers saved in
3622 if (is_float(vtop
->type
.t
)) {
3624 #ifdef TCC_TARGET_X86_64
3625 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3635 if (tok
== ':' && gnu_ext
) {
3643 sv
= *vtop
; /* save value to handle it later */
3644 vtop
--; /* no vpop so that FP stack is not flushed */
3652 bt1
= t1
& VT_BTYPE
;
3654 bt2
= t2
& VT_BTYPE
;
3655 /* cast operands to correct type according to ISOC rules */
3656 if (is_float(bt1
) || is_float(bt2
)) {
3657 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3658 type
.t
= VT_LDOUBLE
;
3659 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3664 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3665 /* cast to biggest op */
3667 /* convert to unsigned if it does not fit in a long long */
3668 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3669 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3670 type
.t
|= VT_UNSIGNED
;
3671 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3672 /* XXX: test pointer compatibility */
3674 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3675 /* XXX: test function pointer compatibility */
3677 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3678 /* XXX: test structure compatibility */
3680 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3681 /* NOTE: as an extension, we accept void on only one side */
3684 /* integer operations */
3686 /* convert to unsigned if it does not fit in an integer */
3687 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3688 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3689 type
.t
|= VT_UNSIGNED
;
3692 /* now we convert second operand */
3694 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3697 if (is_float(type
.t
)) {
3699 #ifdef TCC_TARGET_X86_64
3700 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3704 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3705 /* for long longs, we use fixed registers to avoid having
3706 to handle a complicated move */
3711 /* this is horrible, but we must also convert first
3715 /* put again first value and cast it */
3718 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3728 static void gexpr(void)
3739 /* parse an expression and return its type without any side effect. */
3740 static void expr_type(CType
*type
)
3742 int saved_nocode_wanted
;
3744 saved_nocode_wanted
= nocode_wanted
;
3749 nocode_wanted
= saved_nocode_wanted
;
3752 /* parse a unary expression and return its type without any side
3754 static void unary_type(CType
*type
)
3766 /* parse a constant expression and return value in vtop. */
3767 static void expr_const1(void)
3776 /* parse an integer constant and return its value. */
3777 static int expr_const(void)
3781 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3782 expect("constant expression");
3788 /* return the label token if current token is a label, otherwise
3790 static int is_label(void)
3794 /* fast test first */
3795 if (tok
< TOK_UIDENT
)
3797 /* no need to save tokc because tok is an identifier */
3804 unget_tok(last_tok
);
3809 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3810 int case_reg
, int is_expr
)
3815 /* generate line number info */
3816 if (tcc_state
->do_debug
&&
3817 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3818 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3820 last_line_num
= file
->line_num
;
3824 /* default return value is (void) */
3826 vtop
->type
.t
= VT_VOID
;
3829 if (tok
== TOK_IF
) {
3836 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3838 if (c
== TOK_ELSE
) {
3842 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3843 gsym(d
); /* patch else jmp */
3846 } else if (tok
== TOK_WHILE
) {
3854 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3858 } else if (tok
== '{') {
3862 /* record local declaration stack position */
3864 llabel
= local_label_stack
;
3865 /* handle local labels declarations */
3866 if (tok
== TOK_LABEL
) {
3869 if (tok
< TOK_UIDENT
)
3870 expect("label identifier");
3871 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3881 while (tok
!= '}') {
3886 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3889 /* pop locally defined labels */
3890 label_pop(&local_label_stack
, llabel
);
3891 /* pop locally defined symbols */
3893 /* XXX: this solution makes only valgrind happy...
3894 triggered by gcc.c-torture/execute/20000917-1.c */
3896 switch(vtop
->type
.t
& VT_BTYPE
) {
3901 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3903 error("unsupported expression type");
3906 sym_pop(&local_stack
, s
);
3908 } else if (tok
== TOK_RETURN
) {
3912 gen_assign_cast(&func_vt
);
3913 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3915 /* if returning structure, must copy it to implicit
3916 first pointer arg location */
3919 size
= type_size(&func_vt
,&align
);
3922 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3926 loc
= (loc
- size
) & -4;
3929 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3932 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3934 vtop
->type
= int_type
;
3940 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3943 /* copy structure value to pointer */
3948 } else if (is_float(func_vt
.t
)) {
3949 gv(rc_fret(func_vt
.t
));
3953 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3956 rsym
= gjmp(rsym
); /* jmp */
3957 } else if (tok
== TOK_BREAK
) {
3960 error("cannot break");
3961 *bsym
= gjmp(*bsym
);
3964 } else if (tok
== TOK_CONTINUE
) {
3967 error("cannot continue");
3968 *csym
= gjmp(*csym
);
3971 } else if (tok
== TOK_FOR
) {
3998 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4003 if (tok
== TOK_DO
) {
4008 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4019 if (tok
== TOK_SWITCH
) {
4023 /* XXX: other types than integer */
4024 case_reg
= gv(RC_INT
);
4028 b
= gjmp(0); /* jump to first case */
4030 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4031 /* if no default, jmp after switch */
4039 if (tok
== TOK_CASE
) {
4046 if (gnu_ext
&& tok
== TOK_DOTS
) {
4050 warning("empty case range");
4052 /* since a case is like a label, we must skip it with a jmp */
4059 *case_sym
= gtst(1, 0);
4062 *case_sym
= gtst(1, 0);
4066 *case_sym
= gtst(1, *case_sym
);
4071 goto block_after_label
;
4073 if (tok
== TOK_DEFAULT
) {
4079 error("too many 'default'");
4082 goto block_after_label
;
4084 if (tok
== TOK_GOTO
) {
4086 if (tok
== '*' && gnu_ext
) {
4090 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4093 } else if (tok
>= TOK_UIDENT
) {
4094 s
= label_find(tok
);
4095 /* put forward definition if needed */
4097 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4099 if (s
->r
== LABEL_DECLARED
)
4100 s
->r
= LABEL_FORWARD
;
4102 /* label already defined */
4103 if (s
->r
& LABEL_FORWARD
)
4104 s
->jnext
= gjmp(s
->jnext
);
4106 gjmp_addr(s
->jnext
);
4109 expect("label identifier");
4112 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4120 if (s
->r
== LABEL_DEFINED
)
4121 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4123 s
->r
= LABEL_DEFINED
;
4125 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4128 /* we accept this, but it is a mistake */
4131 warning("deprecated use of label at end of compound statement");
4135 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4138 /* expression case */
4153 /* t is the array or struct type. c is the array or struct
4154 address. cur_index/cur_field is the pointer to the current
4155 value. 'size_only' is true if only size info is needed (only used
4157 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4158 int *cur_index
, Sym
**cur_field
,
4162 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4168 if (gnu_ext
&& (l
= is_label()) != 0)
4170 while (tok
== '[' || tok
== '.') {
4172 if (!(type
->t
& VT_ARRAY
))
4173 expect("array type");
4176 index
= expr_const();
4177 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4178 expect("invalid index");
4179 if (tok
== TOK_DOTS
&& gnu_ext
) {
4181 index_last
= expr_const();
4182 if (index_last
< 0 ||
4183 (s
->c
>= 0 && index_last
>= s
->c
) ||
4185 expect("invalid index");
4191 *cur_index
= index_last
;
4192 type
= pointed_type(type
);
4193 elem_size
= type_size(type
, &align
);
4194 c
+= index
* elem_size
;
4195 /* NOTE: we only support ranges for last designator */
4196 nb_elems
= index_last
- index
+ 1;
4197 if (nb_elems
!= 1) {
4206 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4207 expect("struct/union type");
4220 /* XXX: fix this mess by using explicit storage field */
4222 type1
.t
|= (type
->t
& ~VT_TYPE
);
4236 if (type
->t
& VT_ARRAY
) {
4238 type
= pointed_type(type
);
4239 c
+= index
* type_size(type
, &align
);
4243 error("too many field init");
4244 /* XXX: fix this mess by using explicit storage field */
4246 type1
.t
|= (type
->t
& ~VT_TYPE
);
4251 decl_initializer(type
, sec
, c
, 0, size_only
);
4253 /* XXX: make it more general */
4254 if (!size_only
&& nb_elems
> 1) {
4255 unsigned long c_end
;
4260 error("range init not supported yet for dynamic storage");
4261 c_end
= c
+ nb_elems
* elem_size
;
4262 if (c_end
> sec
->data_allocated
)
4263 section_realloc(sec
, c_end
);
4264 src
= sec
->data
+ c
;
4266 for(i
= 1; i
< nb_elems
; i
++) {
4268 memcpy(dst
, src
, elem_size
);
4274 #define EXPR_CONST 1
4277 /* store a value or an expression directly in global data or in local array */
4278 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4279 int v
, int expr_type
)
4281 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4283 unsigned long long bit_mask
;
4291 /* compound literals must be allocated globally in this case */
4292 saved_global_expr
= global_expr
;
4295 global_expr
= saved_global_expr
;
4296 /* NOTE: symbols are accepted */
4297 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4298 error("initializer element is not constant");
4306 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4309 /* XXX: not portable */
4310 /* XXX: generate error if incorrect relocation */
4311 gen_assign_cast(&dtype
);
4312 bt
= type
->t
& VT_BTYPE
;
4313 /* we'll write at most 12 bytes */
4314 if (c
+ 12 > sec
->data_allocated
) {
4315 section_realloc(sec
, c
+ 12);
4317 ptr
= sec
->data
+ c
;
4318 /* XXX: make code faster ? */
4319 if (!(type
->t
& VT_BITFIELD
)) {
4324 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4325 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4326 bit_mask
= (1LL << bit_size
) - 1;
4328 if ((vtop
->r
& VT_SYM
) &&
4334 (bt
== VT_INT
&& bit_size
!= 32)))
4335 error("initializer element is not computable at load time");
4338 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4340 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4343 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4346 *(double *)ptr
= vtop
->c
.d
;
4349 *(long double *)ptr
= vtop
->c
.ld
;
4352 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4355 if (vtop
->r
& VT_SYM
) {
4356 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4358 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4363 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4370 /* put zeros for variable based init */
4371 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4374 /* nothing to do because globals are already set to zero */
4376 vpush_global_sym(&func_old_type
, TOK_memset
);
4384 /* 't' contains the type and storage info. 'c' is the offset of the
4385 object in section 'sec'. If 'sec' is NULL, it means stack based
4386 allocation. 'first' is true if array '{' must be read (multi
4387 dimension implicit array init handling). 'size_only' is true if
4388 size only evaluation is wanted (only for arrays). */
4389 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4390 int first
, int size_only
)
4392 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4393 int size1
, align1
, expr_type
;
4397 if (type
->t
& VT_ARRAY
) {
4401 t1
= pointed_type(type
);
4402 size1
= type_size(t1
, &align1
);
4405 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4411 /* only parse strings here if correct type (otherwise: handle
4412 them as ((w)char *) expressions */
4413 if ((tok
== TOK_LSTR
&&
4414 #ifdef TCC_TARGET_PE
4415 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4417 (t1
->t
& VT_BTYPE
) == VT_INT
4419 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4420 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4425 /* compute maximum number of chars wanted */
4427 cstr_len
= cstr
->size
;
4429 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4432 if (n
>= 0 && nb
> (n
- array_length
))
4433 nb
= n
- array_length
;
4436 warning("initializer-string for array is too long");
4437 /* in order to go faster for common case (char
4438 string in global variable, we handle it
4440 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4441 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4445 ch
= ((unsigned char *)cstr
->data
)[i
];
4447 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4448 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4456 /* only add trailing zero if enough storage (no
4457 warning in this case since it is standard) */
4458 if (n
< 0 || array_length
< n
) {
4460 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4466 while (tok
!= '}') {
4467 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4468 if (n
>= 0 && index
>= n
)
4469 error("index too large");
4470 /* must put zero in holes (note that doing it that way
4471 ensures that it even works with designators) */
4472 if (!size_only
&& array_length
< index
) {
4473 init_putz(t1
, sec
, c
+ array_length
* size1
,
4474 (index
- array_length
) * size1
);
4477 if (index
> array_length
)
4478 array_length
= index
;
4479 /* special test for multi dimensional arrays (may not
4480 be strictly correct if designators are used at the
4482 if (index
>= n
&& no_oblock
)
4491 /* put zeros at the end */
4492 if (!size_only
&& n
>= 0 && array_length
< n
) {
4493 init_putz(t1
, sec
, c
+ array_length
* size1
,
4494 (n
- array_length
) * size1
);
4496 /* patch type size if needed */
4498 s
->c
= array_length
;
4499 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4500 (sec
|| !first
|| tok
== '{')) {
4503 /* NOTE: the previous test is a specific case for automatic
4504 struct/union init */
4505 /* XXX: union needs only one init */
4507 /* XXX: this test is incorrect for local initializers
4508 beginning with ( without {. It would be much more difficult
4509 to do it correctly (ideally, the expression parser should
4510 be used in all cases) */
4516 while (tok
== '(') {
4520 if (!parse_btype(&type1
, &ad1
))
4522 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4524 if (!is_assignable_types(type
, &type1
))
4525 error("invalid type for cast");
4530 if (first
|| tok
== '{') {
4539 while (tok
!= '}') {
4540 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4542 if (!size_only
&& array_length
< index
) {
4543 init_putz(type
, sec
, c
+ array_length
,
4544 index
- array_length
);
4546 index
= index
+ type_size(&f
->type
, &align1
);
4547 if (index
> array_length
)
4548 array_length
= index
;
4550 /* gr: skip fields from same union - ugly. */
4552 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4553 /* test for same offset */
4554 if (f
->next
->c
!= f
->c
)
4556 /* if yes, test for bitfield shift */
4557 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4558 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4559 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4560 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4561 if (bit_pos_1
!= bit_pos_2
)
4568 if (no_oblock
&& f
== NULL
)
4574 /* put zeros at the end */
4575 if (!size_only
&& array_length
< n
) {
4576 init_putz(type
, sec
, c
+ array_length
,
4585 } else if (tok
== '{') {
4587 decl_initializer(type
, sec
, c
, first
, size_only
);
4589 } else if (size_only
) {
4590 /* just skip expression */
4592 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4596 else if (tok
== ')')
4601 /* currently, we always use constant expression for globals
4602 (may change for scripting case) */
4603 expr_type
= EXPR_CONST
;
4605 expr_type
= EXPR_ANY
;
4606 init_putv(type
, sec
, c
, 0, expr_type
);
4610 /* parse an initializer for type 't' if 'has_init' is non zero, and
4611 allocate space in local or global data space ('r' is either
4612 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4613 variable 'v' of scope 'scope' is declared before initializers are
4614 parsed. If 'v' is zero, then a reference to the new object is put
4615 in the value stack. If 'has_init' is 2, a special parsing is done
4616 to handle string constants. */
4617 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4618 int has_init
, int v
, int scope
)
4620 int size
, align
, addr
, data_offset
;
4622 ParseState saved_parse_state
= {0};
4623 TokenString init_str
;
4626 size
= type_size(type
, &align
);
4627 /* If unknown size, we must evaluate it before
4628 evaluating initializers because
4629 initializers can generate global data too
4630 (e.g. string pointers or ISOC99 compound
4631 literals). It also simplifies local
4632 initializers handling */
4633 tok_str_new(&init_str
);
4636 error("unknown type size");
4637 /* get all init string */
4638 if (has_init
== 2) {
4639 /* only get strings */
4640 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4641 tok_str_add_tok(&init_str
);
4646 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4648 error("unexpected end of file in initializer");
4649 tok_str_add_tok(&init_str
);
4652 else if (tok
== '}') {
4662 tok_str_add(&init_str
, -1);
4663 tok_str_add(&init_str
, 0);
4666 save_parse_state(&saved_parse_state
);
4668 macro_ptr
= init_str
.str
;
4670 decl_initializer(type
, NULL
, 0, 1, 1);
4671 /* prepare second initializer parsing */
4672 macro_ptr
= init_str
.str
;
4675 /* if still unknown size, error */
4676 size
= type_size(type
, &align
);
4678 error("unknown type size");
4680 /* take into account specified alignment if bigger */
4682 if (ad
->aligned
> align
)
4683 align
= ad
->aligned
;
4684 } else if (ad
->packed
) {
4687 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4689 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4691 loc
= (loc
- size
) & -align
;
4693 /* handles bounds */
4694 /* XXX: currently, since we do only one pass, we cannot track
4695 '&' operators, so we add only arrays */
4696 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4697 unsigned long *bounds_ptr
;
4698 /* add padding between regions */
4700 /* then add local bound info */
4701 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4702 bounds_ptr
[0] = addr
;
4703 bounds_ptr
[1] = size
;
4706 /* local variable */
4707 sym_push(v
, type
, r
, addr
);
4709 /* push local reference */
4710 vset(type
, r
, addr
);
4716 if (v
&& scope
== VT_CONST
) {
4717 /* see if the symbol was already defined */
4720 if (!is_compatible_types(&sym
->type
, type
))
4721 error("incompatible types for redefinition of '%s'",
4722 get_tok_str(v
, NULL
));
4723 if (sym
->type
.t
& VT_EXTERN
) {
4724 /* if the variable is extern, it was not allocated */
4725 sym
->type
.t
&= ~VT_EXTERN
;
4726 /* set array size if it was ommited in extern
4728 if ((sym
->type
.t
& VT_ARRAY
) &&
4729 sym
->type
.ref
->c
< 0 &&
4731 sym
->type
.ref
->c
= type
->ref
->c
;
4733 /* we accept several definitions of the same
4734 global variable. this is tricky, because we
4735 must play with the SHN_COMMON type of the symbol */
4736 /* XXX: should check if the variable was already
4737 initialized. It is incorrect to initialized it
4739 /* no init data, we won't add more to the symbol */
4746 /* allocate symbol in corresponding section */
4751 else if (tcc_state
->nocommon
)
4755 data_offset
= sec
->data_offset
;
4756 data_offset
= (data_offset
+ align
- 1) & -align
;
4758 /* very important to increment global pointer at this time
4759 because initializers themselves can create new initializers */
4760 data_offset
+= size
;
4761 /* add padding if bound check */
4762 if (tcc_state
->do_bounds_check
)
4764 sec
->data_offset
= data_offset
;
4765 /* allocate section space to put the data */
4766 if (sec
->sh_type
!= SHT_NOBITS
&&
4767 data_offset
> sec
->data_allocated
)
4768 section_realloc(sec
, data_offset
);
4769 /* align section if needed */
4770 if (align
> sec
->sh_addralign
)
4771 sec
->sh_addralign
= align
;
4773 addr
= 0; /* avoid warning */
4777 if (scope
!= VT_CONST
|| !sym
) {
4778 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4780 /* update symbol definition */
4782 put_extern_sym(sym
, sec
, addr
, size
);
4785 /* put a common area */
4786 put_extern_sym(sym
, NULL
, align
, size
);
4787 /* XXX: find a nicer way */
4788 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4789 esym
->st_shndx
= SHN_COMMON
;
4794 /* push global reference */
4795 sym
= get_sym_ref(type
, sec
, addr
, size
);
4797 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4801 /* handles bounds now because the symbol must be defined
4802 before for the relocation */
4803 if (tcc_state
->do_bounds_check
) {
4804 unsigned long *bounds_ptr
;
4806 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
4807 /* then add global bound info */
4808 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4809 bounds_ptr
[0] = 0; /* relocated */
4810 bounds_ptr
[1] = size
;
4814 decl_initializer(type
, sec
, addr
, 1, 0);
4815 /* restore parse state if needed */
4817 tok_str_free(init_str
.str
);
4818 restore_parse_state(&saved_parse_state
);
4824 void put_func_debug(Sym
*sym
)
4829 /* XXX: we put here a dummy type */
4830 snprintf(buf
, sizeof(buf
), "%s:%c1",
4831 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4832 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4833 cur_text_section
, sym
->c
);
4834 /* //gr gdb wants a line at the function */
4835 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4840 /* parse an old style function declaration list */
4841 /* XXX: check multiple parameter */
4842 static void func_decl_list(Sym
*func_sym
)
4849 /* parse each declaration */
4850 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4851 if (!parse_btype(&btype
, &ad
))
4852 expect("declaration list");
4853 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4854 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4856 /* we accept no variable after */
4860 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4861 /* find parameter in function parameter list */
4864 if ((s
->v
& ~SYM_FIELD
) == v
)
4868 error("declaration for parameter '%s' but no such parameter",
4869 get_tok_str(v
, NULL
));
4871 /* check that no storage specifier except 'register' was given */
4872 if (type
.t
& VT_STORAGE
)
4873 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4874 convert_parameter_type(&type
);
4875 /* we can add the type (NOTE: it could be local to the function) */
4877 /* accept other parameters */
4888 /* parse a function defined by symbol 'sym' and generate its code in
4889 'cur_text_section' */
4890 static void gen_function(Sym
*sym
)
4892 int saved_nocode_wanted
= nocode_wanted
;
4894 ind
= cur_text_section
->data_offset
;
4895 /* NOTE: we patch the symbol size later */
4896 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4897 funcname
= get_tok_str(sym
->v
, NULL
);
4899 /* put debug symbol */
4900 if (tcc_state
->do_debug
)
4901 put_func_debug(sym
);
4902 /* push a dummy symbol to enable local sym storage */
4903 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4904 gfunc_prolog(&sym
->type
);
4906 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4909 cur_text_section
->data_offset
= ind
;
4910 label_pop(&global_label_stack
, NULL
);
4911 sym_pop(&local_stack
, NULL
); /* reset local stack */
4912 /* end of function */
4913 /* patch symbol size */
4914 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4916 if (tcc_state
->do_debug
) {
4917 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4919 /* It's better to crash than to generate wrong code */
4920 cur_text_section
= NULL
;
4921 funcname
= ""; /* for safety */
4922 func_vt
.t
= VT_VOID
; /* for safety */
4923 ind
= 0; /* for safety */
4924 nocode_wanted
= saved_nocode_wanted
;
4927 void gen_inline_functions(void)
4930 int *str
, inline_generated
, i
;
4931 struct InlineFunc
*fn
;
4933 /* iterate while inline function are referenced */
4935 inline_generated
= 0;
4936 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4937 fn
= tcc_state
->inline_fns
[i
];
4939 if (sym
&& sym
->c
) {
4940 /* the function was used: generate its code and
4941 convert it to a normal function */
4942 str
= fn
->token_str
;
4945 strcpy(file
->filename
, fn
->filename
);
4946 sym
->r
= VT_SYM
| VT_CONST
;
4947 sym
->type
.t
&= ~VT_INLINE
;
4951 cur_text_section
= text_section
;
4953 macro_ptr
= NULL
; /* fail safe */
4955 inline_generated
= 1;
4958 if (!inline_generated
)
4961 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4962 fn
= tcc_state
->inline_fns
[i
];
4963 str
= fn
->token_str
;
4966 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
4969 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4978 if (!parse_btype(&btype
, &ad
)) {
4979 /* skip redundant ';' */
4980 /* XXX: find more elegant solution */
4985 if (l
== VT_CONST
&&
4986 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
4987 /* global asm block */
4991 /* special test for old K&R protos without explicit int
4992 type. Only accepted when defining global data */
4993 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
4997 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4998 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5000 /* we accept no variable after */
5004 while (1) { /* iterate thru each declaration */
5006 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5010 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5011 printf("type = '%s'\n", buf
);
5014 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5015 /* if old style function prototype, we accept a
5018 if (sym
->c
== FUNC_OLD
)
5019 func_decl_list(sym
);
5024 error("cannot use local functions");
5025 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5026 expect("function definition");
5028 /* reject abstract declarators in function definition */
5030 while ((sym
= sym
->next
) != NULL
)
5031 if (!(sym
->v
& ~SYM_FIELD
))
5032 expect("identifier");
5034 /* XXX: cannot do better now: convert extern line to static inline */
5035 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5036 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5040 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5043 r
= sym
->type
.ref
->r
;
5044 /* use func_call from prototype if not defined */
5045 if (FUNC_CALL(r
) != FUNC_CDECL
5046 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5047 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5049 /* use export from prototype */
5051 FUNC_EXPORT(type
.ref
->r
) = 1;
5053 /* use static from prototype */
5054 if (sym
->type
.t
& VT_STATIC
)
5055 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5057 if (!is_compatible_types(&sym
->type
, &type
)) {
5059 error("incompatible types for redefinition of '%s'",
5060 get_tok_str(v
, NULL
));
5062 /* if symbol is already defined, then put complete type */
5065 /* put function symbol */
5066 sym
= global_identifier_push(v
, type
.t
, 0);
5067 sym
->type
.ref
= type
.ref
;
5070 /* static inline functions are just recorded as a kind
5071 of macro. Their code will be emitted at the end of
5072 the compilation unit only if they are used */
5073 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5074 (VT_INLINE
| VT_STATIC
)) {
5075 TokenString func_str
;
5077 struct InlineFunc
*fn
;
5078 const char *filename
;
5080 tok_str_new(&func_str
);
5086 error("unexpected end of file");
5087 tok_str_add_tok(&func_str
);
5092 } else if (t
== '}') {
5094 if (block_level
== 0)
5098 tok_str_add(&func_str
, -1);
5099 tok_str_add(&func_str
, 0);
5100 filename
= file
? file
->filename
: "";
5101 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5102 strcpy(fn
->filename
, filename
);
5104 fn
->token_str
= func_str
.str
;
5105 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5108 /* compute text section */
5109 cur_text_section
= ad
.section
;
5110 if (!cur_text_section
)
5111 cur_text_section
= text_section
;
5112 sym
->r
= VT_SYM
| VT_CONST
;
5117 if (btype
.t
& VT_TYPEDEF
) {
5118 /* save typedefed type */
5119 /* XXX: test storage specifiers ? */
5120 sym
= sym_push(v
, &type
, 0, 0);
5121 sym
->type
.t
|= VT_TYPEDEF
;
5122 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5123 /* external function definition */
5124 /* specific case for func_call attribute */
5126 type
.ref
->r
= ad
.func_attr
;
5127 external_sym(v
, &type
, 0);
5129 /* not lvalue if array */
5131 if (!(type
.t
& VT_ARRAY
))
5132 r
|= lvalue_type(type
.t
);
5133 has_init
= (tok
== '=');
5134 if ((btype
.t
& VT_EXTERN
) ||
5135 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5136 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5137 /* external variable */
5138 /* NOTE: as GCC, uninitialized global static
5139 arrays of null size are considered as
5141 #ifdef TCC_TARGET_PE
5142 if (FUNC_IMPORT(ad
.func_attr
))
5143 type
.t
|= VT_IMPORT
;
5145 external_sym(v
, &type
, r
);
5147 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5148 if (type
.t
& VT_STATIC
)
5154 decl_initializer_alloc(&type
, &ad
, r
,