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 if (tcc_state
->do_bounds_check
)
2915 vtop
->r
|= VT_MUSTBOUND
;
2919 /* pass a parameter to a function and do type checking and casting */
2920 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2925 func_type
= func
->c
;
2926 if (func_type
== FUNC_OLD
||
2927 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2928 /* default casting : only need to convert float to double */
2929 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2933 } else if (arg
== NULL
) {
2934 error("too many arguments to function");
2937 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2938 gen_assign_cast(&type
);
2942 /* parse an expression of the form '(type)' or '(expr)' and return its
2944 static void parse_expr_type(CType
*type
)
2950 if (parse_btype(type
, &ad
)) {
2951 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2958 static void parse_type(CType
*type
)
2963 if (!parse_btype(type
, &ad
)) {
2966 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2969 static void vpush_tokc(int t
)
2974 vsetc(&type
, VT_CONST
, &tokc
);
2977 static void unary(void)
2979 int n
, t
, align
, size
, r
;
2984 /* XXX: GCC 2.95.3 does not generate a table although it should be
2998 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3002 vpush_tokc(VT_LLONG
);
3006 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3010 vpush_tokc(VT_FLOAT
);
3014 vpush_tokc(VT_DOUBLE
);
3018 vpush_tokc(VT_LDOUBLE
);
3021 case TOK___FUNCTION__
:
3023 goto tok_identifier
;
3029 /* special function name identifier */
3030 len
= strlen(funcname
) + 1;
3031 /* generate char[len] type */
3036 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3037 ptr
= section_ptr_add(data_section
, len
);
3038 memcpy(ptr
, funcname
, len
);
3043 #ifdef TCC_TARGET_PE
3044 t
= VT_SHORT
| VT_UNSIGNED
;
3050 /* string parsing */
3053 if (tcc_state
->warn_write_strings
)
3058 memset(&ad
, 0, sizeof(AttributeDef
));
3059 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3064 if (parse_btype(&type
, &ad
)) {
3065 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3067 /* check ISOC99 compound literal */
3069 /* data is allocated locally by default */
3074 /* all except arrays are lvalues */
3075 if (!(type
.t
& VT_ARRAY
))
3076 r
|= lvalue_type(type
.t
);
3077 memset(&ad
, 0, sizeof(AttributeDef
));
3078 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3083 } else if (tok
== '{') {
3084 /* save all registers */
3086 /* statement expression : we do not accept break/continue
3087 inside as GCC does */
3088 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3103 /* functions names must be treated as function pointers,
3104 except for unary '&' and sizeof. Since we consider that
3105 functions are not lvalues, we only have to handle it
3106 there and in function calls. */
3107 /* arrays can also be used although they are not lvalues */
3108 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3109 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3111 mk_pointer(&vtop
->type
);
3117 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3119 boolean
.t
= VT_BOOL
;
3121 vtop
->c
.i
= !vtop
->c
.i
;
3122 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3123 vtop
->c
.i
= vtop
->c
.i
^ 1;
3126 vseti(VT_JMP
, gtst(1, 0));
3137 /* in order to force cast, we add zero */
3139 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3140 error("pointer not accepted for unary plus");
3150 parse_expr_type(&type
);
3154 size
= type_size(&type
, &align
);
3155 if (t
== TOK_SIZEOF
) {
3157 error("sizeof applied to an incomplete type");
3162 vtop
->type
.t
|= VT_UNSIGNED
;
3165 case TOK_builtin_types_compatible_p
:
3174 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3175 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3176 vpushi(is_compatible_types(&type1
, &type2
));
3179 case TOK_builtin_constant_p
:
3181 int saved_nocode_wanted
, res
;
3184 saved_nocode_wanted
= nocode_wanted
;
3187 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3189 nocode_wanted
= saved_nocode_wanted
;
3194 case TOK_builtin_frame_address
:
3199 if (tok
!= TOK_CINT
) {
3200 error("__builtin_frame_address only takes integers");
3203 error("TCC only supports __builtin_frame_address(0)");
3209 vset(&type
, VT_LOCAL
, 0);
3212 #ifdef TCC_TARGET_X86_64
3213 case TOK_builtin_malloc
:
3215 goto tok_identifier
;
3216 case TOK_builtin_free
:
3218 goto tok_identifier
;
3235 goto tok_identifier
;
3237 /* allow to take the address of a label */
3238 if (tok
< TOK_UIDENT
)
3239 expect("label identifier");
3240 s
= label_find(tok
);
3242 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3244 if (s
->r
== LABEL_DECLARED
)
3245 s
->r
= LABEL_FORWARD
;
3248 s
->type
.t
= VT_VOID
;
3249 mk_pointer(&s
->type
);
3250 s
->type
.t
|= VT_STATIC
;
3252 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3261 expect("identifier");
3265 error("'%s' undeclared", get_tok_str(t
, NULL
));
3266 /* for simple function calls, we tolerate undeclared
3267 external reference to int() function */
3268 if (tcc_state
->warn_implicit_function_declaration
)
3269 warning("implicit declaration of function '%s'",
3270 get_tok_str(t
, NULL
));
3271 s
= external_global_sym(t
, &func_old_type
, 0);
3273 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3274 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3275 /* if referencing an inline function, then we generate a
3276 symbol to it if not already done. It will have the
3277 effect to generate code for it at the end of the
3278 compilation unit. Inline function as always
3279 generated in the text section. */
3281 put_extern_sym(s
, text_section
, 0, 0);
3282 r
= VT_SYM
| VT_CONST
;
3286 vset(&s
->type
, r
, s
->c
);
3287 /* if forward reference, we must point to s */
3288 if (vtop
->r
& VT_SYM
) {
3295 /* post operations */
3297 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3300 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3303 if (tok
== TOK_ARROW
)
3305 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3309 /* expect pointer on structure */
3310 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3311 expect("struct or union");
3315 while ((s
= s
->next
) != NULL
) {
3320 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3321 /* add field offset to pointer */
3322 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3325 /* change type to field type, and set to lvalue */
3326 vtop
->type
= s
->type
;
3327 vtop
->type
.t
|= qualifiers
;
3328 /* an array is never an lvalue */
3329 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3330 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3331 /* if bound checking, the referenced pointer must be checked */
3332 if (tcc_state
->do_bounds_check
)
3333 vtop
->r
|= VT_MUSTBOUND
;
3336 } else if (tok
== '[') {
3342 } else if (tok
== '(') {
3348 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3349 /* pointer test (no array accepted) */
3350 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3351 vtop
->type
= *pointed_type(&vtop
->type
);
3352 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3356 expect("function pointer");
3359 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3361 /* get return type */
3364 sa
= s
->next
; /* first parameter */
3367 /* compute first implicit argument if a structure is returned */
3368 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3369 /* get some space for the returned structure */
3370 size
= type_size(&s
->type
, &align
);
3371 loc
= (loc
- size
) & -align
;
3373 ret
.r
= VT_LOCAL
| VT_LVAL
;
3374 /* pass it as 'int' to avoid structure arg passing
3376 vseti(VT_LOCAL
, loc
);
3381 /* return in register */
3382 if (is_float(ret
.type
.t
)) {
3383 ret
.r
= reg_fret(ret
.type
.t
);
3385 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3394 gfunc_param_typed(s
, sa
);
3404 error("too few arguments to function");
3406 if (!nocode_wanted
) {
3407 gfunc_call(nb_args
);
3409 vtop
-= (nb_args
+ 1);
3412 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3420 static void uneq(void)
3426 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3427 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3428 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3443 static void expr_prod(void)
3448 while (tok
== '*' || tok
== '/' || tok
== '%') {
3456 static void expr_sum(void)
3461 while (tok
== '+' || tok
== '-') {
3469 static void expr_shift(void)
3474 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3482 static void expr_cmp(void)
3487 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3488 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3496 static void expr_cmpeq(void)
3501 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3509 static void expr_and(void)
3512 while (tok
== '&') {
3519 static void expr_xor(void)
3522 while (tok
== '^') {
3529 static void expr_or(void)
3532 while (tok
== '|') {
3539 /* XXX: fix this mess */
3540 static void expr_land_const(void)
3543 while (tok
== TOK_LAND
) {
3550 /* XXX: fix this mess */
3551 static void expr_lor_const(void)
3554 while (tok
== TOK_LOR
) {
3561 /* only used if non constant */
3562 static void expr_land(void)
3567 if (tok
== TOK_LAND
) {
3572 if (tok
!= TOK_LAND
) {
3582 static void expr_lor(void)
3587 if (tok
== TOK_LOR
) {
3592 if (tok
!= TOK_LOR
) {
3602 /* XXX: better constant handling */
3603 static void expr_eq(void)
3605 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3607 CType type
, type1
, type2
;
3614 boolean
.t
= VT_BOOL
;
3620 if (tok
!= ':' || !gnu_ext
) {
3635 if (vtop
!= vstack
) {
3636 /* needed to avoid having different registers saved in
3638 if (is_float(vtop
->type
.t
)) {
3640 #ifdef TCC_TARGET_X86_64
3641 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3651 if (tok
== ':' && gnu_ext
) {
3659 sv
= *vtop
; /* save value to handle it later */
3660 vtop
--; /* no vpop so that FP stack is not flushed */
3668 bt1
= t1
& VT_BTYPE
;
3670 bt2
= t2
& VT_BTYPE
;
3671 /* cast operands to correct type according to ISOC rules */
3672 if (is_float(bt1
) || is_float(bt2
)) {
3673 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3674 type
.t
= VT_LDOUBLE
;
3675 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3680 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3681 /* cast to biggest op */
3683 /* convert to unsigned if it does not fit in a long long */
3684 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3685 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3686 type
.t
|= VT_UNSIGNED
;
3687 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3688 /* XXX: test pointer compatibility */
3690 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3691 /* XXX: test function pointer compatibility */
3693 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3694 /* XXX: test structure compatibility */
3696 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3697 /* NOTE: as an extension, we accept void on only one side */
3700 /* integer operations */
3702 /* convert to unsigned if it does not fit in an integer */
3703 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3704 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3705 type
.t
|= VT_UNSIGNED
;
3708 /* now we convert second operand */
3710 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3713 if (is_float(type
.t
)) {
3715 #ifdef TCC_TARGET_X86_64
3716 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3720 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3721 /* for long longs, we use fixed registers to avoid having
3722 to handle a complicated move */
3727 /* this is horrible, but we must also convert first
3731 /* put again first value and cast it */
3734 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3744 static void gexpr(void)
3755 /* parse an expression and return its type without any side effect. */
3756 static void expr_type(CType
*type
)
3758 int saved_nocode_wanted
;
3760 saved_nocode_wanted
= nocode_wanted
;
3765 nocode_wanted
= saved_nocode_wanted
;
3768 /* parse a unary expression and return its type without any side
3770 static void unary_type(CType
*type
)
3782 /* parse a constant expression and return value in vtop. */
3783 static void expr_const1(void)
3792 /* parse an integer constant and return its value. */
3793 static int expr_const(void)
3797 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3798 expect("constant expression");
3804 /* return the label token if current token is a label, otherwise
3806 static int is_label(void)
3810 /* fast test first */
3811 if (tok
< TOK_UIDENT
)
3813 /* no need to save tokc because tok is an identifier */
3820 unget_tok(last_tok
);
3825 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3826 int case_reg
, int is_expr
)
3831 /* generate line number info */
3832 if (tcc_state
->do_debug
&&
3833 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3834 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3836 last_line_num
= file
->line_num
;
3840 /* default return value is (void) */
3842 vtop
->type
.t
= VT_VOID
;
3845 if (tok
== TOK_IF
) {
3852 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3854 if (c
== TOK_ELSE
) {
3858 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3859 gsym(d
); /* patch else jmp */
3862 } else if (tok
== TOK_WHILE
) {
3870 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3874 } else if (tok
== '{') {
3878 /* record local declaration stack position */
3880 llabel
= local_label_stack
;
3881 /* handle local labels declarations */
3882 if (tok
== TOK_LABEL
) {
3885 if (tok
< TOK_UIDENT
)
3886 expect("label identifier");
3887 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3897 while (tok
!= '}') {
3902 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3905 /* pop locally defined labels */
3906 label_pop(&local_label_stack
, llabel
);
3907 /* pop locally defined symbols */
3909 /* XXX: this solution makes only valgrind happy...
3910 triggered by gcc.c-torture/execute/20000917-1.c */
3912 switch(vtop
->type
.t
& VT_BTYPE
) {
3917 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3919 error("unsupported expression type");
3922 sym_pop(&local_stack
, s
);
3924 } else if (tok
== TOK_RETURN
) {
3928 gen_assign_cast(&func_vt
);
3929 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3931 /* if returning structure, must copy it to implicit
3932 first pointer arg location */
3935 size
= type_size(&func_vt
,&align
);
3938 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3942 loc
= (loc
- size
) & -4;
3945 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3948 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3950 vtop
->type
= int_type
;
3956 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3959 /* copy structure value to pointer */
3964 } else if (is_float(func_vt
.t
)) {
3965 gv(rc_fret(func_vt
.t
));
3969 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3972 rsym
= gjmp(rsym
); /* jmp */
3973 } else if (tok
== TOK_BREAK
) {
3976 error("cannot break");
3977 *bsym
= gjmp(*bsym
);
3980 } else if (tok
== TOK_CONTINUE
) {
3983 error("cannot continue");
3984 *csym
= gjmp(*csym
);
3987 } else if (tok
== TOK_FOR
) {
4014 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4019 if (tok
== TOK_DO
) {
4024 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4035 if (tok
== TOK_SWITCH
) {
4039 /* XXX: other types than integer */
4040 case_reg
= gv(RC_INT
);
4044 b
= gjmp(0); /* jump to first case */
4046 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4047 /* if no default, jmp after switch */
4055 if (tok
== TOK_CASE
) {
4062 if (gnu_ext
&& tok
== TOK_DOTS
) {
4066 warning("empty case range");
4068 /* since a case is like a label, we must skip it with a jmp */
4075 *case_sym
= gtst(1, 0);
4078 *case_sym
= gtst(1, 0);
4082 *case_sym
= gtst(1, *case_sym
);
4087 goto block_after_label
;
4089 if (tok
== TOK_DEFAULT
) {
4095 error("too many 'default'");
4098 goto block_after_label
;
4100 if (tok
== TOK_GOTO
) {
4102 if (tok
== '*' && gnu_ext
) {
4106 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4109 } else if (tok
>= TOK_UIDENT
) {
4110 s
= label_find(tok
);
4111 /* put forward definition if needed */
4113 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4115 if (s
->r
== LABEL_DECLARED
)
4116 s
->r
= LABEL_FORWARD
;
4118 /* label already defined */
4119 if (s
->r
& LABEL_FORWARD
)
4120 s
->jnext
= gjmp(s
->jnext
);
4122 gjmp_addr(s
->jnext
);
4125 expect("label identifier");
4128 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4136 if (s
->r
== LABEL_DEFINED
)
4137 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4139 s
->r
= LABEL_DEFINED
;
4141 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4144 /* we accept this, but it is a mistake */
4147 warning("deprecated use of label at end of compound statement");
4151 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4154 /* expression case */
4169 /* t is the array or struct type. c is the array or struct
4170 address. cur_index/cur_field is the pointer to the current
4171 value. 'size_only' is true if only size info is needed (only used
4173 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4174 int *cur_index
, Sym
**cur_field
,
4178 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4184 if (gnu_ext
&& (l
= is_label()) != 0)
4186 while (tok
== '[' || tok
== '.') {
4188 if (!(type
->t
& VT_ARRAY
))
4189 expect("array type");
4192 index
= expr_const();
4193 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4194 expect("invalid index");
4195 if (tok
== TOK_DOTS
&& gnu_ext
) {
4197 index_last
= expr_const();
4198 if (index_last
< 0 ||
4199 (s
->c
>= 0 && index_last
>= s
->c
) ||
4201 expect("invalid index");
4207 *cur_index
= index_last
;
4208 type
= pointed_type(type
);
4209 elem_size
= type_size(type
, &align
);
4210 c
+= index
* elem_size
;
4211 /* NOTE: we only support ranges for last designator */
4212 nb_elems
= index_last
- index
+ 1;
4213 if (nb_elems
!= 1) {
4222 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4223 expect("struct/union type");
4236 /* XXX: fix this mess by using explicit storage field */
4238 type1
.t
|= (type
->t
& ~VT_TYPE
);
4252 if (type
->t
& VT_ARRAY
) {
4254 type
= pointed_type(type
);
4255 c
+= index
* type_size(type
, &align
);
4259 error("too many field init");
4260 /* XXX: fix this mess by using explicit storage field */
4262 type1
.t
|= (type
->t
& ~VT_TYPE
);
4267 decl_initializer(type
, sec
, c
, 0, size_only
);
4269 /* XXX: make it more general */
4270 if (!size_only
&& nb_elems
> 1) {
4271 unsigned long c_end
;
4276 error("range init not supported yet for dynamic storage");
4277 c_end
= c
+ nb_elems
* elem_size
;
4278 if (c_end
> sec
->data_allocated
)
4279 section_realloc(sec
, c_end
);
4280 src
= sec
->data
+ c
;
4282 for(i
= 1; i
< nb_elems
; i
++) {
4284 memcpy(dst
, src
, elem_size
);
4290 #define EXPR_CONST 1
4293 /* store a value or an expression directly in global data or in local array */
4294 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4295 int v
, int expr_type
)
4297 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4299 unsigned long long bit_mask
;
4307 /* compound literals must be allocated globally in this case */
4308 saved_global_expr
= global_expr
;
4311 global_expr
= saved_global_expr
;
4312 /* NOTE: symbols are accepted */
4313 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4314 error("initializer element is not constant");
4322 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4325 /* XXX: not portable */
4326 /* XXX: generate error if incorrect relocation */
4327 gen_assign_cast(&dtype
);
4328 bt
= type
->t
& VT_BTYPE
;
4329 /* we'll write at most 12 bytes */
4330 if (c
+ 12 > sec
->data_allocated
) {
4331 section_realloc(sec
, c
+ 12);
4333 ptr
= sec
->data
+ c
;
4334 /* XXX: make code faster ? */
4335 if (!(type
->t
& VT_BITFIELD
)) {
4340 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4341 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4342 bit_mask
= (1LL << bit_size
) - 1;
4344 if ((vtop
->r
& VT_SYM
) &&
4350 (bt
== VT_INT
&& bit_size
!= 32)))
4351 error("initializer element is not computable at load time");
4354 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4356 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4359 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4362 *(double *)ptr
= vtop
->c
.d
;
4365 *(long double *)ptr
= vtop
->c
.ld
;
4368 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4371 if (vtop
->r
& VT_SYM
) {
4372 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4374 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4379 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4386 /* put zeros for variable based init */
4387 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4390 /* nothing to do because globals are already set to zero */
4392 vpush_global_sym(&func_old_type
, TOK_memset
);
4400 /* 't' contains the type and storage info. 'c' is the offset of the
4401 object in section 'sec'. If 'sec' is NULL, it means stack based
4402 allocation. 'first' is true if array '{' must be read (multi
4403 dimension implicit array init handling). 'size_only' is true if
4404 size only evaluation is wanted (only for arrays). */
4405 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4406 int first
, int size_only
)
4408 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4409 int size1
, align1
, expr_type
;
4413 if (type
->t
& VT_ARRAY
) {
4417 t1
= pointed_type(type
);
4418 size1
= type_size(t1
, &align1
);
4421 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4427 /* only parse strings here if correct type (otherwise: handle
4428 them as ((w)char *) expressions */
4429 if ((tok
== TOK_LSTR
&&
4430 #ifdef TCC_TARGET_PE
4431 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4433 (t1
->t
& VT_BTYPE
) == VT_INT
4435 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4436 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4441 /* compute maximum number of chars wanted */
4443 cstr_len
= cstr
->size
;
4445 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4448 if (n
>= 0 && nb
> (n
- array_length
))
4449 nb
= n
- array_length
;
4452 warning("initializer-string for array is too long");
4453 /* in order to go faster for common case (char
4454 string in global variable, we handle it
4456 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4457 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4461 ch
= ((unsigned char *)cstr
->data
)[i
];
4463 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4464 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4472 /* only add trailing zero if enough storage (no
4473 warning in this case since it is standard) */
4474 if (n
< 0 || array_length
< n
) {
4476 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4482 while (tok
!= '}') {
4483 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4484 if (n
>= 0 && index
>= n
)
4485 error("index too large");
4486 /* must put zero in holes (note that doing it that way
4487 ensures that it even works with designators) */
4488 if (!size_only
&& array_length
< index
) {
4489 init_putz(t1
, sec
, c
+ array_length
* size1
,
4490 (index
- array_length
) * size1
);
4493 if (index
> array_length
)
4494 array_length
= index
;
4495 /* special test for multi dimensional arrays (may not
4496 be strictly correct if designators are used at the
4498 if (index
>= n
&& no_oblock
)
4507 /* put zeros at the end */
4508 if (!size_only
&& n
>= 0 && array_length
< n
) {
4509 init_putz(t1
, sec
, c
+ array_length
* size1
,
4510 (n
- array_length
) * size1
);
4512 /* patch type size if needed */
4514 s
->c
= array_length
;
4515 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4516 (sec
|| !first
|| tok
== '{')) {
4519 /* NOTE: the previous test is a specific case for automatic
4520 struct/union init */
4521 /* XXX: union needs only one init */
4523 /* XXX: this test is incorrect for local initializers
4524 beginning with ( without {. It would be much more difficult
4525 to do it correctly (ideally, the expression parser should
4526 be used in all cases) */
4532 while (tok
== '(') {
4536 if (!parse_btype(&type1
, &ad1
))
4538 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4540 if (!is_assignable_types(type
, &type1
))
4541 error("invalid type for cast");
4546 if (first
|| tok
== '{') {
4555 while (tok
!= '}') {
4556 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4558 if (!size_only
&& array_length
< index
) {
4559 init_putz(type
, sec
, c
+ array_length
,
4560 index
- array_length
);
4562 index
= index
+ type_size(&f
->type
, &align1
);
4563 if (index
> array_length
)
4564 array_length
= index
;
4566 /* gr: skip fields from same union - ugly. */
4568 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4569 /* test for same offset */
4570 if (f
->next
->c
!= f
->c
)
4572 /* if yes, test for bitfield shift */
4573 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4574 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4575 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4576 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4577 if (bit_pos_1
!= bit_pos_2
)
4584 if (no_oblock
&& f
== NULL
)
4590 /* put zeros at the end */
4591 if (!size_only
&& array_length
< n
) {
4592 init_putz(type
, sec
, c
+ array_length
,
4601 } else if (tok
== '{') {
4603 decl_initializer(type
, sec
, c
, first
, size_only
);
4605 } else if (size_only
) {
4606 /* just skip expression */
4608 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4612 else if (tok
== ')')
4617 /* currently, we always use constant expression for globals
4618 (may change for scripting case) */
4619 expr_type
= EXPR_CONST
;
4621 expr_type
= EXPR_ANY
;
4622 init_putv(type
, sec
, c
, 0, expr_type
);
4626 /* parse an initializer for type 't' if 'has_init' is non zero, and
4627 allocate space in local or global data space ('r' is either
4628 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4629 variable 'v' of scope 'scope' is declared before initializers are
4630 parsed. If 'v' is zero, then a reference to the new object is put
4631 in the value stack. If 'has_init' is 2, a special parsing is done
4632 to handle string constants. */
4633 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4634 int has_init
, int v
, int scope
)
4636 int size
, align
, addr
, data_offset
;
4638 ParseState saved_parse_state
= {0};
4639 TokenString init_str
;
4642 size
= type_size(type
, &align
);
4643 /* If unknown size, we must evaluate it before
4644 evaluating initializers because
4645 initializers can generate global data too
4646 (e.g. string pointers or ISOC99 compound
4647 literals). It also simplifies local
4648 initializers handling */
4649 tok_str_new(&init_str
);
4652 error("unknown type size");
4653 /* get all init string */
4654 if (has_init
== 2) {
4655 /* only get strings */
4656 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4657 tok_str_add_tok(&init_str
);
4662 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4664 error("unexpected end of file in initializer");
4665 tok_str_add_tok(&init_str
);
4668 else if (tok
== '}') {
4678 tok_str_add(&init_str
, -1);
4679 tok_str_add(&init_str
, 0);
4682 save_parse_state(&saved_parse_state
);
4684 macro_ptr
= init_str
.str
;
4686 decl_initializer(type
, NULL
, 0, 1, 1);
4687 /* prepare second initializer parsing */
4688 macro_ptr
= init_str
.str
;
4691 /* if still unknown size, error */
4692 size
= type_size(type
, &align
);
4694 error("unknown type size");
4696 /* take into account specified alignment if bigger */
4698 if (ad
->aligned
> align
)
4699 align
= ad
->aligned
;
4700 } else if (ad
->packed
) {
4703 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4705 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4707 loc
= (loc
- size
) & -align
;
4709 /* handles bounds */
4710 /* XXX: currently, since we do only one pass, we cannot track
4711 '&' operators, so we add only arrays */
4712 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4713 unsigned long *bounds_ptr
;
4714 /* add padding between regions */
4716 /* then add local bound info */
4717 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4718 bounds_ptr
[0] = addr
;
4719 bounds_ptr
[1] = size
;
4722 /* local variable */
4723 sym_push(v
, type
, r
, addr
);
4725 /* push local reference */
4726 vset(type
, r
, addr
);
4732 if (v
&& scope
== VT_CONST
) {
4733 /* see if the symbol was already defined */
4736 if (!is_compatible_types(&sym
->type
, type
))
4737 error("incompatible types for redefinition of '%s'",
4738 get_tok_str(v
, NULL
));
4739 if (sym
->type
.t
& VT_EXTERN
) {
4740 /* if the variable is extern, it was not allocated */
4741 sym
->type
.t
&= ~VT_EXTERN
;
4742 /* set array size if it was ommited in extern
4744 if ((sym
->type
.t
& VT_ARRAY
) &&
4745 sym
->type
.ref
->c
< 0 &&
4747 sym
->type
.ref
->c
= type
->ref
->c
;
4749 /* we accept several definitions of the same
4750 global variable. this is tricky, because we
4751 must play with the SHN_COMMON type of the symbol */
4752 /* XXX: should check if the variable was already
4753 initialized. It is incorrect to initialized it
4755 /* no init data, we won't add more to the symbol */
4762 /* allocate symbol in corresponding section */
4767 else if (tcc_state
->nocommon
)
4771 data_offset
= sec
->data_offset
;
4772 data_offset
= (data_offset
+ align
- 1) & -align
;
4774 /* very important to increment global pointer at this time
4775 because initializers themselves can create new initializers */
4776 data_offset
+= size
;
4777 /* add padding if bound check */
4778 if (tcc_state
->do_bounds_check
)
4780 sec
->data_offset
= data_offset
;
4781 /* allocate section space to put the data */
4782 if (sec
->sh_type
!= SHT_NOBITS
&&
4783 data_offset
> sec
->data_allocated
)
4784 section_realloc(sec
, data_offset
);
4785 /* align section if needed */
4786 if (align
> sec
->sh_addralign
)
4787 sec
->sh_addralign
= align
;
4789 addr
= 0; /* avoid warning */
4793 if (scope
!= VT_CONST
|| !sym
) {
4794 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4796 /* update symbol definition */
4798 put_extern_sym(sym
, sec
, addr
, size
);
4801 /* put a common area */
4802 put_extern_sym(sym
, NULL
, align
, size
);
4803 /* XXX: find a nicer way */
4804 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4805 esym
->st_shndx
= SHN_COMMON
;
4810 /* push global reference */
4811 sym
= get_sym_ref(type
, sec
, addr
, size
);
4813 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4817 /* handles bounds now because the symbol must be defined
4818 before for the relocation */
4819 if (tcc_state
->do_bounds_check
) {
4820 unsigned long *bounds_ptr
;
4822 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
4823 /* then add global bound info */
4824 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4825 bounds_ptr
[0] = 0; /* relocated */
4826 bounds_ptr
[1] = size
;
4830 decl_initializer(type
, sec
, addr
, 1, 0);
4831 /* restore parse state if needed */
4833 tok_str_free(init_str
.str
);
4834 restore_parse_state(&saved_parse_state
);
4840 void put_func_debug(Sym
*sym
)
4845 /* XXX: we put here a dummy type */
4846 snprintf(buf
, sizeof(buf
), "%s:%c1",
4847 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4848 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4849 cur_text_section
, sym
->c
);
4850 /* //gr gdb wants a line at the function */
4851 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4856 /* parse an old style function declaration list */
4857 /* XXX: check multiple parameter */
4858 static void func_decl_list(Sym
*func_sym
)
4865 /* parse each declaration */
4866 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4867 if (!parse_btype(&btype
, &ad
))
4868 expect("declaration list");
4869 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4870 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4872 /* we accept no variable after */
4876 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4877 /* find parameter in function parameter list */
4880 if ((s
->v
& ~SYM_FIELD
) == v
)
4884 error("declaration for parameter '%s' but no such parameter",
4885 get_tok_str(v
, NULL
));
4887 /* check that no storage specifier except 'register' was given */
4888 if (type
.t
& VT_STORAGE
)
4889 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4890 convert_parameter_type(&type
);
4891 /* we can add the type (NOTE: it could be local to the function) */
4893 /* accept other parameters */
4904 /* parse a function defined by symbol 'sym' and generate its code in
4905 'cur_text_section' */
4906 static void gen_function(Sym
*sym
)
4908 int saved_nocode_wanted
= nocode_wanted
;
4910 ind
= cur_text_section
->data_offset
;
4911 /* NOTE: we patch the symbol size later */
4912 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4913 funcname
= get_tok_str(sym
->v
, NULL
);
4915 /* put debug symbol */
4916 if (tcc_state
->do_debug
)
4917 put_func_debug(sym
);
4918 /* push a dummy symbol to enable local sym storage */
4919 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4920 gfunc_prolog(&sym
->type
);
4922 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4925 cur_text_section
->data_offset
= ind
;
4926 label_pop(&global_label_stack
, NULL
);
4927 sym_pop(&local_stack
, NULL
); /* reset local stack */
4928 /* end of function */
4929 /* patch symbol size */
4930 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4932 if (tcc_state
->do_debug
) {
4933 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4935 /* It's better to crash than to generate wrong code */
4936 cur_text_section
= NULL
;
4937 funcname
= ""; /* for safety */
4938 func_vt
.t
= VT_VOID
; /* for safety */
4939 ind
= 0; /* for safety */
4940 nocode_wanted
= saved_nocode_wanted
;
4943 static void gen_inline_functions(void)
4946 int *str
, inline_generated
, i
;
4947 struct InlineFunc
*fn
;
4949 /* iterate while inline function are referenced */
4951 inline_generated
= 0;
4952 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4953 fn
= tcc_state
->inline_fns
[i
];
4955 if (sym
&& sym
->c
) {
4956 /* the function was used: generate its code and
4957 convert it to a normal function */
4958 str
= fn
->token_str
;
4961 strcpy(file
->filename
, fn
->filename
);
4962 sym
->r
= VT_SYM
| VT_CONST
;
4963 sym
->type
.t
&= ~VT_INLINE
;
4967 cur_text_section
= text_section
;
4969 macro_ptr
= NULL
; /* fail safe */
4971 inline_generated
= 1;
4974 if (!inline_generated
)
4977 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4978 fn
= tcc_state
->inline_fns
[i
];
4979 str
= fn
->token_str
;
4982 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
4985 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4986 static void decl(int l
)
4994 if (!parse_btype(&btype
, &ad
)) {
4995 /* skip redundant ';' */
4996 /* XXX: find more elegant solution */
5001 if (l
== VT_CONST
&&
5002 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5003 /* global asm block */
5007 /* special test for old K&R protos without explicit int
5008 type. Only accepted when defining global data */
5009 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5013 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5014 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5016 /* we accept no variable after */
5020 while (1) { /* iterate thru each declaration */
5022 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5026 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5027 printf("type = '%s'\n", buf
);
5030 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5031 /* if old style function prototype, we accept a
5034 if (sym
->c
== FUNC_OLD
)
5035 func_decl_list(sym
);
5040 error("cannot use local functions");
5041 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5042 expect("function definition");
5044 /* reject abstract declarators in function definition */
5046 while ((sym
= sym
->next
) != NULL
)
5047 if (!(sym
->v
& ~SYM_FIELD
))
5048 expect("identifier");
5050 /* XXX: cannot do better now: convert extern line to static inline */
5051 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5052 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5056 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5059 r
= sym
->type
.ref
->r
;
5060 /* use func_call from prototype if not defined */
5061 if (FUNC_CALL(r
) != FUNC_CDECL
5062 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5063 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5065 /* use export from prototype */
5067 FUNC_EXPORT(type
.ref
->r
) = 1;
5069 /* use static from prototype */
5070 if (sym
->type
.t
& VT_STATIC
)
5071 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5073 if (!is_compatible_types(&sym
->type
, &type
)) {
5075 error("incompatible types for redefinition of '%s'",
5076 get_tok_str(v
, NULL
));
5078 /* if symbol is already defined, then put complete type */
5081 /* put function symbol */
5082 sym
= global_identifier_push(v
, type
.t
, 0);
5083 sym
->type
.ref
= type
.ref
;
5086 /* static inline functions are just recorded as a kind
5087 of macro. Their code will be emitted at the end of
5088 the compilation unit only if they are used */
5089 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5090 (VT_INLINE
| VT_STATIC
)) {
5091 TokenString func_str
;
5093 struct InlineFunc
*fn
;
5094 const char *filename
;
5096 tok_str_new(&func_str
);
5102 error("unexpected end of file");
5103 tok_str_add_tok(&func_str
);
5108 } else if (t
== '}') {
5110 if (block_level
== 0)
5114 tok_str_add(&func_str
, -1);
5115 tok_str_add(&func_str
, 0);
5116 filename
= file
? file
->filename
: "";
5117 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5118 strcpy(fn
->filename
, filename
);
5120 fn
->token_str
= func_str
.str
;
5121 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5124 /* compute text section */
5125 cur_text_section
= ad
.section
;
5126 if (!cur_text_section
)
5127 cur_text_section
= text_section
;
5128 sym
->r
= VT_SYM
| VT_CONST
;
5133 if (btype
.t
& VT_TYPEDEF
) {
5134 /* save typedefed type */
5135 /* XXX: test storage specifiers ? */
5136 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5137 sym
->type
.t
|= VT_TYPEDEF
;
5138 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5139 /* external function definition */
5140 /* specific case for func_call attribute */
5141 type
.ref
->r
= INT_ATTR(&ad
);
5142 external_sym(v
, &type
, 0);
5144 /* not lvalue if array */
5146 if (!(type
.t
& VT_ARRAY
))
5147 r
|= lvalue_type(type
.t
);
5148 has_init
= (tok
== '=');
5149 if ((btype
.t
& VT_EXTERN
) ||
5150 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5151 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5152 /* external variable */
5153 /* NOTE: as GCC, uninitialized global static
5154 arrays of null size are considered as
5156 #ifdef TCC_TARGET_PE
5158 type
.t
|= VT_IMPORT
;
5160 external_sym(v
, &type
, r
);
5162 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5163 if (type
.t
& VT_STATIC
)
5169 #ifdef TCC_TARGET_PE
5171 type
.t
|= VT_EXPORT
;
5173 decl_initializer_alloc(&type
, &ad
, r
,