1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
32 /* It is incorrect to include config.h here, because this file is being
33 compiled for the target, and hence definitions concerning only the host
39 /* Don't use `fancy_abort' here even if config.h says to use it. */
46 #ifdef DECLARE_LIBRARY_RENAMES
47 DECLARE_LIBRARY_RENAMES
50 #if defined (L_negdi2)
60 w
.s
.high
= -uu
.s
.high
- ((UWtype
) w
.s
.low
> 0);
68 __addvsi3 (Wtype a
, Wtype b
)
74 if (b
>= 0 ? w
< a
: w
> a
)
83 __addvdi3 (DWtype a
, DWtype b
)
89 if (b
>= 0 ? w
< a
: w
> a
)
98 __subvsi3 (Wtype a
, Wtype b
)
101 return __addvsi3 (a
, (-b
));
107 if (b
>= 0 ? w
> a
: w
< a
)
117 __subvdi3 (DWtype a
, DWtype b
)
126 if (b
>= 0 ? w
> a
: w
< a
)
136 __mulvsi3 (Wtype a
, Wtype b
)
142 if (((a
>= 0) == (b
>= 0)) ? w
< 0 : w
> 0)
157 if (a
>= 0 ? w
> 0 : w
< 0)
172 if (a
>= 0 ? w
> 0 : w
< 0)
221 __mulvdi3 (DWtype u
, DWtype v
)
227 if (((u
>= 0) == (v
>= 0)) ? w
< 0 : w
> 0)
235 /* Unless shift functions are defined whith full ANSI prototypes,
236 parameter b will be promoted to int if word_type is smaller than an int. */
239 __lshrdi3 (DWtype u
, word_type b
)
250 bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
254 w
.s
.low
= (UWtype
) uu
.s
.high
>> -bm
;
258 UWtype carries
= (UWtype
) uu
.s
.high
<< bm
;
260 w
.s
.high
= (UWtype
) uu
.s
.high
>> b
;
261 w
.s
.low
= ((UWtype
) uu
.s
.low
>> b
) | carries
;
270 __ashldi3 (DWtype u
, word_type b
)
281 bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
285 w
.s
.high
= (UWtype
) uu
.s
.low
<< -bm
;
289 UWtype carries
= (UWtype
) uu
.s
.low
>> bm
;
291 w
.s
.low
= (UWtype
) uu
.s
.low
<< b
;
292 w
.s
.high
= ((UWtype
) uu
.s
.high
<< b
) | carries
;
301 __ashrdi3 (DWtype u
, word_type b
)
312 bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
315 /* w.s.high = 1..1 or 0..0 */
316 w
.s
.high
= uu
.s
.high
>> (sizeof (Wtype
) * BITS_PER_UNIT
- 1);
317 w
.s
.low
= uu
.s
.high
>> -bm
;
321 UWtype carries
= (UWtype
) uu
.s
.high
<< bm
;
323 w
.s
.high
= uu
.s
.high
>> b
;
324 w
.s
.low
= ((UWtype
) uu
.s
.low
>> b
) | carries
;
336 UWtype word
, count
, add
;
340 word
= uu
.s
.low
, add
= 0;
341 else if (uu
.s
.high
!= 0)
342 word
= uu
.s
.high
, add
= BITS_PER_UNIT
* sizeof (Wtype
);
346 count_trailing_zeros (count
, word
);
347 return count
+ add
+ 1;
353 __muldi3 (DWtype u
, DWtype v
)
361 w
.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
);
362 w
.s
.high
+= ((UWtype
) uu
.s
.low
* (UWtype
) vv
.s
.high
363 + (UWtype
) uu
.s
.high
* (UWtype
) vv
.s
.low
);
369 #if (defined (L_udivdi3) || defined (L_divdi3) || \
370 defined (L_umoddi3) || defined (L_moddi3))
371 #if defined (sdiv_qrnnd)
372 #define L_udiv_w_sdiv
377 #if defined (sdiv_qrnnd)
378 #if (defined (L_udivdi3) || defined (L_divdi3) || \
379 defined (L_umoddi3) || defined (L_moddi3))
383 __udiv_w_sdiv (UWtype
*rp
, UWtype a1
, UWtype a0
, UWtype d
)
390 if (a1
< d
- a1
- (a0
>> (W_TYPE_SIZE
- 1)))
392 /* dividend, divisor, and quotient are nonnegative */
393 sdiv_qrnnd (q
, r
, a1
, a0
, d
);
397 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
398 sub_ddmmss (c1
, c0
, a1
, a0
, d
>> 1, d
<< (W_TYPE_SIZE
- 1));
399 /* Divide (c1*2^32 + c0) by d */
400 sdiv_qrnnd (q
, r
, c1
, c0
, d
);
401 /* Add 2^31 to quotient */
402 q
+= (UWtype
) 1 << (W_TYPE_SIZE
- 1);
407 b1
= d
>> 1; /* d/2, between 2^30 and 2^31 - 1 */
408 c1
= a1
>> 1; /* A/2 */
409 c0
= (a1
<< (W_TYPE_SIZE
- 1)) + (a0
>> 1);
411 if (a1
< b1
) /* A < 2^32*b1, so A/2 < 2^31*b1 */
413 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
415 r
= 2*r
+ (a0
& 1); /* Remainder from A/(2*b1) */
432 else if (c1
< b1
) /* So 2^31 <= (A/2)/b1 < 2^32 */
435 c0
= ~c0
; /* logical NOT */
437 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
439 q
= ~q
; /* (A/2)/b1 */
442 r
= 2*r
+ (a0
& 1); /* A/(2*b1) */
460 else /* Implies c1 = b1 */
461 { /* Hence a1 = d - 1 = 2*b1 - 1 */
479 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
481 __udiv_w_sdiv (UWtype
*rp
__attribute__ ((__unused__
)),
482 UWtype a1
__attribute__ ((__unused__
)),
483 UWtype a0
__attribute__ ((__unused__
)),
484 UWtype d
__attribute__ ((__unused__
)))
491 #if (defined (L_udivdi3) || defined (L_divdi3) || \
492 defined (L_umoddi3) || defined (L_moddi3))
497 const UQItype __clz_tab
[] =
499 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
500 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
501 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
502 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
503 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
504 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
505 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
506 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
512 #if (defined (L_udivdi3) || defined (L_divdi3) || \
513 defined (L_umoddi3) || defined (L_moddi3))
517 __udivmoddi4 (UDWtype n
, UDWtype d
, UDWtype
*rp
)
522 UWtype d0
, d1
, n0
, n1
, n2
;
534 #if !UDIV_NEEDS_NORMALIZATION
541 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
544 /* Remainder in n0. */
551 d0
= 1 / d0
; /* Divide intentionally by zero. */
553 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
554 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
556 /* Remainder in n0. */
567 #else /* UDIV_NEEDS_NORMALIZATION */
575 count_leading_zeros (bm
, d0
);
579 /* Normalize, i.e. make the most significant bit of the
583 n1
= (n1
<< bm
) | (n0
>> (W_TYPE_SIZE
- bm
));
587 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
590 /* Remainder in n0 >> bm. */
597 d0
= 1 / d0
; /* Divide intentionally by zero. */
599 count_leading_zeros (bm
, d0
);
603 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
604 conclude (the most significant bit of n1 is set) /\ (the
605 leading quotient digit q1 = 1).
607 This special case is necessary, not an optimization.
608 (Shifts counts of W_TYPE_SIZE are undefined.) */
617 b
= W_TYPE_SIZE
- bm
;
621 n1
= (n1
<< bm
) | (n0
>> b
);
624 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
629 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
631 /* Remainder in n0 >> bm. */
641 #endif /* UDIV_NEEDS_NORMALIZATION */
652 /* Remainder in n1n0. */
664 count_leading_zeros (bm
, d1
);
667 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
668 conclude (the most significant bit of n1 is set) /\ (the
669 quotient digit q0 = 0 or 1).
671 This special case is necessary, not an optimization. */
673 /* The condition on the next line takes advantage of that
674 n1 >= d1 (true due to program flow). */
675 if (n1
> d1
|| n0
>= d0
)
678 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
697 b
= W_TYPE_SIZE
- bm
;
699 d1
= (d1
<< bm
) | (d0
>> b
);
702 n1
= (n1
<< bm
) | (n0
>> b
);
705 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
706 umul_ppmm (m1
, m0
, q0
, d0
);
708 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
711 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
716 /* Remainder in (n1n0 - m1m0) >> bm. */
719 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
720 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
721 rr
.s
.high
= n1
>> bm
;
736 __divdi3 (DWtype u
, DWtype v
)
752 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) 0);
762 __moddi3 (DWtype u
, DWtype v
)
777 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
787 __umoddi3 (UDWtype u
, UDWtype v
)
791 (void) __udivmoddi4 (u
, v
, &w
);
799 __udivdi3 (UDWtype n
, UDWtype d
)
801 return __udivmoddi4 (n
, d
, (UDWtype
*) 0);
807 __cmpdi2 (DWtype a
, DWtype b
)
811 au
.ll
= a
, bu
.ll
= b
;
813 if (au
.s
.high
< bu
.s
.high
)
815 else if (au
.s
.high
> bu
.s
.high
)
817 if ((UWtype
) au
.s
.low
< (UWtype
) bu
.s
.low
)
819 else if ((UWtype
) au
.s
.low
> (UWtype
) bu
.s
.low
)
827 __ucmpdi2 (DWtype a
, DWtype b
)
831 au
.ll
= a
, bu
.ll
= b
;
833 if ((UWtype
) au
.s
.high
< (UWtype
) bu
.s
.high
)
835 else if ((UWtype
) au
.s
.high
> (UWtype
) bu
.s
.high
)
837 if ((UWtype
) au
.s
.low
< (UWtype
) bu
.s
.low
)
839 else if ((UWtype
) au
.s
.low
> (UWtype
) bu
.s
.low
)
845 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
846 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
847 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
850 __fixunstfDI (TFtype a
)
858 /* Compute high word of result, as a flonum. */
859 b
= (a
/ HIGH_WORD_COEFF
);
860 /* Convert that to fixed (but not to DWtype!),
861 and shift it into the high word. */
864 /* Remove high part from the TFtype, leaving the low part as flonum. */
866 /* Convert that to fixed (but not to DWtype!) and add it in.
867 Sometimes A comes out negative. This is significant, since
868 A has more bits than a long int does. */
877 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
882 return - __fixunstfDI (-a
);
883 return __fixunstfDI (a
);
887 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
888 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
889 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
892 __fixunsxfDI (XFtype a
)
900 /* Compute high word of result, as a flonum. */
901 b
= (a
/ HIGH_WORD_COEFF
);
902 /* Convert that to fixed (but not to DWtype!),
903 and shift it into the high word. */
906 /* Remove high part from the XFtype, leaving the low part as flonum. */
908 /* Convert that to fixed (but not to DWtype!) and add it in.
909 Sometimes A comes out negative. This is significant, since
910 A has more bits than a long int does. */
919 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
924 return - __fixunsxfDI (-a
);
925 return __fixunsxfDI (a
);
930 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
931 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
934 __fixunsdfDI (DFtype a
)
942 /* Compute high word of result, as a flonum. */
943 b
= (a
/ HIGH_WORD_COEFF
);
944 /* Convert that to fixed (but not to DWtype!),
945 and shift it into the high word. */
948 /* Remove high part from the DFtype, leaving the low part as flonum. */
950 /* Convert that to fixed (but not to DWtype!) and add it in.
951 Sometimes A comes out negative. This is significant, since
952 A has more bits than a long int does. */
966 return - __fixunsdfDI (-a
);
967 return __fixunsdfDI (a
);
972 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
973 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
976 __fixunssfDI (SFtype original_a
)
978 /* Convert the SFtype to a DFtype, because that is surely not going
979 to lose any bits. Some day someone else can write a faster version
980 that avoids converting to DFtype, and verify it really works right. */
981 DFtype a
= original_a
;
988 /* Compute high word of result, as a flonum. */
989 b
= (a
/ HIGH_WORD_COEFF
);
990 /* Convert that to fixed (but not to DWtype!),
991 and shift it into the high word. */
994 /* Remove high part from the DFtype, leaving the low part as flonum. */
996 /* Convert that to fixed (but not to DWtype!) and add it in.
997 Sometimes A comes out negative. This is significant, since
998 A has more bits than a long int does. */
1000 v
-= (UWtype
) (- a
);
1009 __fixsfdi (SFtype a
)
1012 return - __fixunssfDI (-a
);
1013 return __fixunssfDI (a
);
1017 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
1018 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1019 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1020 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1023 __floatdixf (DWtype u
)
1027 d
= (Wtype
) (u
>> WORD_SIZE
);
1028 d
*= HIGH_HALFWORD_COEFF
;
1029 d
*= HIGH_HALFWORD_COEFF
;
1030 d
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1036 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
1037 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1038 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1039 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1042 __floatditf (DWtype u
)
1046 d
= (Wtype
) (u
>> WORD_SIZE
);
1047 d
*= HIGH_HALFWORD_COEFF
;
1048 d
*= HIGH_HALFWORD_COEFF
;
1049 d
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1056 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1057 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1058 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1061 __floatdidf (DWtype u
)
1065 d
= (Wtype
) (u
>> WORD_SIZE
);
1066 d
*= HIGH_HALFWORD_COEFF
;
1067 d
*= HIGH_HALFWORD_COEFF
;
1068 d
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1075 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1076 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1077 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1079 #define DI_SIZE (sizeof (DWtype) * BITS_PER_UNIT)
1080 #define DF_SIZE DBL_MANT_DIG
1081 #define SF_SIZE FLT_MANT_DIG
1084 __floatdisf (DWtype u
)
1086 /* Do the calculation in DFmode
1087 so that we don't lose any of the precision of the high word
1088 while multiplying it. */
1091 /* Protect against double-rounding error.
1092 Represent any low-order bits, that might be truncated in DFmode,
1093 by a bit that won't be lost. The bit can go in anywhere below the
1094 rounding position of the SFmode. A fixed mask and bit position
1095 handles all usual configurations. It doesn't handle the case
1096 of 128-bit DImode, however. */
1097 if (DF_SIZE
< DI_SIZE
1098 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1100 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - DF_SIZE))
1101 if (! (- ((DWtype
) 1 << DF_SIZE
) < u
1102 && u
< ((DWtype
) 1 << DF_SIZE
)))
1104 if ((UDWtype
) u
& (REP_BIT
- 1))
1106 u
&= ~ (REP_BIT
- 1);
1111 f
= (Wtype
) (u
>> WORD_SIZE
);
1112 f
*= HIGH_HALFWORD_COEFF
;
1113 f
*= HIGH_HALFWORD_COEFF
;
1114 f
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1120 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1121 /* Reenable the normal types, in case limits.h needs them. */
1134 __fixunsxfSI (XFtype a
)
1136 if (a
>= - (DFtype
) Wtype_MIN
)
1137 return (Wtype
) (a
+ Wtype_MIN
) - Wtype_MIN
;
1143 /* Reenable the normal types, in case limits.h needs them. */
1156 __fixunsdfSI (DFtype a
)
1158 if (a
>= - (DFtype
) Wtype_MIN
)
1159 return (Wtype
) (a
+ Wtype_MIN
) - Wtype_MIN
;
1165 /* Reenable the normal types, in case limits.h needs them. */
1178 __fixunssfSI (SFtype a
)
1180 if (a
>= - (SFtype
) Wtype_MIN
)
1181 return (Wtype
) (a
+ Wtype_MIN
) - Wtype_MIN
;
1186 /* From here on down, the routines use normal data types. */
1188 #define SItype bogus_type
1189 #define USItype bogus_type
1190 #define DItype bogus_type
1191 #define UDItype bogus_type
1192 #define SFtype bogus_type
1193 #define DFtype bogus_type
1211 /* Like bcmp except the sign is meaningful.
1212 Result is negative if S1 is less than S2,
1213 positive if S1 is greater, 0 if S1 and S2 are equal. */
1216 __gcc_bcmp (const unsigned char *s1
, const unsigned char *s2
, size_t size
)
1220 unsigned char c1
= *s1
++, c2
= *s2
++;
1230 /* __eprintf used to be used by GCC's private version of <assert.h>.
1231 We no longer provide that header, but this routine remains in libgcc.a
1232 for binary backward compatibility. Note that it is not included in
1233 the shared version of libgcc. */
1235 #ifndef inhibit_libc
1237 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1241 __eprintf (const char *string
, const char *expression
,
1242 unsigned int line
, const char *filename
)
1244 fprintf (stderr
, string
, expression
, line
, filename
);
1254 struct bb_function_info
{
1260 /* Structure emitted by --profile-arcs */
1264 const char *filename
;
1269 /* Older GCC's did not emit these fields. */
1271 struct bb_function_info
*function_infos
;
1274 #ifndef inhibit_libc
1276 /* Arc profile dumper. Requires atexit and stdio. */
1278 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1281 #include "gcov-io.h"
1283 #ifdef TARGET_HAS_F_SETLKW
1288 /* Chain of per-object file bb structures. */
1289 static struct bb
*bb_head
;
1291 /* Dump the coverage counts. We merge with existing counts when
1292 possible, to avoid growing the .da files ad infinitum. */
1295 __bb_exit_func (void)
1299 gcov_type program_sum
= 0;
1300 gcov_type program_max
= 0;
1301 long program_arcs
= 0;
1302 gcov_type merged_sum
= 0;
1303 gcov_type merged_max
= 0;
1304 long merged_arcs
= 0;
1306 #if defined (TARGET_HAS_F_SETLKW)
1307 struct flock s_flock
;
1309 s_flock
.l_type
= F_WRLCK
;
1310 s_flock
.l_whence
= SEEK_SET
;
1311 s_flock
.l_start
= 0;
1312 s_flock
.l_len
= 0; /* Until EOF. */
1313 s_flock
.l_pid
= getpid ();
1316 /* Non-merged stats for this program. */
1317 for (ptr
= bb_head
; ptr
; ptr
= ptr
->next
)
1319 for (i
= 0; i
< ptr
->ncounts
; i
++)
1321 program_sum
+= ptr
->counts
[i
];
1323 if (ptr
->counts
[i
] > program_max
)
1324 program_max
= ptr
->counts
[i
];
1326 program_arcs
+= ptr
->ncounts
;
1329 for (ptr
= bb_head
; ptr
; ptr
= ptr
->next
)
1332 gcov_type object_max
= 0;
1333 gcov_type object_sum
= 0;
1334 long object_functions
= 0;
1337 struct bb_function_info
*fn_info
;
1338 gcov_type
*count_ptr
;
1340 /* Open for modification */
1341 da_file
= fopen (ptr
->filename
, "r+b");
1347 /* Try for appending */
1348 da_file
= fopen (ptr
->filename
, "ab");
1349 /* Some old systems might not allow the 'b' mode modifier.
1350 Therefore, try to open without it. This can lead to a
1351 race condition so that when you delete and re-create the
1352 file, the file might be opened in text mode, but then,
1353 you shouldn't delete the file in the first place. */
1355 da_file
= fopen (ptr
->filename
, "a");
1360 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1366 #if defined (TARGET_HAS_F_SETLKW)
1367 /* After a fork, another process might try to read and/or write
1368 the same file simultanously. So if we can, lock the file to
1369 avoid race conditions. */
1370 while (fcntl (fileno (da_file
), F_SETLKW
, &s_flock
)
1374 for (fn_info
= ptr
->function_infos
; fn_info
->arc_count
!= -1; fn_info
++)
1379 /* Merge data from file. */
1384 (__read_long (&tmp_long
, da_file
, 4) || tmp_long
!= -123l)
1385 /* functions in object file. */
1386 || (__read_long (&tmp_long
, da_file
, 4)
1387 || tmp_long
!= object_functions
)
1388 /* extension block, skipped */
1389 || (__read_long (&tmp_long
, da_file
, 4)
1390 || fseek (da_file
, tmp_long
, SEEK_CUR
)))
1393 fprintf (stderr
, "arc profiling: Error merging output file %s.\n",
1399 /* Merge execution counts for each function. */
1400 count_ptr
= ptr
->counts
;
1402 for (fn_info
= ptr
->function_infos
; fn_info
->arc_count
!= -1;
1405 if (/* function name delim */
1406 (__read_long (&tmp_long
, da_file
, 4)
1408 /* function name length */
1409 || (__read_long (&tmp_long
, da_file
, 4)
1410 || tmp_long
!= (long) strlen (fn_info
->name
))
1412 || fseek (da_file
, ((tmp_long
+ 1) + 3) & ~3, SEEK_CUR
)
1413 /* function name delim */
1414 || (__read_long (&tmp_long
, da_file
, 4)
1418 if (/* function checksum */
1419 (__read_long (&tmp_long
, da_file
, 4)
1420 || tmp_long
!= fn_info
->checksum
)
1422 || (__read_long (&tmp_long
, da_file
, 4)
1423 || tmp_long
!= fn_info
->arc_count
))
1426 for (i
= fn_info
->arc_count
; i
> 0; i
--, count_ptr
++)
1427 if (__read_gcov_type (&tmp_gcov
, da_file
, 8))
1430 *count_ptr
+= tmp_gcov
;
1433 fseek (da_file
, 0, SEEK_SET
);
1436 /* Calculate the per-object statistics. */
1437 for (i
= 0; i
< ptr
->ncounts
; i
++)
1439 object_sum
+= ptr
->counts
[i
];
1441 if (ptr
->counts
[i
] > object_max
)
1442 object_max
= ptr
->counts
[i
];
1444 merged_sum
+= object_sum
;
1445 if (merged_max
< object_max
)
1446 merged_max
= object_max
;
1447 merged_arcs
+= ptr
->ncounts
;
1449 /* Write out the data. */
1451 __write_long (-123, da_file
, 4)
1452 /* number of functions in object file. */
1453 || __write_long (object_functions
, da_file
, 4)
1454 /* length of extra data in bytes. */
1455 || __write_long ((4 + 8 + 8) + (4 + 8 + 8), da_file
, 4)
1457 /* whole program statistics. If merging write per-object
1458 now, rewrite later */
1459 /* number of instrumented arcs. */
1460 || __write_long (merging
? ptr
->ncounts
: program_arcs
, da_file
, 4)
1461 /* sum of counters. */
1462 || __write_gcov_type (merging
? object_sum
: program_sum
, da_file
, 8)
1463 /* maximal counter. */
1464 || __write_gcov_type (merging
? object_max
: program_max
, da_file
, 8)
1466 /* per-object statistics. */
1467 /* number of counters. */
1468 || __write_long (ptr
->ncounts
, da_file
, 4)
1469 /* sum of counters. */
1470 || __write_gcov_type (object_sum
, da_file
, 8)
1471 /* maximal counter. */
1472 || __write_gcov_type (object_max
, da_file
, 8))
1475 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1481 /* Write execution counts for each function. */
1482 count_ptr
= ptr
->counts
;
1484 for (fn_info
= ptr
->function_infos
; fn_info
->arc_count
!= -1;
1487 if (__write_gcov_string (fn_info
->name
,
1488 strlen (fn_info
->name
), da_file
, -1)
1489 || __write_long (fn_info
->checksum
, da_file
, 4)
1490 || __write_long (fn_info
->arc_count
, da_file
, 4))
1493 for (i
= fn_info
->arc_count
; i
> 0; i
--, count_ptr
++)
1494 if (__write_gcov_type (*count_ptr
, da_file
, 8))
1495 goto write_error
; /* RIP Edsger Dijkstra */
1499 if (fclose (da_file
))
1501 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1505 if (error
|| !merging
)
1509 /* Upate whole program statistics. */
1510 for (ptr
= bb_head
; ptr
; ptr
= ptr
->next
)
1515 da_file
= fopen (ptr
->filename
, "r+b");
1518 fprintf (stderr
, "arc profiling: Cannot reopen %s.\n",
1523 #if defined (TARGET_HAS_F_SETLKW)
1524 while (fcntl (fileno (da_file
), F_SETLKW
, &s_flock
)
1529 if (fseek (da_file
, 4 * 3, SEEK_SET
)
1530 /* number of instrumented arcs. */
1531 || __write_long (merged_arcs
, da_file
, 4)
1532 /* sum of counters. */
1533 || __write_gcov_type (merged_sum
, da_file
, 8)
1534 /* maximal counter. */
1535 || __write_gcov_type (merged_max
, da_file
, 8))
1536 fprintf (stderr
, "arc profiling: Error updating program header %s.\n",
1538 if (fclose (da_file
))
1539 fprintf (stderr
, "arc profiling: Error reclosing %s\n",
1544 /* Add a new object file onto the bb chain. Invoked automatically
1545 when running an object file's global ctors. */
1548 __bb_init_func (struct bb
*blocks
)
1550 if (blocks
->zero_word
)
1553 /* Initialize destructor and per-thread data. */
1555 atexit (__bb_exit_func
);
1557 /* Set up linked list. */
1558 blocks
->zero_word
= 1;
1559 blocks
->next
= bb_head
;
1563 /* Called before fork or exec - write out profile information gathered so
1564 far and reset it to zero. This avoids duplication or loss of the
1565 profile information gathered so far. */
1568 __bb_fork_func (void)
1573 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1576 for (i
= ptr
->ncounts
- 1; i
>= 0; i
--)
1581 #endif /* not inhibit_libc */
1584 #ifdef L_clear_cache
1585 /* Clear part of an instruction cache. */
1587 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
1590 __clear_cache (char *beg
__attribute__((__unused__
)),
1591 char *end
__attribute__((__unused__
)))
1593 #ifdef CLEAR_INSN_CACHE
1594 CLEAR_INSN_CACHE (beg
, end
);
1596 #ifdef INSN_CACHE_SIZE
1597 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
1598 static int initialized
;
1602 typedef (*function_ptr
) (void);
1604 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
1605 /* It's cheaper to clear the whole cache.
1606 Put in a series of jump instructions so that calling the beginning
1607 of the cache will clear the whole thing. */
1611 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
1612 & -INSN_CACHE_LINE_WIDTH
);
1613 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
1615 while (ptr
< end_ptr
)
1617 *(INSTRUCTION_TYPE
*)ptr
1618 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
1619 ptr
+= INSN_CACHE_LINE_WIDTH
;
1621 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
1626 /* Call the beginning of the sequence. */
1627 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
1628 & -INSN_CACHE_LINE_WIDTH
))
1631 #else /* Cache is large. */
1635 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
1636 & -INSN_CACHE_LINE_WIDTH
);
1638 while (ptr
< (int) array
+ sizeof array
)
1640 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
1641 ptr
+= INSN_CACHE_LINE_WIDTH
;
1647 /* Find the location in array that occupies the same cache line as BEG. */
1649 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
1650 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
1651 & -INSN_CACHE_PLANE_SIZE
)
1654 /* Compute the cache alignment of the place to stop clearing. */
1655 #if 0 /* This is not needed for gcc's purposes. */
1656 /* If the block to clear is bigger than a cache plane,
1657 we clear the entire cache, and OFFSET is already correct. */
1658 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
1660 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
1661 & -INSN_CACHE_LINE_WIDTH
)
1662 & (INSN_CACHE_PLANE_SIZE
- 1));
1664 #if INSN_CACHE_DEPTH > 1
1665 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
1666 if (end_addr
<= start_addr
)
1667 end_addr
+= INSN_CACHE_PLANE_SIZE
;
1669 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
1671 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
1672 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
1674 while (addr
!= stop
)
1676 /* Call the return instruction at ADDR. */
1677 ((function_ptr
) addr
) ();
1679 addr
+= INSN_CACHE_LINE_WIDTH
;
1682 #else /* just one plane */
1685 /* Call the return instruction at START_ADDR. */
1686 ((function_ptr
) start_addr
) ();
1688 start_addr
+= INSN_CACHE_LINE_WIDTH
;
1690 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
1691 #endif /* just one plane */
1692 #endif /* Cache is large */
1693 #endif /* Cache exists */
1694 #endif /* CLEAR_INSN_CACHE */
1697 #endif /* L_clear_cache */
1701 /* Jump to a trampoline, loading the static chain address. */
1703 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
1716 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
1720 mprotect (char *addr
, int len
, int prot
)
1737 if (VirtualProtect (addr
, len
, np
, &op
))
1743 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
1745 #ifdef TRANSFER_FROM_TRAMPOLINE
1746 TRANSFER_FROM_TRAMPOLINE
1751 #include <sys/signal.h>
1754 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
1755 so define it here, because we need it in __clear_insn_cache below */
1756 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
1757 hence we enable this stuff only if MCT_TEXT is #define'd. */
1772 /* Clear instruction cache so we can call trampolines on stack.
1773 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
1776 __clear_insn_cache (void)
1781 /* Preserve errno, because users would be surprised to have
1782 errno changing without explicitly calling any system-call. */
1785 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
1786 No need to use an address derived from _start or %sp, as 0 works also. */
1787 memctl(0, 4096, MCT_TEXT
);
1792 #endif /* __sysV68__ */
1793 #endif /* L_trampoline */
1798 #include "gbl-ctors.h"
1799 /* Some systems use __main in a way incompatible with its use in gcc, in these
1800 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
1801 give the same symbol without quotes for an alternative entry point. You
1802 must define both, or neither. */
1804 #define NAME__MAIN "__main"
1805 #define SYMBOL__MAIN __main
1808 #ifdef INIT_SECTION_ASM_OP
1809 #undef HAS_INIT_SECTION
1810 #define HAS_INIT_SECTION
1813 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
1815 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
1816 code to run constructors. In that case, we need to handle EH here, too. */
1818 #ifdef EH_FRAME_SECTION_NAME
1819 #include "unwind-dw2-fde.h"
1820 extern unsigned char __EH_FRAME_BEGIN__
[];
1823 /* Run all the global destructors on exit from the program. */
1826 __do_global_dtors (void)
1828 #ifdef DO_GLOBAL_DTORS_BODY
1829 DO_GLOBAL_DTORS_BODY
;
1831 static func_ptr
*p
= __DTOR_LIST__
+ 1;
1838 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
1840 static int completed
= 0;
1844 __deregister_frame_info (__EH_FRAME_BEGIN__
);
1851 #ifndef HAS_INIT_SECTION
1852 /* Run all the global constructors on entry to the program. */
1855 __do_global_ctors (void)
1857 #ifdef EH_FRAME_SECTION_NAME
1859 static struct object object
;
1860 __register_frame_info (__EH_FRAME_BEGIN__
, &object
);
1863 DO_GLOBAL_CTORS_BODY
;
1864 atexit (__do_global_dtors
);
1866 #endif /* no HAS_INIT_SECTION */
1868 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
1869 /* Subroutine called automatically by `main'.
1870 Compiling a global function named `main'
1871 produces an automatic call to this function at the beginning.
1873 For many systems, this routine calls __do_global_ctors.
1874 For systems which support a .init section we use the .init section
1875 to run __do_global_ctors, so we need not do anything here. */
1880 /* Support recursive calls to `main': run initializers just once. */
1881 static int initialized
;
1885 __do_global_ctors ();
1888 #endif /* no HAS_INIT_SECTION or INVOKE__main */
1890 #endif /* L__main */
1891 #endif /* __CYGWIN__ */
1895 #include "gbl-ctors.h"
1897 /* Provide default definitions for the lists of constructors and
1898 destructors, so that we don't get linker errors. These symbols are
1899 intentionally bss symbols, so that gld and/or collect will provide
1900 the right values. */
1902 /* We declare the lists here with two elements each,
1903 so that they are valid empty lists if no other definition is loaded.
1905 If we are using the old "set" extensions to have the gnu linker
1906 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
1907 must be in the bss/common section.
1909 Long term no port should use those extensions. But many still do. */
1910 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
1911 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
1912 func_ptr __CTOR_LIST__
[2] = {0, 0};
1913 func_ptr __DTOR_LIST__
[2] = {0, 0};
1915 func_ptr __CTOR_LIST__
[2];
1916 func_ptr __DTOR_LIST__
[2];
1918 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
1919 #endif /* L_ctors */
1923 #include "gbl-ctors.h"
1931 static func_ptr
*atexit_chain
= 0;
1932 static long atexit_chain_length
= 0;
1933 static volatile long last_atexit_chain_slot
= -1;
1936 atexit (func_ptr func
)
1938 if (++last_atexit_chain_slot
== atexit_chain_length
)
1940 atexit_chain_length
+= 32;
1942 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
1943 * sizeof (func_ptr
));
1945 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
1946 * sizeof (func_ptr
));
1949 atexit_chain_length
= 0;
1950 last_atexit_chain_slot
= -1;
1955 atexit_chain
[last_atexit_chain_slot
] = func
;
1959 extern void _cleanup (void);
1960 extern void _exit (int) __attribute__ ((__noreturn__
));
1967 for ( ; last_atexit_chain_slot
-- >= 0; )
1969 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
1970 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
1972 free (atexit_chain
);
1985 /* Simple; we just need a wrapper for ON_EXIT. */
1987 atexit (func_ptr func
)
1989 return ON_EXIT (func
);
1992 #endif /* ON_EXIT */
1993 #endif /* NEED_ATEXIT */