1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 92-97, 1998 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
)
401 #if (defined (L_udivdi3) || defined (L_divdi3) || \
402 defined (L_umoddi3) || defined (L_moddi3))
407 static const UQItype __clz_tab
[] =
409 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,
410 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,
411 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,
412 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,
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,
415 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,
416 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,
419 #if (defined (L_udivdi3) || defined (L_divdi3) || \
420 defined (L_umoddi3) || defined (L_moddi3))
424 __udivmoddi4 (UDItype n
, UDItype d
, UDItype
*rp
)
429 USItype d0
, d1
, n0
, n1
, n2
;
441 #if !UDIV_NEEDS_NORMALIZATION
448 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
451 /* Remainder in n0. */
458 d0
= 1 / d0
; /* Divide intentionally by zero. */
460 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
461 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
463 /* Remainder in n0. */
474 #else /* UDIV_NEEDS_NORMALIZATION */
482 count_leading_zeros (bm
, d0
);
486 /* Normalize, i.e. make the most significant bit of the
490 n1
= (n1
<< bm
) | (n0
>> (SI_TYPE_SIZE
- bm
));
494 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
497 /* Remainder in n0 >> bm. */
504 d0
= 1 / d0
; /* Divide intentionally by zero. */
506 count_leading_zeros (bm
, d0
);
510 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
511 conclude (the most significant bit of n1 is set) /\ (the
512 leading quotient digit q1 = 1).
514 This special case is necessary, not an optimization.
515 (Shifts counts of SI_TYPE_SIZE are undefined.) */
524 b
= SI_TYPE_SIZE
- bm
;
528 n1
= (n1
<< bm
) | (n0
>> b
);
531 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
536 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
538 /* Remainder in n0 >> bm. */
548 #endif /* UDIV_NEEDS_NORMALIZATION */
559 /* Remainder in n1n0. */
571 count_leading_zeros (bm
, d1
);
574 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
575 conclude (the most significant bit of n1 is set) /\ (the
576 quotient digit q0 = 0 or 1).
578 This special case is necessary, not an optimization. */
580 /* The condition on the next line takes advantage of that
581 n1 >= d1 (true due to program flow). */
582 if (n1
> d1
|| n0
>= d0
)
585 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
604 b
= SI_TYPE_SIZE
- bm
;
606 d1
= (d1
<< bm
) | (d0
>> b
);
609 n1
= (n1
<< bm
) | (n0
>> b
);
612 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
613 umul_ppmm (m1
, m0
, q0
, d0
);
615 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
618 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
623 /* Remainder in (n1n0 - m1m0) >> bm. */
626 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
627 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
628 rr
.s
.high
= n1
>> bm
;
642 UDItype
__udivmoddi4 ();
645 __divdi3 (DItype u
, DItype v
)
656 uu
.ll
= __negdi2 (uu
.ll
);
659 vv
.ll
= __negdi2 (vv
.ll
);
661 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDItype
*) 0);
670 UDItype
__udivmoddi4 ();
672 __moddi3 (DItype u
, DItype v
)
683 uu
.ll
= __negdi2 (uu
.ll
);
685 vv
.ll
= __negdi2 (vv
.ll
);
687 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
696 UDItype
__udivmoddi4 ();
698 __umoddi3 (UDItype u
, UDItype v
)
702 (void) __udivmoddi4 (u
, v
, &w
);
709 UDItype
__udivmoddi4 ();
711 __udivdi3 (UDItype n
, UDItype d
)
713 return __udivmoddi4 (n
, d
, (UDItype
*) 0);
719 __cmpdi2 (DItype a
, DItype b
)
723 au
.ll
= a
, bu
.ll
= b
;
725 if (au
.s
.high
< bu
.s
.high
)
727 else if (au
.s
.high
> bu
.s
.high
)
729 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
731 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
739 __ucmpdi2 (DItype a
, DItype b
)
743 au
.ll
= a
, bu
.ll
= b
;
745 if ((USItype
) au
.s
.high
< (USItype
) bu
.s
.high
)
747 else if ((USItype
) au
.s
.high
> (USItype
) bu
.s
.high
)
749 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
751 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
757 #if defined(L_fixunstfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
758 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
759 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
762 __fixunstfdi (TFtype a
)
770 /* Compute high word of result, as a flonum. */
771 b
= (a
/ HIGH_WORD_COEFF
);
772 /* Convert that to fixed (but not to DItype!),
773 and shift it into the high word. */
776 /* Remove high part from the TFtype, leaving the low part as flonum. */
778 /* Convert that to fixed (but not to DItype!) and add it in.
779 Sometimes A comes out negative. This is significant, since
780 A has more bits than a long int does. */
782 v
-= (USItype
) (- a
);
789 #if defined(L_fixtfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
794 return - __fixunstfdi (-a
);
795 return __fixunstfdi (a
);
799 #if defined(L_fixunsxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
800 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
801 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
804 __fixunsxfdi (XFtype a
)
812 /* Compute high word of result, as a flonum. */
813 b
= (a
/ HIGH_WORD_COEFF
);
814 /* Convert that to fixed (but not to DItype!),
815 and shift it into the high word. */
818 /* Remove high part from the XFtype, leaving the low part as flonum. */
820 /* Convert that to fixed (but not to DItype!) and add it in.
821 Sometimes A comes out negative. This is significant, since
822 A has more bits than a long int does. */
824 v
-= (USItype
) (- a
);
831 #if defined(L_fixxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
836 return - __fixunsxfdi (-a
);
837 return __fixunsxfdi (a
);
842 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
843 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
846 __fixunsdfdi (DFtype a
)
854 /* Compute high word of result, as a flonum. */
855 b
= (a
/ HIGH_WORD_COEFF
);
856 /* Convert that to fixed (but not to DItype!),
857 and shift it into the high word. */
860 /* Remove high part from the DFtype, leaving the low part as flonum. */
862 /* Convert that to fixed (but not to DItype!) and add it in.
863 Sometimes A comes out negative. This is significant, since
864 A has more bits than a long int does. */
866 v
-= (USItype
) (- a
);
878 return - __fixunsdfdi (-a
);
879 return __fixunsdfdi (a
);
884 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
885 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
888 __fixunssfdi (SFtype original_a
)
890 /* Convert the SFtype to a DFtype, because that is surely not going
891 to lose any bits. Some day someone else can write a faster version
892 that avoids converting to DFtype, and verify it really works right. */
893 DFtype a
= original_a
;
900 /* Compute high word of result, as a flonum. */
901 b
= (a
/ HIGH_WORD_COEFF
);
902 /* Convert that to fixed (but not to DItype!),
903 and shift it into the high word. */
906 /* Remove high part from the DFtype, leaving the low part as flonum. */
908 /* Convert that to fixed (but not to DItype!) and add it in.
909 Sometimes A comes out negative. This is significant, since
910 A has more bits than a long int does. */
912 v
-= (USItype
) (- a
);
924 return - __fixunssfdi (-a
);
925 return __fixunssfdi (a
);
929 #if defined(L_floatdixf) && (LONG_DOUBLE_TYPE_SIZE == 96)
930 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
931 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
932 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
935 __floatdixf (DItype u
)
939 d
= (SItype
) (u
>> WORD_SIZE
);
940 d
*= HIGH_HALFWORD_COEFF
;
941 d
*= HIGH_HALFWORD_COEFF
;
942 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
948 #if defined(L_floatditf) && (LONG_DOUBLE_TYPE_SIZE == 128)
949 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
950 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
951 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
954 __floatditf (DItype u
)
958 d
= (SItype
) (u
>> WORD_SIZE
);
959 d
*= HIGH_HALFWORD_COEFF
;
960 d
*= HIGH_HALFWORD_COEFF
;
961 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
968 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
969 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
970 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
973 __floatdidf (DItype u
)
977 d
= (SItype
) (u
>> WORD_SIZE
);
978 d
*= HIGH_HALFWORD_COEFF
;
979 d
*= HIGH_HALFWORD_COEFF
;
980 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
987 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
988 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
989 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
990 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
992 /* Define codes for all the float formats that we know of. Note
993 that this is copied from real.h. */
995 #define UNKNOWN_FLOAT_FORMAT 0
996 #define IEEE_FLOAT_FORMAT 1
997 #define VAX_FLOAT_FORMAT 2
998 #define IBM_FLOAT_FORMAT 3
1000 /* Default to IEEE float if not specified. Nearly all machines use it. */
1001 #ifndef HOST_FLOAT_FORMAT
1002 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1005 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1010 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1015 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1021 __floatdisf (DItype u
)
1023 /* Do the calculation in DFmode
1024 so that we don't lose any of the precision of the high word
1025 while multiplying it. */
1028 /* Protect against double-rounding error.
1029 Represent any low-order bits, that might be truncated in DFmode,
1030 by a bit that won't be lost. The bit can go in anywhere below the
1031 rounding position of the SFmode. A fixed mask and bit position
1032 handles all usual configurations. It doesn't handle the case
1033 of 128-bit DImode, however. */
1034 if (DF_SIZE
< DI_SIZE
1035 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1037 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1038 if (! (- ((UDItype
) 1 << DF_SIZE
) < u
1039 && u
< ((UDItype
) 1 << DF_SIZE
)))
1041 if ((USItype
) u
& (REP_BIT
- 1))
1045 f
= (SItype
) (u
>> WORD_SIZE
);
1046 f
*= HIGH_HALFWORD_COEFF
;
1047 f
*= HIGH_HALFWORD_COEFF
;
1048 f
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1054 #if defined(L_fixunsxfsi) && LONG_DOUBLE_TYPE_SIZE == 96
1055 /* Reenable the normal types, in case limits.h needs them. */
1068 __fixunsxfsi (XFtype a
)
1070 if (a
>= - (DFtype
) LONG_MIN
)
1071 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1077 /* Reenable the normal types, in case limits.h needs them. */
1090 __fixunsdfsi (DFtype a
)
1092 if (a
>= - (DFtype
) LONG_MIN
)
1093 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1099 /* Reenable the normal types, in case limits.h needs them. */
1112 __fixunssfsi (SFtype a
)
1114 if (a
>= - (SFtype
) LONG_MIN
)
1115 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1120 /* From here on down, the routines use normal data types. */
1122 #define SItype bogus_type
1123 #define USItype bogus_type
1124 #define DItype bogus_type
1125 #define UDItype bogus_type
1126 #define SFtype bogus_type
1127 #define DFtype bogus_type
1139 /* Like bcmp except the sign is meaningful.
1140 Result is negative if S1 is less than S2,
1141 positive if S1 is greater, 0 if S1 and S2 are equal. */
1144 __gcc_bcmp (unsigned char *s1
, unsigned char *s2
, size_t size
)
1148 unsigned char c1
= *s1
++, c2
= *s2
++;
1165 #if defined(__svr4__) || defined(__alliant__)
1169 /* The Alliant needs the added underscore. */
1170 asm (".globl __builtin_saveregs");
1171 asm ("__builtin_saveregs:");
1172 asm (".globl ___builtin_saveregs");
1173 asm ("___builtin_saveregs:");
1175 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1176 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1177 area and also for a new va_list
1179 /* Save all argument registers in the arg reg save area. The
1180 arg reg save area must have the following layout (according
1192 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1193 asm (" fst.q %f12,16(%sp)");
1195 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1196 asm (" st.l %r17,36(%sp)");
1197 asm (" st.l %r18,40(%sp)");
1198 asm (" st.l %r19,44(%sp)");
1199 asm (" st.l %r20,48(%sp)");
1200 asm (" st.l %r21,52(%sp)");
1201 asm (" st.l %r22,56(%sp)");
1202 asm (" st.l %r23,60(%sp)");
1203 asm (" st.l %r24,64(%sp)");
1204 asm (" st.l %r25,68(%sp)");
1205 asm (" st.l %r26,72(%sp)");
1206 asm (" st.l %r27,76(%sp)");
1208 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1209 va_list structure. Put in into
1210 r16 so that it will be returned
1213 /* Initialize all fields of the new va_list structure. This
1214 structure looks like:
1217 unsigned long ireg_used;
1218 unsigned long freg_used;
1224 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1225 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1226 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1227 asm (" bri %r1"); /* delayed return */
1228 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1230 #else /* not __svr4__ */
1231 #if defined(__PARAGON__)
1233 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1234 * and we stand a better chance of hooking into libraries
1235 * compiled by PGI. [andyp@ssd.intel.com]
1239 asm (".globl __builtin_saveregs");
1240 asm ("__builtin_saveregs:");
1241 asm (".globl ___builtin_saveregs");
1242 asm ("___builtin_saveregs:");
1244 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1245 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1246 area and also for a new va_list
1248 /* Save all argument registers in the arg reg save area. The
1249 arg reg save area must have the following layout (according
1261 asm (" fst.q f8, 0(sp)");
1262 asm (" fst.q f12,16(sp)");
1263 asm (" st.l r16,32(sp)");
1264 asm (" st.l r17,36(sp)");
1265 asm (" st.l r18,40(sp)");
1266 asm (" st.l r19,44(sp)");
1267 asm (" st.l r20,48(sp)");
1268 asm (" st.l r21,52(sp)");
1269 asm (" st.l r22,56(sp)");
1270 asm (" st.l r23,60(sp)");
1271 asm (" st.l r24,64(sp)");
1272 asm (" st.l r25,68(sp)");
1273 asm (" st.l r26,72(sp)");
1274 asm (" st.l r27,76(sp)");
1276 asm (" adds 80,sp,r16"); /* compute the address of the new
1277 va_list structure. Put in into
1278 r16 so that it will be returned
1281 /* Initialize all fields of the new va_list structure. This
1282 structure looks like:
1285 unsigned long ireg_used;
1286 unsigned long freg_used;
1292 asm (" st.l r0, 0(r16)"); /* nfixed */
1293 asm (" st.l r0, 4(r16)"); /* nfloating */
1294 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1295 asm (" bri r1"); /* delayed return */
1296 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1297 #else /* not __PARAGON__ */
1301 asm (".globl ___builtin_saveregs");
1302 asm ("___builtin_saveregs:");
1303 asm (" mov sp,r30");
1304 asm (" andnot 0x0f,sp,sp");
1305 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1307 /* Fill in the __va_struct. */
1308 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1309 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1310 asm (" st.l r18, 8(sp)");
1311 asm (" st.l r19,12(sp)");
1312 asm (" st.l r20,16(sp)");
1313 asm (" st.l r21,20(sp)");
1314 asm (" st.l r22,24(sp)");
1315 asm (" st.l r23,28(sp)");
1316 asm (" st.l r24,32(sp)");
1317 asm (" st.l r25,36(sp)");
1318 asm (" st.l r26,40(sp)");
1319 asm (" st.l r27,44(sp)");
1321 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1322 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1324 /* Fill in the __va_ctl. */
1325 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1326 asm (" st.l r28,84(sp)"); /* pointer to more args */
1327 asm (" st.l r0, 88(sp)"); /* nfixed */
1328 asm (" st.l r0, 92(sp)"); /* nfloating */
1330 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1332 asm (" mov r30,sp");
1333 /* recover stack and pass address to start
1335 #endif /* not __PARAGON__ */
1336 #endif /* not __svr4__ */
1337 #else /* not __i860__ */
1339 asm (".global __builtin_saveregs");
1340 asm ("__builtin_saveregs:");
1341 asm (".global ___builtin_saveregs");
1342 asm ("___builtin_saveregs:");
1343 #ifdef NEED_PROC_COMMAND
1346 asm ("st %i0,[%fp+68]");
1347 asm ("st %i1,[%fp+72]");
1348 asm ("st %i2,[%fp+76]");
1349 asm ("st %i3,[%fp+80]");
1350 asm ("st %i4,[%fp+84]");
1352 asm ("st %i5,[%fp+88]");
1353 #ifdef NEED_TYPE_COMMAND
1354 asm (".type __builtin_saveregs,#function");
1355 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1357 #else /* not __sparc__ */
1358 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1362 asm (" .set nomips16");
1364 asm (" .ent __builtin_saveregs");
1365 asm (" .globl __builtin_saveregs");
1366 asm ("__builtin_saveregs:");
1367 asm (" sw $4,0($30)");
1368 asm (" sw $5,4($30)");
1369 asm (" sw $6,8($30)");
1370 asm (" sw $7,12($30)");
1372 asm (" .end __builtin_saveregs");
1373 #else /* not __mips__, etc. */
1376 __builtin_saveregs ()
1381 #endif /* not __mips__ */
1382 #endif /* not __sparc__ */
1383 #endif /* not __i860__ */
1387 #ifndef inhibit_libc
1389 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1391 /* This is used by the `assert' macro. */
1393 __eprintf (const char *string
, const char *expression
,
1394 int line
, const char *filename
)
1396 fprintf (stderr
, string
, expression
, line
, filename
);
1406 /* Structure emitted by -a */
1410 const char *filename
;
1414 const unsigned long *addresses
;
1416 /* Older GCC's did not emit these fields. */
1418 const char **functions
;
1419 const long *line_nums
;
1420 const char **filenames
;
1424 #ifdef BLOCK_PROFILER_CODE
1427 #ifndef inhibit_libc
1429 /* Simple minded basic block profiling output dumper for
1430 systems that don't provide tcov support. At present,
1431 it requires atexit and stdio. */
1433 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1437 #include "gbl-ctors.h"
1438 #include "gcov-io.h"
1441 static struct bb
*bb_head
;
1443 /* Return the number of digits needed to print a value */
1444 /* __inline__ */ static int num_digits (long value
, int base
)
1446 int minus
= (value
< 0 && base
!= 16);
1447 unsigned long v
= (minus
) ? -value
: value
;
1461 __bb_exit_func (void)
1463 FILE *da_file
, *file
;
1470 i
= strlen (bb_head
->filename
) - 3;
1472 if (!strcmp (bb_head
->filename
+i
, ".da"))
1474 /* Must be -fprofile-arcs not -a.
1475 Dump data in a form that gcov expects. */
1479 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1481 /* If the file exists, and the number of counts in it is the same,
1482 then merge them in. */
1484 if ((da_file
= fopen (ptr
->filename
, "r")) != 0)
1488 if (__read_long (&n_counts
, da_file
, 8) != 0)
1490 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1495 if (n_counts
== ptr
->ncounts
)
1499 for (i
= 0; i
< n_counts
; i
++)
1503 if (__read_long (&v
, da_file
, 8) != 0)
1505 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1509 ptr
->counts
[i
] += v
;
1513 if (fclose (da_file
) == EOF
)
1514 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1517 if ((da_file
= fopen (ptr
->filename
, "w")) == 0)
1519 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1524 /* ??? Should first write a header to the file. Preferably, a 4 byte
1525 magic number, 4 bytes containing the time the program was
1526 compiled, 4 bytes containing the last modification time of the
1527 source file, and 4 bytes indicating the compiler options used.
1529 That way we can easily verify that the proper source/executable/
1530 data file combination is being used from gcov. */
1532 if (__write_long (ptr
->ncounts
, da_file
, 8) != 0)
1535 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1541 long *count_ptr
= ptr
->counts
;
1543 for (j
= ptr
->ncounts
; j
> 0; j
--)
1545 if (__write_long (*count_ptr
, da_file
, 8) != 0)
1553 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1557 if (fclose (da_file
) == EOF
)
1558 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1565 /* Must be basic block profiling. Emit a human readable output file. */
1567 file
= fopen ("bb.out", "a");
1576 /* This is somewhat type incorrect, but it avoids worrying about
1577 exactly where time.h is included from. It should be ok unless
1578 a void * differs from other pointer formats, or if sizeof (long)
1579 is < sizeof (time_t). It would be nice if we could assume the
1580 use of rationale standards here. */
1582 time ((void *) &time_value
);
1583 fprintf (file
, "Basic block profiling finished on %s\n", ctime ((void *) &time_value
));
1585 /* We check the length field explicitly in order to allow compatibility
1586 with older GCC's which did not provide it. */
1588 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1591 int func_p
= (ptr
->nwords
>= sizeof (struct bb
)
1592 && ptr
->nwords
<= 1000
1594 int line_p
= (func_p
&& ptr
->line_nums
);
1595 int file_p
= (func_p
&& ptr
->filenames
);
1596 int addr_p
= (ptr
->addresses
!= 0);
1597 long ncounts
= ptr
->ncounts
;
1603 int blk_len
= num_digits (ncounts
, 10);
1608 fprintf (file
, "File %s, %ld basic blocks \n\n",
1609 ptr
->filename
, ncounts
);
1611 /* Get max values for each field. */
1612 for (i
= 0; i
< ncounts
; i
++)
1617 if (cnt_max
< ptr
->counts
[i
])
1618 cnt_max
= ptr
->counts
[i
];
1620 if (addr_p
&& addr_max
< ptr
->addresses
[i
])
1621 addr_max
= ptr
->addresses
[i
];
1623 if (line_p
&& line_max
< ptr
->line_nums
[i
])
1624 line_max
= ptr
->line_nums
[i
];
1628 p
= (ptr
->functions
[i
]) ? (ptr
->functions
[i
]) : "<none>";
1636 p
= (ptr
->filenames
[i
]) ? (ptr
->filenames
[i
]) : "<none>";
1643 addr_len
= num_digits (addr_max
, 16);
1644 cnt_len
= num_digits (cnt_max
, 10);
1645 line_len
= num_digits (line_max
, 10);
1647 /* Now print out the basic block information. */
1648 for (i
= 0; i
< ncounts
; i
++)
1651 " Block #%*d: executed %*ld time(s)",
1653 cnt_len
, ptr
->counts
[i
]);
1656 fprintf (file
, " address= 0x%.*lx", addr_len
,
1660 fprintf (file
, " function= %-*s", func_len
,
1661 (ptr
->functions
[i
]) ? ptr
->functions
[i
] : "<none>");
1664 fprintf (file
, " line= %*ld", line_len
, ptr
->line_nums
[i
]);
1667 fprintf (file
, " file= %s",
1668 (ptr
->filenames
[i
]) ? ptr
->filenames
[i
] : "<none>");
1670 fprintf (file
, "\n");
1673 fprintf (file
, "\n");
1677 fprintf (file
, "\n\n");
1683 __bb_init_func (struct bb
*blocks
)
1685 /* User is supposed to check whether the first word is non-0,
1686 but just in case.... */
1688 if (blocks
->zero_word
)
1692 /* Initialize destructor. */
1694 ON_EXIT (__bb_exit_func
, 0);
1697 /* Set up linked list. */
1698 blocks
->zero_word
= 1;
1699 blocks
->next
= bb_head
;
1703 #ifndef MACHINE_STATE_SAVE
1704 #define MACHINE_STATE_SAVE(ID)
1706 #ifndef MACHINE_STATE_RESTORE
1707 #define MACHINE_STATE_RESTORE(ID)
1710 /* Number of buckets in hashtable of basic block addresses. */
1712 #define BB_BUCKETS 311
1714 /* Maximum length of string in file bb.in. */
1716 #define BBINBUFSIZE 500
1718 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1719 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1721 #define BBINBUFSIZESTR "499"
1725 struct bb_edge
*next
;
1726 unsigned long src_addr
;
1727 unsigned long dst_addr
;
1728 unsigned long count
;
1733 TRACE_KEEP
= 0, TRACE_ON
= 1, TRACE_OFF
= 2
1738 struct bb_func
*next
;
1741 enum bb_func_mode mode
;
1744 /* This is the connection to the outside world.
1745 The BLOCK_PROFILER macro must set __bb.blocks
1746 and __bb.blockno. */
1749 unsigned long blockno
;
1753 /* Vars to store addrs of source and destination basic blocks
1756 static unsigned long bb_src
= 0;
1757 static unsigned long bb_dst
= 0;
1759 static FILE *bb_tracefile
= (FILE *) 0;
1760 static struct bb_edge
**bb_hashbuckets
= (struct bb_edge
**) 0;
1761 static struct bb_func
*bb_func_head
= (struct bb_func
*) 0;
1762 static unsigned long bb_callcount
= 0;
1763 static int bb_mode
= 0;
1765 static unsigned long *bb_stack
= (unsigned long *) 0;
1766 static size_t bb_stacksize
= 0;
1768 static int reported
= 0;
1771 Always : Print execution frequencies of basic blocks
1773 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1774 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1775 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1776 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1781 /*#include <sys/types.h>*/
1782 #include <sys/stat.h>
1783 /*#include <malloc.h>*/
1785 /* Commands executed by gopen. */
1787 #define GOPENDECOMPRESS "gzip -cd "
1788 #define GOPENCOMPRESS "gzip -c >"
1790 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1791 If it does not compile, simply replace gopen by fopen and delete
1792 '.gz' from any first parameter to gopen. */
1795 gopen (char *fn
, char *mode
)
1803 if (mode
[0] != 'r' && mode
[0] != 'w')
1806 p
= fn
+ strlen (fn
)-1;
1807 use_gzip
= ((p
[-1] == '.' && (p
[0] == 'Z' || p
[0] == 'z'))
1808 || (p
[-2] == '.' && p
[-1] == 'g' && p
[0] == 'z'));
1815 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1816 + sizeof (GOPENDECOMPRESS
));
1817 strcpy (s
, GOPENDECOMPRESS
);
1818 strcpy (s
+ (sizeof (GOPENDECOMPRESS
)-1), fn
);
1819 f
= popen (s
, mode
);
1827 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1828 + sizeof (GOPENCOMPRESS
));
1829 strcpy (s
, GOPENCOMPRESS
);
1830 strcpy (s
+ (sizeof (GOPENCOMPRESS
)-1), fn
);
1831 if (!(f
= popen (s
, mode
)))
1832 f
= fopen (s
, mode
);
1839 return fopen (fn
, mode
);
1849 if (!fstat (fileno (f
), &buf
) && S_ISFIFO (buf
.st_mode
))
1857 #endif /* HAVE_POPEN */
1859 /* Called once per program. */
1862 __bb_exit_trace_func ()
1864 FILE *file
= fopen ("bb.out", "a");
1877 gclose (bb_tracefile
);
1879 fclose (bb_tracefile
);
1880 #endif /* HAVE_POPEN */
1883 /* Check functions in `bb.in'. */
1888 const struct bb_func
*p
;
1889 int printed_something
= 0;
1893 /* This is somewhat type incorrect. */
1894 time ((void *) &time_value
);
1896 for (p
= bb_func_head
; p
!= (struct bb_func
*) 0; p
= p
->next
)
1898 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1900 if (!ptr
->filename
|| (p
->filename
!= (char *) 0 && strcmp (p
->filename
, ptr
->filename
)))
1902 for (blk
= 0; blk
< ptr
->ncounts
; blk
++)
1904 if (!strcmp (p
->funcname
, ptr
->functions
[blk
]))
1909 if (!printed_something
)
1911 fprintf (file
, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value
));
1912 printed_something
= 1;
1915 fprintf (file
, "\tFunction %s", p
->funcname
);
1917 fprintf (file
, " of file %s", p
->filename
);
1918 fprintf (file
, "\n" );
1923 if (printed_something
)
1924 fprintf (file
, "\n");
1930 if (!bb_hashbuckets
)
1934 fprintf (stderr
, "Profiler: out of memory\n");
1944 unsigned long addr_max
= 0;
1945 unsigned long cnt_max
= 0;
1949 /* This is somewhat type incorrect, but it avoids worrying about
1950 exactly where time.h is included from. It should be ok unless
1951 a void * differs from other pointer formats, or if sizeof (long)
1952 is < sizeof (time_t). It would be nice if we could assume the
1953 use of rationale standards here. */
1955 time ((void *) &time_value
);
1956 fprintf (file
, "Basic block jump tracing");
1958 switch (bb_mode
& 12)
1961 fprintf (file
, " (with call)");
1965 /* Print nothing. */
1969 fprintf (file
, " (with call & ret)");
1973 fprintf (file
, " (with ret)");
1977 fprintf (file
, " finished on %s\n", ctime ((void *) &time_value
));
1979 for (i
= 0; i
< BB_BUCKETS
; i
++)
1981 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
1982 for ( ; bucket
; bucket
= bucket
->next
)
1984 if (addr_max
< bucket
->src_addr
)
1985 addr_max
= bucket
->src_addr
;
1986 if (addr_max
< bucket
->dst_addr
)
1987 addr_max
= bucket
->dst_addr
;
1988 if (cnt_max
< bucket
->count
)
1989 cnt_max
= bucket
->count
;
1992 addr_len
= num_digits (addr_max
, 16);
1993 cnt_len
= num_digits (cnt_max
, 10);
1995 for ( i
= 0; i
< BB_BUCKETS
; i
++)
1997 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
1998 for ( ; bucket
; bucket
= bucket
->next
)
2000 fprintf (file
, "Jump from block 0x%.*lx to "
2001 "block 0x%.*lx executed %*lu time(s)\n",
2002 addr_len
, bucket
->src_addr
,
2003 addr_len
, bucket
->dst_addr
,
2004 cnt_len
, bucket
->count
);
2008 fprintf (file
, "\n");
2016 /* Free allocated memory. */
2021 struct bb_func
*old
= f
;
2024 if (old
->funcname
) free (old
->funcname
);
2025 if (old
->filename
) free (old
->filename
);
2036 for (i
= 0; i
< BB_BUCKETS
; i
++)
2038 struct bb_edge
*old
, *bucket
= bb_hashbuckets
[i
];
2043 bucket
= bucket
->next
;
2047 free (bb_hashbuckets
);
2050 for (b
= bb_head
; b
; b
= b
->next
)
2051 if (b
->flags
) free (b
->flags
);
2054 /* Called once per program. */
2061 char buf
[BBINBUFSIZE
];
2064 enum bb_func_mode m
;
2067 /* Initialize destructor. */
2068 ON_EXIT (__bb_exit_func
, 0);
2071 if (!(file
= fopen ("bb.in", "r")))
2074 while(fscanf (file
, " %" BBINBUFSIZESTR
"s ", buf
) != EOF
)
2086 if (!strcmp (p
, "__bb_trace__"))
2088 else if (!strcmp (p
, "__bb_jumps__"))
2090 else if (!strcmp (p
, "__bb_hidecall__"))
2092 else if (!strcmp (p
, "__bb_showret__"))
2096 struct bb_func
*f
= (struct bb_func
*) malloc (sizeof (struct bb_func
));
2100 f
->next
= bb_func_head
;
2101 if ((pos
= strchr (p
, ':')))
2103 if (!(f
->funcname
= (char *) malloc (strlen (pos
+1)+1)))
2105 strcpy (f
->funcname
, pos
+1);
2107 if ((f
->filename
= (char *) malloc (l
+1)))
2109 strncpy (f
->filename
, p
, l
);
2110 f
->filename
[l
] = '\0';
2113 f
->filename
= (char *) 0;
2117 if (!(f
->funcname
= (char *) malloc (strlen (p
)+1)))
2119 strcpy (f
->funcname
, p
);
2120 f
->filename
= (char *) 0;
2132 bb_tracefile
= gopen ("bbtrace.gz", "w");
2137 bb_tracefile
= fopen ("bbtrace", "w");
2139 #endif /* HAVE_POPEN */
2143 bb_hashbuckets
= (struct bb_edge
**)
2144 malloc (BB_BUCKETS
* sizeof (struct bb_edge
*));
2146 bzero ((char *) bb_hashbuckets
, BB_BUCKETS
);
2152 bb_stack
= (unsigned long *) malloc (bb_stacksize
* sizeof (*bb_stack
));
2156 /* Initialize destructor. */
2157 ON_EXIT (__bb_exit_trace_func
, 0);
2162 /* Called upon entering a basic block. */
2167 struct bb_edge
*bucket
;
2169 MACHINE_STATE_SAVE("1")
2171 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2174 bb_dst
= __bb
.blocks
->addresses
[__bb
.blockno
];
2175 __bb
.blocks
->counts
[__bb
.blockno
]++;
2179 fwrite (&bb_dst
, sizeof (unsigned long), 1, bb_tracefile
);
2184 struct bb_edge
**startbucket
, **oldnext
;
2186 oldnext
= startbucket
2187 = & bb_hashbuckets
[ (((int) bb_src
*8) ^ (int) bb_dst
) % BB_BUCKETS
];
2188 bucket
= *startbucket
;
2190 for (bucket
= *startbucket
; bucket
;
2191 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2193 if (bucket
->src_addr
== bb_src
2194 && bucket
->dst_addr
== bb_dst
)
2197 *oldnext
= bucket
->next
;
2198 bucket
->next
= *startbucket
;
2199 *startbucket
= bucket
;
2204 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2210 fprintf (stderr
, "Profiler: out of memory\n");
2217 bucket
->src_addr
= bb_src
;
2218 bucket
->dst_addr
= bb_dst
;
2219 bucket
->next
= *startbucket
;
2220 *startbucket
= bucket
;
2231 MACHINE_STATE_RESTORE("1")
2235 /* Called when returning from a function and `__bb_showret__' is set. */
2238 __bb_trace_func_ret ()
2240 struct bb_edge
*bucket
;
2242 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2247 struct bb_edge
**startbucket
, **oldnext
;
2249 oldnext
= startbucket
2250 = & bb_hashbuckets
[ (((int) bb_dst
* 8) ^ (int) bb_src
) % BB_BUCKETS
];
2251 bucket
= *startbucket
;
2253 for (bucket
= *startbucket
; bucket
;
2254 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2256 if (bucket
->src_addr
== bb_dst
2257 && bucket
->dst_addr
== bb_src
)
2260 *oldnext
= bucket
->next
;
2261 bucket
->next
= *startbucket
;
2262 *startbucket
= bucket
;
2267 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2273 fprintf (stderr
, "Profiler: out of memory\n");
2280 bucket
->src_addr
= bb_dst
;
2281 bucket
->dst_addr
= bb_src
;
2282 bucket
->next
= *startbucket
;
2283 *startbucket
= bucket
;
2296 /* Called upon entering the first function of a file. */
2299 __bb_init_file (struct bb
*blocks
)
2302 const struct bb_func
*p
;
2303 long blk
, ncounts
= blocks
->ncounts
;
2304 const char **functions
= blocks
->functions
;
2306 /* Set up linked list. */
2307 blocks
->zero_word
= 1;
2308 blocks
->next
= bb_head
;
2313 || !(blocks
->flags
= (char *) malloc (sizeof (char) * blocks
->ncounts
)))
2316 for (blk
= 0; blk
< ncounts
; blk
++)
2317 blocks
->flags
[blk
] = 0;
2319 for (blk
= 0; blk
< ncounts
; blk
++)
2321 for (p
= bb_func_head
; p
; p
= p
->next
)
2323 if (!strcmp (p
->funcname
, functions
[blk
])
2324 && (!p
->filename
|| !strcmp (p
->filename
, blocks
->filename
)))
2326 blocks
->flags
[blk
] |= p
->mode
;
2333 /* Called when exiting from a function. */
2339 MACHINE_STATE_SAVE("2")
2343 if ((bb_mode
& 12) && bb_stacksize
> bb_callcount
)
2345 bb_src
= bb_stack
[bb_callcount
];
2347 __bb_trace_func_ret ();
2353 MACHINE_STATE_RESTORE("2")
2357 /* Called when entering a function. */
2360 __bb_init_trace_func (struct bb
*blocks
, unsigned long blockno
)
2362 static int trace_init
= 0;
2364 MACHINE_STATE_SAVE("3")
2366 if (!blocks
->zero_word
)
2373 __bb_init_file (blocks
);
2383 if (bb_callcount
>= bb_stacksize
)
2385 size_t newsize
= bb_callcount
+ 100;
2387 bb_stack
= (unsigned long *) realloc (bb_stack
, newsize
);
2392 fprintf (stderr
, "Profiler: out of memory\n");
2396 goto stack_overflow
;
2398 bb_stacksize
= newsize
;
2400 bb_stack
[bb_callcount
] = bb_src
;
2411 else if (blocks
->flags
&& (blocks
->flags
[blockno
] & TRACE_ON
))
2417 bb_stack
[bb_callcount
] = bb_src
;
2420 MACHINE_STATE_RESTORE("3")
2423 #endif /* not inhibit_libc */
2424 #endif /* not BLOCK_PROFILER_CODE */
2428 unsigned int __shtab
[] = {
2429 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2430 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2431 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2432 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2433 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2434 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2435 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2436 0x10000000, 0x20000000, 0x40000000, 0x80000000
2440 #ifdef L_clear_cache
2441 /* Clear part of an instruction cache. */
2443 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2446 __clear_cache (char *beg
, char *end
)
2448 #ifdef CLEAR_INSN_CACHE
2449 CLEAR_INSN_CACHE (beg
, end
);
2451 #ifdef INSN_CACHE_SIZE
2452 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
2453 static int initialized
;
2457 typedef (*function_ptr
) ();
2459 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2460 /* It's cheaper to clear the whole cache.
2461 Put in a series of jump instructions so that calling the beginning
2462 of the cache will clear the whole thing. */
2466 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2467 & -INSN_CACHE_LINE_WIDTH
);
2468 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
2470 while (ptr
< end_ptr
)
2472 *(INSTRUCTION_TYPE
*)ptr
2473 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
2474 ptr
+= INSN_CACHE_LINE_WIDTH
;
2476 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
2481 /* Call the beginning of the sequence. */
2482 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2483 & -INSN_CACHE_LINE_WIDTH
))
2486 #else /* Cache is large. */
2490 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2491 & -INSN_CACHE_LINE_WIDTH
);
2493 while (ptr
< (int) array
+ sizeof array
)
2495 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
2496 ptr
+= INSN_CACHE_LINE_WIDTH
;
2502 /* Find the location in array that occupies the same cache line as BEG. */
2504 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
2505 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
2506 & -INSN_CACHE_PLANE_SIZE
)
2509 /* Compute the cache alignment of the place to stop clearing. */
2510 #if 0 /* This is not needed for gcc's purposes. */
2511 /* If the block to clear is bigger than a cache plane,
2512 we clear the entire cache, and OFFSET is already correct. */
2513 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
2515 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
2516 & -INSN_CACHE_LINE_WIDTH
)
2517 & (INSN_CACHE_PLANE_SIZE
- 1));
2519 #if INSN_CACHE_DEPTH > 1
2520 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
2521 if (end_addr
<= start_addr
)
2522 end_addr
+= INSN_CACHE_PLANE_SIZE
;
2524 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
2526 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2527 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2529 while (addr
!= stop
)
2531 /* Call the return instruction at ADDR. */
2532 ((function_ptr
) addr
) ();
2534 addr
+= INSN_CACHE_LINE_WIDTH
;
2537 #else /* just one plane */
2540 /* Call the return instruction at START_ADDR. */
2541 ((function_ptr
) start_addr
) ();
2543 start_addr
+= INSN_CACHE_LINE_WIDTH
;
2545 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
2546 #endif /* just one plane */
2547 #endif /* Cache is large */
2548 #endif /* Cache exists */
2549 #endif /* CLEAR_INSN_CACHE */
2552 #endif /* L_clear_cache */
2556 /* Jump to a trampoline, loading the static chain address. */
2558 #if defined(WINNT) && ! defined(__CYGWIN32__)
2570 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2574 mprotect (char *addr
, int len
, int prot
)
2591 if (VirtualProtect (addr
, len
, np
, &op
))
2599 #ifdef TRANSFER_FROM_TRAMPOLINE
2600 TRANSFER_FROM_TRAMPOLINE
2603 #if defined (NeXT) && defined (__MACH__)
2605 /* Make stack executable so we can call trampolines on stack.
2606 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2610 #include <mach/mach.h>
2614 __enable_execute_stack (char *addr
)
2617 char *eaddr
= addr
+ TRAMPOLINE_SIZE
;
2618 vm_address_t a
= (vm_address_t
) addr
;
2620 /* turn on execute access on stack */
2621 r
= vm_protect (task_self (), a
, TRAMPOLINE_SIZE
, FALSE
, VM_PROT_ALL
);
2622 if (r
!= KERN_SUCCESS
)
2624 mach_error("vm_protect VM_PROT_ALL", r
);
2628 /* We inline the i-cache invalidation for speed */
2630 #ifdef CLEAR_INSN_CACHE
2631 CLEAR_INSN_CACHE (addr
, eaddr
);
2633 __clear_cache ((int) addr
, (int) eaddr
);
2637 #endif /* defined (NeXT) && defined (__MACH__) */
2641 /* Make stack executable so we can call trampolines on stack.
2642 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2644 #include <sys/mman.h>
2645 #include <sys/vmparam.h>
2646 #include <machine/machparam.h>
2649 __enable_execute_stack ()
2652 static unsigned lowest
= USRSTACK
;
2653 unsigned current
= (unsigned) &fp
& -NBPG
;
2655 if (lowest
> current
)
2657 unsigned len
= lowest
- current
;
2658 mremap (current
, &len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
2662 /* Clear instruction cache in case an old trampoline is in it. */
2665 #endif /* __convex__ */
2669 /* Modified from the convex -code above. */
2671 #include <sys/param.h>
2673 #include <sys/m88kbcs.h>
2676 __enable_execute_stack ()
2679 static unsigned long lowest
= USRSTACK
;
2680 unsigned long current
= (unsigned long) &save_errno
& -NBPC
;
2682 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2683 address is seen as 'negative'. That is the case with the stack. */
2686 if (lowest
> current
)
2688 unsigned len
=lowest
-current
;
2689 memctl(current
,len
,MCT_TEXT
);
2693 memctl(current
,NBPC
,MCT_TEXT
);
2697 #endif /* __sysV88__ */
2701 #include <sys/signal.h>
2704 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2705 so define it here, because we need it in __clear_insn_cache below */
2706 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2707 hence we enable this stuff only if MCT_TEXT is #define'd. */
2722 /* Clear instruction cache so we can call trampolines on stack.
2723 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2726 __clear_insn_cache ()
2731 /* Preserve errno, because users would be surprised to have
2732 errno changing without explicitly calling any system-call. */
2735 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2736 No need to use an address derived from _start or %sp, as 0 works also. */
2737 memctl(0, 4096, MCT_TEXT
);
2742 #endif /* __sysV68__ */
2746 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2748 #include <sys/mman.h>
2749 #include <sys/types.h>
2750 #include <sys/param.h>
2751 #include <sys/vmmac.h>
2753 /* Modified from the convex -code above.
2754 mremap promises to clear the i-cache. */
2757 __enable_execute_stack ()
2760 if (mprotect (((unsigned int)&fp
/PAGSIZ
)*PAGSIZ
, PAGSIZ
,
2761 PROT_READ
|PROT_WRITE
|PROT_EXEC
))
2763 perror ("mprotect in __enable_execute_stack");
2768 #endif /* __pyr__ */
2770 #if defined (sony_news) && defined (SYSTYPE_BSD)
2773 #include <sys/types.h>
2774 #include <sys/param.h>
2775 #include <syscall.h>
2776 #include <machine/sysnews.h>
2778 /* cacheflush function for NEWS-OS 4.2.
2779 This function is called from trampoline-initialize code
2780 defined in config/mips/mips.h. */
2783 cacheflush (char *beg
, int size
, int flag
)
2785 if (syscall (SYS_sysnews
, NEWS_CACHEFLUSH
, beg
, size
, FLUSH_BCACHE
))
2787 perror ("cache_flush");
2793 #endif /* sony_news */
2794 #endif /* L_trampoline */
2796 #ifndef __CYGWIN32__
2799 #include "gbl-ctors.h"
2800 /* Some systems use __main in a way incompatible with its use in gcc, in these
2801 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2802 give the same symbol without quotes for an alternative entry point. You
2803 must define both, or neither. */
2805 #define NAME__MAIN "__main"
2806 #define SYMBOL__MAIN __main
2809 #ifdef INIT_SECTION_ASM_OP
2810 #undef HAS_INIT_SECTION
2811 #define HAS_INIT_SECTION
2814 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2815 /* Run all the global destructors on exit from the program. */
2818 __do_global_dtors ()
2820 #ifdef DO_GLOBAL_DTORS_BODY
2821 DO_GLOBAL_DTORS_BODY
;
2823 static func_ptr
*p
= __DTOR_LIST__
+ 1;
2833 #ifndef HAS_INIT_SECTION
2834 /* Run all the global constructors on entry to the program. */
2837 #define ON_EXIT(a, b)
2839 /* Make sure the exit routine is pulled in to define the globals as
2840 bss symbols, just in case the linker does not automatically pull
2841 bss definitions from the library. */
2843 extern int _exit_dummy_decl
;
2844 int *_exit_dummy_ref
= &_exit_dummy_decl
;
2845 #endif /* ON_EXIT */
2848 __do_global_ctors ()
2850 DO_GLOBAL_CTORS_BODY
;
2851 ON_EXIT (__do_global_dtors
, 0);
2853 #endif /* no HAS_INIT_SECTION */
2855 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2856 /* Subroutine called automatically by `main'.
2857 Compiling a global function named `main'
2858 produces an automatic call to this function at the beginning.
2860 For many systems, this routine calls __do_global_ctors.
2861 For systems which support a .init section we use the .init section
2862 to run __do_global_ctors, so we need not do anything here. */
2867 /* Support recursive calls to `main': run initializers just once. */
2868 static int initialized
;
2872 __do_global_ctors ();
2875 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2877 #endif /* L__main */
2878 #endif /* __CYGWIN32__ */
2882 #include "gbl-ctors.h"
2884 /* Provide default definitions for the lists of constructors and
2885 destructors, so that we don't get linker errors. These symbols are
2886 intentionally bss symbols, so that gld and/or collect will provide
2887 the right values. */
2889 /* We declare the lists here with two elements each,
2890 so that they are valid empty lists if no other definition is loaded. */
2891 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2892 #if defined(__NeXT__) || defined(_AIX)
2893 /* After 2.3, try this definition on all systems. */
2894 func_ptr __CTOR_LIST__
[2] = {0, 0};
2895 func_ptr __DTOR_LIST__
[2] = {0, 0};
2897 func_ptr __CTOR_LIST__
[2];
2898 func_ptr __DTOR_LIST__
[2];
2900 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2901 #endif /* L_ctors */
2905 #include "gbl-ctors.h"
2911 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2919 static func_ptr
*atexit_chain
= 0;
2920 static long atexit_chain_length
= 0;
2921 static volatile long last_atexit_chain_slot
= -1;
2923 int atexit (func_ptr func
)
2925 if (++last_atexit_chain_slot
== atexit_chain_length
)
2927 atexit_chain_length
+= 32;
2929 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
2930 * sizeof (func_ptr
));
2932 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
2933 * sizeof (func_ptr
));
2936 atexit_chain_length
= 0;
2937 last_atexit_chain_slot
= -1;
2942 atexit_chain
[last_atexit_chain_slot
] = func
;
2945 #endif /* NEED_ATEXIT */
2947 /* If we have no known way of registering our own __do_global_dtors
2948 routine so that it will be invoked at program exit time, then we
2949 have to define our own exit routine which will get this to happen. */
2951 extern void __do_global_dtors ();
2952 extern void __bb_exit_func ();
2953 extern void _cleanup ();
2954 extern void _exit () __attribute__ ((noreturn
));
2959 #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
2963 for ( ; last_atexit_chain_slot
-- >= 0; )
2965 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
2966 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
2968 free (atexit_chain
);
2971 #else /* No NEED_ATEXIT */
2972 __do_global_dtors ();
2973 #endif /* No NEED_ATEXIT */
2974 #endif /* !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF) */
2975 /* In gbl-ctors.h, ON_EXIT is defined if HAVE_ATEXIT is defined. In
2976 __bb_init_func and _bb_init_prg, __bb_exit_func is registered with
2977 ON_EXIT if ON_EXIT is defined. Thus we must not call __bb_exit_func here
2978 if HAVE_ATEXIT is defined. */
2980 #ifndef inhibit_libc
2983 #endif /* !HAVE_ATEXIT */
2993 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
3002 /* Shared exception handling support routines. */
3005 __default_terminate ()
3010 void (*__terminate_func
)() = __default_terminate
;
3015 (*__terminate_func
)();
3019 __throw_type_match (void *catch_type
, void *throw_type
, void *obj
)
3022 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3023 catch_type
, throw_type
);
3025 if (strcmp ((const char *)catch_type
, (const char *)throw_type
) == 0)
3035 /* EH context structure. */
3039 void **dynamic_handler_chain
;
3040 /* This is language dependent part of the eh context. */
3044 /* This is a safeguard for dynamic handler chain. */
3046 static void *top_elt
[2];
3048 /* Allocate and return a new EH context structure. */
3050 extern void __throw ();
3055 struct eh_context
*eh
= (struct eh_context
*) malloc (sizeof *eh
);
3059 memset (eh
, 0, sizeof *eh
);
3061 eh
->dynamic_handler_chain
= top_elt
;
3067 static __gthread_key_t eh_context_key
;
3069 /* Destructor for struct eh_context. */
3071 eh_context_free (void *ptr
)
3073 __gthread_key_dtor (eh_context_key
, ptr
);
3079 /* Pointer to function to return EH context. */
3081 static struct eh_context
*eh_context_initialize ();
3082 static struct eh_context
*eh_context_static ();
3084 static struct eh_context
*eh_context_specific ();
3087 static struct eh_context
*(*get_eh_context
) () = &eh_context_initialize
;
3089 /* Routine to get EH context.
3090 This one will simply call the function pointer. */
3095 return (void *) (*get_eh_context
) ();
3098 /* Get and set the language specific info pointer. */
3103 struct eh_context
*eh
= (*get_eh_context
) ();
3109 eh_threads_initialize ()
3111 /* Try to create the key. If it fails, revert to static method,
3112 otherwise start using thread specific EH contexts. */
3113 if (__gthread_key_create (&eh_context_key
, &eh_context_free
) == 0)
3114 get_eh_context
= &eh_context_specific
;
3116 get_eh_context
= &eh_context_static
;
3118 #endif /* no __GTHREADS */
3120 /* Initialize EH context.
3121 This will be called only once, since we change GET_EH_CONTEXT
3122 pointer to another routine. */
3124 static struct eh_context
*
3125 eh_context_initialize ()
3129 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
3130 /* Make sure that get_eh_context does not point to us anymore.
3131 Some systems have dummy thread routines in their libc that
3132 return a success (Solaris 2.6 for example). */
3133 if (__gthread_once (&once
, eh_threads_initialize
) != 0
3134 || get_eh_context
== &eh_context_initialize
)
3136 /* Use static version of EH context. */
3137 get_eh_context
= &eh_context_static
;
3140 #else /* no __GTHREADS */
3142 /* Use static version of EH context. */
3143 get_eh_context
= &eh_context_static
;
3145 #endif /* no __GTHREADS */
3147 return (*get_eh_context
) ();
3150 /* Return a static EH context. */
3152 static struct eh_context
*
3153 eh_context_static ()
3155 static struct eh_context
*eh
;
3157 eh
= new_eh_context ();
3162 /* Return a thread specific EH context. */
3164 static struct eh_context
*
3165 eh_context_specific ()
3167 struct eh_context
*eh
;
3168 eh
= (struct eh_context
*) __gthread_getspecific (eh_context_key
);
3171 eh
= new_eh_context ();
3172 if (__gthread_setspecific (eh_context_key
, (void *) eh
) != 0)
3180 /* Support routines for setjmp/longjmp exception handling. */
3182 /* Calls to __sjthrow are generated by the compiler when an exception
3183 is raised when using the setjmp/longjmp exception handling codegen
3186 #ifdef DONT_USE_BUILTIN_SETJMP
3187 extern void longjmp (void *, int);
3190 /* Routine to get the head of the current thread's dynamic handler chain
3191 use for exception handling. */
3194 __get_dynamic_handler_chain ()
3196 struct eh_context
*eh
= (*get_eh_context
) ();
3197 return &eh
->dynamic_handler_chain
;
3200 /* This is used to throw an exception when the setjmp/longjmp codegen
3201 method is used for exception handling.
3203 We call __terminate if there are no handlers left. Otherwise we run the
3204 cleanup actions off the dynamic cleanup stack, and pop the top of the
3205 dynamic handler chain, and use longjmp to transfer back to the associated
3211 struct eh_context
*eh
= (*get_eh_context
) ();
3212 void ***dhc
= &eh
->dynamic_handler_chain
;
3214 void (*func
)(void *, int);
3218 /* The cleanup chain is one word into the buffer. Get the cleanup
3220 cleanup
= (void***)&(*dhc
)[1];
3222 /* If there are any cleanups in the chain, run them now. */
3226 void **buf
= (void**)store
;
3231 #ifdef DONT_USE_BUILTIN_SETJMP
3232 if (! setjmp (&buf
[2]))
3234 if (! __builtin_setjmp (&buf
[2]))
3240 func
= (void(*)(void*, int))cleanup
[0][1];
3241 arg
= (void*)cleanup
[0][2];
3243 /* Update this before running the cleanup. */
3244 cleanup
[0] = (void **)cleanup
[0][0];
3257 /* We must call terminate if we try and rethrow an exception, when
3258 there is no exception currently active and when there are no
3260 if (! eh
->info
|| (*dhc
) == top_elt
)
3263 /* Find the jmpbuf associated with the top element of the dynamic
3264 handler chain. The jumpbuf starts two words into the buffer. */
3265 jmpbuf
= &(*dhc
)[2];
3267 /* Then we pop the top element off the dynamic handler chain. */
3268 *dhc
= (void**)(*dhc
)[0];
3270 /* And then we jump to the handler. */
3272 #ifdef DONT_USE_BUILTIN_SETJMP
3273 longjmp (jmpbuf
, 1);
3275 __builtin_longjmp (jmpbuf
, 1);
3279 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3280 handler, then pop the handler off the dynamic handler stack, and
3281 then throw. This is used to skip the first handler, and transfer
3282 control to the next handler in the dynamic handler stack. */
3287 struct eh_context
*eh
= (*get_eh_context
) ();
3288 void ***dhc
= &eh
->dynamic_handler_chain
;
3289 void (*func
)(void *, int);
3293 /* The cleanup chain is one word into the buffer. Get the cleanup
3295 cleanup
= (void***)&(*dhc
)[1];
3297 /* If there are any cleanups in the chain, run them now. */
3301 void **buf
= (void**)store
;
3306 #ifdef DONT_USE_BUILTIN_SETJMP
3307 if (! setjmp (&buf
[2]))
3309 if (! __builtin_setjmp (&buf
[2]))
3315 func
= (void(*)(void*, int))cleanup
[0][1];
3316 arg
= (void*)cleanup
[0][2];
3318 /* Update this before running the cleanup. */
3319 cleanup
[0] = (void **)cleanup
[0][0];
3332 /* Then we pop the top element off the dynamic handler chain. */
3333 *dhc
= (void**)(*dhc
)[0];
3338 /* Support code for all exception region-based exception handling. */
3340 /* This value identifies the place from which an exception is being
3343 #ifdef EH_TABLE_LOOKUP
3349 typedef struct exception_table
{
3352 void *exception_handler
;
3355 /* This routine takes a PC and a pointer to the exception region TABLE for
3356 its translation unit, and returns the address of the exception handler
3357 associated with the closest exception table handler entry associated
3358 with that PC, or 0 if there are no table entries the PC fits in.
3360 In the advent of a tie, we have to give the last entry, as it represents
3364 find_exception_handler (void *pc
, exception_table
*table
)
3371 /* We can't do a binary search because the table isn't guaranteed
3372 to be sorted from function to function. */
3373 for (pos
= 0; table
[pos
].exception_handler
!= (void *) -1; ++pos
)
3375 if (table
[pos
].start
<= pc
&& table
[pos
].end
> pc
)
3377 /* This can apply. Make sure it is at least as small as
3378 the previous best. */
3379 if (best
== -1 || (table
[pos
].end
<= table
[best
].end
3380 && table
[pos
].start
>= table
[best
].start
))
3383 /* But it is sorted by starting PC within a function. */
3384 else if (best
>= 0 && table
[pos
].start
> pc
)
3388 return table
[best
].exception_handler
;
3393 #endif /* EH_TABLE_LOOKUP */
3395 #ifdef DWARF2_UNWIND_INFO
3396 /* Support code for exception handling using static unwind information. */
3400 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3401 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3402 avoid a warning about casting between int and pointer of different
3405 typedef int ptr_type
__attribute__ ((mode (pointer
)));
3407 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3408 frame called by UDATA or 0. */
3411 get_reg (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3413 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3414 return (void *)(ptr_type
)
3415 *(word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
]);
3416 else if (udata
->saved
[reg
] == REG_SAVED_REG
&& sub_udata
)
3417 return get_reg (udata
->reg_or_offset
[reg
], sub_udata
, 0);
3422 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3425 put_reg (unsigned reg
, void *val
, frame_state
*udata
)
3427 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3428 *(word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
])
3429 = (word_type
)(ptr_type
) val
;
3434 /* Copy the saved value for register REG from frame UDATA to frame
3435 TARGET_UDATA. Unlike the previous two functions, this can handle
3436 registers that are not one word large. */
3439 copy_reg (unsigned reg
, frame_state
*udata
, frame_state
*target_udata
)
3441 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
3442 && target_udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3443 memcpy (target_udata
->cfa
+ target_udata
->reg_or_offset
[reg
],
3444 udata
->cfa
+ udata
->reg_or_offset
[reg
],
3445 __builtin_dwarf_reg_size (reg
));
3450 /* Retrieve the return address for frame UDATA, where SUB_UDATA is a
3451 frame called by UDATA or 0. */
3453 static inline void *
3454 get_return_addr (frame_state
*udata
, frame_state
*sub_udata
)
3456 return __builtin_extract_return_addr
3457 (get_reg (udata
->retaddr_column
, udata
, sub_udata
));
3460 /* Overwrite the return address for frame UDATA with VAL. */
3463 put_return_addr (void *val
, frame_state
*udata
)
3465 val
= __builtin_frob_return_addr (val
);
3466 put_reg (udata
->retaddr_column
, val
, udata
);
3469 /* Given the current frame UDATA and its return address PC, return the
3470 information about the calling frame in CALLER_UDATA. */
3473 next_stack_level (void *pc
, frame_state
*udata
, frame_state
*caller_udata
)
3475 caller_udata
= __frame_state_for (pc
, caller_udata
);
3479 /* Now go back to our caller's stack frame. If our caller's CFA register
3480 was saved in our stack frame, restore it; otherwise, assume the CFA
3481 register is SP and restore it to our CFA value. */
3482 if (udata
->saved
[caller_udata
->cfa_reg
])
3483 caller_udata
->cfa
= get_reg (caller_udata
->cfa_reg
, udata
, 0);
3485 caller_udata
->cfa
= udata
->cfa
;
3486 caller_udata
->cfa
+= caller_udata
->cfa_offset
;
3488 return caller_udata
;
3491 #ifdef INCOMING_REGNO
3492 /* Is the saved value for register REG in frame UDATA stored in a register
3493 window in the previous frame? */
3496 in_reg_window (int reg
, frame_state
*udata
)
3498 if (udata
->saved
[reg
] != REG_SAVED_OFFSET
)
3501 #ifdef STACK_GROWS_DOWNWARD
3502 return udata
->reg_or_offset
[reg
] > 0;
3504 return udata
->reg_or_offset
[reg
] < 0;
3507 #endif /* INCOMING_REGNO */
3509 /* We first search for an exception handler, and if we don't find
3510 it, we call __terminate on the current stack frame so that we may
3511 use the debugger to walk the stack and understand why no handler
3514 If we find one, then we unwind the frames down to the one that
3515 has the handler and transfer control into the handler. */
3520 struct eh_context
*eh
= (*get_eh_context
) ();
3521 void *saved_pc
, *pc
, *handler
, *retaddr
;
3522 frame_state ustruct
, ustruct2
;
3523 frame_state
*udata
= &ustruct
;
3524 frame_state
*sub_udata
= &ustruct2
;
3525 frame_state my_ustruct
, *my_udata
= &my_ustruct
;
3528 /* This is required for C++ semantics. We must call terminate if we
3529 try and rethrow an exception, when there is no exception currently
3534 /* Start at our stack frame. */
3536 udata
= __frame_state_for (&&label
, udata
);
3540 /* We need to get the value from the CFA register. At this point in
3541 compiling __throw we don't know whether or not we will use the frame
3542 pointer register for the CFA, so we check our unwind info. */
3543 if (udata
->cfa_reg
== __builtin_dwarf_fp_regnum ())
3544 udata
->cfa
= __builtin_fp ();
3546 udata
->cfa
= __builtin_sp ();
3547 udata
->cfa
+= udata
->cfa_offset
;
3549 memcpy (my_udata
, udata
, sizeof (*udata
));
3551 /* Do any necessary initialization to access arbitrary stack frames.
3552 On the SPARC, this means flushing the register windows. */
3553 __builtin_unwind_init ();
3555 /* Now reset pc to the right throw point. */
3556 pc
= __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3562 frame_state
*p
= udata
;
3563 udata
= next_stack_level (pc
, udata
, sub_udata
);
3566 /* If we couldn't find the next frame, we lose. */
3570 handler
= find_exception_handler (pc
, udata
->eh_ptr
);
3572 /* If we found one, we can stop searching. */
3575 args_size
= udata
->args_size
;
3579 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3580 hitting the beginning of the next region. */
3581 pc
= get_return_addr (udata
, sub_udata
) - 1;
3584 /* If we haven't found a handler by now, this is an unhandled
3590 /* We found a handler in the throw context, no need to unwind. */
3596 /* Unwind all the frames between this one and the handler by copying
3597 their saved register values into our register save slots. */
3599 /* Remember the PC where we found the handler. */
3600 void *handler_pc
= pc
;
3602 /* Start from the throw context again. */
3604 memcpy (udata
, my_udata
, sizeof (*udata
));
3606 while (pc
!= handler_pc
)
3608 frame_state
*p
= udata
;
3609 udata
= next_stack_level (pc
, udata
, sub_udata
);
3612 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
3613 if (i
!= udata
->retaddr_column
&& udata
->saved
[i
])
3615 #ifdef INCOMING_REGNO
3616 /* If you modify the saved value of the return address
3617 register on the SPARC, you modify the return address for
3618 your caller's frame. Don't do that here, as it will
3619 confuse get_return_addr. */
3620 if (in_reg_window (i
, udata
)
3621 && udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
3622 && udata
->reg_or_offset
[udata
->retaddr_column
] == i
)
3625 copy_reg (i
, udata
, my_udata
);
3628 pc
= get_return_addr (udata
, sub_udata
) - 1;
3631 #ifdef INCOMING_REGNO
3632 /* But we do need to update the saved return address register from
3633 the last frame we unwind, or the handler frame will have the wrong
3635 if (udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
)
3637 i
= udata
->reg_or_offset
[udata
->retaddr_column
];
3638 if (in_reg_window (i
, udata
))
3639 copy_reg (i
, udata
, my_udata
);
3643 /* udata now refers to the frame called by the handler frame. */
3645 /* Emit the stub to adjust sp and jump to the handler. */
3646 retaddr
= __builtin_eh_stub ();
3648 /* And then set our return address to point to the stub. */
3649 if (my_udata
->saved
[my_udata
->retaddr_column
] == REG_SAVED_OFFSET
)
3650 put_return_addr (retaddr
, my_udata
);
3652 __builtin_set_return_addr_reg (retaddr
);
3654 /* Set up the registers we use to communicate with the stub.
3655 We check STACK_GROWS_DOWNWARD so the stub can use adjust_stack. */
3656 __builtin_set_eh_regs (handler
,
3657 #ifdef STACK_GROWS_DOWNWARD
3658 udata
->cfa
- my_udata
->cfa
3660 my_udata
->cfa
- udata
->cfa
3665 /* Epilogue: restore the handler frame's register values and return
3668 #endif /* DWARF2_UNWIND_INFO */
3674 #ifndef inhibit_libc
3675 /* This gets us __GNU_LIBRARY__. */
3676 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
3679 #ifdef __GNU_LIBRARY__
3680 /* Avoid forcing the library's meaning of `write' on the user program
3681 by using the "internal" name (for use within the library) */
3682 #define write(fd, buf, n) __write((fd), (buf), (n))
3684 #endif /* inhibit_libc */
3686 #define MESSAGE "pure virtual method called\n"
3691 #ifndef inhibit_libc
3692 write (2, MESSAGE
, sizeof (MESSAGE
) - 1);