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 /* Permit the tm.h file to select the endianness to use just for this
46 file. This is used when the endianness is determined when the
49 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
50 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
53 /* In the first part of this file, we are interfacing to calls generated
54 by the compiler itself. These calls pass values into these routines
55 which have very specific modes (rather than very specific types), and
56 these compiler-generated calls also expect any return values to have
57 very specific modes (rather than very specific types). Thus, we need
58 to avoid using regular C language type names in this part of the file
59 because the sizes for those types can be configured to be anything.
60 Instead we use the following special type names. */
62 typedef unsigned int UQItype
__attribute__ ((mode (QI
)));
63 typedef int SItype
__attribute__ ((mode (SI
)));
64 typedef unsigned int USItype
__attribute__ ((mode (SI
)));
65 typedef int DItype
__attribute__ ((mode (DI
)));
66 typedef unsigned int UDItype
__attribute__ ((mode (DI
)));
68 typedef float SFtype
__attribute__ ((mode (SF
)));
69 typedef float DFtype
__attribute__ ((mode (DF
)));
71 #if LONG_DOUBLE_TYPE_SIZE == 96
72 typedef float XFtype
__attribute__ ((mode (XF
)));
74 #if LONG_DOUBLE_TYPE_SIZE == 128
75 typedef float TFtype
__attribute__ ((mode (TF
)));
78 typedef int word_type
__attribute__ ((mode (__word__
)));
80 /* Make sure that we don't accidentally use any normal C language built-in
81 type names in the first part of this file. Instead we want to use *only*
82 the type names defined above. The following macro definitions insure
83 that if we *do* accidentally use some normal C language built-in type name,
84 we will get a syntax error. */
86 #define char bogus_type
87 #define short bogus_type
88 #define int bogus_type
89 #define long bogus_type
90 #define unsigned bogus_type
91 #define float bogus_type
92 #define double bogus_type
94 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
96 /* DIstructs are pairs of SItype values in the order determined by
97 LIBGCC2_WORDS_BIG_ENDIAN. */
99 #if LIBGCC2_WORDS_BIG_ENDIAN
100 struct DIstruct
{SItype high
, low
;};
102 struct DIstruct
{SItype low
, high
;};
105 /* We need this union to unpack/pack DImode values, since we don't have
106 any arithmetic yet. Incoming DImode parameters are stored into the
107 `ll' field, and the unpacked result is read from the struct `s'. */
115 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
116 || defined (L_divdi3) || defined (L_udivdi3) \
117 || defined (L_moddi3) || defined (L_umoddi3))
119 #include "longlong.h"
121 #endif /* udiv or mul */
123 extern DItype
__fixunssfdi (SFtype a
);
124 extern DItype
__fixunsdfdi (DFtype a
);
125 #if LONG_DOUBLE_TYPE_SIZE == 96
126 extern DItype
__fixunsxfdi (XFtype a
);
128 #if LONG_DOUBLE_TYPE_SIZE == 128
129 extern DItype
__fixunstfdi (TFtype a
);
132 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
133 #if defined (L_divdi3) || defined (L_moddi3)
145 w
.s
.high
= -uu
.s
.high
- ((USItype
) w
.s
.low
> 0);
151 /* Unless shift functions are defined whith full ANSI prototypes,
152 parameter b will be promoted to int if word_type is smaller than an int. */
155 __lshrdi3 (DItype u
, word_type b
)
166 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
170 w
.s
.low
= (USItype
)uu
.s
.high
>> -bm
;
174 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
175 w
.s
.high
= (USItype
)uu
.s
.high
>> b
;
176 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
185 __ashldi3 (DItype u
, word_type b
)
196 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
200 w
.s
.high
= (USItype
)uu
.s
.low
<< -bm
;
204 USItype carries
= (USItype
)uu
.s
.low
>> bm
;
205 w
.s
.low
= (USItype
)uu
.s
.low
<< b
;
206 w
.s
.high
= ((USItype
)uu
.s
.high
<< b
) | carries
;
215 __ashrdi3 (DItype u
, word_type b
)
226 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
229 /* w.s.high = 1..1 or 0..0 */
230 w
.s
.high
= uu
.s
.high
>> (sizeof (SItype
) * BITS_PER_UNIT
- 1);
231 w
.s
.low
= uu
.s
.high
>> -bm
;
235 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
236 w
.s
.high
= uu
.s
.high
>> b
;
237 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
251 w
.s
.low
= ffs (uu
.s
.low
);
254 w
.s
.low
= ffs (uu
.s
.high
);
257 w
.s
.low
+= BITS_PER_UNIT
* sizeof (SItype
);
266 __muldi3 (DItype u
, DItype v
)
274 w
.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
);
275 w
.s
.high
+= ((USItype
) uu
.s
.low
* (USItype
) vv
.s
.high
276 + (USItype
) uu
.s
.high
* (USItype
) vv
.s
.low
);
283 #if defined (sdiv_qrnnd)
285 __udiv_w_sdiv (USItype
*rp
, USItype a1
, USItype a0
, USItype d
)
292 if (a1
< d
- a1
- (a0
>> (SI_TYPE_SIZE
- 1)))
294 /* dividend, divisor, and quotient are nonnegative */
295 sdiv_qrnnd (q
, r
, a1
, a0
, d
);
299 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
300 sub_ddmmss (c1
, c0
, a1
, a0
, d
>> 1, d
<< (SI_TYPE_SIZE
- 1));
301 /* Divide (c1*2^32 + c0) by d */
302 sdiv_qrnnd (q
, r
, c1
, c0
, d
);
303 /* Add 2^31 to quotient */
304 q
+= (USItype
) 1 << (SI_TYPE_SIZE
- 1);
309 b1
= d
>> 1; /* d/2, between 2^30 and 2^31 - 1 */
310 c1
= a1
>> 1; /* A/2 */
311 c0
= (a1
<< (SI_TYPE_SIZE
- 1)) + (a0
>> 1);
313 if (a1
< b1
) /* A < 2^32*b1, so A/2 < 2^31*b1 */
315 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
317 r
= 2*r
+ (a0
& 1); /* Remainder from A/(2*b1) */
334 else if (c1
< b1
) /* So 2^31 <= (A/2)/b1 < 2^32 */
337 c0
= ~c0
; /* logical NOT */
339 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
341 q
= ~q
; /* (A/2)/b1 */
344 r
= 2*r
+ (a0
& 1); /* A/(2*b1) */
362 else /* Implies c1 = b1 */
363 { /* Hence a1 = d - 1 = 2*b1 - 1 */
381 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
383 __udiv_w_sdiv (USItype
*rp
, USItype a1
, USItype a0
, USItype d
)
388 #if (defined (L_udivdi3) || defined (L_divdi3) || \
389 defined (L_umoddi3) || defined (L_moddi3))
394 static const UQItype __clz_tab
[] =
396 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,
397 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,
398 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,
399 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,
400 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,
401 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,
402 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,
403 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,
406 #if (defined (L_udivdi3) || defined (L_divdi3) || \
407 defined (L_umoddi3) || defined (L_moddi3))
411 __udivmoddi4 (UDItype n
, UDItype d
, UDItype
*rp
)
416 USItype d0
, d1
, n0
, n1
, n2
;
428 #if !UDIV_NEEDS_NORMALIZATION
435 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
438 /* Remainder in n0. */
445 d0
= 1 / d0
; /* Divide intentionally by zero. */
447 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
448 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
450 /* Remainder in n0. */
461 #else /* UDIV_NEEDS_NORMALIZATION */
469 count_leading_zeros (bm
, d0
);
473 /* Normalize, i.e. make the most significant bit of the
477 n1
= (n1
<< bm
) | (n0
>> (SI_TYPE_SIZE
- bm
));
481 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
484 /* Remainder in n0 >> bm. */
491 d0
= 1 / d0
; /* Divide intentionally by zero. */
493 count_leading_zeros (bm
, d0
);
497 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
498 conclude (the most significant bit of n1 is set) /\ (the
499 leading quotient digit q1 = 1).
501 This special case is necessary, not an optimization.
502 (Shifts counts of SI_TYPE_SIZE are undefined.) */
511 b
= SI_TYPE_SIZE
- bm
;
515 n1
= (n1
<< bm
) | (n0
>> b
);
518 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
523 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
525 /* Remainder in n0 >> bm. */
535 #endif /* UDIV_NEEDS_NORMALIZATION */
546 /* Remainder in n1n0. */
558 count_leading_zeros (bm
, d1
);
561 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
562 conclude (the most significant bit of n1 is set) /\ (the
563 quotient digit q0 = 0 or 1).
565 This special case is necessary, not an optimization. */
567 /* The condition on the next line takes advantage of that
568 n1 >= d1 (true due to program flow). */
569 if (n1
> d1
|| n0
>= d0
)
572 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
591 b
= SI_TYPE_SIZE
- bm
;
593 d1
= (d1
<< bm
) | (d0
>> b
);
596 n1
= (n1
<< bm
) | (n0
>> b
);
599 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
600 umul_ppmm (m1
, m0
, q0
, d0
);
602 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
605 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
610 /* Remainder in (n1n0 - m1m0) >> bm. */
613 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
614 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
615 rr
.s
.high
= n1
>> bm
;
629 UDItype
__udivmoddi4 ();
632 __divdi3 (DItype u
, DItype v
)
643 uu
.ll
= __negdi2 (uu
.ll
);
646 vv
.ll
= __negdi2 (vv
.ll
);
648 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDItype
*) 0);
657 UDItype
__udivmoddi4 ();
659 __moddi3 (DItype u
, DItype v
)
670 uu
.ll
= __negdi2 (uu
.ll
);
672 vv
.ll
= __negdi2 (vv
.ll
);
674 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
683 UDItype
__udivmoddi4 ();
685 __umoddi3 (UDItype u
, UDItype v
)
689 (void) __udivmoddi4 (u
, v
, &w
);
696 UDItype
__udivmoddi4 ();
698 __udivdi3 (UDItype n
, UDItype d
)
700 return __udivmoddi4 (n
, d
, (UDItype
*) 0);
706 __cmpdi2 (DItype a
, DItype b
)
710 au
.ll
= a
, bu
.ll
= b
;
712 if (au
.s
.high
< bu
.s
.high
)
714 else if (au
.s
.high
> bu
.s
.high
)
716 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
718 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
726 __ucmpdi2 (DItype a
, DItype b
)
730 au
.ll
= a
, bu
.ll
= b
;
732 if ((USItype
) au
.s
.high
< (USItype
) bu
.s
.high
)
734 else if ((USItype
) au
.s
.high
> (USItype
) bu
.s
.high
)
736 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
738 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
744 #if defined(L_fixunstfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
745 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
746 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
749 __fixunstfdi (TFtype a
)
757 /* Compute high word of result, as a flonum. */
758 b
= (a
/ HIGH_WORD_COEFF
);
759 /* Convert that to fixed (but not to DItype!),
760 and shift it into the high word. */
763 /* Remove high part from the TFtype, leaving the low part as flonum. */
765 /* Convert that to fixed (but not to DItype!) and add it in.
766 Sometimes A comes out negative. This is significant, since
767 A has more bits than a long int does. */
769 v
-= (USItype
) (- a
);
776 #if defined(L_fixtfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
781 return - __fixunstfdi (-a
);
782 return __fixunstfdi (a
);
786 #if defined(L_fixunsxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
787 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
788 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
791 __fixunsxfdi (XFtype a
)
799 /* Compute high word of result, as a flonum. */
800 b
= (a
/ HIGH_WORD_COEFF
);
801 /* Convert that to fixed (but not to DItype!),
802 and shift it into the high word. */
805 /* Remove high part from the XFtype, leaving the low part as flonum. */
807 /* Convert that to fixed (but not to DItype!) and add it in.
808 Sometimes A comes out negative. This is significant, since
809 A has more bits than a long int does. */
811 v
-= (USItype
) (- a
);
818 #if defined(L_fixxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
823 return - __fixunsxfdi (-a
);
824 return __fixunsxfdi (a
);
829 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
830 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
833 __fixunsdfdi (DFtype a
)
841 /* Compute high word of result, as a flonum. */
842 b
= (a
/ HIGH_WORD_COEFF
);
843 /* Convert that to fixed (but not to DItype!),
844 and shift it into the high word. */
847 /* Remove high part from the DFtype, leaving the low part as flonum. */
849 /* Convert that to fixed (but not to DItype!) and add it in.
850 Sometimes A comes out negative. This is significant, since
851 A has more bits than a long int does. */
853 v
-= (USItype
) (- a
);
865 return - __fixunsdfdi (-a
);
866 return __fixunsdfdi (a
);
871 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
872 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
875 __fixunssfdi (SFtype original_a
)
877 /* Convert the SFtype to a DFtype, because that is surely not going
878 to lose any bits. Some day someone else can write a faster version
879 that avoids converting to DFtype, and verify it really works right. */
880 DFtype a
= original_a
;
887 /* Compute high word of result, as a flonum. */
888 b
= (a
/ HIGH_WORD_COEFF
);
889 /* Convert that to fixed (but not to DItype!),
890 and shift it into the high word. */
893 /* Remove high part from the DFtype, leaving the low part as flonum. */
895 /* Convert that to fixed (but not to DItype!) and add it in.
896 Sometimes A comes out negative. This is significant, since
897 A has more bits than a long int does. */
899 v
-= (USItype
) (- a
);
911 return - __fixunssfdi (-a
);
912 return __fixunssfdi (a
);
916 #if defined(L_floatdixf) && (LONG_DOUBLE_TYPE_SIZE == 96)
917 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
918 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
919 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
922 __floatdixf (DItype u
)
930 d
= (USItype
) (u
>> WORD_SIZE
);
931 d
*= HIGH_HALFWORD_COEFF
;
932 d
*= HIGH_HALFWORD_COEFF
;
933 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
935 return (negate
? -d
: d
);
939 #if defined(L_floatditf) && (LONG_DOUBLE_TYPE_SIZE == 128)
940 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
941 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
942 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
945 __floatditf (DItype u
)
953 d
= (USItype
) (u
>> WORD_SIZE
);
954 d
*= HIGH_HALFWORD_COEFF
;
955 d
*= HIGH_HALFWORD_COEFF
;
956 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
958 return (negate
? -d
: d
);
963 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
964 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
965 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
968 __floatdidf (DItype u
)
976 d
= (USItype
) (u
>> WORD_SIZE
);
977 d
*= HIGH_HALFWORD_COEFF
;
978 d
*= HIGH_HALFWORD_COEFF
;
979 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
981 return (negate
? -d
: d
);
986 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
987 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
988 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
989 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
991 /* Define codes for all the float formats that we know of. Note
992 that this is copied from real.h. */
994 #define UNKNOWN_FLOAT_FORMAT 0
995 #define IEEE_FLOAT_FORMAT 1
996 #define VAX_FLOAT_FORMAT 2
997 #define IBM_FLOAT_FORMAT 3
999 /* Default to IEEE float if not specified. Nearly all machines use it. */
1000 #ifndef HOST_FLOAT_FORMAT
1001 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1004 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1009 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1014 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1020 __floatdisf (DItype u
)
1022 /* Do the calculation in DFmode
1023 so that we don't lose any of the precision of the high word
1024 while multiplying it. */
1031 /* Protect against double-rounding error.
1032 Represent any low-order bits, that might be truncated in DFmode,
1033 by a bit that won't be lost. The bit can go in anywhere below the
1034 rounding position of the SFmode. A fixed mask and bit position
1035 handles all usual configurations. It doesn't handle the case
1036 of 128-bit DImode, however. */
1037 if (DF_SIZE
< DI_SIZE
1038 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1040 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1041 if (u
>= ((UDItype
) 1 << DF_SIZE
))
1043 if ((USItype
) u
& (REP_BIT
- 1))
1047 f
= (USItype
) (u
>> WORD_SIZE
);
1048 f
*= HIGH_HALFWORD_COEFF
;
1049 f
*= HIGH_HALFWORD_COEFF
;
1050 f
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1052 return (SFtype
) (negate
? -f
: f
);
1056 #if defined(L_fixunsxfsi) && LONG_DOUBLE_TYPE_SIZE == 96
1057 /* Reenable the normal types, in case limits.h needs them. */
1070 __fixunsxfsi (XFtype a
)
1072 if (a
>= - (DFtype
) LONG_MIN
)
1073 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1079 /* Reenable the normal types, in case limits.h needs them. */
1092 __fixunsdfsi (DFtype a
)
1094 if (a
>= - (DFtype
) LONG_MIN
)
1095 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1101 /* Reenable the normal types, in case limits.h needs them. */
1114 __fixunssfsi (SFtype a
)
1116 if (a
>= - (SFtype
) LONG_MIN
)
1117 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1122 /* From here on down, the routines use normal data types. */
1124 #define SItype bogus_type
1125 #define USItype bogus_type
1126 #define DItype bogus_type
1127 #define UDItype bogus_type
1128 #define SFtype bogus_type
1129 #define DFtype bogus_type
1141 /* Like bcmp except the sign is meaningful.
1142 Result is negative if S1 is less than S2,
1143 positive if S1 is greater, 0 if S1 and S2 are equal. */
1146 __gcc_bcmp (unsigned char *s1
, unsigned char *s2
, size_t size
)
1150 unsigned char c1
= *s1
++, c2
= *s2
++;
1167 #if defined(__svr4__) || defined(__alliant__)
1171 /* The Alliant needs the added underscore. */
1172 asm (".globl __builtin_saveregs");
1173 asm ("__builtin_saveregs:");
1174 asm (".globl ___builtin_saveregs");
1175 asm ("___builtin_saveregs:");
1177 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1178 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1179 area and also for a new va_list
1181 /* Save all argument registers in the arg reg save area. The
1182 arg reg save area must have the following layout (according
1194 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1195 asm (" fst.q %f12,16(%sp)");
1197 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1198 asm (" st.l %r17,36(%sp)");
1199 asm (" st.l %r18,40(%sp)");
1200 asm (" st.l %r19,44(%sp)");
1201 asm (" st.l %r20,48(%sp)");
1202 asm (" st.l %r21,52(%sp)");
1203 asm (" st.l %r22,56(%sp)");
1204 asm (" st.l %r23,60(%sp)");
1205 asm (" st.l %r24,64(%sp)");
1206 asm (" st.l %r25,68(%sp)");
1207 asm (" st.l %r26,72(%sp)");
1208 asm (" st.l %r27,76(%sp)");
1210 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1211 va_list structure. Put in into
1212 r16 so that it will be returned
1215 /* Initialize all fields of the new va_list structure. This
1216 structure looks like:
1219 unsigned long ireg_used;
1220 unsigned long freg_used;
1226 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1227 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1228 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1229 asm (" bri %r1"); /* delayed return */
1230 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1232 #else /* not __svr4__ */
1233 #if defined(__PARAGON__)
1235 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1236 * and we stand a better chance of hooking into libraries
1237 * compiled by PGI. [andyp@ssd.intel.com]
1241 asm (".globl __builtin_saveregs");
1242 asm ("__builtin_saveregs:");
1243 asm (".globl ___builtin_saveregs");
1244 asm ("___builtin_saveregs:");
1246 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1247 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1248 area and also for a new va_list
1250 /* Save all argument registers in the arg reg save area. The
1251 arg reg save area must have the following layout (according
1263 asm (" fst.q f8, 0(sp)");
1264 asm (" fst.q f12,16(sp)");
1265 asm (" st.l r16,32(sp)");
1266 asm (" st.l r17,36(sp)");
1267 asm (" st.l r18,40(sp)");
1268 asm (" st.l r19,44(sp)");
1269 asm (" st.l r20,48(sp)");
1270 asm (" st.l r21,52(sp)");
1271 asm (" st.l r22,56(sp)");
1272 asm (" st.l r23,60(sp)");
1273 asm (" st.l r24,64(sp)");
1274 asm (" st.l r25,68(sp)");
1275 asm (" st.l r26,72(sp)");
1276 asm (" st.l r27,76(sp)");
1278 asm (" adds 80,sp,r16"); /* compute the address of the new
1279 va_list structure. Put in into
1280 r16 so that it will be returned
1283 /* Initialize all fields of the new va_list structure. This
1284 structure looks like:
1287 unsigned long ireg_used;
1288 unsigned long freg_used;
1294 asm (" st.l r0, 0(r16)"); /* nfixed */
1295 asm (" st.l r0, 4(r16)"); /* nfloating */
1296 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1297 asm (" bri r1"); /* delayed return */
1298 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1299 #else /* not __PARAGON__ */
1303 asm (".globl ___builtin_saveregs");
1304 asm ("___builtin_saveregs:");
1305 asm (" mov sp,r30");
1306 asm (" andnot 0x0f,sp,sp");
1307 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1309 /* Fill in the __va_struct. */
1310 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1311 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1312 asm (" st.l r18, 8(sp)");
1313 asm (" st.l r19,12(sp)");
1314 asm (" st.l r20,16(sp)");
1315 asm (" st.l r21,20(sp)");
1316 asm (" st.l r22,24(sp)");
1317 asm (" st.l r23,28(sp)");
1318 asm (" st.l r24,32(sp)");
1319 asm (" st.l r25,36(sp)");
1320 asm (" st.l r26,40(sp)");
1321 asm (" st.l r27,44(sp)");
1323 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1324 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1326 /* Fill in the __va_ctl. */
1327 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1328 asm (" st.l r28,84(sp)"); /* pointer to more args */
1329 asm (" st.l r0, 88(sp)"); /* nfixed */
1330 asm (" st.l r0, 92(sp)"); /* nfloating */
1332 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1334 asm (" mov r30,sp");
1335 /* recover stack and pass address to start
1337 #endif /* not __PARAGON__ */
1338 #endif /* not __svr4__ */
1339 #else /* not __i860__ */
1341 asm (".global __builtin_saveregs");
1342 asm ("__builtin_saveregs:");
1343 asm (".global ___builtin_saveregs");
1344 asm ("___builtin_saveregs:");
1345 #ifdef NEED_PROC_COMMAND
1348 asm ("st %i0,[%fp+68]");
1349 asm ("st %i1,[%fp+72]");
1350 asm ("st %i2,[%fp+76]");
1351 asm ("st %i3,[%fp+80]");
1352 asm ("st %i4,[%fp+84]");
1354 asm ("st %i5,[%fp+88]");
1355 #ifdef NEED_TYPE_COMMAND
1356 asm (".type __builtin_saveregs,#function");
1357 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1359 #else /* not __sparc__ */
1360 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1363 asm (" .ent __builtin_saveregs");
1364 asm (" .globl __builtin_saveregs");
1365 asm ("__builtin_saveregs:");
1366 asm (" sw $4,0($30)");
1367 asm (" sw $5,4($30)");
1368 asm (" sw $6,8($30)");
1369 asm (" sw $7,12($30)");
1371 asm (" .end __builtin_saveregs");
1372 #else /* not __mips__, etc. */
1375 __builtin_saveregs ()
1380 #endif /* not __mips__ */
1381 #endif /* not __sparc__ */
1382 #endif /* not __i860__ */
1386 #ifndef inhibit_libc
1388 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1390 /* This is used by the `assert' macro. */
1392 __eprintf (const char *string
, const char *expression
,
1393 int line
, const char *filename
)
1395 fprintf (stderr
, string
, expression
, line
, filename
);
1405 /* Structure emitted by -a */
1409 const char *filename
;
1413 const unsigned long *addresses
;
1415 /* Older GCC's did not emit these fields. */
1417 const char **functions
;
1418 const long *line_nums
;
1419 const char **filenames
;
1423 #ifdef BLOCK_PROFILER_CODE
1426 #ifndef inhibit_libc
1428 /* Simple minded basic block profiling output dumper for
1429 systems that don't provide tcov support. At present,
1430 it requires atexit and stdio. */
1432 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1436 #include "gbl-ctors.h"
1437 #include "gcov-io.h"
1439 static struct bb
*bb_head
;
1441 /* Return the number of digits needed to print a value */
1442 /* __inline__ */ static int num_digits (long value
, int base
)
1444 int minus
= (value
< 0 && base
!= 16);
1445 unsigned long v
= (minus
) ? -value
: value
;
1459 __bb_exit_func (void)
1461 FILE *da_file
, *file
;
1468 i
= strlen (bb_head
->filename
) - 3;
1470 if (!strcmp (bb_head
->filename
+i
, ".da"))
1472 /* Must be -fprofile-arcs not -a.
1473 Dump data in a form that gcov expects. */
1477 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1479 /* If the file exists, and the number of counts in it is the same,
1480 then merge them in. */
1482 if ((da_file
= fopen (ptr
->filename
, "r")) != 0)
1490 if (__read_long (&n_counts
, da_file
, 8) != 0)
1492 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1497 if (n_counts
== ptr
->ncounts
)
1501 for (i
= 0; i
< n_counts
; i
++)
1508 if (__read_long (&v
, da_file
, 8) != 0)
1510 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1514 ptr
->counts
[i
] += v
;
1518 if (fclose (da_file
) == EOF
)
1519 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1522 if ((da_file
= fopen (ptr
->filename
, "w")) < 0)
1524 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1529 /* ??? Should first write a header to the file. Perferably, a 4 byte
1530 magic number, 4 bytes containing the time the program was
1531 compiled, 4 bytes containing the last modification time of the
1532 source file, and 4 bytes indicating the compiler options used.
1534 That way we can easily verify that the proper source/executable/
1535 data file combination is being used from gcov. */
1537 if (__write_long (ptr
->ncounts
, da_file
, 8) != 0)
1540 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1546 long *count_ptr
= ptr
->counts
;
1548 for (j
= ptr
->ncounts
; j
> 0; j
--)
1550 if (__write_long (*count_ptr
, da_file
, 8) != 0)
1558 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1562 if (fclose (da_file
) == EOF
)
1563 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1570 /* Must be basic block profiling. Emit a human readable output file. */
1572 file
= fopen ("bb.out", "a");
1581 /* This is somewhat type incorrect, but it avoids worrying about
1582 exactly where time.h is included from. It should be ok unless
1583 a void * differs from other pointer formats, or if sizeof (long)
1584 is < sizeof (time_t). It would be nice if we could assume the
1585 use of rationale standards here. */
1587 time ((void *) &time_value
);
1588 fprintf (file
, "Basic block profiling finished on %s\n", ctime ((void *) &time_value
));
1590 /* We check the length field explicitly in order to allow compatibility
1591 with older GCC's which did not provide it. */
1593 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1596 int func_p
= (ptr
->nwords
>= sizeof (struct bb
)
1597 && ptr
->nwords
<= 1000
1599 int line_p
= (func_p
&& ptr
->line_nums
);
1600 int file_p
= (func_p
&& ptr
->filenames
);
1601 int addr_p
= (ptr
->addresses
!= 0);
1602 long ncounts
= ptr
->ncounts
;
1608 int blk_len
= num_digits (ncounts
, 10);
1613 fprintf (file
, "File %s, %ld basic blocks \n\n",
1614 ptr
->filename
, ncounts
);
1616 /* Get max values for each field. */
1617 for (i
= 0; i
< ncounts
; i
++)
1622 if (cnt_max
< ptr
->counts
[i
])
1623 cnt_max
= ptr
->counts
[i
];
1625 if (addr_p
&& addr_max
< ptr
->addresses
[i
])
1626 addr_max
= ptr
->addresses
[i
];
1628 if (line_p
&& line_max
< ptr
->line_nums
[i
])
1629 line_max
= ptr
->line_nums
[i
];
1633 p
= (ptr
->functions
[i
]) ? (ptr
->functions
[i
]) : "<none>";
1641 p
= (ptr
->filenames
[i
]) ? (ptr
->filenames
[i
]) : "<none>";
1648 addr_len
= num_digits (addr_max
, 16);
1649 cnt_len
= num_digits (cnt_max
, 10);
1650 line_len
= num_digits (line_max
, 10);
1652 /* Now print out the basic block information. */
1653 for (i
= 0; i
< ncounts
; i
++)
1656 " Block #%*d: executed %*ld time(s)",
1658 cnt_len
, ptr
->counts
[i
]);
1661 fprintf (file
, " address= 0x%.*lx", addr_len
,
1665 fprintf (file
, " function= %-*s", func_len
,
1666 (ptr
->functions
[i
]) ? ptr
->functions
[i
] : "<none>");
1669 fprintf (file
, " line= %*ld", line_len
, ptr
->line_nums
[i
]);
1672 fprintf (file
, " file= %s",
1673 (ptr
->filenames
[i
]) ? ptr
->filenames
[i
] : "<none>");
1675 fprintf (file
, "\n");
1678 fprintf (file
, "\n");
1682 fprintf (file
, "\n\n");
1688 __bb_init_func (struct bb
*blocks
)
1690 /* User is supposed to check whether the first word is non-0,
1691 but just in case.... */
1693 if (blocks
->zero_word
)
1697 /* Initialize destructor. */
1699 ON_EXIT (__bb_exit_func
, 0);
1702 /* Set up linked list. */
1703 blocks
->zero_word
= 1;
1704 blocks
->next
= bb_head
;
1708 #ifndef MACHINE_STATE_SAVE
1709 #define MACHINE_STATE_SAVE(ID)
1711 #ifndef MACHINE_STATE_RESTORE
1712 #define MACHINE_STATE_RESTORE(ID)
1717 /* Number of buckets in hashtable of basic block addresses. */
1719 #define BB_BUCKETS 311
1721 /* Maximum length of string in file bb.in. */
1723 #define BBINBUFSIZE 500
1725 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1726 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1728 #define BBINBUFSIZESTR "499"
1732 struct bb_edge
*next
;
1733 unsigned long src_addr
;
1734 unsigned long dst_addr
;
1735 unsigned long count
;
1740 TRACE_KEEP
= 0, TRACE_ON
= 1, TRACE_OFF
= 2
1745 struct bb_func
*next
;
1748 enum bb_func_mode mode
;
1751 /* This is the connection to the outside world.
1752 The BLOCK_PROFILER macro must set __bb.blocks
1753 and __bb.blockno. */
1756 unsigned long blockno
;
1760 /* Vars to store addrs of source and destination basic blocks
1763 static unsigned long bb_src
= 0;
1764 static unsigned long bb_dst
= 0;
1766 static FILE *bb_tracefile
= (FILE *) 0;
1767 static struct bb_edge
**bb_hashbuckets
= (struct bb_edge
**) 0;
1768 static struct bb_func
*bb_func_head
= (struct bb_func
*) 0;
1769 static unsigned long bb_callcount
= 0;
1770 static int bb_mode
= 0;
1772 static unsigned long *bb_stack
= (unsigned long *) 0;
1773 static size_t bb_stacksize
= 0;
1775 static int reported
= 0;
1778 Always : Print execution frequencies of basic blocks
1780 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1781 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1782 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1783 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1788 /*#include <sys/types.h>*/
1789 #include <sys/stat.h>
1790 /*#include <malloc.h>*/
1792 /* Commands executed by gopen. */
1794 #define GOPENDECOMPRESS "gzip -cd "
1795 #define GOPENCOMPRESS "gzip -c >"
1797 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1798 If it does not compile, simply replace gopen by fopen and delete
1799 '.gz' from any first parameter to gopen. */
1802 gopen (char *fn
, char *mode
)
1810 if (mode
[0] != 'r' && mode
[0] != 'w')
1813 p
= fn
+ strlen (fn
)-1;
1814 use_gzip
= ((p
[-1] == '.' && (p
[0] == 'Z' || p
[0] == 'z'))
1815 || (p
[-2] == '.' && p
[-1] == 'g' && p
[0] == 'z'));
1822 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1823 + sizeof (GOPENDECOMPRESS
));
1824 strcpy (s
, GOPENDECOMPRESS
);
1825 strcpy (s
+ (sizeof (GOPENDECOMPRESS
)-1), fn
);
1826 f
= popen (s
, mode
);
1834 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1835 + sizeof (GOPENCOMPRESS
));
1836 strcpy (s
, GOPENCOMPRESS
);
1837 strcpy (s
+ (sizeof (GOPENCOMPRESS
)-1), fn
);
1838 if (!(f
= popen (s
, mode
)))
1839 f
= fopen (s
, mode
);
1846 return fopen (fn
, mode
);
1856 if (!fstat (fileno (f
), &buf
) && S_ISFIFO (buf
.st_mode
))
1864 #endif /* HAVE_POPEN */
1866 /* Called once per program. */
1869 __bb_exit_trace_func ()
1871 FILE *file
= fopen ("bb.out", "a");
1885 gclose (bb_tracefile
);
1887 fclose (bb_tracefile
);
1888 #endif /* HAVE_POPEN */
1891 /* Check functions in `bb.in'. */
1896 const struct bb_func
*p
;
1897 int printed_something
= 0;
1901 /* This is somewhat type incorrect. */
1902 time ((void *) &time_value
);
1904 for (p
= bb_func_head
; p
!= (struct bb_func
*) 0; p
= p
->next
)
1906 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1908 if (!ptr
->filename
|| p
->filename
!= (char *) 0 && strcmp (p
->filename
, ptr
->filename
))
1910 for (blk
= 0; blk
< ptr
->ncounts
; blk
++)
1912 if (!strcmp (p
->funcname
, ptr
->functions
[blk
]))
1917 if (!printed_something
)
1919 fprintf (file
, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value
));
1920 printed_something
= 1;
1923 fprintf (file
, "\tFunction %s", p
->funcname
);
1925 fprintf (file
, " of file %s", p
->filename
);
1926 fprintf (file
, "\n" );
1931 if (printed_something
)
1932 fprintf (file
, "\n");
1938 if (!bb_hashbuckets
)
1942 fprintf (stderr
, "Profiler: out of memory\n");
1952 unsigned long addr_max
= 0;
1953 unsigned long cnt_max
= 0;
1957 /* This is somewhat type incorrect, but it avoids worrying about
1958 exactly where time.h is included from. It should be ok unless
1959 a void * differs from other pointer formats, or if sizeof (long)
1960 is < sizeof (time_t). It would be nice if we could assume the
1961 use of rationale standards here. */
1963 time ((void *) &time_value
);
1964 fprintf (file
, "Basic block jump tracing");
1966 switch (bb_mode
& 12)
1969 fprintf (file
, " (with call)");
1973 /* Print nothing. */
1977 fprintf (file
, " (with call & ret)");
1981 fprintf (file
, " (with ret)");
1985 fprintf (file
, " finished on %s\n", ctime ((void *) &time_value
));
1987 for (i
= 0; i
< BB_BUCKETS
; i
++)
1989 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
1990 for ( ; bucket
; bucket
= bucket
->next
)
1992 if (addr_max
< bucket
->src_addr
)
1993 addr_max
= bucket
->src_addr
;
1994 if (addr_max
< bucket
->dst_addr
)
1995 addr_max
= bucket
->dst_addr
;
1996 if (cnt_max
< bucket
->count
)
1997 cnt_max
= bucket
->count
;
2000 addr_len
= num_digits (addr_max
, 16);
2001 cnt_len
= num_digits (cnt_max
, 10);
2003 for ( i
= 0; i
< BB_BUCKETS
; i
++)
2005 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
2006 for ( ; bucket
; bucket
= bucket
->next
)
2008 fprintf (file
, "Jump from block 0x%.*lx to "
2009 "block 0x%.*lx executed %*d time(s)\n",
2010 addr_len
, bucket
->src_addr
,
2011 addr_len
, bucket
->dst_addr
,
2012 cnt_len
, bucket
->count
);
2016 fprintf (file
, "\n");
2024 /* Free allocated memory. */
2029 struct bb_func
*old
= f
;
2032 if (old
->funcname
) free (old
->funcname
);
2033 if (old
->filename
) free (old
->filename
);
2044 for (i
= 0; i
< BB_BUCKETS
; i
++)
2046 struct bb_edge
*old
, *bucket
= bb_hashbuckets
[i
];
2051 bucket
= bucket
->next
;
2055 free (bb_hashbuckets
);
2058 for (b
= bb_head
; b
; b
= b
->next
)
2059 if (b
->flags
) free (b
->flags
);
2062 /* Called once per program. */
2069 char buf
[BBINBUFSIZE
];
2072 enum bb_func_mode m
;
2075 /* Initialize destructor. */
2076 ON_EXIT (__bb_exit_func
, 0);
2079 if (!(file
= fopen ("bb.in", "r")))
2082 while(fscanf (file
, " %" BBINBUFSIZESTR
"s ", buf
) != EOF
)
2094 if (!strcmp (p
, "__bb_trace__"))
2096 else if (!strcmp (p
, "__bb_jumps__"))
2098 else if (!strcmp (p
, "__bb_hidecall__"))
2100 else if (!strcmp (p
, "__bb_showret__"))
2104 struct bb_func
*f
= (struct bb_func
*) malloc (sizeof (struct bb_func
));
2108 f
->next
= bb_func_head
;
2109 if (pos
= strchr (p
, ':'))
2111 if (!(f
->funcname
= (char *) malloc (strlen (pos
+1)+1)))
2113 strcpy (f
->funcname
, pos
+1);
2115 if ((f
->filename
= (char *) malloc (l
+1)))
2117 strncpy (f
->filename
, p
, l
);
2118 f
->filename
[l
] = '\0';
2121 f
->filename
= (char *) 0;
2125 if (!(f
->funcname
= (char *) malloc (strlen (p
)+1)))
2127 strcpy (f
->funcname
, p
);
2128 f
->filename
= (char *) 0;
2140 bb_tracefile
= gopen ("bbtrace.gz", "w");
2145 bb_tracefile
= fopen ("bbtrace", "w");
2147 #endif /* HAVE_POPEN */
2151 bb_hashbuckets
= (struct bb_edge
**)
2152 malloc (BB_BUCKETS
* sizeof (struct bb_edge
*));
2154 bzero ((char *) bb_hashbuckets
, BB_BUCKETS
);
2160 bb_stack
= (unsigned long *) malloc (bb_stacksize
* sizeof (*bb_stack
));
2164 /* Initialize destructor. */
2165 ON_EXIT (__bb_exit_trace_func
, 0);
2170 /* Called upon entering a basic block. */
2175 struct bb_edge
*bucket
;
2177 MACHINE_STATE_SAVE("1")
2179 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2182 bb_dst
= __bb
.blocks
->addresses
[__bb
.blockno
];
2183 __bb
.blocks
->counts
[__bb
.blockno
]++;
2187 fwrite (&bb_dst
, sizeof (unsigned long), 1, bb_tracefile
);
2192 struct bb_edge
**startbucket
, **oldnext
;
2194 oldnext
= startbucket
2195 = & bb_hashbuckets
[ (((int) bb_src
*8) ^ (int) bb_dst
) % BB_BUCKETS
];
2196 bucket
= *startbucket
;
2198 for (bucket
= *startbucket
; bucket
;
2199 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2201 if (bucket
->src_addr
== bb_src
2202 && bucket
->dst_addr
== bb_dst
)
2205 *oldnext
= bucket
->next
;
2206 bucket
->next
= *startbucket
;
2207 *startbucket
= bucket
;
2212 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2218 fprintf (stderr
, "Profiler: out of memory\n");
2225 bucket
->src_addr
= bb_src
;
2226 bucket
->dst_addr
= bb_dst
;
2227 bucket
->next
= *startbucket
;
2228 *startbucket
= bucket
;
2239 MACHINE_STATE_RESTORE("1")
2243 /* Called when returning from a function and `__bb_showret__' is set. */
2246 __bb_trace_func_ret ()
2248 struct bb_edge
*bucket
;
2250 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2255 struct bb_edge
**startbucket
, **oldnext
;
2257 oldnext
= startbucket
2258 = & bb_hashbuckets
[ (((int) bb_dst
* 8) ^ (int) bb_src
) % BB_BUCKETS
];
2259 bucket
= *startbucket
;
2261 for (bucket
= *startbucket
; bucket
;
2262 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2264 if (bucket
->src_addr
== bb_dst
2265 && bucket
->dst_addr
== bb_src
)
2268 *oldnext
= bucket
->next
;
2269 bucket
->next
= *startbucket
;
2270 *startbucket
= bucket
;
2275 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2281 fprintf (stderr
, "Profiler: out of memory\n");
2288 bucket
->src_addr
= bb_dst
;
2289 bucket
->dst_addr
= bb_src
;
2290 bucket
->next
= *startbucket
;
2291 *startbucket
= bucket
;
2304 /* Called upon entering the first function of a file. */
2307 __bb_init_file (struct bb
*blocks
)
2310 const struct bb_func
*p
;
2311 long blk
, ncounts
= blocks
->ncounts
;
2312 const char **functions
= blocks
->functions
;
2314 /* Set up linked list. */
2315 blocks
->zero_word
= 1;
2316 blocks
->next
= bb_head
;
2321 || !(blocks
->flags
= (char *) malloc (sizeof (char) * blocks
->ncounts
)))
2324 for (blk
= 0; blk
< ncounts
; blk
++)
2325 blocks
->flags
[blk
] = 0;
2327 for (blk
= 0; blk
< ncounts
; blk
++)
2329 for (p
= bb_func_head
; p
; p
= p
->next
)
2331 if (!strcmp (p
->funcname
, functions
[blk
])
2332 && (!p
->filename
|| !strcmp (p
->filename
, blocks
->filename
)))
2334 blocks
->flags
[blk
] |= p
->mode
;
2341 /* Called when exiting from a function. */
2347 MACHINE_STATE_SAVE("2")
2351 if ((bb_mode
& 12) && bb_stacksize
> bb_callcount
)
2353 bb_src
= bb_stack
[bb_callcount
];
2355 __bb_trace_func_ret ();
2361 MACHINE_STATE_RESTORE("2")
2365 /* Called when entering a function. */
2368 __bb_init_trace_func (struct bb
*blocks
, unsigned long blockno
)
2370 static int trace_init
= 0;
2372 MACHINE_STATE_SAVE("3")
2374 if (!blocks
->zero_word
)
2381 __bb_init_file (blocks
);
2391 if (bb_callcount
>= bb_stacksize
)
2393 size_t newsize
= bb_callcount
+ 100;
2395 bb_stack
= (unsigned long *) realloc (bb_stack
, newsize
);
2400 fprintf (stderr
, "Profiler: out of memory\n");
2404 goto stack_overflow
;
2406 bb_stacksize
= newsize
;
2408 bb_stack
[bb_callcount
] = bb_src
;
2419 else if (blocks
->flags
&& (blocks
->flags
[blockno
] & TRACE_ON
))
2425 bb_stack
[bb_callcount
] = bb_src
;
2428 MACHINE_STATE_RESTORE("3")
2431 #endif /* not inhibit_libc */
2432 #endif /* not BLOCK_PROFILER_CODE */
2436 unsigned int __shtab
[] = {
2437 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2438 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2439 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2440 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2441 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2442 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2443 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2444 0x10000000, 0x20000000, 0x40000000, 0x80000000
2448 #ifdef L_clear_cache
2449 /* Clear part of an instruction cache. */
2451 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2454 __clear_cache (char *beg
, char *end
)
2456 #ifdef CLEAR_INSN_CACHE
2457 CLEAR_INSN_CACHE (beg
, end
);
2459 #ifdef INSN_CACHE_SIZE
2460 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
2461 static int initialized
;
2465 typedef (*function_ptr
) ();
2467 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2468 /* It's cheaper to clear the whole cache.
2469 Put in a series of jump instructions so that calling the beginning
2470 of the cache will clear the whole thing. */
2474 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2475 & -INSN_CACHE_LINE_WIDTH
);
2476 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
2478 while (ptr
< end_ptr
)
2480 *(INSTRUCTION_TYPE
*)ptr
2481 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
2482 ptr
+= INSN_CACHE_LINE_WIDTH
;
2484 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
2489 /* Call the beginning of the sequence. */
2490 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2491 & -INSN_CACHE_LINE_WIDTH
))
2494 #else /* Cache is large. */
2498 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2499 & -INSN_CACHE_LINE_WIDTH
);
2501 while (ptr
< (int) array
+ sizeof array
)
2503 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
2504 ptr
+= INSN_CACHE_LINE_WIDTH
;
2510 /* Find the location in array that occupies the same cache line as BEG. */
2512 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
2513 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
2514 & -INSN_CACHE_PLANE_SIZE
)
2517 /* Compute the cache alignment of the place to stop clearing. */
2518 #if 0 /* This is not needed for gcc's purposes. */
2519 /* If the block to clear is bigger than a cache plane,
2520 we clear the entire cache, and OFFSET is already correct. */
2521 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
2523 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
2524 & -INSN_CACHE_LINE_WIDTH
)
2525 & (INSN_CACHE_PLANE_SIZE
- 1));
2527 #if INSN_CACHE_DEPTH > 1
2528 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
2529 if (end_addr
<= start_addr
)
2530 end_addr
+= INSN_CACHE_PLANE_SIZE
;
2532 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
2534 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2535 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2537 while (addr
!= stop
)
2539 /* Call the return instruction at ADDR. */
2540 ((function_ptr
) addr
) ();
2542 addr
+= INSN_CACHE_LINE_WIDTH
;
2545 #else /* just one plane */
2548 /* Call the return instruction at START_ADDR. */
2549 ((function_ptr
) start_addr
) ();
2551 start_addr
+= INSN_CACHE_LINE_WIDTH
;
2553 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
2554 #endif /* just one plane */
2555 #endif /* Cache is large */
2556 #endif /* Cache exists */
2557 #endif /* CLEAR_INSN_CACHE */
2560 #endif /* L_clear_cache */
2564 /* Jump to a trampoline, loading the static chain address. */
2566 #if defined(WINNT) && ! defined(__CYGWIN32__)
2578 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2582 mprotect (char *addr
, int len
, int prot
)
2599 if (VirtualProtect (addr
, len
, np
, &op
))
2607 #ifdef TRANSFER_FROM_TRAMPOLINE
2608 TRANSFER_FROM_TRAMPOLINE
2611 #if defined (NeXT) && defined (__MACH__)
2613 /* Make stack executable so we can call trampolines on stack.
2614 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2618 #include <mach/mach.h>
2622 __enable_execute_stack (char *addr
)
2625 char *eaddr
= addr
+ TRAMPOLINE_SIZE
;
2626 vm_address_t a
= (vm_address_t
) addr
;
2628 /* turn on execute access on stack */
2629 r
= vm_protect (task_self (), a
, TRAMPOLINE_SIZE
, FALSE
, VM_PROT_ALL
);
2630 if (r
!= KERN_SUCCESS
)
2632 mach_error("vm_protect VM_PROT_ALL", r
);
2636 /* We inline the i-cache invalidation for speed */
2638 #ifdef CLEAR_INSN_CACHE
2639 CLEAR_INSN_CACHE (addr
, eaddr
);
2641 __clear_cache ((int) addr
, (int) eaddr
);
2645 #endif /* defined (NeXT) && defined (__MACH__) */
2649 /* Make stack executable so we can call trampolines on stack.
2650 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2652 #include <sys/mman.h>
2653 #include <sys/vmparam.h>
2654 #include <machine/machparam.h>
2657 __enable_execute_stack ()
2660 static unsigned lowest
= USRSTACK
;
2661 unsigned current
= (unsigned) &fp
& -NBPG
;
2663 if (lowest
> current
)
2665 unsigned len
= lowest
- current
;
2666 mremap (current
, &len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
2670 /* Clear instruction cache in case an old trampoline is in it. */
2673 #endif /* __convex__ */
2677 /* Modified from the convex -code above. */
2679 #include <sys/param.h>
2681 #include <sys/m88kbcs.h>
2684 __enable_execute_stack ()
2687 static unsigned long lowest
= USRSTACK
;
2688 unsigned long current
= (unsigned long) &save_errno
& -NBPC
;
2690 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2691 address is seen as 'negative'. That is the case with the stack. */
2694 if (lowest
> current
)
2696 unsigned len
=lowest
-current
;
2697 memctl(current
,len
,MCT_TEXT
);
2701 memctl(current
,NBPC
,MCT_TEXT
);
2705 #endif /* __sysV88__ */
2709 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2711 #include <sys/mman.h>
2712 #include <sys/types.h>
2713 #include <sys/param.h>
2714 #include <sys/vmmac.h>
2716 /* Modified from the convex -code above.
2717 mremap promises to clear the i-cache. */
2720 __enable_execute_stack ()
2723 if (mprotect (((unsigned int)&fp
/PAGSIZ
)*PAGSIZ
, PAGSIZ
,
2724 PROT_READ
|PROT_WRITE
|PROT_EXEC
))
2726 perror ("mprotect in __enable_execute_stack");
2731 #endif /* __pyr__ */
2733 #if defined (sony_news) && defined (SYSTYPE_BSD)
2736 #include <sys/types.h>
2737 #include <sys/param.h>
2738 #include <syscall.h>
2739 #include <machine/sysnews.h>
2741 /* cacheflush function for NEWS-OS 4.2.
2742 This function is called from trampoline-initialize code
2743 defined in config/mips/mips.h. */
2746 cacheflush (char *beg
, int size
, int flag
)
2748 if (syscall (SYS_sysnews
, NEWS_CACHEFLUSH
, beg
, size
, FLUSH_BCACHE
))
2750 perror ("cache_flush");
2756 #endif /* sony_news */
2757 #endif /* L_trampoline */
2761 #include "gbl-ctors.h"
2762 /* Some systems use __main in a way incompatible with its use in gcc, in these
2763 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2764 give the same symbol without quotes for an alternative entry point. You
2765 must define both, or neither. */
2767 #define NAME__MAIN "__main"
2768 #define SYMBOL__MAIN __main
2771 #ifdef INIT_SECTION_ASM_OP
2772 #undef HAS_INIT_SECTION
2773 #define HAS_INIT_SECTION
2776 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2777 /* Run all the global destructors on exit from the program. */
2780 __do_global_dtors ()
2782 #ifdef DO_GLOBAL_DTORS_BODY
2783 DO_GLOBAL_DTORS_BODY
;
2785 static func_ptr
*p
= __DTOR_LIST__
+ 1;
2795 #ifndef HAS_INIT_SECTION
2796 /* Run all the global constructors on entry to the program. */
2799 #define ON_EXIT(a, b)
2801 /* Make sure the exit routine is pulled in to define the globals as
2802 bss symbols, just in case the linker does not automatically pull
2803 bss definitions from the library. */
2805 extern int _exit_dummy_decl
;
2806 int *_exit_dummy_ref
= &_exit_dummy_decl
;
2807 #endif /* ON_EXIT */
2810 __do_global_ctors ()
2812 DO_GLOBAL_CTORS_BODY
;
2813 ON_EXIT (__do_global_dtors
, 0);
2815 #endif /* no HAS_INIT_SECTION */
2817 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2818 /* Subroutine called automatically by `main'.
2819 Compiling a global function named `main'
2820 produces an automatic call to this function at the beginning.
2822 For many systems, this routine calls __do_global_ctors.
2823 For systems which support a .init section we use the .init section
2824 to run __do_global_ctors, so we need not do anything here. */
2829 /* Support recursive calls to `main': run initializers just once. */
2830 static int initialized
;
2834 __do_global_ctors ();
2837 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2839 #endif /* L__main */
2843 #include "gbl-ctors.h"
2845 /* Provide default definitions for the lists of constructors and
2846 destructors, so that we don't get linker errors. These symbols are
2847 intentionally bss symbols, so that gld and/or collect will provide
2848 the right values. */
2850 /* We declare the lists here with two elements each,
2851 so that they are valid empty lists if no other definition is loaded. */
2852 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2853 #if defined(__NeXT__) || defined(_AIX)
2854 /* After 2.3, try this definition on all systems. */
2855 func_ptr __CTOR_LIST__
[2] = {0, 0};
2856 func_ptr __DTOR_LIST__
[2] = {0, 0};
2858 func_ptr __CTOR_LIST__
[2];
2859 func_ptr __DTOR_LIST__
[2];
2861 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2862 #endif /* L_ctors */
2866 #include "gbl-ctors.h"
2872 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2880 static func_ptr
*atexit_chain
= 0;
2881 static long atexit_chain_length
= 0;
2882 static volatile long last_atexit_chain_slot
= -1;
2884 int atexit (func_ptr func
)
2886 if (++last_atexit_chain_slot
== atexit_chain_length
)
2888 atexit_chain_length
+= 32;
2890 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
2891 * sizeof (func_ptr
));
2893 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
2894 * sizeof (func_ptr
));
2897 atexit_chain_length
= 0;
2898 last_atexit_chain_slot
= -1;
2903 atexit_chain
[last_atexit_chain_slot
] = func
;
2906 #endif /* NEED_ATEXIT */
2908 /* If we have no known way of registering our own __do_global_dtors
2909 routine so that it will be invoked at program exit time, then we
2910 have to define our own exit routine which will get this to happen. */
2912 extern void __do_global_dtors ();
2913 extern void __bb_exit_func ();
2914 extern void _cleanup ();
2915 extern void _exit () __attribute__ ((noreturn
));
2920 #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
2924 for ( ; last_atexit_chain_slot
-- >= 0; )
2926 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
2927 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
2929 free (atexit_chain
);
2932 #else /* No NEED_ATEXIT */
2933 __do_global_dtors ();
2934 #endif /* No NEED_ATEXIT */
2936 #ifndef inhibit_libc
2948 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2955 /* Shared exception handling support routines. */
2957 /* Language-specific information about the active exception(s). If there
2958 are no active exceptions, it is set to 0. */
2962 __default_terminate ()
2967 void (*__terminate_func
)() = __default_terminate
;
2972 (*__terminate_func
)();
2976 __throw_type_match (void *catch_type
, void *throw_type
, void *obj
)
2979 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
2980 catch_type
, throw_type
);
2982 if (strcmp ((const char *)catch_type
, (const char *)throw_type
) == 0)
2992 /* Support routines for setjmp/longjmp exception handling. */
2994 /* Calls to __sjthrow are generated by the compiler when an exception
2995 is raised when using the setjmp/longjmp exception handling codegen
2998 extern void longjmp (void *, int);
3000 static void *top_elt
[2];
3001 void **__dynamic_handler_chain
= top_elt
;
3003 /* Routine to get the head of the current thread's dynamic handler chain
3004 use for exception handling.
3006 TODO: make thread safe. */
3009 __get_dynamic_handler_chain ()
3011 return &__dynamic_handler_chain
;
3014 /* This is used to throw an exception when the setjmp/longjmp codegen
3015 method is used for exception handling.
3017 We call __terminate if there are no handlers left (we know this
3018 when the dynamic handler chain is top_elt). Otherwise we run the
3019 cleanup actions off the dynamic cleanup stack, and pop the top of
3020 the dynamic handler chain, and use longjmp to transfer back to the
3021 associated handler. */
3026 void ***dhc
= __get_dynamic_handler_chain ();
3028 void (*func
)(void *, int);
3032 /* The cleanup chain is one word into the buffer. Get the cleanup
3034 cleanup
= (void***)&(*dhc
)[1];
3036 /* If there are any cleanups in the chain, run them now. */
3040 void **buf
= (void**)store
;
3045 #ifdef DONT_USE_BUILTIN_SETJMP
3046 if (! setjmp (&buf
[2]))
3048 if (! __builtin_setjmp (&buf
[2]))
3054 func
= (void(*)(void*, int))cleanup
[0][1];
3055 arg
= (void*)cleanup
[0][2];
3057 /* Update this before running the cleanup. */
3058 cleanup
[0] = (void **)cleanup
[0][0];
3071 /* We must call terminate if we try and rethrow an exception, when
3072 there is no exception currently active and when there are no
3074 if (! __eh_info
|| (*dhc
) == top_elt
)
3077 /* Find the jmpbuf associated with the top element of the dynamic
3078 handler chain. The jumpbuf starts two words into the buffer. */
3079 jmpbuf
= &(*dhc
)[2];
3081 /* Then we pop the top element off the dynamic handler chain. */
3082 *dhc
= (void**)(*dhc
)[0];
3084 /* And then we jump to the handler. */
3086 #ifdef DONT_USE_BUILTIN_SETJMP
3087 longjmp (jmpbuf
, 1);
3089 __builtin_longjmp (jmpbuf
, 1);
3093 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3094 handler, then pop the handler off the dynamic handler stack, and
3095 then throw. This is used to skip the first handler, and transfer
3096 control to the next handler in the dynamic handler stack. */
3101 void ***dhc
= __get_dynamic_handler_chain ();
3103 void (*func
)(void *, int);
3107 /* The cleanup chain is one word into the buffer. Get the cleanup
3109 cleanup
= (void***)&(*dhc
)[1];
3111 /* If there are any cleanups in the chain, run them now. */
3115 void **buf
= (void**)store
;
3120 #ifdef DONT_USE_BUILTIN_SETJMP
3121 if (! setjmp (&buf
[2]))
3123 if (! __builtin_setjmp (&buf
[2]))
3129 func
= (void(*)(void*, int))cleanup
[0][1];
3130 arg
= (void*)cleanup
[0][2];
3132 /* Update this before running the cleanup. */
3133 cleanup
[0] = (void **)cleanup
[0][0];
3146 /* Then we pop the top element off the dynamic handler chain. */
3147 *dhc
= (void**)(*dhc
)[0];
3152 /* Support code for all exception region-based exception handling. */
3154 /* This value identifies the place from which an exception is being
3159 #ifdef EH_TABLE_LOOKUP
3165 typedef struct exception_table
{
3168 void *exception_handler
;
3171 /* This routine takes a PC and a pointer to the exception region TABLE for
3172 its translation unit, and returns the address of the exception handler
3173 associated with the closest exception table handler entry associated
3174 with that PC, or 0 if there are no table entries the PC fits in.
3176 In the advent of a tie, we have to give the last entry, as it represents
3180 find_exception_handler (void *pc
, exception_table
*table
)
3187 /* We can't do a binary search because the table isn't guaranteed
3188 to be sorted from function to function. */
3189 for (pos
= 0; table
[pos
].exception_handler
!= (void *) -1; ++pos
)
3191 if (table
[pos
].start
<= pc
&& table
[pos
].end
> pc
)
3193 /* This can apply. Make sure it is at least as small as
3194 the previous best. */
3195 if (best
== -1 || (table
[pos
].end
<= table
[best
].end
3196 && table
[pos
].start
>= table
[best
].start
))
3199 /* But it is sorted by starting PC within a function. */
3200 else if (best
>= 0 && table
[pos
].start
> pc
)
3204 return table
[best
].exception_handler
;
3209 #endif /* EH_TABLE_LOOKUP */
3211 #ifndef DWARF2_UNWIND_INFO
3212 /* Support code for exception handling using inline unwinders or
3213 __unwind_function. */
3215 #ifndef EH_TABLE_LOOKUP
3216 typedef struct exception_table_node
{
3217 exception_table
*table
;
3220 struct exception_table_node
*next
;
3221 } exception_table_node
;
3223 static struct exception_table_node
*exception_table_list
;
3226 __find_first_exception_table_match (void *pc
)
3228 register exception_table_node
*tnp
;
3230 for (tnp
= exception_table_list
; tnp
!= 0; tnp
= tnp
->next
)
3232 if (tnp
->start
<= pc
&& tnp
->end
>= pc
)
3233 return find_exception_handler (pc
, tnp
->table
);
3240 __register_exceptions (exception_table
*table
)
3242 exception_table_node
*node
;
3243 exception_table
*range
= table
+ 1;
3245 if (range
->start
== (void *) -1)
3248 node
= (exception_table_node
*) malloc (sizeof (exception_table_node
));
3249 node
->table
= table
;
3251 /* This look can be optimized away either if the table
3252 is sorted, or if we pass in extra parameters. */
3253 node
->start
= range
->start
;
3254 node
->end
= range
->end
;
3255 for (range
++ ; range
->start
!= (void *) (-1); range
++)
3257 if (range
->start
< node
->start
)
3258 node
->start
= range
->start
;
3259 if (range
->end
> node
->end
)
3260 node
->end
= range
->end
;
3263 node
->next
= exception_table_list
;
3264 exception_table_list
= node
;
3266 #endif /* !EH_TABLE_LOOKUP */
3268 /* Throw stub routine.
3270 This is work in progress, but not completed yet. */
3278 /* See expand_builtin_throw for details. */
3280 void **__eh_pcnthrow () {
3281 static void *buf
[2] = {
3290 __unwind_function(void *ptr
)
3292 asm("movl 8(%esp),%ecx");
3293 /* Undo current frame */
3294 asm("movl %ebp,%esp");
3296 /* like ret, but stay here */
3297 asm("addl $4,%esp");
3299 /* Now, undo previous frame. */
3300 /* This is a test routine, as we have to dynamically probe to find out
3301 what to pop for certain, this is just a guess. */
3302 asm("leal -16(%ebp),%esp");
3306 asm("movl %ebp,%esp");
3309 asm("movl %ecx,0(%esp)");
3312 #elif #machine(rs6000) && !defined _ARCH_PPC
3313 __unwind_function(void *ptr
)
3322 /* use 31 as a scratch register to restore the link register. */
3323 asm("l 31, 8(1);mtlr 31 # l lr,8(1)");
3326 asm("mtctr 3;bctr # b 3");
3328 #elif (#machine(rs6000) || #machine(powerpc)) && defined _ARCH_PPC
3329 __unwind_function(void *ptr
)
3333 asm("lwz 31,-4(1)");
3338 /* use 31 as a scratch register to restore the link register. */
3339 asm("lwz 31, 8(1);mtlr 31 # l lr,8(1)");
3340 asm("lwz 31,-4(1)");
3342 asm("mtctr 3;bctr # b 3");
3345 __unwind_function(void *ptr
)
3347 __label__ return_again
;
3349 /* Replace our frame's return address with the label below.
3350 During execution, we will first return here instead of to
3351 caller, then second return takes caller's frame off the stack.
3352 Two returns matches two actual calls, so is less likely to
3353 confuse debuggers. `16' corresponds to RETURN_ADDRESS_OFFSET. */
3354 __asm ("movl %0,16(fp)" : : "p" (&& return_again
));
3361 __unwind_function(void *ptr
)
3365 #endif /* powerpc */
3367 #else /* DWARF2_UNWIND_INFO */
3368 /* Support code for exception handling using static unwind information. */
3372 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3373 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3374 avoid a warning about casting between int and pointer of different
3377 typedef int ptr_type
__attribute__ ((mode (pointer
)));
3379 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3380 frame called by UDATA or 0. */
3383 get_reg (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3385 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3386 return (void *)(ptr_type
)
3387 *(word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
]);
3388 else if (udata
->saved
[reg
] == REG_SAVED_REG
&& sub_udata
)
3389 return get_reg (udata
->reg_or_offset
[reg
], sub_udata
, 0);
3394 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3397 put_reg (unsigned reg
, void *val
, frame_state
*udata
)
3399 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3400 *(word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
])
3401 = (word_type
)(ptr_type
) val
;
3406 /* Copy the saved value for register REG from frame UDATA to frame
3407 TARGET_UDATA. Unlike the previous two functions, this can handle
3408 registers that are not one word large. */
3411 copy_reg (unsigned reg
, frame_state
*udata
, frame_state
*target_udata
)
3413 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
3414 && target_udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3415 memcpy (target_udata
->cfa
+ target_udata
->reg_or_offset
[reg
],
3416 udata
->cfa
+ udata
->reg_or_offset
[reg
],
3417 __builtin_dwarf_reg_size (reg
));
3422 /* Retrieve the return address for frame UDATA, where SUB_UDATA is a
3423 frame called by UDATA or 0. */
3425 static inline void *
3426 get_return_addr (frame_state
*udata
, frame_state
*sub_udata
)
3428 return __builtin_extract_return_addr
3429 (get_reg (udata
->retaddr_column
, udata
, sub_udata
));
3432 /* Overwrite the return address for frame UDATA with VAL. */
3435 put_return_addr (void *val
, frame_state
*udata
)
3437 val
= __builtin_frob_return_addr (val
);
3438 put_reg (udata
->retaddr_column
, val
, udata
);
3441 /* Given the current frame UDATA and its return address PC, return the
3442 information about the calling frame in CALLER_UDATA. */
3445 next_stack_level (void *pc
, frame_state
*udata
, frame_state
*caller_udata
)
3447 caller_udata
= __frame_state_for (pc
, caller_udata
);
3451 /* Now go back to our caller's stack frame. If our caller's CFA register
3452 was saved in our stack frame, restore it; otherwise, assume the CFA
3453 register is SP and restore it to our CFA value. */
3454 if (udata
->saved
[caller_udata
->cfa_reg
])
3455 caller_udata
->cfa
= get_reg (caller_udata
->cfa_reg
, udata
, 0);
3457 caller_udata
->cfa
= udata
->cfa
;
3458 caller_udata
->cfa
+= caller_udata
->cfa_offset
;
3460 return caller_udata
;
3463 #ifdef INCOMING_REGNO
3464 /* Is the saved value for register REG in frame UDATA stored in a register
3465 window in the previous frame? */
3468 in_reg_window (int reg
, frame_state
*udata
)
3470 if (udata
->saved
[reg
] != REG_SAVED_OFFSET
)
3473 #ifdef STACK_GROWS_DOWNWARD
3474 return udata
->reg_or_offset
[reg
] > 0;
3476 return udata
->reg_or_offset
[reg
] < 0;
3479 #endif /* INCOMING_REGNO */
3481 /* We first search for an exception handler, and if we don't find
3482 it, we call __terminate on the current stack frame so that we may
3483 use the debugger to walk the stack and understand why no handler
3486 If we find one, then we unwind the frames down to the one that
3487 has the handler and transfer control into the handler. */
3492 void *pc
, *handler
, *retaddr
;
3493 frame_state ustruct
, ustruct2
;
3494 frame_state
*udata
= &ustruct
;
3495 frame_state
*sub_udata
= &ustruct2
;
3496 frame_state my_ustruct
, *my_udata
= &my_ustruct
;
3499 /* This is required for C++ semantics. We must call terminate if we
3500 try and rethrow an exception, when there is no exception currently
3505 /* Start at our stack frame. */
3507 udata
= __frame_state_for (&&label
, udata
);
3511 /* We need to get the value from the CFA register. At this point in
3512 compiling __throw we don't know whether or not we will use the frame
3513 pointer register for the CFA, so we check our unwind info. */
3514 if (udata
->cfa_reg
== __builtin_dwarf_fp_regnum ())
3515 udata
->cfa
= __builtin_fp ();
3517 udata
->cfa
= __builtin_sp ();
3518 udata
->cfa
+= udata
->cfa_offset
;
3520 memcpy (my_udata
, udata
, sizeof (*udata
));
3522 /* Do any necessary initialization to access arbitrary stack frames.
3523 On the SPARC, this means flushing the register windows. */
3524 __builtin_unwind_init ();
3526 /* Now reset pc to the right throw point. */
3531 frame_state
*p
= udata
;
3532 udata
= next_stack_level (pc
, udata
, sub_udata
);
3535 /* If we couldn't find the next frame, we lose. */
3539 handler
= find_exception_handler (pc
, udata
->eh_ptr
);
3541 /* If we found one, we can stop searching. */
3544 args_size
= udata
->args_size
;
3548 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3549 hitting the beginning of the next region. */
3550 pc
= get_return_addr (udata
, sub_udata
) - 1;
3553 /* If we haven't found a handler by now, this is an unhandled
3559 /* We found a handler in the throw context, no need to unwind. */
3566 /* Unwind all the frames between this one and the handler by copying
3567 their saved register values into our register save slots. */
3569 /* Remember the PC where we found the handler. */
3570 void *handler_pc
= pc
;
3572 /* Start from the throw context again. */
3574 memcpy (udata
, my_udata
, sizeof (*udata
));
3576 while (pc
!= handler_pc
)
3578 frame_state
*p
= udata
;
3579 udata
= next_stack_level (pc
, udata
, sub_udata
);
3582 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
3583 if (i
!= udata
->retaddr_column
&& udata
->saved
[i
])
3585 #ifdef INCOMING_REGNO
3586 /* If you modify the saved value of the return address
3587 register on the SPARC, you modify the return address for
3588 your caller's frame. Don't do that here, as it will
3589 confuse get_return_addr. */
3590 if (in_reg_window (i
, udata
)
3591 && udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
3592 && udata
->reg_or_offset
[udata
->retaddr_column
] == i
)
3595 copy_reg (i
, udata
, my_udata
);
3598 pc
= get_return_addr (udata
, sub_udata
) - 1;
3601 #ifdef INCOMING_REGNO
3602 /* But we do need to update the saved return address register from
3603 the last frame we unwind, or the handler frame will have the wrong
3605 if (udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
)
3607 i
= udata
->reg_or_offset
[udata
->retaddr_column
];
3608 if (in_reg_window (i
, udata
))
3609 copy_reg (i
, udata
, my_udata
);
3613 /* udata now refers to the frame called by the handler frame. */
3615 /* Emit the stub to adjust sp and jump to the handler. */
3616 retaddr
= __builtin_eh_stub ();
3618 /* And then set our return address to point to the stub. */
3619 if (my_udata
->saved
[my_udata
->retaddr_column
] == REG_SAVED_OFFSET
)
3620 put_return_addr (retaddr
, my_udata
);
3622 __builtin_set_return_addr_reg (retaddr
);
3624 /* Set up the registers we use to communicate with the stub.
3625 We check STACK_GROWS_DOWNWARD so the stub can use adjust_stack. */
3626 __builtin_set_eh_regs (handler
,
3627 #ifdef STACK_GROWS_DOWNWARD
3628 udata
->cfa
- my_udata
->cfa
3630 my_udata
->cfa
- udata
->cfa
3635 /* Epilogue: restore the handler frame's register values and return
3638 #endif /* !DWARF2_UNWIND_INFO */
3643 #ifndef inhibit_libc
3644 /* This gets us __GNU_LIBRARY__. */
3645 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
3648 #ifdef __GNU_LIBRARY__
3649 /* Avoid forcing the library's meaning of `write' on the user program
3650 by using the "internal" name (for use within the library) */
3651 #define write(fd, buf, n) __write((fd), (buf), (n))
3653 #endif /* inhibit_libc */
3655 #define MESSAGE "pure virtual method called\n"
3660 #ifndef inhibit_libc
3661 write (2, MESSAGE
, sizeof (MESSAGE
) - 1);