1 /* TCC runtime library.
2 Parts of this code are (c) 2002 Fabrice Bellard
4 Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc.
6 This file is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 This file is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA.
33 #define W_TYPE_SIZE 32
34 #define BITS_PER_UNIT 8
37 typedef unsigned int UWtype
;
38 typedef unsigned int USItype
;
39 typedef long long DWtype
;
40 typedef unsigned long long UDWtype
;
52 typedef long double XFtype
;
53 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
54 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
56 /* the following deal with IEEE single-precision numbers */
58 #define SIGNBIT 0x80000000
59 #define HIDDEN (1 << 23)
60 #define SIGN(fp) ((fp) & SIGNBIT)
61 #define EXP(fp) (((fp) >> 23) & 0xFF)
62 #define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN)
63 #define PACK(s,e,m) ((s) | ((e) << 23) | (m))
65 /* the following deal with IEEE double-precision numbers */
67 #define HIDDEND (1 << 20)
68 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
69 #define SIGND(fp) ((fp.l.upper) & SIGNBIT)
70 #define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
72 #define HIDDEND_LL ((long long)1 << 52)
73 #define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL)
74 #define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m))
76 /* the following deal with x86 long double-precision numbers */
77 #define EXCESSLD 16382
78 #define EXPLD(fp) (fp.l.upper & 0x7fff)
79 #define SIGNLD(fp) ((fp.l.upper) & 0x8000)
85 unsigned long long lower
;
111 /* XXX: we don't support several builtin supports for now */
112 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM)
114 /* XXX: use gcc/tcc intrinsic ? */
115 #if defined(TCC_TARGET_I386)
116 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
117 __asm__ ("subl %5,%1\n\tsbbl %3,%0" \
118 : "=r" ((USItype) (sh)), \
119 "=&r" ((USItype) (sl)) \
120 : "0" ((USItype) (ah)), \
121 "g" ((USItype) (bh)), \
122 "1" ((USItype) (al)), \
123 "g" ((USItype) (bl)))
124 #define umul_ppmm(w1, w0, u, v) \
126 : "=a" ((USItype) (w0)), \
127 "=d" ((USItype) (w1)) \
128 : "%0" ((USItype) (u)), \
129 "rm" ((USItype) (v)))
130 #define udiv_qrnnd(q, r, n1, n0, dv) \
132 : "=a" ((USItype) (q)), \
133 "=d" ((USItype) (r)) \
134 : "0" ((USItype) (n0)), \
135 "1" ((USItype) (n1)), \
136 "rm" ((USItype) (dv)))
137 #define count_leading_zeros(count, x) \
140 __asm__ ("bsrl %1,%0" \
141 : "=r" (__cbtmp) : "rm" ((USItype) (x))); \
142 (count) = __cbtmp ^ 31; \
145 #error unsupported CPU type
148 /* most of this code is taken from libgcc2.c from gcc */
150 static UDWtype
__udivmoddi4 (UDWtype n
, UDWtype d
, UDWtype
*rp
)
155 UWtype d0
, d1
, n0
, n1
, n2
;
167 #if !defined(UDIV_NEEDS_NORMALIZATION)
174 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
177 /* Remainder in n0. */
184 d0
= 1 / d0
; /* Divide intentionally by zero. */
186 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
187 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
189 /* Remainder in n0. */
200 #else /* UDIV_NEEDS_NORMALIZATION */
208 count_leading_zeros (bm
, d0
);
212 /* Normalize, i.e. make the most significant bit of the
216 n1
= (n1
<< bm
) | (n0
>> (W_TYPE_SIZE
- bm
));
220 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
223 /* Remainder in n0 >> bm. */
230 d0
= 1 / d0
; /* Divide intentionally by zero. */
232 count_leading_zeros (bm
, d0
);
236 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
237 conclude (the most significant bit of n1 is set) /\ (the
238 leading quotient digit q1 = 1).
240 This special case is necessary, not an optimization.
241 (Shifts counts of W_TYPE_SIZE are undefined.) */
250 b
= W_TYPE_SIZE
- bm
;
254 n1
= (n1
<< bm
) | (n0
>> b
);
257 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
262 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
264 /* Remainder in n0 >> bm. */
274 #endif /* UDIV_NEEDS_NORMALIZATION */
285 /* Remainder in n1n0. */
297 count_leading_zeros (bm
, d1
);
300 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
301 conclude (the most significant bit of n1 is set) /\ (the
302 quotient digit q0 = 0 or 1).
304 This special case is necessary, not an optimization. */
306 /* The condition on the next line takes advantage of that
307 n1 >= d1 (true due to program flow). */
308 if (n1
> d1
|| n0
>= d0
)
311 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
330 b
= W_TYPE_SIZE
- bm
;
332 d1
= (d1
<< bm
) | (d0
>> b
);
335 n1
= (n1
<< bm
) | (n0
>> b
);
338 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
339 umul_ppmm (m1
, m0
, q0
, d0
);
341 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
344 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
349 /* Remainder in (n1n0 - m1m0) >> bm. */
352 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
353 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
354 rr
.s
.high
= n1
>> bm
;
366 #define __negdi2(a) (-(a))
368 long long __divdi3(long long u
, long long v
)
379 uu
.ll
= __negdi2 (uu
.ll
);
383 vv
.ll
= __negdi2 (vv
.ll
);
385 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) 0);
391 long long __moddi3(long long u
, long long v
)
402 uu
.ll
= __negdi2 (uu
.ll
);
405 vv
.ll
= __negdi2 (vv
.ll
);
407 __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) &w
);
413 unsigned long long __udivdi3(unsigned long long u
, unsigned long long v
)
415 return __udivmoddi4 (u
, v
, (UDWtype
*) 0);
418 unsigned long long __umoddi3(unsigned long long u
, unsigned long long v
)
422 __udivmoddi4 (u
, v
, &w
);
426 /* XXX: fix tcc's code generator to do this instead */
427 long long __ashrdi3(long long a
, int b
)
433 u
.s
.low
= u
.s
.high
>> (b
- 32);
434 u
.s
.high
= u
.s
.high
>> 31;
436 u
.s
.low
= ((unsigned)u
.s
.low
>> b
) | (u
.s
.high
<< (32 - b
));
437 u
.s
.high
= u
.s
.high
>> b
;
445 /* XXX: fix tcc's code generator to do this instead */
446 unsigned long long __lshrdi3(unsigned long long a
, int b
)
452 u
.s
.low
= (unsigned)u
.s
.high
>> (b
- 32);
455 u
.s
.low
= ((unsigned)u
.s
.low
>> b
) | (u
.s
.high
<< (32 - b
));
456 u
.s
.high
= (unsigned)u
.s
.high
>> b
;
464 /* XXX: fix tcc's code generator to do this instead */
465 long long __ashldi3(long long a
, int b
)
471 u
.s
.high
= (unsigned)u
.s
.low
<< (b
- 32);
474 u
.s
.high
= ((unsigned)u
.s
.high
<< b
) | ((unsigned)u
.s
.low
>> (32 - b
));
475 u
.s
.low
= (unsigned)u
.s
.low
<< b
;
483 #endif /* !__x86_64__ */
485 /* XXX: fix tcc's code generator to do this instead */
486 float __floatundisf(unsigned long long a
)
492 if (uu
.s
.high
>= 0) {
496 r
+= 18446744073709551616.0;
501 double __floatundidf(unsigned long long a
)
507 if (uu
.s
.high
>= 0) {
508 return (double)uu
.ll
;
511 r
+= 18446744073709551616.0;
516 long double __floatundixf(unsigned long long a
)
522 if (uu
.s
.high
>= 0) {
523 return (long double)uu
.ll
;
526 r
+= 18446744073709551616.0;
527 return (long double)r
;
531 unsigned long long __fixunssfdi (float a1
)
533 register union float_long fl1
;
535 register unsigned long l
;
542 exp
= EXP (fl1
.l
) - EXCESS
- 24;
546 return (unsigned long long)-1;
548 return (unsigned long long)l
<< exp
;
555 unsigned long long __fixunsdfdi (double a1
)
557 register union double_long dl1
;
559 register unsigned long long l
;
566 exp
= EXPD (dl1
) - EXCESSD
- 53;
571 return (unsigned long long)-1;
580 unsigned long long __fixunsxfdi (long double a1
)
582 register union ldouble_long dl1
;
584 register unsigned long long l
;
588 if (dl1
.l
.lower
== 0 && dl1
.l
.upper
== 0)
591 exp
= EXPLD (dl1
) - EXCESSLD
- 64;
596 return (unsigned long long)-1;
603 long long __fixsfdi (float a1
)
605 long long ret
; int s
;
606 ret
= __fixunssfdi((s
= a1
>= 0) ? a1
: -a1
);
607 return s
? ret
: -ret
;
610 long long __fixdfdi (double a1
)
612 long long ret
; int s
;
613 ret
= __fixunsdfdi((s
= a1
>= 0) ? a1
: -a1
);
614 return s
? ret
: -ret
;
617 long long __fixxfdi (long double a1
)
619 long long ret
; int s
;
620 ret
= __fixunsxfdi((s
= a1
>= 0) ? a1
: -a1
);
621 return s
? ret
: -ret
;
624 #if defined(TCC_TARGET_X86_64) && !defined(_WIN64)
631 /* Avoid including stdlib.h because it is not easily available when
633 extern void *malloc(unsigned long long);
634 void *memset(void *s
, int c
, size_t n
);
635 extern void free(void*);
636 extern void abort(void);
640 __va_gen_reg
, __va_float_reg
, __va_stack
643 //This should be in sync with the declaration on our include/stdarg.h
644 /* GCC compatible definition of va_list. */
646 unsigned int gp_offset
;
647 unsigned int fp_offset
;
649 unsigned int overflow_offset
;
650 char *overflow_arg_area
;
660 void __va_start(__va_list_struct
*ap
, void *fp
)
662 memset(ap
, 0, sizeof(__va_list_struct
));
663 *ap
= *(__va_list_struct
*)((char *)fp
- 16);
664 ap
->overflow_arg_area
= (char *)fp
+ ap
->overflow_offset
;
665 ap
->reg_save_area
= (char *)fp
- 176 - 16;
668 void *__va_arg(__va_list_struct
*ap
,
669 enum __va_arg_type arg_type
,
672 size
= (size
+ 7) & ~7;
673 align
= (align
+ 7) & ~7;
676 if (ap
->gp_offset
< 48) {
678 return ap
->reg_save_area
+ ap
->gp_offset
- 8;
681 goto use_overflow_area
;
684 if (ap
->fp_offset
< 128 + 48) {
686 return ap
->reg_save_area
+ ap
->fp_offset
- 16;
689 goto use_overflow_area
;
693 ap
->overflow_arg_area
+= size
;
694 ap
->overflow_arg_area
= (char*)((intptr_t)(ap
->overflow_arg_area
+ align
- 1) & -(intptr_t)align
);
695 return ap
->overflow_arg_area
- size
;
699 fprintf(stderr
, "unknown ABI type for __va_arg\n");
705 #endif /* __x86_64__ */
707 /* Flushing for tccrun */
708 #if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_I386)
710 void __clear_cache(char *beginning
, char *end
)
714 #elif defined(TCC_TARGET_ARM)
718 #include <sys/syscall.h>
721 void __clear_cache(char *beginning
, char *end
)
723 /* __ARM_NR_cacheflush is kernel private and should not be used in user space.
724 * However, there is no ARM asm parser in tcc so we use it for now */
726 syscall(__ARM_NR_cacheflush
, beginning
, end
, 0);
728 __asm__ ("push {r7}\n\t"
729 "mov r7, #0xf0002\n\t"
738 #warning __clear_cache not defined for this architecture, avoid using tcc -run