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 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
455 !(reg_classes
[vtop
->r2
] & rc2
))) {
457 #ifndef TCC_TARGET_X86_64
458 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
460 unsigned long long ll
;
461 /* two register type load : expand to two words
463 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
466 vtop
->c
.ui
= ll
; /* first word */
468 vtop
->r
= r
; /* save register value */
469 vpushi(ll
>> 32); /* second word */
470 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
471 (vtop
->r
& VT_LVAL
)) {
472 /* We do not want to modifier the long long
473 pointer here, so the safest (and less
474 efficient) is to save all the other registers
475 in the stack. XXX: totally inefficient. */
477 /* load from memory */
480 vtop
[-1].r
= r
; /* save register value */
481 /* increment pointer to get second word */
482 vtop
->type
.t
= VT_INT
;
491 vtop
[-1].r
= r
; /* save register value */
492 vtop
->r
= vtop
[-1].r2
;
494 /* allocate second register */
498 /* write second register */
502 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
504 /* lvalue of scalar type : need to use lvalue type
505 because of possible cast */
508 /* compute memory access type */
509 if (vtop
->r
& VT_LVAL_BYTE
)
511 else if (vtop
->r
& VT_LVAL_SHORT
)
513 if (vtop
->r
& VT_LVAL_UNSIGNED
)
517 /* restore wanted type */
520 /* one register type load */
525 #ifdef TCC_TARGET_C67
526 /* uses register pairs for doubles */
527 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
534 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
535 void gv2(int rc1
, int rc2
)
539 /* generate more generic register first. But VT_JMP or VT_CMP
540 values must be generated first in all cases to avoid possible
542 v
= vtop
[0].r
& VT_VALMASK
;
543 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
548 /* test if reload is needed for first register */
549 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
559 /* test if reload is needed for first register */
560 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
566 /* wrapper around RC_FRET to return a register by type */
569 #ifdef TCC_TARGET_X86_64
570 if (t
== VT_LDOUBLE
) {
577 /* wrapper around REG_FRET to return a register by type */
580 #ifdef TCC_TARGET_X86_64
581 if (t
== VT_LDOUBLE
) {
588 /* expand long long on stack in two int registers */
593 u
= vtop
->type
.t
& VT_UNSIGNED
;
596 vtop
[0].r
= vtop
[-1].r2
;
597 vtop
[0].r2
= VT_CONST
;
598 vtop
[-1].r2
= VT_CONST
;
599 vtop
[0].type
.t
= VT_INT
| u
;
600 vtop
[-1].type
.t
= VT_INT
| u
;
603 #ifdef TCC_TARGET_ARM
604 /* expand long long on stack */
605 void lexpand_nr(void)
609 u
= vtop
->type
.t
& VT_UNSIGNED
;
612 vtop
->type
.t
= VT_INT
| u
;
613 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
615 vtop
[-1].c
.ui
= vtop
->c
.ull
;
616 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
618 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
620 vtop
->r
= vtop
[-1].r
;
621 } else if (v
> VT_CONST
) {
625 vtop
->r
= vtop
[-1].r2
;
626 vtop
[-1].r2
= VT_CONST
;
627 vtop
[-1].type
.t
= VT_INT
| u
;
631 /* build a long long from two ints */
635 vtop
[-1].r2
= vtop
[0].r
;
640 /* rotate n first stack elements to the bottom
641 I1 ... In -> I2 ... In I1 [top is right]
654 /* rotate n first stack elements to the top
655 I1 ... In -> In I1 ... I(n-1) [top is right]
663 for(i
= 0;i
< n
- 1; i
++)
664 vtop
[-i
] = vtop
[-i
- 1];
668 #ifdef TCC_TARGET_ARM
669 /* like vrott but in other direction
670 In ... I1 -> I(n-1) ... I1 In [top is right]
678 for(i
= n
- 1; i
> 0; i
--)
679 vtop
[-i
] = vtop
[-i
+ 1];
684 /* pop stack value */
688 v
= vtop
->r
& VT_VALMASK
;
689 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
690 /* for x86, we need to pop the FP stack */
691 if (v
== TREG_ST0
&& !nocode_wanted
) {
692 o(0xd8dd); /* fstp %st(0) */
695 if (v
== VT_JMP
|| v
== VT_JMPI
) {
696 /* need to put correct jump if && or || without test */
702 /* convert stack entry to register and duplicate its value in another
710 if ((t
& VT_BTYPE
) == VT_LLONG
) {
717 /* stack: H L L1 H1 */
725 /* duplicate value */
730 #ifdef TCC_TARGET_X86_64
731 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
741 load(r1
, &sv
); /* move r to r1 */
743 /* duplicates value */
749 #ifndef TCC_TARGET_X86_64
750 /* generate CPU independent (unsigned) long long operations */
753 int t
, a
, b
, op1
, c
, i
;
755 unsigned short reg_iret
= REG_IRET
;
756 unsigned short reg_lret
= REG_LRET
;
765 func
= TOK___udivdi3
;
771 func
= TOK___umoddi3
;
778 /* call generic long long function */
779 vpush_global_sym(&func_old_type
, func
);
797 /* stack: L1 H1 L2 H2 */
805 /* stack: H1 H2 L1 L2 */
811 /* stack: H1 H2 L1 L2 ML MH */
814 /* stack: ML MH H1 H2 L1 L2 */
818 /* stack: ML MH H1 L2 H2 L1 */
823 /* stack: ML MH M1 M2 */
826 } else if (op
== '+' || op
== '-') {
827 /* XXX: add non carry method too (for MIPS or alpha) */
833 /* stack: H1 H2 (L1 op L2) */
836 gen_op(op1
+ 1); /* TOK_xxxC2 */
839 /* stack: H1 H2 (L1 op L2) */
842 /* stack: (L1 op L2) H1 H2 */
844 /* stack: (L1 op L2) (H1 op H2) */
852 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
857 /* stack: L H shift */
859 /* constant: simpler */
860 /* NOTE: all comments are for SHL. the other cases are
861 done by swaping words */
905 /* XXX: should provide a faster fallback on x86 ? */
908 func
= TOK___ashrdi3
;
911 func
= TOK___lshrdi3
;
914 func
= TOK___ashldi3
;
920 /* compare operations */
926 /* stack: L1 H1 L2 H2 */
930 /* stack: L1 L2 H1 H2 */
933 /* when values are equal, we need to compare low words. since
934 the jump is inverted, we invert the test too. */
937 else if (op1
== TOK_GT
)
939 else if (op1
== TOK_ULT
)
941 else if (op1
== TOK_UGT
)
950 /* generate non equal test */
951 /* XXX: NOT PORTABLE yet */
955 #if defined(TCC_TARGET_I386)
957 #elif defined(TCC_TARGET_ARM)
959 o(0x1A000000 | encbranch(ind
, 0, 1));
960 #elif defined(TCC_TARGET_C67)
961 error("not implemented");
967 /* compare low. Always unsigned */
971 else if (op1
== TOK_LE
)
973 else if (op1
== TOK_GT
)
975 else if (op1
== TOK_GE
)
986 /* handle integer constant optimizations and various machine
988 void gen_opic(int op
)
990 int c1
, c2
, t1
, t2
, n
;
993 typedef unsigned long long U
;
997 t1
= v1
->type
.t
& VT_BTYPE
;
998 t2
= v2
->type
.t
& VT_BTYPE
;
1002 else if (v1
->type
.t
& VT_UNSIGNED
)
1009 else if (v2
->type
.t
& VT_UNSIGNED
)
1014 /* currently, we cannot do computations with forward symbols */
1015 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1016 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1019 case '+': l1
+= l2
; break;
1020 case '-': l1
-= l2
; break;
1021 case '&': l1
&= l2
; break;
1022 case '^': l1
^= l2
; break;
1023 case '|': l1
|= l2
; break;
1024 case '*': l1
*= l2
; break;
1031 /* if division by zero, generate explicit division */
1034 error("division by zero in constant");
1038 default: l1
/= l2
; break;
1039 case '%': l1
%= l2
; break;
1040 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1041 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1044 case TOK_SHL
: l1
<<= l2
; break;
1045 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1046 case TOK_SAR
: l1
>>= l2
; break;
1048 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1049 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1050 case TOK_EQ
: l1
= l1
== l2
; break;
1051 case TOK_NE
: l1
= l1
!= l2
; break;
1052 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1053 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1054 case TOK_LT
: l1
= l1
< l2
; break;
1055 case TOK_GE
: l1
= l1
>= l2
; break;
1056 case TOK_LE
: l1
= l1
<= l2
; break;
1057 case TOK_GT
: l1
= l1
> l2
; break;
1059 case TOK_LAND
: l1
= l1
&& l2
; break;
1060 case TOK_LOR
: l1
= l1
|| l2
; break;
1067 /* if commutative ops, put c2 as constant */
1068 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1069 op
== '|' || op
== '*')) {
1071 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1072 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1074 /* Filter out NOP operations like x*1, x-0, x&-1... */
1075 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1078 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1079 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1085 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1086 /* try to use shifts instead of muls or divs */
1087 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1096 else if (op
== TOK_PDIV
)
1102 } else if (c2
&& (op
== '+' || op
== '-') &&
1103 ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
1104 (VT_CONST
| VT_SYM
) ||
1105 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1106 /* symbol + constant case */
1113 if (!nocode_wanted
) {
1114 /* call low level op generator */
1115 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1126 /* generate a floating point operation with constant propagation */
1127 void gen_opif(int op
)
1135 /* currently, we cannot do computations with forward symbols */
1136 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1137 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1139 if (v1
->type
.t
== VT_FLOAT
) {
1142 } else if (v1
->type
.t
== VT_DOUBLE
) {
1150 /* NOTE: we only do constant propagation if finite number (not
1151 NaN or infinity) (ANSI spec) */
1152 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1156 case '+': f1
+= f2
; break;
1157 case '-': f1
-= f2
; break;
1158 case '*': f1
*= f2
; break;
1162 error("division by zero in constant");
1167 /* XXX: also handles tests ? */
1171 /* XXX: overflow test ? */
1172 if (v1
->type
.t
== VT_FLOAT
) {
1174 } else if (v1
->type
.t
== VT_DOUBLE
) {
1182 if (!nocode_wanted
) {
1190 static int pointed_size(CType
*type
)
1193 return type_size(pointed_type(type
), &align
);
1196 static inline int is_null_pointer(SValue
*p
)
1198 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1200 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1201 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1204 static inline int is_integer_btype(int bt
)
1206 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1207 bt
== VT_INT
|| bt
== VT_LLONG
);
1210 /* check types for comparison or substraction of pointers */
1211 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1213 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1216 /* null pointers are accepted for all comparisons as gcc */
1217 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1221 bt1
= type1
->t
& VT_BTYPE
;
1222 bt2
= type2
->t
& VT_BTYPE
;
1223 /* accept comparison between pointer and integer with a warning */
1224 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1225 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1226 warning("comparison between pointer and integer");
1230 /* both must be pointers or implicit function pointers */
1231 if (bt1
== VT_PTR
) {
1232 type1
= pointed_type(type1
);
1233 } else if (bt1
!= VT_FUNC
)
1234 goto invalid_operands
;
1236 if (bt2
== VT_PTR
) {
1237 type2
= pointed_type(type2
);
1238 } else if (bt2
!= VT_FUNC
) {
1240 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1242 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1243 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1247 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1248 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1249 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1250 /* gcc-like error if '-' is used */
1252 goto invalid_operands
;
1254 warning("comparison of distinct pointer types lacks a cast");
1258 /* generic gen_op: handles types problems */
1261 int u
, t1
, t2
, bt1
, bt2
, t
;
1264 t1
= vtop
[-1].type
.t
;
1265 t2
= vtop
[0].type
.t
;
1266 bt1
= t1
& VT_BTYPE
;
1267 bt2
= t2
& VT_BTYPE
;
1269 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1270 /* at least one operand is a pointer */
1271 /* relationnal op: must be both pointers */
1272 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1273 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1274 /* pointers are handled are unsigned */
1275 #ifdef TCC_TARGET_X86_64
1276 t
= VT_LLONG
| VT_UNSIGNED
;
1278 t
= VT_INT
| VT_UNSIGNED
;
1282 /* if both pointers, then it must be the '-' op */
1283 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1285 error("cannot use pointers here");
1286 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1287 /* XXX: check that types are compatible */
1288 u
= pointed_size(&vtop
[-1].type
);
1290 /* set to integer type */
1291 #ifdef TCC_TARGET_X86_64
1292 vtop
->type
.t
= VT_LLONG
;
1294 vtop
->type
.t
= VT_INT
;
1299 /* exactly one pointer : must be '+' or '-'. */
1300 if (op
!= '-' && op
!= '+')
1301 error("cannot use pointers here");
1302 /* Put pointer as first operand */
1303 if (bt2
== VT_PTR
) {
1307 type1
= vtop
[-1].type
;
1308 #ifdef TCC_TARGET_X86_64
1309 vpushll(pointed_size(&vtop
[-1].type
));
1311 /* XXX: cast to int ? (long long case) */
1312 vpushi(pointed_size(&vtop
[-1].type
));
1315 #ifdef CONFIG_TCC_BCHECK
1316 /* if evaluating constant expression, no code should be
1317 generated, so no bound check */
1318 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1319 /* if bounded pointers, we generate a special code to
1326 gen_bounded_ptr_add();
1332 /* put again type if gen_opic() swaped operands */
1335 } else if (is_float(bt1
) || is_float(bt2
)) {
1336 /* compute bigger type and do implicit casts */
1337 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1339 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1344 /* floats can only be used for a few operations */
1345 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1346 (op
< TOK_ULT
|| op
> TOK_GT
))
1347 error("invalid operands for binary operation");
1349 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1350 /* cast to biggest op */
1352 /* convert to unsigned if it does not fit in a long long */
1353 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1354 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1358 /* integer operations */
1360 /* convert to unsigned if it does not fit in an integer */
1361 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1362 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1365 /* XXX: currently, some unsigned operations are explicit, so
1366 we modify them here */
1367 if (t
& VT_UNSIGNED
) {
1374 else if (op
== TOK_LT
)
1376 else if (op
== TOK_GT
)
1378 else if (op
== TOK_LE
)
1380 else if (op
== TOK_GE
)
1387 /* special case for shifts and long long: we keep the shift as
1389 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1396 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1397 /* relationnal op: the result is an int */
1398 vtop
->type
.t
= VT_INT
;
1405 #ifndef TCC_TARGET_ARM
1406 /* generic itof for unsigned long long case */
1407 void gen_cvt_itof1(int t
)
1409 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1410 (VT_LLONG
| VT_UNSIGNED
)) {
1413 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1414 #if LDOUBLE_SIZE != 8
1415 else if (t
== VT_LDOUBLE
)
1416 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1419 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1423 vtop
->r
= reg_fret(t
);
1430 /* generic ftoi for unsigned long long case */
1431 void gen_cvt_ftoi1(int t
)
1435 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1436 /* not handled natively */
1437 st
= vtop
->type
.t
& VT_BTYPE
;
1439 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1440 #if LDOUBLE_SIZE != 8
1441 else if (st
== VT_LDOUBLE
)
1442 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1445 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1450 vtop
->r2
= REG_LRET
;
1456 /* force char or short cast */
1457 void force_charshort_cast(int t
)
1461 /* XXX: add optimization if lvalue : just change type and offset */
1466 if (t
& VT_UNSIGNED
) {
1467 vpushi((1 << bits
) - 1);
1473 /* result must be signed or the SAR is converted to an SHL
1474 This was not the case when "t" was a signed short
1475 and the last value on the stack was an unsigned int */
1476 vtop
->type
.t
&= ~VT_UNSIGNED
;
1482 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1483 static void gen_cast(CType
*type
)
1485 int sbt
, dbt
, sf
, df
, c
, p
;
1487 /* special delayed cast for char/short */
1488 /* XXX: in some cases (multiple cascaded casts), it may still
1490 if (vtop
->r
& VT_MUSTCAST
) {
1491 vtop
->r
&= ~VT_MUSTCAST
;
1492 force_charshort_cast(vtop
->type
.t
);
1495 /* bitfields first get cast to ints */
1496 if (vtop
->type
.t
& VT_BITFIELD
) {
1500 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1501 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1506 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1507 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1509 /* constant case: we can do it now */
1510 /* XXX: in ISOC, cannot do it if error in convert */
1511 if (sbt
== VT_FLOAT
)
1512 vtop
->c
.ld
= vtop
->c
.f
;
1513 else if (sbt
== VT_DOUBLE
)
1514 vtop
->c
.ld
= vtop
->c
.d
;
1517 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1518 if (sbt
& VT_UNSIGNED
)
1519 vtop
->c
.ld
= vtop
->c
.ull
;
1521 vtop
->c
.ld
= vtop
->c
.ll
;
1523 if (sbt
& VT_UNSIGNED
)
1524 vtop
->c
.ld
= vtop
->c
.ui
;
1526 vtop
->c
.ld
= vtop
->c
.i
;
1529 if (dbt
== VT_FLOAT
)
1530 vtop
->c
.f
= (float)vtop
->c
.ld
;
1531 else if (dbt
== VT_DOUBLE
)
1532 vtop
->c
.d
= (double)vtop
->c
.ld
;
1533 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1534 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1535 } else if (sf
&& dbt
== VT_BOOL
) {
1536 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1539 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1540 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1541 vtop
->c
.ll
= vtop
->c
.ull
;
1542 else if (sbt
& VT_UNSIGNED
)
1543 vtop
->c
.ll
= vtop
->c
.ui
;
1544 else if (sbt
!= VT_LLONG
)
1545 vtop
->c
.ll
= vtop
->c
.i
;
1547 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1548 vtop
->c
.ull
= vtop
->c
.ll
;
1549 else if (dbt
== VT_BOOL
)
1550 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1551 else if (dbt
!= VT_LLONG
) {
1553 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1555 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1558 if(dbt
& VT_UNSIGNED
)
1559 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1561 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1564 } else if (p
&& dbt
== VT_BOOL
) {
1567 } else if (!nocode_wanted
) {
1568 /* non constant case: generate code */
1570 /* convert from fp to fp */
1573 /* convert int to fp */
1576 /* convert fp to int */
1577 if (dbt
== VT_BOOL
) {
1581 /* we handle char/short/etc... with generic code */
1582 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1583 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1587 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1588 /* additional cast for char/short... */
1593 #ifndef TCC_TARGET_X86_64
1594 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1595 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1596 /* scalar to long long */
1597 /* machine independent conversion */
1599 /* generate high word */
1600 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1604 if (sbt
== VT_PTR
) {
1605 /* cast from pointer to int before we apply
1606 shift operation, which pointers don't support*/
1607 gen_cast(&int_type
);
1613 /* patch second register */
1614 vtop
[-1].r2
= vtop
->r
;
1618 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1619 (dbt
& VT_BTYPE
) == VT_PTR
) {
1620 /* XXX: not sure if this is perfect... need more tests */
1621 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1623 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1624 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1625 /* x86_64 specific: movslq */
1627 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1631 } else if (dbt
== VT_BOOL
) {
1632 /* scalar to bool */
1635 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1636 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1637 if (sbt
== VT_PTR
) {
1638 vtop
->type
.t
= VT_INT
;
1639 warning("nonportable conversion from pointer to char/short");
1641 force_charshort_cast(dbt
);
1642 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1644 if (sbt
== VT_LLONG
) {
1645 /* from long long: just take low order word */
1649 /* if lvalue and single word type, nothing to do because
1650 the lvalue already contains the real type size (see
1651 VT_LVAL_xxx constants) */
1654 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1655 /* if we are casting between pointer types,
1656 we must update the VT_LVAL_xxx size */
1657 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1658 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1663 /* return type size. Put alignment at 'a' */
1664 static int type_size(CType
*type
, int *a
)
1669 bt
= type
->t
& VT_BTYPE
;
1670 if (bt
== VT_STRUCT
) {
1675 } else if (bt
== VT_PTR
) {
1676 if (type
->t
& VT_ARRAY
) {
1680 ts
= type_size(&s
->type
, a
);
1682 if (ts
< 0 && s
->c
< 0)
1690 } else if (bt
== VT_LDOUBLE
) {
1692 return LDOUBLE_SIZE
;
1693 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1694 #ifdef TCC_TARGET_I386
1695 #ifdef TCC_TARGET_PE
1700 #elif defined(TCC_TARGET_ARM)
1710 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1713 } else if (bt
== VT_SHORT
) {
1717 /* char, void, function, _Bool */
1723 /* return the pointed type of t */
1724 static inline CType
*pointed_type(CType
*type
)
1726 return &type
->ref
->type
;
1729 /* modify type so that its it is a pointer to type. */
1730 static void mk_pointer(CType
*type
)
1733 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1734 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1738 /* compare function types. OLD functions match any new functions */
1739 static int is_compatible_func(CType
*type1
, CType
*type2
)
1745 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1747 /* check func_call */
1748 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1750 /* XXX: not complete */
1751 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
1755 while (s1
!= NULL
) {
1758 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
1768 /* return true if type1 and type2 are the same. If unqualified is
1769 true, qualifiers on the types are ignored.
1771 - enums are not checked as gcc __builtin_types_compatible_p ()
1773 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
1777 t1
= type1
->t
& VT_TYPE
;
1778 t2
= type2
->t
& VT_TYPE
;
1780 /* strip qualifiers before comparing */
1781 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1782 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1784 /* XXX: bitfields ? */
1787 /* test more complicated cases */
1788 bt1
= t1
& VT_BTYPE
;
1789 if (bt1
== VT_PTR
) {
1790 type1
= pointed_type(type1
);
1791 type2
= pointed_type(type2
);
1792 return is_compatible_types(type1
, type2
);
1793 } else if (bt1
== VT_STRUCT
) {
1794 return (type1
->ref
== type2
->ref
);
1795 } else if (bt1
== VT_FUNC
) {
1796 return is_compatible_func(type1
, type2
);
1802 /* return true if type1 and type2 are exactly the same (including
1805 static int is_compatible_types(CType
*type1
, CType
*type2
)
1807 return compare_types(type1
,type2
,0);
1810 /* return true if type1 and type2 are the same (ignoring qualifiers).
1812 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
1814 return compare_types(type1
,type2
,1);
1817 /* print a type. If 'varstr' is not NULL, then the variable is also
1818 printed in the type */
1820 /* XXX: add array and function pointers */
1821 void type_to_str(char *buf
, int buf_size
,
1822 CType
*type
, const char *varstr
)
1829 t
= type
->t
& VT_TYPE
;
1832 if (t
& VT_CONSTANT
)
1833 pstrcat(buf
, buf_size
, "const ");
1834 if (t
& VT_VOLATILE
)
1835 pstrcat(buf
, buf_size
, "volatile ");
1836 if (t
& VT_UNSIGNED
)
1837 pstrcat(buf
, buf_size
, "unsigned ");
1867 tstr
= "long double";
1869 pstrcat(buf
, buf_size
, tstr
);
1873 if (bt
== VT_STRUCT
)
1877 pstrcat(buf
, buf_size
, tstr
);
1878 v
= type
->ref
->v
& ~SYM_STRUCT
;
1879 if (v
>= SYM_FIRST_ANOM
)
1880 pstrcat(buf
, buf_size
, "<anonymous>");
1882 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
1886 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
1887 pstrcat(buf
, buf_size
, "(");
1889 while (sa
!= NULL
) {
1890 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
1891 pstrcat(buf
, buf_size
, buf1
);
1894 pstrcat(buf
, buf_size
, ", ");
1896 pstrcat(buf
, buf_size
, ")");
1900 pstrcpy(buf1
, sizeof(buf1
), "*");
1902 pstrcat(buf1
, sizeof(buf1
), varstr
);
1903 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
1907 pstrcat(buf
, buf_size
, " ");
1908 pstrcat(buf
, buf_size
, varstr
);
1913 /* verify type compatibility to store vtop in 'dt' type, and generate
1915 static void gen_assign_cast(CType
*dt
)
1917 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
1918 char buf1
[256], buf2
[256];
1921 st
= &vtop
->type
; /* source type */
1922 dbt
= dt
->t
& VT_BTYPE
;
1923 sbt
= st
->t
& VT_BTYPE
;
1924 if (dt
->t
& VT_CONSTANT
)
1925 warning("assignment of read-only location");
1928 /* special cases for pointers */
1929 /* '0' can also be a pointer */
1930 if (is_null_pointer(vtop
))
1932 /* accept implicit pointer to integer cast with warning */
1933 if (is_integer_btype(sbt
)) {
1934 warning("assignment makes pointer from integer without a cast");
1937 type1
= pointed_type(dt
);
1938 /* a function is implicitely a function pointer */
1939 if (sbt
== VT_FUNC
) {
1940 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
1941 !is_compatible_types(pointed_type(dt
), st
))
1942 warning("assignment from incompatible pointer type");
1947 type2
= pointed_type(st
);
1948 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1949 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
1950 /* void * can match anything */
1952 /* exact type match, except for unsigned */
1955 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1956 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1957 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
1958 warning("assignment from incompatible pointer type");
1960 /* check const and volatile */
1961 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
1962 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
1963 warning("assignment discards qualifiers from pointer target type");
1969 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
1970 warning("assignment makes integer from pointer without a cast");
1972 /* XXX: more tests */
1977 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1978 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1979 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1981 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
1982 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
1983 error("cannot cast '%s' to '%s'", buf1
, buf2
);
1991 /* store vtop in lvalue pushed on stack */
1994 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
1996 ft
= vtop
[-1].type
.t
;
1997 sbt
= vtop
->type
.t
& VT_BTYPE
;
1998 dbt
= ft
& VT_BTYPE
;
1999 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2000 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2001 /* optimize char/short casts */
2002 delayed_cast
= VT_MUSTCAST
;
2003 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2004 /* XXX: factorize */
2005 if (ft
& VT_CONSTANT
)
2006 warning("assignment of read-only location");
2009 if (!(ft
& VT_BITFIELD
))
2010 gen_assign_cast(&vtop
[-1].type
);
2013 if (sbt
== VT_STRUCT
) {
2014 /* if structure, only generate pointer */
2015 /* structure assignment : generate memcpy */
2016 /* XXX: optimize if small size */
2017 if (!nocode_wanted
) {
2018 size
= type_size(&vtop
->type
, &align
);
2022 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2023 else if(!(align
& 3))
2024 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2027 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2031 vtop
->type
.t
= VT_PTR
;
2035 vtop
->type
.t
= VT_PTR
;
2047 /* leave source on stack */
2048 } else if (ft
& VT_BITFIELD
) {
2049 /* bitfield store handling */
2050 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2051 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2052 /* remove bit field info to avoid loops */
2053 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2055 /* duplicate source into other register */
2060 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2061 gen_cast(&vtop
[-1].type
);
2062 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2065 /* duplicate destination */
2067 vtop
[-1] = vtop
[-2];
2069 /* mask and shift source */
2070 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2071 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2072 vpushll((1ULL << bit_size
) - 1ULL);
2074 vpushi((1 << bit_size
) - 1);
2080 /* load destination, mask and or with source */
2082 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2083 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2085 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2092 /* pop off shifted source from "duplicate source..." above */
2096 #ifdef CONFIG_TCC_BCHECK
2097 /* bound check case */
2098 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2104 if (!nocode_wanted
) {
2108 #ifdef TCC_TARGET_X86_64
2109 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2114 r
= gv(rc
); /* generate value */
2115 /* if lvalue was saved on stack, must read it */
2116 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2118 t
= get_reg(RC_INT
);
2119 #ifdef TCC_TARGET_X86_64
2124 sv
.r
= VT_LOCAL
| VT_LVAL
;
2125 sv
.c
.ul
= vtop
[-1].c
.ul
;
2127 vtop
[-1].r
= t
| VT_LVAL
;
2130 #ifndef TCC_TARGET_X86_64
2131 /* two word case handling : store second register at word + 4 */
2132 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2134 /* convert to int to increment easily */
2135 vtop
->type
.t
= VT_INT
;
2141 /* XXX: it works because r2 is spilled last ! */
2142 store(vtop
->r2
, vtop
- 1);
2147 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2148 vtop
->r
|= delayed_cast
;
2152 /* post defines POST/PRE add. c is the token ++ or -- */
2153 void inc(int post
, int c
)
2156 vdup(); /* save lvalue */
2158 gv_dup(); /* duplicate value */
2163 vpushi(c
- TOK_MID
);
2165 vstore(); /* store value */
2167 vpop(); /* if post op, return saved value */
2170 /* Parse GNUC __attribute__ extension. Currently, the following
2171 extensions are recognized:
2172 - aligned(n) : set data/function alignment.
2173 - packed : force data alignment to 1
2174 - section(x) : generate data/code in this section.
2175 - unused : currently ignored, but may be used someday.
2176 - regparm(n) : pass function parameters in registers (i386 only)
2178 static void parse_attribute(AttributeDef
*ad
)
2182 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2186 while (tok
!= ')') {
2187 if (tok
< TOK_IDENT
)
2188 expect("attribute name");
2196 expect("section name");
2197 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2206 if (n
<= 0 || (n
& (n
- 1)) != 0)
2207 error("alignment must be a positive power of two");
2220 /* currently, no need to handle it because tcc does not
2221 track unused objects */
2225 /* currently, no need to handle it because tcc does not
2226 track unused objects */
2231 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
2236 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
2238 #ifdef TCC_TARGET_I386
2248 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
2254 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
2258 FUNC_EXPORT(ad
->func_attr
) = 1;
2261 if (tcc_state
->warn_unsupported
)
2262 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2263 /* skip parameters */
2265 int parenthesis
= 0;
2269 else if (tok
== ')')
2272 } while (parenthesis
&& tok
!= -1);
2285 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2286 static void struct_decl(CType
*type
, int u
)
2288 int a
, v
, size
, align
, maxalign
, c
, offset
;
2289 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2290 Sym
*s
, *ss
, *ass
, **ps
;
2294 a
= tok
; /* save decl type */
2299 /* struct already defined ? return it */
2301 expect("struct/union/enum name");
2305 error("invalid type");
2312 /* we put an undefined size for struct/union */
2313 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2314 s
->r
= 0; /* default alignment is zero as gcc */
2315 /* put struct/union/enum name in type */
2323 error("struct/union/enum already defined");
2324 /* cannot be empty */
2326 /* non empty enums are not allowed */
2327 if (a
== TOK_ENUM
) {
2331 expect("identifier");
2337 /* enum symbols have static storage */
2338 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2339 ss
->type
.t
|= VT_STATIC
;
2344 /* NOTE: we accept a trailing comma */
2355 while (tok
!= '}') {
2356 parse_btype(&btype
, &ad
);
2362 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2363 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2364 expect("identifier");
2365 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2366 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2367 error("invalid type for '%s'",
2368 get_tok_str(v
, NULL
));
2372 bit_size
= expr_const();
2373 /* XXX: handle v = 0 case for messages */
2375 error("negative width in bit-field '%s'",
2376 get_tok_str(v
, NULL
));
2377 if (v
&& bit_size
== 0)
2378 error("zero width for bit-field '%s'",
2379 get_tok_str(v
, NULL
));
2381 size
= type_size(&type1
, &align
);
2383 if (align
< ad
.aligned
)
2385 } else if (ad
.packed
) {
2387 } else if (*tcc_state
->pack_stack_ptr
) {
2388 if (align
> *tcc_state
->pack_stack_ptr
)
2389 align
= *tcc_state
->pack_stack_ptr
;
2392 if (bit_size
>= 0) {
2393 bt
= type1
.t
& VT_BTYPE
;
2400 error("bitfields must have scalar type");
2402 if (bit_size
> bsize
) {
2403 error("width of '%s' exceeds its type",
2404 get_tok_str(v
, NULL
));
2405 } else if (bit_size
== bsize
) {
2406 /* no need for bit fields */
2408 } else if (bit_size
== 0) {
2409 /* XXX: what to do if only padding in a
2411 /* zero size: means to pad */
2414 /* we do not have enough room ?
2415 did the type change?
2417 if ((bit_pos
+ bit_size
) > bsize
||
2418 bt
!= prevbt
|| a
== TOK_UNION
)
2421 /* XXX: handle LSB first */
2422 type1
.t
|= VT_BITFIELD
|
2423 (bit_pos
<< VT_STRUCT_SHIFT
) |
2424 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2425 bit_pos
+= bit_size
;
2431 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2432 /* add new memory data only if starting
2434 if (lbit_pos
== 0) {
2435 if (a
== TOK_STRUCT
) {
2436 c
= (c
+ align
- 1) & -align
;
2445 if (align
> maxalign
)
2449 printf("add field %s offset=%d",
2450 get_tok_str(v
, NULL
), offset
);
2451 if (type1
.t
& VT_BITFIELD
) {
2452 printf(" pos=%d size=%d",
2453 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2454 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2459 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2461 while ((ass
= ass
->next
) != NULL
) {
2462 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2467 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2471 if (tok
== ';' || tok
== TOK_EOF
)
2478 /* store size and alignment */
2479 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2485 /* return 0 if no type declaration. otherwise, return the basic type
2488 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2490 int t
, u
, type_found
, typespec_found
, typedef_found
;
2494 memset(ad
, 0, sizeof(AttributeDef
));
2502 /* currently, we really ignore extension */
2512 if ((t
& VT_BTYPE
) != 0)
2513 error("too many basic types");
2529 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2530 #ifndef TCC_TARGET_PE
2531 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2533 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2534 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2548 if ((t
& VT_BTYPE
) == VT_LONG
) {
2549 #ifdef TCC_TARGET_PE
2550 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2552 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2560 struct_decl(&type1
, VT_ENUM
);
2563 type
->ref
= type1
.ref
;
2567 struct_decl(&type1
, VT_STRUCT
);
2570 /* type modifiers */
2623 /* GNUC attribute */
2624 case TOK_ATTRIBUTE1
:
2625 case TOK_ATTRIBUTE2
:
2626 parse_attribute(ad
);
2633 parse_expr_type(&type1
);
2636 if (typespec_found
|| typedef_found
)
2639 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2642 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2643 type
->ref
= s
->type
.ref
;
2651 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2652 error("signed and unsigned modifier");
2653 if (tcc_state
->char_is_unsigned
) {
2654 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2659 /* long is never used as type */
2660 if ((t
& VT_BTYPE
) == VT_LONG
)
2661 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2662 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2664 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2670 /* convert a function parameter type (array to pointer and function to
2671 function pointer) */
2672 static inline void convert_parameter_type(CType
*pt
)
2674 /* remove const and volatile qualifiers (XXX: const could be used
2675 to indicate a const function parameter */
2676 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2677 /* array must be transformed to pointer according to ANSI C */
2679 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2684 static void post_type(CType
*type
, AttributeDef
*ad
)
2686 int n
, l
, t1
, arg_size
, align
;
2687 Sym
**plast
, *s
, *first
;
2692 /* function declaration */
2700 /* read param name and compute offset */
2701 if (l
!= FUNC_OLD
) {
2702 if (!parse_btype(&pt
, &ad1
)) {
2704 error("invalid type");
2711 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2713 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2714 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2715 error("parameter declared as void");
2716 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
2721 expect("identifier");
2725 convert_parameter_type(&pt
);
2726 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
2732 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
2739 /* if no parameters, then old type prototype */
2743 t1
= type
->t
& VT_STORAGE
;
2744 /* NOTE: const is ignored in returned type as it has a special
2745 meaning in gcc / C++ */
2746 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
2747 post_type(type
, ad
);
2748 /* we push a anonymous symbol which will contain the function prototype */
2749 FUNC_ARGS(ad
->func_attr
) = arg_size
;
2750 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
2752 type
->t
= t1
| VT_FUNC
;
2754 } else if (tok
== '[') {
2755 /* array definition */
2757 if (tok
== TOK_RESTRICT1
)
2763 error("invalid array size");
2766 /* parse next post type */
2767 t1
= type
->t
& VT_STORAGE
;
2768 type
->t
&= ~VT_STORAGE
;
2769 post_type(type
, ad
);
2771 /* we push a anonymous symbol which will contain the array
2773 s
= sym_push(SYM_FIELD
, type
, 0, n
);
2774 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
2779 /* Parse a type declaration (except basic type), and return the type
2780 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2781 expected. 'type' should contain the basic type. 'ad' is the
2782 attribute definition of the basic type. It can be modified by
2785 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
2788 CType type1
, *type2
;
2791 while (tok
== '*') {
2799 qualifiers
|= VT_CONSTANT
;
2804 qualifiers
|= VT_VOLATILE
;
2812 type
->t
|= qualifiers
;
2815 /* XXX: clarify attribute handling */
2816 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2817 parse_attribute(ad
);
2819 /* recursive type */
2820 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2821 type1
.t
= 0; /* XXX: same as int */
2824 /* XXX: this is not correct to modify 'ad' at this point, but
2825 the syntax is not clear */
2826 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2827 parse_attribute(ad
);
2828 type_decl(&type1
, ad
, v
, td
);
2831 /* type identifier */
2832 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
2836 if (!(td
& TYPE_ABSTRACT
))
2837 expect("identifier");
2841 post_type(type
, ad
);
2842 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2843 parse_attribute(ad
);
2846 /* append type at the end of type1 */
2859 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2860 static int lvalue_type(int t
)
2865 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
2867 else if (bt
== VT_SHORT
)
2871 if (t
& VT_UNSIGNED
)
2872 r
|= VT_LVAL_UNSIGNED
;
2876 /* indirection with full error checking and bound check */
2877 static void indir(void)
2879 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
2880 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
2884 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
2886 vtop
->type
= *pointed_type(&vtop
->type
);
2887 /* Arrays and functions are never lvalues */
2888 if (!(vtop
->type
.t
& VT_ARRAY
)
2889 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
2890 vtop
->r
|= lvalue_type(vtop
->type
.t
);
2891 /* if bound checking, the referenced pointer must be checked */
2892 if (tcc_state
->do_bounds_check
)
2893 vtop
->r
|= VT_MUSTBOUND
;
2897 /* pass a parameter to a function and do type checking and casting */
2898 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2903 func_type
= func
->c
;
2904 if (func_type
== FUNC_OLD
||
2905 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2906 /* default casting : only need to convert float to double */
2907 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2911 } else if (arg
== NULL
) {
2912 error("too many arguments to function");
2915 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2916 gen_assign_cast(&type
);
2920 /* parse an expression of the form '(type)' or '(expr)' and return its
2922 static void parse_expr_type(CType
*type
)
2928 if (parse_btype(type
, &ad
)) {
2929 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2936 static void parse_type(CType
*type
)
2941 if (!parse_btype(type
, &ad
)) {
2944 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2947 static void vpush_tokc(int t
)
2951 vsetc(&type
, VT_CONST
, &tokc
);
2954 static void unary(void)
2956 int n
, t
, align
, size
, r
;
2961 /* XXX: GCC 2.95.3 does not generate a table although it should be
2975 vpush_tokc(VT_INT
| VT_UNSIGNED
);
2979 vpush_tokc(VT_LLONG
);
2983 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
2987 vpush_tokc(VT_FLOAT
);
2991 vpush_tokc(VT_DOUBLE
);
2995 vpush_tokc(VT_LDOUBLE
);
2998 case TOK___FUNCTION__
:
3000 goto tok_identifier
;
3006 /* special function name identifier */
3007 len
= strlen(funcname
) + 1;
3008 /* generate char[len] type */
3013 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3014 ptr
= section_ptr_add(data_section
, len
);
3015 memcpy(ptr
, funcname
, len
);
3020 #ifdef TCC_TARGET_PE
3021 t
= VT_SHORT
| VT_UNSIGNED
;
3027 /* string parsing */
3030 if (tcc_state
->warn_write_strings
)
3035 memset(&ad
, 0, sizeof(AttributeDef
));
3036 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3041 if (parse_btype(&type
, &ad
)) {
3042 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3044 /* check ISOC99 compound literal */
3046 /* data is allocated locally by default */
3051 /* all except arrays are lvalues */
3052 if (!(type
.t
& VT_ARRAY
))
3053 r
|= lvalue_type(type
.t
);
3054 memset(&ad
, 0, sizeof(AttributeDef
));
3055 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3060 } else if (tok
== '{') {
3061 /* save all registers */
3063 /* statement expression : we do not accept break/continue
3064 inside as GCC does */
3065 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3080 /* functions names must be treated as function pointers,
3081 except for unary '&' and sizeof. Since we consider that
3082 functions are not lvalues, we only have to handle it
3083 there and in function calls. */
3084 /* arrays can also be used although they are not lvalues */
3085 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3086 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3088 mk_pointer(&vtop
->type
);
3094 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3096 boolean
.t
= VT_BOOL
;
3098 vtop
->c
.i
= !vtop
->c
.i
;
3099 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3100 vtop
->c
.i
= vtop
->c
.i
^ 1;
3103 vseti(VT_JMP
, gtst(1, 0));
3114 /* in order to force cast, we add zero */
3116 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3117 error("pointer not accepted for unary plus");
3127 parse_expr_type(&type
);
3131 size
= type_size(&type
, &align
);
3132 if (t
== TOK_SIZEOF
) {
3134 error("sizeof applied to an incomplete type");
3139 vtop
->type
.t
|= VT_UNSIGNED
;
3142 case TOK_builtin_types_compatible_p
:
3151 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3152 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3153 vpushi(is_compatible_types(&type1
, &type2
));
3156 case TOK_builtin_constant_p
:
3158 int saved_nocode_wanted
, res
;
3161 saved_nocode_wanted
= nocode_wanted
;
3164 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3166 nocode_wanted
= saved_nocode_wanted
;
3171 case TOK_builtin_frame_address
:
3176 if (tok
!= TOK_CINT
) {
3177 error("__builtin_frame_address only takes integers");
3180 error("TCC only supports __builtin_frame_address(0)");
3186 vset(&type
, VT_LOCAL
, 0);
3189 #ifdef TCC_TARGET_X86_64
3190 case TOK_builtin_malloc
:
3192 goto tok_identifier
;
3193 case TOK_builtin_free
:
3195 goto tok_identifier
;
3212 goto tok_identifier
;
3214 /* allow to take the address of a label */
3215 if (tok
< TOK_UIDENT
)
3216 expect("label identifier");
3217 s
= label_find(tok
);
3219 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3221 if (s
->r
== LABEL_DECLARED
)
3222 s
->r
= LABEL_FORWARD
;
3225 s
->type
.t
= VT_VOID
;
3226 mk_pointer(&s
->type
);
3227 s
->type
.t
|= VT_STATIC
;
3229 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3238 expect("identifier");
3242 error("'%s' undeclared", get_tok_str(t
, NULL
));
3243 /* for simple function calls, we tolerate undeclared
3244 external reference to int() function */
3245 if (tcc_state
->warn_implicit_function_declaration
)
3246 warning("implicit declaration of function '%s'",
3247 get_tok_str(t
, NULL
));
3248 s
= external_global_sym(t
, &func_old_type
, 0);
3250 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3251 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3252 /* if referencing an inline function, then we generate a
3253 symbol to it if not already done. It will have the
3254 effect to generate code for it at the end of the
3255 compilation unit. Inline function as always
3256 generated in the text section. */
3258 put_extern_sym(s
, text_section
, 0, 0);
3259 r
= VT_SYM
| VT_CONST
;
3263 vset(&s
->type
, r
, s
->c
);
3264 /* if forward reference, we must point to s */
3265 if (vtop
->r
& VT_SYM
) {
3272 /* post operations */
3274 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3277 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3280 if (tok
== TOK_ARROW
)
3282 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3286 /* expect pointer on structure */
3287 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3288 expect("struct or union");
3292 while ((s
= s
->next
) != NULL
) {
3297 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3298 /* add field offset to pointer */
3299 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3302 /* change type to field type, and set to lvalue */
3303 vtop
->type
= s
->type
;
3304 vtop
->type
.t
|= qualifiers
;
3305 /* an array is never an lvalue */
3306 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3307 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3308 /* if bound checking, the referenced pointer must be checked */
3309 if (tcc_state
->do_bounds_check
)
3310 vtop
->r
|= VT_MUSTBOUND
;
3313 } else if (tok
== '[') {
3319 } else if (tok
== '(') {
3325 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3326 /* pointer test (no array accepted) */
3327 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3328 vtop
->type
= *pointed_type(&vtop
->type
);
3329 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3333 expect("function pointer");
3336 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3338 /* get return type */
3341 sa
= s
->next
; /* first parameter */
3344 /* compute first implicit argument if a structure is returned */
3345 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3346 /* get some space for the returned structure */
3347 size
= type_size(&s
->type
, &align
);
3348 loc
= (loc
- size
) & -align
;
3350 ret
.r
= VT_LOCAL
| VT_LVAL
;
3351 /* pass it as 'int' to avoid structure arg passing
3353 vseti(VT_LOCAL
, loc
);
3358 /* return in register */
3359 if (is_float(ret
.type
.t
)) {
3360 ret
.r
= reg_fret(ret
.type
.t
);
3362 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3371 gfunc_param_typed(s
, sa
);
3381 error("too few arguments to function");
3383 if (!nocode_wanted
) {
3384 gfunc_call(nb_args
);
3386 vtop
-= (nb_args
+ 1);
3389 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3397 static void uneq(void)
3403 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3404 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3405 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3420 static void expr_prod(void)
3425 while (tok
== '*' || tok
== '/' || tok
== '%') {
3433 static void expr_sum(void)
3438 while (tok
== '+' || tok
== '-') {
3446 static void expr_shift(void)
3451 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3459 static void expr_cmp(void)
3464 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3465 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3473 static void expr_cmpeq(void)
3478 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3486 static void expr_and(void)
3489 while (tok
== '&') {
3496 static void expr_xor(void)
3499 while (tok
== '^') {
3506 static void expr_or(void)
3509 while (tok
== '|') {
3516 /* XXX: fix this mess */
3517 static void expr_land_const(void)
3520 while (tok
== TOK_LAND
) {
3527 /* XXX: fix this mess */
3528 static void expr_lor_const(void)
3531 while (tok
== TOK_LOR
) {
3538 /* only used if non constant */
3539 static void expr_land(void)
3544 if (tok
== TOK_LAND
) {
3549 if (tok
!= TOK_LAND
) {
3559 static void expr_lor(void)
3564 if (tok
== TOK_LOR
) {
3569 if (tok
!= TOK_LOR
) {
3579 /* XXX: better constant handling */
3580 static void expr_eq(void)
3582 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3584 CType type
, type1
, type2
;
3591 boolean
.t
= VT_BOOL
;
3597 if (tok
!= ':' || !gnu_ext
) {
3612 if (vtop
!= vstack
) {
3613 /* needed to avoid having different registers saved in
3615 if (is_float(vtop
->type
.t
)) {
3617 #ifdef TCC_TARGET_X86_64
3618 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3628 if (tok
== ':' && gnu_ext
) {
3636 sv
= *vtop
; /* save value to handle it later */
3637 vtop
--; /* no vpop so that FP stack is not flushed */
3645 bt1
= t1
& VT_BTYPE
;
3647 bt2
= t2
& VT_BTYPE
;
3648 /* cast operands to correct type according to ISOC rules */
3649 if (is_float(bt1
) || is_float(bt2
)) {
3650 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3651 type
.t
= VT_LDOUBLE
;
3652 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3657 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3658 /* cast to biggest op */
3660 /* convert to unsigned if it does not fit in a long long */
3661 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3662 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3663 type
.t
|= VT_UNSIGNED
;
3664 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3665 /* XXX: test pointer compatibility */
3667 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3668 /* XXX: test function pointer compatibility */
3670 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3671 /* XXX: test structure compatibility */
3673 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3674 /* NOTE: as an extension, we accept void on only one side */
3677 /* integer operations */
3679 /* convert to unsigned if it does not fit in an integer */
3680 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3681 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3682 type
.t
|= VT_UNSIGNED
;
3685 /* now we convert second operand */
3687 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3690 if (is_float(type
.t
)) {
3692 #ifdef TCC_TARGET_X86_64
3693 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3697 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3698 /* for long longs, we use fixed registers to avoid having
3699 to handle a complicated move */
3704 /* this is horrible, but we must also convert first
3708 /* put again first value and cast it */
3711 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3721 static void gexpr(void)
3732 /* parse an expression and return its type without any side effect. */
3733 static void expr_type(CType
*type
)
3735 int saved_nocode_wanted
;
3737 saved_nocode_wanted
= nocode_wanted
;
3742 nocode_wanted
= saved_nocode_wanted
;
3745 /* parse a unary expression and return its type without any side
3747 static void unary_type(CType
*type
)
3759 /* parse a constant expression and return value in vtop. */
3760 static void expr_const1(void)
3769 /* parse an integer constant and return its value. */
3770 static int expr_const(void)
3774 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3775 expect("constant expression");
3781 /* return the label token if current token is a label, otherwise
3783 static int is_label(void)
3787 /* fast test first */
3788 if (tok
< TOK_UIDENT
)
3790 /* no need to save tokc because tok is an identifier */
3797 unget_tok(last_tok
);
3802 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3803 int case_reg
, int is_expr
)
3808 /* generate line number info */
3809 if (tcc_state
->do_debug
&&
3810 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3811 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3813 last_line_num
= file
->line_num
;
3817 /* default return value is (void) */
3819 vtop
->type
.t
= VT_VOID
;
3822 if (tok
== TOK_IF
) {
3829 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3831 if (c
== TOK_ELSE
) {
3835 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3836 gsym(d
); /* patch else jmp */
3839 } else if (tok
== TOK_WHILE
) {
3847 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3851 } else if (tok
== '{') {
3855 /* record local declaration stack position */
3857 llabel
= local_label_stack
;
3858 /* handle local labels declarations */
3859 if (tok
== TOK_LABEL
) {
3862 if (tok
< TOK_UIDENT
)
3863 expect("label identifier");
3864 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3874 while (tok
!= '}') {
3879 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3882 /* pop locally defined labels */
3883 label_pop(&local_label_stack
, llabel
);
3884 /* pop locally defined symbols */
3886 /* XXX: this solution makes only valgrind happy...
3887 triggered by gcc.c-torture/execute/20000917-1.c */
3889 switch(vtop
->type
.t
& VT_BTYPE
) {
3894 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3896 error("unsupported expression type");
3899 sym_pop(&local_stack
, s
);
3901 } else if (tok
== TOK_RETURN
) {
3905 gen_assign_cast(&func_vt
);
3906 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3908 /* if returning structure, must copy it to implicit
3909 first pointer arg location */
3912 size
= type_size(&func_vt
,&align
);
3915 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3919 loc
= (loc
- size
) & -4;
3922 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3925 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3927 vtop
->type
= int_type
;
3933 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3936 /* copy structure value to pointer */
3941 } else if (is_float(func_vt
.t
)) {
3942 gv(rc_fret(func_vt
.t
));
3946 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3949 rsym
= gjmp(rsym
); /* jmp */
3950 } else if (tok
== TOK_BREAK
) {
3953 error("cannot break");
3954 *bsym
= gjmp(*bsym
);
3957 } else if (tok
== TOK_CONTINUE
) {
3960 error("cannot continue");
3961 *csym
= gjmp(*csym
);
3964 } else if (tok
== TOK_FOR
) {
3991 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3996 if (tok
== TOK_DO
) {
4001 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4012 if (tok
== TOK_SWITCH
) {
4016 /* XXX: other types than integer */
4017 case_reg
= gv(RC_INT
);
4021 b
= gjmp(0); /* jump to first case */
4023 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4024 /* if no default, jmp after switch */
4032 if (tok
== TOK_CASE
) {
4039 if (gnu_ext
&& tok
== TOK_DOTS
) {
4043 warning("empty case range");
4045 /* since a case is like a label, we must skip it with a jmp */
4052 *case_sym
= gtst(1, 0);
4055 *case_sym
= gtst(1, 0);
4059 *case_sym
= gtst(1, *case_sym
);
4064 goto block_after_label
;
4066 if (tok
== TOK_DEFAULT
) {
4072 error("too many 'default'");
4075 goto block_after_label
;
4077 if (tok
== TOK_GOTO
) {
4079 if (tok
== '*' && gnu_ext
) {
4083 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4086 } else if (tok
>= TOK_UIDENT
) {
4087 s
= label_find(tok
);
4088 /* put forward definition if needed */
4090 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4092 if (s
->r
== LABEL_DECLARED
)
4093 s
->r
= LABEL_FORWARD
;
4095 /* label already defined */
4096 if (s
->r
& LABEL_FORWARD
)
4097 s
->jnext
= gjmp(s
->jnext
);
4099 gjmp_addr(s
->jnext
);
4102 expect("label identifier");
4105 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4113 if (s
->r
== LABEL_DEFINED
)
4114 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4116 s
->r
= LABEL_DEFINED
;
4118 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4121 /* we accept this, but it is a mistake */
4124 warning("deprecated use of label at end of compound statement");
4128 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4131 /* expression case */
4146 /* t is the array or struct type. c is the array or struct
4147 address. cur_index/cur_field is the pointer to the current
4148 value. 'size_only' is true if only size info is needed (only used
4150 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4151 int *cur_index
, Sym
**cur_field
,
4155 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4161 if (gnu_ext
&& (l
= is_label()) != 0)
4163 while (tok
== '[' || tok
== '.') {
4165 if (!(type
->t
& VT_ARRAY
))
4166 expect("array type");
4169 index
= expr_const();
4170 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4171 expect("invalid index");
4172 if (tok
== TOK_DOTS
&& gnu_ext
) {
4174 index_last
= expr_const();
4175 if (index_last
< 0 ||
4176 (s
->c
>= 0 && index_last
>= s
->c
) ||
4178 expect("invalid index");
4184 *cur_index
= index_last
;
4185 type
= pointed_type(type
);
4186 elem_size
= type_size(type
, &align
);
4187 c
+= index
* elem_size
;
4188 /* NOTE: we only support ranges for last designator */
4189 nb_elems
= index_last
- index
+ 1;
4190 if (nb_elems
!= 1) {
4199 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4200 expect("struct/union type");
4213 /* XXX: fix this mess by using explicit storage field */
4215 type1
.t
|= (type
->t
& ~VT_TYPE
);
4229 if (type
->t
& VT_ARRAY
) {
4231 type
= pointed_type(type
);
4232 c
+= index
* type_size(type
, &align
);
4236 error("too many field init");
4237 /* XXX: fix this mess by using explicit storage field */
4239 type1
.t
|= (type
->t
& ~VT_TYPE
);
4244 decl_initializer(type
, sec
, c
, 0, size_only
);
4246 /* XXX: make it more general */
4247 if (!size_only
&& nb_elems
> 1) {
4248 unsigned long c_end
;
4253 error("range init not supported yet for dynamic storage");
4254 c_end
= c
+ nb_elems
* elem_size
;
4255 if (c_end
> sec
->data_allocated
)
4256 section_realloc(sec
, c_end
);
4257 src
= sec
->data
+ c
;
4259 for(i
= 1; i
< nb_elems
; i
++) {
4261 memcpy(dst
, src
, elem_size
);
4267 #define EXPR_CONST 1
4270 /* store a value or an expression directly in global data or in local array */
4271 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4272 int v
, int expr_type
)
4274 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4276 unsigned long long bit_mask
;
4284 /* compound literals must be allocated globally in this case */
4285 saved_global_expr
= global_expr
;
4288 global_expr
= saved_global_expr
;
4289 /* NOTE: symbols are accepted */
4290 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4291 error("initializer element is not constant");
4299 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4302 /* XXX: not portable */
4303 /* XXX: generate error if incorrect relocation */
4304 gen_assign_cast(&dtype
);
4305 bt
= type
->t
& VT_BTYPE
;
4306 /* we'll write at most 12 bytes */
4307 if (c
+ 12 > sec
->data_allocated
) {
4308 section_realloc(sec
, c
+ 12);
4310 ptr
= sec
->data
+ c
;
4311 /* XXX: make code faster ? */
4312 if (!(type
->t
& VT_BITFIELD
)) {
4317 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4318 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4319 bit_mask
= (1LL << bit_size
) - 1;
4321 if ((vtop
->r
& VT_SYM
) &&
4327 (bt
== VT_INT
&& bit_size
!= 32)))
4328 error("initializer element is not computable at load time");
4331 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4333 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4336 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4339 *(double *)ptr
= vtop
->c
.d
;
4342 *(long double *)ptr
= vtop
->c
.ld
;
4345 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4348 if (vtop
->r
& VT_SYM
) {
4349 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
4351 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4356 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4363 /* put zeros for variable based init */
4364 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4367 /* nothing to do because globals are already set to zero */
4369 vpush_global_sym(&func_old_type
, TOK_memset
);
4377 /* 't' contains the type and storage info. 'c' is the offset of the
4378 object in section 'sec'. If 'sec' is NULL, it means stack based
4379 allocation. 'first' is true if array '{' must be read (multi
4380 dimension implicit array init handling). 'size_only' is true if
4381 size only evaluation is wanted (only for arrays). */
4382 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4383 int first
, int size_only
)
4385 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4386 int size1
, align1
, expr_type
;
4390 if (type
->t
& VT_ARRAY
) {
4394 t1
= pointed_type(type
);
4395 size1
= type_size(t1
, &align1
);
4398 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4404 /* only parse strings here if correct type (otherwise: handle
4405 them as ((w)char *) expressions */
4406 if ((tok
== TOK_LSTR
&&
4407 #ifdef TCC_TARGET_PE
4408 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4410 (t1
->t
& VT_BTYPE
) == VT_INT
4412 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4413 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4418 /* compute maximum number of chars wanted */
4420 cstr_len
= cstr
->size
;
4422 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4425 if (n
>= 0 && nb
> (n
- array_length
))
4426 nb
= n
- array_length
;
4429 warning("initializer-string for array is too long");
4430 /* in order to go faster for common case (char
4431 string in global variable, we handle it
4433 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4434 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4438 ch
= ((unsigned char *)cstr
->data
)[i
];
4440 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4441 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4449 /* only add trailing zero if enough storage (no
4450 warning in this case since it is standard) */
4451 if (n
< 0 || array_length
< n
) {
4453 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4459 while (tok
!= '}') {
4460 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4461 if (n
>= 0 && index
>= n
)
4462 error("index too large");
4463 /* must put zero in holes (note that doing it that way
4464 ensures that it even works with designators) */
4465 if (!size_only
&& array_length
< index
) {
4466 init_putz(t1
, sec
, c
+ array_length
* size1
,
4467 (index
- array_length
) * size1
);
4470 if (index
> array_length
)
4471 array_length
= index
;
4472 /* special test for multi dimensional arrays (may not
4473 be strictly correct if designators are used at the
4475 if (index
>= n
&& no_oblock
)
4484 /* put zeros at the end */
4485 if (!size_only
&& n
>= 0 && array_length
< n
) {
4486 init_putz(t1
, sec
, c
+ array_length
* size1
,
4487 (n
- array_length
) * size1
);
4489 /* patch type size if needed */
4491 s
->c
= array_length
;
4492 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4493 (sec
|| !first
|| tok
== '{')) {
4496 /* NOTE: the previous test is a specific case for automatic
4497 struct/union init */
4498 /* XXX: union needs only one init */
4500 /* XXX: this test is incorrect for local initializers
4501 beginning with ( without {. It would be much more difficult
4502 to do it correctly (ideally, the expression parser should
4503 be used in all cases) */
4509 while (tok
== '(') {
4513 if (!parse_btype(&type1
, &ad1
))
4515 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4517 if (!is_assignable_types(type
, &type1
))
4518 error("invalid type for cast");
4523 if (first
|| tok
== '{') {
4532 while (tok
!= '}') {
4533 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4535 if (!size_only
&& array_length
< index
) {
4536 init_putz(type
, sec
, c
+ array_length
,
4537 index
- array_length
);
4539 index
= index
+ type_size(&f
->type
, &align1
);
4540 if (index
> array_length
)
4541 array_length
= index
;
4543 /* gr: skip fields from same union - ugly. */
4545 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4546 /* test for same offset */
4547 if (f
->next
->c
!= f
->c
)
4549 /* if yes, test for bitfield shift */
4550 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4551 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4552 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4553 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4554 if (bit_pos_1
!= bit_pos_2
)
4561 if (no_oblock
&& f
== NULL
)
4567 /* put zeros at the end */
4568 if (!size_only
&& array_length
< n
) {
4569 init_putz(type
, sec
, c
+ array_length
,
4578 } else if (tok
== '{') {
4580 decl_initializer(type
, sec
, c
, first
, size_only
);
4582 } else if (size_only
) {
4583 /* just skip expression */
4585 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4589 else if (tok
== ')')
4594 /* currently, we always use constant expression for globals
4595 (may change for scripting case) */
4596 expr_type
= EXPR_CONST
;
4598 expr_type
= EXPR_ANY
;
4599 init_putv(type
, sec
, c
, 0, expr_type
);
4603 /* parse an initializer for type 't' if 'has_init' is non zero, and
4604 allocate space in local or global data space ('r' is either
4605 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4606 variable 'v' of scope 'scope' is declared before initializers are
4607 parsed. If 'v' is zero, then a reference to the new object is put
4608 in the value stack. If 'has_init' is 2, a special parsing is done
4609 to handle string constants. */
4610 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4611 int has_init
, int v
, int scope
)
4613 int size
, align
, addr
, data_offset
;
4615 ParseState saved_parse_state
= {0};
4616 TokenString init_str
;
4619 size
= type_size(type
, &align
);
4620 /* If unknown size, we must evaluate it before
4621 evaluating initializers because
4622 initializers can generate global data too
4623 (e.g. string pointers or ISOC99 compound
4624 literals). It also simplifies local
4625 initializers handling */
4626 tok_str_new(&init_str
);
4629 error("unknown type size");
4630 /* get all init string */
4631 if (has_init
== 2) {
4632 /* only get strings */
4633 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4634 tok_str_add_tok(&init_str
);
4639 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4641 error("unexpected end of file in initializer");
4642 tok_str_add_tok(&init_str
);
4645 else if (tok
== '}') {
4655 tok_str_add(&init_str
, -1);
4656 tok_str_add(&init_str
, 0);
4659 save_parse_state(&saved_parse_state
);
4661 macro_ptr
= init_str
.str
;
4663 decl_initializer(type
, NULL
, 0, 1, 1);
4664 /* prepare second initializer parsing */
4665 macro_ptr
= init_str
.str
;
4668 /* if still unknown size, error */
4669 size
= type_size(type
, &align
);
4671 error("unknown type size");
4673 /* take into account specified alignment if bigger */
4675 if (ad
->aligned
> align
)
4676 align
= ad
->aligned
;
4677 } else if (ad
->packed
) {
4680 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4682 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4684 loc
= (loc
- size
) & -align
;
4686 /* handles bounds */
4687 /* XXX: currently, since we do only one pass, we cannot track
4688 '&' operators, so we add only arrays */
4689 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4690 unsigned long *bounds_ptr
;
4691 /* add padding between regions */
4693 /* then add local bound info */
4694 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4695 bounds_ptr
[0] = addr
;
4696 bounds_ptr
[1] = size
;
4699 /* local variable */
4700 sym_push(v
, type
, r
, addr
);
4702 /* push local reference */
4703 vset(type
, r
, addr
);
4709 if (v
&& scope
== VT_CONST
) {
4710 /* see if the symbol was already defined */
4713 if (!is_compatible_types(&sym
->type
, type
))
4714 error("incompatible types for redefinition of '%s'",
4715 get_tok_str(v
, NULL
));
4716 if (sym
->type
.t
& VT_EXTERN
) {
4717 /* if the variable is extern, it was not allocated */
4718 sym
->type
.t
&= ~VT_EXTERN
;
4719 /* set array size if it was ommited in extern
4721 if ((sym
->type
.t
& VT_ARRAY
) &&
4722 sym
->type
.ref
->c
< 0 &&
4724 sym
->type
.ref
->c
= type
->ref
->c
;
4726 /* we accept several definitions of the same
4727 global variable. this is tricky, because we
4728 must play with the SHN_COMMON type of the symbol */
4729 /* XXX: should check if the variable was already
4730 initialized. It is incorrect to initialized it
4732 /* no init data, we won't add more to the symbol */
4739 /* allocate symbol in corresponding section */
4744 else if (tcc_state
->nocommon
)
4748 data_offset
= sec
->data_offset
;
4749 data_offset
= (data_offset
+ align
- 1) & -align
;
4751 /* very important to increment global pointer at this time
4752 because initializers themselves can create new initializers */
4753 data_offset
+= size
;
4754 /* add padding if bound check */
4755 if (tcc_state
->do_bounds_check
)
4757 sec
->data_offset
= data_offset
;
4758 /* allocate section space to put the data */
4759 if (sec
->sh_type
!= SHT_NOBITS
&&
4760 data_offset
> sec
->data_allocated
)
4761 section_realloc(sec
, data_offset
);
4762 /* align section if needed */
4763 if (align
> sec
->sh_addralign
)
4764 sec
->sh_addralign
= align
;
4766 addr
= 0; /* avoid warning */
4770 if (scope
!= VT_CONST
|| !sym
) {
4771 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4773 /* update symbol definition */
4775 put_extern_sym(sym
, sec
, addr
, size
);
4778 /* put a common area */
4779 put_extern_sym(sym
, NULL
, align
, size
);
4780 /* XXX: find a nicer way */
4781 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4782 esym
->st_shndx
= SHN_COMMON
;
4787 /* push global reference */
4788 sym
= get_sym_ref(type
, sec
, addr
, size
);
4790 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4794 /* handles bounds now because the symbol must be defined
4795 before for the relocation */
4796 if (tcc_state
->do_bounds_check
) {
4797 unsigned long *bounds_ptr
;
4799 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
4800 /* then add global bound info */
4801 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4802 bounds_ptr
[0] = 0; /* relocated */
4803 bounds_ptr
[1] = size
;
4807 decl_initializer(type
, sec
, addr
, 1, 0);
4808 /* restore parse state if needed */
4810 tok_str_free(init_str
.str
);
4811 restore_parse_state(&saved_parse_state
);
4817 void put_func_debug(Sym
*sym
)
4822 /* XXX: we put here a dummy type */
4823 snprintf(buf
, sizeof(buf
), "%s:%c1",
4824 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4825 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4826 cur_text_section
, sym
->c
);
4827 /* //gr gdb wants a line at the function */
4828 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4833 /* parse an old style function declaration list */
4834 /* XXX: check multiple parameter */
4835 static void func_decl_list(Sym
*func_sym
)
4842 /* parse each declaration */
4843 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4844 if (!parse_btype(&btype
, &ad
))
4845 expect("declaration list");
4846 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4847 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4849 /* we accept no variable after */
4853 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4854 /* find parameter in function parameter list */
4857 if ((s
->v
& ~SYM_FIELD
) == v
)
4861 error("declaration for parameter '%s' but no such parameter",
4862 get_tok_str(v
, NULL
));
4864 /* check that no storage specifier except 'register' was given */
4865 if (type
.t
& VT_STORAGE
)
4866 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4867 convert_parameter_type(&type
);
4868 /* we can add the type (NOTE: it could be local to the function) */
4870 /* accept other parameters */
4881 /* parse a function defined by symbol 'sym' and generate its code in
4882 'cur_text_section' */
4883 static void gen_function(Sym
*sym
)
4885 int saved_nocode_wanted
= nocode_wanted
;
4887 ind
= cur_text_section
->data_offset
;
4888 /* NOTE: we patch the symbol size later */
4889 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4890 funcname
= get_tok_str(sym
->v
, NULL
);
4892 /* put debug symbol */
4893 if (tcc_state
->do_debug
)
4894 put_func_debug(sym
);
4895 /* push a dummy symbol to enable local sym storage */
4896 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4897 gfunc_prolog(&sym
->type
);
4899 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4902 cur_text_section
->data_offset
= ind
;
4903 label_pop(&global_label_stack
, NULL
);
4904 sym_pop(&local_stack
, NULL
); /* reset local stack */
4905 /* end of function */
4906 /* patch symbol size */
4907 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4909 if (tcc_state
->do_debug
) {
4910 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4912 /* It's better to crash than to generate wrong code */
4913 cur_text_section
= NULL
;
4914 funcname
= ""; /* for safety */
4915 func_vt
.t
= VT_VOID
; /* for safety */
4916 ind
= 0; /* for safety */
4917 nocode_wanted
= saved_nocode_wanted
;
4920 static void gen_inline_functions(void)
4923 int *str
, inline_generated
, i
;
4924 struct InlineFunc
*fn
;
4926 /* iterate while inline function are referenced */
4928 inline_generated
= 0;
4929 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4930 fn
= tcc_state
->inline_fns
[i
];
4932 if (sym
&& sym
->c
) {
4933 /* the function was used: generate its code and
4934 convert it to a normal function */
4935 str
= fn
->token_str
;
4938 strcpy(file
->filename
, fn
->filename
);
4939 sym
->r
= VT_SYM
| VT_CONST
;
4940 sym
->type
.t
&= ~VT_INLINE
;
4944 cur_text_section
= text_section
;
4946 macro_ptr
= NULL
; /* fail safe */
4949 inline_generated
= 1;
4952 if (!inline_generated
)
4955 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
4958 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4959 static void decl(int l
)
4967 if (!parse_btype(&btype
, &ad
)) {
4968 /* skip redundant ';' */
4969 /* XXX: find more elegant solution */
4974 if (l
== VT_CONST
&&
4975 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
4976 /* global asm block */
4980 /* special test for old K&R protos without explicit int
4981 type. Only accepted when defining global data */
4982 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
4986 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4987 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4989 /* we accept no variable after */
4993 while (1) { /* iterate thru each declaration */
4995 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4999 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5000 printf("type = '%s'\n", buf
);
5003 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5004 /* if old style function prototype, we accept a
5007 if (sym
->c
== FUNC_OLD
)
5008 func_decl_list(sym
);
5013 error("cannot use local functions");
5014 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5015 expect("function definition");
5017 /* reject abstract declarators in function definition */
5019 while ((sym
= sym
->next
) != NULL
)
5020 if (!(sym
->v
& ~SYM_FIELD
))
5021 expect("identifier");
5023 /* XXX: cannot do better now: convert extern line to static inline */
5024 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5025 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5029 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5032 r
= sym
->type
.ref
->r
;
5033 /* use func_call from prototype if not defined */
5034 if (FUNC_CALL(r
) != FUNC_CDECL
5035 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5036 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5038 /* use export from prototype */
5040 FUNC_EXPORT(type
.ref
->r
) = 1;
5042 /* use static from prototype */
5043 if (sym
->type
.t
& VT_STATIC
)
5044 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5046 if (!is_compatible_types(&sym
->type
, &type
)) {
5048 error("incompatible types for redefinition of '%s'",
5049 get_tok_str(v
, NULL
));
5051 /* if symbol is already defined, then put complete type */
5054 /* put function symbol */
5055 sym
= global_identifier_push(v
, type
.t
, 0);
5056 sym
->type
.ref
= type
.ref
;
5059 /* static inline functions are just recorded as a kind
5060 of macro. Their code will be emitted at the end of
5061 the compilation unit only if they are used */
5062 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5063 (VT_INLINE
| VT_STATIC
)) {
5064 TokenString func_str
;
5066 struct InlineFunc
*fn
;
5067 const char *filename
;
5069 tok_str_new(&func_str
);
5075 error("unexpected end of file");
5076 tok_str_add_tok(&func_str
);
5081 } else if (t
== '}') {
5083 if (block_level
== 0)
5087 tok_str_add(&func_str
, -1);
5088 tok_str_add(&func_str
, 0);
5089 filename
= file
? file
->filename
: "";
5090 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5091 strcpy(fn
->filename
, filename
);
5093 fn
->token_str
= func_str
.str
;
5094 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5097 /* compute text section */
5098 cur_text_section
= ad
.section
;
5099 if (!cur_text_section
)
5100 cur_text_section
= text_section
;
5101 sym
->r
= VT_SYM
| VT_CONST
;
5106 if (btype
.t
& VT_TYPEDEF
) {
5107 /* save typedefed type */
5108 /* XXX: test storage specifiers ? */
5109 sym
= sym_push(v
, &type
, 0, 0);
5110 sym
->type
.t
|= VT_TYPEDEF
;
5111 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5112 /* external function definition */
5113 /* specific case for func_call attribute */
5115 type
.ref
->r
= ad
.func_attr
;
5116 external_sym(v
, &type
, 0);
5118 /* not lvalue if array */
5120 if (!(type
.t
& VT_ARRAY
))
5121 r
|= lvalue_type(type
.t
);
5122 has_init
= (tok
== '=');
5123 if ((btype
.t
& VT_EXTERN
) ||
5124 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5125 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5126 /* external variable */
5127 /* NOTE: as GCC, uninitialized global static
5128 arrays of null size are considered as
5130 external_sym(v
, &type
, r
);
5132 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5133 if (type
.t
& VT_STATIC
)
5139 decl_initializer_alloc(&type
, &ad
, r
,