1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
29 /* It is incorrect to include config.h here, because this file is being
30 compiled for the target, and hence definitions concerning only the host
40 /* Don't use `fancy_abort' here even if config.h says to use it. */
45 #if (SUPPORTS_WEAK == 1) && (defined (ASM_OUTPUT_DEF) || defined (ASM_OUTPUT_WEAK_ALIAS))
49 /* In a cross-compilation situation, default to inhibiting compilation
50 of routines that use libc. */
52 #if defined(CROSS_COMPILE) && !defined(inhibit_libc)
56 /* Permit the tm.h file to select the endianness to use just for this
57 file. This is used when the endianness is determined when the
60 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
61 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
64 /* In the first part of this file, we are interfacing to calls generated
65 by the compiler itself. These calls pass values into these routines
66 which have very specific modes (rather than very specific types), and
67 these compiler-generated calls also expect any return values to have
68 very specific modes (rather than very specific types). Thus, we need
69 to avoid using regular C language type names in this part of the file
70 because the sizes for those types can be configured to be anything.
71 Instead we use the following special type names. */
73 typedef unsigned int UQItype
__attribute__ ((mode (QI
)));
74 typedef int SItype
__attribute__ ((mode (SI
)));
75 typedef unsigned int USItype
__attribute__ ((mode (SI
)));
76 typedef int DItype
__attribute__ ((mode (DI
)));
77 typedef unsigned int UDItype
__attribute__ ((mode (DI
)));
79 typedef float SFtype
__attribute__ ((mode (SF
)));
80 typedef float DFtype
__attribute__ ((mode (DF
)));
82 #if LONG_DOUBLE_TYPE_SIZE == 96
83 typedef float XFtype
__attribute__ ((mode (XF
)));
85 #if LONG_DOUBLE_TYPE_SIZE == 128
86 typedef float TFtype
__attribute__ ((mode (TF
)));
89 typedef int word_type
__attribute__ ((mode (__word__
)));
91 /* Make sure that we don't accidentally use any normal C language built-in
92 type names in the first part of this file. Instead we want to use *only*
93 the type names defined above. The following macro definitions insure
94 that if we *do* accidentally use some normal C language built-in type name,
95 we will get a syntax error. */
97 #define char bogus_type
98 #define short bogus_type
99 #define int bogus_type
100 #define long bogus_type
101 #define unsigned bogus_type
102 #define float bogus_type
103 #define double bogus_type
105 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
107 /* DIstructs are pairs of SItype values in the order determined by
108 LIBGCC2_WORDS_BIG_ENDIAN. */
110 #if LIBGCC2_WORDS_BIG_ENDIAN
111 struct DIstruct
{SItype high
, low
;};
113 struct DIstruct
{SItype low
, high
;};
116 /* We need this union to unpack/pack DImode values, since we don't have
117 any arithmetic yet. Incoming DImode parameters are stored into the
118 `ll' field, and the unpacked result is read from the struct `s'. */
126 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
127 || defined (L_divdi3) || defined (L_udivdi3) \
128 || defined (L_moddi3) || defined (L_umoddi3))
130 #include "longlong.h"
132 #endif /* udiv or mul */
134 extern DItype
__fixunssfdi (SFtype a
);
135 extern DItype
__fixunsdfdi (DFtype a
);
136 #if LONG_DOUBLE_TYPE_SIZE == 96
137 extern DItype
__fixunsxfdi (XFtype a
);
139 #if LONG_DOUBLE_TYPE_SIZE == 128
140 extern DItype
__fixunstfdi (TFtype a
);
143 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
144 #if defined (L_divdi3) || defined (L_moddi3)
156 w
.s
.high
= -uu
.s
.high
- ((USItype
) w
.s
.low
> 0);
162 /* Unless shift functions are defined whith full ANSI prototypes,
163 parameter b will be promoted to int if word_type is smaller than an int. */
166 __lshrdi3 (DItype u
, word_type b
)
177 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
181 w
.s
.low
= (USItype
)uu
.s
.high
>> -bm
;
185 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
186 w
.s
.high
= (USItype
)uu
.s
.high
>> b
;
187 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
196 __ashldi3 (DItype u
, word_type b
)
207 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
211 w
.s
.high
= (USItype
)uu
.s
.low
<< -bm
;
215 USItype carries
= (USItype
)uu
.s
.low
>> bm
;
216 w
.s
.low
= (USItype
)uu
.s
.low
<< b
;
217 w
.s
.high
= ((USItype
)uu
.s
.high
<< b
) | carries
;
226 __ashrdi3 (DItype u
, word_type b
)
237 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
240 /* w.s.high = 1..1 or 0..0 */
241 w
.s
.high
= uu
.s
.high
>> (sizeof (SItype
) * BITS_PER_UNIT
- 1);
242 w
.s
.low
= uu
.s
.high
>> -bm
;
246 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
247 w
.s
.high
= uu
.s
.high
>> b
;
248 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
262 w
.s
.low
= ffs (uu
.s
.low
);
265 w
.s
.low
= ffs (uu
.s
.high
);
268 w
.s
.low
+= BITS_PER_UNIT
* sizeof (SItype
);
277 __muldi3 (DItype u
, DItype v
)
285 w
.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
);
286 w
.s
.high
+= ((USItype
) uu
.s
.low
* (USItype
) vv
.s
.high
287 + (USItype
) uu
.s
.high
* (USItype
) vv
.s
.low
);
294 #if defined (sdiv_qrnnd)
296 __udiv_w_sdiv (USItype
*rp
, USItype a1
, USItype a0
, USItype d
)
303 if (a1
< d
- a1
- (a0
>> (SI_TYPE_SIZE
- 1)))
305 /* dividend, divisor, and quotient are nonnegative */
306 sdiv_qrnnd (q
, r
, a1
, a0
, d
);
310 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
311 sub_ddmmss (c1
, c0
, a1
, a0
, d
>> 1, d
<< (SI_TYPE_SIZE
- 1));
312 /* Divide (c1*2^32 + c0) by d */
313 sdiv_qrnnd (q
, r
, c1
, c0
, d
);
314 /* Add 2^31 to quotient */
315 q
+= (USItype
) 1 << (SI_TYPE_SIZE
- 1);
320 b1
= d
>> 1; /* d/2, between 2^30 and 2^31 - 1 */
321 c1
= a1
>> 1; /* A/2 */
322 c0
= (a1
<< (SI_TYPE_SIZE
- 1)) + (a0
>> 1);
324 if (a1
< b1
) /* A < 2^32*b1, so A/2 < 2^31*b1 */
326 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
328 r
= 2*r
+ (a0
& 1); /* Remainder from A/(2*b1) */
345 else if (c1
< b1
) /* So 2^31 <= (A/2)/b1 < 2^32 */
348 c0
= ~c0
; /* logical NOT */
350 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
352 q
= ~q
; /* (A/2)/b1 */
355 r
= 2*r
+ (a0
& 1); /* A/(2*b1) */
373 else /* Implies c1 = b1 */
374 { /* Hence a1 = d - 1 = 2*b1 - 1 */
392 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
394 __udiv_w_sdiv (USItype
*rp
, USItype a1
, USItype a0
, USItype d
)
399 #if (defined (L_udivdi3) || defined (L_divdi3) || \
400 defined (L_umoddi3) || defined (L_moddi3))
405 static const UQItype __clz_tab
[] =
407 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,
408 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,
409 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,
410 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,
411 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,
412 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,
413 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,
414 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,
417 #if (defined (L_udivdi3) || defined (L_divdi3) || \
418 defined (L_umoddi3) || defined (L_moddi3))
422 __udivmoddi4 (UDItype n
, UDItype d
, UDItype
*rp
)
427 USItype d0
, d1
, n0
, n1
, n2
;
439 #if !UDIV_NEEDS_NORMALIZATION
446 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
449 /* Remainder in n0. */
456 d0
= 1 / d0
; /* Divide intentionally by zero. */
458 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
459 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
461 /* Remainder in n0. */
472 #else /* UDIV_NEEDS_NORMALIZATION */
480 count_leading_zeros (bm
, d0
);
484 /* Normalize, i.e. make the most significant bit of the
488 n1
= (n1
<< bm
) | (n0
>> (SI_TYPE_SIZE
- bm
));
492 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
495 /* Remainder in n0 >> bm. */
502 d0
= 1 / d0
; /* Divide intentionally by zero. */
504 count_leading_zeros (bm
, d0
);
508 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
509 conclude (the most significant bit of n1 is set) /\ (the
510 leading quotient digit q1 = 1).
512 This special case is necessary, not an optimization.
513 (Shifts counts of SI_TYPE_SIZE are undefined.) */
522 b
= SI_TYPE_SIZE
- bm
;
526 n1
= (n1
<< bm
) | (n0
>> b
);
529 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
534 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
536 /* Remainder in n0 >> bm. */
546 #endif /* UDIV_NEEDS_NORMALIZATION */
557 /* Remainder in n1n0. */
569 count_leading_zeros (bm
, d1
);
572 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
573 conclude (the most significant bit of n1 is set) /\ (the
574 quotient digit q0 = 0 or 1).
576 This special case is necessary, not an optimization. */
578 /* The condition on the next line takes advantage of that
579 n1 >= d1 (true due to program flow). */
580 if (n1
> d1
|| n0
>= d0
)
583 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
602 b
= SI_TYPE_SIZE
- bm
;
604 d1
= (d1
<< bm
) | (d0
>> b
);
607 n1
= (n1
<< bm
) | (n0
>> b
);
610 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
611 umul_ppmm (m1
, m0
, q0
, d0
);
613 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
616 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
621 /* Remainder in (n1n0 - m1m0) >> bm. */
624 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
625 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
626 rr
.s
.high
= n1
>> bm
;
640 UDItype
__udivmoddi4 ();
643 __divdi3 (DItype u
, DItype v
)
654 uu
.ll
= __negdi2 (uu
.ll
);
657 vv
.ll
= __negdi2 (vv
.ll
);
659 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDItype
*) 0);
668 UDItype
__udivmoddi4 ();
670 __moddi3 (DItype u
, DItype v
)
681 uu
.ll
= __negdi2 (uu
.ll
);
683 vv
.ll
= __negdi2 (vv
.ll
);
685 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
694 UDItype
__udivmoddi4 ();
696 __umoddi3 (UDItype u
, UDItype v
)
700 (void) __udivmoddi4 (u
, v
, &w
);
707 UDItype
__udivmoddi4 ();
709 __udivdi3 (UDItype n
, UDItype d
)
711 return __udivmoddi4 (n
, d
, (UDItype
*) 0);
717 __cmpdi2 (DItype a
, DItype b
)
721 au
.ll
= a
, bu
.ll
= b
;
723 if (au
.s
.high
< bu
.s
.high
)
725 else if (au
.s
.high
> bu
.s
.high
)
727 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
729 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
737 __ucmpdi2 (DItype a
, DItype b
)
741 au
.ll
= a
, bu
.ll
= b
;
743 if ((USItype
) au
.s
.high
< (USItype
) bu
.s
.high
)
745 else if ((USItype
) au
.s
.high
> (USItype
) bu
.s
.high
)
747 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
749 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
755 #if defined(L_fixunstfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
756 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
757 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
760 __fixunstfdi (TFtype a
)
768 /* Compute high word of result, as a flonum. */
769 b
= (a
/ HIGH_WORD_COEFF
);
770 /* Convert that to fixed (but not to DItype!),
771 and shift it into the high word. */
774 /* Remove high part from the TFtype, leaving the low part as flonum. */
776 /* Convert that to fixed (but not to DItype!) and add it in.
777 Sometimes A comes out negative. This is significant, since
778 A has more bits than a long int does. */
780 v
-= (USItype
) (- a
);
787 #if defined(L_fixtfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
792 return - __fixunstfdi (-a
);
793 return __fixunstfdi (a
);
797 #if defined(L_fixunsxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
798 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
799 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
802 __fixunsxfdi (XFtype a
)
810 /* Compute high word of result, as a flonum. */
811 b
= (a
/ HIGH_WORD_COEFF
);
812 /* Convert that to fixed (but not to DItype!),
813 and shift it into the high word. */
816 /* Remove high part from the XFtype, leaving the low part as flonum. */
818 /* Convert that to fixed (but not to DItype!) and add it in.
819 Sometimes A comes out negative. This is significant, since
820 A has more bits than a long int does. */
822 v
-= (USItype
) (- a
);
829 #if defined(L_fixxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
834 return - __fixunsxfdi (-a
);
835 return __fixunsxfdi (a
);
840 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
841 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
844 __fixunsdfdi (DFtype a
)
852 /* Compute high word of result, as a flonum. */
853 b
= (a
/ HIGH_WORD_COEFF
);
854 /* Convert that to fixed (but not to DItype!),
855 and shift it into the high word. */
858 /* Remove high part from the DFtype, leaving the low part as flonum. */
860 /* Convert that to fixed (but not to DItype!) and add it in.
861 Sometimes A comes out negative. This is significant, since
862 A has more bits than a long int does. */
864 v
-= (USItype
) (- a
);
876 return - __fixunsdfdi (-a
);
877 return __fixunsdfdi (a
);
882 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
883 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
886 __fixunssfdi (SFtype original_a
)
888 /* Convert the SFtype to a DFtype, because that is surely not going
889 to lose any bits. Some day someone else can write a faster version
890 that avoids converting to DFtype, and verify it really works right. */
891 DFtype a
= original_a
;
898 /* Compute high word of result, as a flonum. */
899 b
= (a
/ HIGH_WORD_COEFF
);
900 /* Convert that to fixed (but not to DItype!),
901 and shift it into the high word. */
904 /* Remove high part from the DFtype, leaving the low part as flonum. */
906 /* Convert that to fixed (but not to DItype!) and add it in.
907 Sometimes A comes out negative. This is significant, since
908 A has more bits than a long int does. */
910 v
-= (USItype
) (- a
);
922 return - __fixunssfdi (-a
);
923 return __fixunssfdi (a
);
927 #if defined(L_floatdixf) && (LONG_DOUBLE_TYPE_SIZE == 96)
928 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
929 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
930 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
933 __floatdixf (DItype u
)
941 d
= (USItype
) (u
>> WORD_SIZE
);
942 d
*= HIGH_HALFWORD_COEFF
;
943 d
*= HIGH_HALFWORD_COEFF
;
944 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
946 return (negate
? -d
: d
);
950 #if defined(L_floatditf) && (LONG_DOUBLE_TYPE_SIZE == 128)
951 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
952 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
953 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
956 __floatditf (DItype u
)
964 d
= (USItype
) (u
>> WORD_SIZE
);
965 d
*= HIGH_HALFWORD_COEFF
;
966 d
*= HIGH_HALFWORD_COEFF
;
967 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
969 return (negate
? -d
: d
);
974 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
975 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
976 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
979 __floatdidf (DItype u
)
987 d
= (USItype
) (u
>> WORD_SIZE
);
988 d
*= HIGH_HALFWORD_COEFF
;
989 d
*= HIGH_HALFWORD_COEFF
;
990 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
992 return (negate
? -d
: d
);
997 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
998 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
999 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1000 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1002 /* Define codes for all the float formats that we know of. Note
1003 that this is copied from real.h. */
1005 #define UNKNOWN_FLOAT_FORMAT 0
1006 #define IEEE_FLOAT_FORMAT 1
1007 #define VAX_FLOAT_FORMAT 2
1008 #define IBM_FLOAT_FORMAT 3
1010 /* Default to IEEE float if not specified. Nearly all machines use it. */
1011 #ifndef HOST_FLOAT_FORMAT
1012 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1015 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1020 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1025 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1031 __floatdisf (DItype u
)
1033 /* Do the calculation in DFmode
1034 so that we don't lose any of the precision of the high word
1035 while multiplying it. */
1042 /* Protect against double-rounding error.
1043 Represent any low-order bits, that might be truncated in DFmode,
1044 by a bit that won't be lost. The bit can go in anywhere below the
1045 rounding position of the SFmode. A fixed mask and bit position
1046 handles all usual configurations. It doesn't handle the case
1047 of 128-bit DImode, however. */
1048 if (DF_SIZE
< DI_SIZE
1049 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1051 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1052 if (u
>= ((UDItype
) 1 << DF_SIZE
))
1054 if ((USItype
) u
& (REP_BIT
- 1))
1058 f
= (USItype
) (u
>> WORD_SIZE
);
1059 f
*= HIGH_HALFWORD_COEFF
;
1060 f
*= HIGH_HALFWORD_COEFF
;
1061 f
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1063 return (SFtype
) (negate
? -f
: f
);
1067 #if defined(L_fixunsxfsi) && LONG_DOUBLE_TYPE_SIZE == 96
1068 /* Reenable the normal types, in case limits.h needs them. */
1081 __fixunsxfsi (XFtype a
)
1083 if (a
>= - (DFtype
) LONG_MIN
)
1084 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1090 /* Reenable the normal types, in case limits.h needs them. */
1103 __fixunsdfsi (DFtype a
)
1105 if (a
>= - (DFtype
) LONG_MIN
)
1106 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1112 /* Reenable the normal types, in case limits.h needs them. */
1125 __fixunssfsi (SFtype a
)
1127 if (a
>= - (SFtype
) LONG_MIN
)
1128 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1133 /* From here on down, the routines use normal data types. */
1135 #define SItype bogus_type
1136 #define USItype bogus_type
1137 #define DItype bogus_type
1138 #define UDItype bogus_type
1139 #define SFtype bogus_type
1140 #define DFtype bogus_type
1152 /* Like bcmp except the sign is meaningful.
1153 Result is negative if S1 is less than S2,
1154 positive if S1 is greater, 0 if S1 and S2 are equal. */
1157 __gcc_bcmp (unsigned char *s1
, unsigned char *s2
, size_t size
)
1161 unsigned char c1
= *s1
++, c2
= *s2
++;
1178 #if defined(__svr4__) || defined(__alliant__)
1182 /* The Alliant needs the added underscore. */
1183 asm (".globl __builtin_saveregs");
1184 asm ("__builtin_saveregs:");
1185 asm (".globl ___builtin_saveregs");
1186 asm ("___builtin_saveregs:");
1188 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1189 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1190 area and also for a new va_list
1192 /* Save all argument registers in the arg reg save area. The
1193 arg reg save area must have the following layout (according
1205 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1206 asm (" fst.q %f12,16(%sp)");
1208 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1209 asm (" st.l %r17,36(%sp)");
1210 asm (" st.l %r18,40(%sp)");
1211 asm (" st.l %r19,44(%sp)");
1212 asm (" st.l %r20,48(%sp)");
1213 asm (" st.l %r21,52(%sp)");
1214 asm (" st.l %r22,56(%sp)");
1215 asm (" st.l %r23,60(%sp)");
1216 asm (" st.l %r24,64(%sp)");
1217 asm (" st.l %r25,68(%sp)");
1218 asm (" st.l %r26,72(%sp)");
1219 asm (" st.l %r27,76(%sp)");
1221 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1222 va_list structure. Put in into
1223 r16 so that it will be returned
1226 /* Initialize all fields of the new va_list structure. This
1227 structure looks like:
1230 unsigned long ireg_used;
1231 unsigned long freg_used;
1237 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1238 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1239 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1240 asm (" bri %r1"); /* delayed return */
1241 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1243 #else /* not __svr4__ */
1244 #if defined(__PARAGON__)
1246 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1247 * and we stand a better chance of hooking into libraries
1248 * compiled by PGI. [andyp@ssd.intel.com]
1252 asm (".globl __builtin_saveregs");
1253 asm ("__builtin_saveregs:");
1254 asm (".globl ___builtin_saveregs");
1255 asm ("___builtin_saveregs:");
1257 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1258 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1259 area and also for a new va_list
1261 /* Save all argument registers in the arg reg save area. The
1262 arg reg save area must have the following layout (according
1274 asm (" fst.q f8, 0(sp)");
1275 asm (" fst.q f12,16(sp)");
1276 asm (" st.l r16,32(sp)");
1277 asm (" st.l r17,36(sp)");
1278 asm (" st.l r18,40(sp)");
1279 asm (" st.l r19,44(sp)");
1280 asm (" st.l r20,48(sp)");
1281 asm (" st.l r21,52(sp)");
1282 asm (" st.l r22,56(sp)");
1283 asm (" st.l r23,60(sp)");
1284 asm (" st.l r24,64(sp)");
1285 asm (" st.l r25,68(sp)");
1286 asm (" st.l r26,72(sp)");
1287 asm (" st.l r27,76(sp)");
1289 asm (" adds 80,sp,r16"); /* compute the address of the new
1290 va_list structure. Put in into
1291 r16 so that it will be returned
1294 /* Initialize all fields of the new va_list structure. This
1295 structure looks like:
1298 unsigned long ireg_used;
1299 unsigned long freg_used;
1305 asm (" st.l r0, 0(r16)"); /* nfixed */
1306 asm (" st.l r0, 4(r16)"); /* nfloating */
1307 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1308 asm (" bri r1"); /* delayed return */
1309 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1310 #else /* not __PARAGON__ */
1314 asm (".globl ___builtin_saveregs");
1315 asm ("___builtin_saveregs:");
1316 asm (" mov sp,r30");
1317 asm (" andnot 0x0f,sp,sp");
1318 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1320 /* Fill in the __va_struct. */
1321 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1322 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1323 asm (" st.l r18, 8(sp)");
1324 asm (" st.l r19,12(sp)");
1325 asm (" st.l r20,16(sp)");
1326 asm (" st.l r21,20(sp)");
1327 asm (" st.l r22,24(sp)");
1328 asm (" st.l r23,28(sp)");
1329 asm (" st.l r24,32(sp)");
1330 asm (" st.l r25,36(sp)");
1331 asm (" st.l r26,40(sp)");
1332 asm (" st.l r27,44(sp)");
1334 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1335 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1337 /* Fill in the __va_ctl. */
1338 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1339 asm (" st.l r28,84(sp)"); /* pointer to more args */
1340 asm (" st.l r0, 88(sp)"); /* nfixed */
1341 asm (" st.l r0, 92(sp)"); /* nfloating */
1343 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1345 asm (" mov r30,sp");
1346 /* recover stack and pass address to start
1348 #endif /* not __PARAGON__ */
1349 #endif /* not __svr4__ */
1350 #else /* not __i860__ */
1352 asm (".global __builtin_saveregs");
1353 asm ("__builtin_saveregs:");
1354 asm (".global ___builtin_saveregs");
1355 asm ("___builtin_saveregs:");
1356 #ifdef NEED_PROC_COMMAND
1359 asm ("st %i0,[%fp+68]");
1360 asm ("st %i1,[%fp+72]");
1361 asm ("st %i2,[%fp+76]");
1362 asm ("st %i3,[%fp+80]");
1363 asm ("st %i4,[%fp+84]");
1365 asm ("st %i5,[%fp+88]");
1366 #ifdef NEED_TYPE_COMMAND
1367 asm (".type __builtin_saveregs,#function");
1368 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1370 #else /* not __sparc__ */
1371 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1375 asm (" .set nomips16");
1377 asm (" .ent __builtin_saveregs");
1378 asm (" .globl __builtin_saveregs");
1379 asm ("__builtin_saveregs:");
1380 asm (" sw $4,0($30)");
1381 asm (" sw $5,4($30)");
1382 asm (" sw $6,8($30)");
1383 asm (" sw $7,12($30)");
1385 asm (" .end __builtin_saveregs");
1386 #else /* not __mips__, etc. */
1389 __builtin_saveregs ()
1394 #endif /* not __mips__ */
1395 #endif /* not __sparc__ */
1396 #endif /* not __i860__ */
1400 #ifndef inhibit_libc
1402 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1404 /* This is used by the `assert' macro. */
1406 __eprintf (const char *string
, const char *expression
,
1407 int line
, const char *filename
)
1409 fprintf (stderr
, string
, expression
, line
, filename
);
1419 /* Structure emitted by -a */
1423 const char *filename
;
1427 const unsigned long *addresses
;
1429 /* Older GCC's did not emit these fields. */
1431 const char **functions
;
1432 const long *line_nums
;
1433 const char **filenames
;
1437 #ifdef BLOCK_PROFILER_CODE
1440 #ifndef inhibit_libc
1442 /* Simple minded basic block profiling output dumper for
1443 systems that don't provide tcov support. At present,
1444 it requires atexit and stdio. */
1446 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1450 #include "gbl-ctors.h"
1451 #include "gcov-io.h"
1454 static struct bb
*bb_head
;
1456 /* Return the number of digits needed to print a value */
1457 /* __inline__ */ static int num_digits (long value
, int base
)
1459 int minus
= (value
< 0 && base
!= 16);
1460 unsigned long v
= (minus
) ? -value
: value
;
1474 __bb_exit_func (void)
1476 FILE *da_file
, *file
;
1483 i
= strlen (bb_head
->filename
) - 3;
1485 if (!strcmp (bb_head
->filename
+i
, ".da"))
1487 /* Must be -fprofile-arcs not -a.
1488 Dump data in a form that gcov expects. */
1492 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1494 /* If the file exists, and the number of counts in it is the same,
1495 then merge them in. */
1497 if ((da_file
= fopen (ptr
->filename
, "r")) != 0)
1505 if (__read_long (&n_counts
, da_file
, 8) != 0)
1507 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1512 if (n_counts
== ptr
->ncounts
)
1516 for (i
= 0; i
< n_counts
; i
++)
1523 if (__read_long (&v
, da_file
, 8) != 0)
1525 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1529 ptr
->counts
[i
] += v
;
1533 if (fclose (da_file
) == EOF
)
1534 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1537 if ((da_file
= fopen (ptr
->filename
, "w")) == 0)
1539 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1544 /* ??? Should first write a header to the file. Preferably, a 4 byte
1545 magic number, 4 bytes containing the time the program was
1546 compiled, 4 bytes containing the last modification time of the
1547 source file, and 4 bytes indicating the compiler options used.
1549 That way we can easily verify that the proper source/executable/
1550 data file combination is being used from gcov. */
1552 if (__write_long (ptr
->ncounts
, da_file
, 8) != 0)
1555 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1561 long *count_ptr
= ptr
->counts
;
1563 for (j
= ptr
->ncounts
; j
> 0; j
--)
1565 if (__write_long (*count_ptr
, da_file
, 8) != 0)
1573 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1577 if (fclose (da_file
) == EOF
)
1578 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1585 /* Must be basic block profiling. Emit a human readable output file. */
1587 file
= fopen ("bb.out", "a");
1596 /* This is somewhat type incorrect, but it avoids worrying about
1597 exactly where time.h is included from. It should be ok unless
1598 a void * differs from other pointer formats, or if sizeof (long)
1599 is < sizeof (time_t). It would be nice if we could assume the
1600 use of rationale standards here. */
1602 time ((void *) &time_value
);
1603 fprintf (file
, "Basic block profiling finished on %s\n", ctime ((void *) &time_value
));
1605 /* We check the length field explicitly in order to allow compatibility
1606 with older GCC's which did not provide it. */
1608 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1611 int func_p
= (ptr
->nwords
>= sizeof (struct bb
)
1612 && ptr
->nwords
<= 1000
1614 int line_p
= (func_p
&& ptr
->line_nums
);
1615 int file_p
= (func_p
&& ptr
->filenames
);
1616 int addr_p
= (ptr
->addresses
!= 0);
1617 long ncounts
= ptr
->ncounts
;
1623 int blk_len
= num_digits (ncounts
, 10);
1628 fprintf (file
, "File %s, %ld basic blocks \n\n",
1629 ptr
->filename
, ncounts
);
1631 /* Get max values for each field. */
1632 for (i
= 0; i
< ncounts
; i
++)
1637 if (cnt_max
< ptr
->counts
[i
])
1638 cnt_max
= ptr
->counts
[i
];
1640 if (addr_p
&& addr_max
< ptr
->addresses
[i
])
1641 addr_max
= ptr
->addresses
[i
];
1643 if (line_p
&& line_max
< ptr
->line_nums
[i
])
1644 line_max
= ptr
->line_nums
[i
];
1648 p
= (ptr
->functions
[i
]) ? (ptr
->functions
[i
]) : "<none>";
1656 p
= (ptr
->filenames
[i
]) ? (ptr
->filenames
[i
]) : "<none>";
1663 addr_len
= num_digits (addr_max
, 16);
1664 cnt_len
= num_digits (cnt_max
, 10);
1665 line_len
= num_digits (line_max
, 10);
1667 /* Now print out the basic block information. */
1668 for (i
= 0; i
< ncounts
; i
++)
1671 " Block #%*d: executed %*ld time(s)",
1673 cnt_len
, ptr
->counts
[i
]);
1676 fprintf (file
, " address= 0x%.*lx", addr_len
,
1680 fprintf (file
, " function= %-*s", func_len
,
1681 (ptr
->functions
[i
]) ? ptr
->functions
[i
] : "<none>");
1684 fprintf (file
, " line= %*ld", line_len
, ptr
->line_nums
[i
]);
1687 fprintf (file
, " file= %s",
1688 (ptr
->filenames
[i
]) ? ptr
->filenames
[i
] : "<none>");
1690 fprintf (file
, "\n");
1693 fprintf (file
, "\n");
1697 fprintf (file
, "\n\n");
1703 __bb_init_func (struct bb
*blocks
)
1705 /* User is supposed to check whether the first word is non-0,
1706 but just in case.... */
1708 if (blocks
->zero_word
)
1712 /* Initialize destructor. */
1714 ON_EXIT (__bb_exit_func
, 0);
1717 /* Set up linked list. */
1718 blocks
->zero_word
= 1;
1719 blocks
->next
= bb_head
;
1723 #ifndef MACHINE_STATE_SAVE
1724 #define MACHINE_STATE_SAVE(ID)
1726 #ifndef MACHINE_STATE_RESTORE
1727 #define MACHINE_STATE_RESTORE(ID)
1730 /* Number of buckets in hashtable of basic block addresses. */
1732 #define BB_BUCKETS 311
1734 /* Maximum length of string in file bb.in. */
1736 #define BBINBUFSIZE 500
1738 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1739 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1741 #define BBINBUFSIZESTR "499"
1745 struct bb_edge
*next
;
1746 unsigned long src_addr
;
1747 unsigned long dst_addr
;
1748 unsigned long count
;
1753 TRACE_KEEP
= 0, TRACE_ON
= 1, TRACE_OFF
= 2
1758 struct bb_func
*next
;
1761 enum bb_func_mode mode
;
1764 /* This is the connection to the outside world.
1765 The BLOCK_PROFILER macro must set __bb.blocks
1766 and __bb.blockno. */
1769 unsigned long blockno
;
1773 /* Vars to store addrs of source and destination basic blocks
1776 static unsigned long bb_src
= 0;
1777 static unsigned long bb_dst
= 0;
1779 static FILE *bb_tracefile
= (FILE *) 0;
1780 static struct bb_edge
**bb_hashbuckets
= (struct bb_edge
**) 0;
1781 static struct bb_func
*bb_func_head
= (struct bb_func
*) 0;
1782 static unsigned long bb_callcount
= 0;
1783 static int bb_mode
= 0;
1785 static unsigned long *bb_stack
= (unsigned long *) 0;
1786 static size_t bb_stacksize
= 0;
1788 static int reported
= 0;
1791 Always : Print execution frequencies of basic blocks
1793 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1794 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1795 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1796 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1801 /*#include <sys/types.h>*/
1802 #include <sys/stat.h>
1803 /*#include <malloc.h>*/
1805 /* Commands executed by gopen. */
1807 #define GOPENDECOMPRESS "gzip -cd "
1808 #define GOPENCOMPRESS "gzip -c >"
1810 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1811 If it does not compile, simply replace gopen by fopen and delete
1812 '.gz' from any first parameter to gopen. */
1815 gopen (char *fn
, char *mode
)
1823 if (mode
[0] != 'r' && mode
[0] != 'w')
1826 p
= fn
+ strlen (fn
)-1;
1827 use_gzip
= ((p
[-1] == '.' && (p
[0] == 'Z' || p
[0] == 'z'))
1828 || (p
[-2] == '.' && p
[-1] == 'g' && p
[0] == 'z'));
1835 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1836 + sizeof (GOPENDECOMPRESS
));
1837 strcpy (s
, GOPENDECOMPRESS
);
1838 strcpy (s
+ (sizeof (GOPENDECOMPRESS
)-1), fn
);
1839 f
= popen (s
, mode
);
1847 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1848 + sizeof (GOPENCOMPRESS
));
1849 strcpy (s
, GOPENCOMPRESS
);
1850 strcpy (s
+ (sizeof (GOPENCOMPRESS
)-1), fn
);
1851 if (!(f
= popen (s
, mode
)))
1852 f
= fopen (s
, mode
);
1859 return fopen (fn
, mode
);
1869 if (!fstat (fileno (f
), &buf
) && S_ISFIFO (buf
.st_mode
))
1877 #endif /* HAVE_POPEN */
1879 /* Called once per program. */
1882 __bb_exit_trace_func ()
1884 FILE *file
= fopen ("bb.out", "a");
1898 gclose (bb_tracefile
);
1900 fclose (bb_tracefile
);
1901 #endif /* HAVE_POPEN */
1904 /* Check functions in `bb.in'. */
1909 const struct bb_func
*p
;
1910 int printed_something
= 0;
1914 /* This is somewhat type incorrect. */
1915 time ((void *) &time_value
);
1917 for (p
= bb_func_head
; p
!= (struct bb_func
*) 0; p
= p
->next
)
1919 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1921 if (!ptr
->filename
|| p
->filename
!= (char *) 0 && strcmp (p
->filename
, ptr
->filename
))
1923 for (blk
= 0; blk
< ptr
->ncounts
; blk
++)
1925 if (!strcmp (p
->funcname
, ptr
->functions
[blk
]))
1930 if (!printed_something
)
1932 fprintf (file
, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value
));
1933 printed_something
= 1;
1936 fprintf (file
, "\tFunction %s", p
->funcname
);
1938 fprintf (file
, " of file %s", p
->filename
);
1939 fprintf (file
, "\n" );
1944 if (printed_something
)
1945 fprintf (file
, "\n");
1951 if (!bb_hashbuckets
)
1955 fprintf (stderr
, "Profiler: out of memory\n");
1965 unsigned long addr_max
= 0;
1966 unsigned long cnt_max
= 0;
1970 /* This is somewhat type incorrect, but it avoids worrying about
1971 exactly where time.h is included from. It should be ok unless
1972 a void * differs from other pointer formats, or if sizeof (long)
1973 is < sizeof (time_t). It would be nice if we could assume the
1974 use of rationale standards here. */
1976 time ((void *) &time_value
);
1977 fprintf (file
, "Basic block jump tracing");
1979 switch (bb_mode
& 12)
1982 fprintf (file
, " (with call)");
1986 /* Print nothing. */
1990 fprintf (file
, " (with call & ret)");
1994 fprintf (file
, " (with ret)");
1998 fprintf (file
, " finished on %s\n", ctime ((void *) &time_value
));
2000 for (i
= 0; i
< BB_BUCKETS
; i
++)
2002 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
2003 for ( ; bucket
; bucket
= bucket
->next
)
2005 if (addr_max
< bucket
->src_addr
)
2006 addr_max
= bucket
->src_addr
;
2007 if (addr_max
< bucket
->dst_addr
)
2008 addr_max
= bucket
->dst_addr
;
2009 if (cnt_max
< bucket
->count
)
2010 cnt_max
= bucket
->count
;
2013 addr_len
= num_digits (addr_max
, 16);
2014 cnt_len
= num_digits (cnt_max
, 10);
2016 for ( i
= 0; i
< BB_BUCKETS
; i
++)
2018 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
2019 for ( ; bucket
; bucket
= bucket
->next
)
2021 fprintf (file
, "Jump from block 0x%.*lx to "
2022 "block 0x%.*lx executed %*d time(s)\n",
2023 addr_len
, bucket
->src_addr
,
2024 addr_len
, bucket
->dst_addr
,
2025 cnt_len
, bucket
->count
);
2029 fprintf (file
, "\n");
2037 /* Free allocated memory. */
2042 struct bb_func
*old
= f
;
2045 if (old
->funcname
) free (old
->funcname
);
2046 if (old
->filename
) free (old
->filename
);
2057 for (i
= 0; i
< BB_BUCKETS
; i
++)
2059 struct bb_edge
*old
, *bucket
= bb_hashbuckets
[i
];
2064 bucket
= bucket
->next
;
2068 free (bb_hashbuckets
);
2071 for (b
= bb_head
; b
; b
= b
->next
)
2072 if (b
->flags
) free (b
->flags
);
2075 /* Called once per program. */
2082 char buf
[BBINBUFSIZE
];
2085 enum bb_func_mode m
;
2088 /* Initialize destructor. */
2089 ON_EXIT (__bb_exit_func
, 0);
2092 if (!(file
= fopen ("bb.in", "r")))
2095 while(fscanf (file
, " %" BBINBUFSIZESTR
"s ", buf
) != EOF
)
2107 if (!strcmp (p
, "__bb_trace__"))
2109 else if (!strcmp (p
, "__bb_jumps__"))
2111 else if (!strcmp (p
, "__bb_hidecall__"))
2113 else if (!strcmp (p
, "__bb_showret__"))
2117 struct bb_func
*f
= (struct bb_func
*) malloc (sizeof (struct bb_func
));
2121 f
->next
= bb_func_head
;
2122 if (pos
= strchr (p
, ':'))
2124 if (!(f
->funcname
= (char *) malloc (strlen (pos
+1)+1)))
2126 strcpy (f
->funcname
, pos
+1);
2128 if ((f
->filename
= (char *) malloc (l
+1)))
2130 strncpy (f
->filename
, p
, l
);
2131 f
->filename
[l
] = '\0';
2134 f
->filename
= (char *) 0;
2138 if (!(f
->funcname
= (char *) malloc (strlen (p
)+1)))
2140 strcpy (f
->funcname
, p
);
2141 f
->filename
= (char *) 0;
2153 bb_tracefile
= gopen ("bbtrace.gz", "w");
2158 bb_tracefile
= fopen ("bbtrace", "w");
2160 #endif /* HAVE_POPEN */
2164 bb_hashbuckets
= (struct bb_edge
**)
2165 malloc (BB_BUCKETS
* sizeof (struct bb_edge
*));
2167 bzero ((char *) bb_hashbuckets
, BB_BUCKETS
);
2173 bb_stack
= (unsigned long *) malloc (bb_stacksize
* sizeof (*bb_stack
));
2177 /* Initialize destructor. */
2178 ON_EXIT (__bb_exit_trace_func
, 0);
2183 /* Called upon entering a basic block. */
2188 struct bb_edge
*bucket
;
2190 MACHINE_STATE_SAVE("1")
2192 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2195 bb_dst
= __bb
.blocks
->addresses
[__bb
.blockno
];
2196 __bb
.blocks
->counts
[__bb
.blockno
]++;
2200 fwrite (&bb_dst
, sizeof (unsigned long), 1, bb_tracefile
);
2205 struct bb_edge
**startbucket
, **oldnext
;
2207 oldnext
= startbucket
2208 = & bb_hashbuckets
[ (((int) bb_src
*8) ^ (int) bb_dst
) % BB_BUCKETS
];
2209 bucket
= *startbucket
;
2211 for (bucket
= *startbucket
; bucket
;
2212 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2214 if (bucket
->src_addr
== bb_src
2215 && bucket
->dst_addr
== bb_dst
)
2218 *oldnext
= bucket
->next
;
2219 bucket
->next
= *startbucket
;
2220 *startbucket
= bucket
;
2225 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2231 fprintf (stderr
, "Profiler: out of memory\n");
2238 bucket
->src_addr
= bb_src
;
2239 bucket
->dst_addr
= bb_dst
;
2240 bucket
->next
= *startbucket
;
2241 *startbucket
= bucket
;
2252 MACHINE_STATE_RESTORE("1")
2256 /* Called when returning from a function and `__bb_showret__' is set. */
2259 __bb_trace_func_ret ()
2261 struct bb_edge
*bucket
;
2263 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2268 struct bb_edge
**startbucket
, **oldnext
;
2270 oldnext
= startbucket
2271 = & bb_hashbuckets
[ (((int) bb_dst
* 8) ^ (int) bb_src
) % BB_BUCKETS
];
2272 bucket
= *startbucket
;
2274 for (bucket
= *startbucket
; bucket
;
2275 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2277 if (bucket
->src_addr
== bb_dst
2278 && bucket
->dst_addr
== bb_src
)
2281 *oldnext
= bucket
->next
;
2282 bucket
->next
= *startbucket
;
2283 *startbucket
= bucket
;
2288 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2294 fprintf (stderr
, "Profiler: out of memory\n");
2301 bucket
->src_addr
= bb_dst
;
2302 bucket
->dst_addr
= bb_src
;
2303 bucket
->next
= *startbucket
;
2304 *startbucket
= bucket
;
2317 /* Called upon entering the first function of a file. */
2320 __bb_init_file (struct bb
*blocks
)
2323 const struct bb_func
*p
;
2324 long blk
, ncounts
= blocks
->ncounts
;
2325 const char **functions
= blocks
->functions
;
2327 /* Set up linked list. */
2328 blocks
->zero_word
= 1;
2329 blocks
->next
= bb_head
;
2334 || !(blocks
->flags
= (char *) malloc (sizeof (char) * blocks
->ncounts
)))
2337 for (blk
= 0; blk
< ncounts
; blk
++)
2338 blocks
->flags
[blk
] = 0;
2340 for (blk
= 0; blk
< ncounts
; blk
++)
2342 for (p
= bb_func_head
; p
; p
= p
->next
)
2344 if (!strcmp (p
->funcname
, functions
[blk
])
2345 && (!p
->filename
|| !strcmp (p
->filename
, blocks
->filename
)))
2347 blocks
->flags
[blk
] |= p
->mode
;
2354 /* Called when exiting from a function. */
2360 MACHINE_STATE_SAVE("2")
2364 if ((bb_mode
& 12) && bb_stacksize
> bb_callcount
)
2366 bb_src
= bb_stack
[bb_callcount
];
2368 __bb_trace_func_ret ();
2374 MACHINE_STATE_RESTORE("2")
2378 /* Called when entering a function. */
2381 __bb_init_trace_func (struct bb
*blocks
, unsigned long blockno
)
2383 static int trace_init
= 0;
2385 MACHINE_STATE_SAVE("3")
2387 if (!blocks
->zero_word
)
2394 __bb_init_file (blocks
);
2404 if (bb_callcount
>= bb_stacksize
)
2406 size_t newsize
= bb_callcount
+ 100;
2408 bb_stack
= (unsigned long *) realloc (bb_stack
, newsize
);
2413 fprintf (stderr
, "Profiler: out of memory\n");
2417 goto stack_overflow
;
2419 bb_stacksize
= newsize
;
2421 bb_stack
[bb_callcount
] = bb_src
;
2432 else if (blocks
->flags
&& (blocks
->flags
[blockno
] & TRACE_ON
))
2438 bb_stack
[bb_callcount
] = bb_src
;
2441 MACHINE_STATE_RESTORE("3")
2444 #endif /* not inhibit_libc */
2445 #endif /* not BLOCK_PROFILER_CODE */
2449 unsigned int __shtab
[] = {
2450 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2451 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2452 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2453 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2454 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2455 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2456 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2457 0x10000000, 0x20000000, 0x40000000, 0x80000000
2461 #ifdef L_clear_cache
2462 /* Clear part of an instruction cache. */
2464 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2467 __clear_cache (char *beg
, char *end
)
2469 #ifdef CLEAR_INSN_CACHE
2470 CLEAR_INSN_CACHE (beg
, end
);
2472 #ifdef INSN_CACHE_SIZE
2473 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
2474 static int initialized
;
2478 typedef (*function_ptr
) ();
2480 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2481 /* It's cheaper to clear the whole cache.
2482 Put in a series of jump instructions so that calling the beginning
2483 of the cache will clear the whole thing. */
2487 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2488 & -INSN_CACHE_LINE_WIDTH
);
2489 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
2491 while (ptr
< end_ptr
)
2493 *(INSTRUCTION_TYPE
*)ptr
2494 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
2495 ptr
+= INSN_CACHE_LINE_WIDTH
;
2497 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
2502 /* Call the beginning of the sequence. */
2503 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2504 & -INSN_CACHE_LINE_WIDTH
))
2507 #else /* Cache is large. */
2511 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2512 & -INSN_CACHE_LINE_WIDTH
);
2514 while (ptr
< (int) array
+ sizeof array
)
2516 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
2517 ptr
+= INSN_CACHE_LINE_WIDTH
;
2523 /* Find the location in array that occupies the same cache line as BEG. */
2525 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
2526 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
2527 & -INSN_CACHE_PLANE_SIZE
)
2530 /* Compute the cache alignment of the place to stop clearing. */
2531 #if 0 /* This is not needed for gcc's purposes. */
2532 /* If the block to clear is bigger than a cache plane,
2533 we clear the entire cache, and OFFSET is already correct. */
2534 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
2536 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
2537 & -INSN_CACHE_LINE_WIDTH
)
2538 & (INSN_CACHE_PLANE_SIZE
- 1));
2540 #if INSN_CACHE_DEPTH > 1
2541 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
2542 if (end_addr
<= start_addr
)
2543 end_addr
+= INSN_CACHE_PLANE_SIZE
;
2545 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
2547 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2548 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2550 while (addr
!= stop
)
2552 /* Call the return instruction at ADDR. */
2553 ((function_ptr
) addr
) ();
2555 addr
+= INSN_CACHE_LINE_WIDTH
;
2558 #else /* just one plane */
2561 /* Call the return instruction at START_ADDR. */
2562 ((function_ptr
) start_addr
) ();
2564 start_addr
+= INSN_CACHE_LINE_WIDTH
;
2566 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
2567 #endif /* just one plane */
2568 #endif /* Cache is large */
2569 #endif /* Cache exists */
2570 #endif /* CLEAR_INSN_CACHE */
2573 #endif /* L_clear_cache */
2577 /* Jump to a trampoline, loading the static chain address. */
2579 #if defined(WINNT) && ! defined(__CYGWIN32__)
2591 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2595 mprotect (char *addr
, int len
, int prot
)
2612 if (VirtualProtect (addr
, len
, np
, &op
))
2620 #ifdef TRANSFER_FROM_TRAMPOLINE
2621 TRANSFER_FROM_TRAMPOLINE
2624 #if defined (NeXT) && defined (__MACH__)
2626 /* Make stack executable so we can call trampolines on stack.
2627 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2631 #include <mach/mach.h>
2635 __enable_execute_stack (char *addr
)
2638 char *eaddr
= addr
+ TRAMPOLINE_SIZE
;
2639 vm_address_t a
= (vm_address_t
) addr
;
2641 /* turn on execute access on stack */
2642 r
= vm_protect (task_self (), a
, TRAMPOLINE_SIZE
, FALSE
, VM_PROT_ALL
);
2643 if (r
!= KERN_SUCCESS
)
2645 mach_error("vm_protect VM_PROT_ALL", r
);
2649 /* We inline the i-cache invalidation for speed */
2651 #ifdef CLEAR_INSN_CACHE
2652 CLEAR_INSN_CACHE (addr
, eaddr
);
2654 __clear_cache ((int) addr
, (int) eaddr
);
2658 #endif /* defined (NeXT) && defined (__MACH__) */
2662 /* Make stack executable so we can call trampolines on stack.
2663 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2665 #include <sys/mman.h>
2666 #include <sys/vmparam.h>
2667 #include <machine/machparam.h>
2670 __enable_execute_stack ()
2673 static unsigned lowest
= USRSTACK
;
2674 unsigned current
= (unsigned) &fp
& -NBPG
;
2676 if (lowest
> current
)
2678 unsigned len
= lowest
- current
;
2679 mremap (current
, &len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
2683 /* Clear instruction cache in case an old trampoline is in it. */
2686 #endif /* __convex__ */
2690 /* Modified from the convex -code above. */
2692 #include <sys/param.h>
2694 #include <sys/m88kbcs.h>
2697 __enable_execute_stack ()
2700 static unsigned long lowest
= USRSTACK
;
2701 unsigned long current
= (unsigned long) &save_errno
& -NBPC
;
2703 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2704 address is seen as 'negative'. That is the case with the stack. */
2707 if (lowest
> current
)
2709 unsigned len
=lowest
-current
;
2710 memctl(current
,len
,MCT_TEXT
);
2714 memctl(current
,NBPC
,MCT_TEXT
);
2718 #endif /* __sysV88__ */
2722 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2724 #include <sys/mman.h>
2725 #include <sys/types.h>
2726 #include <sys/param.h>
2727 #include <sys/vmmac.h>
2729 /* Modified from the convex -code above.
2730 mremap promises to clear the i-cache. */
2733 __enable_execute_stack ()
2736 if (mprotect (((unsigned int)&fp
/PAGSIZ
)*PAGSIZ
, PAGSIZ
,
2737 PROT_READ
|PROT_WRITE
|PROT_EXEC
))
2739 perror ("mprotect in __enable_execute_stack");
2744 #endif /* __pyr__ */
2746 #if defined (sony_news) && defined (SYSTYPE_BSD)
2749 #include <sys/types.h>
2750 #include <sys/param.h>
2751 #include <syscall.h>
2752 #include <machine/sysnews.h>
2754 /* cacheflush function for NEWS-OS 4.2.
2755 This function is called from trampoline-initialize code
2756 defined in config/mips/mips.h. */
2759 cacheflush (char *beg
, int size
, int flag
)
2761 if (syscall (SYS_sysnews
, NEWS_CACHEFLUSH
, beg
, size
, FLUSH_BCACHE
))
2763 perror ("cache_flush");
2769 #endif /* sony_news */
2770 #endif /* L_trampoline */
2774 #include "gbl-ctors.h"
2775 /* Some systems use __main in a way incompatible with its use in gcc, in these
2776 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2777 give the same symbol without quotes for an alternative entry point. You
2778 must define both, or neither. */
2780 #define NAME__MAIN "__main"
2781 #define SYMBOL__MAIN __main
2784 #ifdef INIT_SECTION_ASM_OP
2785 #undef HAS_INIT_SECTION
2786 #define HAS_INIT_SECTION
2789 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2790 /* Run all the global destructors on exit from the program. */
2793 __do_global_dtors ()
2795 #ifdef DO_GLOBAL_DTORS_BODY
2796 DO_GLOBAL_DTORS_BODY
;
2798 static func_ptr
*p
= __DTOR_LIST__
+ 1;
2808 #ifndef HAS_INIT_SECTION
2809 /* Run all the global constructors on entry to the program. */
2812 #define ON_EXIT(a, b)
2814 /* Make sure the exit routine is pulled in to define the globals as
2815 bss symbols, just in case the linker does not automatically pull
2816 bss definitions from the library. */
2818 extern int _exit_dummy_decl
;
2819 int *_exit_dummy_ref
= &_exit_dummy_decl
;
2820 #endif /* ON_EXIT */
2823 __do_global_ctors ()
2825 DO_GLOBAL_CTORS_BODY
;
2826 ON_EXIT (__do_global_dtors
, 0);
2828 #endif /* no HAS_INIT_SECTION */
2830 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2831 /* Subroutine called automatically by `main'.
2832 Compiling a global function named `main'
2833 produces an automatic call to this function at the beginning.
2835 For many systems, this routine calls __do_global_ctors.
2836 For systems which support a .init section we use the .init section
2837 to run __do_global_ctors, so we need not do anything here. */
2842 /* Support recursive calls to `main': run initializers just once. */
2843 static int initialized
;
2847 __do_global_ctors ();
2850 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2852 #endif /* L__main */
2856 #include "gbl-ctors.h"
2858 /* Provide default definitions for the lists of constructors and
2859 destructors, so that we don't get linker errors. These symbols are
2860 intentionally bss symbols, so that gld and/or collect will provide
2861 the right values. */
2863 /* We declare the lists here with two elements each,
2864 so that they are valid empty lists if no other definition is loaded. */
2865 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2866 #if defined(__NeXT__) || defined(_AIX)
2867 /* After 2.3, try this definition on all systems. */
2868 func_ptr __CTOR_LIST__
[2] = {0, 0};
2869 func_ptr __DTOR_LIST__
[2] = {0, 0};
2871 func_ptr __CTOR_LIST__
[2];
2872 func_ptr __DTOR_LIST__
[2];
2874 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2875 #endif /* L_ctors */
2879 #include "gbl-ctors.h"
2885 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2893 static func_ptr
*atexit_chain
= 0;
2894 static long atexit_chain_length
= 0;
2895 static volatile long last_atexit_chain_slot
= -1;
2897 int atexit (func_ptr func
)
2899 if (++last_atexit_chain_slot
== atexit_chain_length
)
2901 atexit_chain_length
+= 32;
2903 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
2904 * sizeof (func_ptr
));
2906 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
2907 * sizeof (func_ptr
));
2910 atexit_chain_length
= 0;
2911 last_atexit_chain_slot
= -1;
2916 atexit_chain
[last_atexit_chain_slot
] = func
;
2919 #endif /* NEED_ATEXIT */
2921 /* If we have no known way of registering our own __do_global_dtors
2922 routine so that it will be invoked at program exit time, then we
2923 have to define our own exit routine which will get this to happen. */
2925 extern void __do_global_dtors ();
2926 extern void __bb_exit_func ();
2927 extern void _cleanup ();
2928 extern void _exit () __attribute__ ((noreturn
));
2933 #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
2937 for ( ; last_atexit_chain_slot
-- >= 0; )
2939 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
2940 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
2942 free (atexit_chain
);
2945 #else /* No NEED_ATEXIT */
2946 __do_global_dtors ();
2947 #endif /* No NEED_ATEXIT */
2949 #ifndef inhibit_libc
2961 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2970 /* Shared exception handling support routines. */
2973 __default_terminate ()
2978 void (*__terminate_func
)() = __default_terminate
;
2983 (*__terminate_func
)();
2987 __throw_type_match (void *catch_type
, void *throw_type
, void *obj
)
2990 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
2991 catch_type
, throw_type
);
2993 if (strcmp ((const char *)catch_type
, (const char *)throw_type
) == 0)
3003 /* EH context structure. */
3007 void **dynamic_handler_chain
;
3008 /* This is language dependent part of the eh context. */
3012 /* This is a safeguard for dynamic handler chain. */
3014 static void *top_elt
[2];
3016 /* Allocate and return a new EH context structure. */
3018 extern void __throw ();
3023 struct eh_context
*eh
= (struct eh_context
*) malloc (sizeof *eh
);
3027 memset (eh
, 0, sizeof *eh
);
3029 eh
->dynamic_handler_chain
= top_elt
;
3035 static __gthread_key_t eh_context_key
;
3037 /* Destructor for struct eh_context. */
3039 eh_context_free (void *ptr
)
3041 __gthread_key_dtor (eh_context_key
, ptr
);
3047 /* Pointer to function to return EH context. */
3049 static struct eh_context
*eh_context_initialize ();
3050 static struct eh_context
*eh_context_static ();
3052 static struct eh_context
*eh_context_specific ();
3055 static struct eh_context
*(*get_eh_context
) () = &eh_context_initialize
;
3057 /* Routine to get EH context.
3058 This one will simply call the function pointer. */
3063 return (void *) (*get_eh_context
) ();
3066 /* Get and set the language specific info pointer. */
3071 struct eh_context
*eh
= (*get_eh_context
) ();
3077 eh_threads_initialize ()
3079 /* Try to create the key. If it fails, revert to static method,
3080 otherwise start using thread specific EH contexts. */
3081 if (__gthread_key_create (&eh_context_key
, &eh_context_free
) == 0)
3082 get_eh_context
= &eh_context_specific
;
3084 get_eh_context
= &eh_context_static
;
3086 #endif /* no __GTHREADS */
3088 /* Initialize EH context.
3089 This will be called only once, since we change GET_EH_CONTEXT
3090 pointer to another routine. */
3092 static struct eh_context
*
3093 eh_context_initialize ()
3097 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
3098 if (__gthread_once (&once
, eh_threads_initialize
) == -1)
3100 /* Use static version of EH context. */
3101 get_eh_context
= &eh_context_static
;
3104 #else /* no __GTHREADS */
3106 /* Use static version of EH context. */
3107 get_eh_context
= &eh_context_static
;
3109 #endif /* no __GTHREADS */
3111 return (*get_eh_context
) ();
3114 /* Return a static EH context. */
3116 static struct eh_context
*
3117 eh_context_static ()
3119 static struct eh_context
*eh
;
3121 eh
= new_eh_context ();
3126 /* Return a thread specific EH context. */
3128 static struct eh_context
*
3129 eh_context_specific ()
3131 struct eh_context
*eh
;
3132 eh
= (struct eh_context
*) __gthread_getspecific (eh_context_key
);
3135 eh
= new_eh_context ();
3136 if (__gthread_setspecific (eh_context_key
, (void *) eh
) != 0)
3144 /* Support routines for setjmp/longjmp exception handling. */
3146 /* Calls to __sjthrow are generated by the compiler when an exception
3147 is raised when using the setjmp/longjmp exception handling codegen
3150 #ifdef DONT_USE_BUILTIN_SETJMP
3151 extern void longjmp (void *, int);
3154 /* Routine to get the head of the current thread's dynamic handler chain
3155 use for exception handling. */
3158 __get_dynamic_handler_chain ()
3160 struct eh_context
*eh
= (*get_eh_context
) ();
3161 return &eh
->dynamic_handler_chain
;
3164 /* This is used to throw an exception when the setjmp/longjmp codegen
3165 method is used for exception handling.
3167 We call __terminate if there are no handlers left. Otherwise we run the
3168 cleanup actions off the dynamic cleanup stack, and pop the top of the
3169 dynamic handler chain, and use longjmp to transfer back to the associated
3175 struct eh_context
*eh
= (*get_eh_context
) ();
3176 void ***dhc
= &eh
->dynamic_handler_chain
;
3178 void (*func
)(void *, int);
3182 /* The cleanup chain is one word into the buffer. Get the cleanup
3184 cleanup
= (void***)&(*dhc
)[1];
3186 /* If there are any cleanups in the chain, run them now. */
3190 void **buf
= (void**)store
;
3195 #ifdef DONT_USE_BUILTIN_SETJMP
3196 if (! setjmp (&buf
[2]))
3198 if (! __builtin_setjmp (&buf
[2]))
3204 func
= (void(*)(void*, int))cleanup
[0][1];
3205 arg
= (void*)cleanup
[0][2];
3207 /* Update this before running the cleanup. */
3208 cleanup
[0] = (void **)cleanup
[0][0];
3221 /* We must call terminate if we try and rethrow an exception, when
3222 there is no exception currently active and when there are no
3224 if (! eh
->info
|| (*dhc
) == top_elt
)
3227 /* Find the jmpbuf associated with the top element of the dynamic
3228 handler chain. The jumpbuf starts two words into the buffer. */
3229 jmpbuf
= &(*dhc
)[2];
3231 /* Then we pop the top element off the dynamic handler chain. */
3232 *dhc
= (void**)(*dhc
)[0];
3234 /* And then we jump to the handler. */
3236 #ifdef DONT_USE_BUILTIN_SETJMP
3237 longjmp (jmpbuf
, 1);
3239 __builtin_longjmp (jmpbuf
, 1);
3243 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3244 handler, then pop the handler off the dynamic handler stack, and
3245 then throw. This is used to skip the first handler, and transfer
3246 control to the next handler in the dynamic handler stack. */
3251 struct eh_context
*eh
= (*get_eh_context
) ();
3252 void ***dhc
= &eh
->dynamic_handler_chain
;
3254 void (*func
)(void *, int);
3258 /* The cleanup chain is one word into the buffer. Get the cleanup
3260 cleanup
= (void***)&(*dhc
)[1];
3262 /* If there are any cleanups in the chain, run them now. */
3266 void **buf
= (void**)store
;
3271 #ifdef DONT_USE_BUILTIN_SETJMP
3272 if (! setjmp (&buf
[2]))
3274 if (! __builtin_setjmp (&buf
[2]))
3280 func
= (void(*)(void*, int))cleanup
[0][1];
3281 arg
= (void*)cleanup
[0][2];
3283 /* Update this before running the cleanup. */
3284 cleanup
[0] = (void **)cleanup
[0][0];
3297 /* Then we pop the top element off the dynamic handler chain. */
3298 *dhc
= (void**)(*dhc
)[0];
3303 /* Support code for all exception region-based exception handling. */
3305 /* This value identifies the place from which an exception is being
3308 #ifdef EH_TABLE_LOOKUP
3314 typedef struct exception_table
{
3317 void *exception_handler
;
3320 /* This routine takes a PC and a pointer to the exception region TABLE for
3321 its translation unit, and returns the address of the exception handler
3322 associated with the closest exception table handler entry associated
3323 with that PC, or 0 if there are no table entries the PC fits in.
3325 In the advent of a tie, we have to give the last entry, as it represents
3329 find_exception_handler (void *pc
, exception_table
*table
)
3336 /* We can't do a binary search because the table isn't guaranteed
3337 to be sorted from function to function. */
3338 for (pos
= 0; table
[pos
].exception_handler
!= (void *) -1; ++pos
)
3340 if (table
[pos
].start
<= pc
&& table
[pos
].end
> pc
)
3342 /* This can apply. Make sure it is at least as small as
3343 the previous best. */
3344 if (best
== -1 || (table
[pos
].end
<= table
[best
].end
3345 && table
[pos
].start
>= table
[best
].start
))
3348 /* But it is sorted by starting PC within a function. */
3349 else if (best
>= 0 && table
[pos
].start
> pc
)
3353 return table
[best
].exception_handler
;
3358 #endif /* EH_TABLE_LOOKUP */
3360 #ifdef DWARF2_UNWIND_INFO
3361 /* Support code for exception handling using static unwind information. */
3365 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3366 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3367 avoid a warning about casting between int and pointer of different
3370 typedef int ptr_type
__attribute__ ((mode (pointer
)));
3372 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3373 frame called by UDATA or 0. */
3376 get_reg (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3378 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3379 return (void *)(ptr_type
)
3380 *(word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
]);
3381 else if (udata
->saved
[reg
] == REG_SAVED_REG
&& sub_udata
)
3382 return get_reg (udata
->reg_or_offset
[reg
], sub_udata
, 0);
3387 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3390 put_reg (unsigned reg
, void *val
, frame_state
*udata
)
3392 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3393 *(word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
])
3394 = (word_type
)(ptr_type
) val
;
3399 /* Copy the saved value for register REG from frame UDATA to frame
3400 TARGET_UDATA. Unlike the previous two functions, this can handle
3401 registers that are not one word large. */
3404 copy_reg (unsigned reg
, frame_state
*udata
, frame_state
*target_udata
)
3406 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
3407 && target_udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3408 memcpy (target_udata
->cfa
+ target_udata
->reg_or_offset
[reg
],
3409 udata
->cfa
+ udata
->reg_or_offset
[reg
],
3410 __builtin_dwarf_reg_size (reg
));
3415 /* Retrieve the return address for frame UDATA, where SUB_UDATA is a
3416 frame called by UDATA or 0. */
3418 static inline void *
3419 get_return_addr (frame_state
*udata
, frame_state
*sub_udata
)
3421 return __builtin_extract_return_addr
3422 (get_reg (udata
->retaddr_column
, udata
, sub_udata
));
3425 /* Overwrite the return address for frame UDATA with VAL. */
3428 put_return_addr (void *val
, frame_state
*udata
)
3430 val
= __builtin_frob_return_addr (val
);
3431 put_reg (udata
->retaddr_column
, val
, udata
);
3434 /* Given the current frame UDATA and its return address PC, return the
3435 information about the calling frame in CALLER_UDATA. */
3438 next_stack_level (void *pc
, frame_state
*udata
, frame_state
*caller_udata
)
3440 caller_udata
= __frame_state_for (pc
, caller_udata
);
3444 /* Now go back to our caller's stack frame. If our caller's CFA register
3445 was saved in our stack frame, restore it; otherwise, assume the CFA
3446 register is SP and restore it to our CFA value. */
3447 if (udata
->saved
[caller_udata
->cfa_reg
])
3448 caller_udata
->cfa
= get_reg (caller_udata
->cfa_reg
, udata
, 0);
3450 caller_udata
->cfa
= udata
->cfa
;
3451 caller_udata
->cfa
+= caller_udata
->cfa_offset
;
3453 return caller_udata
;
3456 #ifdef INCOMING_REGNO
3457 /* Is the saved value for register REG in frame UDATA stored in a register
3458 window in the previous frame? */
3461 in_reg_window (int reg
, frame_state
*udata
)
3463 if (udata
->saved
[reg
] != REG_SAVED_OFFSET
)
3466 #ifdef STACK_GROWS_DOWNWARD
3467 return udata
->reg_or_offset
[reg
] > 0;
3469 return udata
->reg_or_offset
[reg
] < 0;
3472 #endif /* INCOMING_REGNO */
3474 /* We first search for an exception handler, and if we don't find
3475 it, we call __terminate on the current stack frame so that we may
3476 use the debugger to walk the stack and understand why no handler
3479 If we find one, then we unwind the frames down to the one that
3480 has the handler and transfer control into the handler. */
3485 struct eh_context
*eh
= (*get_eh_context
) ();
3486 void *saved_pc
, *pc
, *handler
, *retaddr
;
3487 frame_state ustruct
, ustruct2
;
3488 frame_state
*udata
= &ustruct
;
3489 frame_state
*sub_udata
= &ustruct2
;
3490 frame_state my_ustruct
, *my_udata
= &my_ustruct
;
3493 /* This is required for C++ semantics. We must call terminate if we
3494 try and rethrow an exception, when there is no exception currently
3499 /* Start at our stack frame. */
3501 udata
= __frame_state_for (&&label
, udata
);
3505 /* We need to get the value from the CFA register. At this point in
3506 compiling __throw we don't know whether or not we will use the frame
3507 pointer register for the CFA, so we check our unwind info. */
3508 if (udata
->cfa_reg
== __builtin_dwarf_fp_regnum ())
3509 udata
->cfa
= __builtin_fp ();
3511 udata
->cfa
= __builtin_sp ();
3512 udata
->cfa
+= udata
->cfa_offset
;
3514 memcpy (my_udata
, udata
, sizeof (*udata
));
3516 /* Do any necessary initialization to access arbitrary stack frames.
3517 On the SPARC, this means flushing the register windows. */
3518 __builtin_unwind_init ();
3520 /* Now reset pc to the right throw point. */
3521 pc
= __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3527 frame_state
*p
= udata
;
3528 udata
= next_stack_level (pc
, udata
, sub_udata
);
3531 /* If we couldn't find the next frame, we lose. */
3535 handler
= find_exception_handler (pc
, udata
->eh_ptr
);
3537 /* If we found one, we can stop searching. */
3540 args_size
= udata
->args_size
;
3544 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3545 hitting the beginning of the next region. */
3546 pc
= get_return_addr (udata
, sub_udata
) - 1;
3549 /* If we haven't found a handler by now, this is an unhandled
3555 /* We found a handler in the throw context, no need to unwind. */
3562 /* Unwind all the frames between this one and the handler by copying
3563 their saved register values into our register save slots. */
3565 /* Remember the PC where we found the handler. */
3566 void *handler_pc
= pc
;
3568 /* Start from the throw context again. */
3570 memcpy (udata
, my_udata
, sizeof (*udata
));
3572 while (pc
!= handler_pc
)
3574 frame_state
*p
= udata
;
3575 udata
= next_stack_level (pc
, udata
, sub_udata
);
3578 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
3579 if (i
!= udata
->retaddr_column
&& udata
->saved
[i
])
3581 #ifdef INCOMING_REGNO
3582 /* If you modify the saved value of the return address
3583 register on the SPARC, you modify the return address for
3584 your caller's frame. Don't do that here, as it will
3585 confuse get_return_addr. */
3586 if (in_reg_window (i
, udata
)
3587 && udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
3588 && udata
->reg_or_offset
[udata
->retaddr_column
] == i
)
3591 copy_reg (i
, udata
, my_udata
);
3594 pc
= get_return_addr (udata
, sub_udata
) - 1;
3597 #ifdef INCOMING_REGNO
3598 /* But we do need to update the saved return address register from
3599 the last frame we unwind, or the handler frame will have the wrong
3601 if (udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
)
3603 i
= udata
->reg_or_offset
[udata
->retaddr_column
];
3604 if (in_reg_window (i
, udata
))
3605 copy_reg (i
, udata
, my_udata
);
3609 /* udata now refers to the frame called by the handler frame. */
3611 /* Emit the stub to adjust sp and jump to the handler. */
3612 retaddr
= __builtin_eh_stub ();
3614 /* And then set our return address to point to the stub. */
3615 if (my_udata
->saved
[my_udata
->retaddr_column
] == REG_SAVED_OFFSET
)
3616 put_return_addr (retaddr
, my_udata
);
3618 __builtin_set_return_addr_reg (retaddr
);
3620 /* Set up the registers we use to communicate with the stub.
3621 We check STACK_GROWS_DOWNWARD so the stub can use adjust_stack. */
3622 __builtin_set_eh_regs (handler
,
3623 #ifdef STACK_GROWS_DOWNWARD
3624 udata
->cfa
- my_udata
->cfa
3626 my_udata
->cfa
- udata
->cfa
3631 /* Epilogue: restore the handler frame's register values and return
3634 #endif /* DWARF2_UNWIND_INFO */
3639 #ifndef inhibit_libc
3640 /* This gets us __GNU_LIBRARY__. */
3641 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
3644 #ifdef __GNU_LIBRARY__
3645 /* Avoid forcing the library's meaning of `write' on the user program
3646 by using the "internal" name (for use within the library) */
3647 #define write(fd, buf, n) __write((fd), (buf), (n))
3649 #endif /* inhibit_libc */
3651 #define MESSAGE "pure virtual method called\n"
3656 #ifndef inhibit_libc
3657 write (2, MESSAGE
, sizeof (MESSAGE
) - 1);