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
)) == (VT_CONST
| VT_SYM
)
1104 && !(vtop
[-1].sym
->type
.t
& VT_IMPORT
))
1106 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1107 /* symbol + constant case */
1114 if (!nocode_wanted
) {
1115 /* call low level op generator */
1116 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1127 /* generate a floating point operation with constant propagation */
1128 void gen_opif(int op
)
1136 /* currently, we cannot do computations with forward symbols */
1137 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1138 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1140 if (v1
->type
.t
== VT_FLOAT
) {
1143 } else if (v1
->type
.t
== VT_DOUBLE
) {
1151 /* NOTE: we only do constant propagation if finite number (not
1152 NaN or infinity) (ANSI spec) */
1153 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1157 case '+': f1
+= f2
; break;
1158 case '-': f1
-= f2
; break;
1159 case '*': f1
*= f2
; break;
1163 error("division by zero in constant");
1168 /* XXX: also handles tests ? */
1172 /* XXX: overflow test ? */
1173 if (v1
->type
.t
== VT_FLOAT
) {
1175 } else if (v1
->type
.t
== VT_DOUBLE
) {
1183 if (!nocode_wanted
) {
1191 static int pointed_size(CType
*type
)
1194 return type_size(pointed_type(type
), &align
);
1197 static inline int is_null_pointer(SValue
*p
)
1199 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1201 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1202 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1205 static inline int is_integer_btype(int bt
)
1207 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1208 bt
== VT_INT
|| bt
== VT_LLONG
);
1211 /* check types for comparison or substraction of pointers */
1212 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1214 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1217 /* null pointers are accepted for all comparisons as gcc */
1218 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1222 bt1
= type1
->t
& VT_BTYPE
;
1223 bt2
= type2
->t
& VT_BTYPE
;
1224 /* accept comparison between pointer and integer with a warning */
1225 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1226 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1227 warning("comparison between pointer and integer");
1231 /* both must be pointers or implicit function pointers */
1232 if (bt1
== VT_PTR
) {
1233 type1
= pointed_type(type1
);
1234 } else if (bt1
!= VT_FUNC
)
1235 goto invalid_operands
;
1237 if (bt2
== VT_PTR
) {
1238 type2
= pointed_type(type2
);
1239 } else if (bt2
!= VT_FUNC
) {
1241 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1243 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1244 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1248 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1249 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1250 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1251 /* gcc-like error if '-' is used */
1253 goto invalid_operands
;
1255 warning("comparison of distinct pointer types lacks a cast");
1259 /* generic gen_op: handles types problems */
1262 int u
, t1
, t2
, bt1
, bt2
, t
;
1265 t1
= vtop
[-1].type
.t
;
1266 t2
= vtop
[0].type
.t
;
1267 bt1
= t1
& VT_BTYPE
;
1268 bt2
= t2
& VT_BTYPE
;
1270 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1271 /* at least one operand is a pointer */
1272 /* relationnal op: must be both pointers */
1273 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1274 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1275 /* pointers are handled are unsigned */
1276 #ifdef TCC_TARGET_X86_64
1277 t
= VT_LLONG
| VT_UNSIGNED
;
1279 t
= VT_INT
| VT_UNSIGNED
;
1283 /* if both pointers, then it must be the '-' op */
1284 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1286 error("cannot use pointers here");
1287 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1288 /* XXX: check that types are compatible */
1289 u
= pointed_size(&vtop
[-1].type
);
1291 /* set to integer type */
1292 #ifdef TCC_TARGET_X86_64
1293 vtop
->type
.t
= VT_LLONG
;
1295 vtop
->type
.t
= VT_INT
;
1300 /* exactly one pointer : must be '+' or '-'. */
1301 if (op
!= '-' && op
!= '+')
1302 error("cannot use pointers here");
1303 /* Put pointer as first operand */
1304 if (bt2
== VT_PTR
) {
1308 type1
= vtop
[-1].type
;
1309 #ifdef TCC_TARGET_X86_64
1310 vpushll(pointed_size(&vtop
[-1].type
));
1312 /* XXX: cast to int ? (long long case) */
1313 vpushi(pointed_size(&vtop
[-1].type
));
1316 #ifdef CONFIG_TCC_BCHECK
1317 /* if evaluating constant expression, no code should be
1318 generated, so no bound check */
1319 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1320 /* if bounded pointers, we generate a special code to
1327 gen_bounded_ptr_add();
1333 /* put again type if gen_opic() swaped operands */
1336 } else if (is_float(bt1
) || is_float(bt2
)) {
1337 /* compute bigger type and do implicit casts */
1338 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1340 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1345 /* floats can only be used for a few operations */
1346 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1347 (op
< TOK_ULT
|| op
> TOK_GT
))
1348 error("invalid operands for binary operation");
1350 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1351 /* cast to biggest op */
1353 /* convert to unsigned if it does not fit in a long long */
1354 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1355 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1359 /* integer operations */
1361 /* convert to unsigned if it does not fit in an integer */
1362 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1363 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1366 /* XXX: currently, some unsigned operations are explicit, so
1367 we modify them here */
1368 if (t
& VT_UNSIGNED
) {
1375 else if (op
== TOK_LT
)
1377 else if (op
== TOK_GT
)
1379 else if (op
== TOK_LE
)
1381 else if (op
== TOK_GE
)
1388 /* special case for shifts and long long: we keep the shift as
1390 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1397 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1398 /* relationnal op: the result is an int */
1399 vtop
->type
.t
= VT_INT
;
1406 #ifndef TCC_TARGET_ARM
1407 /* generic itof for unsigned long long case */
1408 void gen_cvt_itof1(int t
)
1410 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1411 (VT_LLONG
| VT_UNSIGNED
)) {
1414 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1415 #if LDOUBLE_SIZE != 8
1416 else if (t
== VT_LDOUBLE
)
1417 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1420 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1424 vtop
->r
= reg_fret(t
);
1431 /* generic ftoi for unsigned long long case */
1432 void gen_cvt_ftoi1(int t
)
1436 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1437 /* not handled natively */
1438 st
= vtop
->type
.t
& VT_BTYPE
;
1440 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1441 #if LDOUBLE_SIZE != 8
1442 else if (st
== VT_LDOUBLE
)
1443 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1446 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1451 vtop
->r2
= REG_LRET
;
1457 /* force char or short cast */
1458 void force_charshort_cast(int t
)
1462 /* XXX: add optimization if lvalue : just change type and offset */
1467 if (t
& VT_UNSIGNED
) {
1468 vpushi((1 << bits
) - 1);
1474 /* result must be signed or the SAR is converted to an SHL
1475 This was not the case when "t" was a signed short
1476 and the last value on the stack was an unsigned int */
1477 vtop
->type
.t
&= ~VT_UNSIGNED
;
1483 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1484 static void gen_cast(CType
*type
)
1486 int sbt
, dbt
, sf
, df
, c
, p
;
1488 /* special delayed cast for char/short */
1489 /* XXX: in some cases (multiple cascaded casts), it may still
1491 if (vtop
->r
& VT_MUSTCAST
) {
1492 vtop
->r
&= ~VT_MUSTCAST
;
1493 force_charshort_cast(vtop
->type
.t
);
1496 /* bitfields first get cast to ints */
1497 if (vtop
->type
.t
& VT_BITFIELD
) {
1501 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1502 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1507 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1508 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1510 /* constant case: we can do it now */
1511 /* XXX: in ISOC, cannot do it if error in convert */
1512 if (sbt
== VT_FLOAT
)
1513 vtop
->c
.ld
= vtop
->c
.f
;
1514 else if (sbt
== VT_DOUBLE
)
1515 vtop
->c
.ld
= vtop
->c
.d
;
1518 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1519 if (sbt
& VT_UNSIGNED
)
1520 vtop
->c
.ld
= vtop
->c
.ull
;
1522 vtop
->c
.ld
= vtop
->c
.ll
;
1524 if (sbt
& VT_UNSIGNED
)
1525 vtop
->c
.ld
= vtop
->c
.ui
;
1527 vtop
->c
.ld
= vtop
->c
.i
;
1530 if (dbt
== VT_FLOAT
)
1531 vtop
->c
.f
= (float)vtop
->c
.ld
;
1532 else if (dbt
== VT_DOUBLE
)
1533 vtop
->c
.d
= (double)vtop
->c
.ld
;
1534 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1535 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1536 } else if (sf
&& dbt
== VT_BOOL
) {
1537 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1540 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1541 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1542 vtop
->c
.ll
= vtop
->c
.ull
;
1543 else if (sbt
& VT_UNSIGNED
)
1544 vtop
->c
.ll
= vtop
->c
.ui
;
1545 else if (sbt
!= VT_LLONG
)
1546 vtop
->c
.ll
= vtop
->c
.i
;
1548 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1549 vtop
->c
.ull
= vtop
->c
.ll
;
1550 else if (dbt
== VT_BOOL
)
1551 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1552 else if (dbt
!= VT_LLONG
) {
1554 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1556 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1559 if(dbt
& VT_UNSIGNED
)
1560 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1562 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1565 } else if (p
&& dbt
== VT_BOOL
) {
1568 } else if (!nocode_wanted
) {
1569 /* non constant case: generate code */
1571 /* convert from fp to fp */
1574 /* convert int to fp */
1577 /* convert fp to int */
1578 if (dbt
== VT_BOOL
) {
1582 /* we handle char/short/etc... with generic code */
1583 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1584 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1588 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1589 /* additional cast for char/short... */
1594 #ifndef TCC_TARGET_X86_64
1595 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1596 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1597 /* scalar to long long */
1598 /* machine independent conversion */
1600 /* generate high word */
1601 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1605 if (sbt
== VT_PTR
) {
1606 /* cast from pointer to int before we apply
1607 shift operation, which pointers don't support*/
1608 gen_cast(&int_type
);
1614 /* patch second register */
1615 vtop
[-1].r2
= vtop
->r
;
1619 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1620 (dbt
& VT_BTYPE
) == VT_PTR
) {
1621 /* XXX: not sure if this is perfect... need more tests */
1622 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1624 if (sbt
!= (VT_INT
| VT_UNSIGNED
) &&
1625 sbt
!= VT_PTR
&& sbt
!= VT_FUNC
) {
1626 /* x86_64 specific: movslq */
1628 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1632 } else if (dbt
== VT_BOOL
) {
1633 /* scalar to bool */
1636 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1637 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1638 if (sbt
== VT_PTR
) {
1639 vtop
->type
.t
= VT_INT
;
1640 warning("nonportable conversion from pointer to char/short");
1642 force_charshort_cast(dbt
);
1643 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1645 if (sbt
== VT_LLONG
) {
1646 /* from long long: just take low order word */
1650 /* if lvalue and single word type, nothing to do because
1651 the lvalue already contains the real type size (see
1652 VT_LVAL_xxx constants) */
1655 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1656 /* if we are casting between pointer types,
1657 we must update the VT_LVAL_xxx size */
1658 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1659 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1664 /* return type size. Put alignment at 'a' */
1665 static int type_size(CType
*type
, int *a
)
1670 bt
= type
->t
& VT_BTYPE
;
1671 if (bt
== VT_STRUCT
) {
1676 } else if (bt
== VT_PTR
) {
1677 if (type
->t
& VT_ARRAY
) {
1681 ts
= type_size(&s
->type
, a
);
1683 if (ts
< 0 && s
->c
< 0)
1691 } else if (bt
== VT_LDOUBLE
) {
1693 return LDOUBLE_SIZE
;
1694 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
1695 #ifdef TCC_TARGET_I386
1696 #ifdef TCC_TARGET_PE
1701 #elif defined(TCC_TARGET_ARM)
1711 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
1714 } else if (bt
== VT_SHORT
) {
1718 /* char, void, function, _Bool */
1724 /* return the pointed type of t */
1725 static inline CType
*pointed_type(CType
*type
)
1727 return &type
->ref
->type
;
1730 /* modify type so that its it is a pointer to type. */
1731 static void mk_pointer(CType
*type
)
1734 s
= sym_push(SYM_FIELD
, type
, 0, -1);
1735 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
1739 /* compare function types. OLD functions match any new functions */
1740 static int is_compatible_func(CType
*type1
, CType
*type2
)
1746 if (!is_compatible_types(&s1
->type
, &s2
->type
))
1748 /* check func_call */
1749 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
1751 /* XXX: not complete */
1752 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
1756 while (s1
!= NULL
) {
1759 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
1769 /* return true if type1 and type2 are the same. If unqualified is
1770 true, qualifiers on the types are ignored.
1772 - enums are not checked as gcc __builtin_types_compatible_p ()
1774 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
1778 t1
= type1
->t
& VT_TYPE
;
1779 t2
= type2
->t
& VT_TYPE
;
1781 /* strip qualifiers before comparing */
1782 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1783 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1785 /* XXX: bitfields ? */
1788 /* test more complicated cases */
1789 bt1
= t1
& VT_BTYPE
;
1790 if (bt1
== VT_PTR
) {
1791 type1
= pointed_type(type1
);
1792 type2
= pointed_type(type2
);
1793 return is_compatible_types(type1
, type2
);
1794 } else if (bt1
== VT_STRUCT
) {
1795 return (type1
->ref
== type2
->ref
);
1796 } else if (bt1
== VT_FUNC
) {
1797 return is_compatible_func(type1
, type2
);
1803 /* return true if type1 and type2 are exactly the same (including
1806 static int is_compatible_types(CType
*type1
, CType
*type2
)
1808 return compare_types(type1
,type2
,0);
1811 /* return true if type1 and type2 are the same (ignoring qualifiers).
1813 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
1815 return compare_types(type1
,type2
,1);
1818 /* print a type. If 'varstr' is not NULL, then the variable is also
1819 printed in the type */
1821 /* XXX: add array and function pointers */
1822 void type_to_str(char *buf
, int buf_size
,
1823 CType
*type
, const char *varstr
)
1830 t
= type
->t
& VT_TYPE
;
1833 if (t
& VT_CONSTANT
)
1834 pstrcat(buf
, buf_size
, "const ");
1835 if (t
& VT_VOLATILE
)
1836 pstrcat(buf
, buf_size
, "volatile ");
1837 if (t
& VT_UNSIGNED
)
1838 pstrcat(buf
, buf_size
, "unsigned ");
1868 tstr
= "long double";
1870 pstrcat(buf
, buf_size
, tstr
);
1874 if (bt
== VT_STRUCT
)
1878 pstrcat(buf
, buf_size
, tstr
);
1879 v
= type
->ref
->v
& ~SYM_STRUCT
;
1880 if (v
>= SYM_FIRST_ANOM
)
1881 pstrcat(buf
, buf_size
, "<anonymous>");
1883 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
1887 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
1888 pstrcat(buf
, buf_size
, "(");
1890 while (sa
!= NULL
) {
1891 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
1892 pstrcat(buf
, buf_size
, buf1
);
1895 pstrcat(buf
, buf_size
, ", ");
1897 pstrcat(buf
, buf_size
, ")");
1901 pstrcpy(buf1
, sizeof(buf1
), "*");
1903 pstrcat(buf1
, sizeof(buf1
), varstr
);
1904 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
1908 pstrcat(buf
, buf_size
, " ");
1909 pstrcat(buf
, buf_size
, varstr
);
1914 /* verify type compatibility to store vtop in 'dt' type, and generate
1916 static void gen_assign_cast(CType
*dt
)
1918 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
1919 char buf1
[256], buf2
[256];
1922 st
= &vtop
->type
; /* source type */
1923 dbt
= dt
->t
& VT_BTYPE
;
1924 sbt
= st
->t
& VT_BTYPE
;
1925 if (dt
->t
& VT_CONSTANT
)
1926 warning("assignment of read-only location");
1929 /* special cases for pointers */
1930 /* '0' can also be a pointer */
1931 if (is_null_pointer(vtop
))
1933 /* accept implicit pointer to integer cast with warning */
1934 if (is_integer_btype(sbt
)) {
1935 warning("assignment makes pointer from integer without a cast");
1938 type1
= pointed_type(dt
);
1939 /* a function is implicitely a function pointer */
1940 if (sbt
== VT_FUNC
) {
1941 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
1942 !is_compatible_types(pointed_type(dt
), st
))
1943 warning("assignment from incompatible pointer type");
1948 type2
= pointed_type(st
);
1949 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1950 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
1951 /* void * can match anything */
1953 /* exact type match, except for unsigned */
1956 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1957 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1958 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
1959 warning("assignment from incompatible pointer type");
1961 /* check const and volatile */
1962 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
1963 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
1964 warning("assignment discards qualifiers from pointer target type");
1970 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
1971 warning("assignment makes integer from pointer without a cast");
1973 /* XXX: more tests */
1978 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1979 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
1980 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1982 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
1983 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
1984 error("cannot cast '%s' to '%s'", buf1
, buf2
);
1992 /* store vtop in lvalue pushed on stack */
1995 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
1997 ft
= vtop
[-1].type
.t
;
1998 sbt
= vtop
->type
.t
& VT_BTYPE
;
1999 dbt
= ft
& VT_BTYPE
;
2000 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2001 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2002 /* optimize char/short casts */
2003 delayed_cast
= VT_MUSTCAST
;
2004 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2005 /* XXX: factorize */
2006 if (ft
& VT_CONSTANT
)
2007 warning("assignment of read-only location");
2010 if (!(ft
& VT_BITFIELD
))
2011 gen_assign_cast(&vtop
[-1].type
);
2014 if (sbt
== VT_STRUCT
) {
2015 /* if structure, only generate pointer */
2016 /* structure assignment : generate memcpy */
2017 /* XXX: optimize if small size */
2018 if (!nocode_wanted
) {
2019 size
= type_size(&vtop
->type
, &align
);
2023 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2024 else if(!(align
& 3))
2025 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2028 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2032 vtop
->type
.t
= VT_PTR
;
2036 vtop
->type
.t
= VT_PTR
;
2048 /* leave source on stack */
2049 } else if (ft
& VT_BITFIELD
) {
2050 /* bitfield store handling */
2051 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2052 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2053 /* remove bit field info to avoid loops */
2054 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2056 /* duplicate source into other register */
2061 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2062 gen_cast(&vtop
[-1].type
);
2063 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2066 /* duplicate destination */
2068 vtop
[-1] = vtop
[-2];
2070 /* mask and shift source */
2071 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2072 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2073 vpushll((1ULL << bit_size
) - 1ULL);
2075 vpushi((1 << bit_size
) - 1);
2081 /* load destination, mask and or with source */
2083 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2084 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2086 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2093 /* pop off shifted source from "duplicate source..." above */
2097 #ifdef CONFIG_TCC_BCHECK
2098 /* bound check case */
2099 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2105 if (!nocode_wanted
) {
2109 #ifdef TCC_TARGET_X86_64
2110 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2115 r
= gv(rc
); /* generate value */
2116 /* if lvalue was saved on stack, must read it */
2117 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2119 t
= get_reg(RC_INT
);
2120 #ifdef TCC_TARGET_X86_64
2125 sv
.r
= VT_LOCAL
| VT_LVAL
;
2126 sv
.c
.ul
= vtop
[-1].c
.ul
;
2128 vtop
[-1].r
= t
| VT_LVAL
;
2131 #ifndef TCC_TARGET_X86_64
2132 /* two word case handling : store second register at word + 4 */
2133 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2135 /* convert to int to increment easily */
2136 vtop
->type
.t
= VT_INT
;
2142 /* XXX: it works because r2 is spilled last ! */
2143 store(vtop
->r2
, vtop
- 1);
2148 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2149 vtop
->r
|= delayed_cast
;
2153 /* post defines POST/PRE add. c is the token ++ or -- */
2154 void inc(int post
, int c
)
2157 vdup(); /* save lvalue */
2159 gv_dup(); /* duplicate value */
2164 vpushi(c
- TOK_MID
);
2166 vstore(); /* store value */
2168 vpop(); /* if post op, return saved value */
2171 /* Parse GNUC __attribute__ extension. Currently, the following
2172 extensions are recognized:
2173 - aligned(n) : set data/function alignment.
2174 - packed : force data alignment to 1
2175 - section(x) : generate data/code in this section.
2176 - unused : currently ignored, but may be used someday.
2177 - regparm(n) : pass function parameters in registers (i386 only)
2179 static void parse_attribute(AttributeDef
*ad
)
2183 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2187 while (tok
!= ')') {
2188 if (tok
< TOK_IDENT
)
2189 expect("attribute name");
2197 expect("section name");
2198 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2207 if (n
<= 0 || (n
& (n
- 1)) != 0)
2208 error("alignment must be a positive power of two");
2221 /* currently, no need to handle it because tcc does not
2222 track unused objects */
2226 /* currently, no need to handle it because tcc does not
2227 track unused objects */
2232 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
2237 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
2239 #ifdef TCC_TARGET_I386
2249 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
2255 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
2259 FUNC_EXPORT(ad
->func_attr
) = 1;
2262 FUNC_IMPORT(ad
->func_attr
) = 1;
2265 if (tcc_state
->warn_unsupported
)
2266 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2267 /* skip parameters */
2269 int parenthesis
= 0;
2273 else if (tok
== ')')
2276 } while (parenthesis
&& tok
!= -1);
2289 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2290 static void struct_decl(CType
*type
, int u
)
2292 int a
, v
, size
, align
, maxalign
, c
, offset
;
2293 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2294 Sym
*s
, *ss
, *ass
, **ps
;
2298 a
= tok
; /* save decl type */
2303 /* struct already defined ? return it */
2305 expect("struct/union/enum name");
2309 error("invalid type");
2316 /* we put an undefined size for struct/union */
2317 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2318 s
->r
= 0; /* default alignment is zero as gcc */
2319 /* put struct/union/enum name in type */
2327 error("struct/union/enum already defined");
2328 /* cannot be empty */
2330 /* non empty enums are not allowed */
2331 if (a
== TOK_ENUM
) {
2335 expect("identifier");
2341 /* enum symbols have static storage */
2342 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2343 ss
->type
.t
|= VT_STATIC
;
2348 /* NOTE: we accept a trailing comma */
2359 while (tok
!= '}') {
2360 parse_btype(&btype
, &ad
);
2366 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2367 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2368 expect("identifier");
2369 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2370 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2371 error("invalid type for '%s'",
2372 get_tok_str(v
, NULL
));
2376 bit_size
= expr_const();
2377 /* XXX: handle v = 0 case for messages */
2379 error("negative width in bit-field '%s'",
2380 get_tok_str(v
, NULL
));
2381 if (v
&& bit_size
== 0)
2382 error("zero width for bit-field '%s'",
2383 get_tok_str(v
, NULL
));
2385 size
= type_size(&type1
, &align
);
2387 if (align
< ad
.aligned
)
2389 } else if (ad
.packed
) {
2391 } else if (*tcc_state
->pack_stack_ptr
) {
2392 if (align
> *tcc_state
->pack_stack_ptr
)
2393 align
= *tcc_state
->pack_stack_ptr
;
2396 if (bit_size
>= 0) {
2397 bt
= type1
.t
& VT_BTYPE
;
2404 error("bitfields must have scalar type");
2406 if (bit_size
> bsize
) {
2407 error("width of '%s' exceeds its type",
2408 get_tok_str(v
, NULL
));
2409 } else if (bit_size
== bsize
) {
2410 /* no need for bit fields */
2412 } else if (bit_size
== 0) {
2413 /* XXX: what to do if only padding in a
2415 /* zero size: means to pad */
2418 /* we do not have enough room ?
2419 did the type change?
2421 if ((bit_pos
+ bit_size
) > bsize
||
2422 bt
!= prevbt
|| a
== TOK_UNION
)
2425 /* XXX: handle LSB first */
2426 type1
.t
|= VT_BITFIELD
|
2427 (bit_pos
<< VT_STRUCT_SHIFT
) |
2428 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2429 bit_pos
+= bit_size
;
2435 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2436 /* add new memory data only if starting
2438 if (lbit_pos
== 0) {
2439 if (a
== TOK_STRUCT
) {
2440 c
= (c
+ align
- 1) & -align
;
2449 if (align
> maxalign
)
2453 printf("add field %s offset=%d",
2454 get_tok_str(v
, NULL
), offset
);
2455 if (type1
.t
& VT_BITFIELD
) {
2456 printf(" pos=%d size=%d",
2457 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2458 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2463 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2465 while ((ass
= ass
->next
) != NULL
) {
2466 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2471 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2475 if (tok
== ';' || tok
== TOK_EOF
)
2482 /* store size and alignment */
2483 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2489 /* return 0 if no type declaration. otherwise, return the basic type
2492 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2494 int t
, u
, type_found
, typespec_found
, typedef_found
;
2498 memset(ad
, 0, sizeof(AttributeDef
));
2506 /* currently, we really ignore extension */
2516 if ((t
& VT_BTYPE
) != 0)
2517 error("too many basic types");
2533 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2534 #ifndef TCC_TARGET_PE
2535 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2537 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2538 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2552 if ((t
& VT_BTYPE
) == VT_LONG
) {
2553 #ifdef TCC_TARGET_PE
2554 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2556 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2564 struct_decl(&type1
, VT_ENUM
);
2567 type
->ref
= type1
.ref
;
2571 struct_decl(&type1
, VT_STRUCT
);
2574 /* type modifiers */
2627 /* GNUC attribute */
2628 case TOK_ATTRIBUTE1
:
2629 case TOK_ATTRIBUTE2
:
2630 parse_attribute(ad
);
2637 parse_expr_type(&type1
);
2640 if (typespec_found
|| typedef_found
)
2643 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
2646 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
2647 type
->ref
= s
->type
.ref
;
2655 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
2656 error("signed and unsigned modifier");
2657 if (tcc_state
->char_is_unsigned
) {
2658 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
2663 /* long is never used as type */
2664 if ((t
& VT_BTYPE
) == VT_LONG
)
2665 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2666 t
= (t
& ~VT_BTYPE
) | VT_INT
;
2668 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2674 /* convert a function parameter type (array to pointer and function to
2675 function pointer) */
2676 static inline void convert_parameter_type(CType
*pt
)
2678 /* remove const and volatile qualifiers (XXX: const could be used
2679 to indicate a const function parameter */
2680 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2681 /* array must be transformed to pointer according to ANSI C */
2683 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
2688 static void post_type(CType
*type
, AttributeDef
*ad
)
2690 int n
, l
, t1
, arg_size
, align
;
2691 Sym
**plast
, *s
, *first
;
2696 /* function declaration */
2704 /* read param name and compute offset */
2705 if (l
!= FUNC_OLD
) {
2706 if (!parse_btype(&pt
, &ad1
)) {
2708 error("invalid type");
2715 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
2717 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2718 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
2719 error("parameter declared as void");
2720 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
2725 expect("identifier");
2729 convert_parameter_type(&pt
);
2730 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
2736 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
2743 /* if no parameters, then old type prototype */
2747 t1
= type
->t
& VT_STORAGE
;
2748 /* NOTE: const is ignored in returned type as it has a special
2749 meaning in gcc / C++ */
2750 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
2751 post_type(type
, ad
);
2752 /* we push a anonymous symbol which will contain the function prototype */
2753 FUNC_ARGS(ad
->func_attr
) = arg_size
;
2754 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
2756 type
->t
= t1
| VT_FUNC
;
2758 } else if (tok
== '[') {
2759 /* array definition */
2761 if (tok
== TOK_RESTRICT1
)
2767 error("invalid array size");
2770 /* parse next post type */
2771 t1
= type
->t
& VT_STORAGE
;
2772 type
->t
&= ~VT_STORAGE
;
2773 post_type(type
, ad
);
2775 /* we push a anonymous symbol which will contain the array
2777 s
= sym_push(SYM_FIELD
, type
, 0, n
);
2778 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
2783 /* Parse a type declaration (except basic type), and return the type
2784 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2785 expected. 'type' should contain the basic type. 'ad' is the
2786 attribute definition of the basic type. It can be modified by
2789 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
2792 CType type1
, *type2
;
2795 while (tok
== '*') {
2803 qualifiers
|= VT_CONSTANT
;
2808 qualifiers
|= VT_VOLATILE
;
2816 type
->t
|= qualifiers
;
2819 /* XXX: clarify attribute handling */
2820 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2821 parse_attribute(ad
);
2823 /* recursive type */
2824 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2825 type1
.t
= 0; /* XXX: same as int */
2828 /* XXX: this is not correct to modify 'ad' at this point, but
2829 the syntax is not clear */
2830 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2831 parse_attribute(ad
);
2832 type_decl(&type1
, ad
, v
, td
);
2835 /* type identifier */
2836 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
2840 if (!(td
& TYPE_ABSTRACT
))
2841 expect("identifier");
2845 post_type(type
, ad
);
2846 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
2847 parse_attribute(ad
);
2850 /* append type at the end of type1 */
2863 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2864 static int lvalue_type(int t
)
2869 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
2871 else if (bt
== VT_SHORT
)
2875 if (t
& VT_UNSIGNED
)
2876 r
|= VT_LVAL_UNSIGNED
;
2880 /* indirection with full error checking and bound check */
2881 static void indir(void)
2883 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
2884 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
2888 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
2890 vtop
->type
= *pointed_type(&vtop
->type
);
2891 /* Arrays and functions are never lvalues */
2892 if (!(vtop
->type
.t
& VT_ARRAY
)
2893 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
2894 vtop
->r
|= lvalue_type(vtop
->type
.t
);
2895 /* if bound checking, the referenced pointer must be checked */
2896 if (tcc_state
->do_bounds_check
)
2897 vtop
->r
|= VT_MUSTBOUND
;
2901 /* pass a parameter to a function and do type checking and casting */
2902 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
2907 func_type
= func
->c
;
2908 if (func_type
== FUNC_OLD
||
2909 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
2910 /* default casting : only need to convert float to double */
2911 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
2915 } else if (arg
== NULL
) {
2916 error("too many arguments to function");
2919 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
2920 gen_assign_cast(&type
);
2924 /* parse an expression of the form '(type)' or '(expr)' and return its
2926 static void parse_expr_type(CType
*type
)
2932 if (parse_btype(type
, &ad
)) {
2933 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2940 static void parse_type(CType
*type
)
2945 if (!parse_btype(type
, &ad
)) {
2948 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
2951 static void vpush_tokc(int t
)
2955 vsetc(&type
, VT_CONST
, &tokc
);
2958 static void unary(void)
2960 int n
, t
, align
, size
, r
;
2965 /* XXX: GCC 2.95.3 does not generate a table although it should be
2979 vpush_tokc(VT_INT
| VT_UNSIGNED
);
2983 vpush_tokc(VT_LLONG
);
2987 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
2991 vpush_tokc(VT_FLOAT
);
2995 vpush_tokc(VT_DOUBLE
);
2999 vpush_tokc(VT_LDOUBLE
);
3002 case TOK___FUNCTION__
:
3004 goto tok_identifier
;
3010 /* special function name identifier */
3011 len
= strlen(funcname
) + 1;
3012 /* generate char[len] type */
3017 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3018 ptr
= section_ptr_add(data_section
, len
);
3019 memcpy(ptr
, funcname
, len
);
3024 #ifdef TCC_TARGET_PE
3025 t
= VT_SHORT
| VT_UNSIGNED
;
3031 /* string parsing */
3034 if (tcc_state
->warn_write_strings
)
3039 memset(&ad
, 0, sizeof(AttributeDef
));
3040 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3045 if (parse_btype(&type
, &ad
)) {
3046 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3048 /* check ISOC99 compound literal */
3050 /* data is allocated locally by default */
3055 /* all except arrays are lvalues */
3056 if (!(type
.t
& VT_ARRAY
))
3057 r
|= lvalue_type(type
.t
);
3058 memset(&ad
, 0, sizeof(AttributeDef
));
3059 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3064 } else if (tok
== '{') {
3065 /* save all registers */
3067 /* statement expression : we do not accept break/continue
3068 inside as GCC does */
3069 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3084 /* functions names must be treated as function pointers,
3085 except for unary '&' and sizeof. Since we consider that
3086 functions are not lvalues, we only have to handle it
3087 there and in function calls. */
3088 /* arrays can also be used although they are not lvalues */
3089 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3090 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3092 mk_pointer(&vtop
->type
);
3098 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3100 boolean
.t
= VT_BOOL
;
3102 vtop
->c
.i
= !vtop
->c
.i
;
3103 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3104 vtop
->c
.i
= vtop
->c
.i
^ 1;
3107 vseti(VT_JMP
, gtst(1, 0));
3118 /* in order to force cast, we add zero */
3120 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3121 error("pointer not accepted for unary plus");
3131 parse_expr_type(&type
);
3135 size
= type_size(&type
, &align
);
3136 if (t
== TOK_SIZEOF
) {
3138 error("sizeof applied to an incomplete type");
3143 vtop
->type
.t
|= VT_UNSIGNED
;
3146 case TOK_builtin_types_compatible_p
:
3155 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3156 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3157 vpushi(is_compatible_types(&type1
, &type2
));
3160 case TOK_builtin_constant_p
:
3162 int saved_nocode_wanted
, res
;
3165 saved_nocode_wanted
= nocode_wanted
;
3168 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3170 nocode_wanted
= saved_nocode_wanted
;
3175 case TOK_builtin_frame_address
:
3180 if (tok
!= TOK_CINT
) {
3181 error("__builtin_frame_address only takes integers");
3184 error("TCC only supports __builtin_frame_address(0)");
3190 vset(&type
, VT_LOCAL
, 0);
3193 #ifdef TCC_TARGET_X86_64
3194 case TOK_builtin_malloc
:
3196 goto tok_identifier
;
3197 case TOK_builtin_free
:
3199 goto tok_identifier
;
3216 goto tok_identifier
;
3218 /* allow to take the address of a label */
3219 if (tok
< TOK_UIDENT
)
3220 expect("label identifier");
3221 s
= label_find(tok
);
3223 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3225 if (s
->r
== LABEL_DECLARED
)
3226 s
->r
= LABEL_FORWARD
;
3229 s
->type
.t
= VT_VOID
;
3230 mk_pointer(&s
->type
);
3231 s
->type
.t
|= VT_STATIC
;
3233 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3242 expect("identifier");
3246 error("'%s' undeclared", get_tok_str(t
, NULL
));
3247 /* for simple function calls, we tolerate undeclared
3248 external reference to int() function */
3249 if (tcc_state
->warn_implicit_function_declaration
)
3250 warning("implicit declaration of function '%s'",
3251 get_tok_str(t
, NULL
));
3252 s
= external_global_sym(t
, &func_old_type
, 0);
3254 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3255 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3256 /* if referencing an inline function, then we generate a
3257 symbol to it if not already done. It will have the
3258 effect to generate code for it at the end of the
3259 compilation unit. Inline function as always
3260 generated in the text section. */
3262 put_extern_sym(s
, text_section
, 0, 0);
3263 r
= VT_SYM
| VT_CONST
;
3267 vset(&s
->type
, r
, s
->c
);
3268 /* if forward reference, we must point to s */
3269 if (vtop
->r
& VT_SYM
) {
3276 /* post operations */
3278 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3281 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3284 if (tok
== TOK_ARROW
)
3286 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3290 /* expect pointer on structure */
3291 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3292 expect("struct or union");
3296 while ((s
= s
->next
) != NULL
) {
3301 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3302 /* add field offset to pointer */
3303 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3306 /* change type to field type, and set to lvalue */
3307 vtop
->type
= s
->type
;
3308 vtop
->type
.t
|= qualifiers
;
3309 /* an array is never an lvalue */
3310 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3311 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3312 /* if bound checking, the referenced pointer must be checked */
3313 if (tcc_state
->do_bounds_check
)
3314 vtop
->r
|= VT_MUSTBOUND
;
3317 } else if (tok
== '[') {
3323 } else if (tok
== '(') {
3329 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3330 /* pointer test (no array accepted) */
3331 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3332 vtop
->type
= *pointed_type(&vtop
->type
);
3333 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3337 expect("function pointer");
3340 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3342 /* get return type */
3345 sa
= s
->next
; /* first parameter */
3348 /* compute first implicit argument if a structure is returned */
3349 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3350 /* get some space for the returned structure */
3351 size
= type_size(&s
->type
, &align
);
3352 loc
= (loc
- size
) & -align
;
3354 ret
.r
= VT_LOCAL
| VT_LVAL
;
3355 /* pass it as 'int' to avoid structure arg passing
3357 vseti(VT_LOCAL
, loc
);
3362 /* return in register */
3363 if (is_float(ret
.type
.t
)) {
3364 ret
.r
= reg_fret(ret
.type
.t
);
3366 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3375 gfunc_param_typed(s
, sa
);
3385 error("too few arguments to function");
3387 if (!nocode_wanted
) {
3388 gfunc_call(nb_args
);
3390 vtop
-= (nb_args
+ 1);
3393 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3401 static void uneq(void)
3407 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
3408 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
3409 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
3424 static void expr_prod(void)
3429 while (tok
== '*' || tok
== '/' || tok
== '%') {
3437 static void expr_sum(void)
3442 while (tok
== '+' || tok
== '-') {
3450 static void expr_shift(void)
3455 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3463 static void expr_cmp(void)
3468 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3469 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3477 static void expr_cmpeq(void)
3482 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3490 static void expr_and(void)
3493 while (tok
== '&') {
3500 static void expr_xor(void)
3503 while (tok
== '^') {
3510 static void expr_or(void)
3513 while (tok
== '|') {
3520 /* XXX: fix this mess */
3521 static void expr_land_const(void)
3524 while (tok
== TOK_LAND
) {
3531 /* XXX: fix this mess */
3532 static void expr_lor_const(void)
3535 while (tok
== TOK_LOR
) {
3542 /* only used if non constant */
3543 static void expr_land(void)
3548 if (tok
== TOK_LAND
) {
3553 if (tok
!= TOK_LAND
) {
3563 static void expr_lor(void)
3568 if (tok
== TOK_LOR
) {
3573 if (tok
!= TOK_LOR
) {
3583 /* XXX: better constant handling */
3584 static void expr_eq(void)
3586 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
3588 CType type
, type1
, type2
;
3595 boolean
.t
= VT_BOOL
;
3601 if (tok
!= ':' || !gnu_ext
) {
3616 if (vtop
!= vstack
) {
3617 /* needed to avoid having different registers saved in
3619 if (is_float(vtop
->type
.t
)) {
3621 #ifdef TCC_TARGET_X86_64
3622 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3632 if (tok
== ':' && gnu_ext
) {
3640 sv
= *vtop
; /* save value to handle it later */
3641 vtop
--; /* no vpop so that FP stack is not flushed */
3649 bt1
= t1
& VT_BTYPE
;
3651 bt2
= t2
& VT_BTYPE
;
3652 /* cast operands to correct type according to ISOC rules */
3653 if (is_float(bt1
) || is_float(bt2
)) {
3654 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3655 type
.t
= VT_LDOUBLE
;
3656 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3661 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3662 /* cast to biggest op */
3664 /* convert to unsigned if it does not fit in a long long */
3665 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3666 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3667 type
.t
|= VT_UNSIGNED
;
3668 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3669 /* XXX: test pointer compatibility */
3671 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
3672 /* XXX: test function pointer compatibility */
3674 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
3675 /* XXX: test structure compatibility */
3677 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
3678 /* NOTE: as an extension, we accept void on only one side */
3681 /* integer operations */
3683 /* convert to unsigned if it does not fit in an integer */
3684 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3685 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3686 type
.t
|= VT_UNSIGNED
;
3689 /* now we convert second operand */
3691 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3694 if (is_float(type
.t
)) {
3696 #ifdef TCC_TARGET_X86_64
3697 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
3701 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
3702 /* for long longs, we use fixed registers to avoid having
3703 to handle a complicated move */
3708 /* this is horrible, but we must also convert first
3712 /* put again first value and cast it */
3715 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
3725 static void gexpr(void)
3736 /* parse an expression and return its type without any side effect. */
3737 static void expr_type(CType
*type
)
3739 int saved_nocode_wanted
;
3741 saved_nocode_wanted
= nocode_wanted
;
3746 nocode_wanted
= saved_nocode_wanted
;
3749 /* parse a unary expression and return its type without any side
3751 static void unary_type(CType
*type
)
3763 /* parse a constant expression and return value in vtop. */
3764 static void expr_const1(void)
3773 /* parse an integer constant and return its value. */
3774 static int expr_const(void)
3778 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
3779 expect("constant expression");
3785 /* return the label token if current token is a label, otherwise
3787 static int is_label(void)
3791 /* fast test first */
3792 if (tok
< TOK_UIDENT
)
3794 /* no need to save tokc because tok is an identifier */
3801 unget_tok(last_tok
);
3806 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
3807 int case_reg
, int is_expr
)
3812 /* generate line number info */
3813 if (tcc_state
->do_debug
&&
3814 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
3815 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
3817 last_line_num
= file
->line_num
;
3821 /* default return value is (void) */
3823 vtop
->type
.t
= VT_VOID
;
3826 if (tok
== TOK_IF
) {
3833 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3835 if (c
== TOK_ELSE
) {
3839 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
3840 gsym(d
); /* patch else jmp */
3843 } else if (tok
== TOK_WHILE
) {
3851 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
3855 } else if (tok
== '{') {
3859 /* record local declaration stack position */
3861 llabel
= local_label_stack
;
3862 /* handle local labels declarations */
3863 if (tok
== TOK_LABEL
) {
3866 if (tok
< TOK_UIDENT
)
3867 expect("label identifier");
3868 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
3878 while (tok
!= '}') {
3883 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
3886 /* pop locally defined labels */
3887 label_pop(&local_label_stack
, llabel
);
3888 /* pop locally defined symbols */
3890 /* XXX: this solution makes only valgrind happy...
3891 triggered by gcc.c-torture/execute/20000917-1.c */
3893 switch(vtop
->type
.t
& VT_BTYPE
) {
3898 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
3900 error("unsupported expression type");
3903 sym_pop(&local_stack
, s
);
3905 } else if (tok
== TOK_RETURN
) {
3909 gen_assign_cast(&func_vt
);
3910 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
3912 /* if returning structure, must copy it to implicit
3913 first pointer arg location */
3916 size
= type_size(&func_vt
,&align
);
3919 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
3923 loc
= (loc
- size
) & -4;
3926 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
3929 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
3931 vtop
->type
= int_type
;
3937 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
3940 /* copy structure value to pointer */
3945 } else if (is_float(func_vt
.t
)) {
3946 gv(rc_fret(func_vt
.t
));
3950 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3953 rsym
= gjmp(rsym
); /* jmp */
3954 } else if (tok
== TOK_BREAK
) {
3957 error("cannot break");
3958 *bsym
= gjmp(*bsym
);
3961 } else if (tok
== TOK_CONTINUE
) {
3964 error("cannot continue");
3965 *csym
= gjmp(*csym
);
3968 } else if (tok
== TOK_FOR
) {
3995 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4000 if (tok
== TOK_DO
) {
4005 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4016 if (tok
== TOK_SWITCH
) {
4020 /* XXX: other types than integer */
4021 case_reg
= gv(RC_INT
);
4025 b
= gjmp(0); /* jump to first case */
4027 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4028 /* if no default, jmp after switch */
4036 if (tok
== TOK_CASE
) {
4043 if (gnu_ext
&& tok
== TOK_DOTS
) {
4047 warning("empty case range");
4049 /* since a case is like a label, we must skip it with a jmp */
4056 *case_sym
= gtst(1, 0);
4059 *case_sym
= gtst(1, 0);
4063 *case_sym
= gtst(1, *case_sym
);
4068 goto block_after_label
;
4070 if (tok
== TOK_DEFAULT
) {
4076 error("too many 'default'");
4079 goto block_after_label
;
4081 if (tok
== TOK_GOTO
) {
4083 if (tok
== '*' && gnu_ext
) {
4087 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4090 } else if (tok
>= TOK_UIDENT
) {
4091 s
= label_find(tok
);
4092 /* put forward definition if needed */
4094 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4096 if (s
->r
== LABEL_DECLARED
)
4097 s
->r
= LABEL_FORWARD
;
4099 /* label already defined */
4100 if (s
->r
& LABEL_FORWARD
)
4101 s
->jnext
= gjmp(s
->jnext
);
4103 gjmp_addr(s
->jnext
);
4106 expect("label identifier");
4109 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4117 if (s
->r
== LABEL_DEFINED
)
4118 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4120 s
->r
= LABEL_DEFINED
;
4122 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4125 /* we accept this, but it is a mistake */
4128 warning("deprecated use of label at end of compound statement");
4132 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4135 /* expression case */
4150 /* t is the array or struct type. c is the array or struct
4151 address. cur_index/cur_field is the pointer to the current
4152 value. 'size_only' is true if only size info is needed (only used
4154 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4155 int *cur_index
, Sym
**cur_field
,
4159 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4165 if (gnu_ext
&& (l
= is_label()) != 0)
4167 while (tok
== '[' || tok
== '.') {
4169 if (!(type
->t
& VT_ARRAY
))
4170 expect("array type");
4173 index
= expr_const();
4174 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4175 expect("invalid index");
4176 if (tok
== TOK_DOTS
&& gnu_ext
) {
4178 index_last
= expr_const();
4179 if (index_last
< 0 ||
4180 (s
->c
>= 0 && index_last
>= s
->c
) ||
4182 expect("invalid index");
4188 *cur_index
= index_last
;
4189 type
= pointed_type(type
);
4190 elem_size
= type_size(type
, &align
);
4191 c
+= index
* elem_size
;
4192 /* NOTE: we only support ranges for last designator */
4193 nb_elems
= index_last
- index
+ 1;
4194 if (nb_elems
!= 1) {
4203 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4204 expect("struct/union type");
4217 /* XXX: fix this mess by using explicit storage field */
4219 type1
.t
|= (type
->t
& ~VT_TYPE
);
4233 if (type
->t
& VT_ARRAY
) {
4235 type
= pointed_type(type
);
4236 c
+= index
* type_size(type
, &align
);
4240 error("too many field init");
4241 /* XXX: fix this mess by using explicit storage field */
4243 type1
.t
|= (type
->t
& ~VT_TYPE
);
4248 decl_initializer(type
, sec
, c
, 0, size_only
);
4250 /* XXX: make it more general */
4251 if (!size_only
&& nb_elems
> 1) {
4252 unsigned long c_end
;
4257 error("range init not supported yet for dynamic storage");
4258 c_end
= c
+ nb_elems
* elem_size
;
4259 if (c_end
> sec
->data_allocated
)
4260 section_realloc(sec
, c_end
);
4261 src
= sec
->data
+ c
;
4263 for(i
= 1; i
< nb_elems
; i
++) {
4265 memcpy(dst
, src
, elem_size
);
4271 #define EXPR_CONST 1
4274 /* store a value or an expression directly in global data or in local array */
4275 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4276 int v
, int expr_type
)
4278 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4280 unsigned long long bit_mask
;
4288 /* compound literals must be allocated globally in this case */
4289 saved_global_expr
= global_expr
;
4292 global_expr
= saved_global_expr
;
4293 /* NOTE: symbols are accepted */
4294 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4295 error("initializer element is not constant");
4303 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4306 /* XXX: not portable */
4307 /* XXX: generate error if incorrect relocation */
4308 gen_assign_cast(&dtype
);
4309 bt
= type
->t
& VT_BTYPE
;
4310 /* we'll write at most 12 bytes */
4311 if (c
+ 12 > sec
->data_allocated
) {
4312 section_realloc(sec
, c
+ 12);
4314 ptr
= sec
->data
+ c
;
4315 /* XXX: make code faster ? */
4316 if (!(type
->t
& VT_BITFIELD
)) {
4321 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4322 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4323 bit_mask
= (1LL << bit_size
) - 1;
4325 if ((vtop
->r
& VT_SYM
) &&
4331 (bt
== VT_INT
&& bit_size
!= 32)))
4332 error("initializer element is not computable at load time");
4335 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4337 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4340 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4343 *(double *)ptr
= vtop
->c
.d
;
4346 *(long double *)ptr
= vtop
->c
.ld
;
4349 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4352 if (vtop
->r
& VT_SYM
) {
4353 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4355 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4360 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4367 /* put zeros for variable based init */
4368 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4371 /* nothing to do because globals are already set to zero */
4373 vpush_global_sym(&func_old_type
, TOK_memset
);
4381 /* 't' contains the type and storage info. 'c' is the offset of the
4382 object in section 'sec'. If 'sec' is NULL, it means stack based
4383 allocation. 'first' is true if array '{' must be read (multi
4384 dimension implicit array init handling). 'size_only' is true if
4385 size only evaluation is wanted (only for arrays). */
4386 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4387 int first
, int size_only
)
4389 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
4390 int size1
, align1
, expr_type
;
4394 if (type
->t
& VT_ARRAY
) {
4398 t1
= pointed_type(type
);
4399 size1
= type_size(t1
, &align1
);
4402 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4408 /* only parse strings here if correct type (otherwise: handle
4409 them as ((w)char *) expressions */
4410 if ((tok
== TOK_LSTR
&&
4411 #ifdef TCC_TARGET_PE
4412 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4414 (t1
->t
& VT_BTYPE
) == VT_INT
4416 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4417 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4422 /* compute maximum number of chars wanted */
4424 cstr_len
= cstr
->size
;
4426 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4429 if (n
>= 0 && nb
> (n
- array_length
))
4430 nb
= n
- array_length
;
4433 warning("initializer-string for array is too long");
4434 /* in order to go faster for common case (char
4435 string in global variable, we handle it
4437 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4438 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4442 ch
= ((unsigned char *)cstr
->data
)[i
];
4444 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4445 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4453 /* only add trailing zero if enough storage (no
4454 warning in this case since it is standard) */
4455 if (n
< 0 || array_length
< n
) {
4457 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4463 while (tok
!= '}') {
4464 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4465 if (n
>= 0 && index
>= n
)
4466 error("index too large");
4467 /* must put zero in holes (note that doing it that way
4468 ensures that it even works with designators) */
4469 if (!size_only
&& array_length
< index
) {
4470 init_putz(t1
, sec
, c
+ array_length
* size1
,
4471 (index
- array_length
) * size1
);
4474 if (index
> array_length
)
4475 array_length
= index
;
4476 /* special test for multi dimensional arrays (may not
4477 be strictly correct if designators are used at the
4479 if (index
>= n
&& no_oblock
)
4488 /* put zeros at the end */
4489 if (!size_only
&& n
>= 0 && array_length
< n
) {
4490 init_putz(t1
, sec
, c
+ array_length
* size1
,
4491 (n
- array_length
) * size1
);
4493 /* patch type size if needed */
4495 s
->c
= array_length
;
4496 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
4497 (sec
|| !first
|| tok
== '{')) {
4500 /* NOTE: the previous test is a specific case for automatic
4501 struct/union init */
4502 /* XXX: union needs only one init */
4504 /* XXX: this test is incorrect for local initializers
4505 beginning with ( without {. It would be much more difficult
4506 to do it correctly (ideally, the expression parser should
4507 be used in all cases) */
4513 while (tok
== '(') {
4517 if (!parse_btype(&type1
, &ad1
))
4519 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4521 if (!is_assignable_types(type
, &type1
))
4522 error("invalid type for cast");
4527 if (first
|| tok
== '{') {
4536 while (tok
!= '}') {
4537 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
4539 if (!size_only
&& array_length
< index
) {
4540 init_putz(type
, sec
, c
+ array_length
,
4541 index
- array_length
);
4543 index
= index
+ type_size(&f
->type
, &align1
);
4544 if (index
> array_length
)
4545 array_length
= index
;
4547 /* gr: skip fields from same union - ugly. */
4549 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4550 /* test for same offset */
4551 if (f
->next
->c
!= f
->c
)
4553 /* if yes, test for bitfield shift */
4554 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
4555 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4556 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4557 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4558 if (bit_pos_1
!= bit_pos_2
)
4565 if (no_oblock
&& f
== NULL
)
4571 /* put zeros at the end */
4572 if (!size_only
&& array_length
< n
) {
4573 init_putz(type
, sec
, c
+ array_length
,
4582 } else if (tok
== '{') {
4584 decl_initializer(type
, sec
, c
, first
, size_only
);
4586 } else if (size_only
) {
4587 /* just skip expression */
4589 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
4593 else if (tok
== ')')
4598 /* currently, we always use constant expression for globals
4599 (may change for scripting case) */
4600 expr_type
= EXPR_CONST
;
4602 expr_type
= EXPR_ANY
;
4603 init_putv(type
, sec
, c
, 0, expr_type
);
4607 /* parse an initializer for type 't' if 'has_init' is non zero, and
4608 allocate space in local or global data space ('r' is either
4609 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4610 variable 'v' of scope 'scope' is declared before initializers are
4611 parsed. If 'v' is zero, then a reference to the new object is put
4612 in the value stack. If 'has_init' is 2, a special parsing is done
4613 to handle string constants. */
4614 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
4615 int has_init
, int v
, int scope
)
4617 int size
, align
, addr
, data_offset
;
4619 ParseState saved_parse_state
= {0};
4620 TokenString init_str
;
4623 size
= type_size(type
, &align
);
4624 /* If unknown size, we must evaluate it before
4625 evaluating initializers because
4626 initializers can generate global data too
4627 (e.g. string pointers or ISOC99 compound
4628 literals). It also simplifies local
4629 initializers handling */
4630 tok_str_new(&init_str
);
4633 error("unknown type size");
4634 /* get all init string */
4635 if (has_init
== 2) {
4636 /* only get strings */
4637 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4638 tok_str_add_tok(&init_str
);
4643 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
4645 error("unexpected end of file in initializer");
4646 tok_str_add_tok(&init_str
);
4649 else if (tok
== '}') {
4659 tok_str_add(&init_str
, -1);
4660 tok_str_add(&init_str
, 0);
4663 save_parse_state(&saved_parse_state
);
4665 macro_ptr
= init_str
.str
;
4667 decl_initializer(type
, NULL
, 0, 1, 1);
4668 /* prepare second initializer parsing */
4669 macro_ptr
= init_str
.str
;
4672 /* if still unknown size, error */
4673 size
= type_size(type
, &align
);
4675 error("unknown type size");
4677 /* take into account specified alignment if bigger */
4679 if (ad
->aligned
> align
)
4680 align
= ad
->aligned
;
4681 } else if (ad
->packed
) {
4684 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
4686 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
))
4688 loc
= (loc
- size
) & -align
;
4690 /* handles bounds */
4691 /* XXX: currently, since we do only one pass, we cannot track
4692 '&' operators, so we add only arrays */
4693 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
4694 unsigned long *bounds_ptr
;
4695 /* add padding between regions */
4697 /* then add local bound info */
4698 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
4699 bounds_ptr
[0] = addr
;
4700 bounds_ptr
[1] = size
;
4703 /* local variable */
4704 sym_push(v
, type
, r
, addr
);
4706 /* push local reference */
4707 vset(type
, r
, addr
);
4713 if (v
&& scope
== VT_CONST
) {
4714 /* see if the symbol was already defined */
4717 if (!is_compatible_types(&sym
->type
, type
))
4718 error("incompatible types for redefinition of '%s'",
4719 get_tok_str(v
, NULL
));
4720 if (sym
->type
.t
& VT_EXTERN
) {
4721 /* if the variable is extern, it was not allocated */
4722 sym
->type
.t
&= ~VT_EXTERN
;
4723 /* set array size if it was ommited in extern
4725 if ((sym
->type
.t
& VT_ARRAY
) &&
4726 sym
->type
.ref
->c
< 0 &&
4728 sym
->type
.ref
->c
= type
->ref
->c
;
4730 /* we accept several definitions of the same
4731 global variable. this is tricky, because we
4732 must play with the SHN_COMMON type of the symbol */
4733 /* XXX: should check if the variable was already
4734 initialized. It is incorrect to initialized it
4736 /* no init data, we won't add more to the symbol */
4743 /* allocate symbol in corresponding section */
4748 else if (tcc_state
->nocommon
)
4752 data_offset
= sec
->data_offset
;
4753 data_offset
= (data_offset
+ align
- 1) & -align
;
4755 /* very important to increment global pointer at this time
4756 because initializers themselves can create new initializers */
4757 data_offset
+= size
;
4758 /* add padding if bound check */
4759 if (tcc_state
->do_bounds_check
)
4761 sec
->data_offset
= data_offset
;
4762 /* allocate section space to put the data */
4763 if (sec
->sh_type
!= SHT_NOBITS
&&
4764 data_offset
> sec
->data_allocated
)
4765 section_realloc(sec
, data_offset
);
4766 /* align section if needed */
4767 if (align
> sec
->sh_addralign
)
4768 sec
->sh_addralign
= align
;
4770 addr
= 0; /* avoid warning */
4774 if (scope
!= VT_CONST
|| !sym
) {
4775 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
4777 /* update symbol definition */
4779 put_extern_sym(sym
, sec
, addr
, size
);
4782 /* put a common area */
4783 put_extern_sym(sym
, NULL
, align
, size
);
4784 /* XXX: find a nicer way */
4785 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
4786 esym
->st_shndx
= SHN_COMMON
;
4791 /* push global reference */
4792 sym
= get_sym_ref(type
, sec
, addr
, size
);
4794 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4798 /* handles bounds now because the symbol must be defined
4799 before for the relocation */
4800 if (tcc_state
->do_bounds_check
) {
4801 unsigned long *bounds_ptr
;
4803 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
4804 /* then add global bound info */
4805 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
4806 bounds_ptr
[0] = 0; /* relocated */
4807 bounds_ptr
[1] = size
;
4811 decl_initializer(type
, sec
, addr
, 1, 0);
4812 /* restore parse state if needed */
4814 tok_str_free(init_str
.str
);
4815 restore_parse_state(&saved_parse_state
);
4821 void put_func_debug(Sym
*sym
)
4826 /* XXX: we put here a dummy type */
4827 snprintf(buf
, sizeof(buf
), "%s:%c1",
4828 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
4829 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
4830 cur_text_section
, sym
->c
);
4831 /* //gr gdb wants a line at the function */
4832 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
4837 /* parse an old style function declaration list */
4838 /* XXX: check multiple parameter */
4839 static void func_decl_list(Sym
*func_sym
)
4846 /* parse each declaration */
4847 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
4848 if (!parse_btype(&btype
, &ad
))
4849 expect("declaration list");
4850 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4851 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4853 /* we accept no variable after */
4857 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
4858 /* find parameter in function parameter list */
4861 if ((s
->v
& ~SYM_FIELD
) == v
)
4865 error("declaration for parameter '%s' but no such parameter",
4866 get_tok_str(v
, NULL
));
4868 /* check that no storage specifier except 'register' was given */
4869 if (type
.t
& VT_STORAGE
)
4870 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
4871 convert_parameter_type(&type
);
4872 /* we can add the type (NOTE: it could be local to the function) */
4874 /* accept other parameters */
4885 /* parse a function defined by symbol 'sym' and generate its code in
4886 'cur_text_section' */
4887 static void gen_function(Sym
*sym
)
4889 int saved_nocode_wanted
= nocode_wanted
;
4891 ind
= cur_text_section
->data_offset
;
4892 /* NOTE: we patch the symbol size later */
4893 put_extern_sym(sym
, cur_text_section
, ind
, 0);
4894 funcname
= get_tok_str(sym
->v
, NULL
);
4896 /* put debug symbol */
4897 if (tcc_state
->do_debug
)
4898 put_func_debug(sym
);
4899 /* push a dummy symbol to enable local sym storage */
4900 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4901 gfunc_prolog(&sym
->type
);
4903 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
4906 cur_text_section
->data_offset
= ind
;
4907 label_pop(&global_label_stack
, NULL
);
4908 sym_pop(&local_stack
, NULL
); /* reset local stack */
4909 /* end of function */
4910 /* patch symbol size */
4911 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
4913 if (tcc_state
->do_debug
) {
4914 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
4916 /* It's better to crash than to generate wrong code */
4917 cur_text_section
= NULL
;
4918 funcname
= ""; /* for safety */
4919 func_vt
.t
= VT_VOID
; /* for safety */
4920 ind
= 0; /* for safety */
4921 nocode_wanted
= saved_nocode_wanted
;
4924 static void gen_inline_functions(void)
4927 int *str
, inline_generated
, i
;
4928 struct InlineFunc
*fn
;
4930 /* iterate while inline function are referenced */
4932 inline_generated
= 0;
4933 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4934 fn
= tcc_state
->inline_fns
[i
];
4936 if (sym
&& sym
->c
) {
4937 /* the function was used: generate its code and
4938 convert it to a normal function */
4939 str
= fn
->token_str
;
4942 strcpy(file
->filename
, fn
->filename
);
4943 sym
->r
= VT_SYM
| VT_CONST
;
4944 sym
->type
.t
&= ~VT_INLINE
;
4948 cur_text_section
= text_section
;
4950 macro_ptr
= NULL
; /* fail safe */
4952 inline_generated
= 1;
4955 if (!inline_generated
)
4958 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
4959 fn
= tcc_state
->inline_fns
[i
];
4960 str
= fn
->token_str
;
4963 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
4966 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4967 static void decl(int l
)
4975 if (!parse_btype(&btype
, &ad
)) {
4976 /* skip redundant ';' */
4977 /* XXX: find more elegant solution */
4982 if (l
== VT_CONST
&&
4983 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
4984 /* global asm block */
4988 /* special test for old K&R protos without explicit int
4989 type. Only accepted when defining global data */
4990 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
4994 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
4995 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
4997 /* we accept no variable after */
5001 while (1) { /* iterate thru each declaration */
5003 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5007 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5008 printf("type = '%s'\n", buf
);
5011 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5012 /* if old style function prototype, we accept a
5015 if (sym
->c
== FUNC_OLD
)
5016 func_decl_list(sym
);
5021 error("cannot use local functions");
5022 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5023 expect("function definition");
5025 /* reject abstract declarators in function definition */
5027 while ((sym
= sym
->next
) != NULL
)
5028 if (!(sym
->v
& ~SYM_FIELD
))
5029 expect("identifier");
5031 /* XXX: cannot do better now: convert extern line to static inline */
5032 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5033 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5037 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5040 r
= sym
->type
.ref
->r
;
5041 /* use func_call from prototype if not defined */
5042 if (FUNC_CALL(r
) != FUNC_CDECL
5043 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5044 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5046 /* use export from prototype */
5048 FUNC_EXPORT(type
.ref
->r
) = 1;
5050 /* use static from prototype */
5051 if (sym
->type
.t
& VT_STATIC
)
5052 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5054 if (!is_compatible_types(&sym
->type
, &type
)) {
5056 error("incompatible types for redefinition of '%s'",
5057 get_tok_str(v
, NULL
));
5059 /* if symbol is already defined, then put complete type */
5062 /* put function symbol */
5063 sym
= global_identifier_push(v
, type
.t
, 0);
5064 sym
->type
.ref
= type
.ref
;
5067 /* static inline functions are just recorded as a kind
5068 of macro. Their code will be emitted at the end of
5069 the compilation unit only if they are used */
5070 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5071 (VT_INLINE
| VT_STATIC
)) {
5072 TokenString func_str
;
5074 struct InlineFunc
*fn
;
5075 const char *filename
;
5077 tok_str_new(&func_str
);
5083 error("unexpected end of file");
5084 tok_str_add_tok(&func_str
);
5089 } else if (t
== '}') {
5091 if (block_level
== 0)
5095 tok_str_add(&func_str
, -1);
5096 tok_str_add(&func_str
, 0);
5097 filename
= file
? file
->filename
: "";
5098 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5099 strcpy(fn
->filename
, filename
);
5101 fn
->token_str
= func_str
.str
;
5102 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5105 /* compute text section */
5106 cur_text_section
= ad
.section
;
5107 if (!cur_text_section
)
5108 cur_text_section
= text_section
;
5109 sym
->r
= VT_SYM
| VT_CONST
;
5114 if (btype
.t
& VT_TYPEDEF
) {
5115 /* save typedefed type */
5116 /* XXX: test storage specifiers ? */
5117 sym
= sym_push(v
, &type
, 0, 0);
5118 sym
->type
.t
|= VT_TYPEDEF
;
5119 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5120 /* external function definition */
5121 /* specific case for func_call attribute */
5123 type
.ref
->r
= ad
.func_attr
;
5124 external_sym(v
, &type
, 0);
5126 /* not lvalue if array */
5128 if (!(type
.t
& VT_ARRAY
))
5129 r
|= lvalue_type(type
.t
);
5130 has_init
= (tok
== '=');
5131 if ((btype
.t
& VT_EXTERN
) ||
5132 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5133 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5134 /* external variable */
5135 /* NOTE: as GCC, uninitialized global static
5136 arrays of null size are considered as
5138 #ifdef TCC_TARGET_PE
5139 if (FUNC_IMPORT(ad
.func_attr
))
5140 type
.t
|= VT_IMPORT
;
5142 external_sym(v
, &type
, r
);
5144 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5145 if (type
.t
& VT_STATIC
)
5151 decl_initializer_alloc(&type
, &ad
, r
,