1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 92-98, 1999 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
35 /* We disable this when inhibit_libc, so that gcc can still be built without
36 needing header files first. */
37 /* ??? This is not a good solution, since prototypes may be required in
38 some cases for correct code. See also frame.c. */
40 /* fixproto guarantees these system headers exist. */
51 /* Don't use `fancy_abort' here even if config.h says to use it. */
56 #if (SUPPORTS_WEAK == 1) && (defined (ASM_OUTPUT_DEF) || defined (ASM_OUTPUT_WEAK_ALIAS))
60 /* In a cross-compilation situation, default to inhibiting compilation
61 of routines that use libc. */
63 #if defined(CROSS_COMPILE) && !defined(inhibit_libc)
67 /* Permit the tm.h file to select the endianness to use just for this
68 file. This is used when the endianness is determined when the
71 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
72 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
75 #ifndef LIBGCC2_LONG_DOUBLE_TYPE_SIZE
76 #define LIBGCC2_LONG_DOUBLE_TYPE_SIZE LONG_DOUBLE_TYPE_SIZE
79 /* In the first part of this file, we are interfacing to calls generated
80 by the compiler itself. These calls pass values into these routines
81 which have very specific modes (rather than very specific types), and
82 these compiler-generated calls also expect any return values to have
83 very specific modes (rather than very specific types). Thus, we need
84 to avoid using regular C language type names in this part of the file
85 because the sizes for those types can be configured to be anything.
86 Instead we use the following special type names. */
88 typedef unsigned int UQItype
__attribute__ ((mode (QI
)));
89 typedef int SItype
__attribute__ ((mode (SI
)));
90 typedef unsigned int USItype
__attribute__ ((mode (SI
)));
91 typedef int DItype
__attribute__ ((mode (DI
)));
92 typedef unsigned int UDItype
__attribute__ ((mode (DI
)));
94 typedef float SFtype
__attribute__ ((mode (SF
)));
95 typedef float DFtype
__attribute__ ((mode (DF
)));
97 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
98 typedef float XFtype
__attribute__ ((mode (XF
)));
100 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
101 typedef float TFtype
__attribute__ ((mode (TF
)));
104 typedef int word_type
__attribute__ ((mode (__word__
)));
106 /* Make sure that we don't accidentally use any normal C language built-in
107 type names in the first part of this file. Instead we want to use *only*
108 the type names defined above. The following macro definitions insure
109 that if we *do* accidentally use some normal C language built-in type name,
110 we will get a syntax error. */
112 #define char bogus_type
113 #define short bogus_type
114 #define int bogus_type
115 #define long bogus_type
116 #define unsigned bogus_type
117 #define float bogus_type
118 #define double bogus_type
120 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
122 /* DIstructs are pairs of SItype values in the order determined by
123 LIBGCC2_WORDS_BIG_ENDIAN. */
125 #if LIBGCC2_WORDS_BIG_ENDIAN
126 struct DIstruct
{SItype high
, low
;};
128 struct DIstruct
{SItype low
, high
;};
131 /* We need this union to unpack/pack DImode values, since we don't have
132 any arithmetic yet. Incoming DImode parameters are stored into the
133 `ll' field, and the unpacked result is read from the struct `s'. */
141 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
142 || defined (L_divdi3) || defined (L_udivdi3) \
143 || defined (L_moddi3) || defined (L_umoddi3))
145 #include "longlong.h"
147 #endif /* udiv or mul */
149 extern DItype
__fixunssfdi (SFtype a
);
150 extern DItype
__fixunsdfdi (DFtype a
);
151 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
152 extern DItype
__fixunsxfdi (XFtype a
);
154 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
155 extern DItype
__fixunstfdi (TFtype a
);
158 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
159 #if defined (L_divdi3) || defined (L_moddi3)
171 w
.s
.high
= -uu
.s
.high
- ((USItype
) w
.s
.low
> 0);
177 /* Unless shift functions are defined whith full ANSI prototypes,
178 parameter b will be promoted to int if word_type is smaller than an int. */
181 __lshrdi3 (DItype u
, word_type b
)
192 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
196 w
.s
.low
= (USItype
)uu
.s
.high
>> -bm
;
200 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
201 w
.s
.high
= (USItype
)uu
.s
.high
>> b
;
202 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
211 __ashldi3 (DItype u
, word_type b
)
222 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
226 w
.s
.high
= (USItype
)uu
.s
.low
<< -bm
;
230 USItype carries
= (USItype
)uu
.s
.low
>> bm
;
231 w
.s
.low
= (USItype
)uu
.s
.low
<< b
;
232 w
.s
.high
= ((USItype
)uu
.s
.high
<< b
) | carries
;
241 __ashrdi3 (DItype u
, word_type b
)
252 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
255 /* w.s.high = 1..1 or 0..0 */
256 w
.s
.high
= uu
.s
.high
>> (sizeof (SItype
) * BITS_PER_UNIT
- 1);
257 w
.s
.low
= uu
.s
.high
>> -bm
;
261 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
262 w
.s
.high
= uu
.s
.high
>> b
;
263 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
277 w
.s
.low
= ffs (uu
.s
.low
);
280 w
.s
.low
= ffs (uu
.s
.high
);
283 w
.s
.low
+= BITS_PER_UNIT
* sizeof (SItype
);
292 __muldi3 (DItype u
, DItype v
)
300 w
.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
);
301 w
.s
.high
+= ((USItype
) uu
.s
.low
* (USItype
) vv
.s
.high
302 + (USItype
) uu
.s
.high
* (USItype
) vv
.s
.low
);
309 #if defined (sdiv_qrnnd)
311 __udiv_w_sdiv (USItype
*rp
, USItype a1
, USItype a0
, USItype d
)
318 if (a1
< d
- a1
- (a0
>> (SI_TYPE_SIZE
- 1)))
320 /* dividend, divisor, and quotient are nonnegative */
321 sdiv_qrnnd (q
, r
, a1
, a0
, d
);
325 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
326 sub_ddmmss (c1
, c0
, a1
, a0
, d
>> 1, d
<< (SI_TYPE_SIZE
- 1));
327 /* Divide (c1*2^32 + c0) by d */
328 sdiv_qrnnd (q
, r
, c1
, c0
, d
);
329 /* Add 2^31 to quotient */
330 q
+= (USItype
) 1 << (SI_TYPE_SIZE
- 1);
335 b1
= d
>> 1; /* d/2, between 2^30 and 2^31 - 1 */
336 c1
= a1
>> 1; /* A/2 */
337 c0
= (a1
<< (SI_TYPE_SIZE
- 1)) + (a0
>> 1);
339 if (a1
< b1
) /* A < 2^32*b1, so A/2 < 2^31*b1 */
341 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
343 r
= 2*r
+ (a0
& 1); /* Remainder from A/(2*b1) */
360 else if (c1
< b1
) /* So 2^31 <= (A/2)/b1 < 2^32 */
363 c0
= ~c0
; /* logical NOT */
365 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
367 q
= ~q
; /* (A/2)/b1 */
370 r
= 2*r
+ (a0
& 1); /* A/(2*b1) */
388 else /* Implies c1 = b1 */
389 { /* Hence a1 = d - 1 = 2*b1 - 1 */
407 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
409 __udiv_w_sdiv (USItype
*rp
__attribute__ ((__unused__
)),
410 USItype a1
__attribute__ ((__unused__
)),
411 USItype a0
__attribute__ ((__unused__
)),
412 USItype d
__attribute__ ((__unused__
)))
419 #if (defined (L_udivdi3) || defined (L_divdi3) || \
420 defined (L_umoddi3) || defined (L_moddi3))
425 static const UQItype __clz_tab
[] =
427 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,
428 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,
429 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,
430 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,
431 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,
432 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,
433 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,
434 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,
437 #if (defined (L_udivdi3) || defined (L_divdi3) || \
438 defined (L_umoddi3) || defined (L_moddi3))
442 __udivmoddi4 (UDItype n
, UDItype d
, UDItype
*rp
)
447 USItype d0
, d1
, n0
, n1
, n2
;
459 #if !UDIV_NEEDS_NORMALIZATION
466 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
469 /* Remainder in n0. */
476 d0
= 1 / d0
; /* Divide intentionally by zero. */
478 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
479 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
481 /* Remainder in n0. */
492 #else /* UDIV_NEEDS_NORMALIZATION */
500 count_leading_zeros (bm
, d0
);
504 /* Normalize, i.e. make the most significant bit of the
508 n1
= (n1
<< bm
) | (n0
>> (SI_TYPE_SIZE
- bm
));
512 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
515 /* Remainder in n0 >> bm. */
522 d0
= 1 / d0
; /* Divide intentionally by zero. */
524 count_leading_zeros (bm
, d0
);
528 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
529 conclude (the most significant bit of n1 is set) /\ (the
530 leading quotient digit q1 = 1).
532 This special case is necessary, not an optimization.
533 (Shifts counts of SI_TYPE_SIZE are undefined.) */
542 b
= SI_TYPE_SIZE
- bm
;
546 n1
= (n1
<< bm
) | (n0
>> b
);
549 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
554 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
556 /* Remainder in n0 >> bm. */
566 #endif /* UDIV_NEEDS_NORMALIZATION */
577 /* Remainder in n1n0. */
589 count_leading_zeros (bm
, d1
);
592 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
593 conclude (the most significant bit of n1 is set) /\ (the
594 quotient digit q0 = 0 or 1).
596 This special case is necessary, not an optimization. */
598 /* The condition on the next line takes advantage of that
599 n1 >= d1 (true due to program flow). */
600 if (n1
> d1
|| n0
>= d0
)
603 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
622 b
= SI_TYPE_SIZE
- bm
;
624 d1
= (d1
<< bm
) | (d0
>> b
);
627 n1
= (n1
<< bm
) | (n0
>> b
);
630 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
631 umul_ppmm (m1
, m0
, q0
, d0
);
633 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
636 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
641 /* Remainder in (n1n0 - m1m0) >> bm. */
644 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
645 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
646 rr
.s
.high
= n1
>> bm
;
660 UDItype
__udivmoddi4 ();
663 __divdi3 (DItype u
, DItype v
)
674 uu
.ll
= __negdi2 (uu
.ll
);
677 vv
.ll
= __negdi2 (vv
.ll
);
679 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDItype
*) 0);
688 UDItype
__udivmoddi4 ();
690 __moddi3 (DItype u
, DItype v
)
701 uu
.ll
= __negdi2 (uu
.ll
);
703 vv
.ll
= __negdi2 (vv
.ll
);
705 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
714 UDItype
__udivmoddi4 ();
716 __umoddi3 (UDItype u
, UDItype v
)
720 (void) __udivmoddi4 (u
, v
, &w
);
727 UDItype
__udivmoddi4 ();
729 __udivdi3 (UDItype n
, UDItype d
)
731 return __udivmoddi4 (n
, d
, (UDItype
*) 0);
737 __cmpdi2 (DItype a
, DItype b
)
741 au
.ll
= a
, bu
.ll
= b
;
743 if (au
.s
.high
< bu
.s
.high
)
745 else if (au
.s
.high
> bu
.s
.high
)
747 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
749 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
757 __ucmpdi2 (DItype a
, DItype b
)
761 au
.ll
= a
, bu
.ll
= b
;
763 if ((USItype
) au
.s
.high
< (USItype
) bu
.s
.high
)
765 else if ((USItype
) au
.s
.high
> (USItype
) bu
.s
.high
)
767 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
769 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
775 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
776 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
777 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
780 __fixunstfdi (TFtype a
)
788 /* Compute high word of result, as a flonum. */
789 b
= (a
/ HIGH_WORD_COEFF
);
790 /* Convert that to fixed (but not to DItype!),
791 and shift it into the high word. */
794 /* Remove high part from the TFtype, leaving the low part as flonum. */
796 /* Convert that to fixed (but not to DItype!) and add it in.
797 Sometimes A comes out negative. This is significant, since
798 A has more bits than a long int does. */
800 v
-= (USItype
) (- a
);
807 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
812 return - __fixunstfdi (-a
);
813 return __fixunstfdi (a
);
817 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
818 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
819 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
822 __fixunsxfdi (XFtype a
)
830 /* Compute high word of result, as a flonum. */
831 b
= (a
/ HIGH_WORD_COEFF
);
832 /* Convert that to fixed (but not to DItype!),
833 and shift it into the high word. */
836 /* Remove high part from the XFtype, leaving the low part as flonum. */
838 /* Convert that to fixed (but not to DItype!) and add it in.
839 Sometimes A comes out negative. This is significant, since
840 A has more bits than a long int does. */
842 v
-= (USItype
) (- a
);
849 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
854 return - __fixunsxfdi (-a
);
855 return __fixunsxfdi (a
);
860 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
861 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
864 __fixunsdfdi (DFtype a
)
872 /* Compute high word of result, as a flonum. */
873 b
= (a
/ HIGH_WORD_COEFF
);
874 /* Convert that to fixed (but not to DItype!),
875 and shift it into the high word. */
878 /* Remove high part from the DFtype, leaving the low part as flonum. */
880 /* Convert that to fixed (but not to DItype!) and add it in.
881 Sometimes A comes out negative. This is significant, since
882 A has more bits than a long int does. */
884 v
-= (USItype
) (- a
);
896 return - __fixunsdfdi (-a
);
897 return __fixunsdfdi (a
);
902 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
903 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
906 __fixunssfdi (SFtype original_a
)
908 /* Convert the SFtype to a DFtype, because that is surely not going
909 to lose any bits. Some day someone else can write a faster version
910 that avoids converting to DFtype, and verify it really works right. */
911 DFtype a
= original_a
;
918 /* Compute high word of result, as a flonum. */
919 b
= (a
/ HIGH_WORD_COEFF
);
920 /* Convert that to fixed (but not to DItype!),
921 and shift it into the high word. */
924 /* Remove high part from the DFtype, leaving the low part as flonum. */
926 /* Convert that to fixed (but not to DItype!) and add it in.
927 Sometimes A comes out negative. This is significant, since
928 A has more bits than a long int does. */
930 v
-= (USItype
) (- a
);
942 return - __fixunssfdi (-a
);
943 return __fixunssfdi (a
);
947 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
948 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
949 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
950 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
953 __floatdixf (DItype u
)
957 d
= (SItype
) (u
>> WORD_SIZE
);
958 d
*= HIGH_HALFWORD_COEFF
;
959 d
*= HIGH_HALFWORD_COEFF
;
960 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
966 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
967 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
968 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
969 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
972 __floatditf (DItype u
)
976 d
= (SItype
) (u
>> WORD_SIZE
);
977 d
*= HIGH_HALFWORD_COEFF
;
978 d
*= HIGH_HALFWORD_COEFF
;
979 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
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)
991 __floatdidf (DItype u
)
995 d
= (SItype
) (u
>> WORD_SIZE
);
996 d
*= HIGH_HALFWORD_COEFF
;
997 d
*= HIGH_HALFWORD_COEFF
;
998 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1005 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1006 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1007 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1008 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1010 /* Define codes for all the float formats that we know of. Note
1011 that this is copied from real.h. */
1013 #define UNKNOWN_FLOAT_FORMAT 0
1014 #define IEEE_FLOAT_FORMAT 1
1015 #define VAX_FLOAT_FORMAT 2
1016 #define IBM_FLOAT_FORMAT 3
1018 /* Default to IEEE float if not specified. Nearly all machines use it. */
1019 #ifndef HOST_FLOAT_FORMAT
1020 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1023 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1028 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1033 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1039 __floatdisf (DItype u
)
1041 /* Do the calculation in DFmode
1042 so that we don't lose any of the precision of the high word
1043 while multiplying it. */
1046 /* Protect against double-rounding error.
1047 Represent any low-order bits, that might be truncated in DFmode,
1048 by a bit that won't be lost. The bit can go in anywhere below the
1049 rounding position of the SFmode. A fixed mask and bit position
1050 handles all usual configurations. It doesn't handle the case
1051 of 128-bit DImode, however. */
1052 if (DF_SIZE
< DI_SIZE
1053 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1055 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1056 if (! (- ((DItype
) 1 << DF_SIZE
) < u
1057 && u
< ((DItype
) 1 << DF_SIZE
)))
1059 if ((USItype
) u
& (REP_BIT
- 1))
1063 f
= (SItype
) (u
>> WORD_SIZE
);
1064 f
*= HIGH_HALFWORD_COEFF
;
1065 f
*= HIGH_HALFWORD_COEFF
;
1066 f
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1072 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1073 /* Reenable the normal types, in case limits.h needs them. */
1086 __fixunsxfsi (XFtype a
)
1088 if (a
>= - (DFtype
) LONG_MIN
)
1089 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1095 /* Reenable the normal types, in case limits.h needs them. */
1108 __fixunsdfsi (DFtype a
)
1110 if (a
>= - (DFtype
) LONG_MIN
)
1111 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1117 /* Reenable the normal types, in case limits.h needs them. */
1130 __fixunssfsi (SFtype a
)
1132 if (a
>= - (SFtype
) LONG_MIN
)
1133 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1138 /* From here on down, the routines use normal data types. */
1140 #define SItype bogus_type
1141 #define USItype bogus_type
1142 #define DItype bogus_type
1143 #define UDItype bogus_type
1144 #define SFtype bogus_type
1145 #define DFtype bogus_type
1157 /* Like bcmp except the sign is meaningful.
1158 Result is negative if S1 is less than S2,
1159 positive if S1 is greater, 0 if S1 and S2 are equal. */
1162 __gcc_bcmp (unsigned char *s1
, unsigned char *s2
, size_t size
)
1166 unsigned char c1
= *s1
++, c2
= *s2
++;
1183 #if defined(__svr4__) || defined(__alliant__)
1187 /* The Alliant needs the added underscore. */
1188 asm (".globl __builtin_saveregs");
1189 asm ("__builtin_saveregs:");
1190 asm (".globl ___builtin_saveregs");
1191 asm ("___builtin_saveregs:");
1193 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1194 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1195 area and also for a new va_list
1197 /* Save all argument registers in the arg reg save area. The
1198 arg reg save area must have the following layout (according
1210 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1211 asm (" fst.q %f12,16(%sp)");
1213 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1214 asm (" st.l %r17,36(%sp)");
1215 asm (" st.l %r18,40(%sp)");
1216 asm (" st.l %r19,44(%sp)");
1217 asm (" st.l %r20,48(%sp)");
1218 asm (" st.l %r21,52(%sp)");
1219 asm (" st.l %r22,56(%sp)");
1220 asm (" st.l %r23,60(%sp)");
1221 asm (" st.l %r24,64(%sp)");
1222 asm (" st.l %r25,68(%sp)");
1223 asm (" st.l %r26,72(%sp)");
1224 asm (" st.l %r27,76(%sp)");
1226 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1227 va_list structure. Put in into
1228 r16 so that it will be returned
1231 /* Initialize all fields of the new va_list structure. This
1232 structure looks like:
1235 unsigned long ireg_used;
1236 unsigned long freg_used;
1242 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1243 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1244 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1245 asm (" bri %r1"); /* delayed return */
1246 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1248 #else /* not __svr4__ */
1249 #if defined(__PARAGON__)
1251 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1252 * and we stand a better chance of hooking into libraries
1253 * compiled by PGI. [andyp@ssd.intel.com]
1257 asm (".globl __builtin_saveregs");
1258 asm ("__builtin_saveregs:");
1259 asm (".globl ___builtin_saveregs");
1260 asm ("___builtin_saveregs:");
1262 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1263 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1264 area and also for a new va_list
1266 /* Save all argument registers in the arg reg save area. The
1267 arg reg save area must have the following layout (according
1279 asm (" fst.q f8, 0(sp)");
1280 asm (" fst.q f12,16(sp)");
1281 asm (" st.l r16,32(sp)");
1282 asm (" st.l r17,36(sp)");
1283 asm (" st.l r18,40(sp)");
1284 asm (" st.l r19,44(sp)");
1285 asm (" st.l r20,48(sp)");
1286 asm (" st.l r21,52(sp)");
1287 asm (" st.l r22,56(sp)");
1288 asm (" st.l r23,60(sp)");
1289 asm (" st.l r24,64(sp)");
1290 asm (" st.l r25,68(sp)");
1291 asm (" st.l r26,72(sp)");
1292 asm (" st.l r27,76(sp)");
1294 asm (" adds 80,sp,r16"); /* compute the address of the new
1295 va_list structure. Put in into
1296 r16 so that it will be returned
1299 /* Initialize all fields of the new va_list structure. This
1300 structure looks like:
1303 unsigned long ireg_used;
1304 unsigned long freg_used;
1310 asm (" st.l r0, 0(r16)"); /* nfixed */
1311 asm (" st.l r0, 4(r16)"); /* nfloating */
1312 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1313 asm (" bri r1"); /* delayed return */
1314 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1315 #else /* not __PARAGON__ */
1319 asm (".globl ___builtin_saveregs");
1320 asm ("___builtin_saveregs:");
1321 asm (" mov sp,r30");
1322 asm (" andnot 0x0f,sp,sp");
1323 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1325 /* Fill in the __va_struct. */
1326 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1327 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1328 asm (" st.l r18, 8(sp)");
1329 asm (" st.l r19,12(sp)");
1330 asm (" st.l r20,16(sp)");
1331 asm (" st.l r21,20(sp)");
1332 asm (" st.l r22,24(sp)");
1333 asm (" st.l r23,28(sp)");
1334 asm (" st.l r24,32(sp)");
1335 asm (" st.l r25,36(sp)");
1336 asm (" st.l r26,40(sp)");
1337 asm (" st.l r27,44(sp)");
1339 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1340 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1342 /* Fill in the __va_ctl. */
1343 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1344 asm (" st.l r28,84(sp)"); /* pointer to more args */
1345 asm (" st.l r0, 88(sp)"); /* nfixed */
1346 asm (" st.l r0, 92(sp)"); /* nfloating */
1348 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1350 asm (" mov r30,sp");
1351 /* recover stack and pass address to start
1353 #endif /* not __PARAGON__ */
1354 #endif /* not __svr4__ */
1355 #else /* not __i860__ */
1357 asm (".global __builtin_saveregs");
1358 asm ("__builtin_saveregs:");
1359 asm (".global ___builtin_saveregs");
1360 asm ("___builtin_saveregs:");
1361 #ifdef NEED_PROC_COMMAND
1364 asm ("st %i0,[%fp+68]");
1365 asm ("st %i1,[%fp+72]");
1366 asm ("st %i2,[%fp+76]");
1367 asm ("st %i3,[%fp+80]");
1368 asm ("st %i4,[%fp+84]");
1370 asm ("st %i5,[%fp+88]");
1371 #ifdef NEED_TYPE_COMMAND
1372 asm (".type __builtin_saveregs,#function");
1373 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1375 #else /* not __sparc__ */
1376 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1380 asm (" .set nomips16");
1382 asm (" .ent __builtin_saveregs");
1383 asm (" .globl __builtin_saveregs");
1384 asm ("__builtin_saveregs:");
1385 asm (" sw $4,0($30)");
1386 asm (" sw $5,4($30)");
1387 asm (" sw $6,8($30)");
1388 asm (" sw $7,12($30)");
1390 asm (" .end __builtin_saveregs");
1391 #else /* not __mips__, etc. */
1394 __builtin_saveregs ()
1399 #endif /* not __mips__ */
1400 #endif /* not __sparc__ */
1401 #endif /* not __i860__ */
1405 #ifndef inhibit_libc
1407 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1409 /* This is used by the `assert' macro. */
1410 extern void __eprintf (const char *, const char *, unsigned int, const char *)
1411 __attribute__ ((__noreturn__
));
1414 __eprintf (const char *string
, const char *expression
,
1415 unsigned int line
, const char *filename
)
1417 fprintf (stderr
, string
, expression
, line
, filename
);
1427 /* Structure emitted by -a */
1431 const char *filename
;
1435 const unsigned long *addresses
;
1437 /* Older GCC's did not emit these fields. */
1439 const char **functions
;
1440 const long *line_nums
;
1441 const char **filenames
;
1445 #ifdef BLOCK_PROFILER_CODE
1448 #ifndef inhibit_libc
1450 /* Simple minded basic block profiling output dumper for
1451 systems that don't provide tcov support. At present,
1452 it requires atexit and stdio. */
1454 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1458 #include "gbl-ctors.h"
1459 #include "gcov-io.h"
1462 static struct bb
*bb_head
;
1464 /* Return the number of digits needed to print a value */
1465 /* __inline__ */ static int num_digits (long value
, int base
)
1467 int minus
= (value
< 0 && base
!= 16);
1468 unsigned long v
= (minus
) ? -value
: value
;
1482 __bb_exit_func (void)
1484 FILE *da_file
, *file
;
1491 i
= strlen (bb_head
->filename
) - 3;
1493 if (!strcmp (bb_head
->filename
+i
, ".da"))
1495 /* Must be -fprofile-arcs not -a.
1496 Dump data in a form that gcov expects. */
1500 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1502 /* If the file exists, and the number of counts in it is the same,
1503 then merge them in. */
1505 if ((da_file
= fopen (ptr
->filename
, "r")) != 0)
1509 if (__read_long (&n_counts
, da_file
, 8) != 0)
1511 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1516 if (n_counts
== ptr
->ncounts
)
1520 for (i
= 0; i
< n_counts
; i
++)
1524 if (__read_long (&v
, da_file
, 8) != 0)
1526 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1530 ptr
->counts
[i
] += v
;
1534 if (fclose (da_file
) == EOF
)
1535 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1538 if ((da_file
= fopen (ptr
->filename
, "w")) == 0)
1540 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1545 /* ??? Should first write a header to the file. Preferably, a 4 byte
1546 magic number, 4 bytes containing the time the program was
1547 compiled, 4 bytes containing the last modification time of the
1548 source file, and 4 bytes indicating the compiler options used.
1550 That way we can easily verify that the proper source/executable/
1551 data file combination is being used from gcov. */
1553 if (__write_long (ptr
->ncounts
, da_file
, 8) != 0)
1556 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1562 long *count_ptr
= ptr
->counts
;
1564 for (j
= ptr
->ncounts
; j
> 0; j
--)
1566 if (__write_long (*count_ptr
, da_file
, 8) != 0)
1574 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1578 if (fclose (da_file
) == EOF
)
1579 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1586 /* Must be basic block profiling. Emit a human readable output file. */
1588 file
= fopen ("bb.out", "a");
1597 /* This is somewhat type incorrect, but it avoids worrying about
1598 exactly where time.h is included from. It should be ok unless
1599 a void * differs from other pointer formats, or if sizeof (long)
1600 is < sizeof (time_t). It would be nice if we could assume the
1601 use of rationale standards here. */
1603 time ((void *) &time_value
);
1604 fprintf (file
, "Basic block profiling finished on %s\n", ctime ((void *) &time_value
));
1606 /* We check the length field explicitly in order to allow compatibility
1607 with older GCC's which did not provide it. */
1609 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1612 int func_p
= (ptr
->nwords
>= sizeof (struct bb
)
1613 && ptr
->nwords
<= 1000
1615 int line_p
= (func_p
&& ptr
->line_nums
);
1616 int file_p
= (func_p
&& ptr
->filenames
);
1617 int addr_p
= (ptr
->addresses
!= 0);
1618 long ncounts
= ptr
->ncounts
;
1624 int blk_len
= num_digits (ncounts
, 10);
1629 fprintf (file
, "File %s, %ld basic blocks \n\n",
1630 ptr
->filename
, ncounts
);
1632 /* Get max values for each field. */
1633 for (i
= 0; i
< ncounts
; i
++)
1638 if (cnt_max
< ptr
->counts
[i
])
1639 cnt_max
= ptr
->counts
[i
];
1641 if (addr_p
&& addr_max
< ptr
->addresses
[i
])
1642 addr_max
= ptr
->addresses
[i
];
1644 if (line_p
&& line_max
< ptr
->line_nums
[i
])
1645 line_max
= ptr
->line_nums
[i
];
1649 p
= (ptr
->functions
[i
]) ? (ptr
->functions
[i
]) : "<none>";
1657 p
= (ptr
->filenames
[i
]) ? (ptr
->filenames
[i
]) : "<none>";
1664 addr_len
= num_digits (addr_max
, 16);
1665 cnt_len
= num_digits (cnt_max
, 10);
1666 line_len
= num_digits (line_max
, 10);
1668 /* Now print out the basic block information. */
1669 for (i
= 0; i
< ncounts
; i
++)
1672 " Block #%*d: executed %*ld time(s)",
1674 cnt_len
, ptr
->counts
[i
]);
1677 fprintf (file
, " address= 0x%.*lx", addr_len
,
1681 fprintf (file
, " function= %-*s", func_len
,
1682 (ptr
->functions
[i
]) ? ptr
->functions
[i
] : "<none>");
1685 fprintf (file
, " line= %*ld", line_len
, ptr
->line_nums
[i
]);
1688 fprintf (file
, " file= %s",
1689 (ptr
->filenames
[i
]) ? ptr
->filenames
[i
] : "<none>");
1691 fprintf (file
, "\n");
1694 fprintf (file
, "\n");
1698 fprintf (file
, "\n\n");
1704 __bb_init_func (struct bb
*blocks
)
1706 /* User is supposed to check whether the first word is non-0,
1707 but just in case.... */
1709 if (blocks
->zero_word
)
1713 /* Initialize destructor. */
1715 ON_EXIT (__bb_exit_func
, 0);
1718 /* Set up linked list. */
1719 blocks
->zero_word
= 1;
1720 blocks
->next
= bb_head
;
1724 #ifndef MACHINE_STATE_SAVE
1725 #define MACHINE_STATE_SAVE(ID)
1727 #ifndef MACHINE_STATE_RESTORE
1728 #define MACHINE_STATE_RESTORE(ID)
1731 /* Number of buckets in hashtable of basic block addresses. */
1733 #define BB_BUCKETS 311
1735 /* Maximum length of string in file bb.in. */
1737 #define BBINBUFSIZE 500
1739 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1740 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1742 #define BBINBUFSIZESTR "499"
1746 struct bb_edge
*next
;
1747 unsigned long src_addr
;
1748 unsigned long dst_addr
;
1749 unsigned long count
;
1754 TRACE_KEEP
= 0, TRACE_ON
= 1, TRACE_OFF
= 2
1759 struct bb_func
*next
;
1762 enum bb_func_mode mode
;
1765 /* This is the connection to the outside world.
1766 The BLOCK_PROFILER macro must set __bb.blocks
1767 and __bb.blockno. */
1770 unsigned long blockno
;
1774 /* Vars to store addrs of source and destination basic blocks
1777 static unsigned long bb_src
= 0;
1778 static unsigned long bb_dst
= 0;
1780 static FILE *bb_tracefile
= (FILE *) 0;
1781 static struct bb_edge
**bb_hashbuckets
= (struct bb_edge
**) 0;
1782 static struct bb_func
*bb_func_head
= (struct bb_func
*) 0;
1783 static unsigned long bb_callcount
= 0;
1784 static int bb_mode
= 0;
1786 static unsigned long *bb_stack
= (unsigned long *) 0;
1787 static size_t bb_stacksize
= 0;
1789 static int reported
= 0;
1792 Always : Print execution frequencies of basic blocks
1794 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1795 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1796 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1797 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1802 /*#include <sys/types.h>*/
1803 #include <sys/stat.h>
1804 /*#include <malloc.h>*/
1806 /* Commands executed by gopen. */
1808 #define GOPENDECOMPRESS "gzip -cd "
1809 #define GOPENCOMPRESS "gzip -c >"
1811 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1812 If it does not compile, simply replace gopen by fopen and delete
1813 '.gz' from any first parameter to gopen. */
1816 gopen (char *fn
, char *mode
)
1824 if (mode
[0] != 'r' && mode
[0] != 'w')
1827 p
= fn
+ strlen (fn
)-1;
1828 use_gzip
= ((p
[-1] == '.' && (p
[0] == 'Z' || p
[0] == 'z'))
1829 || (p
[-2] == '.' && p
[-1] == 'g' && p
[0] == 'z'));
1836 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1837 + sizeof (GOPENDECOMPRESS
));
1838 strcpy (s
, GOPENDECOMPRESS
);
1839 strcpy (s
+ (sizeof (GOPENDECOMPRESS
)-1), fn
);
1840 f
= popen (s
, mode
);
1848 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1849 + sizeof (GOPENCOMPRESS
));
1850 strcpy (s
, GOPENCOMPRESS
);
1851 strcpy (s
+ (sizeof (GOPENCOMPRESS
)-1), fn
);
1852 if (!(f
= popen (s
, mode
)))
1853 f
= fopen (s
, mode
);
1860 return fopen (fn
, mode
);
1870 if (!fstat (fileno (f
), &buf
) && S_ISFIFO (buf
.st_mode
))
1878 #endif /* HAVE_POPEN */
1880 /* Called once per program. */
1883 __bb_exit_trace_func ()
1885 FILE *file
= fopen ("bb.out", "a");
1898 gclose (bb_tracefile
);
1900 fclose (bb_tracefile
);
1901 #endif /* HAVE_POPEN */
1904 /* Check functions in `bb.in'. */
1909 const struct bb_func
*p
;
1910 int printed_something
= 0;
1914 /* This is somewhat type incorrect. */
1915 time ((void *) &time_value
);
1917 for (p
= bb_func_head
; p
!= (struct bb_func
*) 0; p
= p
->next
)
1919 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1921 if (!ptr
->filename
|| (p
->filename
!= (char *) 0 && strcmp (p
->filename
, ptr
->filename
)))
1923 for (blk
= 0; blk
< ptr
->ncounts
; blk
++)
1925 if (!strcmp (p
->funcname
, ptr
->functions
[blk
]))
1930 if (!printed_something
)
1932 fprintf (file
, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value
));
1933 printed_something
= 1;
1936 fprintf (file
, "\tFunction %s", p
->funcname
);
1938 fprintf (file
, " of file %s", p
->filename
);
1939 fprintf (file
, "\n" );
1944 if (printed_something
)
1945 fprintf (file
, "\n");
1951 if (!bb_hashbuckets
)
1955 fprintf (stderr
, "Profiler: out of memory\n");
1965 unsigned long addr_max
= 0;
1966 unsigned long cnt_max
= 0;
1970 /* This is somewhat type incorrect, but it avoids worrying about
1971 exactly where time.h is included from. It should be ok unless
1972 a void * differs from other pointer formats, or if sizeof (long)
1973 is < sizeof (time_t). It would be nice if we could assume the
1974 use of rationale standards here. */
1976 time ((void *) &time_value
);
1977 fprintf (file
, "Basic block jump tracing");
1979 switch (bb_mode
& 12)
1982 fprintf (file
, " (with call)");
1986 /* Print nothing. */
1990 fprintf (file
, " (with call & ret)");
1994 fprintf (file
, " (with ret)");
1998 fprintf (file
, " finished on %s\n", ctime ((void *) &time_value
));
2000 for (i
= 0; i
< BB_BUCKETS
; i
++)
2002 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
2003 for ( ; bucket
; bucket
= bucket
->next
)
2005 if (addr_max
< bucket
->src_addr
)
2006 addr_max
= bucket
->src_addr
;
2007 if (addr_max
< bucket
->dst_addr
)
2008 addr_max
= bucket
->dst_addr
;
2009 if (cnt_max
< bucket
->count
)
2010 cnt_max
= bucket
->count
;
2013 addr_len
= num_digits (addr_max
, 16);
2014 cnt_len
= num_digits (cnt_max
, 10);
2016 for ( i
= 0; i
< BB_BUCKETS
; i
++)
2018 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
2019 for ( ; bucket
; bucket
= bucket
->next
)
2021 fprintf (file
, "Jump from block 0x%.*lx to "
2022 "block 0x%.*lx executed %*lu time(s)\n",
2023 addr_len
, bucket
->src_addr
,
2024 addr_len
, bucket
->dst_addr
,
2025 cnt_len
, bucket
->count
);
2029 fprintf (file
, "\n");
2037 /* Free allocated memory. */
2042 struct bb_func
*old
= f
;
2045 if (old
->funcname
) free (old
->funcname
);
2046 if (old
->filename
) free (old
->filename
);
2057 for (i
= 0; i
< BB_BUCKETS
; i
++)
2059 struct bb_edge
*old
, *bucket
= bb_hashbuckets
[i
];
2064 bucket
= bucket
->next
;
2068 free (bb_hashbuckets
);
2071 for (b
= bb_head
; b
; b
= b
->next
)
2072 if (b
->flags
) free (b
->flags
);
2075 /* Called once per program. */
2082 char buf
[BBINBUFSIZE
];
2085 enum bb_func_mode m
;
2088 /* Initialize destructor. */
2089 ON_EXIT (__bb_exit_func
, 0);
2092 if (!(file
= fopen ("bb.in", "r")))
2095 while(fscanf (file
, " %" BBINBUFSIZESTR
"s ", buf
) != EOF
)
2107 if (!strcmp (p
, "__bb_trace__"))
2109 else if (!strcmp (p
, "__bb_jumps__"))
2111 else if (!strcmp (p
, "__bb_hidecall__"))
2113 else if (!strcmp (p
, "__bb_showret__"))
2117 struct bb_func
*f
= (struct bb_func
*) malloc (sizeof (struct bb_func
));
2121 f
->next
= bb_func_head
;
2122 if ((pos
= strchr (p
, ':')))
2124 if (!(f
->funcname
= (char *) malloc (strlen (pos
+1)+1)))
2126 strcpy (f
->funcname
, pos
+1);
2128 if ((f
->filename
= (char *) malloc (l
+1)))
2130 strncpy (f
->filename
, p
, l
);
2131 f
->filename
[l
] = '\0';
2134 f
->filename
= (char *) 0;
2138 if (!(f
->funcname
= (char *) malloc (strlen (p
)+1)))
2140 strcpy (f
->funcname
, p
);
2141 f
->filename
= (char *) 0;
2153 bb_tracefile
= gopen ("bbtrace.gz", "w");
2158 bb_tracefile
= fopen ("bbtrace", "w");
2160 #endif /* HAVE_POPEN */
2164 bb_hashbuckets
= (struct bb_edge
**)
2165 malloc (BB_BUCKETS
* sizeof (struct bb_edge
*));
2167 memset (bb_hashbuckets
, 0, BB_BUCKETS
* sizeof (struct bb_edge
*));
2173 bb_stack
= (unsigned long *) malloc (bb_stacksize
* sizeof (*bb_stack
));
2177 /* Initialize destructor. */
2178 ON_EXIT (__bb_exit_trace_func
, 0);
2183 /* Called upon entering a basic block. */
2188 struct bb_edge
*bucket
;
2190 MACHINE_STATE_SAVE("1")
2192 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2195 bb_dst
= __bb
.blocks
->addresses
[__bb
.blockno
];
2196 __bb
.blocks
->counts
[__bb
.blockno
]++;
2200 fwrite (&bb_dst
, sizeof (unsigned long), 1, bb_tracefile
);
2205 struct bb_edge
**startbucket
, **oldnext
;
2207 oldnext
= startbucket
2208 = & bb_hashbuckets
[ (((int) bb_src
*8) ^ (int) bb_dst
) % BB_BUCKETS
];
2209 bucket
= *startbucket
;
2211 for (bucket
= *startbucket
; bucket
;
2212 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2214 if (bucket
->src_addr
== bb_src
2215 && bucket
->dst_addr
== bb_dst
)
2218 *oldnext
= bucket
->next
;
2219 bucket
->next
= *startbucket
;
2220 *startbucket
= bucket
;
2225 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2231 fprintf (stderr
, "Profiler: out of memory\n");
2238 bucket
->src_addr
= bb_src
;
2239 bucket
->dst_addr
= bb_dst
;
2240 bucket
->next
= *startbucket
;
2241 *startbucket
= bucket
;
2252 MACHINE_STATE_RESTORE("1")
2256 /* Called when returning from a function and `__bb_showret__' is set. */
2259 __bb_trace_func_ret ()
2261 struct bb_edge
*bucket
;
2263 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2268 struct bb_edge
**startbucket
, **oldnext
;
2270 oldnext
= startbucket
2271 = & bb_hashbuckets
[ (((int) bb_dst
* 8) ^ (int) bb_src
) % BB_BUCKETS
];
2272 bucket
= *startbucket
;
2274 for (bucket
= *startbucket
; bucket
;
2275 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2277 if (bucket
->src_addr
== bb_dst
2278 && bucket
->dst_addr
== bb_src
)
2281 *oldnext
= bucket
->next
;
2282 bucket
->next
= *startbucket
;
2283 *startbucket
= bucket
;
2288 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2294 fprintf (stderr
, "Profiler: out of memory\n");
2301 bucket
->src_addr
= bb_dst
;
2302 bucket
->dst_addr
= bb_src
;
2303 bucket
->next
= *startbucket
;
2304 *startbucket
= bucket
;
2317 /* Called upon entering the first function of a file. */
2320 __bb_init_file (struct bb
*blocks
)
2323 const struct bb_func
*p
;
2324 long blk
, ncounts
= blocks
->ncounts
;
2325 const char **functions
= blocks
->functions
;
2327 /* Set up linked list. */
2328 blocks
->zero_word
= 1;
2329 blocks
->next
= bb_head
;
2334 || !(blocks
->flags
= (char *) malloc (sizeof (char) * blocks
->ncounts
)))
2337 for (blk
= 0; blk
< ncounts
; blk
++)
2338 blocks
->flags
[blk
] = 0;
2340 for (blk
= 0; blk
< ncounts
; blk
++)
2342 for (p
= bb_func_head
; p
; p
= p
->next
)
2344 if (!strcmp (p
->funcname
, functions
[blk
])
2345 && (!p
->filename
|| !strcmp (p
->filename
, blocks
->filename
)))
2347 blocks
->flags
[blk
] |= p
->mode
;
2354 /* Called when exiting from a function. */
2360 MACHINE_STATE_SAVE("2")
2364 if ((bb_mode
& 12) && bb_stacksize
> bb_callcount
)
2366 bb_src
= bb_stack
[bb_callcount
];
2368 __bb_trace_func_ret ();
2374 MACHINE_STATE_RESTORE("2")
2378 /* Called when entering a function. */
2381 __bb_init_trace_func (struct bb
*blocks
, unsigned long blockno
)
2383 static int trace_init
= 0;
2385 MACHINE_STATE_SAVE("3")
2387 if (!blocks
->zero_word
)
2394 __bb_init_file (blocks
);
2404 if (bb_callcount
>= bb_stacksize
)
2406 size_t newsize
= bb_callcount
+ 100;
2408 bb_stack
= (unsigned long *) realloc (bb_stack
, newsize
);
2413 fprintf (stderr
, "Profiler: out of memory\n");
2417 goto stack_overflow
;
2419 bb_stacksize
= newsize
;
2421 bb_stack
[bb_callcount
] = bb_src
;
2432 else if (blocks
->flags
&& (blocks
->flags
[blockno
] & TRACE_ON
))
2438 bb_stack
[bb_callcount
] = bb_src
;
2441 MACHINE_STATE_RESTORE("3")
2444 #endif /* not inhibit_libc */
2445 #endif /* not BLOCK_PROFILER_CODE */
2449 unsigned int __shtab
[] = {
2450 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2451 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2452 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2453 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2454 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2455 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2456 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2457 0x10000000, 0x20000000, 0x40000000, 0x80000000
2461 #ifdef L_clear_cache
2462 /* Clear part of an instruction cache. */
2464 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2467 __clear_cache (char *beg
, char *end
)
2469 #ifdef CLEAR_INSN_CACHE
2470 CLEAR_INSN_CACHE (beg
, end
);
2472 #ifdef INSN_CACHE_SIZE
2473 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
2474 static int initialized
;
2478 typedef (*function_ptr
) ();
2480 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2481 /* It's cheaper to clear the whole cache.
2482 Put in a series of jump instructions so that calling the beginning
2483 of the cache will clear the whole thing. */
2487 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2488 & -INSN_CACHE_LINE_WIDTH
);
2489 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
2491 while (ptr
< end_ptr
)
2493 *(INSTRUCTION_TYPE
*)ptr
2494 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
2495 ptr
+= INSN_CACHE_LINE_WIDTH
;
2497 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
2502 /* Call the beginning of the sequence. */
2503 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2504 & -INSN_CACHE_LINE_WIDTH
))
2507 #else /* Cache is large. */
2511 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2512 & -INSN_CACHE_LINE_WIDTH
);
2514 while (ptr
< (int) array
+ sizeof array
)
2516 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
2517 ptr
+= INSN_CACHE_LINE_WIDTH
;
2523 /* Find the location in array that occupies the same cache line as BEG. */
2525 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
2526 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
2527 & -INSN_CACHE_PLANE_SIZE
)
2530 /* Compute the cache alignment of the place to stop clearing. */
2531 #if 0 /* This is not needed for gcc's purposes. */
2532 /* If the block to clear is bigger than a cache plane,
2533 we clear the entire cache, and OFFSET is already correct. */
2534 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
2536 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
2537 & -INSN_CACHE_LINE_WIDTH
)
2538 & (INSN_CACHE_PLANE_SIZE
- 1));
2540 #if INSN_CACHE_DEPTH > 1
2541 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
2542 if (end_addr
<= start_addr
)
2543 end_addr
+= INSN_CACHE_PLANE_SIZE
;
2545 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
2547 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2548 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2550 while (addr
!= stop
)
2552 /* Call the return instruction at ADDR. */
2553 ((function_ptr
) addr
) ();
2555 addr
+= INSN_CACHE_LINE_WIDTH
;
2558 #else /* just one plane */
2561 /* Call the return instruction at START_ADDR. */
2562 ((function_ptr
) start_addr
) ();
2564 start_addr
+= INSN_CACHE_LINE_WIDTH
;
2566 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
2567 #endif /* just one plane */
2568 #endif /* Cache is large */
2569 #endif /* Cache exists */
2570 #endif /* CLEAR_INSN_CACHE */
2573 #endif /* L_clear_cache */
2577 /* Jump to a trampoline, loading the static chain address. */
2579 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2591 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2595 mprotect (char *addr
, int len
, int prot
)
2612 if (VirtualProtect (addr
, len
, np
, &op
))
2618 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2620 #ifdef TRANSFER_FROM_TRAMPOLINE
2621 TRANSFER_FROM_TRAMPOLINE
2624 #if defined (NeXT) && defined (__MACH__)
2626 /* Make stack executable so we can call trampolines on stack.
2627 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2631 #include <mach/mach.h>
2635 __enable_execute_stack (char *addr
)
2638 char *eaddr
= addr
+ TRAMPOLINE_SIZE
;
2639 vm_address_t a
= (vm_address_t
) addr
;
2641 /* turn on execute access on stack */
2642 r
= vm_protect (task_self (), a
, TRAMPOLINE_SIZE
, FALSE
, VM_PROT_ALL
);
2643 if (r
!= KERN_SUCCESS
)
2645 mach_error("vm_protect VM_PROT_ALL", r
);
2649 /* We inline the i-cache invalidation for speed */
2651 #ifdef CLEAR_INSN_CACHE
2652 CLEAR_INSN_CACHE (addr
, eaddr
);
2654 __clear_cache ((int) addr
, (int) eaddr
);
2658 #endif /* defined (NeXT) && defined (__MACH__) */
2662 /* Make stack executable so we can call trampolines on stack.
2663 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2665 #include <sys/mman.h>
2666 #include <sys/vmparam.h>
2667 #include <machine/machparam.h>
2670 __enable_execute_stack ()
2673 static unsigned lowest
= USRSTACK
;
2674 unsigned current
= (unsigned) &fp
& -NBPG
;
2676 if (lowest
> current
)
2678 unsigned len
= lowest
- current
;
2679 mremap (current
, &len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
2683 /* Clear instruction cache in case an old trampoline is in it. */
2686 #endif /* __convex__ */
2690 /* Modified from the convex -code above. */
2692 #include <sys/param.h>
2694 #include <sys/m88kbcs.h>
2697 __enable_execute_stack ()
2700 static unsigned long lowest
= USRSTACK
;
2701 unsigned long current
= (unsigned long) &save_errno
& -NBPC
;
2703 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2704 address is seen as 'negative'. That is the case with the stack. */
2707 if (lowest
> current
)
2709 unsigned len
=lowest
-current
;
2710 memctl(current
,len
,MCT_TEXT
);
2714 memctl(current
,NBPC
,MCT_TEXT
);
2718 #endif /* __sysV88__ */
2722 #include <sys/signal.h>
2725 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2726 so define it here, because we need it in __clear_insn_cache below */
2727 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2728 hence we enable this stuff only if MCT_TEXT is #define'd. */
2743 /* Clear instruction cache so we can call trampolines on stack.
2744 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2747 __clear_insn_cache ()
2752 /* Preserve errno, because users would be surprised to have
2753 errno changing without explicitly calling any system-call. */
2756 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2757 No need to use an address derived from _start or %sp, as 0 works also. */
2758 memctl(0, 4096, MCT_TEXT
);
2763 #endif /* __sysV68__ */
2767 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2769 #include <sys/mman.h>
2770 #include <sys/types.h>
2771 #include <sys/param.h>
2772 #include <sys/vmmac.h>
2774 /* Modified from the convex -code above.
2775 mremap promises to clear the i-cache. */
2778 __enable_execute_stack ()
2781 if (mprotect (((unsigned int)&fp
/PAGSIZ
)*PAGSIZ
, PAGSIZ
,
2782 PROT_READ
|PROT_WRITE
|PROT_EXEC
))
2784 perror ("mprotect in __enable_execute_stack");
2789 #endif /* __pyr__ */
2791 #if defined (sony_news) && defined (SYSTYPE_BSD)
2794 #include <sys/types.h>
2795 #include <sys/param.h>
2796 #include <syscall.h>
2797 #include <machine/sysnews.h>
2799 /* cacheflush function for NEWS-OS 4.2.
2800 This function is called from trampoline-initialize code
2801 defined in config/mips/mips.h. */
2804 cacheflush (char *beg
, int size
, int flag
)
2806 if (syscall (SYS_sysnews
, NEWS_CACHEFLUSH
, beg
, size
, FLUSH_BCACHE
))
2808 perror ("cache_flush");
2814 #endif /* sony_news */
2815 #endif /* L_trampoline */
2820 #include "gbl-ctors.h"
2821 /* Some systems use __main in a way incompatible with its use in gcc, in these
2822 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2823 give the same symbol without quotes for an alternative entry point. You
2824 must define both, or neither. */
2826 #define NAME__MAIN "__main"
2827 #define SYMBOL__MAIN __main
2830 #ifdef INIT_SECTION_ASM_OP
2831 #undef HAS_INIT_SECTION
2832 #define HAS_INIT_SECTION
2835 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2836 /* Run all the global destructors on exit from the program. */
2839 __do_global_dtors ()
2841 #ifdef DO_GLOBAL_DTORS_BODY
2842 DO_GLOBAL_DTORS_BODY
;
2844 static func_ptr
*p
= __DTOR_LIST__
+ 1;
2854 #ifndef HAS_INIT_SECTION
2855 /* Run all the global constructors on entry to the program. */
2858 #define ON_EXIT(a, b)
2860 /* Make sure the exit routine is pulled in to define the globals as
2861 bss symbols, just in case the linker does not automatically pull
2862 bss definitions from the library. */
2864 extern int _exit_dummy_decl
;
2865 int *_exit_dummy_ref
= &_exit_dummy_decl
;
2866 #endif /* ON_EXIT */
2869 __do_global_ctors ()
2871 DO_GLOBAL_CTORS_BODY
;
2872 ON_EXIT (__do_global_dtors
, 0);
2874 #endif /* no HAS_INIT_SECTION */
2876 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2877 /* Subroutine called automatically by `main'.
2878 Compiling a global function named `main'
2879 produces an automatic call to this function at the beginning.
2881 For many systems, this routine calls __do_global_ctors.
2882 For systems which support a .init section we use the .init section
2883 to run __do_global_ctors, so we need not do anything here. */
2888 /* Support recursive calls to `main': run initializers just once. */
2889 static int initialized
;
2893 __do_global_ctors ();
2896 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2898 #endif /* L__main */
2899 #endif /* __CYGWIN__ */
2903 #include "gbl-ctors.h"
2905 /* Provide default definitions for the lists of constructors and
2906 destructors, so that we don't get linker errors. These symbols are
2907 intentionally bss symbols, so that gld and/or collect will provide
2908 the right values. */
2910 /* We declare the lists here with two elements each,
2911 so that they are valid empty lists if no other definition is loaded.
2913 If we are using the old "set" extensions to have the gnu linker
2914 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2915 must be in the bss/common section.
2917 Long term no port should use those extensions. But many still do. */
2918 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2919 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
2920 func_ptr __CTOR_LIST__
[2] = {0, 0};
2921 func_ptr __DTOR_LIST__
[2] = {0, 0};
2923 func_ptr __CTOR_LIST__
[2];
2924 func_ptr __DTOR_LIST__
[2];
2926 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2927 #endif /* L_ctors */
2931 #include "gbl-ctors.h"
2937 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2945 static func_ptr
*atexit_chain
= 0;
2946 static long atexit_chain_length
= 0;
2947 static volatile long last_atexit_chain_slot
= -1;
2949 int atexit (func_ptr func
)
2951 if (++last_atexit_chain_slot
== atexit_chain_length
)
2953 atexit_chain_length
+= 32;
2955 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
2956 * sizeof (func_ptr
));
2958 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
2959 * sizeof (func_ptr
));
2962 atexit_chain_length
= 0;
2963 last_atexit_chain_slot
= -1;
2968 atexit_chain
[last_atexit_chain_slot
] = func
;
2971 #endif /* NEED_ATEXIT */
2973 /* If we have no known way of registering our own __do_global_dtors
2974 routine so that it will be invoked at program exit time, then we
2975 have to define our own exit routine which will get this to happen. */
2977 extern void __do_global_dtors ();
2978 extern void __bb_exit_func ();
2979 extern void _cleanup ();
2980 extern void _exit () __attribute__ ((noreturn
));
2985 #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
2989 for ( ; last_atexit_chain_slot
-- >= 0; )
2991 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
2992 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
2994 free (atexit_chain
);
2997 #else /* No NEED_ATEXIT */
2998 __do_global_dtors ();
2999 #endif /* No NEED_ATEXIT */
3000 #endif /* !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF) */
3001 /* In gbl-ctors.h, ON_EXIT is defined if HAVE_ATEXIT is defined. In
3002 __bb_init_func and _bb_init_prg, __bb_exit_func is registered with
3003 ON_EXIT if ON_EXIT is defined. Thus we must not call __bb_exit_func here
3004 if HAVE_ATEXIT is defined. */
3006 #ifndef inhibit_libc
3009 #endif /* !HAVE_ATEXIT */
3018 #else /* ON_EXIT defined */
3019 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
3021 # ifndef HAVE_ATEXIT
3022 /* Provide a fake for atexit() using ON_EXIT. */
3023 int atexit (func_ptr func
)
3025 return ON_EXIT (func
, NULL
);
3027 # endif /* HAVE_ATEXIT */
3028 #endif /* ON_EXIT defined */
3036 /* Shared exception handling support routines. */
3038 extern void __default_terminate (void) __attribute__ ((__noreturn__
));
3041 __default_terminate ()
3046 void (*__terminate_func
)() = __default_terminate
;
3051 (*__terminate_func
)();
3055 __throw_type_match (void *catch_type
, void *throw_type
, void *obj
)
3058 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3059 catch_type
, throw_type
);
3061 if (strcmp ((const char *)catch_type
, (const char *)throw_type
) == 0)
3072 /* Include definitions of EH context and table layout */
3074 #include "eh-common.h"
3075 #ifndef inhibit_libc
3079 /* Allocate and return a new EH context structure. */
3081 extern void __throw ();
3086 struct eh_full_context
{
3087 struct eh_context c
;
3089 } *ehfc
= (struct eh_full_context
*) malloc (sizeof *ehfc
);
3094 memset (ehfc
, 0, sizeof *ehfc
);
3096 ehfc
->c
.dynamic_handler_chain
= (void **) ehfc
->top_elt
;
3098 /* This should optimize out entirely. This should always be true,
3099 but just in case it ever isn't, don't allow bogus code to be
3102 if ((void*)(&ehfc
->c
) != (void*)ehfc
)
3109 static __gthread_key_t eh_context_key
;
3111 /* Destructor for struct eh_context. */
3113 eh_context_free (void *ptr
)
3115 __gthread_key_dtor (eh_context_key
, ptr
);
3121 /* Pointer to function to return EH context. */
3123 static struct eh_context
*eh_context_initialize ();
3124 static struct eh_context
*eh_context_static ();
3126 static struct eh_context
*eh_context_specific ();
3129 static struct eh_context
*(*get_eh_context
) () = &eh_context_initialize
;
3131 /* Routine to get EH context.
3132 This one will simply call the function pointer. */
3137 return (void *) (*get_eh_context
) ();
3140 /* Get and set the language specific info pointer. */
3145 struct eh_context
*eh
= (*get_eh_context
) ();
3151 eh_threads_initialize ()
3153 /* Try to create the key. If it fails, revert to static method,
3154 otherwise start using thread specific EH contexts. */
3155 if (__gthread_key_create (&eh_context_key
, &eh_context_free
) == 0)
3156 get_eh_context
= &eh_context_specific
;
3158 get_eh_context
= &eh_context_static
;
3160 #endif /* no __GTHREADS */
3162 /* Initialize EH context.
3163 This will be called only once, since we change GET_EH_CONTEXT
3164 pointer to another routine. */
3166 static struct eh_context
*
3167 eh_context_initialize ()
3171 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
3172 /* Make sure that get_eh_context does not point to us anymore.
3173 Some systems have dummy thread routines in their libc that
3174 return a success (Solaris 2.6 for example). */
3175 if (__gthread_once (&once
, eh_threads_initialize
) != 0
3176 || get_eh_context
== &eh_context_initialize
)
3178 /* Use static version of EH context. */
3179 get_eh_context
= &eh_context_static
;
3182 #else /* no __GTHREADS */
3184 /* Use static version of EH context. */
3185 get_eh_context
= &eh_context_static
;
3187 #endif /* no __GTHREADS */
3189 return (*get_eh_context
) ();
3192 /* Return a static EH context. */
3194 static struct eh_context
*
3195 eh_context_static ()
3197 static struct eh_context eh
;
3198 static int initialized
;
3199 static void *top_elt
[2];
3204 memset (&eh
, 0, sizeof eh
);
3205 eh
.dynamic_handler_chain
= top_elt
;
3211 /* Return a thread specific EH context. */
3213 static struct eh_context
*
3214 eh_context_specific ()
3216 struct eh_context
*eh
;
3217 eh
= (struct eh_context
*) __gthread_getspecific (eh_context_key
);
3220 eh
= new_eh_context ();
3221 if (__gthread_setspecific (eh_context_key
, (void *) eh
) != 0)
3229 /* Support routines for setjmp/longjmp exception handling. */
3231 /* Calls to __sjthrow are generated by the compiler when an exception
3232 is raised when using the setjmp/longjmp exception handling codegen
3235 #ifdef DONT_USE_BUILTIN_SETJMP
3236 extern void longjmp (void *, int);
3239 /* Routine to get the head of the current thread's dynamic handler chain
3240 use for exception handling. */
3243 __get_dynamic_handler_chain ()
3245 struct eh_context
*eh
= (*get_eh_context
) ();
3246 return &eh
->dynamic_handler_chain
;
3249 /* This is used to throw an exception when the setjmp/longjmp codegen
3250 method is used for exception handling.
3252 We call __terminate if there are no handlers left. Otherwise we run the
3253 cleanup actions off the dynamic cleanup stack, and pop the top of the
3254 dynamic handler chain, and use longjmp to transfer back to the associated
3257 extern void __sjthrow (void) __attribute__ ((__noreturn__
));
3262 struct eh_context
*eh
= (*get_eh_context
) ();
3263 void ***dhc
= &eh
->dynamic_handler_chain
;
3265 void (*func
)(void *, int);
3269 /* The cleanup chain is one word into the buffer. Get the cleanup
3271 cleanup
= (void***)&(*dhc
)[1];
3273 /* If there are any cleanups in the chain, run them now. */
3277 void **buf
= (void**)store
;
3282 #ifdef DONT_USE_BUILTIN_SETJMP
3283 if (! setjmp (&buf
[2]))
3285 if (! __builtin_setjmp (&buf
[2]))
3291 func
= (void(*)(void*, int))cleanup
[0][1];
3292 arg
= (void*)cleanup
[0][2];
3294 /* Update this before running the cleanup. */
3295 cleanup
[0] = (void **)cleanup
[0][0];
3308 /* We must call terminate if we try and rethrow an exception, when
3309 there is no exception currently active and when there are no
3311 if (! eh
->info
|| (*dhc
)[0] == 0)
3314 /* Find the jmpbuf associated with the top element of the dynamic
3315 handler chain. The jumpbuf starts two words into the buffer. */
3316 jmpbuf
= &(*dhc
)[2];
3318 /* Then we pop the top element off the dynamic handler chain. */
3319 *dhc
= (void**)(*dhc
)[0];
3321 /* And then we jump to the handler. */
3323 #ifdef DONT_USE_BUILTIN_SETJMP
3324 longjmp (jmpbuf
, 1);
3326 __builtin_longjmp (jmpbuf
, 1);
3330 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3331 handler, then pop the handler off the dynamic handler stack, and
3332 then throw. This is used to skip the first handler, and transfer
3333 control to the next handler in the dynamic handler stack. */
3335 extern void __sjpopnthrow (void) __attribute__ ((__noreturn__
));
3340 struct eh_context
*eh
= (*get_eh_context
) ();
3341 void ***dhc
= &eh
->dynamic_handler_chain
;
3342 void (*func
)(void *, int);
3346 /* The cleanup chain is one word into the buffer. Get the cleanup
3348 cleanup
= (void***)&(*dhc
)[1];
3350 /* If there are any cleanups in the chain, run them now. */
3354 void **buf
= (void**)store
;
3359 #ifdef DONT_USE_BUILTIN_SETJMP
3360 if (! setjmp (&buf
[2]))
3362 if (! __builtin_setjmp (&buf
[2]))
3368 func
= (void(*)(void*, int))cleanup
[0][1];
3369 arg
= (void*)cleanup
[0][2];
3371 /* Update this before running the cleanup. */
3372 cleanup
[0] = (void **)cleanup
[0][0];
3385 /* Then we pop the top element off the dynamic handler chain. */
3386 *dhc
= (void**)(*dhc
)[0];
3391 /* Support code for all exception region-based exception handling. */
3394 __eh_rtime_match (void *rtime
)
3397 __eh_matcher matcher
;
3400 info
= *(__get_eh_info ());
3401 matcher
= ((__eh_info
*)info
)->match_function
;
3404 #ifndef inhibit_libc
3405 fprintf (stderr
, "Internal Compiler Bug: No runtime type matcher.");
3409 ret
= (*matcher
) (info
, rtime
, (void *)0);
3410 return (ret
!= NULL
);
3413 /* This value identifies the place from which an exception is being
3416 #ifdef EH_TABLE_LOOKUP
3422 #ifdef DWARF2_UNWIND_INFO
3425 /* Return the table version of an exception descriptor */
3428 __get_eh_table_version (exception_descriptor
*table
)
3430 return table
->lang
.version
;
3433 /* Return the originating table language of an exception descriptor */
3436 __get_eh_table_language (exception_descriptor
*table
)
3438 return table
->lang
.language
;
3441 /* This routine takes a PC and a pointer to the exception region TABLE for
3442 its translation unit, and returns the address of the exception handler
3443 associated with the closest exception table handler entry associated
3444 with that PC, or 0 if there are no table entries the PC fits in.
3446 In the advent of a tie, we have to give the last entry, as it represents
3450 old_find_exception_handler (void *pc
, old_exception_table
*table
)
3457 /* We can't do a binary search because the table isn't guaranteed
3458 to be sorted from function to function. */
3459 for (pos
= 0; table
[pos
].start_region
!= (void *) -1; ++pos
)
3461 if (table
[pos
].start_region
<= pc
&& table
[pos
].end_region
> pc
)
3463 /* This can apply. Make sure it is at least as small as
3464 the previous best. */
3465 if (best
== -1 || (table
[pos
].end_region
<= table
[best
].end_region
3466 && table
[pos
].start_region
>= table
[best
].start_region
))
3469 /* But it is sorted by starting PC within a function. */
3470 else if (best
>= 0 && table
[pos
].start_region
> pc
)
3474 return table
[best
].exception_handler
;
3480 /* find_exception_handler finds the correct handler, if there is one, to
3481 handle an exception.
3482 returns a pointer to the handler which controlled should be transferred
3483 to, or NULL if there is nothing left.
3485 PC - pc where the exception originates. If this is a rethrow,
3486 then this starts out as a pointer to the exception table
3487 entry we wish to rethrow out of.
3488 TABLE - exception table for the current module.
3489 EH_INFO - eh info pointer for this exception.
3490 RETHROW - 1 if this is a rethrow. (see incoming value of PC).
3491 CLEANUP - returned flag indicating whether this is a cleanup handler.
3494 find_exception_handler (void *pc
, exception_descriptor
*table
,
3495 __eh_info
*eh_info
, int rethrow
, int *cleanup
)
3498 void *retval
= NULL
;
3503 /* The new model assumed the table is sorted inner-most out so the
3504 first region we find which matches is the correct one */
3506 exception_table
*tab
= &(table
->table
[0]);
3508 /* Subtract 1 from the PC to avoid hitting the next region */
3511 /* pc is actually the region table entry to rethrow out of */
3512 pos
= ((exception_table
*) pc
) - tab
;
3513 pc
= ((exception_table
*) pc
)->end_region
- 1;
3515 /* The label is always on the LAST handler entry for a region,
3516 so we know the next entry is a different region, even if the
3517 addresses are the same. Make sure its not end of table tho. */
3518 if (tab
[pos
].start_region
!= (void *) -1)
3524 /* We can't do a binary search because the table is in inner-most
3525 to outermost address ranges within functions */
3526 for ( ; tab
[pos
].start_region
!= (void *) -1; pos
++)
3528 if (tab
[pos
].start_region
<= pc
&& tab
[pos
].end_region
> pc
)
3530 if (tab
[pos
].match_info
)
3532 __eh_matcher matcher
= eh_info
->match_function
;
3533 /* match info but no matcher is NOT a match */
3536 void *ret
= (*matcher
)((void *) eh_info
,
3537 tab
[pos
].match_info
, table
);
3541 retval
= tab
[pos
].exception_handler
;
3550 retval
= tab
[pos
].exception_handler
;
3557 #endif /* DWARF2_UNWIND_INFO */
3558 #endif /* EH_TABLE_LOOKUP */
3560 #ifdef DWARF2_UNWIND_INFO
3561 /* Support code for exception handling using static unwind information. */
3565 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3566 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3567 avoid a warning about casting between int and pointer of different
3570 typedef int ptr_type
__attribute__ ((mode (pointer
)));
3572 #ifdef INCOMING_REGNO
3573 /* Is the saved value for register REG in frame UDATA stored in a register
3574 window in the previous frame? */
3576 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3577 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3578 compiled functions won't work with the frame-unwind stuff here.
3579 Perhaps the entireity of in_reg_window should be conditional on having
3580 seen a DW_CFA_GNU_window_save? */
3581 #define target_flags 0
3584 in_reg_window (int reg
, frame_state
*udata
)
3586 if (udata
->saved
[reg
] == REG_SAVED_REG
)
3587 return INCOMING_REGNO (reg
) == reg
;
3588 if (udata
->saved
[reg
] != REG_SAVED_OFFSET
)
3591 #ifdef STACK_GROWS_DOWNWARD
3592 return udata
->reg_or_offset
[reg
] > 0;
3594 return udata
->reg_or_offset
[reg
] < 0;
3598 static inline int in_reg_window (int reg
, frame_state
*udata
) { return 0; }
3599 #endif /* INCOMING_REGNO */
3601 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3602 frame called by UDATA or 0. */
3605 get_reg_addr (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3607 while (udata
->saved
[reg
] == REG_SAVED_REG
)
3609 reg
= udata
->reg_or_offset
[reg
];
3610 if (in_reg_window (reg
, udata
))
3616 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3617 return (word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
]);
3622 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3623 frame called by UDATA or 0. */
3625 static inline void *
3626 get_reg (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3628 return (void *)(ptr_type
) *get_reg_addr (reg
, udata
, sub_udata
);
3631 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3634 put_reg (unsigned reg
, void *val
, frame_state
*udata
)
3636 *get_reg_addr (reg
, udata
, NULL
) = (word_type
)(ptr_type
) val
;
3639 /* Copy the saved value for register REG from frame UDATA to frame
3640 TARGET_UDATA. Unlike the previous two functions, this can handle
3641 registers that are not one word large. */
3644 copy_reg (unsigned reg
, frame_state
*udata
, frame_state
*target_udata
)
3646 word_type
*preg
= get_reg_addr (reg
, udata
, NULL
);
3647 word_type
*ptreg
= get_reg_addr (reg
, target_udata
, NULL
);
3649 memcpy (ptreg
, preg
, __builtin_dwarf_reg_size (reg
));
3652 /* Retrieve the return address for frame UDATA. */
3654 static inline void *
3655 get_return_addr (frame_state
*udata
, frame_state
*sub_udata
)
3657 return __builtin_extract_return_addr
3658 (get_reg (udata
->retaddr_column
, udata
, sub_udata
));
3661 /* Overwrite the return address for frame UDATA with VAL. */
3664 put_return_addr (void *val
, frame_state
*udata
)
3666 val
= __builtin_frob_return_addr (val
);
3667 put_reg (udata
->retaddr_column
, val
, udata
);
3670 /* Given the current frame UDATA and its return address PC, return the
3671 information about the calling frame in CALLER_UDATA. */
3674 next_stack_level (void *pc
, frame_state
*udata
, frame_state
*caller_udata
)
3676 caller_udata
= __frame_state_for (pc
, caller_udata
);
3680 /* Now go back to our caller's stack frame. If our caller's CFA register
3681 was saved in our stack frame, restore it; otherwise, assume the CFA
3682 register is SP and restore it to our CFA value. */
3683 if (udata
->saved
[caller_udata
->cfa_reg
])
3684 caller_udata
->cfa
= get_reg (caller_udata
->cfa_reg
, udata
, 0);
3686 caller_udata
->cfa
= udata
->cfa
;
3687 caller_udata
->cfa
+= caller_udata
->cfa_offset
;
3689 return caller_udata
;
3692 /* Hook to call before __terminate if only cleanup handlers remain. */
3694 __unwinding_cleanup ()
3698 /* throw_helper performs some of the common grunt work for a throw. This
3699 routine is called by throw and rethrows. This is pretty much split
3700 out from the old __throw routine. An addition has been added which allows
3701 for a dummy call to a routine __unwinding_cleanup() when there are nothing
3702 but cleanups remaining. This allows a debugger to examine the state
3703 at which the throw was executed, before any cleanups, rather than
3704 at the terminate point after the stack has been unwound.
3706 EH is the current eh_context structure.
3707 PC is the address of the call to __throw.
3708 MY_UDATA is the unwind information for __throw.
3709 OFFSET_P is where we return the SP adjustment offset. */
3712 throw_helper (eh
, pc
, my_udata
, offset_p
)
3713 struct eh_context
*eh
;
3715 frame_state
*my_udata
;
3718 frame_state ustruct2
, *udata
= &ustruct2
;
3719 frame_state ustruct
;
3720 frame_state
*sub_udata
= &ustruct
;
3721 void *saved_pc
= pc
;
3725 frame_state saved_ustruct
;
3728 int only_cleanup
= 0;
3730 int saved_state
= 0;
3732 __eh_info
*eh_info
= (__eh_info
*)eh
->info
;
3734 /* Do we find a handler based on a re-throw PC? */
3735 if (eh
->table_index
!= (void *) 0)
3738 memcpy (udata
, my_udata
, sizeof (*udata
));
3740 handler
= (void *) 0;
3743 frame_state
*p
= udata
;
3744 udata
= next_stack_level (pc
, udata
, sub_udata
);
3747 /* If we couldn't find the next frame, we lose. */
3751 if (udata
->eh_ptr
== NULL
)
3754 new_eh_model
= (((exception_descriptor
*)(udata
->eh_ptr
))->
3755 runtime_id_field
== NEW_EH_RUNTIME
);
3760 handler
= find_exception_handler (eh
->table_index
, udata
->eh_ptr
,
3761 eh_info
, 1, &cleanup
);
3762 eh
->table_index
= (void *)0;
3766 handler
= find_exception_handler (pc
, udata
->eh_ptr
, eh_info
,
3769 handler
= old_find_exception_handler (pc
, udata
->eh_ptr
);
3771 /* If we found one, we can stop searching, if its not a cleanup.
3772 for cleanups, we save the state, and keep looking. This allows
3773 us to call a debug hook if there are nothing but cleanups left. */
3780 saved_ustruct
= *udata
;
3781 handler_p
= handler
;
3794 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3795 hitting the beginning of the next region. */
3796 pc
= get_return_addr (udata
, sub_udata
) - 1;
3801 udata
= &saved_ustruct
;
3802 handler
= handler_p
;
3805 __unwinding_cleanup ();
3808 /* If we haven't found a handler by now, this is an unhandled
3813 eh
->handler_label
= handler
;
3815 args_size
= udata
->args_size
;
3818 /* We found a handler in the throw context, no need to unwind. */
3824 /* Unwind all the frames between this one and the handler by copying
3825 their saved register values into our register save slots. */
3827 /* Remember the PC where we found the handler. */
3828 void *handler_pc
= pc
;
3830 /* Start from the throw context again. */
3832 memcpy (udata
, my_udata
, sizeof (*udata
));
3834 while (pc
!= handler_pc
)
3836 frame_state
*p
= udata
;
3837 udata
= next_stack_level (pc
, udata
, sub_udata
);
3840 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
3841 if (i
!= udata
->retaddr_column
&& udata
->saved
[i
])
3843 /* If you modify the saved value of the return address
3844 register on the SPARC, you modify the return address for
3845 your caller's frame. Don't do that here, as it will
3846 confuse get_return_addr. */
3847 if (in_reg_window (i
, udata
)
3848 && udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
3849 && udata
->reg_or_offset
[udata
->retaddr_column
] == i
)
3851 copy_reg (i
, udata
, my_udata
);
3854 pc
= get_return_addr (udata
, sub_udata
) - 1;
3857 /* But we do need to update the saved return address register from
3858 the last frame we unwind, or the handler frame will have the wrong
3860 if (udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
)
3862 i
= udata
->reg_or_offset
[udata
->retaddr_column
];
3863 if (in_reg_window (i
, udata
))
3864 copy_reg (i
, udata
, my_udata
);
3867 /* udata now refers to the frame called by the handler frame. */
3869 /* We adjust SP by the difference between __throw's CFA and the CFA for
3870 the frame called by the handler frame, because those CFAs correspond
3871 to the SP values at the two call sites. We need to further adjust by
3872 the args_size of the handler frame itself to get the handler frame's
3873 SP from before the args were pushed for that call. */
3874 #ifdef STACK_GROWS_DOWNWARD
3875 *offset_p
= udata
->cfa
- my_udata
->cfa
+ args_size
;
3877 *offset_p
= my_udata
->cfa
- udata
->cfa
- args_size
;
3884 /* We first search for an exception handler, and if we don't find
3885 it, we call __terminate on the current stack frame so that we may
3886 use the debugger to walk the stack and understand why no handler
3889 If we find one, then we unwind the frames down to the one that
3890 has the handler and transfer control into the handler. */
3892 /*extern void __throw(void) __attribute__ ((__noreturn__));*/
3897 struct eh_context
*eh
= (*get_eh_context
) ();
3901 /* XXX maybe make my_ustruct static so we don't have to look it up for
3903 frame_state my_ustruct
, *my_udata
= &my_ustruct
;
3905 /* This is required for C++ semantics. We must call terminate if we
3906 try and rethrow an exception, when there is no exception currently
3911 /* Start at our stack frame. */
3913 my_udata
= __frame_state_for (&&label
, my_udata
);
3917 /* We need to get the value from the CFA register. */
3918 my_udata
->cfa
= __builtin_dwarf_cfa ();
3920 /* Do any necessary initialization to access arbitrary stack frames.
3921 On the SPARC, this means flushing the register windows. */
3922 __builtin_unwind_init ();
3924 /* Now reset pc to the right throw point. */
3925 pc
= __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3927 handler
= throw_helper (eh
, pc
, my_udata
, &offset
);
3931 __builtin_eh_return ((void *)eh
, offset
, handler
);
3933 /* Epilogue: restore the handler frame's register values and return
3937 /*extern void __rethrow(void *) __attribute__ ((__noreturn__));*/
3943 struct eh_context
*eh
= (*get_eh_context
) ();
3947 /* XXX maybe make my_ustruct static so we don't have to look it up for
3949 frame_state my_ustruct
, *my_udata
= &my_ustruct
;
3951 /* This is required for C++ semantics. We must call terminate if we
3952 try and rethrow an exception, when there is no exception currently
3957 /* This is the table index we want to rethrow from. The value of
3958 the END_REGION label is used for the PC of the throw, and the
3959 search begins with the next table entry. */
3960 eh
->table_index
= index
;
3962 /* Start at our stack frame. */
3964 my_udata
= __frame_state_for (&&label
, my_udata
);
3968 /* We need to get the value from the CFA register. */
3969 my_udata
->cfa
= __builtin_dwarf_cfa ();
3971 /* Do any necessary initialization to access arbitrary stack frames.
3972 On the SPARC, this means flushing the register windows. */
3973 __builtin_unwind_init ();
3975 /* Now reset pc to the right throw point. */
3976 pc
= __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3978 handler
= throw_helper (eh
, pc
, my_udata
, &offset
);
3982 __builtin_eh_return ((void *)eh
, offset
, handler
);
3984 /* Epilogue: restore the handler frame's register values and return
3987 #endif /* DWARF2_UNWIND_INFO */
3992 #ifndef inhibit_libc
3993 /* This gets us __GNU_LIBRARY__. */
3994 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
3997 #ifdef __GNU_LIBRARY__
3998 /* Avoid forcing the library's meaning of `write' on the user program
3999 by using the "internal" name (for use within the library) */
4000 #define write(fd, buf, n) __write((fd), (buf), (n))
4002 #endif /* inhibit_libc */
4004 #define MESSAGE "pure virtual method called\n"
4009 #ifndef inhibit_libc
4010 write (2, MESSAGE
, sizeof (MESSAGE
) - 1);