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 Free Software Foundation, Inc.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
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 GNU CC is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with GNU CC; see the file COPYING. If not, write to
29 the Free Software Foundation, 59 Temple Place - Suite 330,
30 Boston, MA 02111-1307, USA. */
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
42 /* Don't use `fancy_abort' here even if config.h says to use it. */
49 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
50 #if defined (L_divdi3) || defined (L_moddi3)
62 w
.s
.high
= -uu
.s
.high
- ((UWtype
) w
.s
.low
> 0);
70 __addvsi3 (Wtype a
, Wtype b
)
76 if (b
>= 0 ? w
< a
: w
> a
)
85 __addvdi3 (DWtype a
, DWtype b
)
91 if (b
>= 0 ? w
< a
: w
> a
)
100 __subvsi3 (Wtype a
, Wtype b
)
103 return __addvsi3 (a
, (-b
));
109 if (b
>= 0 ? w
> a
: w
< a
)
119 __subvdi3 (DWtype a
, DWtype b
)
128 if (b
>= 0 ? w
> a
: w
< a
)
138 __mulvsi3 (Wtype a
, Wtype b
)
144 if (((a
>= 0) == (b
>= 0)) ? w
< 0 : w
> 0)
159 if (a
>= 0 ? w
> 0 : w
< 0)
174 if (a
>= 0 ? w
> 0 : w
< 0)
223 __mulvdi3 (DWtype u
, DWtype v
)
229 if (((u
>= 0) == (v
>= 0)) ? w
< 0 : w
> 0)
237 /* Unless shift functions are defined whith full ANSI prototypes,
238 parameter b will be promoted to int if word_type is smaller than an int. */
241 __lshrdi3 (DWtype u
, word_type b
)
252 bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
256 w
.s
.low
= (UWtype
) uu
.s
.high
>> -bm
;
260 UWtype carries
= (UWtype
) uu
.s
.high
<< bm
;
262 w
.s
.high
= (UWtype
) uu
.s
.high
>> b
;
263 w
.s
.low
= ((UWtype
) uu
.s
.low
>> b
) | carries
;
272 __ashldi3 (DWtype u
, word_type b
)
283 bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
287 w
.s
.high
= (UWtype
) uu
.s
.low
<< -bm
;
291 UWtype carries
= (UWtype
) uu
.s
.low
>> bm
;
293 w
.s
.low
= (UWtype
) uu
.s
.low
<< b
;
294 w
.s
.high
= ((UWtype
) uu
.s
.high
<< b
) | carries
;
303 __ashrdi3 (DWtype u
, word_type b
)
314 bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
317 /* w.s.high = 1..1 or 0..0 */
318 w
.s
.high
= uu
.s
.high
>> (sizeof (Wtype
) * BITS_PER_UNIT
- 1);
319 w
.s
.low
= uu
.s
.high
>> -bm
;
323 UWtype carries
= (UWtype
) uu
.s
.high
<< bm
;
325 w
.s
.high
= uu
.s
.high
>> b
;
326 w
.s
.low
= ((UWtype
) uu
.s
.low
>> b
) | carries
;
338 UWtype word
, count
, add
;
342 word
= uu
.s
.low
, add
= 0;
343 else if (uu
.s
.high
!= 0)
344 word
= uu
.s
.high
, add
= BITS_PER_UNIT
* sizeof (Wtype
);
348 count_trailing_zeros (count
, word
);
349 return count
+ add
+ 1;
355 __muldi3 (DWtype u
, DWtype v
)
363 w
.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
);
364 w
.s
.high
+= ((UWtype
) uu
.s
.low
* (UWtype
) vv
.s
.high
365 + (UWtype
) uu
.s
.high
* (UWtype
) vv
.s
.low
);
372 #if defined (sdiv_qrnnd)
374 __udiv_w_sdiv (UWtype
*rp
, UWtype a1
, UWtype a0
, UWtype d
)
381 if (a1
< d
- a1
- (a0
>> (W_TYPE_SIZE
- 1)))
383 /* dividend, divisor, and quotient are nonnegative */
384 sdiv_qrnnd (q
, r
, a1
, a0
, d
);
388 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
389 sub_ddmmss (c1
, c0
, a1
, a0
, d
>> 1, d
<< (W_TYPE_SIZE
- 1));
390 /* Divide (c1*2^32 + c0) by d */
391 sdiv_qrnnd (q
, r
, c1
, c0
, d
);
392 /* Add 2^31 to quotient */
393 q
+= (UWtype
) 1 << (W_TYPE_SIZE
- 1);
398 b1
= d
>> 1; /* d/2, between 2^30 and 2^31 - 1 */
399 c1
= a1
>> 1; /* A/2 */
400 c0
= (a1
<< (W_TYPE_SIZE
- 1)) + (a0
>> 1);
402 if (a1
< b1
) /* A < 2^32*b1, so A/2 < 2^31*b1 */
404 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
406 r
= 2*r
+ (a0
& 1); /* Remainder from A/(2*b1) */
423 else if (c1
< b1
) /* So 2^31 <= (A/2)/b1 < 2^32 */
426 c0
= ~c0
; /* logical NOT */
428 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
430 q
= ~q
; /* (A/2)/b1 */
433 r
= 2*r
+ (a0
& 1); /* A/(2*b1) */
451 else /* Implies c1 = b1 */
452 { /* Hence a1 = d - 1 = 2*b1 - 1 */
470 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
472 __udiv_w_sdiv (UWtype
*rp
__attribute__ ((__unused__
)),
473 UWtype a1
__attribute__ ((__unused__
)),
474 UWtype a0
__attribute__ ((__unused__
)),
475 UWtype d
__attribute__ ((__unused__
)))
482 #if (defined (L_udivdi3) || defined (L_divdi3) || \
483 defined (L_umoddi3) || defined (L_moddi3))
488 const UQItype __clz_tab
[] =
490 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,
491 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,
492 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,
493 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,
494 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,
495 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,
496 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,
497 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,
503 #if (defined (L_udivdi3) || defined (L_divdi3) || \
504 defined (L_umoddi3) || defined (L_moddi3))
508 __udivmoddi4 (UDWtype n
, UDWtype d
, UDWtype
*rp
)
513 UWtype d0
, d1
, n0
, n1
, n2
;
525 #if !UDIV_NEEDS_NORMALIZATION
532 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
535 /* Remainder in n0. */
542 d0
= 1 / d0
; /* Divide intentionally by zero. */
544 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
545 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
547 /* Remainder in n0. */
558 #else /* UDIV_NEEDS_NORMALIZATION */
566 count_leading_zeros (bm
, d0
);
570 /* Normalize, i.e. make the most significant bit of the
574 n1
= (n1
<< bm
) | (n0
>> (W_TYPE_SIZE
- bm
));
578 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
581 /* Remainder in n0 >> bm. */
588 d0
= 1 / d0
; /* Divide intentionally by zero. */
590 count_leading_zeros (bm
, d0
);
594 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
595 conclude (the most significant bit of n1 is set) /\ (the
596 leading quotient digit q1 = 1).
598 This special case is necessary, not an optimization.
599 (Shifts counts of W_TYPE_SIZE are undefined.) */
608 b
= W_TYPE_SIZE
- bm
;
612 n1
= (n1
<< bm
) | (n0
>> b
);
615 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
620 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
622 /* Remainder in n0 >> bm. */
632 #endif /* UDIV_NEEDS_NORMALIZATION */
643 /* Remainder in n1n0. */
655 count_leading_zeros (bm
, d1
);
658 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
659 conclude (the most significant bit of n1 is set) /\ (the
660 quotient digit q0 = 0 or 1).
662 This special case is necessary, not an optimization. */
664 /* The condition on the next line takes advantage of that
665 n1 >= d1 (true due to program flow). */
666 if (n1
> d1
|| n0
>= d0
)
669 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
688 b
= W_TYPE_SIZE
- bm
;
690 d1
= (d1
<< bm
) | (d0
>> b
);
693 n1
= (n1
<< bm
) | (n0
>> b
);
696 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
697 umul_ppmm (m1
, m0
, q0
, d0
);
699 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
702 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
707 /* Remainder in (n1n0 - m1m0) >> bm. */
710 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
711 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
712 rr
.s
.high
= n1
>> bm
;
727 __divdi3 (DWtype u
, DWtype v
)
738 uu
.ll
= __negdi2 (uu
.ll
);
741 vv
.ll
= __negdi2 (vv
.ll
);
743 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) 0);
753 __moddi3 (DWtype u
, DWtype v
)
764 uu
.ll
= __negdi2 (uu
.ll
);
766 vv
.ll
= __negdi2 (vv
.ll
);
768 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
778 __umoddi3 (UDWtype u
, UDWtype v
)
782 (void) __udivmoddi4 (u
, v
, &w
);
790 __udivdi3 (UDWtype n
, UDWtype d
)
792 return __udivmoddi4 (n
, d
, (UDWtype
*) 0);
798 __cmpdi2 (DWtype a
, DWtype b
)
802 au
.ll
= a
, bu
.ll
= b
;
804 if (au
.s
.high
< bu
.s
.high
)
806 else if (au
.s
.high
> bu
.s
.high
)
808 if ((UWtype
) au
.s
.low
< (UWtype
) bu
.s
.low
)
810 else if ((UWtype
) au
.s
.low
> (UWtype
) bu
.s
.low
)
818 __ucmpdi2 (DWtype a
, DWtype b
)
822 au
.ll
= a
, bu
.ll
= b
;
824 if ((UWtype
) au
.s
.high
< (UWtype
) bu
.s
.high
)
826 else if ((UWtype
) au
.s
.high
> (UWtype
) bu
.s
.high
)
828 if ((UWtype
) au
.s
.low
< (UWtype
) bu
.s
.low
)
830 else if ((UWtype
) au
.s
.low
> (UWtype
) bu
.s
.low
)
836 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
837 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
838 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
841 __fixunstfDI (TFtype a
)
849 /* Compute high word of result, as a flonum. */
850 b
= (a
/ HIGH_WORD_COEFF
);
851 /* Convert that to fixed (but not to DWtype!),
852 and shift it into the high word. */
855 /* Remove high part from the TFtype, leaving the low part as flonum. */
857 /* Convert that to fixed (but not to DWtype!) and add it in.
858 Sometimes A comes out negative. This is significant, since
859 A has more bits than a long int does. */
868 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
873 return - __fixunstfDI (-a
);
874 return __fixunstfDI (a
);
878 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
879 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
880 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
883 __fixunsxfDI (XFtype a
)
891 /* Compute high word of result, as a flonum. */
892 b
= (a
/ HIGH_WORD_COEFF
);
893 /* Convert that to fixed (but not to DWtype!),
894 and shift it into the high word. */
897 /* Remove high part from the XFtype, leaving the low part as flonum. */
899 /* Convert that to fixed (but not to DWtype!) and add it in.
900 Sometimes A comes out negative. This is significant, since
901 A has more bits than a long int does. */
910 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
915 return - __fixunsxfDI (-a
);
916 return __fixunsxfDI (a
);
921 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
922 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
925 __fixunsdfDI (DFtype a
)
933 /* Compute high word of result, as a flonum. */
934 b
= (a
/ HIGH_WORD_COEFF
);
935 /* Convert that to fixed (but not to DWtype!),
936 and shift it into the high word. */
939 /* Remove high part from the DFtype, leaving the low part as flonum. */
941 /* Convert that to fixed (but not to DWtype!) and add it in.
942 Sometimes A comes out negative. This is significant, since
943 A has more bits than a long int does. */
957 return - __fixunsdfDI (-a
);
958 return __fixunsdfDI (a
);
963 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
964 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
967 __fixunssfDI (SFtype original_a
)
969 /* Convert the SFtype to a DFtype, because that is surely not going
970 to lose any bits. Some day someone else can write a faster version
971 that avoids converting to DFtype, and verify it really works right. */
972 DFtype a
= original_a
;
979 /* Compute high word of result, as a flonum. */
980 b
= (a
/ HIGH_WORD_COEFF
);
981 /* Convert that to fixed (but not to DWtype!),
982 and shift it into the high word. */
985 /* Remove high part from the DFtype, leaving the low part as flonum. */
987 /* Convert that to fixed (but not to DWtype!) and add it in.
988 Sometimes A comes out negative. This is significant, since
989 A has more bits than a long int does. */
1000 __fixsfdi (SFtype a
)
1003 return - __fixunssfDI (-a
);
1004 return __fixunssfDI (a
);
1008 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
1009 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1010 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1011 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1014 __floatdixf (DWtype u
)
1018 d
= (Wtype
) (u
>> WORD_SIZE
);
1019 d
*= HIGH_HALFWORD_COEFF
;
1020 d
*= HIGH_HALFWORD_COEFF
;
1021 d
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1027 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
1028 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1029 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1030 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1033 __floatditf (DWtype u
)
1037 d
= (Wtype
) (u
>> WORD_SIZE
);
1038 d
*= HIGH_HALFWORD_COEFF
;
1039 d
*= HIGH_HALFWORD_COEFF
;
1040 d
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1047 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1048 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1049 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1052 __floatdidf (DWtype u
)
1056 d
= (Wtype
) (u
>> WORD_SIZE
);
1057 d
*= HIGH_HALFWORD_COEFF
;
1058 d
*= HIGH_HALFWORD_COEFF
;
1059 d
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1066 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1067 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1068 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1069 #define DI_SIZE (sizeof (DWtype) * BITS_PER_UNIT)
1071 /* Define codes for all the float formats that we know of. Note
1072 that this is copied from real.h. */
1074 #define UNKNOWN_FLOAT_FORMAT 0
1075 #define IEEE_FLOAT_FORMAT 1
1076 #define VAX_FLOAT_FORMAT 2
1077 #define IBM_FLOAT_FORMAT 3
1079 /* Default to IEEE float if not specified. Nearly all machines use it. */
1080 #ifndef HOST_FLOAT_FORMAT
1081 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1084 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1089 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1094 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1100 __floatdisf (DWtype u
)
1102 /* Do the calculation in DFmode
1103 so that we don't lose any of the precision of the high word
1104 while multiplying it. */
1107 /* Protect against double-rounding error.
1108 Represent any low-order bits, that might be truncated in DFmode,
1109 by a bit that won't be lost. The bit can go in anywhere below the
1110 rounding position of the SFmode. A fixed mask and bit position
1111 handles all usual configurations. It doesn't handle the case
1112 of 128-bit DImode, however. */
1113 if (DF_SIZE
< DI_SIZE
1114 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1116 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - DF_SIZE))
1117 if (! (- ((DWtype
) 1 << DF_SIZE
) < u
1118 && u
< ((DWtype
) 1 << DF_SIZE
)))
1120 if ((UDWtype
) u
& (REP_BIT
- 1))
1124 f
= (Wtype
) (u
>> WORD_SIZE
);
1125 f
*= HIGH_HALFWORD_COEFF
;
1126 f
*= HIGH_HALFWORD_COEFF
;
1127 f
+= (UWtype
) (u
& (HIGH_WORD_COEFF
- 1));
1133 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1134 /* Reenable the normal types, in case limits.h needs them. */
1147 __fixunsxfSI (XFtype a
)
1149 if (a
>= - (DFtype
) Wtype_MIN
)
1150 return (Wtype
) (a
+ Wtype_MIN
) - Wtype_MIN
;
1156 /* Reenable the normal types, in case limits.h needs them. */
1169 __fixunsdfSI (DFtype a
)
1171 if (a
>= - (DFtype
) Wtype_MIN
)
1172 return (Wtype
) (a
+ Wtype_MIN
) - Wtype_MIN
;
1178 /* Reenable the normal types, in case limits.h needs them. */
1191 __fixunssfSI (SFtype a
)
1193 if (a
>= - (SFtype
) Wtype_MIN
)
1194 return (Wtype
) (a
+ Wtype_MIN
) - Wtype_MIN
;
1199 /* From here on down, the routines use normal data types. */
1201 #define SItype bogus_type
1202 #define USItype bogus_type
1203 #define DItype bogus_type
1204 #define UDItype bogus_type
1205 #define SFtype bogus_type
1206 #define DFtype bogus_type
1224 /* Like bcmp except the sign is meaningful.
1225 Result is negative if S1 is less than S2,
1226 positive if S1 is greater, 0 if S1 and S2 are equal. */
1229 __gcc_bcmp (const unsigned char *s1
, const unsigned char *s2
, size_t size
)
1233 unsigned char c1
= *s1
++, c2
= *s2
++;
1243 /* __eprintf used to be used by GCC's private version of <assert.h>.
1244 We no longer provide that header, but this routine remains in libgcc.a
1245 for binary backward compatibility. Note that it is not included in
1246 the shared version of libgcc. */
1248 #ifndef inhibit_libc
1250 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1254 __eprintf (const char *string
, const char *expression
,
1255 unsigned int line
, const char *filename
)
1257 fprintf (stderr
, string
, expression
, line
, filename
);
1267 /* Structure emitted by -a */
1271 const char *filename
;
1275 const unsigned long *addresses
;
1277 /* Older GCC's did not emit these fields. */
1279 const char **functions
;
1280 const long *line_nums
;
1281 const char **filenames
;
1285 #ifdef BLOCK_PROFILER_CODE
1288 #ifndef inhibit_libc
1290 /* Simple minded basic block profiling output dumper for
1291 systems that don't provide tcov support. At present,
1292 it requires atexit and stdio. */
1294 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1296 char *ctime
PARAMS ((const time_t *));
1298 #include "gbl-ctors.h"
1299 #include "gcov-io.h"
1301 #ifdef TARGET_HAS_F_SETLKW
1306 static struct bb
*bb_head
;
1308 static int num_digits (long value
, int base
) __attribute__ ((const));
1310 /* Return the number of digits needed to print a value */
1311 /* __inline__ */ static int num_digits (long value
, int base
)
1313 int minus
= (value
< 0 && base
!= 16);
1314 unsigned long v
= (minus
) ? -value
: value
;
1328 __bb_exit_func (void)
1330 FILE *da_file
, *file
;
1337 i
= strlen (bb_head
->filename
) - 3;
1339 if (!strcmp (bb_head
->filename
+i
, ".da"))
1341 /* Must be -fprofile-arcs not -a.
1342 Dump data in a form that gcov expects. */
1346 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1350 /* Make sure the output file exists -
1351 but don't clobber exiting data. */
1352 if ((da_file
= fopen (ptr
->filename
, "a")) != 0)
1355 /* Need to re-open in order to be able to write from the start. */
1356 da_file
= fopen (ptr
->filename
, "r+b");
1357 /* Some old systems might not allow the 'b' mode modifier.
1358 Therefore, try to open without it. This can lead to a race
1359 condition so that when you delete and re-create the file, the
1360 file might be opened in text mode, but then, you shouldn't
1361 delete the file in the first place. */
1363 da_file
= fopen (ptr
->filename
, "r+");
1366 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1371 /* After a fork, another process might try to read and/or write
1372 the same file simultanously. So if we can, lock the file to
1373 avoid race conditions. */
1374 #if defined (TARGET_HAS_F_SETLKW)
1376 struct flock s_flock
;
1378 s_flock
.l_type
= F_WRLCK
;
1379 s_flock
.l_whence
= SEEK_SET
;
1380 s_flock
.l_start
= 0;
1382 s_flock
.l_pid
= getpid ();
1384 while (fcntl (fileno (da_file
), F_SETLKW
, &s_flock
)
1389 /* If the file is not empty, and the number of counts in it is the
1390 same, then merge them in. */
1391 firstchar
= fgetc (da_file
);
1392 if (firstchar
== EOF
)
1394 if (ferror (da_file
))
1396 fprintf (stderr
, "arc profiling: Can't read output file ");
1397 perror (ptr
->filename
);
1404 if (ungetc (firstchar
, da_file
) == EOF
)
1406 if (__read_long (&n_counts
, da_file
, 8) != 0)
1408 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1413 if (n_counts
== ptr
->ncounts
)
1417 for (i
= 0; i
< n_counts
; i
++)
1421 if (__read_long (&v
, da_file
, 8) != 0)
1423 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1427 ptr
->counts
[i
] += v
;
1435 /* ??? Should first write a header to the file. Preferably, a 4 byte
1436 magic number, 4 bytes containing the time the program was
1437 compiled, 4 bytes containing the last modification time of the
1438 source file, and 4 bytes indicating the compiler options used.
1440 That way we can easily verify that the proper source/executable/
1441 data file combination is being used from gcov. */
1443 if (__write_long (ptr
->ncounts
, da_file
, 8) != 0)
1446 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1452 long *count_ptr
= ptr
->counts
;
1454 for (j
= ptr
->ncounts
; j
> 0; j
--)
1456 if (__write_long (*count_ptr
, da_file
, 8) != 0)
1464 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1468 if (fclose (da_file
) == EOF
)
1469 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1476 /* Must be basic block profiling. Emit a human readable output file. */
1478 file
= fopen ("bb.out", "a");
1487 /* This is somewhat type incorrect, but it avoids worrying about
1488 exactly where time.h is included from. It should be ok unless
1489 a void * differs from other pointer formats, or if sizeof (long)
1490 is < sizeof (time_t). It would be nice if we could assume the
1491 use of rationale standards here. */
1493 time ((void *) &time_value
);
1494 fprintf (file
, "Basic block profiling finished on %s\n", ctime ((void *) &time_value
));
1496 /* We check the length field explicitly in order to allow compatibility
1497 with older GCC's which did not provide it. */
1499 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1502 int func_p
= (ptr
->nwords
>= (long) sizeof (struct bb
)
1503 && ptr
->nwords
<= 1000
1505 int line_p
= (func_p
&& ptr
->line_nums
);
1506 int file_p
= (func_p
&& ptr
->filenames
);
1507 int addr_p
= (ptr
->addresses
!= 0);
1508 long ncounts
= ptr
->ncounts
;
1514 int blk_len
= num_digits (ncounts
, 10);
1519 fprintf (file
, "File %s, %ld basic blocks \n\n",
1520 ptr
->filename
, ncounts
);
1522 /* Get max values for each field. */
1523 for (i
= 0; i
< ncounts
; i
++)
1528 if (cnt_max
< ptr
->counts
[i
])
1529 cnt_max
= ptr
->counts
[i
];
1531 if (addr_p
&& (unsigned long) addr_max
< ptr
->addresses
[i
])
1532 addr_max
= ptr
->addresses
[i
];
1534 if (line_p
&& line_max
< ptr
->line_nums
[i
])
1535 line_max
= ptr
->line_nums
[i
];
1539 p
= (ptr
->functions
[i
]) ? (ptr
->functions
[i
]) : "<none>";
1547 p
= (ptr
->filenames
[i
]) ? (ptr
->filenames
[i
]) : "<none>";
1554 addr_len
= num_digits (addr_max
, 16);
1555 cnt_len
= num_digits (cnt_max
, 10);
1556 line_len
= num_digits (line_max
, 10);
1558 /* Now print out the basic block information. */
1559 for (i
= 0; i
< ncounts
; i
++)
1562 " Block #%*d: executed %*ld time(s)",
1564 cnt_len
, ptr
->counts
[i
]);
1567 fprintf (file
, " address= 0x%.*lx", addr_len
,
1571 fprintf (file
, " function= %-*s", func_len
,
1572 (ptr
->functions
[i
]) ? ptr
->functions
[i
] : "<none>");
1575 fprintf (file
, " line= %*ld", line_len
, ptr
->line_nums
[i
]);
1578 fprintf (file
, " file= %s",
1579 (ptr
->filenames
[i
]) ? ptr
->filenames
[i
] : "<none>");
1581 fprintf (file
, "\n");
1584 fprintf (file
, "\n");
1588 fprintf (file
, "\n\n");
1594 __bb_init_func (struct bb
*blocks
)
1596 /* User is supposed to check whether the first word is non-0,
1597 but just in case.... */
1599 if (blocks
->zero_word
)
1602 /* Initialize destructor. */
1604 atexit (__bb_exit_func
);
1606 /* Set up linked list. */
1607 blocks
->zero_word
= 1;
1608 blocks
->next
= bb_head
;
1612 /* Called before fork or exec - write out profile information gathered so
1613 far and reset it to zero. This avoids duplication or loss of the
1614 profile information gathered so far. */
1616 __bb_fork_func (void)
1621 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1624 for (i
= ptr
->ncounts
- 1; i
>= 0; i
--)
1629 #ifndef MACHINE_STATE_SAVE
1630 #define MACHINE_STATE_SAVE(ID)
1632 #ifndef MACHINE_STATE_RESTORE
1633 #define MACHINE_STATE_RESTORE(ID)
1636 /* Number of buckets in hashtable of basic block addresses. */
1638 #define BB_BUCKETS 311
1640 /* Maximum length of string in file bb.in. */
1642 #define BBINBUFSIZE 500
1646 struct bb_edge
*next
;
1647 unsigned long src_addr
;
1648 unsigned long dst_addr
;
1649 unsigned long count
;
1654 TRACE_KEEP
= 0, TRACE_ON
= 1, TRACE_OFF
= 2
1659 struct bb_func
*next
;
1662 enum bb_func_mode mode
;
1665 /* This is the connection to the outside world.
1666 The BLOCK_PROFILER macro must set __bb.blocks
1667 and __bb.blockno. */
1670 unsigned long blockno
;
1674 /* Vars to store addrs of source and destination basic blocks
1677 static unsigned long bb_src
= 0;
1678 static unsigned long bb_dst
= 0;
1680 static FILE *bb_tracefile
= (FILE *) 0;
1681 static struct bb_edge
**bb_hashbuckets
= (struct bb_edge
**) 0;
1682 static struct bb_func
*bb_func_head
= (struct bb_func
*) 0;
1683 static unsigned long bb_callcount
= 0;
1684 static int bb_mode
= 0;
1686 static unsigned long *bb_stack
= (unsigned long *) 0;
1687 static size_t bb_stacksize
= 0;
1689 static int reported
= 0;
1692 Always : Print execution frequencies of basic blocks
1694 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1695 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1696 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1697 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1702 /*#include <sys/types.h>*/
1703 #include <sys/stat.h>
1704 /*#include <malloc.h>*/
1706 /* Commands executed by gopen. */
1708 #define GOPENDECOMPRESS "gzip -cd "
1709 #define GOPENCOMPRESS "gzip -c >"
1711 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1712 If it does not compile, simply replace gopen by fopen and delete
1713 '.gz' from any first parameter to gopen. */
1716 gopen (char *fn
, char *mode
)
1724 if (mode
[0] != 'r' && mode
[0] != 'w')
1727 p
= fn
+ strlen (fn
)-1;
1728 use_gzip
= ((p
[-1] == '.' && (p
[0] == 'Z' || p
[0] == 'z'))
1729 || (p
[-2] == '.' && p
[-1] == 'g' && p
[0] == 'z'));
1736 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1737 + sizeof (GOPENDECOMPRESS
));
1738 strcpy (s
, GOPENDECOMPRESS
);
1739 strcpy (s
+ (sizeof (GOPENDECOMPRESS
)-1), fn
);
1740 f
= popen (s
, mode
);
1748 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1749 + sizeof (GOPENCOMPRESS
));
1750 strcpy (s
, GOPENCOMPRESS
);
1751 strcpy (s
+ (sizeof (GOPENCOMPRESS
)-1), fn
);
1752 if (!(f
= popen (s
, mode
)))
1753 f
= fopen (s
, mode
);
1760 return fopen (fn
, mode
);
1770 if (!fstat (fileno (f
), &buf
) && S_ISFIFO (buf
.st_mode
))
1778 #endif /* HAVE_POPEN */
1780 /* Called once per program. */
1783 __bb_exit_trace_func (void)
1785 FILE *file
= fopen ("bb.out", "a");
1798 gclose (bb_tracefile
);
1800 fclose (bb_tracefile
);
1801 #endif /* HAVE_POPEN */
1804 /* Check functions in `bb.in'. */
1809 const struct bb_func
*p
;
1810 int printed_something
= 0;
1814 /* This is somewhat type incorrect. */
1815 time ((void *) &time_value
);
1817 for (p
= bb_func_head
; p
!= (struct bb_func
*) 0; p
= p
->next
)
1819 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1821 if (!ptr
->filename
|| (p
->filename
!= (char *) 0 && strcmp (p
->filename
, ptr
->filename
)))
1823 for (blk
= 0; blk
< ptr
->ncounts
; blk
++)
1825 if (!strcmp (p
->funcname
, ptr
->functions
[blk
]))
1830 if (!printed_something
)
1832 fprintf (file
, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value
));
1833 printed_something
= 1;
1836 fprintf (file
, "\tFunction %s", p
->funcname
);
1838 fprintf (file
, " of file %s", p
->filename
);
1839 fprintf (file
, "\n" );
1844 if (printed_something
)
1845 fprintf (file
, "\n");
1851 if (!bb_hashbuckets
)
1855 fprintf (stderr
, "Profiler: out of memory\n");
1865 unsigned long addr_max
= 0;
1866 unsigned long cnt_max
= 0;
1870 /* This is somewhat type incorrect, but it avoids worrying about
1871 exactly where time.h is included from. It should be ok unless
1872 a void * differs from other pointer formats, or if sizeof (long)
1873 is < sizeof (time_t). It would be nice if we could assume the
1874 use of rationale standards here. */
1876 time ((void *) &time_value
);
1877 fprintf (file
, "Basic block jump tracing");
1879 switch (bb_mode
& 12)
1882 fprintf (file
, " (with call)");
1886 /* Print nothing. */
1890 fprintf (file
, " (with call & ret)");
1894 fprintf (file
, " (with ret)");
1898 fprintf (file
, " finished on %s\n", ctime ((void *) &time_value
));
1900 for (i
= 0; i
< BB_BUCKETS
; i
++)
1902 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
1903 for ( ; bucket
; bucket
= bucket
->next
)
1905 if (addr_max
< bucket
->src_addr
)
1906 addr_max
= bucket
->src_addr
;
1907 if (addr_max
< bucket
->dst_addr
)
1908 addr_max
= bucket
->dst_addr
;
1909 if (cnt_max
< bucket
->count
)
1910 cnt_max
= bucket
->count
;
1913 addr_len
= num_digits (addr_max
, 16);
1914 cnt_len
= num_digits (cnt_max
, 10);
1916 for ( i
= 0; i
< BB_BUCKETS
; i
++)
1918 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
1919 for ( ; bucket
; bucket
= bucket
->next
)
1922 "Jump from block 0x%.*lx to block 0x%.*lx executed %*lu time(s)\n",
1923 addr_len
, bucket
->src_addr
,
1924 addr_len
, bucket
->dst_addr
,
1925 cnt_len
, bucket
->count
);
1929 fprintf (file
, "\n");
1937 /* Free allocated memory. */
1942 struct bb_func
*old
= f
;
1945 if (old
->funcname
) free (old
->funcname
);
1946 if (old
->filename
) free (old
->filename
);
1957 for (i
= 0; i
< BB_BUCKETS
; i
++)
1959 struct bb_edge
*old
, *bucket
= bb_hashbuckets
[i
];
1964 bucket
= bucket
->next
;
1968 free (bb_hashbuckets
);
1971 for (b
= bb_head
; b
; b
= b
->next
)
1972 if (b
->flags
) free (b
->flags
);
1975 /* Called once per program. */
1978 __bb_init_prg (void)
1981 char buf
[BBINBUFSIZE
];
1984 enum bb_func_mode m
;
1987 /* Initialize destructor. */
1988 atexit (__bb_exit_func
);
1990 if (!(file
= fopen ("bb.in", "r")))
1993 while(fgets (buf
, BBINBUFSIZE
, file
) != 0)
2009 if (!strcmp (p
, "__bb_trace__"))
2011 else if (!strcmp (p
, "__bb_jumps__"))
2013 else if (!strcmp (p
, "__bb_hidecall__"))
2015 else if (!strcmp (p
, "__bb_showret__"))
2019 struct bb_func
*f
= (struct bb_func
*) malloc (sizeof (struct bb_func
));
2023 f
->next
= bb_func_head
;
2024 if ((pos
= strchr (p
, ':')))
2026 if (!(f
->funcname
= (char *) malloc (strlen (pos
+1)+1)))
2028 strcpy (f
->funcname
, pos
+1);
2030 if ((f
->filename
= (char *) malloc (l
+1)))
2032 strncpy (f
->filename
, p
, l
);
2033 f
->filename
[l
] = '\0';
2036 f
->filename
= (char *) 0;
2040 if (!(f
->funcname
= (char *) malloc (strlen (p
)+1)))
2042 strcpy (f
->funcname
, p
);
2043 f
->filename
= (char *) 0;
2055 bb_tracefile
= gopen ("bbtrace.gz", "w");
2060 bb_tracefile
= fopen ("bbtrace", "w");
2062 #endif /* HAVE_POPEN */
2066 bb_hashbuckets
= (struct bb_edge
**)
2067 malloc (BB_BUCKETS
* sizeof (struct bb_edge
*));
2069 /* Use a loop here rather than calling bzero to avoid having to
2070 conditionalize its existance. */
2071 for (i
= 0; i
< BB_BUCKETS
; i
++)
2072 bb_hashbuckets
[i
] = 0;
2078 bb_stack
= (unsigned long *) malloc (bb_stacksize
* sizeof (*bb_stack
));
2081 /* Initialize destructor. */
2082 atexit (__bb_exit_trace_func
);
2085 /* Called upon entering a basic block. */
2088 __bb_trace_func (void)
2090 struct bb_edge
*bucket
;
2092 MACHINE_STATE_SAVE("1")
2094 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2097 bb_dst
= __bb
.blocks
->addresses
[__bb
.blockno
];
2098 __bb
.blocks
->counts
[__bb
.blockno
]++;
2102 fwrite (&bb_dst
, sizeof (unsigned long), 1, bb_tracefile
);
2107 struct bb_edge
**startbucket
, **oldnext
;
2109 oldnext
= startbucket
2110 = & bb_hashbuckets
[ (((int) bb_src
*8) ^ (int) bb_dst
) % BB_BUCKETS
];
2111 bucket
= *startbucket
;
2113 for (bucket
= *startbucket
; bucket
;
2114 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2116 if (bucket
->src_addr
== bb_src
2117 && bucket
->dst_addr
== bb_dst
)
2120 *oldnext
= bucket
->next
;
2121 bucket
->next
= *startbucket
;
2122 *startbucket
= bucket
;
2127 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2133 fprintf (stderr
, "Profiler: out of memory\n");
2140 bucket
->src_addr
= bb_src
;
2141 bucket
->dst_addr
= bb_dst
;
2142 bucket
->next
= *startbucket
;
2143 *startbucket
= bucket
;
2154 MACHINE_STATE_RESTORE("1")
2158 /* Called when returning from a function and `__bb_showret__' is set. */
2161 __bb_trace_func_ret (void)
2163 struct bb_edge
*bucket
;
2165 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2170 struct bb_edge
**startbucket
, **oldnext
;
2172 oldnext
= startbucket
2173 = & bb_hashbuckets
[ (((int) bb_dst
* 8) ^ (int) bb_src
) % BB_BUCKETS
];
2174 bucket
= *startbucket
;
2176 for (bucket
= *startbucket
; bucket
;
2177 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2179 if (bucket
->src_addr
== bb_dst
2180 && bucket
->dst_addr
== bb_src
)
2183 *oldnext
= bucket
->next
;
2184 bucket
->next
= *startbucket
;
2185 *startbucket
= bucket
;
2190 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2196 fprintf (stderr
, "Profiler: out of memory\n");
2203 bucket
->src_addr
= bb_dst
;
2204 bucket
->dst_addr
= bb_src
;
2205 bucket
->next
= *startbucket
;
2206 *startbucket
= bucket
;
2219 /* Called upon entering the first function of a file. */
2222 __bb_init_file (struct bb
*blocks
)
2225 const struct bb_func
*p
;
2226 long blk
, ncounts
= blocks
->ncounts
;
2227 const char **functions
= blocks
->functions
;
2229 /* Set up linked list. */
2230 blocks
->zero_word
= 1;
2231 blocks
->next
= bb_head
;
2236 || !(blocks
->flags
= (char *) malloc (sizeof (char) * blocks
->ncounts
)))
2239 for (blk
= 0; blk
< ncounts
; blk
++)
2240 blocks
->flags
[blk
] = 0;
2242 for (blk
= 0; blk
< ncounts
; blk
++)
2244 for (p
= bb_func_head
; p
; p
= p
->next
)
2246 if (!strcmp (p
->funcname
, functions
[blk
])
2247 && (!p
->filename
|| !strcmp (p
->filename
, blocks
->filename
)))
2249 blocks
->flags
[blk
] |= p
->mode
;
2256 /* Called when exiting from a function. */
2259 __bb_trace_ret (void)
2262 MACHINE_STATE_SAVE("2")
2266 if ((bb_mode
& 12) && bb_stacksize
> bb_callcount
)
2268 bb_src
= bb_stack
[bb_callcount
];
2270 __bb_trace_func_ret ();
2276 MACHINE_STATE_RESTORE("2")
2280 /* Called when entering a function. */
2283 __bb_init_trace_func (struct bb
*blocks
, unsigned long blockno
)
2285 static int trace_init
= 0;
2287 MACHINE_STATE_SAVE("3")
2289 if (!blocks
->zero_word
)
2296 __bb_init_file (blocks
);
2306 if (bb_callcount
>= bb_stacksize
)
2308 size_t newsize
= bb_callcount
+ 100;
2310 bb_stack
= (unsigned long *) realloc (bb_stack
, newsize
);
2315 fprintf (stderr
, "Profiler: out of memory\n");
2319 goto stack_overflow
;
2321 bb_stacksize
= newsize
;
2323 bb_stack
[bb_callcount
] = bb_src
;
2334 else if (blocks
->flags
&& (blocks
->flags
[blockno
] & TRACE_ON
))
2340 bb_stack
[bb_callcount
] = bb_src
;
2343 MACHINE_STATE_RESTORE("3")
2346 #endif /* not inhibit_libc */
2347 #endif /* not BLOCK_PROFILER_CODE */
2350 #ifdef L_clear_cache
2351 /* Clear part of an instruction cache. */
2353 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2356 __clear_cache (char *beg
__attribute__((__unused__
)),
2357 char *end
__attribute__((__unused__
)))
2359 #ifdef CLEAR_INSN_CACHE
2360 CLEAR_INSN_CACHE (beg
, end
);
2362 #ifdef INSN_CACHE_SIZE
2363 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
2364 static int initialized
;
2368 typedef (*function_ptr
) (void);
2370 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2371 /* It's cheaper to clear the whole cache.
2372 Put in a series of jump instructions so that calling the beginning
2373 of the cache will clear the whole thing. */
2377 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2378 & -INSN_CACHE_LINE_WIDTH
);
2379 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
2381 while (ptr
< end_ptr
)
2383 *(INSTRUCTION_TYPE
*)ptr
2384 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
2385 ptr
+= INSN_CACHE_LINE_WIDTH
;
2387 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
2392 /* Call the beginning of the sequence. */
2393 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2394 & -INSN_CACHE_LINE_WIDTH
))
2397 #else /* Cache is large. */
2401 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2402 & -INSN_CACHE_LINE_WIDTH
);
2404 while (ptr
< (int) array
+ sizeof array
)
2406 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
2407 ptr
+= INSN_CACHE_LINE_WIDTH
;
2413 /* Find the location in array that occupies the same cache line as BEG. */
2415 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
2416 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
2417 & -INSN_CACHE_PLANE_SIZE
)
2420 /* Compute the cache alignment of the place to stop clearing. */
2421 #if 0 /* This is not needed for gcc's purposes. */
2422 /* If the block to clear is bigger than a cache plane,
2423 we clear the entire cache, and OFFSET is already correct. */
2424 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
2426 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
2427 & -INSN_CACHE_LINE_WIDTH
)
2428 & (INSN_CACHE_PLANE_SIZE
- 1));
2430 #if INSN_CACHE_DEPTH > 1
2431 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
2432 if (end_addr
<= start_addr
)
2433 end_addr
+= INSN_CACHE_PLANE_SIZE
;
2435 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
2437 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2438 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2440 while (addr
!= stop
)
2442 /* Call the return instruction at ADDR. */
2443 ((function_ptr
) addr
) ();
2445 addr
+= INSN_CACHE_LINE_WIDTH
;
2448 #else /* just one plane */
2451 /* Call the return instruction at START_ADDR. */
2452 ((function_ptr
) start_addr
) ();
2454 start_addr
+= INSN_CACHE_LINE_WIDTH
;
2456 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
2457 #endif /* just one plane */
2458 #endif /* Cache is large */
2459 #endif /* Cache exists */
2460 #endif /* CLEAR_INSN_CACHE */
2463 #endif /* L_clear_cache */
2467 /* Jump to a trampoline, loading the static chain address. */
2469 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2482 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2486 mprotect (char *addr
, int len
, int prot
)
2503 if (VirtualProtect (addr
, len
, np
, &op
))
2509 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2511 #ifdef TRANSFER_FROM_TRAMPOLINE
2512 TRANSFER_FROM_TRAMPOLINE
2515 #if defined (NeXT) && defined (__MACH__)
2517 /* Make stack executable so we can call trampolines on stack.
2518 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2522 #include <mach/mach.h>
2526 __enable_execute_stack (char *addr
)
2529 char *eaddr
= addr
+ TRAMPOLINE_SIZE
;
2530 vm_address_t a
= (vm_address_t
) addr
;
2532 /* turn on execute access on stack */
2533 r
= vm_protect (task_self (), a
, TRAMPOLINE_SIZE
, FALSE
, VM_PROT_ALL
);
2534 if (r
!= KERN_SUCCESS
)
2536 mach_error("vm_protect VM_PROT_ALL", r
);
2540 /* We inline the i-cache invalidation for speed */
2542 #ifdef CLEAR_INSN_CACHE
2543 CLEAR_INSN_CACHE (addr
, eaddr
);
2545 __clear_cache ((int) addr
, (int) eaddr
);
2549 #endif /* defined (NeXT) && defined (__MACH__) */
2553 /* Make stack executable so we can call trampolines on stack.
2554 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2556 #include <sys/mman.h>
2557 #include <sys/vmparam.h>
2558 #include <machine/machparam.h>
2561 __enable_execute_stack (void)
2564 static unsigned lowest
= USRSTACK
;
2565 unsigned current
= (unsigned) &fp
& -NBPG
;
2567 if (lowest
> current
)
2569 unsigned len
= lowest
- current
;
2570 mremap (current
, &len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
2574 /* Clear instruction cache in case an old trampoline is in it. */
2577 #endif /* __convex__ */
2581 /* Modified from the convex -code above. */
2583 #include <sys/param.h>
2585 #include <sys/m88kbcs.h>
2588 __enable_execute_stack (void)
2591 static unsigned long lowest
= USRSTACK
;
2592 unsigned long current
= (unsigned long) &save_errno
& -NBPC
;
2594 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2595 address is seen as 'negative'. That is the case with the stack. */
2598 if (lowest
> current
)
2600 unsigned len
=lowest
-current
;
2601 memctl(current
,len
,MCT_TEXT
);
2605 memctl(current
,NBPC
,MCT_TEXT
);
2609 #endif /* __sysV88__ */
2613 #include <sys/signal.h>
2616 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2617 so define it here, because we need it in __clear_insn_cache below */
2618 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2619 hence we enable this stuff only if MCT_TEXT is #define'd. */
2634 /* Clear instruction cache so we can call trampolines on stack.
2635 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2638 __clear_insn_cache (void)
2643 /* Preserve errno, because users would be surprised to have
2644 errno changing without explicitly calling any system-call. */
2647 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2648 No need to use an address derived from _start or %sp, as 0 works also. */
2649 memctl(0, 4096, MCT_TEXT
);
2654 #endif /* __sysV68__ */
2658 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2660 #include <sys/mman.h>
2661 #include <sys/types.h>
2662 #include <sys/param.h>
2663 #include <sys/vmmac.h>
2665 /* Modified from the convex -code above.
2666 mremap promises to clear the i-cache. */
2669 __enable_execute_stack (void)
2672 if (mprotect (((unsigned int)&fp
/PAGSIZ
)*PAGSIZ
, PAGSIZ
,
2673 PROT_READ
|PROT_WRITE
|PROT_EXEC
))
2675 perror ("mprotect in __enable_execute_stack");
2680 #endif /* __pyr__ */
2682 #if defined (sony_news) && defined (SYSTYPE_BSD)
2685 #include <sys/types.h>
2686 #include <sys/param.h>
2687 #include <syscall.h>
2688 #include <machine/sysnews.h>
2690 /* cacheflush function for NEWS-OS 4.2.
2691 This function is called from trampoline-initialize code
2692 defined in config/mips/mips.h. */
2695 cacheflush (char *beg
, int size
, int flag
)
2697 if (syscall (SYS_sysnews
, NEWS_CACHEFLUSH
, beg
, size
, FLUSH_BCACHE
))
2699 perror ("cache_flush");
2705 #endif /* sony_news */
2706 #endif /* L_trampoline */
2711 #include "gbl-ctors.h"
2712 /* Some systems use __main in a way incompatible with its use in gcc, in these
2713 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2714 give the same symbol without quotes for an alternative entry point. You
2715 must define both, or neither. */
2717 #define NAME__MAIN "__main"
2718 #define SYMBOL__MAIN __main
2721 #ifdef INIT_SECTION_ASM_OP
2722 #undef HAS_INIT_SECTION
2723 #define HAS_INIT_SECTION
2726 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2728 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2729 code to run constructors. In that case, we need to handle EH here, too. */
2731 #ifdef EH_FRAME_SECTION
2732 #include "unwind-dw2-fde.h"
2733 extern unsigned char __EH_FRAME_BEGIN__
[];
2736 /* Run all the global destructors on exit from the program. */
2739 __do_global_dtors (void)
2741 #ifdef DO_GLOBAL_DTORS_BODY
2742 DO_GLOBAL_DTORS_BODY
;
2744 static func_ptr
*p
= __DTOR_LIST__
+ 1;
2751 #if defined (EH_FRAME_SECTION) && !defined (HAS_INIT_SECTION)
2753 static int completed
= 0;
2757 __deregister_frame_info (__EH_FRAME_BEGIN__
);
2764 #ifndef HAS_INIT_SECTION
2765 /* Run all the global constructors on entry to the program. */
2768 __do_global_ctors (void)
2770 #ifdef EH_FRAME_SECTION
2772 static struct object object
;
2773 __register_frame_info (__EH_FRAME_BEGIN__
, &object
);
2776 DO_GLOBAL_CTORS_BODY
;
2777 atexit (__do_global_dtors
);
2779 #endif /* no HAS_INIT_SECTION */
2781 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2782 /* Subroutine called automatically by `main'.
2783 Compiling a global function named `main'
2784 produces an automatic call to this function at the beginning.
2786 For many systems, this routine calls __do_global_ctors.
2787 For systems which support a .init section we use the .init section
2788 to run __do_global_ctors, so we need not do anything here. */
2793 /* Support recursive calls to `main': run initializers just once. */
2794 static int initialized
;
2798 __do_global_ctors ();
2801 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2803 #endif /* L__main */
2804 #endif /* __CYGWIN__ */
2808 #include "gbl-ctors.h"
2810 /* Provide default definitions for the lists of constructors and
2811 destructors, so that we don't get linker errors. These symbols are
2812 intentionally bss symbols, so that gld and/or collect will provide
2813 the right values. */
2815 /* We declare the lists here with two elements each,
2816 so that they are valid empty lists if no other definition is loaded.
2818 If we are using the old "set" extensions to have the gnu linker
2819 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2820 must be in the bss/common section.
2822 Long term no port should use those extensions. But many still do. */
2823 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2824 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
2825 func_ptr __CTOR_LIST__
[2] = {0, 0};
2826 func_ptr __DTOR_LIST__
[2] = {0, 0};
2828 func_ptr __CTOR_LIST__
[2];
2829 func_ptr __DTOR_LIST__
[2];
2831 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2832 #endif /* L_ctors */
2836 #include "gbl-ctors.h"
2844 static func_ptr
*atexit_chain
= 0;
2845 static long atexit_chain_length
= 0;
2846 static volatile long last_atexit_chain_slot
= -1;
2849 atexit (func_ptr func
)
2851 if (++last_atexit_chain_slot
== atexit_chain_length
)
2853 atexit_chain_length
+= 32;
2855 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
2856 * sizeof (func_ptr
));
2858 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
2859 * sizeof (func_ptr
));
2862 atexit_chain_length
= 0;
2863 last_atexit_chain_slot
= -1;
2868 atexit_chain
[last_atexit_chain_slot
] = func
;
2872 extern void _cleanup (void);
2873 extern void _exit (int) __attribute__ ((__noreturn__
));
2880 for ( ; last_atexit_chain_slot
-- >= 0; )
2882 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
2883 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
2885 free (atexit_chain
);
2898 /* Simple; we just need a wrapper for ON_EXIT. */
2900 atexit (func_ptr func
)
2902 return ON_EXIT (func
);
2905 #endif /* ON_EXIT */
2906 #endif /* NEED_ATEXIT */