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
21 void swap(int *p
, int *q
)
29 void vsetc(CType
*type
, int r
, CValue
*vc
)
33 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
35 /* cannot let cpu flags if other instruction are generated. Also
36 avoid leaving VT_JMP anywhere except on the top of the stack
37 because it would complicate the code generator. */
39 v
= vtop
->r
& VT_VALMASK
;
40 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
50 /* push integer constant */
55 vsetc(&int_type
, VT_CONST
, &cval
);
58 /* push long long constant */
59 void vpushll(long long v
)
65 vsetc(&ctype
, VT_CONST
, &cval
);
68 /* Return a static symbol pointing to a section */
69 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
70 unsigned long offset
, unsigned long size
)
76 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
77 sym
->type
.ref
= type
->ref
;
78 sym
->r
= VT_CONST
| VT_SYM
;
79 put_extern_sym(sym
, sec
, offset
, size
);
83 /* push a reference to a section offset by adding a dummy symbol */
84 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
89 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
90 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
93 /* define a new external reference to a symbol 'v' of type 'u' */
94 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
100 /* push forward reference */
101 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
102 s
->type
.ref
= type
->ref
;
103 s
->r
= r
| VT_CONST
| VT_SYM
;
108 /* define a new external reference to a symbol 'v' of type 'u' */
109 static Sym
*external_sym(int v
, CType
*type
, int r
)
115 /* push forward reference */
116 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
117 s
->type
.t
|= VT_EXTERN
;
119 if (!is_compatible_types(&s
->type
, type
))
120 error("incompatible types for redefinition of '%s'",
121 get_tok_str(v
, NULL
));
126 /* push a reference to global symbol v */
127 static void vpush_global_sym(CType
*type
, int v
)
132 sym
= external_global_sym(v
, type
, 0);
134 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
138 void vset(CType
*type
, int r
, int v
)
143 vsetc(type
, r
, &cval
);
146 void vseti(int r
, int v
)
162 void vpushv(SValue
*v
)
164 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
165 error("memory full");
175 /* save r to the memory stack, and mark it as being free */
178 int l
, saved
, size
, align
;
182 /* modify all stack values */
185 for(p
=vstack
;p
<=vtop
;p
++) {
186 if ((p
->r
& VT_VALMASK
) == r
||
187 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
188 /* must save value on stack if not already done */
190 /* NOTE: must reload 'r' because r might be equal to r2 */
191 r
= p
->r
& VT_VALMASK
;
192 /* store register in the stack */
194 if ((p
->r
& VT_LVAL
) ||
195 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
196 #ifdef TCC_TARGET_X86_64
197 type
= &char_pointer_type
;
201 size
= type_size(type
, &align
);
202 loc
= (loc
- size
) & -align
;
204 sv
.r
= VT_LOCAL
| VT_LVAL
;
207 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
208 /* x86 specific: need to pop fp register ST0 if saved */
210 o(0xd9dd); /* fstp %st(1) */
213 #ifndef TCC_TARGET_X86_64
214 /* special long long case */
215 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
223 /* mark that stack entry as being saved on the stack */
224 if (p
->r
& VT_LVAL
) {
225 /* also clear the bounded flag because the
226 relocation address of the function was stored in
228 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
230 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
238 /* find a register of class 'rc2' with at most one reference on stack.
239 * If none, call get_reg(rc) */
240 int get_reg_ex(int rc
, int rc2
)
245 for(r
=0;r
<NB_REGS
;r
++) {
246 if (reg_classes
[r
] & rc2
) {
249 for(p
= vstack
; p
<= vtop
; p
++) {
250 if ((p
->r
& VT_VALMASK
) == r
||
251 (p
->r2
& VT_VALMASK
) == r
)
261 /* find a free register of class 'rc'. If none, save one register */
267 /* find a free register */
268 for(r
=0;r
<NB_REGS
;r
++) {
269 if (reg_classes
[r
] & rc
) {
270 for(p
=vstack
;p
<=vtop
;p
++) {
271 if ((p
->r
& VT_VALMASK
) == r
||
272 (p
->r2
& VT_VALMASK
) == r
)
280 /* no register left : free the first one on the stack (VERY
281 IMPORTANT to start from the bottom to ensure that we don't
282 spill registers used in gen_opi()) */
283 for(p
=vstack
;p
<=vtop
;p
++) {
284 r
= p
->r
& VT_VALMASK
;
285 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
287 /* also look at second register (if long long) */
288 r
= p
->r2
& VT_VALMASK
;
289 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
295 /* Should never comes here */
299 /* save registers up to (vtop - n) stack entry */
300 void save_regs(int n
)
305 for(p
= vstack
;p
<= p1
; p
++) {
306 r
= p
->r
& VT_VALMASK
;
313 /* move register 's' to 'r', and flush previous value of r to memory
315 void move_reg(int r
, int s
)
328 /* get address of vtop (vtop MUST BE an lvalue) */
332 /* tricky: if saved lvalue, then we can go back to lvalue */
333 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
334 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
337 #ifdef CONFIG_TCC_BCHECK
338 /* generate lvalue bound code */
344 vtop
->r
&= ~VT_MUSTBOUND
;
345 /* if lvalue, then use checking code before dereferencing */
346 if (vtop
->r
& VT_LVAL
) {
347 /* if not VT_BOUNDED value, then make one */
348 if (!(vtop
->r
& VT_BOUNDED
)) {
349 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
350 /* must save type because we must set it to int to get pointer */
352 vtop
->type
.t
= VT_INT
;
355 gen_bounded_ptr_add();
356 vtop
->r
|= lval_type
;
359 /* then check for dereferencing */
360 gen_bounded_ptr_deref();
365 /* store vtop a register belonging to class 'rc'. lvalues are
366 converted to values. Cannot be used if cannot be converted to
367 register value (such as structures). */
370 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
372 /* NOTE: get_reg can modify vstack[] */
373 if (vtop
->type
.t
& VT_BITFIELD
) {
376 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
377 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
378 /* remove bit field info to avoid loops */
379 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
380 /* cast to int to propagate signedness in following ops */
381 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
386 if((vtop
->type
.t
& VT_UNSIGNED
) ||
387 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
388 type
.t
|= VT_UNSIGNED
;
390 /* generate shifts */
391 vpushi(bits
- (bit_pos
+ bit_size
));
393 vpushi(bits
- bit_size
);
394 /* NOTE: transformed to SHR if unsigned */
398 if (is_float(vtop
->type
.t
) &&
399 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
402 unsigned long offset
;
403 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
407 /* XXX: unify with initializers handling ? */
408 /* CPUs usually cannot use float constants, so we store them
409 generically in data segment */
410 size
= type_size(&vtop
->type
, &align
);
411 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
412 data_section
->data_offset
= offset
;
413 /* XXX: not portable yet */
414 #if defined(__i386__) || defined(__x86_64__)
415 /* Zero pad x87 tenbyte long doubles */
416 if (size
== LDOUBLE_SIZE
)
417 vtop
->c
.tab
[2] &= 0xffff;
419 ptr
= section_ptr_add(data_section
, size
);
421 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
425 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
429 ptr
[i
] = vtop
->c
.tab
[i
];
430 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
431 vtop
->r
|= VT_LVAL
| VT_SYM
;
435 #ifdef CONFIG_TCC_BCHECK
436 if (vtop
->r
& VT_MUSTBOUND
)
440 r
= vtop
->r
& VT_VALMASK
;
444 /* need to reload if:
446 - lvalue (need to dereference pointer)
447 - already a register, but not in the right class */
449 (vtop
->r
& VT_LVAL
) ||
450 !(reg_classes
[r
] & rc
) ||
451 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
452 !(reg_classes
[vtop
->r2
] & rc2
))) {
454 #ifndef TCC_TARGET_X86_64
455 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
457 unsigned long long ll
;
458 /* two register type load : expand to two words
460 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
463 vtop
->c
.ui
= ll
; /* first word */
465 vtop
->r
= r
; /* save register value */
466 vpushi(ll
>> 32); /* second word */
467 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
468 (vtop
->r
& VT_LVAL
)) {
469 /* We do not want to modifier the long long
470 pointer here, so the safest (and less
471 efficient) is to save all the other registers
472 in the stack. XXX: totally inefficient. */
474 /* load from memory */
477 vtop
[-1].r
= r
; /* save register value */
478 /* increment pointer to get second word */
479 vtop
->type
.t
= VT_INT
;
488 vtop
[-1].r
= r
; /* save register value */
489 vtop
->r
= vtop
[-1].r2
;
491 /* allocate second register */
495 /* write second register */
499 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
501 /* lvalue of scalar type : need to use lvalue type
502 because of possible cast */
505 /* compute memory access type */
506 if (vtop
->r
& VT_LVAL_BYTE
)
508 else if (vtop
->r
& VT_LVAL_SHORT
)
510 if (vtop
->r
& VT_LVAL_UNSIGNED
)
514 /* restore wanted type */
517 /* one register type load */
522 #ifdef TCC_TARGET_C67
523 /* uses register pairs for doubles */
524 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
531 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
532 void gv2(int rc1
, int rc2
)
536 /* generate more generic register first. But VT_JMP or VT_CMP
537 values must be generated first in all cases to avoid possible
539 v
= vtop
[0].r
& VT_VALMASK
;
540 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
545 /* test if reload is needed for first register */
546 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
556 /* test if reload is needed for first register */
557 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
563 /* wrapper around RC_FRET to return a register by type */
566 #ifdef TCC_TARGET_X86_64
567 if (t
== VT_LDOUBLE
) {
574 /* wrapper around REG_FRET to return a register by type */
577 #ifdef TCC_TARGET_X86_64
578 if (t
== VT_LDOUBLE
) {
585 /* expand long long on stack in two int registers */
590 u
= vtop
->type
.t
& VT_UNSIGNED
;
593 vtop
[0].r
= vtop
[-1].r2
;
594 vtop
[0].r2
= VT_CONST
;
595 vtop
[-1].r2
= VT_CONST
;
596 vtop
[0].type
.t
= VT_INT
| u
;
597 vtop
[-1].type
.t
= VT_INT
| u
;
600 #ifdef TCC_TARGET_ARM
601 /* expand long long on stack */
602 void lexpand_nr(void)
606 u
= vtop
->type
.t
& VT_UNSIGNED
;
609 vtop
->type
.t
= VT_INT
| u
;
610 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
612 vtop
[-1].c
.ui
= vtop
->c
.ull
;
613 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
615 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
617 vtop
->r
= vtop
[-1].r
;
618 } else if (v
> VT_CONST
) {
622 vtop
->r
= vtop
[-1].r2
;
623 vtop
[-1].r2
= VT_CONST
;
624 vtop
[-1].type
.t
= VT_INT
| u
;
628 /* build a long long from two ints */
632 vtop
[-1].r2
= vtop
[0].r
;
637 /* rotate n first stack elements to the bottom
638 I1 ... In -> I2 ... In I1 [top is right]
651 /* rotate n first stack elements to the top
652 I1 ... In -> In I1 ... I(n-1) [top is right]
660 for(i
= 0;i
< n
- 1; i
++)
661 vtop
[-i
] = vtop
[-i
- 1];
665 #ifdef TCC_TARGET_ARM
666 /* like vrott but in other direction
667 In ... I1 -> I(n-1) ... I1 In [top is right]
675 for(i
= n
- 1; i
> 0; i
--)
676 vtop
[-i
] = vtop
[-i
+ 1];
681 /* pop stack value */
685 v
= vtop
->r
& VT_VALMASK
;
686 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
687 /* for x86, we need to pop the FP stack */
688 if (v
== TREG_ST0
&& !nocode_wanted
) {
689 o(0xd9dd); /* fstp %st(1) */
692 if (v
== VT_JMP
|| v
== VT_JMPI
) {
693 /* need to put correct jump if && or || without test */
699 /* convert stack entry to register and duplicate its value in another
707 if ((t
& VT_BTYPE
) == VT_LLONG
) {
714 /* stack: H L L1 H1 */
722 /* duplicate value */
727 #ifdef TCC_TARGET_X86_64
728 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
738 load(r1
, &sv
); /* move r to r1 */
740 /* duplicates value */
745 #ifndef TCC_TARGET_X86_64
746 /* generate CPU independent (unsigned) long long operations */
749 int t
, a
, b
, op1
, c
, i
;
751 unsigned short reg_iret
= REG_IRET
;
752 unsigned short reg_lret
= REG_LRET
;
761 func
= TOK___udivdi3
;
767 func
= TOK___umoddi3
;
774 /* call generic long long function */
775 vpush_global_sym(&func_old_type
, func
);
793 /* stack: L1 H1 L2 H2 */
801 /* stack: H1 H2 L1 L2 */
807 /* stack: H1 H2 L1 L2 ML MH */
810 /* stack: ML MH H1 H2 L1 L2 */
814 /* stack: ML MH H1 L2 H2 L1 */
819 /* stack: ML MH M1 M2 */
822 } else if (op
== '+' || op
== '-') {
823 /* XXX: add non carry method too (for MIPS or alpha) */
829 /* stack: H1 H2 (L1 op L2) */
832 gen_op(op1
+ 1); /* TOK_xxxC2 */
835 /* stack: H1 H2 (L1 op L2) */
838 /* stack: (L1 op L2) H1 H2 */
840 /* stack: (L1 op L2) (H1 op H2) */
848 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
853 /* stack: L H shift */
855 /* constant: simpler */
856 /* NOTE: all comments are for SHL. the other cases are
857 done by swaping words */
901 /* XXX: should provide a faster fallback on x86 ? */
904 func
= TOK___ashrdi3
;
907 func
= TOK___lshrdi3
;
910 func
= TOK___ashldi3
;
916 /* compare operations */
922 /* stack: L1 H1 L2 H2 */
926 /* stack: L1 L2 H1 H2 */
929 /* when values are equal, we need to compare low words. since
930 the jump is inverted, we invert the test too. */
933 else if (op1
== TOK_GT
)
935 else if (op1
== TOK_ULT
)
937 else if (op1
== TOK_UGT
)
946 /* generate non equal test */
947 /* XXX: NOT PORTABLE yet */
951 #if defined(TCC_TARGET_I386)
953 #elif defined(TCC_TARGET_ARM)
955 o(0x1A000000 | encbranch(ind
, 0, 1));
956 #elif defined(TCC_TARGET_C67)
957 error("not implemented");
963 /* compare low. Always unsigned */
967 else if (op1
== TOK_LE
)
969 else if (op1
== TOK_GT
)
971 else if (op1
== TOK_GE
)
982 /* handle integer constant optimizations and various machine
984 void gen_opic(int op
)
986 int c1
, c2
, t1
, t2
, n
;
989 typedef unsigned long long U
;
993 t1
= v1
->type
.t
& VT_BTYPE
;
994 t2
= v2
->type
.t
& VT_BTYPE
;
998 else if (v1
->type
.t
& VT_UNSIGNED
)
1005 else if (v2
->type
.t
& VT_UNSIGNED
)
1010 /* currently, we cannot do computations with forward symbols */
1011 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1012 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1015 case '+': l1
+= l2
; break;
1016 case '-': l1
-= l2
; break;
1017 case '&': l1
&= l2
; break;
1018 case '^': l1
^= l2
; break;
1019 case '|': l1
|= l2
; break;
1020 case '*': l1
*= l2
; break;
1027 /* if division by zero, generate explicit division */
1030 error("division by zero in constant");
1034 default: l1
/= l2
; break;
1035 case '%': l1
%= l2
; break;
1036 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1037 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1040 case TOK_SHL
: l1
<<= l2
; break;
1041 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1042 case TOK_SAR
: l1
>>= l2
; break;
1044 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1045 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1046 case TOK_EQ
: l1
= l1
== l2
; break;
1047 case TOK_NE
: l1
= l1
!= l2
; break;
1048 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1049 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1050 case TOK_LT
: l1
= l1
< l2
; break;
1051 case TOK_GE
: l1
= l1
>= l2
; break;
1052 case TOK_LE
: l1
= l1
<= l2
; break;
1053 case TOK_GT
: l1
= l1
> l2
; break;
1055 case TOK_LAND
: l1
= l1
&& l2
; break;
1056 case TOK_LOR
: l1
= l1
|| l2
; break;
1063 /* if commutative ops, put c2 as constant */
1064 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1065 op
== '|' || op
== '*')) {
1067 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1068 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1070 /* Filter out NOP operations like x*1, x-0, x&-1... */
1071 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1074 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1075 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1081 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1082 /* try to use shifts instead of muls or divs */
1083 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1092 else if (op
== TOK_PDIV
)
1098 } else if (c2
&& (op
== '+' || op
== '-') &&
1099 ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
1100 (VT_CONST
| VT_SYM
) ||
1101 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1102 /* symbol + constant case */
1109 if (!nocode_wanted
) {
1110 /* call low level op generator */
1111 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1122 /* generate a floating point operation with constant propagation */
1123 void gen_opif(int op
)
1131 /* currently, we cannot do computations with forward symbols */
1132 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1133 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1135 if (v1
->type
.t
== VT_FLOAT
) {
1138 } else if (v1
->type
.t
== VT_DOUBLE
) {
1146 /* NOTE: we only do constant propagation if finite number (not
1147 NaN or infinity) (ANSI spec) */
1148 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1152 case '+': f1
+= f2
; break;
1153 case '-': f1
-= f2
; break;
1154 case '*': f1
*= f2
; break;
1158 error("division by zero in constant");
1163 /* XXX: also handles tests ? */
1167 /* XXX: overflow test ? */
1168 if (v1
->type
.t
== VT_FLOAT
) {
1170 } else if (v1
->type
.t
== VT_DOUBLE
) {
1178 if (!nocode_wanted
) {
1186 static int pointed_size(CType
*type
)
1189 return type_size(pointed_type(type
), &align
);
1192 static inline int is_null_pointer(SValue
*p
)
1194 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1196 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1197 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1200 static inline int is_integer_btype(int bt
)
1202 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1203 bt
== VT_INT
|| bt
== VT_LLONG
);
1206 /* check types for comparison or substraction of pointers */
1207 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1209 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1212 /* null pointers are accepted for all comparisons as gcc */
1213 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1217 bt1
= type1
->t
& VT_BTYPE
;
1218 bt2
= type2
->t
& VT_BTYPE
;
1219 /* accept comparison between pointer and integer with a warning */
1220 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1221 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1222 warning("comparison between pointer and integer");
1226 /* both must be pointers or implicit function pointers */
1227 if (bt1
== VT_PTR
) {
1228 type1
= pointed_type(type1
);
1229 } else if (bt1
!= VT_FUNC
)
1230 goto invalid_operands
;
1232 if (bt2
== VT_PTR
) {
1233 type2
= pointed_type(type2
);
1234 } else if (bt2
!= VT_FUNC
) {
1236 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1238 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1239 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1243 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1244 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1245 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1246 /* gcc-like error if '-' is used */
1248 goto invalid_operands
;
1250 warning("comparison of distinct pointer types lacks a cast");
1254 /* generic gen_op: handles types problems */
1257 int u
, t1
, t2
, bt1
, bt2
, t
;
1260 t1
= vtop
[-1].type
.t
;
1261 t2
= vtop
[0].type
.t
;
1262 bt1
= t1
& VT_BTYPE
;
1263 bt2
= t2
& VT_BTYPE
;
1265 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1266 /* at least one operand is a pointer */
1267 /* relationnal op: must be both pointers */
1268 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1269 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1270 /* pointers are handled are unsigned */
1271 #ifdef TCC_TARGET_X86_64
1272 t
= VT_LLONG
| VT_UNSIGNED
;
1274 t
= VT_INT
| VT_UNSIGNED
;
1278 /* if both pointers, then it must be the '-' op */
1279 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1281 error("cannot use pointers here");
1282 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1283 /* XXX: check that types are compatible */
1284 u
= pointed_size(&vtop
[-1].type
);
1286 /* set to integer type */
1287 #ifdef TCC_TARGET_X86_64
1288 vtop
->type
.t
= VT_LLONG
;
1290 vtop
->type
.t
= VT_INT
;
1295 /* exactly one pointer : must be '+' or '-'. */
1296 if (op
!= '-' && op
!= '+')
1297 error("cannot use pointers here");
1298 /* Put pointer as first operand */
1299 if (bt2
== VT_PTR
) {
1303 type1
= vtop
[-1].type
;
1304 #ifdef TCC_TARGET_X86_64
1305 vpushll(pointed_size(&vtop
[-1].type
));
1307 /* XXX: cast to int ? (long long case) */
1308 vpushi(pointed_size(&vtop
[-1].type
));
1311 #ifdef CONFIG_TCC_BCHECK
1312 /* if evaluating constant expression, no code should be
1313 generated, so no bound check */
1314 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1315 /* if bounded pointers, we generate a special code to
1322 gen_bounded_ptr_add();
1328 /* put again type if gen_opic() swaped operands */
1331 } else if (is_float(bt1
) || is_float(bt2
)) {
1332 /* compute bigger type and do implicit casts */
1333 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1335 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1340 /* floats can only be used for a few operations */
1341 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1342 (op
< TOK_ULT
|| op
> TOK_GT
))
1343 error("invalid operands for binary operation");
1345 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1346 /* cast to biggest op */
1348 /* convert to unsigned if it does not fit in a long long */
1349 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1350 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1354 /* integer operations */
1356 /* convert to unsigned if it does not fit in an integer */
1357 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1358 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1361 /* XXX: currently, some unsigned operations are explicit, so
1362 we modify them here */
1363 if (t
& VT_UNSIGNED
) {
1370 else if (op
== TOK_LT
)
1372 else if (op
== TOK_GT
)
1374 else if (op
== TOK_LE
)
1376 else if (op
== TOK_GE
)
1383 /* special case for shifts and long long: we keep the shift as
1385 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1392 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1393 /* relationnal op: the result is an int */
1394 vtop
->type
.t
= VT_INT
;
1401 #ifndef TCC_TARGET_ARM
1402 /* generic itof for unsigned long long case */
1403 void gen_cvt_itof1(int t
)
1405 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1406 (VT_LLONG
| VT_UNSIGNED
)) {
1409 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1410 #if LDOUBLE_SIZE != 8
1411 else if (t
== VT_LDOUBLE
)
1412 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1415 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1419 vtop
->r
= reg_fret(t
);
1426 /* generic ftoi for unsigned long long case */
1427 void gen_cvt_ftoi1(int t
)
1431 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1432 /* not handled natively */
1433 st
= vtop
->type
.t
& VT_BTYPE
;
1435 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1436 #if LDOUBLE_SIZE != 8
1437 else if (st
== VT_LDOUBLE
)
1438 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1441 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1446 vtop
->r2
= REG_LRET
;
1452 /* force char or short cast */
1453 void force_charshort_cast(int t
)
1457 /* XXX: add optimization if lvalue : just change type and offset */
1462 if (t
& VT_UNSIGNED
) {
1463 vpushi((1 << bits
) - 1);
1469 /* result must be signed or the SAR is converted to an SHL
1470 This was not the case when "t" was a signed short
1471 and the last value on the stack was an unsigned int */
1472 vtop
->type
.t
&= ~VT_UNSIGNED
;
1478 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1479 static void gen_cast(CType
*type
)
1481 int sbt
, dbt
, sf
, df
, c
, p
;
1483 /* special delayed cast for char/short */
1484 /* XXX: in some cases (multiple cascaded casts), it may still
1486 if (vtop
->r
& VT_MUSTCAST
) {
1487 vtop
->r
&= ~VT_MUSTCAST
;
1488 force_charshort_cast(vtop
->type
.t
);
1491 /* bitfields first get cast to ints */
1492 if (vtop
->type
.t
& VT_BITFIELD
) {
1496 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1497 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1502 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1503 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1505 /* constant case: we can do it now */
1506 /* XXX: in ISOC, cannot do it if error in convert */
1507 if (sbt
== VT_FLOAT
)
1508 vtop
->c
.ld
= vtop
->c
.f
;
1509 else if (sbt
== VT_DOUBLE
)
1510 vtop
->c
.ld
= vtop
->c
.d
;
1513 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1514 if (sbt
& VT_UNSIGNED
)
1515 vtop
->c
.ld
= vtop
->c
.ull
;
1517 vtop
->c
.ld
= vtop
->c
.ll
;
1519 if (sbt
& VT_UNSIGNED
)
1520 vtop
->c
.ld
= vtop
->c
.ui
;
1522 vtop
->c
.ld
= vtop
->c
.i
;
1525 if (dbt
== VT_FLOAT
)
1526 vtop
->c
.f
= (float)vtop
->c
.ld
;
1527 else if (dbt
== VT_DOUBLE
)
1528 vtop
->c
.d
= (double)vtop
->c
.ld
;
1529 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1530 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1531 } else if (sf
&& dbt
== VT_BOOL
) {
1532 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1535 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1536 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1537 vtop
->c
.ll
= vtop
->c
.ull
;
1538 else if (sbt
& VT_UNSIGNED
)
1539 vtop
->c
.ll
= vtop
->c
.ui
;
1540 else if (sbt
!= VT_LLONG
)
1541 vtop
->c
.ll
= vtop
->c
.i
;
1543 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1544 vtop
->c
.ull
= vtop
->c
.ll
;
1545 else if (dbt
== VT_BOOL
)
1546 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1547 else if (dbt
!= VT_LLONG
) {
1549 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1551 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1554 if(dbt
& VT_UNSIGNED
)
1555 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1557 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1560 } else if (p
&& dbt
== VT_BOOL
) {
1563 } else if (!nocode_wanted
) {
1564 /* non constant case: generate code */
1566 /* convert from fp to fp */
1569 /* convert int to fp */
1572 /* convert fp to int */
1573 if (dbt
== VT_BOOL
) {
1577 /* we handle char/short/etc... with generic code */
1578 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1579 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1583 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1584 /* additional cast for char/short... */
1589 #ifndef TCC_TARGET_X86_64
1590 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1591 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1592 /* scalar to long long */
1593 /* machine independent conversion */
1595 /* generate high word */
1596 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1600 if (sbt
== VT_PTR
) {
1601 /* cast from pointer to int before we apply
1602 shift operation, which pointers don't support*/
1603 gen_cast(&int_type
);
1609 /* patch second register */
1610 vtop
[-1].r2
= vtop
->r
;
1614 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1615 (dbt
& VT_BTYPE
) == VT_PTR
) {
1616 /* XXX: not sure if this is perfect... need more tests */
1617 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1619 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1620 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1621 /* x86_64 specific: movslq */
1623 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1627 } else if (dbt
== VT_BOOL
) {
1628 /* scalar to bool */
1631 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1632 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1633 if (sbt
== VT_PTR
) {
1634 vtop
->type
.t
= VT_INT
;
1635 warning("nonportable conversion from pointer to char/short");
1637 force_charshort_cast(dbt
);
1638 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1640 if (sbt
== VT_LLONG
) {
1641 /* from long long: just take low order word */
1645 /* if lvalue and single word type, nothing to do because
1646 the lvalue already contains the real type size (see
1647 VT_LVAL_xxx constants) */
1650 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1651 /* if we are casting between pointer types,
1652 we must update the VT_LVAL_xxx size */
1653 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1654 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1659 /* return type size. Put alignment at 'a' */
1660 static int type_size(CType
*type
, int *a
)
1665 bt
= type
->t
& VT_BTYPE
;
1666 if (bt
== VT_STRUCT
) {
1671 } else if (bt
== VT_PTR
) {
1672 if (type
->t
& VT_ARRAY
) {
1676 ts
= type_size(&s
->type
, a
);
1678 if (ts
< 0 && s
->c
< 0)
1686 } else if (bt
== VT_LDOUBLE
) {
1688 return LDOUBLE_SIZE
;
1689 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1690 #ifdef TCC_TARGET_I386
1691 #ifdef TCC_TARGET_PE
1696 #elif defined(TCC_TARGET_ARM)
1706 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1709 } else if (bt
== VT_SHORT
) {
1713 /* char, void, function, _Bool */
1719 /* return the pointed type of t */
1720 static inline CType
*pointed_type(CType
*type
)
1722 return &type
->ref
->type
;
1725 /* modify type so that its it is a pointer to type. */
1726 static void mk_pointer(CType
*type
)
1729 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1730 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1734 /* compare function types. OLD functions match any new functions */
1735 static int is_compatible_func(CType
*type1
, CType
*type2
)
1741 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1743 /* check func_call */
1744 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1746 /* XXX: not complete */
1747 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
1751 while (s1
!= NULL
) {
1754 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
1764 /* return true if type1 and type2 are the same. If unqualified is
1765 true, qualifiers on the types are ignored.
1767 - enums are not checked as gcc __builtin_types_compatible_p ()
1769 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
1773 t1
= type1
->t
& VT_TYPE
;
1774 t2
= type2
->t
& VT_TYPE
;
1776 /* strip qualifiers before comparing */
1777 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1778 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1780 /* XXX: bitfields ? */
1783 /* test more complicated cases */
1784 bt1
= t1
& VT_BTYPE
;
1785 if (bt1
== VT_PTR
) {
1786 type1
= pointed_type(type1
);
1787 type2
= pointed_type(type2
);
1788 return is_compatible_types(type1
, type2
);
1789 } else if (bt1
== VT_STRUCT
) {
1790 return (type1
->ref
== type2
->ref
);
1791 } else if (bt1
== VT_FUNC
) {
1792 return is_compatible_func(type1
, type2
);
1798 /* return true if type1 and type2 are exactly the same (including
1801 static int is_compatible_types(CType
*type1
, CType
*type2
)
1803 return compare_types(type1
,type2
,0);
1806 /* return true if type1 and type2 are the same (ignoring qualifiers).
1808 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
1810 return compare_types(type1
,type2
,1);
1813 /* print a type. If 'varstr' is not NULL, then the variable is also
1814 printed in the type */
1816 /* XXX: add array and function pointers */
1817 void type_to_str(char *buf
, int buf_size
,
1818 CType
*type
, const char *varstr
)
1825 t
= type
->t
& VT_TYPE
;
1828 if (t
& VT_CONSTANT
)
1829 pstrcat(buf
, buf_size
, "const ");
1830 if (t
& VT_VOLATILE
)
1831 pstrcat(buf
, buf_size
, "volatile ");
1832 if (t
& VT_UNSIGNED
)
1833 pstrcat(buf
, buf_size
, "unsigned ");
1863 tstr
= "long double";
1865 pstrcat(buf
, buf_size
, tstr
);
1869 if (bt
== VT_STRUCT
)
1873 pstrcat(buf
, buf_size
, tstr
);
1874 v
= type
->ref
->v
& ~SYM_STRUCT
;
1875 if (v
>= SYM_FIRST_ANOM
)
1876 pstrcat(buf
, buf_size
, "<anonymous>");
1878 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
1882 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
1883 pstrcat(buf
, buf_size
, "(");
1885 while (sa
!= NULL
) {
1886 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
1887 pstrcat(buf
, buf_size
, buf1
);
1890 pstrcat(buf
, buf_size
, ", ");
1892 pstrcat(buf
, buf_size
, ")");
1896 pstrcpy(buf1
, sizeof(buf1
), "*");
1898 pstrcat(buf1
, sizeof(buf1
), varstr
);
1899 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
1903 pstrcat(buf
, buf_size
, " ");
1904 pstrcat(buf
, buf_size
, varstr
);
1909 /* verify type compatibility to store vtop in 'dt' type, and generate
1911 static void gen_assign_cast(CType
*dt
)
1913 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
1914 char buf1
[256], buf2
[256];
1917 st
= &vtop
->type
; /* source type */
1918 dbt
= dt
->t
& VT_BTYPE
;
1919 sbt
= st
->t
& VT_BTYPE
;
1920 if (dt
->t
& VT_CONSTANT
)
1921 warning("assignment of read-only location");
1924 /* special cases for pointers */
1925 /* '0' can also be a pointer */
1926 if (is_null_pointer(vtop
))
1928 /* accept implicit pointer to integer cast with warning */
1929 if (is_integer_btype(sbt
)) {
1930 warning("assignment makes pointer from integer without a cast");
1933 type1
= pointed_type(dt
);
1934 /* a function is implicitely a function pointer */
1935 if (sbt
== VT_FUNC
) {
1936 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
1937 !is_compatible_types(pointed_type(dt
), st
))
1944 type2
= pointed_type(st
);
1945 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1946 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
1947 /* void * can match anything */
1949 /* exact type match, except for unsigned */
1952 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1953 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1954 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
1955 warning("assignment from incompatible pointer type");
1957 /* check const and volatile */
1958 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
1959 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
1960 warning("assignment discards qualifiers from pointer target type");
1966 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
1967 warning("assignment makes integer from pointer without a cast");
1969 /* XXX: more tests */
1974 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1975 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1976 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1978 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
1979 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
1980 error("cannot cast '%s' to '%s'", buf1
, buf2
);
1988 /* store vtop in lvalue pushed on stack */
1991 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
1993 ft
= vtop
[-1].type
.t
;
1994 sbt
= vtop
->type
.t
& VT_BTYPE
;
1995 dbt
= ft
& VT_BTYPE
;
1996 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
1997 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
1998 /* optimize char/short casts */
1999 delayed_cast
= VT_MUSTCAST
;
2000 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2001 /* XXX: factorize */
2002 if (ft
& VT_CONSTANT
)
2003 warning("assignment of read-only location");
2006 if (!(ft
& VT_BITFIELD
))
2007 gen_assign_cast(&vtop
[-1].type
);
2010 if (sbt
== VT_STRUCT
) {
2011 /* if structure, only generate pointer */
2012 /* structure assignment : generate memcpy */
2013 /* XXX: optimize if small size */
2014 if (!nocode_wanted
) {
2015 size
= type_size(&vtop
->type
, &align
);
2019 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2020 else if(!(align
& 3))
2021 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2024 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2028 vtop
->type
.t
= VT_PTR
;
2032 vtop
->type
.t
= VT_PTR
;
2044 /* leave source on stack */
2045 } else if (ft
& VT_BITFIELD
) {
2046 /* bitfield store handling */
2047 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2048 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2049 /* remove bit field info to avoid loops */
2050 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2052 /* duplicate source into other register */
2057 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2058 gen_cast(&vtop
[-1].type
);
2059 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2062 /* duplicate destination */
2064 vtop
[-1] = vtop
[-2];
2066 /* mask and shift source */
2067 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2068 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2069 vpushll((1ULL << bit_size
) - 1ULL);
2071 vpushi((1 << bit_size
) - 1);
2077 /* load destination, mask and or with source */
2079 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2080 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2082 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2089 /* pop off shifted source from "duplicate source..." above */
2093 #ifdef CONFIG_TCC_BCHECK
2094 /* bound check case */
2095 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2101 if (!nocode_wanted
) {
2105 #ifdef TCC_TARGET_X86_64
2106 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2111 r
= gv(rc
); /* generate value */
2112 /* if lvalue was saved on stack, must read it */
2113 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2115 t
= get_reg(RC_INT
);
2116 #ifdef TCC_TARGET_X86_64
2121 sv
.r
= VT_LOCAL
| VT_LVAL
;
2122 sv
.c
.ul
= vtop
[-1].c
.ul
;
2124 vtop
[-1].r
= t
| VT_LVAL
;
2127 #ifndef TCC_TARGET_X86_64
2128 /* two word case handling : store second register at word + 4 */
2129 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2131 /* convert to int to increment easily */
2132 vtop
->type
.t
= VT_INT
;
2138 /* XXX: it works because r2 is spilled last ! */
2139 store(vtop
->r2
, vtop
- 1);
2144 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2145 vtop
->r
|= delayed_cast
;
2149 /* post defines POST/PRE add. c is the token ++ or -- */
2150 void inc(int post
, int c
)
2153 vdup(); /* save lvalue */
2155 gv_dup(); /* duplicate value */
2160 vpushi(c
- TOK_MID
);
2162 vstore(); /* store value */
2164 vpop(); /* if post op, return saved value */
2167 /* Parse GNUC __attribute__ extension. Currently, the following
2168 extensions are recognized:
2169 - aligned(n) : set data/function alignment.
2170 - packed : force data alignment to 1
2171 - section(x) : generate data/code in this section.
2172 - unused : currently ignored, but may be used someday.
2173 - regparm(n) : pass function parameters in registers (i386 only)
2175 static void parse_attribute(AttributeDef
*ad
)
2179 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2183 while (tok
!= ')') {
2184 if (tok
< TOK_IDENT
)
2185 expect("attribute name");
2193 expect("section name");
2194 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2203 if (n
<= 0 || (n
& (n
- 1)) != 0)
2204 error("alignment must be a positive power of two");
2217 /* currently, no need to handle it because tcc does not
2218 track unused objects */
2222 /* currently, no need to handle it because tcc does not
2223 track unused objects */
2228 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
2233 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
2235 #ifdef TCC_TARGET_I386
2245 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
2251 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
2255 FUNC_EXPORT(ad
->func_attr
) = 1;
2258 if (tcc_state
->warn_unsupported
)
2259 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2260 /* skip parameters */
2262 int parenthesis
= 0;
2266 else if (tok
== ')')
2269 } while (parenthesis
&& tok
!= -1);
2282 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2283 static void struct_decl(CType
*type
, int u
)
2285 int a
, v
, size
, align
, maxalign
, c
, offset
;
2286 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2287 Sym
*s
, *ss
, *ass
, **ps
;
2291 a
= tok
; /* save decl type */
2296 /* struct already defined ? return it */
2298 expect("struct/union/enum name");
2302 error("invalid type");
2309 /* we put an undefined size for struct/union */
2310 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2311 s
->r
= 0; /* default alignment is zero as gcc */
2312 /* put struct/union/enum name in type */
2320 error("struct/union/enum already defined");
2321 /* cannot be empty */
2323 /* non empty enums are not allowed */
2324 if (a
== TOK_ENUM
) {
2328 expect("identifier");
2334 /* enum symbols have static storage */
2335 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2336 ss
->type
.t
|= VT_STATIC
;
2341 /* NOTE: we accept a trailing comma */
2352 while (tok
!= '}') {
2353 parse_btype(&btype
, &ad
);
2359 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2360 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2361 expect("identifier");
2362 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2363 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2364 error("invalid type for '%s'",
2365 get_tok_str(v
, NULL
));
2369 bit_size
= expr_const();
2370 /* XXX: handle v = 0 case for messages */
2372 error("negative width in bit-field '%s'",
2373 get_tok_str(v
, NULL
));
2374 if (v
&& bit_size
== 0)
2375 error("zero width for bit-field '%s'",
2376 get_tok_str(v
, NULL
));
2378 size
= type_size(&type1
, &align
);
2380 if (align
< ad
.aligned
)
2382 } else if (ad
.packed
) {
2384 } else if (*tcc_state
->pack_stack_ptr
) {
2385 if (align
> *tcc_state
->pack_stack_ptr
)
2386 align
= *tcc_state
->pack_stack_ptr
;
2389 if (bit_size
>= 0) {
2390 bt
= type1
.t
& VT_BTYPE
;
2397 error("bitfields must have scalar type");
2399 if (bit_size
> bsize
) {
2400 error("width of '%s' exceeds its type",
2401 get_tok_str(v
, NULL
));
2402 } else if (bit_size
== bsize
) {
2403 /* no need for bit fields */
2405 } else if (bit_size
== 0) {
2406 /* XXX: what to do if only padding in a
2408 /* zero size: means to pad */
2411 /* we do not have enough room ?
2412 did the type change?
2414 if ((bit_pos
+ bit_size
) > bsize
||
2415 bt
!= prevbt
|| a
== TOK_UNION
)
2418 /* XXX: handle LSB first */
2419 type1
.t
|= VT_BITFIELD
|
2420 (bit_pos
<< VT_STRUCT_SHIFT
) |
2421 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2422 bit_pos
+= bit_size
;
2428 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2429 /* add new memory data only if starting
2431 if (lbit_pos
== 0) {
2432 if (a
== TOK_STRUCT
) {
2433 c
= (c
+ align
- 1) & -align
;
2442 if (align
> maxalign
)
2446 printf("add field %s offset=%d",
2447 get_tok_str(v
, NULL
), offset
);
2448 if (type1
.t
& VT_BITFIELD
) {
2449 printf(" pos=%d size=%d",
2450 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2451 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2456 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2458 while ((ass
= ass
->next
) != NULL
) {
2459 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2464 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2468 if (tok
== ';' || tok
== TOK_EOF
)
2475 /* store size and alignment */
2476 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2482 /* return 0 if no type declaration. otherwise, return the basic type
2485 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2487 int t
, u
, type_found
, typespec_found
, typedef_found
;
2491 memset(ad
, 0, sizeof(AttributeDef
));
2499 /* currently, we really ignore extension */
2509 if ((t
& VT_BTYPE
) != 0)
2510 error("too many basic types");
2526 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2527 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2528 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2529 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2543 if ((t
& VT_BTYPE
) == VT_LONG
) {
2544 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2551 struct_decl(&type1
, VT_ENUM
);
2554 type
->ref
= type1
.ref
;
2558 struct_decl(&type1
, VT_STRUCT
);
2561 /* type modifiers */
2614 /* GNUC attribute */
2615 case TOK_ATTRIBUTE1
:
2616 case TOK_ATTRIBUTE2
:
2617 parse_attribute(ad
);
2624 parse_expr_type(&type1
);
2627 if (typespec_found
|| typedef_found
)
2630 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2633 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2634 type
->ref
= s
->type
.ref
;
2642 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2643 error("signed and unsigned modifier");
2644 if (tcc_state
->char_is_unsigned
) {
2645 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2650 /* long is never used as type */
2651 if ((t
& VT_BTYPE
) == VT_LONG
)
2652 #ifndef TCC_TARGET_X86_64
2653 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2655 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2661 /* convert a function parameter type (array to pointer and function to
2662 function pointer) */
2663 static inline void convert_parameter_type(CType
*pt
)
2665 /* remove const and volatile qualifiers (XXX: const could be used
2666 to indicate a const function parameter */
2667 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2668 /* array must be transformed to pointer according to ANSI C */
2670 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2675 static void post_type(CType
*type
, AttributeDef
*ad
)
2677 int n
, l
, t1
, arg_size
, align
;
2678 Sym
**plast
, *s
, *first
;
2683 /* function declaration */
2691 /* read param name and compute offset */
2692 if (l
!= FUNC_OLD
) {
2693 if (!parse_btype(&pt
, &ad1
)) {
2695 error("invalid type");
2702 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2704 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2705 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2706 error("parameter declared as void");
2707 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
2712 expect("identifier");
2716 convert_parameter_type(&pt
);
2717 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
2723 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
2730 /* if no parameters, then old type prototype */
2734 t1
= type
->t
& VT_STORAGE
;
2735 /* NOTE: const is ignored in returned type as it has a special
2736 meaning in gcc / C++ */
2737 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
2738 post_type(type
, ad
);
2739 /* we push a anonymous symbol which will contain the function prototype */
2740 FUNC_ARGS(ad
->func_attr
) = arg_size
;
2741 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
2743 type
->t
= t1
| VT_FUNC
;
2745 } else if (tok
== '[') {
2746 /* array definition */
2748 if (tok
== TOK_RESTRICT1
)
2754 error("invalid array size");
2757 /* parse next post type */
2758 t1
= type
->t
& VT_STORAGE
;
2759 type
->t
&= ~VT_STORAGE
;
2760 post_type(type
, ad
);
2762 /* we push a anonymous symbol which will contain the array
2764 s
= sym_push(SYM_FIELD
, type
, 0, n
);
2765 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
2770 /* Parse a type declaration (except basic type), and return the type
2771 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2772 expected. 'type' should contain the basic type. 'ad' is the
2773 attribute definition of the basic type. It can be modified by
2776 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
2779 CType type1
, *type2
;
2782 while (tok
== '*') {
2790 qualifiers
|= VT_CONSTANT
;
2795 qualifiers
|= VT_VOLATILE
;
2803 type
->t
|= qualifiers
;
2806 /* XXX: clarify attribute handling */
2807 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2808 parse_attribute(ad
);
2810 /* recursive type */
2811 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2812 type1
.t
= 0; /* XXX: same as int */
2815 /* XXX: this is not correct to modify 'ad' at this point, but
2816 the syntax is not clear */
2817 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2818 parse_attribute(ad
);
2819 type_decl(&type1
, ad
, v
, td
);
2822 /* type identifier */
2823 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
2827 if (!(td
& TYPE_ABSTRACT
))
2828 expect("identifier");
2832 post_type(type
, ad
);
2833 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2834 parse_attribute(ad
);
2837 /* append type at the end of type1 */
2850 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2851 static int lvalue_type(int t
)
2856 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
2858 else if (bt
== VT_SHORT
)
2862 if (t
& VT_UNSIGNED
)
2863 r
|= VT_LVAL_UNSIGNED
;
2867 /* indirection with full error checking and bound check */
2868 static void indir(void)
2870 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
2871 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
2875 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
2877 vtop
->type
= *pointed_type(&vtop
->type
);
2878 /* Arrays and functions are never lvalues */
2879 if (!(vtop
->type
.t
& VT_ARRAY
)
2880 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
2881 vtop
->r
|= lvalue_type(vtop
->type
.t
);
2882 /* if bound checking, the referenced pointer must be checked */
2883 if (tcc_state
->do_bounds_check
)
2884 vtop
->r
|= VT_MUSTBOUND
;
2888 /* pass a parameter to a function and do type checking and casting */
2889 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2894 func_type
= func
->c
;
2895 if (func_type
== FUNC_OLD
||
2896 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2897 /* default casting : only need to convert float to double */
2898 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2902 } else if (arg
== NULL
) {
2903 error("too many arguments to function");
2906 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2907 gen_assign_cast(&type
);
2911 /* parse an expression of the form '(type)' or '(expr)' and return its
2913 static void parse_expr_type(CType
*type
)
2919 if (parse_btype(type
, &ad
)) {
2920 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2927 static void parse_type(CType
*type
)
2932 if (!parse_btype(type
, &ad
)) {
2935 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2938 static void vpush_tokc(int t
)
2942 vsetc(&type
, VT_CONST
, &tokc
);
2945 static void unary(void)
2947 int n
, t
, align
, size
, r
;
2952 /* XXX: GCC 2.95.3 does not generate a table although it should be
2966 vpush_tokc(VT_INT
| VT_UNSIGNED
);
2970 vpush_tokc(VT_LLONG
);
2974 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
2978 vpush_tokc(VT_FLOAT
);
2982 vpush_tokc(VT_DOUBLE
);
2986 vpush_tokc(VT_LDOUBLE
);
2989 case TOK___FUNCTION__
:
2991 goto tok_identifier
;
2997 /* special function name identifier */
2998 len
= strlen(funcname
) + 1;
2999 /* generate char[len] type */
3004 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3005 ptr
= section_ptr_add(data_section
, len
);
3006 memcpy(ptr
, funcname
, len
);
3011 #ifdef TCC_TARGET_PE
3012 t
= VT_SHORT
| VT_UNSIGNED
;
3018 /* string parsing */
3021 if (tcc_state
->warn_write_strings
)
3026 memset(&ad
, 0, sizeof(AttributeDef
));
3027 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3032 if (parse_btype(&type
, &ad
)) {
3033 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3035 /* check ISOC99 compound literal */
3037 /* data is allocated locally by default */
3042 /* all except arrays are lvalues */
3043 if (!(type
.t
& VT_ARRAY
))
3044 r
|= lvalue_type(type
.t
);
3045 memset(&ad
, 0, sizeof(AttributeDef
));
3046 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3051 } else if (tok
== '{') {
3052 /* save all registers */
3054 /* statement expression : we do not accept break/continue
3055 inside as GCC does */
3056 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3071 /* functions names must be treated as function pointers,
3072 except for unary '&' and sizeof. Since we consider that
3073 functions are not lvalues, we only have to handle it
3074 there and in function calls. */
3075 /* arrays can also be used although they are not lvalues */
3076 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3077 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3079 mk_pointer(&vtop
->type
);
3085 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3087 boolean
.t
= VT_BOOL
;
3089 vtop
->c
.i
= !vtop
->c
.i
;
3090 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3091 vtop
->c
.i
= vtop
->c
.i
^ 1;
3094 vseti(VT_JMP
, gtst(1, 0));
3105 /* in order to force cast, we add zero */
3107 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3108 error("pointer not accepted for unary plus");
3118 parse_expr_type(&type
);
3122 size
= type_size(&type
, &align
);
3123 if (t
== TOK_SIZEOF
) {
3125 error("sizeof applied to an incomplete type");
3130 vtop
->type
.t
|= VT_UNSIGNED
;
3133 case TOK_builtin_types_compatible_p
:
3142 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3143 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3144 vpushi(is_compatible_types(&type1
, &type2
));
3147 case TOK_builtin_constant_p
:
3149 int saved_nocode_wanted
, res
;
3152 saved_nocode_wanted
= nocode_wanted
;
3155 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3157 nocode_wanted
= saved_nocode_wanted
;
3162 case TOK_builtin_frame_address
:
3167 if (tok
!= TOK_CINT
) {
3168 error("__builtin_frame_address only takes integers");
3171 error("TCC only supports __builtin_frame_address(0)");
3177 vset(&type
, VT_LOCAL
, 0);
3180 #ifdef TCC_TARGET_X86_64
3181 case TOK_builtin_malloc
:
3183 goto tok_identifier
;
3184 case TOK_builtin_free
:
3186 goto tok_identifier
;
3203 goto tok_identifier
;
3205 /* allow to take the address of a label */
3206 if (tok
< TOK_UIDENT
)
3207 expect("label identifier");
3208 s
= label_find(tok
);
3210 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3212 if (s
->r
== LABEL_DECLARED
)
3213 s
->r
= LABEL_FORWARD
;
3216 s
->type
.t
= VT_VOID
;
3217 mk_pointer(&s
->type
);
3218 s
->type
.t
|= VT_STATIC
;
3220 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3229 expect("identifier");
3233 error("'%s' undeclared", get_tok_str(t
, NULL
));
3234 /* for simple function calls, we tolerate undeclared
3235 external reference to int() function */
3236 if (tcc_state
->warn_implicit_function_declaration
)
3237 warning("implicit declaration of function '%s'",
3238 get_tok_str(t
, NULL
));
3239 s
= external_global_sym(t
, &func_old_type
, 0);
3241 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3242 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3243 /* if referencing an inline function, then we generate a
3244 symbol to it if not already done. It will have the
3245 effect to generate code for it at the end of the
3246 compilation unit. Inline function as always
3247 generated in the text section. */
3249 put_extern_sym(s
, text_section
, 0, 0);
3250 r
= VT_SYM
| VT_CONST
;
3254 vset(&s
->type
, r
, s
->c
);
3255 /* if forward reference, we must point to s */
3256 if (vtop
->r
& VT_SYM
) {
3263 /* post operations */
3265 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3268 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3270 if (tok
== TOK_ARROW
)
3275 /* expect pointer on structure */
3276 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3277 expect("struct or union");
3281 while ((s
= s
->next
) != NULL
) {
3286 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3287 /* add field offset to pointer */
3288 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3291 /* change type to field type, and set to lvalue */
3292 vtop
->type
= s
->type
;
3293 /* an array is never an lvalue */
3294 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3295 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3296 /* if bound checking, the referenced pointer must be checked */
3297 if (tcc_state
->do_bounds_check
)
3298 vtop
->r
|= VT_MUSTBOUND
;
3301 } else if (tok
== '[') {
3307 } else if (tok
== '(') {
3313 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3314 /* pointer test (no array accepted) */
3315 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3316 vtop
->type
= *pointed_type(&vtop
->type
);
3317 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3321 expect("function pointer");
3324 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3326 /* get return type */
3329 sa
= s
->next
; /* first parameter */
3332 /* compute first implicit argument if a structure is returned */
3333 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3334 /* get some space for the returned structure */
3335 size
= type_size(&s
->type
, &align
);
3336 loc
= (loc
- size
) & -align
;
3338 ret
.r
= VT_LOCAL
| VT_LVAL
;
3339 /* pass it as 'int' to avoid structure arg passing
3341 vseti(VT_LOCAL
, loc
);
3346 /* return in register */
3347 if (is_float(ret
.type
.t
)) {
3348 ret
.r
= reg_fret(ret
.type
.t
);
3350 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3359 gfunc_param_typed(s
, sa
);
3369 error("too few arguments to function");
3371 if (!nocode_wanted
) {
3372 gfunc_call(nb_args
);
3374 vtop
-= (nb_args
+ 1);
3377 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3385 static void uneq(void)
3391 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3392 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3393 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3408 static void expr_prod(void)
3413 while (tok
== '*' || tok
== '/' || tok
== '%') {
3421 static void expr_sum(void)
3426 while (tok
== '+' || tok
== '-') {
3434 static void expr_shift(void)
3439 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3447 static void expr_cmp(void)
3452 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3453 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3461 static void expr_cmpeq(void)
3466 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3474 static void expr_and(void)
3477 while (tok
== '&') {
3484 static void expr_xor(void)
3487 while (tok
== '^') {
3494 static void expr_or(void)
3497 while (tok
== '|') {
3504 /* XXX: fix this mess */
3505 static void expr_land_const(void)
3508 while (tok
== TOK_LAND
) {
3515 /* XXX: fix this mess */
3516 static void expr_lor_const(void)
3519 while (tok
== TOK_LOR
) {
3526 /* only used if non constant */
3527 static void expr_land(void)
3532 if (tok
== TOK_LAND
) {
3537 if (tok
!= TOK_LAND
) {
3547 static void expr_lor(void)
3552 if (tok
== TOK_LOR
) {
3557 if (tok
!= TOK_LOR
) {
3567 /* XXX: better constant handling */
3568 static void expr_eq(void)
3570 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3572 CType type
, type1
, type2
;
3579 boolean
.t
= VT_BOOL
;
3585 if (tok
!= ':' || !gnu_ext
) {
3600 if (vtop
!= vstack
) {
3601 /* needed to avoid having different registers saved in
3603 if (is_float(vtop
->type
.t
)) {
3605 #ifdef TCC_TARGET_X86_64
3606 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3616 if (tok
== ':' && gnu_ext
) {
3624 sv
= *vtop
; /* save value to handle it later */
3625 vtop
--; /* no vpop so that FP stack is not flushed */
3633 bt1
= t1
& VT_BTYPE
;
3635 bt2
= t2
& VT_BTYPE
;
3636 /* cast operands to correct type according to ISOC rules */
3637 if (is_float(bt1
) || is_float(bt2
)) {
3638 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3639 type
.t
= VT_LDOUBLE
;
3640 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3645 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3646 /* cast to biggest op */
3648 /* convert to unsigned if it does not fit in a long long */
3649 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3650 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3651 type
.t
|= VT_UNSIGNED
;
3652 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3653 /* XXX: test pointer compatibility */
3655 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3656 /* XXX: test function pointer compatibility */
3658 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3659 /* XXX: test structure compatibility */
3661 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3662 /* NOTE: as an extension, we accept void on only one side */
3665 /* integer operations */
3667 /* convert to unsigned if it does not fit in an integer */
3668 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3669 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3670 type
.t
|= VT_UNSIGNED
;
3673 /* now we convert second operand */
3675 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3678 if (is_float(type
.t
)) {
3680 #ifdef TCC_TARGET_X86_64
3681 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3685 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3686 /* for long longs, we use fixed registers to avoid having
3687 to handle a complicated move */
3692 /* this is horrible, but we must also convert first
3696 /* put again first value and cast it */
3699 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3709 static void gexpr(void)
3720 /* parse an expression and return its type without any side effect. */
3721 static void expr_type(CType
*type
)
3723 int saved_nocode_wanted
;
3725 saved_nocode_wanted
= nocode_wanted
;
3730 nocode_wanted
= saved_nocode_wanted
;
3733 /* parse a unary expression and return its type without any side
3735 static void unary_type(CType
*type
)
3747 /* parse a constant expression and return value in vtop. */
3748 static void expr_const1(void)
3757 /* parse an integer constant and return its value. */
3758 static int expr_const(void)
3762 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3763 expect("constant expression");
3769 /* return the label token if current token is a label, otherwise
3771 static int is_label(void)
3775 /* fast test first */
3776 if (tok
< TOK_UIDENT
)
3778 /* no need to save tokc because tok is an identifier */
3785 unget_tok(last_tok
);
3790 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3791 int case_reg
, int is_expr
)
3796 /* generate line number info */
3797 if (tcc_state
->do_debug
&&
3798 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3799 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3801 last_line_num
= file
->line_num
;
3805 /* default return value is (void) */
3807 vtop
->type
.t
= VT_VOID
;
3810 if (tok
== TOK_IF
) {
3817 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3819 if (c
== TOK_ELSE
) {
3823 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3824 gsym(d
); /* patch else jmp */
3827 } else if (tok
== TOK_WHILE
) {
3835 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3839 } else if (tok
== '{') {
3843 /* record local declaration stack position */
3845 llabel
= local_label_stack
;
3846 /* handle local labels declarations */
3847 if (tok
== TOK_LABEL
) {
3850 if (tok
< TOK_UIDENT
)
3851 expect("label identifier");
3852 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3862 while (tok
!= '}') {
3867 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3870 /* pop locally defined labels */
3871 label_pop(&local_label_stack
, llabel
);
3872 /* pop locally defined symbols */
3874 /* XXX: this solution makes only valgrind happy...
3875 triggered by gcc.c-torture/execute/20000917-1.c */
3877 switch(vtop
->type
.t
& VT_BTYPE
) {
3882 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3884 error("unsupported expression type");
3887 sym_pop(&local_stack
, s
);
3889 } else if (tok
== TOK_RETURN
) {
3893 gen_assign_cast(&func_vt
);
3894 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3896 /* if returning structure, must copy it to implicit
3897 first pointer arg location */
3900 size
= type_size(&func_vt
,&align
);
3903 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3907 loc
= (loc
- size
) & -4;
3910 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3913 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3915 vtop
->type
= int_type
;
3921 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3924 /* copy structure value to pointer */
3929 } else if (is_float(func_vt
.t
)) {
3930 gv(rc_fret(func_vt
.t
));
3934 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3937 rsym
= gjmp(rsym
); /* jmp */
3938 } else if (tok
== TOK_BREAK
) {
3941 error("cannot break");
3942 *bsym
= gjmp(*bsym
);
3945 } else if (tok
== TOK_CONTINUE
) {
3948 error("cannot continue");
3949 *csym
= gjmp(*csym
);
3952 } else if (tok
== TOK_FOR
) {
3979 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3984 if (tok
== TOK_DO
) {
3989 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4000 if (tok
== TOK_SWITCH
) {
4004 /* XXX: other types than integer */
4005 case_reg
= gv(RC_INT
);
4009 b
= gjmp(0); /* jump to first case */
4011 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4012 /* if no default, jmp after switch */
4020 if (tok
== TOK_CASE
) {
4027 if (gnu_ext
&& tok
== TOK_DOTS
) {
4031 warning("empty case range");
4033 /* since a case is like a label, we must skip it with a jmp */
4040 *case_sym
= gtst(1, 0);
4043 *case_sym
= gtst(1, 0);
4047 *case_sym
= gtst(1, *case_sym
);
4052 goto block_after_label
;
4054 if (tok
== TOK_DEFAULT
) {
4060 error("too many 'default'");
4063 goto block_after_label
;
4065 if (tok
== TOK_GOTO
) {
4067 if (tok
== '*' && gnu_ext
) {
4071 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4074 } else if (tok
>= TOK_UIDENT
) {
4075 s
= label_find(tok
);
4076 /* put forward definition if needed */
4078 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4080 if (s
->r
== LABEL_DECLARED
)
4081 s
->r
= LABEL_FORWARD
;
4083 /* label already defined */
4084 if (s
->r
& LABEL_FORWARD
)
4085 s
->next
= (void *)gjmp((long)s
->next
);
4087 gjmp_addr((long)s
->next
);
4090 expect("label identifier");
4093 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4101 if (s
->r
== LABEL_DEFINED
)
4102 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4103 gsym((long)s
->next
);
4104 s
->r
= LABEL_DEFINED
;
4106 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4108 s
->next
= (void *)ind
;
4109 /* we accept this, but it is a mistake */
4112 warning("deprecated use of label at end of compound statement");
4116 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4119 /* expression case */
4134 /* t is the array or struct type. c is the array or struct
4135 address. cur_index/cur_field is the pointer to the current
4136 value. 'size_only' is true if only size info is needed (only used
4138 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4139 int *cur_index
, Sym
**cur_field
,
4143 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4149 if (gnu_ext
&& (l
= is_label()) != 0)
4151 while (tok
== '[' || tok
== '.') {
4153 if (!(type
->t
& VT_ARRAY
))
4154 expect("array type");
4157 index
= expr_const();
4158 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4159 expect("invalid index");
4160 if (tok
== TOK_DOTS
&& gnu_ext
) {
4162 index_last
= expr_const();
4163 if (index_last
< 0 ||
4164 (s
->c
>= 0 && index_last
>= s
->c
) ||
4166 expect("invalid index");
4172 *cur_index
= index_last
;
4173 type
= pointed_type(type
);
4174 elem_size
= type_size(type
, &align
);
4175 c
+= index
* elem_size
;
4176 /* NOTE: we only support ranges for last designator */
4177 nb_elems
= index_last
- index
+ 1;
4178 if (nb_elems
!= 1) {
4187 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4188 expect("struct/union type");
4201 /* XXX: fix this mess by using explicit storage field */
4203 type1
.t
|= (type
->t
& ~VT_TYPE
);
4217 if (type
->t
& VT_ARRAY
) {
4219 type
= pointed_type(type
);
4220 c
+= index
* type_size(type
, &align
);
4224 error("too many field init");
4225 /* XXX: fix this mess by using explicit storage field */
4227 type1
.t
|= (type
->t
& ~VT_TYPE
);
4232 decl_initializer(type
, sec
, c
, 0, size_only
);
4234 /* XXX: make it more general */
4235 if (!size_only
&& nb_elems
> 1) {
4236 unsigned long c_end
;
4241 error("range init not supported yet for dynamic storage");
4242 c_end
= c
+ nb_elems
* elem_size
;
4243 if (c_end
> sec
->data_allocated
)
4244 section_realloc(sec
, c_end
);
4245 src
= sec
->data
+ c
;
4247 for(i
= 1; i
< nb_elems
; i
++) {
4249 memcpy(dst
, src
, elem_size
);
4255 #define EXPR_CONST 1
4258 /* store a value or an expression directly in global data or in local array */
4259 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4260 int v
, int expr_type
)
4262 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4264 unsigned long long bit_mask
;
4272 /* compound literals must be allocated globally in this case */
4273 saved_global_expr
= global_expr
;
4276 global_expr
= saved_global_expr
;
4277 /* NOTE: symbols are accepted */
4278 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4279 error("initializer element is not constant");
4287 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4290 /* XXX: not portable */
4291 /* XXX: generate error if incorrect relocation */
4292 gen_assign_cast(&dtype
);
4293 bt
= type
->t
& VT_BTYPE
;
4294 /* we'll write at most 12 bytes */
4295 if (c
+ 12 > sec
->data_allocated
) {
4296 section_realloc(sec
, c
+ 12);
4298 ptr
= sec
->data
+ c
;
4299 /* XXX: make code faster ? */
4300 if (!(type
->t
& VT_BITFIELD
)) {
4305 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4306 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4307 bit_mask
= (1LL << bit_size
) - 1;
4309 if ((vtop
->r
& VT_SYM
) &&
4315 (bt
== VT_INT
&& bit_size
!= 32)))
4316 error("initializer element is not computable at load time");
4319 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4321 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4324 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4327 *(double *)ptr
= vtop
->c
.d
;
4330 *(long double *)ptr
= vtop
->c
.ld
;
4333 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4336 if (vtop
->r
& VT_SYM
) {
4337 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
4339 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4344 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4351 /* put zeros for variable based init */
4352 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4355 /* nothing to do because globals are already set to zero */
4357 vpush_global_sym(&func_old_type
, TOK_memset
);
4365 /* 't' contains the type and storage info. 'c' is the offset of the
4366 object in section 'sec'. If 'sec' is NULL, it means stack based
4367 allocation. 'first' is true if array '{' must be read (multi
4368 dimension implicit array init handling). 'size_only' is true if
4369 size only evaluation is wanted (only for arrays). */
4370 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4371 int first
, int size_only
)
4373 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4374 int size1
, align1
, expr_type
;
4378 if (type
->t
& VT_ARRAY
) {
4382 t1
= pointed_type(type
);
4383 size1
= type_size(t1
, &align1
);
4386 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4392 /* only parse strings here if correct type (otherwise: handle
4393 them as ((w)char *) expressions */
4394 if ((tok
== TOK_LSTR
&&
4395 #ifdef TCC_TARGET_PE
4396 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4398 (t1
->t
& VT_BTYPE
) == VT_INT
4400 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4401 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4406 /* compute maximum number of chars wanted */
4408 cstr_len
= cstr
->size
;
4410 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4413 if (n
>= 0 && nb
> (n
- array_length
))
4414 nb
= n
- array_length
;
4417 warning("initializer-string for array is too long");
4418 /* in order to go faster for common case (char
4419 string in global variable, we handle it
4421 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4422 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4426 ch
= ((unsigned char *)cstr
->data
)[i
];
4428 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4429 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4437 /* only add trailing zero if enough storage (no
4438 warning in this case since it is standard) */
4439 if (n
< 0 || array_length
< n
) {
4441 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4447 while (tok
!= '}') {
4448 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4449 if (n
>= 0 && index
>= n
)
4450 error("index too large");
4451 /* must put zero in holes (note that doing it that way
4452 ensures that it even works with designators) */
4453 if (!size_only
&& array_length
< index
) {
4454 init_putz(t1
, sec
, c
+ array_length
* size1
,
4455 (index
- array_length
) * size1
);
4458 if (index
> array_length
)
4459 array_length
= index
;
4460 /* special test for multi dimensional arrays (may not
4461 be strictly correct if designators are used at the
4463 if (index
>= n
&& no_oblock
)
4472 /* put zeros at the end */
4473 if (!size_only
&& n
>= 0 && array_length
< n
) {
4474 init_putz(t1
, sec
, c
+ array_length
* size1
,
4475 (n
- array_length
) * size1
);
4477 /* patch type size if needed */
4479 s
->c
= array_length
;
4480 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4481 (sec
|| !first
|| tok
== '{')) {
4484 /* NOTE: the previous test is a specific case for automatic
4485 struct/union init */
4486 /* XXX: union needs only one init */
4488 /* XXX: this test is incorrect for local initializers
4489 beginning with ( without {. It would be much more difficult
4490 to do it correctly (ideally, the expression parser should
4491 be used in all cases) */
4497 while (tok
== '(') {
4501 if (!parse_btype(&type1
, &ad1
))
4503 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4505 if (!is_assignable_types(type
, &type1
))
4506 error("invalid type for cast");
4511 if (first
|| tok
== '{') {
4520 while (tok
!= '}') {
4521 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4523 if (!size_only
&& array_length
< index
) {
4524 init_putz(type
, sec
, c
+ array_length
,
4525 index
- array_length
);
4527 index
= index
+ type_size(&f
->type
, &align1
);
4528 if (index
> array_length
)
4529 array_length
= index
;
4531 if (no_oblock
&& f
== NULL
)
4537 /* put zeros at the end */
4538 if (!size_only
&& array_length
< n
) {
4539 init_putz(type
, sec
, c
+ array_length
,
4548 } else if (tok
== '{') {
4550 decl_initializer(type
, sec
, c
, first
, size_only
);
4552 } else if (size_only
) {
4553 /* just skip expression */
4555 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4559 else if (tok
== ')')
4564 /* currently, we always use constant expression for globals
4565 (may change for scripting case) */
4566 expr_type
= EXPR_CONST
;
4568 expr_type
= EXPR_ANY
;
4569 init_putv(type
, sec
, c
, 0, expr_type
);
4573 /* parse an initializer for type 't' if 'has_init' is non zero, and
4574 allocate space in local or global data space ('r' is either
4575 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4576 variable 'v' of scope 'scope' is declared before initializers are
4577 parsed. If 'v' is zero, then a reference to the new object is put
4578 in the value stack. If 'has_init' is 2, a special parsing is done
4579 to handle string constants. */
4580 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4581 int has_init
, int v
, int scope
)
4583 int size
, align
, addr
, data_offset
;
4585 ParseState saved_parse_state
= {0};
4586 TokenString init_str
;
4589 size
= type_size(type
, &align
);
4590 /* If unknown size, we must evaluate it before
4591 evaluating initializers because
4592 initializers can generate global data too
4593 (e.g. string pointers or ISOC99 compound
4594 literals). It also simplifies local
4595 initializers handling */
4596 tok_str_new(&init_str
);
4599 error("unknown type size");
4600 /* get all init string */
4601 if (has_init
== 2) {
4602 /* only get strings */
4603 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4604 tok_str_add_tok(&init_str
);
4609 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4611 error("unexpected end of file in initializer");
4612 tok_str_add_tok(&init_str
);
4615 else if (tok
== '}') {
4625 tok_str_add(&init_str
, -1);
4626 tok_str_add(&init_str
, 0);
4629 save_parse_state(&saved_parse_state
);
4631 macro_ptr
= init_str
.str
;
4633 decl_initializer(type
, NULL
, 0, 1, 1);
4634 /* prepare second initializer parsing */
4635 macro_ptr
= init_str
.str
;
4638 /* if still unknown size, error */
4639 size
= type_size(type
, &align
);
4641 error("unknown type size");
4643 /* take into account specified alignment if bigger */
4645 if (ad
->aligned
> align
)
4646 align
= ad
->aligned
;
4647 } else if (ad
->packed
) {
4650 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4652 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4654 loc
= (loc
- size
) & -align
;
4656 /* handles bounds */
4657 /* XXX: currently, since we do only one pass, we cannot track
4658 '&' operators, so we add only arrays */
4659 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4660 unsigned long *bounds_ptr
;
4661 /* add padding between regions */
4663 /* then add local bound info */
4664 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4665 bounds_ptr
[0] = addr
;
4666 bounds_ptr
[1] = size
;
4669 /* local variable */
4670 sym_push(v
, type
, r
, addr
);
4672 /* push local reference */
4673 vset(type
, r
, addr
);
4679 if (v
&& scope
== VT_CONST
) {
4680 /* see if the symbol was already defined */
4683 if (!is_compatible_types(&sym
->type
, type
))
4684 error("incompatible types for redefinition of '%s'",
4685 get_tok_str(v
, NULL
));
4686 if (sym
->type
.t
& VT_EXTERN
) {
4687 /* if the variable is extern, it was not allocated */
4688 sym
->type
.t
&= ~VT_EXTERN
;
4689 /* set array size if it was ommited in extern
4691 if ((sym
->type
.t
& VT_ARRAY
) &&
4692 sym
->type
.ref
->c
< 0 &&
4694 sym
->type
.ref
->c
= type
->ref
->c
;
4696 /* we accept several definitions of the same
4697 global variable. this is tricky, because we
4698 must play with the SHN_COMMON type of the symbol */
4699 /* XXX: should check if the variable was already
4700 initialized. It is incorrect to initialized it
4702 /* no init data, we won't add more to the symbol */
4709 /* allocate symbol in corresponding section */
4714 else if (tcc_state
->nocommon
)
4718 data_offset
= sec
->data_offset
;
4719 data_offset
= (data_offset
+ align
- 1) & -align
;
4721 /* very important to increment global pointer at this time
4722 because initializers themselves can create new initializers */
4723 data_offset
+= size
;
4724 /* add padding if bound check */
4725 if (tcc_state
->do_bounds_check
)
4727 sec
->data_offset
= data_offset
;
4728 /* allocate section space to put the data */
4729 if (sec
->sh_type
!= SHT_NOBITS
&&
4730 data_offset
> sec
->data_allocated
)
4731 section_realloc(sec
, data_offset
);
4732 /* align section if needed */
4733 if (align
> sec
->sh_addralign
)
4734 sec
->sh_addralign
= align
;
4736 addr
= 0; /* avoid warning */
4740 if (scope
!= VT_CONST
|| !sym
) {
4741 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4743 /* update symbol definition */
4745 put_extern_sym(sym
, sec
, addr
, size
);
4748 /* put a common area */
4749 put_extern_sym(sym
, NULL
, align
, size
);
4750 /* XXX: find a nicer way */
4751 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4752 esym
->st_shndx
= SHN_COMMON
;
4757 /* push global reference */
4758 sym
= get_sym_ref(type
, sec
, addr
, size
);
4760 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4764 /* handles bounds now because the symbol must be defined
4765 before for the relocation */
4766 if (tcc_state
->do_bounds_check
) {
4767 unsigned long *bounds_ptr
;
4769 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
4770 /* then add global bound info */
4771 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4772 bounds_ptr
[0] = 0; /* relocated */
4773 bounds_ptr
[1] = size
;
4777 decl_initializer(type
, sec
, addr
, 1, 0);
4778 /* restore parse state if needed */
4780 tok_str_free(init_str
.str
);
4781 restore_parse_state(&saved_parse_state
);
4787 void put_func_debug(Sym
*sym
)
4792 /* XXX: we put here a dummy type */
4793 snprintf(buf
, sizeof(buf
), "%s:%c1",
4794 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4795 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4796 cur_text_section
, sym
->c
);
4797 /* //gr gdb wants a line at the function */
4798 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4803 /* parse an old style function declaration list */
4804 /* XXX: check multiple parameter */
4805 static void func_decl_list(Sym
*func_sym
)
4812 /* parse each declaration */
4813 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4814 if (!parse_btype(&btype
, &ad
))
4815 expect("declaration list");
4816 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4817 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4819 /* we accept no variable after */
4823 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4824 /* find parameter in function parameter list */
4827 if ((s
->v
& ~SYM_FIELD
) == v
)
4831 error("declaration for parameter '%s' but no such parameter",
4832 get_tok_str(v
, NULL
));
4834 /* check that no storage specifier except 'register' was given */
4835 if (type
.t
& VT_STORAGE
)
4836 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4837 convert_parameter_type(&type
);
4838 /* we can add the type (NOTE: it could be local to the function) */
4840 /* accept other parameters */
4851 /* parse a function defined by symbol 'sym' and generate its code in
4852 'cur_text_section' */
4853 static void gen_function(Sym
*sym
)
4855 int saved_nocode_wanted
= nocode_wanted
;
4857 ind
= cur_text_section
->data_offset
;
4858 /* NOTE: we patch the symbol size later */
4859 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4860 funcname
= get_tok_str(sym
->v
, NULL
);
4862 /* put debug symbol */
4863 if (tcc_state
->do_debug
)
4864 put_func_debug(sym
);
4865 /* push a dummy symbol to enable local sym storage */
4866 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4867 gfunc_prolog(&sym
->type
);
4869 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4872 cur_text_section
->data_offset
= ind
;
4873 label_pop(&global_label_stack
, NULL
);
4874 sym_pop(&local_stack
, NULL
); /* reset local stack */
4875 /* end of function */
4876 /* patch symbol size */
4877 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4879 if (tcc_state
->do_debug
) {
4880 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4882 /* It's better to crash than to generate wrong code */
4883 cur_text_section
= NULL
;
4884 funcname
= ""; /* for safety */
4885 func_vt
.t
= VT_VOID
; /* for safety */
4886 ind
= 0; /* for safety */
4887 nocode_wanted
= saved_nocode_wanted
;
4890 static void gen_inline_functions(void)
4894 int *str
, inline_generated
;
4896 /* iterate while inline function are referenced */
4898 inline_generated
= 0;
4899 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
4901 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
4902 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
4903 (VT_STATIC
| VT_INLINE
) &&
4905 /* the function was used: generate its code and
4906 convert it to a normal function */
4907 str
= INLINE_DEF(sym
->r
);
4908 sym
->r
= VT_SYM
| VT_CONST
;
4909 sym
->type
.t
&= ~VT_INLINE
;
4913 cur_text_section
= text_section
;
4915 macro_ptr
= NULL
; /* fail safe */
4918 inline_generated
= 1;
4921 if (!inline_generated
)
4925 /* free all remaining inline function tokens */
4926 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
4928 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
4929 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
4930 (VT_STATIC
| VT_INLINE
)) {
4931 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
4932 if (sym
->r
== (VT_SYM
| VT_CONST
)) //gr beware!
4934 str
= INLINE_DEF(sym
->r
);
4936 sym
->r
= 0; /* fail safe */
4941 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4942 static void decl(int l
)
4950 if (!parse_btype(&btype
, &ad
)) {
4951 /* skip redundant ';' */
4952 /* XXX: find more elegant solution */
4957 if (l
== VT_CONST
&&
4958 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
4959 /* global asm block */
4963 /* special test for old K&R protos without explicit int
4964 type. Only accepted when defining global data */
4965 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
4969 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4970 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4972 /* we accept no variable after */
4976 while (1) { /* iterate thru each declaration */
4978 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4982 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
4983 printf("type = '%s'\n", buf
);
4986 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
4987 /* if old style function prototype, we accept a
4990 if (sym
->c
== FUNC_OLD
)
4991 func_decl_list(sym
);
4996 error("cannot use local functions");
4997 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
4998 expect("function definition");
5000 /* reject abstract declarators in function definition */
5002 while ((sym
= sym
->next
) != NULL
)
5003 if (!(sym
->v
& ~SYM_FIELD
))
5004 expect("identifier");
5006 /* XXX: cannot do better now: convert extern line to static inline */
5007 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5008 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5012 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5014 /* specific case: if not func_call defined, we put
5015 the one of the prototype */
5016 /* XXX: should have default value */
5017 r
= sym
->type
.ref
->r
;
5018 if (FUNC_CALL(r
) != FUNC_CDECL
5019 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5020 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5022 FUNC_EXPORT(type
.ref
->r
) = 1;
5024 if (!is_compatible_types(&sym
->type
, &type
)) {
5026 error("incompatible types for redefinition of '%s'",
5027 get_tok_str(v
, NULL
));
5029 /* if symbol is already defined, then put complete type */
5032 /* put function symbol */
5033 sym
= global_identifier_push(v
, type
.t
, 0);
5034 sym
->type
.ref
= type
.ref
;
5037 /* static inline functions are just recorded as a kind
5038 of macro. Their code will be emitted at the end of
5039 the compilation unit only if they are used */
5040 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5041 (VT_INLINE
| VT_STATIC
)) {
5042 TokenString func_str
;
5045 tok_str_new(&func_str
);
5051 error("unexpected end of file");
5052 tok_str_add_tok(&func_str
);
5057 } else if (t
== '}') {
5059 if (block_level
== 0)
5063 tok_str_add(&func_str
, -1);
5064 tok_str_add(&func_str
, 0);
5065 INLINE_DEF(sym
->r
) = func_str
.str
;
5067 /* compute text section */
5068 cur_text_section
= ad
.section
;
5069 if (!cur_text_section
)
5070 cur_text_section
= text_section
;
5071 sym
->r
= VT_SYM
| VT_CONST
;
5076 if (btype
.t
& VT_TYPEDEF
) {
5077 /* save typedefed type */
5078 /* XXX: test storage specifiers ? */
5079 sym
= sym_push(v
, &type
, 0, 0);
5080 sym
->type
.t
|= VT_TYPEDEF
;
5081 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5082 /* external function definition */
5083 /* specific case for func_call attribute */
5085 type
.ref
->r
= ad
.func_attr
;
5086 external_sym(v
, &type
, 0);
5088 /* not lvalue if array */
5090 if (!(type
.t
& VT_ARRAY
))
5091 r
|= lvalue_type(type
.t
);
5092 has_init
= (tok
== '=');
5093 if ((btype
.t
& VT_EXTERN
) ||
5094 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5095 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5096 /* external variable */
5097 /* NOTE: as GCC, uninitialized global static
5098 arrays of null size are considered as
5100 external_sym(v
, &type
, r
);
5102 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5103 if (type
.t
& VT_STATIC
)
5109 decl_initializer_alloc(&type
, &ad
, r
,