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
)
66 vsetc(&ctype
, VT_CONST
, &cval
);
69 /* Return a static symbol pointing to a section */
70 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
71 unsigned long offset
, unsigned long size
)
77 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
78 sym
->type
.ref
= type
->ref
;
79 sym
->r
= VT_CONST
| VT_SYM
;
80 put_extern_sym(sym
, sec
, offset
, size
);
84 /* push a reference to a section offset by adding a dummy symbol */
85 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
90 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
91 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
94 /* define a new external reference to a symbol 'v' of type 'u' */
95 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
101 /* push forward reference */
102 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
103 s
->type
.ref
= type
->ref
;
104 s
->r
= r
| VT_CONST
| VT_SYM
;
109 /* define a new external reference to a symbol 'v' of type 'u' */
110 static Sym
*external_sym(int v
, CType
*type
, int r
)
116 /* push forward reference */
117 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
118 s
->type
.t
|= VT_EXTERN
;
119 } else if (s
->type
.ref
== func_old_type
.ref
) {
120 s
->type
.ref
= type
->ref
;
121 s
->r
= r
| VT_CONST
| VT_SYM
;
122 s
->type
.t
|= VT_EXTERN
;
123 } else if (!is_compatible_types(&s
->type
, type
)) {
124 error("incompatible types for redefinition of '%s'",
125 get_tok_str(v
, NULL
));
130 /* push a reference to global symbol v */
131 static void vpush_global_sym(CType
*type
, int v
)
136 sym
= external_global_sym(v
, type
, 0);
138 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
142 void vset(CType
*type
, int r
, int v
)
147 vsetc(type
, r
, &cval
);
150 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 #ifndef TCC_TARGET_X86_64
457 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
462 #ifndef TCC_TARGET_X86_64
463 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
465 unsigned long long ll
;
466 /* two register type load : expand to two words
468 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
471 vtop
->c
.ui
= ll
; /* first word */
473 vtop
->r
= r
; /* save register value */
474 vpushi(ll
>> 32); /* second word */
475 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
476 (vtop
->r
& VT_LVAL
)) {
477 /* We do not want to modifier the long long
478 pointer here, so the safest (and less
479 efficient) is to save all the other registers
480 in the stack. XXX: totally inefficient. */
482 /* load from memory */
485 vtop
[-1].r
= r
; /* save register value */
486 /* increment pointer to get second word */
487 vtop
->type
.t
= VT_INT
;
496 vtop
[-1].r
= r
; /* save register value */
497 vtop
->r
= vtop
[-1].r2
;
499 /* allocate second register */
503 /* write second register */
507 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
509 /* lvalue of scalar type : need to use lvalue type
510 because of possible cast */
513 /* compute memory access type */
514 if (vtop
->r
& VT_LVAL_BYTE
)
516 else if (vtop
->r
& VT_LVAL_SHORT
)
518 if (vtop
->r
& VT_LVAL_UNSIGNED
)
522 /* restore wanted type */
525 /* one register type load */
530 #ifdef TCC_TARGET_C67
531 /* uses register pairs for doubles */
532 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
539 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
540 void gv2(int rc1
, int rc2
)
544 /* generate more generic register first. But VT_JMP or VT_CMP
545 values must be generated first in all cases to avoid possible
547 v
= vtop
[0].r
& VT_VALMASK
;
548 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
553 /* test if reload is needed for first register */
554 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
564 /* test if reload is needed for first register */
565 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
571 /* wrapper around RC_FRET to return a register by type */
574 #ifdef TCC_TARGET_X86_64
575 if (t
== VT_LDOUBLE
) {
582 /* wrapper around REG_FRET to return a register by type */
585 #ifdef TCC_TARGET_X86_64
586 if (t
== VT_LDOUBLE
) {
593 /* expand long long on stack in two int registers */
598 u
= vtop
->type
.t
& VT_UNSIGNED
;
601 vtop
[0].r
= vtop
[-1].r2
;
602 vtop
[0].r2
= VT_CONST
;
603 vtop
[-1].r2
= VT_CONST
;
604 vtop
[0].type
.t
= VT_INT
| u
;
605 vtop
[-1].type
.t
= VT_INT
| u
;
608 #ifdef TCC_TARGET_ARM
609 /* expand long long on stack */
610 void lexpand_nr(void)
614 u
= vtop
->type
.t
& VT_UNSIGNED
;
617 vtop
->type
.t
= VT_INT
| u
;
618 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
620 vtop
[-1].c
.ui
= vtop
->c
.ull
;
621 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
623 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
625 vtop
->r
= vtop
[-1].r
;
626 } else if (v
> VT_CONST
) {
630 vtop
->r
= vtop
[-1].r2
;
631 vtop
[-1].r2
= VT_CONST
;
632 vtop
[-1].type
.t
= VT_INT
| u
;
636 /* build a long long from two ints */
640 vtop
[-1].r2
= vtop
[0].r
;
645 /* rotate n first stack elements to the bottom
646 I1 ... In -> I2 ... In I1 [top is right]
659 /* rotate n first stack elements to the top
660 I1 ... In -> In I1 ... I(n-1) [top is right]
668 for(i
= 0;i
< n
- 1; i
++)
669 vtop
[-i
] = vtop
[-i
- 1];
673 #ifdef TCC_TARGET_ARM
674 /* like vrott but in other direction
675 In ... I1 -> I(n-1) ... I1 In [top is right]
683 for(i
= n
- 1; i
> 0; i
--)
684 vtop
[-i
] = vtop
[-i
+ 1];
689 /* pop stack value */
693 v
= vtop
->r
& VT_VALMASK
;
694 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
695 /* for x86, we need to pop the FP stack */
696 if (v
== TREG_ST0
&& !nocode_wanted
) {
697 o(0xd8dd); /* fstp %st(0) */
700 if (v
== VT_JMP
|| v
== VT_JMPI
) {
701 /* need to put correct jump if && or || without test */
707 /* convert stack entry to register and duplicate its value in another
715 if ((t
& VT_BTYPE
) == VT_LLONG
) {
722 /* stack: H L L1 H1 */
730 /* duplicate value */
735 #ifdef TCC_TARGET_X86_64
736 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
746 load(r1
, &sv
); /* move r to r1 */
748 /* duplicates value */
754 #ifndef TCC_TARGET_X86_64
755 /* generate CPU independent (unsigned) long long operations */
758 int t
, a
, b
, op1
, c
, i
;
760 unsigned short reg_iret
= REG_IRET
;
761 unsigned short reg_lret
= REG_LRET
;
770 func
= TOK___udivdi3
;
776 func
= TOK___umoddi3
;
783 /* call generic long long function */
784 vpush_global_sym(&func_old_type
, func
);
802 /* stack: L1 H1 L2 H2 */
810 /* stack: H1 H2 L1 L2 */
816 /* stack: H1 H2 L1 L2 ML MH */
819 /* stack: ML MH H1 H2 L1 L2 */
823 /* stack: ML MH H1 L2 H2 L1 */
828 /* stack: ML MH M1 M2 */
831 } else if (op
== '+' || op
== '-') {
832 /* XXX: add non carry method too (for MIPS or alpha) */
838 /* stack: H1 H2 (L1 op L2) */
841 gen_op(op1
+ 1); /* TOK_xxxC2 */
844 /* stack: H1 H2 (L1 op L2) */
847 /* stack: (L1 op L2) H1 H2 */
849 /* stack: (L1 op L2) (H1 op H2) */
857 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
862 /* stack: L H shift */
864 /* constant: simpler */
865 /* NOTE: all comments are for SHL. the other cases are
866 done by swaping words */
910 /* XXX: should provide a faster fallback on x86 ? */
913 func
= TOK___ashrdi3
;
916 func
= TOK___lshrdi3
;
919 func
= TOK___ashldi3
;
925 /* compare operations */
931 /* stack: L1 H1 L2 H2 */
935 /* stack: L1 L2 H1 H2 */
938 /* when values are equal, we need to compare low words. since
939 the jump is inverted, we invert the test too. */
942 else if (op1
== TOK_GT
)
944 else if (op1
== TOK_ULT
)
946 else if (op1
== TOK_UGT
)
955 /* generate non equal test */
956 /* XXX: NOT PORTABLE yet */
960 #if defined(TCC_TARGET_I386)
962 #elif defined(TCC_TARGET_ARM)
964 o(0x1A000000 | encbranch(ind
, 0, 1));
965 #elif defined(TCC_TARGET_C67)
966 error("not implemented");
972 /* compare low. Always unsigned */
976 else if (op1
== TOK_LE
)
978 else if (op1
== TOK_GT
)
980 else if (op1
== TOK_GE
)
991 /* handle integer constant optimizations and various machine
993 void gen_opic(int op
)
995 int c1
, c2
, t1
, t2
, n
;
998 typedef unsigned long long U
;
1002 t1
= v1
->type
.t
& VT_BTYPE
;
1003 t2
= v2
->type
.t
& VT_BTYPE
;
1007 else if (v1
->type
.t
& VT_UNSIGNED
)
1014 else if (v2
->type
.t
& VT_UNSIGNED
)
1019 /* currently, we cannot do computations with forward symbols */
1020 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1021 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1024 case '+': l1
+= l2
; break;
1025 case '-': l1
-= l2
; break;
1026 case '&': l1
&= l2
; break;
1027 case '^': l1
^= l2
; break;
1028 case '|': l1
|= l2
; break;
1029 case '*': l1
*= l2
; break;
1036 /* if division by zero, generate explicit division */
1039 error("division by zero in constant");
1043 default: l1
/= l2
; break;
1044 case '%': l1
%= l2
; break;
1045 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1046 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1049 case TOK_SHL
: l1
<<= l2
; break;
1050 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1051 case TOK_SAR
: l1
>>= l2
; break;
1053 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1054 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1055 case TOK_EQ
: l1
= l1
== l2
; break;
1056 case TOK_NE
: l1
= l1
!= l2
; break;
1057 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1058 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1059 case TOK_LT
: l1
= l1
< l2
; break;
1060 case TOK_GE
: l1
= l1
>= l2
; break;
1061 case TOK_LE
: l1
= l1
<= l2
; break;
1062 case TOK_GT
: l1
= l1
> l2
; break;
1064 case TOK_LAND
: l1
= l1
&& l2
; break;
1065 case TOK_LOR
: l1
= l1
|| l2
; break;
1072 /* if commutative ops, put c2 as constant */
1073 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1074 op
== '|' || op
== '*')) {
1076 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1077 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1079 /* Filter out NOP operations like x*1, x-0, x&-1... */
1080 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1083 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1084 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1090 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1091 /* try to use shifts instead of muls or divs */
1092 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1101 else if (op
== TOK_PDIV
)
1107 } else if (c2
&& (op
== '+' || op
== '-') &&
1108 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
)
1109 && !(vtop
[-1].sym
->type
.t
& VT_IMPORT
))
1111 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1112 /* symbol + constant case */
1119 if (!nocode_wanted
) {
1120 /* call low level op generator */
1121 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1132 /* generate a floating point operation with constant propagation */
1133 void gen_opif(int op
)
1141 /* currently, we cannot do computations with forward symbols */
1142 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1143 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1145 if (v1
->type
.t
== VT_FLOAT
) {
1148 } else if (v1
->type
.t
== VT_DOUBLE
) {
1156 /* NOTE: we only do constant propagation if finite number (not
1157 NaN or infinity) (ANSI spec) */
1158 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1162 case '+': f1
+= f2
; break;
1163 case '-': f1
-= f2
; break;
1164 case '*': f1
*= f2
; break;
1168 error("division by zero in constant");
1173 /* XXX: also handles tests ? */
1177 /* XXX: overflow test ? */
1178 if (v1
->type
.t
== VT_FLOAT
) {
1180 } else if (v1
->type
.t
== VT_DOUBLE
) {
1188 if (!nocode_wanted
) {
1196 static int pointed_size(CType
*type
)
1199 return type_size(pointed_type(type
), &align
);
1202 static inline int is_null_pointer(SValue
*p
)
1204 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1206 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1207 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1210 static inline int is_integer_btype(int bt
)
1212 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1213 bt
== VT_INT
|| bt
== VT_LLONG
);
1216 /* check types for comparison or substraction of pointers */
1217 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1219 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1222 /* null pointers are accepted for all comparisons as gcc */
1223 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1227 bt1
= type1
->t
& VT_BTYPE
;
1228 bt2
= type2
->t
& VT_BTYPE
;
1229 /* accept comparison between pointer and integer with a warning */
1230 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1231 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1232 warning("comparison between pointer and integer");
1236 /* both must be pointers or implicit function pointers */
1237 if (bt1
== VT_PTR
) {
1238 type1
= pointed_type(type1
);
1239 } else if (bt1
!= VT_FUNC
)
1240 goto invalid_operands
;
1242 if (bt2
== VT_PTR
) {
1243 type2
= pointed_type(type2
);
1244 } else if (bt2
!= VT_FUNC
) {
1246 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1248 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1249 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1253 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1254 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1255 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1256 /* gcc-like error if '-' is used */
1258 goto invalid_operands
;
1260 warning("comparison of distinct pointer types lacks a cast");
1264 /* generic gen_op: handles types problems */
1267 int u
, t1
, t2
, bt1
, bt2
, t
;
1270 t1
= vtop
[-1].type
.t
;
1271 t2
= vtop
[0].type
.t
;
1272 bt1
= t1
& VT_BTYPE
;
1273 bt2
= t2
& VT_BTYPE
;
1275 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1276 /* at least one operand is a pointer */
1277 /* relationnal op: must be both pointers */
1278 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1279 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1280 /* pointers are handled are unsigned */
1281 #ifdef TCC_TARGET_X86_64
1282 t
= VT_LLONG
| VT_UNSIGNED
;
1284 t
= VT_INT
| VT_UNSIGNED
;
1288 /* if both pointers, then it must be the '-' op */
1289 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1291 error("cannot use pointers here");
1292 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1293 /* XXX: check that types are compatible */
1294 u
= pointed_size(&vtop
[-1].type
);
1296 /* set to integer type */
1297 #ifdef TCC_TARGET_X86_64
1298 vtop
->type
.t
= VT_LLONG
;
1300 vtop
->type
.t
= VT_INT
;
1305 /* exactly one pointer : must be '+' or '-'. */
1306 if (op
!= '-' && op
!= '+')
1307 error("cannot use pointers here");
1308 /* Put pointer as first operand */
1309 if (bt2
== VT_PTR
) {
1313 type1
= vtop
[-1].type
;
1314 type1
.t
&= ~VT_ARRAY
;
1315 #ifdef TCC_TARGET_X86_64
1316 vpushll(pointed_size(&vtop
[-1].type
));
1318 /* XXX: cast to int ? (long long case) */
1319 vpushi(pointed_size(&vtop
[-1].type
));
1322 #ifdef CONFIG_TCC_BCHECK
1323 /* if evaluating constant expression, no code should be
1324 generated, so no bound check */
1325 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1326 /* if bounded pointers, we generate a special code to
1333 gen_bounded_ptr_add();
1339 /* put again type if gen_opic() swaped operands */
1342 } else if (is_float(bt1
) || is_float(bt2
)) {
1343 /* compute bigger type and do implicit casts */
1344 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1346 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1351 /* floats can only be used for a few operations */
1352 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1353 (op
< TOK_ULT
|| op
> TOK_GT
))
1354 error("invalid operands for binary operation");
1356 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1357 /* cast to biggest op */
1359 /* convert to unsigned if it does not fit in a long long */
1360 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1361 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1365 /* integer operations */
1367 /* convert to unsigned if it does not fit in an integer */
1368 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1369 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1372 /* XXX: currently, some unsigned operations are explicit, so
1373 we modify them here */
1374 if (t
& VT_UNSIGNED
) {
1381 else if (op
== TOK_LT
)
1383 else if (op
== TOK_GT
)
1385 else if (op
== TOK_LE
)
1387 else if (op
== TOK_GE
)
1394 /* special case for shifts and long long: we keep the shift as
1396 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1403 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1404 /* relationnal op: the result is an int */
1405 vtop
->type
.t
= VT_INT
;
1412 #ifndef TCC_TARGET_ARM
1413 /* generic itof for unsigned long long case */
1414 void gen_cvt_itof1(int t
)
1416 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1417 (VT_LLONG
| VT_UNSIGNED
)) {
1420 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1421 #if LDOUBLE_SIZE != 8
1422 else if (t
== VT_LDOUBLE
)
1423 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1426 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1430 vtop
->r
= reg_fret(t
);
1437 /* generic ftoi for unsigned long long case */
1438 void gen_cvt_ftoi1(int t
)
1442 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1443 /* not handled natively */
1444 st
= vtop
->type
.t
& VT_BTYPE
;
1446 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1447 #if LDOUBLE_SIZE != 8
1448 else if (st
== VT_LDOUBLE
)
1449 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1452 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1457 vtop
->r2
= REG_LRET
;
1463 /* force char or short cast */
1464 void force_charshort_cast(int t
)
1468 /* XXX: add optimization if lvalue : just change type and offset */
1473 if (t
& VT_UNSIGNED
) {
1474 vpushi((1 << bits
) - 1);
1480 /* result must be signed or the SAR is converted to an SHL
1481 This was not the case when "t" was a signed short
1482 and the last value on the stack was an unsigned int */
1483 vtop
->type
.t
&= ~VT_UNSIGNED
;
1489 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1490 static void gen_cast(CType
*type
)
1492 int sbt
, dbt
, sf
, df
, c
, p
;
1494 /* special delayed cast for char/short */
1495 /* XXX: in some cases (multiple cascaded casts), it may still
1497 if (vtop
->r
& VT_MUSTCAST
) {
1498 vtop
->r
&= ~VT_MUSTCAST
;
1499 force_charshort_cast(vtop
->type
.t
);
1502 /* bitfields first get cast to ints */
1503 if (vtop
->type
.t
& VT_BITFIELD
) {
1507 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1508 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1513 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1514 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1516 /* constant case: we can do it now */
1517 /* XXX: in ISOC, cannot do it if error in convert */
1518 if (sbt
== VT_FLOAT
)
1519 vtop
->c
.ld
= vtop
->c
.f
;
1520 else if (sbt
== VT_DOUBLE
)
1521 vtop
->c
.ld
= vtop
->c
.d
;
1524 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1525 if (sbt
& VT_UNSIGNED
)
1526 vtop
->c
.ld
= vtop
->c
.ull
;
1528 vtop
->c
.ld
= vtop
->c
.ll
;
1530 if (sbt
& VT_UNSIGNED
)
1531 vtop
->c
.ld
= vtop
->c
.ui
;
1533 vtop
->c
.ld
= vtop
->c
.i
;
1536 if (dbt
== VT_FLOAT
)
1537 vtop
->c
.f
= (float)vtop
->c
.ld
;
1538 else if (dbt
== VT_DOUBLE
)
1539 vtop
->c
.d
= (double)vtop
->c
.ld
;
1540 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1541 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1542 } else if (sf
&& dbt
== VT_BOOL
) {
1543 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1546 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1547 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1548 vtop
->c
.ll
= vtop
->c
.ull
;
1549 else if (sbt
& VT_UNSIGNED
)
1550 vtop
->c
.ll
= vtop
->c
.ui
;
1551 #ifdef TCC_TARGET_X86_64
1552 else if (sbt
== VT_PTR
)
1555 else if (sbt
!= VT_LLONG
)
1556 vtop
->c
.ll
= vtop
->c
.i
;
1558 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1559 vtop
->c
.ull
= vtop
->c
.ll
;
1560 else if (dbt
== VT_BOOL
)
1561 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1562 else if (dbt
!= VT_LLONG
) {
1564 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1566 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1569 if(dbt
& VT_UNSIGNED
)
1570 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1572 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1575 } else if (p
&& dbt
== VT_BOOL
) {
1578 } else if (!nocode_wanted
) {
1579 /* non constant case: generate code */
1581 /* convert from fp to fp */
1584 /* convert int to fp */
1587 /* convert fp to int */
1588 if (dbt
== VT_BOOL
) {
1592 /* we handle char/short/etc... with generic code */
1593 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1594 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1598 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1599 /* additional cast for char/short... */
1604 #ifndef TCC_TARGET_X86_64
1605 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1606 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1607 /* scalar to long long */
1608 /* machine independent conversion */
1610 /* generate high word */
1611 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1615 if (sbt
== VT_PTR
) {
1616 /* cast from pointer to int before we apply
1617 shift operation, which pointers don't support*/
1618 gen_cast(&int_type
);
1624 /* patch second register */
1625 vtop
[-1].r2
= vtop
->r
;
1629 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1630 (dbt
& VT_BTYPE
) == VT_PTR
) {
1631 /* XXX: not sure if this is perfect... need more tests */
1632 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1634 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1635 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1636 /* x86_64 specific: movslq */
1638 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1642 } else if (dbt
== VT_BOOL
) {
1643 /* scalar to bool */
1646 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1647 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1648 if (sbt
== VT_PTR
) {
1649 vtop
->type
.t
= VT_INT
;
1650 warning("nonportable conversion from pointer to char/short");
1652 force_charshort_cast(dbt
);
1653 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1655 if (sbt
== VT_LLONG
) {
1656 /* from long long: just take low order word */
1660 /* if lvalue and single word type, nothing to do because
1661 the lvalue already contains the real type size (see
1662 VT_LVAL_xxx constants) */
1665 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1666 /* if we are casting between pointer types,
1667 we must update the VT_LVAL_xxx size */
1668 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1669 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1674 /* return type size. Put alignment at 'a' */
1675 static int type_size(CType
*type
, int *a
)
1680 bt
= type
->t
& VT_BTYPE
;
1681 if (bt
== VT_STRUCT
) {
1686 } else if (bt
== VT_PTR
) {
1687 if (type
->t
& VT_ARRAY
) {
1691 ts
= type_size(&s
->type
, a
);
1693 if (ts
< 0 && s
->c
< 0)
1701 } else if (bt
== VT_LDOUBLE
) {
1703 return LDOUBLE_SIZE
;
1704 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1705 #ifdef TCC_TARGET_I386
1706 #ifdef TCC_TARGET_PE
1711 #elif defined(TCC_TARGET_ARM)
1721 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1724 } else if (bt
== VT_SHORT
) {
1728 /* char, void, function, _Bool */
1734 /* return the pointed type of t */
1735 static inline CType
*pointed_type(CType
*type
)
1737 return &type
->ref
->type
;
1740 /* modify type so that its it is a pointer to type. */
1741 static void mk_pointer(CType
*type
)
1744 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1745 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1749 /* compare function types. OLD functions match any new functions */
1750 static int is_compatible_func(CType
*type1
, CType
*type2
)
1756 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1758 /* check func_call */
1759 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1761 /* XXX: not complete */
1762 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
1766 while (s1
!= NULL
) {
1769 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
1779 /* return true if type1 and type2 are the same. If unqualified is
1780 true, qualifiers on the types are ignored.
1782 - enums are not checked as gcc __builtin_types_compatible_p ()
1784 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
1788 t1
= type1
->t
& VT_TYPE
;
1789 t2
= type2
->t
& VT_TYPE
;
1791 /* strip qualifiers before comparing */
1792 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1793 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1795 /* XXX: bitfields ? */
1798 /* test more complicated cases */
1799 bt1
= t1
& VT_BTYPE
;
1800 if (bt1
== VT_PTR
) {
1801 type1
= pointed_type(type1
);
1802 type2
= pointed_type(type2
);
1803 return is_compatible_types(type1
, type2
);
1804 } else if (bt1
== VT_STRUCT
) {
1805 return (type1
->ref
== type2
->ref
);
1806 } else if (bt1
== VT_FUNC
) {
1807 return is_compatible_func(type1
, type2
);
1813 /* return true if type1 and type2 are exactly the same (including
1816 static int is_compatible_types(CType
*type1
, CType
*type2
)
1818 return compare_types(type1
,type2
,0);
1821 /* return true if type1 and type2 are the same (ignoring qualifiers).
1823 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
1825 return compare_types(type1
,type2
,1);
1828 /* print a type. If 'varstr' is not NULL, then the variable is also
1829 printed in the type */
1831 /* XXX: add array and function pointers */
1832 void type_to_str(char *buf
, int buf_size
,
1833 CType
*type
, const char *varstr
)
1840 t
= type
->t
& VT_TYPE
;
1843 if (t
& VT_CONSTANT
)
1844 pstrcat(buf
, buf_size
, "const ");
1845 if (t
& VT_VOLATILE
)
1846 pstrcat(buf
, buf_size
, "volatile ");
1847 if (t
& VT_UNSIGNED
)
1848 pstrcat(buf
, buf_size
, "unsigned ");
1878 tstr
= "long double";
1880 pstrcat(buf
, buf_size
, tstr
);
1884 if (bt
== VT_STRUCT
)
1888 pstrcat(buf
, buf_size
, tstr
);
1889 v
= type
->ref
->v
& ~SYM_STRUCT
;
1890 if (v
>= SYM_FIRST_ANOM
)
1891 pstrcat(buf
, buf_size
, "<anonymous>");
1893 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
1897 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
1898 pstrcat(buf
, buf_size
, "(");
1900 while (sa
!= NULL
) {
1901 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
1902 pstrcat(buf
, buf_size
, buf1
);
1905 pstrcat(buf
, buf_size
, ", ");
1907 pstrcat(buf
, buf_size
, ")");
1911 pstrcpy(buf1
, sizeof(buf1
), "*");
1913 pstrcat(buf1
, sizeof(buf1
), varstr
);
1914 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
1918 pstrcat(buf
, buf_size
, " ");
1919 pstrcat(buf
, buf_size
, varstr
);
1924 /* verify type compatibility to store vtop in 'dt' type, and generate
1926 static void gen_assign_cast(CType
*dt
)
1928 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
1929 char buf1
[256], buf2
[256];
1932 st
= &vtop
->type
; /* source type */
1933 dbt
= dt
->t
& VT_BTYPE
;
1934 sbt
= st
->t
& VT_BTYPE
;
1935 if (dt
->t
& VT_CONSTANT
)
1936 warning("assignment of read-only location");
1939 /* special cases for pointers */
1940 /* '0' can also be a pointer */
1941 if (is_null_pointer(vtop
))
1943 /* accept implicit pointer to integer cast with warning */
1944 if (is_integer_btype(sbt
)) {
1945 warning("assignment makes pointer from integer without a cast");
1948 type1
= pointed_type(dt
);
1949 /* a function is implicitely a function pointer */
1950 if (sbt
== VT_FUNC
) {
1951 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
1952 !is_compatible_types(pointed_type(dt
), st
))
1953 warning("assignment from incompatible pointer type");
1958 type2
= pointed_type(st
);
1959 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1960 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
1961 /* void * can match anything */
1963 /* exact type match, except for unsigned */
1966 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1967 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1968 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
1969 warning("assignment from incompatible pointer type");
1971 /* check const and volatile */
1972 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
1973 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
1974 warning("assignment discards qualifiers from pointer target type");
1980 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
1981 warning("assignment makes integer from pointer without a cast");
1983 /* XXX: more tests */
1988 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1989 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1990 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1992 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
1993 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
1994 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2002 /* store vtop in lvalue pushed on stack */
2005 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2007 ft
= vtop
[-1].type
.t
;
2008 sbt
= vtop
->type
.t
& VT_BTYPE
;
2009 dbt
= ft
& VT_BTYPE
;
2010 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2011 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2012 /* optimize char/short casts */
2013 delayed_cast
= VT_MUSTCAST
;
2014 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2015 /* XXX: factorize */
2016 if (ft
& VT_CONSTANT
)
2017 warning("assignment of read-only location");
2020 if (!(ft
& VT_BITFIELD
))
2021 gen_assign_cast(&vtop
[-1].type
);
2024 if (sbt
== VT_STRUCT
) {
2025 /* if structure, only generate pointer */
2026 /* structure assignment : generate memcpy */
2027 /* XXX: optimize if small size */
2028 if (!nocode_wanted
) {
2029 size
= type_size(&vtop
->type
, &align
);
2033 vtop
->type
.t
= VT_PTR
;
2036 /* address of memcpy() */
2039 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2040 else if(!(align
& 3))
2041 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2044 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2049 vtop
->type
.t
= VT_PTR
;
2058 /* leave source on stack */
2059 } else if (ft
& VT_BITFIELD
) {
2060 /* bitfield store handling */
2061 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2062 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2063 /* remove bit field info to avoid loops */
2064 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2066 /* duplicate source into other register */
2071 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2072 gen_cast(&vtop
[-1].type
);
2073 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2076 /* duplicate destination */
2078 vtop
[-1] = vtop
[-2];
2080 /* mask and shift source */
2081 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2082 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2083 vpushll((1ULL << bit_size
) - 1ULL);
2085 vpushi((1 << bit_size
) - 1);
2091 /* load destination, mask and or with source */
2093 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2094 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2096 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2103 /* pop off shifted source from "duplicate source..." above */
2107 #ifdef CONFIG_TCC_BCHECK
2108 /* bound check case */
2109 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2115 if (!nocode_wanted
) {
2119 #ifdef TCC_TARGET_X86_64
2120 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2125 r
= gv(rc
); /* generate value */
2126 /* if lvalue was saved on stack, must read it */
2127 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2129 t
= get_reg(RC_INT
);
2130 #ifdef TCC_TARGET_X86_64
2135 sv
.r
= VT_LOCAL
| VT_LVAL
;
2136 sv
.c
.ul
= vtop
[-1].c
.ul
;
2138 vtop
[-1].r
= t
| VT_LVAL
;
2141 #ifndef TCC_TARGET_X86_64
2142 /* two word case handling : store second register at word + 4 */
2143 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2145 /* convert to int to increment easily */
2146 vtop
->type
.t
= VT_INT
;
2152 /* XXX: it works because r2 is spilled last ! */
2153 store(vtop
->r2
, vtop
- 1);
2158 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2159 vtop
->r
|= delayed_cast
;
2163 /* post defines POST/PRE add. c is the token ++ or -- */
2164 void inc(int post
, int c
)
2167 vdup(); /* save lvalue */
2169 gv_dup(); /* duplicate value */
2174 vpushi(c
- TOK_MID
);
2176 vstore(); /* store value */
2178 vpop(); /* if post op, return saved value */
2181 /* Parse GNUC __attribute__ extension. Currently, the following
2182 extensions are recognized:
2183 - aligned(n) : set data/function alignment.
2184 - packed : force data alignment to 1
2185 - section(x) : generate data/code in this section.
2186 - unused : currently ignored, but may be used someday.
2187 - regparm(n) : pass function parameters in registers (i386 only)
2189 static void parse_attribute(AttributeDef
*ad
)
2193 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2197 while (tok
!= ')') {
2198 if (tok
< TOK_IDENT
)
2199 expect("attribute name");
2207 expect("section name");
2208 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2217 if (n
<= 0 || (n
& (n
- 1)) != 0)
2218 error("alignment must be a positive power of two");
2231 /* currently, no need to handle it because tcc does not
2232 track unused objects */
2236 /* currently, no need to handle it because tcc does not
2237 track unused objects */
2242 ad
->func_call
= FUNC_CDECL
;
2247 ad
->func_call
= FUNC_STDCALL
;
2249 #ifdef TCC_TARGET_I386
2259 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2265 ad
->func_call
= FUNC_FASTCALLW
;
2269 ad
->func_export
= 1;
2272 ad
->func_import
= 1;
2275 if (tcc_state
->warn_unsupported
)
2276 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2277 /* skip parameters */
2279 int parenthesis
= 0;
2283 else if (tok
== ')')
2286 } while (parenthesis
&& tok
!= -1);
2299 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2300 static void struct_decl(CType
*type
, int u
)
2302 int a
, v
, size
, align
, maxalign
, c
, offset
;
2303 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2304 Sym
*s
, *ss
, *ass
, **ps
;
2308 a
= tok
; /* save decl type */
2313 /* struct already defined ? return it */
2315 expect("struct/union/enum name");
2319 error("invalid type");
2326 /* we put an undefined size for struct/union */
2327 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2328 s
->r
= 0; /* default alignment is zero as gcc */
2329 /* put struct/union/enum name in type */
2337 error("struct/union/enum already defined");
2338 /* cannot be empty */
2340 /* non empty enums are not allowed */
2341 if (a
== TOK_ENUM
) {
2345 expect("identifier");
2351 /* enum symbols have static storage */
2352 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2353 ss
->type
.t
|= VT_STATIC
;
2358 /* NOTE: we accept a trailing comma */
2369 while (tok
!= '}') {
2370 parse_btype(&btype
, &ad
);
2376 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2377 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2378 expect("identifier");
2379 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2380 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2381 error("invalid type for '%s'",
2382 get_tok_str(v
, NULL
));
2386 bit_size
= expr_const();
2387 /* XXX: handle v = 0 case for messages */
2389 error("negative width in bit-field '%s'",
2390 get_tok_str(v
, NULL
));
2391 if (v
&& bit_size
== 0)
2392 error("zero width for bit-field '%s'",
2393 get_tok_str(v
, NULL
));
2395 size
= type_size(&type1
, &align
);
2397 if (align
< ad
.aligned
)
2399 } else if (ad
.packed
) {
2401 } else if (*tcc_state
->pack_stack_ptr
) {
2402 if (align
> *tcc_state
->pack_stack_ptr
)
2403 align
= *tcc_state
->pack_stack_ptr
;
2406 if (bit_size
>= 0) {
2407 bt
= type1
.t
& VT_BTYPE
;
2414 error("bitfields must have scalar type");
2416 if (bit_size
> bsize
) {
2417 error("width of '%s' exceeds its type",
2418 get_tok_str(v
, NULL
));
2419 } else if (bit_size
== bsize
) {
2420 /* no need for bit fields */
2422 } else if (bit_size
== 0) {
2423 /* XXX: what to do if only padding in a
2425 /* zero size: means to pad */
2428 /* we do not have enough room ?
2429 did the type change?
2431 if ((bit_pos
+ bit_size
) > bsize
||
2432 bt
!= prevbt
|| a
== TOK_UNION
)
2435 /* XXX: handle LSB first */
2436 type1
.t
|= VT_BITFIELD
|
2437 (bit_pos
<< VT_STRUCT_SHIFT
) |
2438 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2439 bit_pos
+= bit_size
;
2445 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2446 /* add new memory data only if starting
2448 if (lbit_pos
== 0) {
2449 if (a
== TOK_STRUCT
) {
2450 c
= (c
+ align
- 1) & -align
;
2459 if (align
> maxalign
)
2463 printf("add field %s offset=%d",
2464 get_tok_str(v
, NULL
), offset
);
2465 if (type1
.t
& VT_BITFIELD
) {
2466 printf(" pos=%d size=%d",
2467 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2468 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2473 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2475 while ((ass
= ass
->next
) != NULL
) {
2476 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2481 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2485 if (tok
== ';' || tok
== TOK_EOF
)
2492 /* store size and alignment */
2493 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2499 /* return 0 if no type declaration. otherwise, return the basic type
2502 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2504 int t
, u
, type_found
, typespec_found
, typedef_found
;
2508 memset(ad
, 0, sizeof(AttributeDef
));
2516 /* currently, we really ignore extension */
2526 if ((t
& VT_BTYPE
) != 0)
2527 error("too many basic types");
2543 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2544 #ifndef TCC_TARGET_PE
2545 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2547 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2548 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2562 if ((t
& VT_BTYPE
) == VT_LONG
) {
2563 #ifdef TCC_TARGET_PE
2564 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2566 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2574 struct_decl(&type1
, VT_ENUM
);
2577 type
->ref
= type1
.ref
;
2581 struct_decl(&type1
, VT_STRUCT
);
2584 /* type modifiers */
2637 /* GNUC attribute */
2638 case TOK_ATTRIBUTE1
:
2639 case TOK_ATTRIBUTE2
:
2640 parse_attribute(ad
);
2647 parse_expr_type(&type1
);
2650 if (typespec_found
|| typedef_found
)
2653 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2656 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2657 type
->ref
= s
->type
.ref
;
2659 /* get attributes from typedef */
2660 if (0 == ad
->aligned
)
2661 ad
->aligned
= FUNC_ALIGN(s
->r
);
2662 if (0 == ad
->func_call
)
2663 ad
->func_call
= FUNC_CALL(s
->r
);
2664 ad
->packed
|= FUNC_PACKED(s
->r
);
2673 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2674 error("signed and unsigned modifier");
2675 if (tcc_state
->char_is_unsigned
) {
2676 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2681 /* long is never used as type */
2682 if ((t
& VT_BTYPE
) == VT_LONG
)
2683 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2684 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2686 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2692 /* convert a function parameter type (array to pointer and function to
2693 function pointer) */
2694 static inline void convert_parameter_type(CType
*pt
)
2696 /* remove const and volatile qualifiers (XXX: const could be used
2697 to indicate a const function parameter */
2698 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2699 /* array must be transformed to pointer according to ANSI C */
2701 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2706 static void post_type(CType
*type
, AttributeDef
*ad
)
2708 int n
, l
, t1
, arg_size
, align
;
2709 Sym
**plast
, *s
, *first
;
2714 /* function declaration */
2722 /* read param name and compute offset */
2723 if (l
!= FUNC_OLD
) {
2724 if (!parse_btype(&pt
, &ad1
)) {
2726 error("invalid type");
2733 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2735 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2736 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2737 error("parameter declared as void");
2738 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
2743 expect("identifier");
2747 convert_parameter_type(&pt
);
2748 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
2754 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
2761 /* if no parameters, then old type prototype */
2765 t1
= type
->t
& VT_STORAGE
;
2766 /* NOTE: const is ignored in returned type as it has a special
2767 meaning in gcc / C++ */
2768 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
2769 post_type(type
, ad
);
2770 /* we push a anonymous symbol which will contain the function prototype */
2771 ad
->func_args
= arg_size
;
2772 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
2774 type
->t
= t1
| VT_FUNC
;
2776 } else if (tok
== '[') {
2777 /* array definition */
2779 if (tok
== TOK_RESTRICT1
)
2785 error("invalid array size");
2788 /* parse next post type */
2789 t1
= type
->t
& VT_STORAGE
;
2790 type
->t
&= ~VT_STORAGE
;
2791 post_type(type
, ad
);
2793 /* we push a anonymous symbol which will contain the array
2795 s
= sym_push(SYM_FIELD
, type
, 0, n
);
2796 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
2801 /* Parse a type declaration (except basic type), and return the type
2802 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2803 expected. 'type' should contain the basic type. 'ad' is the
2804 attribute definition of the basic type. It can be modified by
2807 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
2810 CType type1
, *type2
;
2813 while (tok
== '*') {
2821 qualifiers
|= VT_CONSTANT
;
2826 qualifiers
|= VT_VOLATILE
;
2834 type
->t
|= qualifiers
;
2837 /* XXX: clarify attribute handling */
2838 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2839 parse_attribute(ad
);
2841 /* recursive type */
2842 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2843 type1
.t
= 0; /* XXX: same as int */
2846 /* XXX: this is not correct to modify 'ad' at this point, but
2847 the syntax is not clear */
2848 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2849 parse_attribute(ad
);
2850 type_decl(&type1
, ad
, v
, td
);
2853 /* type identifier */
2854 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
2858 if (!(td
& TYPE_ABSTRACT
))
2859 expect("identifier");
2863 post_type(type
, ad
);
2864 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2865 parse_attribute(ad
);
2868 /* append type at the end of type1 */
2881 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2882 static int lvalue_type(int t
)
2887 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
2889 else if (bt
== VT_SHORT
)
2893 if (t
& VT_UNSIGNED
)
2894 r
|= VT_LVAL_UNSIGNED
;
2898 /* indirection with full error checking and bound check */
2899 static void indir(void)
2901 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
2902 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
2906 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
2908 vtop
->type
= *pointed_type(&vtop
->type
);
2909 /* Arrays and functions are never lvalues */
2910 if (!(vtop
->type
.t
& VT_ARRAY
)
2911 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
2912 vtop
->r
|= lvalue_type(vtop
->type
.t
);
2913 /* if bound checking, the referenced pointer must be checked */
2914 #ifdef CONFIG_TCC_BCHECK
2915 if (tcc_state
->do_bounds_check
)
2916 vtop
->r
|= VT_MUSTBOUND
;
2921 /* pass a parameter to a function and do type checking and casting */
2922 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2927 func_type
= func
->c
;
2928 if (func_type
== FUNC_OLD
||
2929 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2930 /* default casting : only need to convert float to double */
2931 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2935 } else if (arg
== NULL
) {
2936 error("too many arguments to function");
2939 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2940 gen_assign_cast(&type
);
2944 /* parse an expression of the form '(type)' or '(expr)' and return its
2946 static void parse_expr_type(CType
*type
)
2952 if (parse_btype(type
, &ad
)) {
2953 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2960 static void parse_type(CType
*type
)
2965 if (!parse_btype(type
, &ad
)) {
2968 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2971 static void vpush_tokc(int t
)
2976 vsetc(&type
, VT_CONST
, &tokc
);
2979 static void unary(void)
2981 int n
, t
, align
, size
, r
;
2986 /* XXX: GCC 2.95.3 does not generate a table although it should be
3000 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3004 vpush_tokc(VT_LLONG
);
3008 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3012 vpush_tokc(VT_FLOAT
);
3016 vpush_tokc(VT_DOUBLE
);
3020 vpush_tokc(VT_LDOUBLE
);
3023 case TOK___FUNCTION__
:
3025 goto tok_identifier
;
3031 /* special function name identifier */
3032 len
= strlen(funcname
) + 1;
3033 /* generate char[len] type */
3038 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3039 ptr
= section_ptr_add(data_section
, len
);
3040 memcpy(ptr
, funcname
, len
);
3045 #ifdef TCC_TARGET_PE
3046 t
= VT_SHORT
| VT_UNSIGNED
;
3052 /* string parsing */
3055 if (tcc_state
->warn_write_strings
)
3060 memset(&ad
, 0, sizeof(AttributeDef
));
3061 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3066 if (parse_btype(&type
, &ad
)) {
3067 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3069 /* check ISOC99 compound literal */
3071 /* data is allocated locally by default */
3076 /* all except arrays are lvalues */
3077 if (!(type
.t
& VT_ARRAY
))
3078 r
|= lvalue_type(type
.t
);
3079 memset(&ad
, 0, sizeof(AttributeDef
));
3080 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3085 } else if (tok
== '{') {
3086 /* save all registers */
3088 /* statement expression : we do not accept break/continue
3089 inside as GCC does */
3090 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3105 /* functions names must be treated as function pointers,
3106 except for unary '&' and sizeof. Since we consider that
3107 functions are not lvalues, we only have to handle it
3108 there and in function calls. */
3109 /* arrays can also be used although they are not lvalues */
3110 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3111 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3113 mk_pointer(&vtop
->type
);
3119 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3121 boolean
.t
= VT_BOOL
;
3123 vtop
->c
.i
= !vtop
->c
.i
;
3124 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3125 vtop
->c
.i
= vtop
->c
.i
^ 1;
3128 vseti(VT_JMP
, gtst(1, 0));
3139 /* in order to force cast, we add zero */
3141 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3142 error("pointer not accepted for unary plus");
3152 parse_expr_type(&type
);
3156 size
= type_size(&type
, &align
);
3157 if (t
== TOK_SIZEOF
) {
3159 error("sizeof applied to an incomplete type");
3164 vtop
->type
.t
|= VT_UNSIGNED
;
3167 case TOK_builtin_types_compatible_p
:
3176 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3177 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3178 vpushi(is_compatible_types(&type1
, &type2
));
3181 case TOK_builtin_constant_p
:
3183 int saved_nocode_wanted
, res
;
3186 saved_nocode_wanted
= nocode_wanted
;
3189 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3191 nocode_wanted
= saved_nocode_wanted
;
3196 case TOK_builtin_frame_address
:
3201 if (tok
!= TOK_CINT
) {
3202 error("__builtin_frame_address only takes integers");
3205 error("TCC only supports __builtin_frame_address(0)");
3211 vset(&type
, VT_LOCAL
, 0);
3214 #ifdef TCC_TARGET_X86_64
3215 case TOK_builtin_malloc
:
3217 goto tok_identifier
;
3218 case TOK_builtin_free
:
3220 goto tok_identifier
;
3237 goto tok_identifier
;
3239 /* allow to take the address of a label */
3240 if (tok
< TOK_UIDENT
)
3241 expect("label identifier");
3242 s
= label_find(tok
);
3244 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3246 if (s
->r
== LABEL_DECLARED
)
3247 s
->r
= LABEL_FORWARD
;
3250 s
->type
.t
= VT_VOID
;
3251 mk_pointer(&s
->type
);
3252 s
->type
.t
|= VT_STATIC
;
3254 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3263 expect("identifier");
3267 error("'%s' undeclared", get_tok_str(t
, NULL
));
3268 /* for simple function calls, we tolerate undeclared
3269 external reference to int() function */
3270 if (tcc_state
->warn_implicit_function_declaration
)
3271 warning("implicit declaration of function '%s'",
3272 get_tok_str(t
, NULL
));
3273 s
= external_global_sym(t
, &func_old_type
, 0);
3275 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3276 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3277 /* if referencing an inline function, then we generate a
3278 symbol to it if not already done. It will have the
3279 effect to generate code for it at the end of the
3280 compilation unit. Inline function as always
3281 generated in the text section. */
3283 put_extern_sym(s
, text_section
, 0, 0);
3284 r
= VT_SYM
| VT_CONST
;
3288 vset(&s
->type
, r
, s
->c
);
3289 /* if forward reference, we must point to s */
3290 if (vtop
->r
& VT_SYM
) {
3297 /* post operations */
3299 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3302 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3305 if (tok
== TOK_ARROW
)
3307 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3311 /* expect pointer on structure */
3312 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3313 expect("struct or union");
3317 while ((s
= s
->next
) != NULL
) {
3322 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3323 /* add field offset to pointer */
3324 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3327 /* change type to field type, and set to lvalue */
3328 vtop
->type
= s
->type
;
3329 vtop
->type
.t
|= qualifiers
;
3330 /* an array is never an lvalue */
3331 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3332 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3333 #ifdef CONFIG_TCC_BCHECK
3334 /* if bound checking, the referenced pointer must be checked */
3335 if (tcc_state
->do_bounds_check
)
3336 vtop
->r
|= VT_MUSTBOUND
;
3340 } else if (tok
== '[') {
3346 } else if (tok
== '(') {
3352 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3353 /* pointer test (no array accepted) */
3354 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3355 vtop
->type
= *pointed_type(&vtop
->type
);
3356 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3360 expect("function pointer");
3363 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3365 /* get return type */
3368 sa
= s
->next
; /* first parameter */
3371 /* compute first implicit argument if a structure is returned */
3372 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3373 /* get some space for the returned structure */
3374 size
= type_size(&s
->type
, &align
);
3375 loc
= (loc
- size
) & -align
;
3377 ret
.r
= VT_LOCAL
| VT_LVAL
;
3378 /* pass it as 'int' to avoid structure arg passing
3380 vseti(VT_LOCAL
, loc
);
3385 /* return in register */
3386 if (is_float(ret
.type
.t
)) {
3387 ret
.r
= reg_fret(ret
.type
.t
);
3389 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3398 gfunc_param_typed(s
, sa
);
3408 error("too few arguments to function");
3410 if (!nocode_wanted
) {
3411 gfunc_call(nb_args
);
3413 vtop
-= (nb_args
+ 1);
3416 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3424 static void uneq(void)
3430 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3431 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3432 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3447 static void expr_prod(void)
3452 while (tok
== '*' || tok
== '/' || tok
== '%') {
3460 static void expr_sum(void)
3465 while (tok
== '+' || tok
== '-') {
3473 static void expr_shift(void)
3478 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3486 static void expr_cmp(void)
3491 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3492 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3500 static void expr_cmpeq(void)
3505 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3513 static void expr_and(void)
3516 while (tok
== '&') {
3523 static void expr_xor(void)
3526 while (tok
== '^') {
3533 static void expr_or(void)
3536 while (tok
== '|') {
3543 /* XXX: fix this mess */
3544 static void expr_land_const(void)
3547 while (tok
== TOK_LAND
) {
3554 /* XXX: fix this mess */
3555 static void expr_lor_const(void)
3558 while (tok
== TOK_LOR
) {
3565 /* only used if non constant */
3566 static void expr_land(void)
3571 if (tok
== TOK_LAND
) {
3576 if (tok
!= TOK_LAND
) {
3586 static void expr_lor(void)
3591 if (tok
== TOK_LOR
) {
3596 if (tok
!= TOK_LOR
) {
3606 /* XXX: better constant handling */
3607 static void expr_eq(void)
3609 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3611 CType type
, type1
, type2
;
3618 boolean
.t
= VT_BOOL
;
3624 if (tok
!= ':' || !gnu_ext
) {
3639 if (vtop
!= vstack
) {
3640 /* needed to avoid having different registers saved in
3642 if (is_float(vtop
->type
.t
)) {
3644 #ifdef TCC_TARGET_X86_64
3645 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3655 if (tok
== ':' && gnu_ext
) {
3663 sv
= *vtop
; /* save value to handle it later */
3664 vtop
--; /* no vpop so that FP stack is not flushed */
3672 bt1
= t1
& VT_BTYPE
;
3674 bt2
= t2
& VT_BTYPE
;
3675 /* cast operands to correct type according to ISOC rules */
3676 if (is_float(bt1
) || is_float(bt2
)) {
3677 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3678 type
.t
= VT_LDOUBLE
;
3679 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3684 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3685 /* cast to biggest op */
3687 /* convert to unsigned if it does not fit in a long long */
3688 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3689 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3690 type
.t
|= VT_UNSIGNED
;
3691 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3692 /* XXX: test pointer compatibility */
3694 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3695 /* XXX: test function pointer compatibility */
3697 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3698 /* XXX: test structure compatibility */
3700 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3701 /* NOTE: as an extension, we accept void on only one side */
3704 /* integer operations */
3706 /* convert to unsigned if it does not fit in an integer */
3707 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3708 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3709 type
.t
|= VT_UNSIGNED
;
3712 /* now we convert second operand */
3714 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3717 if (is_float(type
.t
)) {
3719 #ifdef TCC_TARGET_X86_64
3720 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3724 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3725 /* for long longs, we use fixed registers to avoid having
3726 to handle a complicated move */
3731 /* this is horrible, but we must also convert first
3735 /* put again first value and cast it */
3738 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3748 static void gexpr(void)
3759 /* parse an expression and return its type without any side effect. */
3760 static void expr_type(CType
*type
)
3762 int saved_nocode_wanted
;
3764 saved_nocode_wanted
= nocode_wanted
;
3769 nocode_wanted
= saved_nocode_wanted
;
3772 /* parse a unary expression and return its type without any side
3774 static void unary_type(CType
*type
)
3786 /* parse a constant expression and return value in vtop. */
3787 static void expr_const1(void)
3796 /* parse an integer constant and return its value. */
3797 static int expr_const(void)
3801 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3802 expect("constant expression");
3808 /* return the label token if current token is a label, otherwise
3810 static int is_label(void)
3814 /* fast test first */
3815 if (tok
< TOK_UIDENT
)
3817 /* no need to save tokc because tok is an identifier */
3824 unget_tok(last_tok
);
3829 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3830 int case_reg
, int is_expr
)
3835 /* generate line number info */
3836 if (tcc_state
->do_debug
&&
3837 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3838 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3840 last_line_num
= file
->line_num
;
3844 /* default return value is (void) */
3846 vtop
->type
.t
= VT_VOID
;
3849 if (tok
== TOK_IF
) {
3856 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3858 if (c
== TOK_ELSE
) {
3862 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3863 gsym(d
); /* patch else jmp */
3866 } else if (tok
== TOK_WHILE
) {
3874 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3878 } else if (tok
== '{') {
3882 /* record local declaration stack position */
3884 llabel
= local_label_stack
;
3885 /* handle local labels declarations */
3886 if (tok
== TOK_LABEL
) {
3889 if (tok
< TOK_UIDENT
)
3890 expect("label identifier");
3891 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3901 while (tok
!= '}') {
3906 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3909 /* pop locally defined labels */
3910 label_pop(&local_label_stack
, llabel
);
3911 /* pop locally defined symbols */
3913 /* XXX: this solution makes only valgrind happy...
3914 triggered by gcc.c-torture/execute/20000917-1.c */
3916 switch(vtop
->type
.t
& VT_BTYPE
) {
3921 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3923 error("unsupported expression type");
3926 sym_pop(&local_stack
, s
);
3928 } else if (tok
== TOK_RETURN
) {
3932 gen_assign_cast(&func_vt
);
3933 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3935 /* if returning structure, must copy it to implicit
3936 first pointer arg location */
3939 size
= type_size(&func_vt
,&align
);
3942 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3946 loc
= (loc
- size
) & -4;
3949 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3952 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3954 vtop
->type
= int_type
;
3960 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3963 /* copy structure value to pointer */
3968 } else if (is_float(func_vt
.t
)) {
3969 gv(rc_fret(func_vt
.t
));
3973 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3976 rsym
= gjmp(rsym
); /* jmp */
3977 } else if (tok
== TOK_BREAK
) {
3980 error("cannot break");
3981 *bsym
= gjmp(*bsym
);
3984 } else if (tok
== TOK_CONTINUE
) {
3987 error("cannot continue");
3988 *csym
= gjmp(*csym
);
3991 } else if (tok
== TOK_FOR
) {
4018 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4023 if (tok
== TOK_DO
) {
4028 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4039 if (tok
== TOK_SWITCH
) {
4043 /* XXX: other types than integer */
4044 case_reg
= gv(RC_INT
);
4048 b
= gjmp(0); /* jump to first case */
4050 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4051 /* if no default, jmp after switch */
4059 if (tok
== TOK_CASE
) {
4066 if (gnu_ext
&& tok
== TOK_DOTS
) {
4070 warning("empty case range");
4072 /* since a case is like a label, we must skip it with a jmp */
4079 *case_sym
= gtst(1, 0);
4082 *case_sym
= gtst(1, 0);
4086 *case_sym
= gtst(1, *case_sym
);
4091 goto block_after_label
;
4093 if (tok
== TOK_DEFAULT
) {
4099 error("too many 'default'");
4102 goto block_after_label
;
4104 if (tok
== TOK_GOTO
) {
4106 if (tok
== '*' && gnu_ext
) {
4110 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4113 } else if (tok
>= TOK_UIDENT
) {
4114 s
= label_find(tok
);
4115 /* put forward definition if needed */
4117 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4119 if (s
->r
== LABEL_DECLARED
)
4120 s
->r
= LABEL_FORWARD
;
4122 /* label already defined */
4123 if (s
->r
& LABEL_FORWARD
)
4124 s
->jnext
= gjmp(s
->jnext
);
4126 gjmp_addr(s
->jnext
);
4129 expect("label identifier");
4132 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4140 if (s
->r
== LABEL_DEFINED
)
4141 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4143 s
->r
= LABEL_DEFINED
;
4145 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4148 /* we accept this, but it is a mistake */
4151 warning("deprecated use of label at end of compound statement");
4155 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4158 /* expression case */
4173 /* t is the array or struct type. c is the array or struct
4174 address. cur_index/cur_field is the pointer to the current
4175 value. 'size_only' is true if only size info is needed (only used
4177 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4178 int *cur_index
, Sym
**cur_field
,
4182 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4188 if (gnu_ext
&& (l
= is_label()) != 0)
4190 while (tok
== '[' || tok
== '.') {
4192 if (!(type
->t
& VT_ARRAY
))
4193 expect("array type");
4196 index
= expr_const();
4197 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4198 expect("invalid index");
4199 if (tok
== TOK_DOTS
&& gnu_ext
) {
4201 index_last
= expr_const();
4202 if (index_last
< 0 ||
4203 (s
->c
>= 0 && index_last
>= s
->c
) ||
4205 expect("invalid index");
4211 *cur_index
= index_last
;
4212 type
= pointed_type(type
);
4213 elem_size
= type_size(type
, &align
);
4214 c
+= index
* elem_size
;
4215 /* NOTE: we only support ranges for last designator */
4216 nb_elems
= index_last
- index
+ 1;
4217 if (nb_elems
!= 1) {
4226 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4227 expect("struct/union type");
4240 /* XXX: fix this mess by using explicit storage field */
4242 type1
.t
|= (type
->t
& ~VT_TYPE
);
4256 if (type
->t
& VT_ARRAY
) {
4258 type
= pointed_type(type
);
4259 c
+= index
* type_size(type
, &align
);
4263 error("too many field init");
4264 /* XXX: fix this mess by using explicit storage field */
4266 type1
.t
|= (type
->t
& ~VT_TYPE
);
4271 decl_initializer(type
, sec
, c
, 0, size_only
);
4273 /* XXX: make it more general */
4274 if (!size_only
&& nb_elems
> 1) {
4275 unsigned long c_end
;
4280 error("range init not supported yet for dynamic storage");
4281 c_end
= c
+ nb_elems
* elem_size
;
4282 if (c_end
> sec
->data_allocated
)
4283 section_realloc(sec
, c_end
);
4284 src
= sec
->data
+ c
;
4286 for(i
= 1; i
< nb_elems
; i
++) {
4288 memcpy(dst
, src
, elem_size
);
4294 #define EXPR_CONST 1
4297 /* store a value or an expression directly in global data or in local array */
4298 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4299 int v
, int expr_type
)
4301 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4303 unsigned long long bit_mask
;
4311 /* compound literals must be allocated globally in this case */
4312 saved_global_expr
= global_expr
;
4315 global_expr
= saved_global_expr
;
4316 /* NOTE: symbols are accepted */
4317 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4318 error("initializer element is not constant");
4326 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4329 /* XXX: not portable */
4330 /* XXX: generate error if incorrect relocation */
4331 gen_assign_cast(&dtype
);
4332 bt
= type
->t
& VT_BTYPE
;
4333 /* we'll write at most 12 bytes */
4334 if (c
+ 12 > sec
->data_allocated
) {
4335 section_realloc(sec
, c
+ 12);
4337 ptr
= sec
->data
+ c
;
4338 /* XXX: make code faster ? */
4339 if (!(type
->t
& VT_BITFIELD
)) {
4344 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4345 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4346 bit_mask
= (1LL << bit_size
) - 1;
4348 if ((vtop
->r
& VT_SYM
) &&
4354 (bt
== VT_INT
&& bit_size
!= 32)))
4355 error("initializer element is not computable at load time");
4358 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4360 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4363 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4366 *(double *)ptr
= vtop
->c
.d
;
4369 *(long double *)ptr
= vtop
->c
.ld
;
4372 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4375 if (vtop
->r
& VT_SYM
) {
4376 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4378 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4383 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4390 /* put zeros for variable based init */
4391 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4394 /* nothing to do because globals are already set to zero */
4396 vpush_global_sym(&func_old_type
, TOK_memset
);
4404 /* 't' contains the type and storage info. 'c' is the offset of the
4405 object in section 'sec'. If 'sec' is NULL, it means stack based
4406 allocation. 'first' is true if array '{' must be read (multi
4407 dimension implicit array init handling). 'size_only' is true if
4408 size only evaluation is wanted (only for arrays). */
4409 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4410 int first
, int size_only
)
4412 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4413 int size1
, align1
, expr_type
;
4417 if (type
->t
& VT_ARRAY
) {
4421 t1
= pointed_type(type
);
4422 size1
= type_size(t1
, &align1
);
4425 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4431 /* only parse strings here if correct type (otherwise: handle
4432 them as ((w)char *) expressions */
4433 if ((tok
== TOK_LSTR
&&
4434 #ifdef TCC_TARGET_PE
4435 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4437 (t1
->t
& VT_BTYPE
) == VT_INT
4439 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4440 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4445 /* compute maximum number of chars wanted */
4447 cstr_len
= cstr
->size
;
4449 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4452 if (n
>= 0 && nb
> (n
- array_length
))
4453 nb
= n
- array_length
;
4456 warning("initializer-string for array is too long");
4457 /* in order to go faster for common case (char
4458 string in global variable, we handle it
4460 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4461 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4465 ch
= ((unsigned char *)cstr
->data
)[i
];
4467 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4468 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4476 /* only add trailing zero if enough storage (no
4477 warning in this case since it is standard) */
4478 if (n
< 0 || array_length
< n
) {
4480 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4486 while (tok
!= '}') {
4487 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4488 if (n
>= 0 && index
>= n
)
4489 error("index too large");
4490 /* must put zero in holes (note that doing it that way
4491 ensures that it even works with designators) */
4492 if (!size_only
&& array_length
< index
) {
4493 init_putz(t1
, sec
, c
+ array_length
* size1
,
4494 (index
- array_length
) * size1
);
4497 if (index
> array_length
)
4498 array_length
= index
;
4499 /* special test for multi dimensional arrays (may not
4500 be strictly correct if designators are used at the
4502 if (index
>= n
&& no_oblock
)
4511 /* put zeros at the end */
4512 if (!size_only
&& n
>= 0 && array_length
< n
) {
4513 init_putz(t1
, sec
, c
+ array_length
* size1
,
4514 (n
- array_length
) * size1
);
4516 /* patch type size if needed */
4518 s
->c
= array_length
;
4519 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4520 (sec
|| !first
|| tok
== '{')) {
4523 /* NOTE: the previous test is a specific case for automatic
4524 struct/union init */
4525 /* XXX: union needs only one init */
4527 /* XXX: this test is incorrect for local initializers
4528 beginning with ( without {. It would be much more difficult
4529 to do it correctly (ideally, the expression parser should
4530 be used in all cases) */
4536 while (tok
== '(') {
4540 if (!parse_btype(&type1
, &ad1
))
4542 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4544 if (!is_assignable_types(type
, &type1
))
4545 error("invalid type for cast");
4550 if (first
|| tok
== '{') {
4559 while (tok
!= '}') {
4560 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4562 if (!size_only
&& array_length
< index
) {
4563 init_putz(type
, sec
, c
+ array_length
,
4564 index
- array_length
);
4566 index
= index
+ type_size(&f
->type
, &align1
);
4567 if (index
> array_length
)
4568 array_length
= index
;
4570 /* gr: skip fields from same union - ugly. */
4572 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4573 /* test for same offset */
4574 if (f
->next
->c
!= f
->c
)
4576 /* if yes, test for bitfield shift */
4577 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4578 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4579 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4580 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4581 if (bit_pos_1
!= bit_pos_2
)
4588 if (no_oblock
&& f
== NULL
)
4594 /* put zeros at the end */
4595 if (!size_only
&& array_length
< n
) {
4596 init_putz(type
, sec
, c
+ array_length
,
4605 } else if (tok
== '{') {
4607 decl_initializer(type
, sec
, c
, first
, size_only
);
4609 } else if (size_only
) {
4610 /* just skip expression */
4612 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4616 else if (tok
== ')')
4621 /* currently, we always use constant expression for globals
4622 (may change for scripting case) */
4623 expr_type
= EXPR_CONST
;
4625 expr_type
= EXPR_ANY
;
4626 init_putv(type
, sec
, c
, 0, expr_type
);
4630 /* parse an initializer for type 't' if 'has_init' is non zero, and
4631 allocate space in local or global data space ('r' is either
4632 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4633 variable 'v' of scope 'scope' is declared before initializers are
4634 parsed. If 'v' is zero, then a reference to the new object is put
4635 in the value stack. If 'has_init' is 2, a special parsing is done
4636 to handle string constants. */
4637 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4638 int has_init
, int v
, int scope
)
4640 int size
, align
, addr
, data_offset
;
4642 ParseState saved_parse_state
= {0};
4643 TokenString init_str
;
4646 size
= type_size(type
, &align
);
4647 /* If unknown size, we must evaluate it before
4648 evaluating initializers because
4649 initializers can generate global data too
4650 (e.g. string pointers or ISOC99 compound
4651 literals). It also simplifies local
4652 initializers handling */
4653 tok_str_new(&init_str
);
4656 error("unknown type size");
4657 /* get all init string */
4658 if (has_init
== 2) {
4659 /* only get strings */
4660 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4661 tok_str_add_tok(&init_str
);
4666 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4668 error("unexpected end of file in initializer");
4669 tok_str_add_tok(&init_str
);
4672 else if (tok
== '}') {
4682 tok_str_add(&init_str
, -1);
4683 tok_str_add(&init_str
, 0);
4686 save_parse_state(&saved_parse_state
);
4688 macro_ptr
= init_str
.str
;
4690 decl_initializer(type
, NULL
, 0, 1, 1);
4691 /* prepare second initializer parsing */
4692 macro_ptr
= init_str
.str
;
4695 /* if still unknown size, error */
4696 size
= type_size(type
, &align
);
4698 error("unknown type size");
4700 /* take into account specified alignment if bigger */
4702 if (ad
->aligned
> align
)
4703 align
= ad
->aligned
;
4704 } else if (ad
->packed
) {
4707 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4709 #ifdef CONFIG_TCC_BCHECK
4710 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4713 loc
= (loc
- size
) & -align
;
4715 #ifdef CONFIG_TCC_BCHECK
4716 /* handles bounds */
4717 /* XXX: currently, since we do only one pass, we cannot track
4718 '&' operators, so we add only arrays */
4719 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4720 unsigned long *bounds_ptr
;
4721 /* add padding between regions */
4723 /* then add local bound info */
4724 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4725 bounds_ptr
[0] = addr
;
4726 bounds_ptr
[1] = size
;
4730 /* local variable */
4731 sym_push(v
, type
, r
, addr
);
4733 /* push local reference */
4734 vset(type
, r
, addr
);
4740 if (v
&& scope
== VT_CONST
) {
4741 /* see if the symbol was already defined */
4744 if (!is_compatible_types(&sym
->type
, type
))
4745 error("incompatible types for redefinition of '%s'",
4746 get_tok_str(v
, NULL
));
4747 if (sym
->type
.t
& VT_EXTERN
) {
4748 /* if the variable is extern, it was not allocated */
4749 sym
->type
.t
&= ~VT_EXTERN
;
4750 /* set array size if it was ommited in extern
4752 if ((sym
->type
.t
& VT_ARRAY
) &&
4753 sym
->type
.ref
->c
< 0 &&
4755 sym
->type
.ref
->c
= type
->ref
->c
;
4757 /* we accept several definitions of the same
4758 global variable. this is tricky, because we
4759 must play with the SHN_COMMON type of the symbol */
4760 /* XXX: should check if the variable was already
4761 initialized. It is incorrect to initialized it
4763 /* no init data, we won't add more to the symbol */
4770 /* allocate symbol in corresponding section */
4775 else if (tcc_state
->nocommon
)
4779 data_offset
= sec
->data_offset
;
4780 data_offset
= (data_offset
+ align
- 1) & -align
;
4782 /* very important to increment global pointer at this time
4783 because initializers themselves can create new initializers */
4784 data_offset
+= size
;
4785 #ifdef CONFIG_TCC_BCHECK
4786 /* add padding if bound check */
4787 if (tcc_state
->do_bounds_check
)
4790 sec
->data_offset
= data_offset
;
4791 /* allocate section space to put the data */
4792 if (sec
->sh_type
!= SHT_NOBITS
&&
4793 data_offset
> sec
->data_allocated
)
4794 section_realloc(sec
, data_offset
);
4795 /* align section if needed */
4796 if (align
> sec
->sh_addralign
)
4797 sec
->sh_addralign
= align
;
4799 addr
= 0; /* avoid warning */
4803 if (scope
!= VT_CONST
|| !sym
) {
4804 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4806 /* update symbol definition */
4808 put_extern_sym(sym
, sec
, addr
, size
);
4811 /* put a common area */
4812 put_extern_sym(sym
, NULL
, align
, size
);
4813 /* XXX: find a nicer way */
4814 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4815 esym
->st_shndx
= SHN_COMMON
;
4820 /* push global reference */
4821 sym
= get_sym_ref(type
, sec
, addr
, size
);
4823 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4826 #ifdef CONFIG_TCC_BCHECK
4827 /* handles bounds now because the symbol must be defined
4828 before for the relocation */
4829 if (tcc_state
->do_bounds_check
) {
4830 unsigned long *bounds_ptr
;
4832 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
4833 /* then add global bound info */
4834 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4835 bounds_ptr
[0] = 0; /* relocated */
4836 bounds_ptr
[1] = size
;
4841 decl_initializer(type
, sec
, addr
, 1, 0);
4842 /* restore parse state if needed */
4844 tok_str_free(init_str
.str
);
4845 restore_parse_state(&saved_parse_state
);
4851 void put_func_debug(Sym
*sym
)
4856 /* XXX: we put here a dummy type */
4857 snprintf(buf
, sizeof(buf
), "%s:%c1",
4858 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4859 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4860 cur_text_section
, sym
->c
);
4861 /* //gr gdb wants a line at the function */
4862 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4867 /* parse an old style function declaration list */
4868 /* XXX: check multiple parameter */
4869 static void func_decl_list(Sym
*func_sym
)
4876 /* parse each declaration */
4877 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4878 if (!parse_btype(&btype
, &ad
))
4879 expect("declaration list");
4880 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4881 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4883 /* we accept no variable after */
4887 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4888 /* find parameter in function parameter list */
4891 if ((s
->v
& ~SYM_FIELD
) == v
)
4895 error("declaration for parameter '%s' but no such parameter",
4896 get_tok_str(v
, NULL
));
4898 /* check that no storage specifier except 'register' was given */
4899 if (type
.t
& VT_STORAGE
)
4900 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4901 convert_parameter_type(&type
);
4902 /* we can add the type (NOTE: it could be local to the function) */
4904 /* accept other parameters */
4915 /* parse a function defined by symbol 'sym' and generate its code in
4916 'cur_text_section' */
4917 static void gen_function(Sym
*sym
)
4919 int saved_nocode_wanted
= nocode_wanted
;
4921 ind
= cur_text_section
->data_offset
;
4922 /* NOTE: we patch the symbol size later */
4923 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4924 funcname
= get_tok_str(sym
->v
, NULL
);
4926 /* put debug symbol */
4927 if (tcc_state
->do_debug
)
4928 put_func_debug(sym
);
4929 /* push a dummy symbol to enable local sym storage */
4930 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4931 gfunc_prolog(&sym
->type
);
4933 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4936 cur_text_section
->data_offset
= ind
;
4937 label_pop(&global_label_stack
, NULL
);
4938 sym_pop(&local_stack
, NULL
); /* reset local stack */
4939 /* end of function */
4940 /* patch symbol size */
4941 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4943 if (tcc_state
->do_debug
) {
4944 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4946 /* It's better to crash than to generate wrong code */
4947 cur_text_section
= NULL
;
4948 funcname
= ""; /* for safety */
4949 func_vt
.t
= VT_VOID
; /* for safety */
4950 ind
= 0; /* for safety */
4951 nocode_wanted
= saved_nocode_wanted
;
4954 static void gen_inline_functions(void)
4957 int *str
, inline_generated
, i
;
4958 struct InlineFunc
*fn
;
4960 /* iterate while inline function are referenced */
4962 inline_generated
= 0;
4963 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4964 fn
= tcc_state
->inline_fns
[i
];
4966 if (sym
&& sym
->c
) {
4967 /* the function was used: generate its code and
4968 convert it to a normal function */
4969 str
= fn
->token_str
;
4972 strcpy(file
->filename
, fn
->filename
);
4973 sym
->r
= VT_SYM
| VT_CONST
;
4974 sym
->type
.t
&= ~VT_INLINE
;
4978 cur_text_section
= text_section
;
4980 macro_ptr
= NULL
; /* fail safe */
4982 inline_generated
= 1;
4985 if (!inline_generated
)
4988 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4989 fn
= tcc_state
->inline_fns
[i
];
4990 str
= fn
->token_str
;
4993 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
4996 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4997 static void decl(int l
)
5005 if (!parse_btype(&btype
, &ad
)) {
5006 /* skip redundant ';' */
5007 /* XXX: find more elegant solution */
5012 if (l
== VT_CONST
&&
5013 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5014 /* global asm block */
5018 /* special test for old K&R protos without explicit int
5019 type. Only accepted when defining global data */
5020 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5024 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5025 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5027 /* we accept no variable after */
5031 while (1) { /* iterate thru each declaration */
5033 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5037 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5038 printf("type = '%s'\n", buf
);
5041 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5042 /* if old style function prototype, we accept a
5045 if (sym
->c
== FUNC_OLD
)
5046 func_decl_list(sym
);
5051 error("cannot use local functions");
5052 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5053 expect("function definition");
5055 /* reject abstract declarators in function definition */
5057 while ((sym
= sym
->next
) != NULL
)
5058 if (!(sym
->v
& ~SYM_FIELD
))
5059 expect("identifier");
5061 /* XXX: cannot do better now: convert extern line to static inline */
5062 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5063 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5067 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5070 r
= sym
->type
.ref
->r
;
5071 /* use func_call from prototype if not defined */
5072 if (FUNC_CALL(r
) != FUNC_CDECL
5073 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5074 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5076 /* use export from prototype */
5078 FUNC_EXPORT(type
.ref
->r
) = 1;
5080 /* use static from prototype */
5081 if (sym
->type
.t
& VT_STATIC
)
5082 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5084 if (!is_compatible_types(&sym
->type
, &type
)) {
5086 error("incompatible types for redefinition of '%s'",
5087 get_tok_str(v
, NULL
));
5089 /* if symbol is already defined, then put complete type */
5092 /* put function symbol */
5093 sym
= global_identifier_push(v
, type
.t
, 0);
5094 sym
->type
.ref
= type
.ref
;
5097 /* static inline functions are just recorded as a kind
5098 of macro. Their code will be emitted at the end of
5099 the compilation unit only if they are used */
5100 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5101 (VT_INLINE
| VT_STATIC
)) {
5102 TokenString func_str
;
5104 struct InlineFunc
*fn
;
5105 const char *filename
;
5107 tok_str_new(&func_str
);
5113 error("unexpected end of file");
5114 tok_str_add_tok(&func_str
);
5119 } else if (t
== '}') {
5121 if (block_level
== 0)
5125 tok_str_add(&func_str
, -1);
5126 tok_str_add(&func_str
, 0);
5127 filename
= file
? file
->filename
: "";
5128 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5129 strcpy(fn
->filename
, filename
);
5131 fn
->token_str
= func_str
.str
;
5132 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5135 /* compute text section */
5136 cur_text_section
= ad
.section
;
5137 if (!cur_text_section
)
5138 cur_text_section
= text_section
;
5139 sym
->r
= VT_SYM
| VT_CONST
;
5144 if (btype
.t
& VT_TYPEDEF
) {
5145 /* save typedefed type */
5146 /* XXX: test storage specifiers ? */
5147 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5148 sym
->type
.t
|= VT_TYPEDEF
;
5149 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5150 /* external function definition */
5151 /* specific case for func_call attribute */
5152 type
.ref
->r
= INT_ATTR(&ad
);
5153 external_sym(v
, &type
, 0);
5155 /* not lvalue if array */
5157 if (!(type
.t
& VT_ARRAY
))
5158 r
|= lvalue_type(type
.t
);
5159 has_init
= (tok
== '=');
5160 if ((btype
.t
& VT_EXTERN
) ||
5161 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5162 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5163 /* external variable */
5164 /* NOTE: as GCC, uninitialized global static
5165 arrays of null size are considered as
5167 #ifdef TCC_TARGET_PE
5169 type
.t
|= VT_IMPORT
;
5171 external_sym(v
, &type
, r
);
5173 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5174 if (type
.t
& VT_STATIC
)
5180 #ifdef TCC_TARGET_PE
5182 type
.t
|= VT_EXPORT
;
5184 decl_initializer_alloc(&type
, &ad
, r
,