2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 void swap(int *p
, int *q
)
29 void vsetc(CType
*type
, int r
, CValue
*vc
)
33 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
35 /* cannot let cpu flags if other instruction are generated. Also
36 avoid leaving VT_JMP anywhere except on the top of the stack
37 because it would complicate the code generator. */
39 v
= vtop
->r
& VT_VALMASK
;
40 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
50 /* push integer constant */
55 vsetc(&int_type
, VT_CONST
, &cval
);
58 /* push long long constant */
59 void vpushll(long long v
)
65 vsetc(&ctype
, VT_CONST
, &cval
);
68 /* Return a static symbol pointing to a section */
69 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
70 unsigned long offset
, unsigned long size
)
76 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
77 sym
->type
.ref
= type
->ref
;
78 sym
->r
= VT_CONST
| VT_SYM
;
79 put_extern_sym(sym
, sec
, offset
, size
);
83 /* push a reference to a section offset by adding a dummy symbol */
84 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
89 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
90 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
93 /* define a new external reference to a symbol 'v' of type 'u' */
94 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
100 /* push forward reference */
101 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
102 s
->type
.ref
= type
->ref
;
103 s
->r
= r
| VT_CONST
| VT_SYM
;
108 /* define a new external reference to a symbol 'v' of type 'u' */
109 static Sym
*external_sym(int v
, CType
*type
, int r
)
115 /* push forward reference */
116 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
117 s
->type
.t
|= VT_EXTERN
;
118 } else if (s
->type
.ref
== func_old_type
.ref
) {
119 s
->type
.ref
= type
->ref
;
120 s
->r
= r
| VT_CONST
| VT_SYM
;
121 s
->type
.t
|= VT_EXTERN
;
122 } else if (!is_compatible_types(&s
->type
, type
)) {
123 error("incompatible types for redefinition of '%s'",
124 get_tok_str(v
, NULL
));
129 /* push a reference to global symbol v */
130 static void vpush_global_sym(CType
*type
, int v
)
135 sym
= external_global_sym(v
, type
, 0);
137 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
141 void vset(CType
*type
, int r
, int v
)
146 vsetc(type
, r
, &cval
);
149 void vseti(int r
, int v
)
165 void vpushv(SValue
*v
)
167 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
168 error("memory full");
178 /* save r to the memory stack, and mark it as being free */
181 int l
, saved
, size
, align
;
185 /* modify all stack values */
188 for(p
=vstack
;p
<=vtop
;p
++) {
189 if ((p
->r
& VT_VALMASK
) == r
||
190 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
191 /* must save value on stack if not already done */
193 /* NOTE: must reload 'r' because r might be equal to r2 */
194 r
= p
->r
& VT_VALMASK
;
195 /* store register in the stack */
197 if ((p
->r
& VT_LVAL
) ||
198 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
199 #ifdef TCC_TARGET_X86_64
200 type
= &char_pointer_type
;
204 size
= type_size(type
, &align
);
205 loc
= (loc
- size
) & -align
;
207 sv
.r
= VT_LOCAL
| VT_LVAL
;
210 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
211 /* x86 specific: need to pop fp register ST0 if saved */
213 o(0xd8dd); /* fstp %st(0) */
216 #ifndef TCC_TARGET_X86_64
217 /* special long long case */
218 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
226 /* mark that stack entry as being saved on the stack */
227 if (p
->r
& VT_LVAL
) {
228 /* also clear the bounded flag because the
229 relocation address of the function was stored in
231 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
233 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
241 /* find a register of class 'rc2' with at most one reference on stack.
242 * If none, call get_reg(rc) */
243 int get_reg_ex(int rc
, int rc2
)
248 for(r
=0;r
<NB_REGS
;r
++) {
249 if (reg_classes
[r
] & rc2
) {
252 for(p
= vstack
; p
<= vtop
; p
++) {
253 if ((p
->r
& VT_VALMASK
) == r
||
254 (p
->r2
& VT_VALMASK
) == r
)
264 /* find a free register of class 'rc'. If none, save one register */
270 /* find a free register */
271 for(r
=0;r
<NB_REGS
;r
++) {
272 if (reg_classes
[r
] & rc
) {
273 for(p
=vstack
;p
<=vtop
;p
++) {
274 if ((p
->r
& VT_VALMASK
) == r
||
275 (p
->r2
& VT_VALMASK
) == r
)
283 /* no register left : free the first one on the stack (VERY
284 IMPORTANT to start from the bottom to ensure that we don't
285 spill registers used in gen_opi()) */
286 for(p
=vstack
;p
<=vtop
;p
++) {
287 r
= p
->r
& VT_VALMASK
;
288 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
290 /* also look at second register (if long long) */
291 r
= p
->r2
& VT_VALMASK
;
292 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
298 /* Should never comes here */
302 /* save registers up to (vtop - n) stack entry */
303 void save_regs(int n
)
308 for(p
= vstack
;p
<= p1
; p
++) {
309 r
= p
->r
& VT_VALMASK
;
316 /* move register 's' to 'r', and flush previous value of r to memory
318 void move_reg(int r
, int s
)
331 /* get address of vtop (vtop MUST BE an lvalue) */
335 /* tricky: if saved lvalue, then we can go back to lvalue */
336 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
337 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
340 #ifdef CONFIG_TCC_BCHECK
341 /* generate lvalue bound code */
347 vtop
->r
&= ~VT_MUSTBOUND
;
348 /* if lvalue, then use checking code before dereferencing */
349 if (vtop
->r
& VT_LVAL
) {
350 /* if not VT_BOUNDED value, then make one */
351 if (!(vtop
->r
& VT_BOUNDED
)) {
352 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
353 /* must save type because we must set it to int to get pointer */
355 vtop
->type
.t
= VT_INT
;
358 gen_bounded_ptr_add();
359 vtop
->r
|= lval_type
;
362 /* then check for dereferencing */
363 gen_bounded_ptr_deref();
368 /* store vtop a register belonging to class 'rc'. lvalues are
369 converted to values. Cannot be used if cannot be converted to
370 register value (such as structures). */
373 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
375 /* NOTE: get_reg can modify vstack[] */
376 if (vtop
->type
.t
& VT_BITFIELD
) {
379 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
380 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
381 /* remove bit field info to avoid loops */
382 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
383 /* cast to int to propagate signedness in following ops */
384 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
389 if((vtop
->type
.t
& VT_UNSIGNED
) ||
390 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
391 type
.t
|= VT_UNSIGNED
;
393 /* generate shifts */
394 vpushi(bits
- (bit_pos
+ bit_size
));
396 vpushi(bits
- bit_size
);
397 /* NOTE: transformed to SHR if unsigned */
401 if (is_float(vtop
->type
.t
) &&
402 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
405 unsigned long offset
;
406 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
410 /* XXX: unify with initializers handling ? */
411 /* CPUs usually cannot use float constants, so we store them
412 generically in data segment */
413 size
= type_size(&vtop
->type
, &align
);
414 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
415 data_section
->data_offset
= offset
;
416 /* XXX: not portable yet */
417 #if defined(__i386__) || defined(__x86_64__)
418 /* Zero pad x87 tenbyte long doubles */
419 if (size
== LDOUBLE_SIZE
)
420 vtop
->c
.tab
[2] &= 0xffff;
422 ptr
= section_ptr_add(data_section
, size
);
424 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
428 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
432 ptr
[i
] = vtop
->c
.tab
[i
];
433 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
434 vtop
->r
|= VT_LVAL
| VT_SYM
;
438 #ifdef CONFIG_TCC_BCHECK
439 if (vtop
->r
& VT_MUSTBOUND
)
443 r
= vtop
->r
& VT_VALMASK
;
447 /* need to reload if:
449 - lvalue (need to dereference pointer)
450 - already a register, but not in the right class */
452 || (vtop
->r
& VT_LVAL
)
453 || !(reg_classes
[r
] & rc
)
454 #ifndef TCC_TARGET_X86_64
455 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
460 #ifndef TCC_TARGET_X86_64
461 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
463 unsigned long long ll
;
464 /* two register type load : expand to two words
466 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
469 vtop
->c
.ui
= ll
; /* first word */
471 vtop
->r
= r
; /* save register value */
472 vpushi(ll
>> 32); /* second word */
473 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
474 (vtop
->r
& VT_LVAL
)) {
475 /* We do not want to modifier the long long
476 pointer here, so the safest (and less
477 efficient) is to save all the other registers
478 in the stack. XXX: totally inefficient. */
480 /* load from memory */
483 vtop
[-1].r
= r
; /* save register value */
484 /* increment pointer to get second word */
485 vtop
->type
.t
= VT_INT
;
494 vtop
[-1].r
= r
; /* save register value */
495 vtop
->r
= vtop
[-1].r2
;
497 /* allocate second register */
501 /* write second register */
505 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
507 /* lvalue of scalar type : need to use lvalue type
508 because of possible cast */
511 /* compute memory access type */
512 if (vtop
->r
& VT_LVAL_BYTE
)
514 else if (vtop
->r
& VT_LVAL_SHORT
)
516 if (vtop
->r
& VT_LVAL_UNSIGNED
)
520 /* restore wanted type */
523 /* one register type load */
528 #ifdef TCC_TARGET_C67
529 /* uses register pairs for doubles */
530 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
537 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
538 void gv2(int rc1
, int rc2
)
542 /* generate more generic register first. But VT_JMP or VT_CMP
543 values must be generated first in all cases to avoid possible
545 v
= vtop
[0].r
& VT_VALMASK
;
546 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
551 /* test if reload is needed for first register */
552 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
562 /* test if reload is needed for first register */
563 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
569 /* wrapper around RC_FRET to return a register by type */
572 #ifdef TCC_TARGET_X86_64
573 if (t
== VT_LDOUBLE
) {
580 /* wrapper around REG_FRET to return a register by type */
583 #ifdef TCC_TARGET_X86_64
584 if (t
== VT_LDOUBLE
) {
591 /* expand long long on stack in two int registers */
596 u
= vtop
->type
.t
& VT_UNSIGNED
;
599 vtop
[0].r
= vtop
[-1].r2
;
600 vtop
[0].r2
= VT_CONST
;
601 vtop
[-1].r2
= VT_CONST
;
602 vtop
[0].type
.t
= VT_INT
| u
;
603 vtop
[-1].type
.t
= VT_INT
| u
;
606 #ifdef TCC_TARGET_ARM
607 /* expand long long on stack */
608 void lexpand_nr(void)
612 u
= vtop
->type
.t
& VT_UNSIGNED
;
615 vtop
->type
.t
= VT_INT
| u
;
616 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
618 vtop
[-1].c
.ui
= vtop
->c
.ull
;
619 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
621 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
623 vtop
->r
= vtop
[-1].r
;
624 } else if (v
> VT_CONST
) {
628 vtop
->r
= vtop
[-1].r2
;
629 vtop
[-1].r2
= VT_CONST
;
630 vtop
[-1].type
.t
= VT_INT
| u
;
634 /* build a long long from two ints */
638 vtop
[-1].r2
= vtop
[0].r
;
643 /* rotate n first stack elements to the bottom
644 I1 ... In -> I2 ... In I1 [top is right]
657 /* rotate n first stack elements to the top
658 I1 ... In -> In I1 ... I(n-1) [top is right]
666 for(i
= 0;i
< n
- 1; i
++)
667 vtop
[-i
] = vtop
[-i
- 1];
671 #ifdef TCC_TARGET_ARM
672 /* like vrott but in other direction
673 In ... I1 -> I(n-1) ... I1 In [top is right]
681 for(i
= n
- 1; i
> 0; i
--)
682 vtop
[-i
] = vtop
[-i
+ 1];
687 /* pop stack value */
691 v
= vtop
->r
& VT_VALMASK
;
692 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
693 /* for x86, we need to pop the FP stack */
694 if (v
== TREG_ST0
&& !nocode_wanted
) {
695 o(0xd8dd); /* fstp %st(0) */
698 if (v
== VT_JMP
|| v
== VT_JMPI
) {
699 /* need to put correct jump if && or || without test */
705 /* convert stack entry to register and duplicate its value in another
713 if ((t
& VT_BTYPE
) == VT_LLONG
) {
720 /* stack: H L L1 H1 */
728 /* duplicate value */
733 #ifdef TCC_TARGET_X86_64
734 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
744 load(r1
, &sv
); /* move r to r1 */
746 /* duplicates value */
752 #ifndef TCC_TARGET_X86_64
753 /* generate CPU independent (unsigned) long long operations */
756 int t
, a
, b
, op1
, c
, i
;
758 unsigned short reg_iret
= REG_IRET
;
759 unsigned short reg_lret
= REG_LRET
;
768 func
= TOK___udivdi3
;
774 func
= TOK___umoddi3
;
781 /* call generic long long function */
782 vpush_global_sym(&func_old_type
, func
);
800 /* stack: L1 H1 L2 H2 */
808 /* stack: H1 H2 L1 L2 */
814 /* stack: H1 H2 L1 L2 ML MH */
817 /* stack: ML MH H1 H2 L1 L2 */
821 /* stack: ML MH H1 L2 H2 L1 */
826 /* stack: ML MH M1 M2 */
829 } else if (op
== '+' || op
== '-') {
830 /* XXX: add non carry method too (for MIPS or alpha) */
836 /* stack: H1 H2 (L1 op L2) */
839 gen_op(op1
+ 1); /* TOK_xxxC2 */
842 /* stack: H1 H2 (L1 op L2) */
845 /* stack: (L1 op L2) H1 H2 */
847 /* stack: (L1 op L2) (H1 op H2) */
855 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
860 /* stack: L H shift */
862 /* constant: simpler */
863 /* NOTE: all comments are for SHL. the other cases are
864 done by swaping words */
908 /* XXX: should provide a faster fallback on x86 ? */
911 func
= TOK___ashrdi3
;
914 func
= TOK___lshrdi3
;
917 func
= TOK___ashldi3
;
923 /* compare operations */
929 /* stack: L1 H1 L2 H2 */
933 /* stack: L1 L2 H1 H2 */
936 /* when values are equal, we need to compare low words. since
937 the jump is inverted, we invert the test too. */
940 else if (op1
== TOK_GT
)
942 else if (op1
== TOK_ULT
)
944 else if (op1
== TOK_UGT
)
953 /* generate non equal test */
954 /* XXX: NOT PORTABLE yet */
958 #if defined(TCC_TARGET_I386)
960 #elif defined(TCC_TARGET_ARM)
962 o(0x1A000000 | encbranch(ind
, 0, 1));
963 #elif defined(TCC_TARGET_C67)
964 error("not implemented");
970 /* compare low. Always unsigned */
974 else if (op1
== TOK_LE
)
976 else if (op1
== TOK_GT
)
978 else if (op1
== TOK_GE
)
989 /* handle integer constant optimizations and various machine
991 void gen_opic(int op
)
993 int c1
, c2
, t1
, t2
, n
;
996 typedef unsigned long long U
;
1000 t1
= v1
->type
.t
& VT_BTYPE
;
1001 t2
= v2
->type
.t
& VT_BTYPE
;
1005 else if (v1
->type
.t
& VT_UNSIGNED
)
1012 else if (v2
->type
.t
& VT_UNSIGNED
)
1017 /* currently, we cannot do computations with forward symbols */
1018 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1019 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1022 case '+': l1
+= l2
; break;
1023 case '-': l1
-= l2
; break;
1024 case '&': l1
&= l2
; break;
1025 case '^': l1
^= l2
; break;
1026 case '|': l1
|= l2
; break;
1027 case '*': l1
*= l2
; break;
1034 /* if division by zero, generate explicit division */
1037 error("division by zero in constant");
1041 default: l1
/= l2
; break;
1042 case '%': l1
%= l2
; break;
1043 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1044 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1047 case TOK_SHL
: l1
<<= l2
; break;
1048 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1049 case TOK_SAR
: l1
>>= l2
; break;
1051 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1052 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1053 case TOK_EQ
: l1
= l1
== l2
; break;
1054 case TOK_NE
: l1
= l1
!= l2
; break;
1055 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1056 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1057 case TOK_LT
: l1
= l1
< l2
; break;
1058 case TOK_GE
: l1
= l1
>= l2
; break;
1059 case TOK_LE
: l1
= l1
<= l2
; break;
1060 case TOK_GT
: l1
= l1
> l2
; break;
1062 case TOK_LAND
: l1
= l1
&& l2
; break;
1063 case TOK_LOR
: l1
= l1
|| l2
; break;
1070 /* if commutative ops, put c2 as constant */
1071 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1072 op
== '|' || op
== '*')) {
1074 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1075 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1077 /* Filter out NOP operations like x*1, x-0, x&-1... */
1078 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1081 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1082 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1088 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1089 /* try to use shifts instead of muls or divs */
1090 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1099 else if (op
== TOK_PDIV
)
1105 } else if (c2
&& (op
== '+' || op
== '-') &&
1106 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
)
1107 && !(vtop
[-1].sym
->type
.t
& VT_IMPORT
))
1109 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1110 /* symbol + constant case */
1117 if (!nocode_wanted
) {
1118 /* call low level op generator */
1119 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1130 /* generate a floating point operation with constant propagation */
1131 void gen_opif(int op
)
1139 /* currently, we cannot do computations with forward symbols */
1140 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1141 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1143 if (v1
->type
.t
== VT_FLOAT
) {
1146 } else if (v1
->type
.t
== VT_DOUBLE
) {
1154 /* NOTE: we only do constant propagation if finite number (not
1155 NaN or infinity) (ANSI spec) */
1156 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1160 case '+': f1
+= f2
; break;
1161 case '-': f1
-= f2
; break;
1162 case '*': f1
*= f2
; break;
1166 error("division by zero in constant");
1171 /* XXX: also handles tests ? */
1175 /* XXX: overflow test ? */
1176 if (v1
->type
.t
== VT_FLOAT
) {
1178 } else if (v1
->type
.t
== VT_DOUBLE
) {
1186 if (!nocode_wanted
) {
1194 static int pointed_size(CType
*type
)
1197 return type_size(pointed_type(type
), &align
);
1200 static inline int is_null_pointer(SValue
*p
)
1202 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1204 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1205 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1208 static inline int is_integer_btype(int bt
)
1210 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1211 bt
== VT_INT
|| bt
== VT_LLONG
);
1214 /* check types for comparison or substraction of pointers */
1215 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1217 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1220 /* null pointers are accepted for all comparisons as gcc */
1221 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1225 bt1
= type1
->t
& VT_BTYPE
;
1226 bt2
= type2
->t
& VT_BTYPE
;
1227 /* accept comparison between pointer and integer with a warning */
1228 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1229 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1230 warning("comparison between pointer and integer");
1234 /* both must be pointers or implicit function pointers */
1235 if (bt1
== VT_PTR
) {
1236 type1
= pointed_type(type1
);
1237 } else if (bt1
!= VT_FUNC
)
1238 goto invalid_operands
;
1240 if (bt2
== VT_PTR
) {
1241 type2
= pointed_type(type2
);
1242 } else if (bt2
!= VT_FUNC
) {
1244 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1246 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1247 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1251 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1252 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1253 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1254 /* gcc-like error if '-' is used */
1256 goto invalid_operands
;
1258 warning("comparison of distinct pointer types lacks a cast");
1262 /* generic gen_op: handles types problems */
1265 int u
, t1
, t2
, bt1
, bt2
, t
;
1268 t1
= vtop
[-1].type
.t
;
1269 t2
= vtop
[0].type
.t
;
1270 bt1
= t1
& VT_BTYPE
;
1271 bt2
= t2
& VT_BTYPE
;
1273 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1274 /* at least one operand is a pointer */
1275 /* relationnal op: must be both pointers */
1276 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1277 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1278 /* pointers are handled are unsigned */
1279 #ifdef TCC_TARGET_X86_64
1280 t
= VT_LLONG
| VT_UNSIGNED
;
1282 t
= VT_INT
| VT_UNSIGNED
;
1286 /* if both pointers, then it must be the '-' op */
1287 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1289 error("cannot use pointers here");
1290 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1291 /* XXX: check that types are compatible */
1292 u
= pointed_size(&vtop
[-1].type
);
1294 /* set to integer type */
1295 #ifdef TCC_TARGET_X86_64
1296 vtop
->type
.t
= VT_LLONG
;
1298 vtop
->type
.t
= VT_INT
;
1303 /* exactly one pointer : must be '+' or '-'. */
1304 if (op
!= '-' && op
!= '+')
1305 error("cannot use pointers here");
1306 /* Put pointer as first operand */
1307 if (bt2
== VT_PTR
) {
1311 type1
= vtop
[-1].type
;
1312 type1
.t
&= ~VT_ARRAY
;
1313 #ifdef TCC_TARGET_X86_64
1314 vpushll(pointed_size(&vtop
[-1].type
));
1316 /* XXX: cast to int ? (long long case) */
1317 vpushi(pointed_size(&vtop
[-1].type
));
1320 #ifdef CONFIG_TCC_BCHECK
1321 /* if evaluating constant expression, no code should be
1322 generated, so no bound check */
1323 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1324 /* if bounded pointers, we generate a special code to
1331 gen_bounded_ptr_add();
1337 /* put again type if gen_opic() swaped operands */
1340 } else if (is_float(bt1
) || is_float(bt2
)) {
1341 /* compute bigger type and do implicit casts */
1342 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1344 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1349 /* floats can only be used for a few operations */
1350 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1351 (op
< TOK_ULT
|| op
> TOK_GT
))
1352 error("invalid operands for binary operation");
1354 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1355 /* cast to biggest op */
1357 /* convert to unsigned if it does not fit in a long long */
1358 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1359 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1363 /* integer operations */
1365 /* convert to unsigned if it does not fit in an integer */
1366 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1367 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1370 /* XXX: currently, some unsigned operations are explicit, so
1371 we modify them here */
1372 if (t
& VT_UNSIGNED
) {
1379 else if (op
== TOK_LT
)
1381 else if (op
== TOK_GT
)
1383 else if (op
== TOK_LE
)
1385 else if (op
== TOK_GE
)
1392 /* special case for shifts and long long: we keep the shift as
1394 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1401 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1402 /* relationnal op: the result is an int */
1403 vtop
->type
.t
= VT_INT
;
1410 #ifndef TCC_TARGET_ARM
1411 /* generic itof for unsigned long long case */
1412 void gen_cvt_itof1(int t
)
1414 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1415 (VT_LLONG
| VT_UNSIGNED
)) {
1418 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1419 #if LDOUBLE_SIZE != 8
1420 else if (t
== VT_LDOUBLE
)
1421 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1424 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1428 vtop
->r
= reg_fret(t
);
1435 /* generic ftoi for unsigned long long case */
1436 void gen_cvt_ftoi1(int t
)
1440 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1441 /* not handled natively */
1442 st
= vtop
->type
.t
& VT_BTYPE
;
1444 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1445 #if LDOUBLE_SIZE != 8
1446 else if (st
== VT_LDOUBLE
)
1447 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1450 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1455 vtop
->r2
= REG_LRET
;
1461 /* force char or short cast */
1462 void force_charshort_cast(int t
)
1466 /* XXX: add optimization if lvalue : just change type and offset */
1471 if (t
& VT_UNSIGNED
) {
1472 vpushi((1 << bits
) - 1);
1478 /* result must be signed or the SAR is converted to an SHL
1479 This was not the case when "t" was a signed short
1480 and the last value on the stack was an unsigned int */
1481 vtop
->type
.t
&= ~VT_UNSIGNED
;
1487 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1488 static void gen_cast(CType
*type
)
1490 int sbt
, dbt
, sf
, df
, c
, p
;
1492 /* special delayed cast for char/short */
1493 /* XXX: in some cases (multiple cascaded casts), it may still
1495 if (vtop
->r
& VT_MUSTCAST
) {
1496 vtop
->r
&= ~VT_MUSTCAST
;
1497 force_charshort_cast(vtop
->type
.t
);
1500 /* bitfields first get cast to ints */
1501 if (vtop
->type
.t
& VT_BITFIELD
) {
1505 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1506 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1511 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1512 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1514 /* constant case: we can do it now */
1515 /* XXX: in ISOC, cannot do it if error in convert */
1516 if (sbt
== VT_FLOAT
)
1517 vtop
->c
.ld
= vtop
->c
.f
;
1518 else if (sbt
== VT_DOUBLE
)
1519 vtop
->c
.ld
= vtop
->c
.d
;
1522 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1523 if (sbt
& VT_UNSIGNED
)
1524 vtop
->c
.ld
= vtop
->c
.ull
;
1526 vtop
->c
.ld
= vtop
->c
.ll
;
1528 if (sbt
& VT_UNSIGNED
)
1529 vtop
->c
.ld
= vtop
->c
.ui
;
1531 vtop
->c
.ld
= vtop
->c
.i
;
1534 if (dbt
== VT_FLOAT
)
1535 vtop
->c
.f
= (float)vtop
->c
.ld
;
1536 else if (dbt
== VT_DOUBLE
)
1537 vtop
->c
.d
= (double)vtop
->c
.ld
;
1538 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1539 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1540 } else if (sf
&& dbt
== VT_BOOL
) {
1541 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1544 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1545 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1546 vtop
->c
.ll
= vtop
->c
.ull
;
1547 else if (sbt
& VT_UNSIGNED
)
1548 vtop
->c
.ll
= vtop
->c
.ui
;
1549 #ifdef TCC_TARGET_X86_64
1550 else if (sbt
== VT_PTR
)
1553 else if (sbt
!= VT_LLONG
)
1554 vtop
->c
.ll
= vtop
->c
.i
;
1556 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1557 vtop
->c
.ull
= vtop
->c
.ll
;
1558 else if (dbt
== VT_BOOL
)
1559 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1560 else if (dbt
!= VT_LLONG
) {
1562 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1564 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1567 if(dbt
& VT_UNSIGNED
)
1568 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1570 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1573 } else if (p
&& dbt
== VT_BOOL
) {
1576 } else if (!nocode_wanted
) {
1577 /* non constant case: generate code */
1579 /* convert from fp to fp */
1582 /* convert int to fp */
1585 /* convert fp to int */
1586 if (dbt
== VT_BOOL
) {
1590 /* we handle char/short/etc... with generic code */
1591 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1592 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1596 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1597 /* additional cast for char/short... */
1602 #ifndef TCC_TARGET_X86_64
1603 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1604 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1605 /* scalar to long long */
1606 /* machine independent conversion */
1608 /* generate high word */
1609 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1613 if (sbt
== VT_PTR
) {
1614 /* cast from pointer to int before we apply
1615 shift operation, which pointers don't support*/
1616 gen_cast(&int_type
);
1622 /* patch second register */
1623 vtop
[-1].r2
= vtop
->r
;
1627 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1628 (dbt
& VT_BTYPE
) == VT_PTR
) {
1629 /* XXX: not sure if this is perfect... need more tests */
1630 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1632 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1633 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1634 /* x86_64 specific: movslq */
1636 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1640 } else if (dbt
== VT_BOOL
) {
1641 /* scalar to bool */
1644 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1645 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1646 if (sbt
== VT_PTR
) {
1647 vtop
->type
.t
= VT_INT
;
1648 warning("nonportable conversion from pointer to char/short");
1650 force_charshort_cast(dbt
);
1651 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1653 if (sbt
== VT_LLONG
) {
1654 /* from long long: just take low order word */
1658 /* if lvalue and single word type, nothing to do because
1659 the lvalue already contains the real type size (see
1660 VT_LVAL_xxx constants) */
1663 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1664 /* if we are casting between pointer types,
1665 we must update the VT_LVAL_xxx size */
1666 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1667 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1672 /* return type size. Put alignment at 'a' */
1673 static int type_size(CType
*type
, int *a
)
1678 bt
= type
->t
& VT_BTYPE
;
1679 if (bt
== VT_STRUCT
) {
1684 } else if (bt
== VT_PTR
) {
1685 if (type
->t
& VT_ARRAY
) {
1689 ts
= type_size(&s
->type
, a
);
1691 if (ts
< 0 && s
->c
< 0)
1699 } else if (bt
== VT_LDOUBLE
) {
1701 return LDOUBLE_SIZE
;
1702 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1703 #ifdef TCC_TARGET_I386
1704 #ifdef TCC_TARGET_PE
1709 #elif defined(TCC_TARGET_ARM)
1719 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1722 } else if (bt
== VT_SHORT
) {
1726 /* char, void, function, _Bool */
1732 /* return the pointed type of t */
1733 static inline CType
*pointed_type(CType
*type
)
1735 return &type
->ref
->type
;
1738 /* modify type so that its it is a pointer to type. */
1739 static void mk_pointer(CType
*type
)
1742 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1743 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1747 /* compare function types. OLD functions match any new functions */
1748 static int is_compatible_func(CType
*type1
, CType
*type2
)
1754 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1756 /* check func_call */
1757 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1759 /* XXX: not complete */
1760 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
1764 while (s1
!= NULL
) {
1767 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
1777 /* return true if type1 and type2 are the same. If unqualified is
1778 true, qualifiers on the types are ignored.
1780 - enums are not checked as gcc __builtin_types_compatible_p ()
1782 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
1786 t1
= type1
->t
& VT_TYPE
;
1787 t2
= type2
->t
& VT_TYPE
;
1789 /* strip qualifiers before comparing */
1790 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1791 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1793 /* XXX: bitfields ? */
1796 /* test more complicated cases */
1797 bt1
= t1
& VT_BTYPE
;
1798 if (bt1
== VT_PTR
) {
1799 type1
= pointed_type(type1
);
1800 type2
= pointed_type(type2
);
1801 return is_compatible_types(type1
, type2
);
1802 } else if (bt1
== VT_STRUCT
) {
1803 return (type1
->ref
== type2
->ref
);
1804 } else if (bt1
== VT_FUNC
) {
1805 return is_compatible_func(type1
, type2
);
1811 /* return true if type1 and type2 are exactly the same (including
1814 static int is_compatible_types(CType
*type1
, CType
*type2
)
1816 return compare_types(type1
,type2
,0);
1819 /* return true if type1 and type2 are the same (ignoring qualifiers).
1821 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
1823 return compare_types(type1
,type2
,1);
1826 /* print a type. If 'varstr' is not NULL, then the variable is also
1827 printed in the type */
1829 /* XXX: add array and function pointers */
1830 void type_to_str(char *buf
, int buf_size
,
1831 CType
*type
, const char *varstr
)
1838 t
= type
->t
& VT_TYPE
;
1841 if (t
& VT_CONSTANT
)
1842 pstrcat(buf
, buf_size
, "const ");
1843 if (t
& VT_VOLATILE
)
1844 pstrcat(buf
, buf_size
, "volatile ");
1845 if (t
& VT_UNSIGNED
)
1846 pstrcat(buf
, buf_size
, "unsigned ");
1876 tstr
= "long double";
1878 pstrcat(buf
, buf_size
, tstr
);
1882 if (bt
== VT_STRUCT
)
1886 pstrcat(buf
, buf_size
, tstr
);
1887 v
= type
->ref
->v
& ~SYM_STRUCT
;
1888 if (v
>= SYM_FIRST_ANOM
)
1889 pstrcat(buf
, buf_size
, "<anonymous>");
1891 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
1895 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
1896 pstrcat(buf
, buf_size
, "(");
1898 while (sa
!= NULL
) {
1899 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
1900 pstrcat(buf
, buf_size
, buf1
);
1903 pstrcat(buf
, buf_size
, ", ");
1905 pstrcat(buf
, buf_size
, ")");
1909 pstrcpy(buf1
, sizeof(buf1
), "*");
1911 pstrcat(buf1
, sizeof(buf1
), varstr
);
1912 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
1916 pstrcat(buf
, buf_size
, " ");
1917 pstrcat(buf
, buf_size
, varstr
);
1922 /* verify type compatibility to store vtop in 'dt' type, and generate
1924 static void gen_assign_cast(CType
*dt
)
1926 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
1927 char buf1
[256], buf2
[256];
1930 st
= &vtop
->type
; /* source type */
1931 dbt
= dt
->t
& VT_BTYPE
;
1932 sbt
= st
->t
& VT_BTYPE
;
1933 if (dt
->t
& VT_CONSTANT
)
1934 warning("assignment of read-only location");
1937 /* special cases for pointers */
1938 /* '0' can also be a pointer */
1939 if (is_null_pointer(vtop
))
1941 /* accept implicit pointer to integer cast with warning */
1942 if (is_integer_btype(sbt
)) {
1943 warning("assignment makes pointer from integer without a cast");
1946 type1
= pointed_type(dt
);
1947 /* a function is implicitely a function pointer */
1948 if (sbt
== VT_FUNC
) {
1949 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
1950 !is_compatible_types(pointed_type(dt
), st
))
1951 warning("assignment from incompatible pointer type");
1956 type2
= pointed_type(st
);
1957 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1958 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
1959 /* void * can match anything */
1961 /* exact type match, except for unsigned */
1964 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1965 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1966 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
1967 warning("assignment from incompatible pointer type");
1969 /* check const and volatile */
1970 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
1971 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
1972 warning("assignment discards qualifiers from pointer target type");
1978 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
1979 warning("assignment makes integer from pointer without a cast");
1981 /* XXX: more tests */
1986 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1987 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1988 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1990 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
1991 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
1992 error("cannot cast '%s' to '%s'", buf1
, buf2
);
2000 /* store vtop in lvalue pushed on stack */
2003 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2005 ft
= vtop
[-1].type
.t
;
2006 sbt
= vtop
->type
.t
& VT_BTYPE
;
2007 dbt
= ft
& VT_BTYPE
;
2008 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2009 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2010 /* optimize char/short casts */
2011 delayed_cast
= VT_MUSTCAST
;
2012 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2013 /* XXX: factorize */
2014 if (ft
& VT_CONSTANT
)
2015 warning("assignment of read-only location");
2018 if (!(ft
& VT_BITFIELD
))
2019 gen_assign_cast(&vtop
[-1].type
);
2022 if (sbt
== VT_STRUCT
) {
2023 /* if structure, only generate pointer */
2024 /* structure assignment : generate memcpy */
2025 /* XXX: optimize if small size */
2026 if (!nocode_wanted
) {
2027 size
= type_size(&vtop
->type
, &align
);
2031 vtop
->type
.t
= VT_PTR
;
2034 /* address of memcpy() */
2037 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2038 else if(!(align
& 3))
2039 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2042 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2047 vtop
->type
.t
= VT_PTR
;
2056 /* leave source on stack */
2057 } else if (ft
& VT_BITFIELD
) {
2058 /* bitfield store handling */
2059 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2060 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2061 /* remove bit field info to avoid loops */
2062 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2064 /* duplicate source into other register */
2069 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2070 gen_cast(&vtop
[-1].type
);
2071 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2074 /* duplicate destination */
2076 vtop
[-1] = vtop
[-2];
2078 /* mask and shift source */
2079 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2080 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2081 vpushll((1ULL << bit_size
) - 1ULL);
2083 vpushi((1 << bit_size
) - 1);
2089 /* load destination, mask and or with source */
2091 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2092 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2094 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2101 /* pop off shifted source from "duplicate source..." above */
2105 #ifdef CONFIG_TCC_BCHECK
2106 /* bound check case */
2107 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2113 if (!nocode_wanted
) {
2117 #ifdef TCC_TARGET_X86_64
2118 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2123 r
= gv(rc
); /* generate value */
2124 /* if lvalue was saved on stack, must read it */
2125 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2127 t
= get_reg(RC_INT
);
2128 #ifdef TCC_TARGET_X86_64
2133 sv
.r
= VT_LOCAL
| VT_LVAL
;
2134 sv
.c
.ul
= vtop
[-1].c
.ul
;
2136 vtop
[-1].r
= t
| VT_LVAL
;
2139 #ifndef TCC_TARGET_X86_64
2140 /* two word case handling : store second register at word + 4 */
2141 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2143 /* convert to int to increment easily */
2144 vtop
->type
.t
= VT_INT
;
2150 /* XXX: it works because r2 is spilled last ! */
2151 store(vtop
->r2
, vtop
- 1);
2156 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2157 vtop
->r
|= delayed_cast
;
2161 /* post defines POST/PRE add. c is the token ++ or -- */
2162 void inc(int post
, int c
)
2165 vdup(); /* save lvalue */
2167 gv_dup(); /* duplicate value */
2172 vpushi(c
- TOK_MID
);
2174 vstore(); /* store value */
2176 vpop(); /* if post op, return saved value */
2179 /* Parse GNUC __attribute__ extension. Currently, the following
2180 extensions are recognized:
2181 - aligned(n) : set data/function alignment.
2182 - packed : force data alignment to 1
2183 - section(x) : generate data/code in this section.
2184 - unused : currently ignored, but may be used someday.
2185 - regparm(n) : pass function parameters in registers (i386 only)
2187 static void parse_attribute(AttributeDef
*ad
)
2191 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2195 while (tok
!= ')') {
2196 if (tok
< TOK_IDENT
)
2197 expect("attribute name");
2205 expect("section name");
2206 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2215 if (n
<= 0 || (n
& (n
- 1)) != 0)
2216 error("alignment must be a positive power of two");
2229 /* currently, no need to handle it because tcc does not
2230 track unused objects */
2234 /* currently, no need to handle it because tcc does not
2235 track unused objects */
2240 ad
->func_call
= FUNC_CDECL
;
2245 ad
->func_call
= FUNC_STDCALL
;
2247 #ifdef TCC_TARGET_I386
2257 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2263 ad
->func_call
= FUNC_FASTCALLW
;
2267 ad
->func_export
= 1;
2270 ad
->func_import
= 1;
2273 if (tcc_state
->warn_unsupported
)
2274 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2275 /* skip parameters */
2277 int parenthesis
= 0;
2281 else if (tok
== ')')
2284 } while (parenthesis
&& tok
!= -1);
2297 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2298 static void struct_decl(CType
*type
, int u
)
2300 int a
, v
, size
, align
, maxalign
, c
, offset
;
2301 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2302 Sym
*s
, *ss
, *ass
, **ps
;
2306 a
= tok
; /* save decl type */
2311 /* struct already defined ? return it */
2313 expect("struct/union/enum name");
2317 error("invalid type");
2324 /* we put an undefined size for struct/union */
2325 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2326 s
->r
= 0; /* default alignment is zero as gcc */
2327 /* put struct/union/enum name in type */
2335 error("struct/union/enum already defined");
2336 /* cannot be empty */
2338 /* non empty enums are not allowed */
2339 if (a
== TOK_ENUM
) {
2343 expect("identifier");
2349 /* enum symbols have static storage */
2350 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2351 ss
->type
.t
|= VT_STATIC
;
2356 /* NOTE: we accept a trailing comma */
2367 while (tok
!= '}') {
2368 parse_btype(&btype
, &ad
);
2374 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2375 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2376 expect("identifier");
2377 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2378 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2379 error("invalid type for '%s'",
2380 get_tok_str(v
, NULL
));
2384 bit_size
= expr_const();
2385 /* XXX: handle v = 0 case for messages */
2387 error("negative width in bit-field '%s'",
2388 get_tok_str(v
, NULL
));
2389 if (v
&& bit_size
== 0)
2390 error("zero width for bit-field '%s'",
2391 get_tok_str(v
, NULL
));
2393 size
= type_size(&type1
, &align
);
2395 if (align
< ad
.aligned
)
2397 } else if (ad
.packed
) {
2399 } else if (*tcc_state
->pack_stack_ptr
) {
2400 if (align
> *tcc_state
->pack_stack_ptr
)
2401 align
= *tcc_state
->pack_stack_ptr
;
2404 if (bit_size
>= 0) {
2405 bt
= type1
.t
& VT_BTYPE
;
2412 error("bitfields must have scalar type");
2414 if (bit_size
> bsize
) {
2415 error("width of '%s' exceeds its type",
2416 get_tok_str(v
, NULL
));
2417 } else if (bit_size
== bsize
) {
2418 /* no need for bit fields */
2420 } else if (bit_size
== 0) {
2421 /* XXX: what to do if only padding in a
2423 /* zero size: means to pad */
2426 /* we do not have enough room ?
2427 did the type change?
2429 if ((bit_pos
+ bit_size
) > bsize
||
2430 bt
!= prevbt
|| a
== TOK_UNION
)
2433 /* XXX: handle LSB first */
2434 type1
.t
|= VT_BITFIELD
|
2435 (bit_pos
<< VT_STRUCT_SHIFT
) |
2436 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2437 bit_pos
+= bit_size
;
2443 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2444 /* add new memory data only if starting
2446 if (lbit_pos
== 0) {
2447 if (a
== TOK_STRUCT
) {
2448 c
= (c
+ align
- 1) & -align
;
2457 if (align
> maxalign
)
2461 printf("add field %s offset=%d",
2462 get_tok_str(v
, NULL
), offset
);
2463 if (type1
.t
& VT_BITFIELD
) {
2464 printf(" pos=%d size=%d",
2465 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2466 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2471 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2473 while ((ass
= ass
->next
) != NULL
) {
2474 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2479 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2483 if (tok
== ';' || tok
== TOK_EOF
)
2490 /* store size and alignment */
2491 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2497 /* return 0 if no type declaration. otherwise, return the basic type
2500 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2502 int t
, u
, type_found
, typespec_found
, typedef_found
;
2506 memset(ad
, 0, sizeof(AttributeDef
));
2514 /* currently, we really ignore extension */
2524 if ((t
& VT_BTYPE
) != 0)
2525 error("too many basic types");
2541 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2542 #ifndef TCC_TARGET_PE
2543 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2545 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2546 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2560 if ((t
& VT_BTYPE
) == VT_LONG
) {
2561 #ifdef TCC_TARGET_PE
2562 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2564 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2572 struct_decl(&type1
, VT_ENUM
);
2575 type
->ref
= type1
.ref
;
2579 struct_decl(&type1
, VT_STRUCT
);
2582 /* type modifiers */
2635 /* GNUC attribute */
2636 case TOK_ATTRIBUTE1
:
2637 case TOK_ATTRIBUTE2
:
2638 parse_attribute(ad
);
2645 parse_expr_type(&type1
);
2648 if (typespec_found
|| typedef_found
)
2651 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2654 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2655 type
->ref
= s
->type
.ref
;
2657 /* get attributes from typedef */
2658 if (0 == ad
->aligned
)
2659 ad
->aligned
= FUNC_ALIGN(s
->r
);
2660 if (0 == ad
->func_call
)
2661 ad
->func_call
= FUNC_CALL(s
->r
);
2662 ad
->packed
|= FUNC_PACKED(s
->r
);
2671 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2672 error("signed and unsigned modifier");
2673 if (tcc_state
->char_is_unsigned
) {
2674 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2679 /* long is never used as type */
2680 if ((t
& VT_BTYPE
) == VT_LONG
)
2681 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2682 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2684 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2690 /* convert a function parameter type (array to pointer and function to
2691 function pointer) */
2692 static inline void convert_parameter_type(CType
*pt
)
2694 /* remove const and volatile qualifiers (XXX: const could be used
2695 to indicate a const function parameter */
2696 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2697 /* array must be transformed to pointer according to ANSI C */
2699 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2704 static void post_type(CType
*type
, AttributeDef
*ad
)
2706 int n
, l
, t1
, arg_size
, align
;
2707 Sym
**plast
, *s
, *first
;
2712 /* function declaration */
2720 /* read param name and compute offset */
2721 if (l
!= FUNC_OLD
) {
2722 if (!parse_btype(&pt
, &ad1
)) {
2724 error("invalid type");
2731 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2733 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2734 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2735 error("parameter declared as void");
2736 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
2741 expect("identifier");
2745 convert_parameter_type(&pt
);
2746 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
2752 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
2759 /* if no parameters, then old type prototype */
2763 t1
= type
->t
& VT_STORAGE
;
2764 /* NOTE: const is ignored in returned type as it has a special
2765 meaning in gcc / C++ */
2766 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
2767 post_type(type
, ad
);
2768 /* we push a anonymous symbol which will contain the function prototype */
2769 ad
->func_args
= arg_size
;
2770 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
2772 type
->t
= t1
| VT_FUNC
;
2774 } else if (tok
== '[') {
2775 /* array definition */
2777 if (tok
== TOK_RESTRICT1
)
2783 error("invalid array size");
2786 /* parse next post type */
2787 t1
= type
->t
& VT_STORAGE
;
2788 type
->t
&= ~VT_STORAGE
;
2789 post_type(type
, ad
);
2791 /* we push a anonymous symbol which will contain the array
2793 s
= sym_push(SYM_FIELD
, type
, 0, n
);
2794 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
2799 /* Parse a type declaration (except basic type), and return the type
2800 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2801 expected. 'type' should contain the basic type. 'ad' is the
2802 attribute definition of the basic type. It can be modified by
2805 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
2808 CType type1
, *type2
;
2811 while (tok
== '*') {
2819 qualifiers
|= VT_CONSTANT
;
2824 qualifiers
|= VT_VOLATILE
;
2832 type
->t
|= qualifiers
;
2835 /* XXX: clarify attribute handling */
2836 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2837 parse_attribute(ad
);
2839 /* recursive type */
2840 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2841 type1
.t
= 0; /* XXX: same as int */
2844 /* XXX: this is not correct to modify 'ad' at this point, but
2845 the syntax is not clear */
2846 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2847 parse_attribute(ad
);
2848 type_decl(&type1
, ad
, v
, td
);
2851 /* type identifier */
2852 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
2856 if (!(td
& TYPE_ABSTRACT
))
2857 expect("identifier");
2861 post_type(type
, ad
);
2862 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2863 parse_attribute(ad
);
2866 /* append type at the end of type1 */
2879 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2880 static int lvalue_type(int t
)
2885 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
2887 else if (bt
== VT_SHORT
)
2891 if (t
& VT_UNSIGNED
)
2892 r
|= VT_LVAL_UNSIGNED
;
2896 /* indirection with full error checking and bound check */
2897 static void indir(void)
2899 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
2900 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
2904 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
2906 vtop
->type
= *pointed_type(&vtop
->type
);
2907 /* Arrays and functions are never lvalues */
2908 if (!(vtop
->type
.t
& VT_ARRAY
)
2909 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
2910 vtop
->r
|= lvalue_type(vtop
->type
.t
);
2911 /* if bound checking, the referenced pointer must be checked */
2912 if (tcc_state
->do_bounds_check
)
2913 vtop
->r
|= VT_MUSTBOUND
;
2917 /* pass a parameter to a function and do type checking and casting */
2918 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2923 func_type
= func
->c
;
2924 if (func_type
== FUNC_OLD
||
2925 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2926 /* default casting : only need to convert float to double */
2927 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2931 } else if (arg
== NULL
) {
2932 error("too many arguments to function");
2935 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2936 gen_assign_cast(&type
);
2940 /* parse an expression of the form '(type)' or '(expr)' and return its
2942 static void parse_expr_type(CType
*type
)
2948 if (parse_btype(type
, &ad
)) {
2949 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2956 static void parse_type(CType
*type
)
2961 if (!parse_btype(type
, &ad
)) {
2964 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2967 static void vpush_tokc(int t
)
2971 vsetc(&type
, VT_CONST
, &tokc
);
2974 static void unary(void)
2976 int n
, t
, align
, size
, r
;
2981 /* XXX: GCC 2.95.3 does not generate a table although it should be
2995 vpush_tokc(VT_INT
| VT_UNSIGNED
);
2999 vpush_tokc(VT_LLONG
);
3003 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3007 vpush_tokc(VT_FLOAT
);
3011 vpush_tokc(VT_DOUBLE
);
3015 vpush_tokc(VT_LDOUBLE
);
3018 case TOK___FUNCTION__
:
3020 goto tok_identifier
;
3026 /* special function name identifier */
3027 len
= strlen(funcname
) + 1;
3028 /* generate char[len] type */
3033 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3034 ptr
= section_ptr_add(data_section
, len
);
3035 memcpy(ptr
, funcname
, len
);
3040 #ifdef TCC_TARGET_PE
3041 t
= VT_SHORT
| VT_UNSIGNED
;
3047 /* string parsing */
3050 if (tcc_state
->warn_write_strings
)
3055 memset(&ad
, 0, sizeof(AttributeDef
));
3056 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3061 if (parse_btype(&type
, &ad
)) {
3062 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3064 /* check ISOC99 compound literal */
3066 /* data is allocated locally by default */
3071 /* all except arrays are lvalues */
3072 if (!(type
.t
& VT_ARRAY
))
3073 r
|= lvalue_type(type
.t
);
3074 memset(&ad
, 0, sizeof(AttributeDef
));
3075 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3080 } else if (tok
== '{') {
3081 /* save all registers */
3083 /* statement expression : we do not accept break/continue
3084 inside as GCC does */
3085 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3100 /* functions names must be treated as function pointers,
3101 except for unary '&' and sizeof. Since we consider that
3102 functions are not lvalues, we only have to handle it
3103 there and in function calls. */
3104 /* arrays can also be used although they are not lvalues */
3105 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3106 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3108 mk_pointer(&vtop
->type
);
3114 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3116 boolean
.t
= VT_BOOL
;
3118 vtop
->c
.i
= !vtop
->c
.i
;
3119 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3120 vtop
->c
.i
= vtop
->c
.i
^ 1;
3123 vseti(VT_JMP
, gtst(1, 0));
3134 /* in order to force cast, we add zero */
3136 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3137 error("pointer not accepted for unary plus");
3147 parse_expr_type(&type
);
3151 size
= type_size(&type
, &align
);
3152 if (t
== TOK_SIZEOF
) {
3154 error("sizeof applied to an incomplete type");
3159 vtop
->type
.t
|= VT_UNSIGNED
;
3162 case TOK_builtin_types_compatible_p
:
3171 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3172 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3173 vpushi(is_compatible_types(&type1
, &type2
));
3176 case TOK_builtin_constant_p
:
3178 int saved_nocode_wanted
, res
;
3181 saved_nocode_wanted
= nocode_wanted
;
3184 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3186 nocode_wanted
= saved_nocode_wanted
;
3191 case TOK_builtin_frame_address
:
3196 if (tok
!= TOK_CINT
) {
3197 error("__builtin_frame_address only takes integers");
3200 error("TCC only supports __builtin_frame_address(0)");
3206 vset(&type
, VT_LOCAL
, 0);
3209 #ifdef TCC_TARGET_X86_64
3210 case TOK_builtin_malloc
:
3212 goto tok_identifier
;
3213 case TOK_builtin_free
:
3215 goto tok_identifier
;
3232 goto tok_identifier
;
3234 /* allow to take the address of a label */
3235 if (tok
< TOK_UIDENT
)
3236 expect("label identifier");
3237 s
= label_find(tok
);
3239 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3241 if (s
->r
== LABEL_DECLARED
)
3242 s
->r
= LABEL_FORWARD
;
3245 s
->type
.t
= VT_VOID
;
3246 mk_pointer(&s
->type
);
3247 s
->type
.t
|= VT_STATIC
;
3249 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3258 expect("identifier");
3262 error("'%s' undeclared", get_tok_str(t
, NULL
));
3263 /* for simple function calls, we tolerate undeclared
3264 external reference to int() function */
3265 if (tcc_state
->warn_implicit_function_declaration
)
3266 warning("implicit declaration of function '%s'",
3267 get_tok_str(t
, NULL
));
3268 s
= external_global_sym(t
, &func_old_type
, 0);
3270 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3271 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3272 /* if referencing an inline function, then we generate a
3273 symbol to it if not already done. It will have the
3274 effect to generate code for it at the end of the
3275 compilation unit. Inline function as always
3276 generated in the text section. */
3278 put_extern_sym(s
, text_section
, 0, 0);
3279 r
= VT_SYM
| VT_CONST
;
3283 vset(&s
->type
, r
, s
->c
);
3284 /* if forward reference, we must point to s */
3285 if (vtop
->r
& VT_SYM
) {
3292 /* post operations */
3294 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3297 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3300 if (tok
== TOK_ARROW
)
3302 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3306 /* expect pointer on structure */
3307 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3308 expect("struct or union");
3312 while ((s
= s
->next
) != NULL
) {
3317 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3318 /* add field offset to pointer */
3319 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3322 /* change type to field type, and set to lvalue */
3323 vtop
->type
= s
->type
;
3324 vtop
->type
.t
|= qualifiers
;
3325 /* an array is never an lvalue */
3326 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3327 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3328 /* if bound checking, the referenced pointer must be checked */
3329 if (tcc_state
->do_bounds_check
)
3330 vtop
->r
|= VT_MUSTBOUND
;
3333 } else if (tok
== '[') {
3339 } else if (tok
== '(') {
3345 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3346 /* pointer test (no array accepted) */
3347 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3348 vtop
->type
= *pointed_type(&vtop
->type
);
3349 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3353 expect("function pointer");
3356 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3358 /* get return type */
3361 sa
= s
->next
; /* first parameter */
3364 /* compute first implicit argument if a structure is returned */
3365 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3366 /* get some space for the returned structure */
3367 size
= type_size(&s
->type
, &align
);
3368 loc
= (loc
- size
) & -align
;
3370 ret
.r
= VT_LOCAL
| VT_LVAL
;
3371 /* pass it as 'int' to avoid structure arg passing
3373 vseti(VT_LOCAL
, loc
);
3378 /* return in register */
3379 if (is_float(ret
.type
.t
)) {
3380 ret
.r
= reg_fret(ret
.type
.t
);
3382 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3391 gfunc_param_typed(s
, sa
);
3401 error("too few arguments to function");
3403 if (!nocode_wanted
) {
3404 gfunc_call(nb_args
);
3406 vtop
-= (nb_args
+ 1);
3409 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3417 static void uneq(void)
3423 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3424 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3425 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3440 static void expr_prod(void)
3445 while (tok
== '*' || tok
== '/' || tok
== '%') {
3453 static void expr_sum(void)
3458 while (tok
== '+' || tok
== '-') {
3466 static void expr_shift(void)
3471 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3479 static void expr_cmp(void)
3484 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3485 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3493 static void expr_cmpeq(void)
3498 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3506 static void expr_and(void)
3509 while (tok
== '&') {
3516 static void expr_xor(void)
3519 while (tok
== '^') {
3526 static void expr_or(void)
3529 while (tok
== '|') {
3536 /* XXX: fix this mess */
3537 static void expr_land_const(void)
3540 while (tok
== TOK_LAND
) {
3547 /* XXX: fix this mess */
3548 static void expr_lor_const(void)
3551 while (tok
== TOK_LOR
) {
3558 /* only used if non constant */
3559 static void expr_land(void)
3564 if (tok
== TOK_LAND
) {
3569 if (tok
!= TOK_LAND
) {
3579 static void expr_lor(void)
3584 if (tok
== TOK_LOR
) {
3589 if (tok
!= TOK_LOR
) {
3599 /* XXX: better constant handling */
3600 static void expr_eq(void)
3602 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3604 CType type
, type1
, type2
;
3611 boolean
.t
= VT_BOOL
;
3617 if (tok
!= ':' || !gnu_ext
) {
3632 if (vtop
!= vstack
) {
3633 /* needed to avoid having different registers saved in
3635 if (is_float(vtop
->type
.t
)) {
3637 #ifdef TCC_TARGET_X86_64
3638 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3648 if (tok
== ':' && gnu_ext
) {
3656 sv
= *vtop
; /* save value to handle it later */
3657 vtop
--; /* no vpop so that FP stack is not flushed */
3665 bt1
= t1
& VT_BTYPE
;
3667 bt2
= t2
& VT_BTYPE
;
3668 /* cast operands to correct type according to ISOC rules */
3669 if (is_float(bt1
) || is_float(bt2
)) {
3670 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3671 type
.t
= VT_LDOUBLE
;
3672 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3677 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3678 /* cast to biggest op */
3680 /* convert to unsigned if it does not fit in a long long */
3681 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3682 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3683 type
.t
|= VT_UNSIGNED
;
3684 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3685 /* XXX: test pointer compatibility */
3687 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3688 /* XXX: test function pointer compatibility */
3690 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3691 /* XXX: test structure compatibility */
3693 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3694 /* NOTE: as an extension, we accept void on only one side */
3697 /* integer operations */
3699 /* convert to unsigned if it does not fit in an integer */
3700 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3701 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3702 type
.t
|= VT_UNSIGNED
;
3705 /* now we convert second operand */
3707 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3710 if (is_float(type
.t
)) {
3712 #ifdef TCC_TARGET_X86_64
3713 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3717 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3718 /* for long longs, we use fixed registers to avoid having
3719 to handle a complicated move */
3724 /* this is horrible, but we must also convert first
3728 /* put again first value and cast it */
3731 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3741 static void gexpr(void)
3752 /* parse an expression and return its type without any side effect. */
3753 static void expr_type(CType
*type
)
3755 int saved_nocode_wanted
;
3757 saved_nocode_wanted
= nocode_wanted
;
3762 nocode_wanted
= saved_nocode_wanted
;
3765 /* parse a unary expression and return its type without any side
3767 static void unary_type(CType
*type
)
3779 /* parse a constant expression and return value in vtop. */
3780 static void expr_const1(void)
3789 /* parse an integer constant and return its value. */
3790 static int expr_const(void)
3794 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3795 expect("constant expression");
3801 /* return the label token if current token is a label, otherwise
3803 static int is_label(void)
3807 /* fast test first */
3808 if (tok
< TOK_UIDENT
)
3810 /* no need to save tokc because tok is an identifier */
3817 unget_tok(last_tok
);
3822 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3823 int case_reg
, int is_expr
)
3828 /* generate line number info */
3829 if (tcc_state
->do_debug
&&
3830 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3831 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3833 last_line_num
= file
->line_num
;
3837 /* default return value is (void) */
3839 vtop
->type
.t
= VT_VOID
;
3842 if (tok
== TOK_IF
) {
3849 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3851 if (c
== TOK_ELSE
) {
3855 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3856 gsym(d
); /* patch else jmp */
3859 } else if (tok
== TOK_WHILE
) {
3867 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3871 } else if (tok
== '{') {
3875 /* record local declaration stack position */
3877 llabel
= local_label_stack
;
3878 /* handle local labels declarations */
3879 if (tok
== TOK_LABEL
) {
3882 if (tok
< TOK_UIDENT
)
3883 expect("label identifier");
3884 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3894 while (tok
!= '}') {
3899 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3902 /* pop locally defined labels */
3903 label_pop(&local_label_stack
, llabel
);
3904 /* pop locally defined symbols */
3906 /* XXX: this solution makes only valgrind happy...
3907 triggered by gcc.c-torture/execute/20000917-1.c */
3909 switch(vtop
->type
.t
& VT_BTYPE
) {
3914 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3916 error("unsupported expression type");
3919 sym_pop(&local_stack
, s
);
3921 } else if (tok
== TOK_RETURN
) {
3925 gen_assign_cast(&func_vt
);
3926 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3928 /* if returning structure, must copy it to implicit
3929 first pointer arg location */
3932 size
= type_size(&func_vt
,&align
);
3935 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3939 loc
= (loc
- size
) & -4;
3942 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3945 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3947 vtop
->type
= int_type
;
3953 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3956 /* copy structure value to pointer */
3961 } else if (is_float(func_vt
.t
)) {
3962 gv(rc_fret(func_vt
.t
));
3966 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3969 rsym
= gjmp(rsym
); /* jmp */
3970 } else if (tok
== TOK_BREAK
) {
3973 error("cannot break");
3974 *bsym
= gjmp(*bsym
);
3977 } else if (tok
== TOK_CONTINUE
) {
3980 error("cannot continue");
3981 *csym
= gjmp(*csym
);
3984 } else if (tok
== TOK_FOR
) {
4011 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4016 if (tok
== TOK_DO
) {
4021 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4032 if (tok
== TOK_SWITCH
) {
4036 /* XXX: other types than integer */
4037 case_reg
= gv(RC_INT
);
4041 b
= gjmp(0); /* jump to first case */
4043 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4044 /* if no default, jmp after switch */
4052 if (tok
== TOK_CASE
) {
4059 if (gnu_ext
&& tok
== TOK_DOTS
) {
4063 warning("empty case range");
4065 /* since a case is like a label, we must skip it with a jmp */
4072 *case_sym
= gtst(1, 0);
4075 *case_sym
= gtst(1, 0);
4079 *case_sym
= gtst(1, *case_sym
);
4084 goto block_after_label
;
4086 if (tok
== TOK_DEFAULT
) {
4092 error("too many 'default'");
4095 goto block_after_label
;
4097 if (tok
== TOK_GOTO
) {
4099 if (tok
== '*' && gnu_ext
) {
4103 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4106 } else if (tok
>= TOK_UIDENT
) {
4107 s
= label_find(tok
);
4108 /* put forward definition if needed */
4110 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4112 if (s
->r
== LABEL_DECLARED
)
4113 s
->r
= LABEL_FORWARD
;
4115 /* label already defined */
4116 if (s
->r
& LABEL_FORWARD
)
4117 s
->jnext
= gjmp(s
->jnext
);
4119 gjmp_addr(s
->jnext
);
4122 expect("label identifier");
4125 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4133 if (s
->r
== LABEL_DEFINED
)
4134 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4136 s
->r
= LABEL_DEFINED
;
4138 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4141 /* we accept this, but it is a mistake */
4144 warning("deprecated use of label at end of compound statement");
4148 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4151 /* expression case */
4166 /* t is the array or struct type. c is the array or struct
4167 address. cur_index/cur_field is the pointer to the current
4168 value. 'size_only' is true if only size info is needed (only used
4170 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4171 int *cur_index
, Sym
**cur_field
,
4175 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4181 if (gnu_ext
&& (l
= is_label()) != 0)
4183 while (tok
== '[' || tok
== '.') {
4185 if (!(type
->t
& VT_ARRAY
))
4186 expect("array type");
4189 index
= expr_const();
4190 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4191 expect("invalid index");
4192 if (tok
== TOK_DOTS
&& gnu_ext
) {
4194 index_last
= expr_const();
4195 if (index_last
< 0 ||
4196 (s
->c
>= 0 && index_last
>= s
->c
) ||
4198 expect("invalid index");
4204 *cur_index
= index_last
;
4205 type
= pointed_type(type
);
4206 elem_size
= type_size(type
, &align
);
4207 c
+= index
* elem_size
;
4208 /* NOTE: we only support ranges for last designator */
4209 nb_elems
= index_last
- index
+ 1;
4210 if (nb_elems
!= 1) {
4219 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4220 expect("struct/union type");
4233 /* XXX: fix this mess by using explicit storage field */
4235 type1
.t
|= (type
->t
& ~VT_TYPE
);
4249 if (type
->t
& VT_ARRAY
) {
4251 type
= pointed_type(type
);
4252 c
+= index
* type_size(type
, &align
);
4256 error("too many field init");
4257 /* XXX: fix this mess by using explicit storage field */
4259 type1
.t
|= (type
->t
& ~VT_TYPE
);
4264 decl_initializer(type
, sec
, c
, 0, size_only
);
4266 /* XXX: make it more general */
4267 if (!size_only
&& nb_elems
> 1) {
4268 unsigned long c_end
;
4273 error("range init not supported yet for dynamic storage");
4274 c_end
= c
+ nb_elems
* elem_size
;
4275 if (c_end
> sec
->data_allocated
)
4276 section_realloc(sec
, c_end
);
4277 src
= sec
->data
+ c
;
4279 for(i
= 1; i
< nb_elems
; i
++) {
4281 memcpy(dst
, src
, elem_size
);
4287 #define EXPR_CONST 1
4290 /* store a value or an expression directly in global data or in local array */
4291 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4292 int v
, int expr_type
)
4294 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4296 unsigned long long bit_mask
;
4304 /* compound literals must be allocated globally in this case */
4305 saved_global_expr
= global_expr
;
4308 global_expr
= saved_global_expr
;
4309 /* NOTE: symbols are accepted */
4310 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4311 error("initializer element is not constant");
4319 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4322 /* XXX: not portable */
4323 /* XXX: generate error if incorrect relocation */
4324 gen_assign_cast(&dtype
);
4325 bt
= type
->t
& VT_BTYPE
;
4326 /* we'll write at most 12 bytes */
4327 if (c
+ 12 > sec
->data_allocated
) {
4328 section_realloc(sec
, c
+ 12);
4330 ptr
= sec
->data
+ c
;
4331 /* XXX: make code faster ? */
4332 if (!(type
->t
& VT_BITFIELD
)) {
4337 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4338 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4339 bit_mask
= (1LL << bit_size
) - 1;
4341 if ((vtop
->r
& VT_SYM
) &&
4347 (bt
== VT_INT
&& bit_size
!= 32)))
4348 error("initializer element is not computable at load time");
4351 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4353 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4356 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4359 *(double *)ptr
= vtop
->c
.d
;
4362 *(long double *)ptr
= vtop
->c
.ld
;
4365 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4368 if (vtop
->r
& VT_SYM
) {
4369 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4371 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4376 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4383 /* put zeros for variable based init */
4384 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4387 /* nothing to do because globals are already set to zero */
4389 vpush_global_sym(&func_old_type
, TOK_memset
);
4397 /* 't' contains the type and storage info. 'c' is the offset of the
4398 object in section 'sec'. If 'sec' is NULL, it means stack based
4399 allocation. 'first' is true if array '{' must be read (multi
4400 dimension implicit array init handling). 'size_only' is true if
4401 size only evaluation is wanted (only for arrays). */
4402 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4403 int first
, int size_only
)
4405 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4406 int size1
, align1
, expr_type
;
4410 if (type
->t
& VT_ARRAY
) {
4414 t1
= pointed_type(type
);
4415 size1
= type_size(t1
, &align1
);
4418 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4424 /* only parse strings here if correct type (otherwise: handle
4425 them as ((w)char *) expressions */
4426 if ((tok
== TOK_LSTR
&&
4427 #ifdef TCC_TARGET_PE
4428 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4430 (t1
->t
& VT_BTYPE
) == VT_INT
4432 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4433 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4438 /* compute maximum number of chars wanted */
4440 cstr_len
= cstr
->size
;
4442 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4445 if (n
>= 0 && nb
> (n
- array_length
))
4446 nb
= n
- array_length
;
4449 warning("initializer-string for array is too long");
4450 /* in order to go faster for common case (char
4451 string in global variable, we handle it
4453 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4454 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4458 ch
= ((unsigned char *)cstr
->data
)[i
];
4460 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4461 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4469 /* only add trailing zero if enough storage (no
4470 warning in this case since it is standard) */
4471 if (n
< 0 || array_length
< n
) {
4473 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4479 while (tok
!= '}') {
4480 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4481 if (n
>= 0 && index
>= n
)
4482 error("index too large");
4483 /* must put zero in holes (note that doing it that way
4484 ensures that it even works with designators) */
4485 if (!size_only
&& array_length
< index
) {
4486 init_putz(t1
, sec
, c
+ array_length
* size1
,
4487 (index
- array_length
) * size1
);
4490 if (index
> array_length
)
4491 array_length
= index
;
4492 /* special test for multi dimensional arrays (may not
4493 be strictly correct if designators are used at the
4495 if (index
>= n
&& no_oblock
)
4504 /* put zeros at the end */
4505 if (!size_only
&& n
>= 0 && array_length
< n
) {
4506 init_putz(t1
, sec
, c
+ array_length
* size1
,
4507 (n
- array_length
) * size1
);
4509 /* patch type size if needed */
4511 s
->c
= array_length
;
4512 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4513 (sec
|| !first
|| tok
== '{')) {
4516 /* NOTE: the previous test is a specific case for automatic
4517 struct/union init */
4518 /* XXX: union needs only one init */
4520 /* XXX: this test is incorrect for local initializers
4521 beginning with ( without {. It would be much more difficult
4522 to do it correctly (ideally, the expression parser should
4523 be used in all cases) */
4529 while (tok
== '(') {
4533 if (!parse_btype(&type1
, &ad1
))
4535 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4537 if (!is_assignable_types(type
, &type1
))
4538 error("invalid type for cast");
4543 if (first
|| tok
== '{') {
4552 while (tok
!= '}') {
4553 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4555 if (!size_only
&& array_length
< index
) {
4556 init_putz(type
, sec
, c
+ array_length
,
4557 index
- array_length
);
4559 index
= index
+ type_size(&f
->type
, &align1
);
4560 if (index
> array_length
)
4561 array_length
= index
;
4563 /* gr: skip fields from same union - ugly. */
4565 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4566 /* test for same offset */
4567 if (f
->next
->c
!= f
->c
)
4569 /* if yes, test for bitfield shift */
4570 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4571 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4572 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4573 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4574 if (bit_pos_1
!= bit_pos_2
)
4581 if (no_oblock
&& f
== NULL
)
4587 /* put zeros at the end */
4588 if (!size_only
&& array_length
< n
) {
4589 init_putz(type
, sec
, c
+ array_length
,
4598 } else if (tok
== '{') {
4600 decl_initializer(type
, sec
, c
, first
, size_only
);
4602 } else if (size_only
) {
4603 /* just skip expression */
4605 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4609 else if (tok
== ')')
4614 /* currently, we always use constant expression for globals
4615 (may change for scripting case) */
4616 expr_type
= EXPR_CONST
;
4618 expr_type
= EXPR_ANY
;
4619 init_putv(type
, sec
, c
, 0, expr_type
);
4623 /* parse an initializer for type 't' if 'has_init' is non zero, and
4624 allocate space in local or global data space ('r' is either
4625 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4626 variable 'v' of scope 'scope' is declared before initializers are
4627 parsed. If 'v' is zero, then a reference to the new object is put
4628 in the value stack. If 'has_init' is 2, a special parsing is done
4629 to handle string constants. */
4630 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4631 int has_init
, int v
, int scope
)
4633 int size
, align
, addr
, data_offset
;
4635 ParseState saved_parse_state
= {0};
4636 TokenString init_str
;
4639 size
= type_size(type
, &align
);
4640 /* If unknown size, we must evaluate it before
4641 evaluating initializers because
4642 initializers can generate global data too
4643 (e.g. string pointers or ISOC99 compound
4644 literals). It also simplifies local
4645 initializers handling */
4646 tok_str_new(&init_str
);
4649 error("unknown type size");
4650 /* get all init string */
4651 if (has_init
== 2) {
4652 /* only get strings */
4653 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4654 tok_str_add_tok(&init_str
);
4659 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4661 error("unexpected end of file in initializer");
4662 tok_str_add_tok(&init_str
);
4665 else if (tok
== '}') {
4675 tok_str_add(&init_str
, -1);
4676 tok_str_add(&init_str
, 0);
4679 save_parse_state(&saved_parse_state
);
4681 macro_ptr
= init_str
.str
;
4683 decl_initializer(type
, NULL
, 0, 1, 1);
4684 /* prepare second initializer parsing */
4685 macro_ptr
= init_str
.str
;
4688 /* if still unknown size, error */
4689 size
= type_size(type
, &align
);
4691 error("unknown type size");
4693 /* take into account specified alignment if bigger */
4695 if (ad
->aligned
> align
)
4696 align
= ad
->aligned
;
4697 } else if (ad
->packed
) {
4700 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4702 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4704 loc
= (loc
- size
) & -align
;
4706 /* handles bounds */
4707 /* XXX: currently, since we do only one pass, we cannot track
4708 '&' operators, so we add only arrays */
4709 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4710 unsigned long *bounds_ptr
;
4711 /* add padding between regions */
4713 /* then add local bound info */
4714 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4715 bounds_ptr
[0] = addr
;
4716 bounds_ptr
[1] = size
;
4719 /* local variable */
4720 sym_push(v
, type
, r
, addr
);
4722 /* push local reference */
4723 vset(type
, r
, addr
);
4729 if (v
&& scope
== VT_CONST
) {
4730 /* see if the symbol was already defined */
4733 if (!is_compatible_types(&sym
->type
, type
))
4734 error("incompatible types for redefinition of '%s'",
4735 get_tok_str(v
, NULL
));
4736 if (sym
->type
.t
& VT_EXTERN
) {
4737 /* if the variable is extern, it was not allocated */
4738 sym
->type
.t
&= ~VT_EXTERN
;
4739 /* set array size if it was ommited in extern
4741 if ((sym
->type
.t
& VT_ARRAY
) &&
4742 sym
->type
.ref
->c
< 0 &&
4744 sym
->type
.ref
->c
= type
->ref
->c
;
4746 /* we accept several definitions of the same
4747 global variable. this is tricky, because we
4748 must play with the SHN_COMMON type of the symbol */
4749 /* XXX: should check if the variable was already
4750 initialized. It is incorrect to initialized it
4752 /* no init data, we won't add more to the symbol */
4759 /* allocate symbol in corresponding section */
4764 else if (tcc_state
->nocommon
)
4768 data_offset
= sec
->data_offset
;
4769 data_offset
= (data_offset
+ align
- 1) & -align
;
4771 /* very important to increment global pointer at this time
4772 because initializers themselves can create new initializers */
4773 data_offset
+= size
;
4774 /* add padding if bound check */
4775 if (tcc_state
->do_bounds_check
)
4777 sec
->data_offset
= data_offset
;
4778 /* allocate section space to put the data */
4779 if (sec
->sh_type
!= SHT_NOBITS
&&
4780 data_offset
> sec
->data_allocated
)
4781 section_realloc(sec
, data_offset
);
4782 /* align section if needed */
4783 if (align
> sec
->sh_addralign
)
4784 sec
->sh_addralign
= align
;
4786 addr
= 0; /* avoid warning */
4790 if (scope
!= VT_CONST
|| !sym
) {
4791 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4793 /* update symbol definition */
4795 put_extern_sym(sym
, sec
, addr
, size
);
4798 /* put a common area */
4799 put_extern_sym(sym
, NULL
, align
, size
);
4800 /* XXX: find a nicer way */
4801 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4802 esym
->st_shndx
= SHN_COMMON
;
4807 /* push global reference */
4808 sym
= get_sym_ref(type
, sec
, addr
, size
);
4810 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4814 /* handles bounds now because the symbol must be defined
4815 before for the relocation */
4816 if (tcc_state
->do_bounds_check
) {
4817 unsigned long *bounds_ptr
;
4819 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
4820 /* then add global bound info */
4821 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4822 bounds_ptr
[0] = 0; /* relocated */
4823 bounds_ptr
[1] = size
;
4827 decl_initializer(type
, sec
, addr
, 1, 0);
4828 /* restore parse state if needed */
4830 tok_str_free(init_str
.str
);
4831 restore_parse_state(&saved_parse_state
);
4837 void put_func_debug(Sym
*sym
)
4842 /* XXX: we put here a dummy type */
4843 snprintf(buf
, sizeof(buf
), "%s:%c1",
4844 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4845 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4846 cur_text_section
, sym
->c
);
4847 /* //gr gdb wants a line at the function */
4848 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4853 /* parse an old style function declaration list */
4854 /* XXX: check multiple parameter */
4855 static void func_decl_list(Sym
*func_sym
)
4862 /* parse each declaration */
4863 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4864 if (!parse_btype(&btype
, &ad
))
4865 expect("declaration list");
4866 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4867 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4869 /* we accept no variable after */
4873 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4874 /* find parameter in function parameter list */
4877 if ((s
->v
& ~SYM_FIELD
) == v
)
4881 error("declaration for parameter '%s' but no such parameter",
4882 get_tok_str(v
, NULL
));
4884 /* check that no storage specifier except 'register' was given */
4885 if (type
.t
& VT_STORAGE
)
4886 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4887 convert_parameter_type(&type
);
4888 /* we can add the type (NOTE: it could be local to the function) */
4890 /* accept other parameters */
4901 /* parse a function defined by symbol 'sym' and generate its code in
4902 'cur_text_section' */
4903 static void gen_function(Sym
*sym
)
4905 int saved_nocode_wanted
= nocode_wanted
;
4907 ind
= cur_text_section
->data_offset
;
4908 /* NOTE: we patch the symbol size later */
4909 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4910 funcname
= get_tok_str(sym
->v
, NULL
);
4912 /* put debug symbol */
4913 if (tcc_state
->do_debug
)
4914 put_func_debug(sym
);
4915 /* push a dummy symbol to enable local sym storage */
4916 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4917 gfunc_prolog(&sym
->type
);
4919 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4922 cur_text_section
->data_offset
= ind
;
4923 label_pop(&global_label_stack
, NULL
);
4924 sym_pop(&local_stack
, NULL
); /* reset local stack */
4925 /* end of function */
4926 /* patch symbol size */
4927 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4929 if (tcc_state
->do_debug
) {
4930 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4932 /* It's better to crash than to generate wrong code */
4933 cur_text_section
= NULL
;
4934 funcname
= ""; /* for safety */
4935 func_vt
.t
= VT_VOID
; /* for safety */
4936 ind
= 0; /* for safety */
4937 nocode_wanted
= saved_nocode_wanted
;
4940 static void gen_inline_functions(void)
4943 int *str
, inline_generated
, i
;
4944 struct InlineFunc
*fn
;
4946 /* iterate while inline function are referenced */
4948 inline_generated
= 0;
4949 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4950 fn
= tcc_state
->inline_fns
[i
];
4952 if (sym
&& sym
->c
) {
4953 /* the function was used: generate its code and
4954 convert it to a normal function */
4955 str
= fn
->token_str
;
4958 strcpy(file
->filename
, fn
->filename
);
4959 sym
->r
= VT_SYM
| VT_CONST
;
4960 sym
->type
.t
&= ~VT_INLINE
;
4964 cur_text_section
= text_section
;
4966 macro_ptr
= NULL
; /* fail safe */
4968 inline_generated
= 1;
4971 if (!inline_generated
)
4974 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4975 fn
= tcc_state
->inline_fns
[i
];
4976 str
= fn
->token_str
;
4979 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
4982 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4983 static void decl(int l
)
4991 if (!parse_btype(&btype
, &ad
)) {
4992 /* skip redundant ';' */
4993 /* XXX: find more elegant solution */
4998 if (l
== VT_CONST
&&
4999 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5000 /* global asm block */
5004 /* special test for old K&R protos without explicit int
5005 type. Only accepted when defining global data */
5006 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5010 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5011 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5013 /* we accept no variable after */
5017 while (1) { /* iterate thru each declaration */
5019 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5023 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5024 printf("type = '%s'\n", buf
);
5027 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5028 /* if old style function prototype, we accept a
5031 if (sym
->c
== FUNC_OLD
)
5032 func_decl_list(sym
);
5037 error("cannot use local functions");
5038 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5039 expect("function definition");
5041 /* reject abstract declarators in function definition */
5043 while ((sym
= sym
->next
) != NULL
)
5044 if (!(sym
->v
& ~SYM_FIELD
))
5045 expect("identifier");
5047 /* XXX: cannot do better now: convert extern line to static inline */
5048 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5049 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5053 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5056 r
= sym
->type
.ref
->r
;
5057 /* use func_call from prototype if not defined */
5058 if (FUNC_CALL(r
) != FUNC_CDECL
5059 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5060 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5062 /* use export from prototype */
5064 FUNC_EXPORT(type
.ref
->r
) = 1;
5066 /* use static from prototype */
5067 if (sym
->type
.t
& VT_STATIC
)
5068 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5070 if (!is_compatible_types(&sym
->type
, &type
)) {
5072 error("incompatible types for redefinition of '%s'",
5073 get_tok_str(v
, NULL
));
5075 /* if symbol is already defined, then put complete type */
5078 /* put function symbol */
5079 sym
= global_identifier_push(v
, type
.t
, 0);
5080 sym
->type
.ref
= type
.ref
;
5083 /* static inline functions are just recorded as a kind
5084 of macro. Their code will be emitted at the end of
5085 the compilation unit only if they are used */
5086 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5087 (VT_INLINE
| VT_STATIC
)) {
5088 TokenString func_str
;
5090 struct InlineFunc
*fn
;
5091 const char *filename
;
5093 tok_str_new(&func_str
);
5099 error("unexpected end of file");
5100 tok_str_add_tok(&func_str
);
5105 } else if (t
== '}') {
5107 if (block_level
== 0)
5111 tok_str_add(&func_str
, -1);
5112 tok_str_add(&func_str
, 0);
5113 filename
= file
? file
->filename
: "";
5114 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5115 strcpy(fn
->filename
, filename
);
5117 fn
->token_str
= func_str
.str
;
5118 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5121 /* compute text section */
5122 cur_text_section
= ad
.section
;
5123 if (!cur_text_section
)
5124 cur_text_section
= text_section
;
5125 sym
->r
= VT_SYM
| VT_CONST
;
5130 if (btype
.t
& VT_TYPEDEF
) {
5131 /* save typedefed type */
5132 /* XXX: test storage specifiers ? */
5133 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5134 sym
->type
.t
|= VT_TYPEDEF
;
5135 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5136 /* external function definition */
5137 /* specific case for func_call attribute */
5138 type
.ref
->r
= INT_ATTR(&ad
);
5139 external_sym(v
, &type
, 0);
5141 /* not lvalue if array */
5143 if (!(type
.t
& VT_ARRAY
))
5144 r
|= lvalue_type(type
.t
);
5145 has_init
= (tok
== '=');
5146 if ((btype
.t
& VT_EXTERN
) ||
5147 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5148 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5149 /* external variable */
5150 /* NOTE: as GCC, uninitialized global static
5151 arrays of null size are considered as
5153 #ifdef TCC_TARGET_PE
5155 type
.t
|= VT_IMPORT
;
5157 external_sym(v
, &type
, r
);
5159 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5160 if (type
.t
& VT_STATIC
)
5166 decl_initializer_alloc(&type
, &ad
, r
,