1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 92-97, 1998 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
29 /* It is incorrect to include config.h here, because this file is being
30 compiled for the target, and hence definitions concerning only the host
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 /* In the first part of this file, we are interfacing to calls generated
76 by the compiler itself. These calls pass values into these routines
77 which have very specific modes (rather than very specific types), and
78 these compiler-generated calls also expect any return values to have
79 very specific modes (rather than very specific types). Thus, we need
80 to avoid using regular C language type names in this part of the file
81 because the sizes for those types can be configured to be anything.
82 Instead we use the following special type names. */
84 typedef unsigned int UQItype
__attribute__ ((mode (QI
)));
85 typedef int SItype
__attribute__ ((mode (SI
)));
86 typedef unsigned int USItype
__attribute__ ((mode (SI
)));
87 typedef int DItype
__attribute__ ((mode (DI
)));
88 typedef unsigned int UDItype
__attribute__ ((mode (DI
)));
90 typedef float SFtype
__attribute__ ((mode (SF
)));
91 typedef float DFtype
__attribute__ ((mode (DF
)));
93 #if LONG_DOUBLE_TYPE_SIZE == 96
94 typedef float XFtype
__attribute__ ((mode (XF
)));
96 #if LONG_DOUBLE_TYPE_SIZE == 128
97 typedef float TFtype
__attribute__ ((mode (TF
)));
100 typedef int word_type
__attribute__ ((mode (__word__
)));
102 /* Make sure that we don't accidentally use any normal C language built-in
103 type names in the first part of this file. Instead we want to use *only*
104 the type names defined above. The following macro definitions insure
105 that if we *do* accidentally use some normal C language built-in type name,
106 we will get a syntax error. */
108 #define char bogus_type
109 #define short bogus_type
110 #define int bogus_type
111 #define long bogus_type
112 #define unsigned bogus_type
113 #define float bogus_type
114 #define double bogus_type
116 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
118 /* DIstructs are pairs of SItype values in the order determined by
119 LIBGCC2_WORDS_BIG_ENDIAN. */
121 #if LIBGCC2_WORDS_BIG_ENDIAN
122 struct DIstruct
{SItype high
, low
;};
124 struct DIstruct
{SItype low
, high
;};
127 /* We need this union to unpack/pack DImode values, since we don't have
128 any arithmetic yet. Incoming DImode parameters are stored into the
129 `ll' field, and the unpacked result is read from the struct `s'. */
137 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
138 || defined (L_divdi3) || defined (L_udivdi3) \
139 || defined (L_moddi3) || defined (L_umoddi3))
141 #include "longlong.h"
143 #endif /* udiv or mul */
145 extern DItype
__fixunssfdi (SFtype a
);
146 extern DItype
__fixunsdfdi (DFtype a
);
147 #if LONG_DOUBLE_TYPE_SIZE == 96
148 extern DItype
__fixunsxfdi (XFtype a
);
150 #if LONG_DOUBLE_TYPE_SIZE == 128
151 extern DItype
__fixunstfdi (TFtype a
);
154 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
155 #if defined (L_divdi3) || defined (L_moddi3)
167 w
.s
.high
= -uu
.s
.high
- ((USItype
) w
.s
.low
> 0);
173 /* Unless shift functions are defined whith full ANSI prototypes,
174 parameter b will be promoted to int if word_type is smaller than an int. */
177 __lshrdi3 (DItype u
, word_type b
)
188 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
192 w
.s
.low
= (USItype
)uu
.s
.high
>> -bm
;
196 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
197 w
.s
.high
= (USItype
)uu
.s
.high
>> b
;
198 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
207 __ashldi3 (DItype u
, word_type b
)
218 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
222 w
.s
.high
= (USItype
)uu
.s
.low
<< -bm
;
226 USItype carries
= (USItype
)uu
.s
.low
>> bm
;
227 w
.s
.low
= (USItype
)uu
.s
.low
<< b
;
228 w
.s
.high
= ((USItype
)uu
.s
.high
<< b
) | carries
;
237 __ashrdi3 (DItype u
, word_type b
)
248 bm
= (sizeof (SItype
) * BITS_PER_UNIT
) - b
;
251 /* w.s.high = 1..1 or 0..0 */
252 w
.s
.high
= uu
.s
.high
>> (sizeof (SItype
) * BITS_PER_UNIT
- 1);
253 w
.s
.low
= uu
.s
.high
>> -bm
;
257 USItype carries
= (USItype
)uu
.s
.high
<< bm
;
258 w
.s
.high
= uu
.s
.high
>> b
;
259 w
.s
.low
= ((USItype
)uu
.s
.low
>> b
) | carries
;
273 w
.s
.low
= ffs (uu
.s
.low
);
276 w
.s
.low
= ffs (uu
.s
.high
);
279 w
.s
.low
+= BITS_PER_UNIT
* sizeof (SItype
);
288 __muldi3 (DItype u
, DItype v
)
296 w
.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
);
297 w
.s
.high
+= ((USItype
) uu
.s
.low
* (USItype
) vv
.s
.high
298 + (USItype
) uu
.s
.high
* (USItype
) vv
.s
.low
);
305 #if defined (sdiv_qrnnd)
307 __udiv_w_sdiv (USItype
*rp
, USItype a1
, USItype a0
, USItype d
)
314 if (a1
< d
- a1
- (a0
>> (SI_TYPE_SIZE
- 1)))
316 /* dividend, divisor, and quotient are nonnegative */
317 sdiv_qrnnd (q
, r
, a1
, a0
, d
);
321 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
322 sub_ddmmss (c1
, c0
, a1
, a0
, d
>> 1, d
<< (SI_TYPE_SIZE
- 1));
323 /* Divide (c1*2^32 + c0) by d */
324 sdiv_qrnnd (q
, r
, c1
, c0
, d
);
325 /* Add 2^31 to quotient */
326 q
+= (USItype
) 1 << (SI_TYPE_SIZE
- 1);
331 b1
= d
>> 1; /* d/2, between 2^30 and 2^31 - 1 */
332 c1
= a1
>> 1; /* A/2 */
333 c0
= (a1
<< (SI_TYPE_SIZE
- 1)) + (a0
>> 1);
335 if (a1
< b1
) /* A < 2^32*b1, so A/2 < 2^31*b1 */
337 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
339 r
= 2*r
+ (a0
& 1); /* Remainder from A/(2*b1) */
356 else if (c1
< b1
) /* So 2^31 <= (A/2)/b1 < 2^32 */
359 c0
= ~c0
; /* logical NOT */
361 sdiv_qrnnd (q
, r
, c1
, c0
, b1
); /* (A/2) / (d/2) */
363 q
= ~q
; /* (A/2)/b1 */
366 r
= 2*r
+ (a0
& 1); /* A/(2*b1) */
384 else /* Implies c1 = b1 */
385 { /* Hence a1 = d - 1 = 2*b1 - 1 */
403 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
405 __udiv_w_sdiv (USItype
*rp
__attribute__ ((__unused__
)),
406 USItype a1
__attribute__ ((__unused__
)),
407 USItype a0
__attribute__ ((__unused__
)),
408 USItype d
__attribute__ ((__unused__
)))
415 #if (defined (L_udivdi3) || defined (L_divdi3) || \
416 defined (L_umoddi3) || defined (L_moddi3))
421 static const UQItype __clz_tab
[] =
423 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,
424 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,
425 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,
426 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,
427 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,
428 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,
429 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,
430 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 #if (defined (L_udivdi3) || defined (L_divdi3) || \
434 defined (L_umoddi3) || defined (L_moddi3))
438 __udivmoddi4 (UDItype n
, UDItype d
, UDItype
*rp
)
443 USItype d0
, d1
, n0
, n1
, n2
;
455 #if !UDIV_NEEDS_NORMALIZATION
462 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
465 /* Remainder in n0. */
472 d0
= 1 / d0
; /* Divide intentionally by zero. */
474 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
475 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
477 /* Remainder in n0. */
488 #else /* UDIV_NEEDS_NORMALIZATION */
496 count_leading_zeros (bm
, d0
);
500 /* Normalize, i.e. make the most significant bit of the
504 n1
= (n1
<< bm
) | (n0
>> (SI_TYPE_SIZE
- bm
));
508 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
511 /* Remainder in n0 >> bm. */
518 d0
= 1 / d0
; /* Divide intentionally by zero. */
520 count_leading_zeros (bm
, d0
);
524 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
525 conclude (the most significant bit of n1 is set) /\ (the
526 leading quotient digit q1 = 1).
528 This special case is necessary, not an optimization.
529 (Shifts counts of SI_TYPE_SIZE are undefined.) */
538 b
= SI_TYPE_SIZE
- bm
;
542 n1
= (n1
<< bm
) | (n0
>> b
);
545 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
550 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
552 /* Remainder in n0 >> bm. */
562 #endif /* UDIV_NEEDS_NORMALIZATION */
573 /* Remainder in n1n0. */
585 count_leading_zeros (bm
, d1
);
588 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
589 conclude (the most significant bit of n1 is set) /\ (the
590 quotient digit q0 = 0 or 1).
592 This special case is necessary, not an optimization. */
594 /* The condition on the next line takes advantage of that
595 n1 >= d1 (true due to program flow). */
596 if (n1
> d1
|| n0
>= d0
)
599 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
618 b
= SI_TYPE_SIZE
- bm
;
620 d1
= (d1
<< bm
) | (d0
>> b
);
623 n1
= (n1
<< bm
) | (n0
>> b
);
626 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
627 umul_ppmm (m1
, m0
, q0
, d0
);
629 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
632 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
637 /* Remainder in (n1n0 - m1m0) >> bm. */
640 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
641 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
642 rr
.s
.high
= n1
>> bm
;
656 UDItype
__udivmoddi4 ();
659 __divdi3 (DItype u
, DItype v
)
670 uu
.ll
= __negdi2 (uu
.ll
);
673 vv
.ll
= __negdi2 (vv
.ll
);
675 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDItype
*) 0);
684 UDItype
__udivmoddi4 ();
686 __moddi3 (DItype u
, DItype v
)
697 uu
.ll
= __negdi2 (uu
.ll
);
699 vv
.ll
= __negdi2 (vv
.ll
);
701 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, &w
);
710 UDItype
__udivmoddi4 ();
712 __umoddi3 (UDItype u
, UDItype v
)
716 (void) __udivmoddi4 (u
, v
, &w
);
723 UDItype
__udivmoddi4 ();
725 __udivdi3 (UDItype n
, UDItype d
)
727 return __udivmoddi4 (n
, d
, (UDItype
*) 0);
733 __cmpdi2 (DItype a
, DItype b
)
737 au
.ll
= a
, bu
.ll
= b
;
739 if (au
.s
.high
< bu
.s
.high
)
741 else if (au
.s
.high
> bu
.s
.high
)
743 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
745 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
753 __ucmpdi2 (DItype a
, DItype b
)
757 au
.ll
= a
, bu
.ll
= b
;
759 if ((USItype
) au
.s
.high
< (USItype
) bu
.s
.high
)
761 else if ((USItype
) au
.s
.high
> (USItype
) bu
.s
.high
)
763 if ((USItype
) au
.s
.low
< (USItype
) bu
.s
.low
)
765 else if ((USItype
) au
.s
.low
> (USItype
) bu
.s
.low
)
771 #if defined(L_fixunstfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
772 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
773 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
776 __fixunstfdi (TFtype a
)
784 /* Compute high word of result, as a flonum. */
785 b
= (a
/ HIGH_WORD_COEFF
);
786 /* Convert that to fixed (but not to DItype!),
787 and shift it into the high word. */
790 /* Remove high part from the TFtype, leaving the low part as flonum. */
792 /* Convert that to fixed (but not to DItype!) and add it in.
793 Sometimes A comes out negative. This is significant, since
794 A has more bits than a long int does. */
796 v
-= (USItype
) (- a
);
803 #if defined(L_fixtfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
808 return - __fixunstfdi (-a
);
809 return __fixunstfdi (a
);
813 #if defined(L_fixunsxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
814 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
815 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
818 __fixunsxfdi (XFtype a
)
826 /* Compute high word of result, as a flonum. */
827 b
= (a
/ HIGH_WORD_COEFF
);
828 /* Convert that to fixed (but not to DItype!),
829 and shift it into the high word. */
832 /* Remove high part from the XFtype, leaving the low part as flonum. */
834 /* Convert that to fixed (but not to DItype!) and add it in.
835 Sometimes A comes out negative. This is significant, since
836 A has more bits than a long int does. */
838 v
-= (USItype
) (- a
);
845 #if defined(L_fixxfdi) && (LONG_DOUBLE_TYPE_SIZE == 96)
850 return - __fixunsxfdi (-a
);
851 return __fixunsxfdi (a
);
856 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
857 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
860 __fixunsdfdi (DFtype a
)
868 /* Compute high word of result, as a flonum. */
869 b
= (a
/ HIGH_WORD_COEFF
);
870 /* Convert that to fixed (but not to DItype!),
871 and shift it into the high word. */
874 /* Remove high part from the DFtype, leaving the low part as flonum. */
876 /* Convert that to fixed (but not to DItype!) and add it in.
877 Sometimes A comes out negative. This is significant, since
878 A has more bits than a long int does. */
880 v
-= (USItype
) (- a
);
892 return - __fixunsdfdi (-a
);
893 return __fixunsdfdi (a
);
898 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
899 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
902 __fixunssfdi (SFtype original_a
)
904 /* Convert the SFtype to a DFtype, because that is surely not going
905 to lose any bits. Some day someone else can write a faster version
906 that avoids converting to DFtype, and verify it really works right. */
907 DFtype a
= original_a
;
914 /* Compute high word of result, as a flonum. */
915 b
= (a
/ HIGH_WORD_COEFF
);
916 /* Convert that to fixed (but not to DItype!),
917 and shift it into the high word. */
920 /* Remove high part from the DFtype, leaving the low part as flonum. */
922 /* Convert that to fixed (but not to DItype!) and add it in.
923 Sometimes A comes out negative. This is significant, since
924 A has more bits than a long int does. */
926 v
-= (USItype
) (- a
);
938 return - __fixunssfdi (-a
);
939 return __fixunssfdi (a
);
943 #if defined(L_floatdixf) && (LONG_DOUBLE_TYPE_SIZE == 96)
944 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
945 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
946 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
949 __floatdixf (DItype u
)
953 d
= (SItype
) (u
>> WORD_SIZE
);
954 d
*= HIGH_HALFWORD_COEFF
;
955 d
*= HIGH_HALFWORD_COEFF
;
956 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
962 #if defined(L_floatditf) && (LONG_DOUBLE_TYPE_SIZE == 128)
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 __floatditf (DItype u
)
972 d
= (SItype
) (u
>> WORD_SIZE
);
973 d
*= HIGH_HALFWORD_COEFF
;
974 d
*= HIGH_HALFWORD_COEFF
;
975 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
982 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
983 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
984 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
987 __floatdidf (DItype u
)
991 d
= (SItype
) (u
>> WORD_SIZE
);
992 d
*= HIGH_HALFWORD_COEFF
;
993 d
*= HIGH_HALFWORD_COEFF
;
994 d
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1001 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1002 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1003 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1004 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1006 /* Define codes for all the float formats that we know of. Note
1007 that this is copied from real.h. */
1009 #define UNKNOWN_FLOAT_FORMAT 0
1010 #define IEEE_FLOAT_FORMAT 1
1011 #define VAX_FLOAT_FORMAT 2
1012 #define IBM_FLOAT_FORMAT 3
1014 /* Default to IEEE float if not specified. Nearly all machines use it. */
1015 #ifndef HOST_FLOAT_FORMAT
1016 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1019 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1024 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1029 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1035 __floatdisf (DItype u
)
1037 /* Do the calculation in DFmode
1038 so that we don't lose any of the precision of the high word
1039 while multiplying it. */
1042 /* Protect against double-rounding error.
1043 Represent any low-order bits, that might be truncated in DFmode,
1044 by a bit that won't be lost. The bit can go in anywhere below the
1045 rounding position of the SFmode. A fixed mask and bit position
1046 handles all usual configurations. It doesn't handle the case
1047 of 128-bit DImode, however. */
1048 if (DF_SIZE
< DI_SIZE
1049 && DF_SIZE
> (DI_SIZE
- DF_SIZE
+ SF_SIZE
))
1051 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1052 if (! (- ((DItype
) 1 << DF_SIZE
) < u
1053 && u
< ((DItype
) 1 << DF_SIZE
)))
1055 if ((USItype
) u
& (REP_BIT
- 1))
1059 f
= (SItype
) (u
>> WORD_SIZE
);
1060 f
*= HIGH_HALFWORD_COEFF
;
1061 f
*= HIGH_HALFWORD_COEFF
;
1062 f
+= (USItype
) (u
& (HIGH_WORD_COEFF
- 1));
1068 #if defined(L_fixunsxfsi) && LONG_DOUBLE_TYPE_SIZE == 96
1069 /* Reenable the normal types, in case limits.h needs them. */
1082 __fixunsxfsi (XFtype a
)
1084 if (a
>= - (DFtype
) LONG_MIN
)
1085 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1091 /* Reenable the normal types, in case limits.h needs them. */
1104 __fixunsdfsi (DFtype a
)
1106 if (a
>= - (DFtype
) LONG_MIN
)
1107 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1113 /* Reenable the normal types, in case limits.h needs them. */
1126 __fixunssfsi (SFtype a
)
1128 if (a
>= - (SFtype
) LONG_MIN
)
1129 return (SItype
) (a
+ LONG_MIN
) - LONG_MIN
;
1134 /* From here on down, the routines use normal data types. */
1136 #define SItype bogus_type
1137 #define USItype bogus_type
1138 #define DItype bogus_type
1139 #define UDItype bogus_type
1140 #define SFtype bogus_type
1141 #define DFtype bogus_type
1153 /* Like bcmp except the sign is meaningful.
1154 Result is negative if S1 is less than S2,
1155 positive if S1 is greater, 0 if S1 and S2 are equal. */
1158 __gcc_bcmp (unsigned char *s1
, unsigned char *s2
, size_t size
)
1162 unsigned char c1
= *s1
++, c2
= *s2
++;
1179 #if defined(__svr4__) || defined(__alliant__)
1183 /* The Alliant needs the added underscore. */
1184 asm (".globl __builtin_saveregs");
1185 asm ("__builtin_saveregs:");
1186 asm (".globl ___builtin_saveregs");
1187 asm ("___builtin_saveregs:");
1189 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1190 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1191 area and also for a new va_list
1193 /* Save all argument registers in the arg reg save area. The
1194 arg reg save area must have the following layout (according
1206 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1207 asm (" fst.q %f12,16(%sp)");
1209 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1210 asm (" st.l %r17,36(%sp)");
1211 asm (" st.l %r18,40(%sp)");
1212 asm (" st.l %r19,44(%sp)");
1213 asm (" st.l %r20,48(%sp)");
1214 asm (" st.l %r21,52(%sp)");
1215 asm (" st.l %r22,56(%sp)");
1216 asm (" st.l %r23,60(%sp)");
1217 asm (" st.l %r24,64(%sp)");
1218 asm (" st.l %r25,68(%sp)");
1219 asm (" st.l %r26,72(%sp)");
1220 asm (" st.l %r27,76(%sp)");
1222 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1223 va_list structure. Put in into
1224 r16 so that it will be returned
1227 /* Initialize all fields of the new va_list structure. This
1228 structure looks like:
1231 unsigned long ireg_used;
1232 unsigned long freg_used;
1238 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1239 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1240 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1241 asm (" bri %r1"); /* delayed return */
1242 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1244 #else /* not __svr4__ */
1245 #if defined(__PARAGON__)
1247 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1248 * and we stand a better chance of hooking into libraries
1249 * compiled by PGI. [andyp@ssd.intel.com]
1253 asm (".globl __builtin_saveregs");
1254 asm ("__builtin_saveregs:");
1255 asm (".globl ___builtin_saveregs");
1256 asm ("___builtin_saveregs:");
1258 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1259 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1260 area and also for a new va_list
1262 /* Save all argument registers in the arg reg save area. The
1263 arg reg save area must have the following layout (according
1275 asm (" fst.q f8, 0(sp)");
1276 asm (" fst.q f12,16(sp)");
1277 asm (" st.l r16,32(sp)");
1278 asm (" st.l r17,36(sp)");
1279 asm (" st.l r18,40(sp)");
1280 asm (" st.l r19,44(sp)");
1281 asm (" st.l r20,48(sp)");
1282 asm (" st.l r21,52(sp)");
1283 asm (" st.l r22,56(sp)");
1284 asm (" st.l r23,60(sp)");
1285 asm (" st.l r24,64(sp)");
1286 asm (" st.l r25,68(sp)");
1287 asm (" st.l r26,72(sp)");
1288 asm (" st.l r27,76(sp)");
1290 asm (" adds 80,sp,r16"); /* compute the address of the new
1291 va_list structure. Put in into
1292 r16 so that it will be returned
1295 /* Initialize all fields of the new va_list structure. This
1296 structure looks like:
1299 unsigned long ireg_used;
1300 unsigned long freg_used;
1306 asm (" st.l r0, 0(r16)"); /* nfixed */
1307 asm (" st.l r0, 4(r16)"); /* nfloating */
1308 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1309 asm (" bri r1"); /* delayed return */
1310 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1311 #else /* not __PARAGON__ */
1315 asm (".globl ___builtin_saveregs");
1316 asm ("___builtin_saveregs:");
1317 asm (" mov sp,r30");
1318 asm (" andnot 0x0f,sp,sp");
1319 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1321 /* Fill in the __va_struct. */
1322 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1323 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1324 asm (" st.l r18, 8(sp)");
1325 asm (" st.l r19,12(sp)");
1326 asm (" st.l r20,16(sp)");
1327 asm (" st.l r21,20(sp)");
1328 asm (" st.l r22,24(sp)");
1329 asm (" st.l r23,28(sp)");
1330 asm (" st.l r24,32(sp)");
1331 asm (" st.l r25,36(sp)");
1332 asm (" st.l r26,40(sp)");
1333 asm (" st.l r27,44(sp)");
1335 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1336 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1338 /* Fill in the __va_ctl. */
1339 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1340 asm (" st.l r28,84(sp)"); /* pointer to more args */
1341 asm (" st.l r0, 88(sp)"); /* nfixed */
1342 asm (" st.l r0, 92(sp)"); /* nfloating */
1344 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1346 asm (" mov r30,sp");
1347 /* recover stack and pass address to start
1349 #endif /* not __PARAGON__ */
1350 #endif /* not __svr4__ */
1351 #else /* not __i860__ */
1353 asm (".global __builtin_saveregs");
1354 asm ("__builtin_saveregs:");
1355 asm (".global ___builtin_saveregs");
1356 asm ("___builtin_saveregs:");
1357 #ifdef NEED_PROC_COMMAND
1360 asm ("st %i0,[%fp+68]");
1361 asm ("st %i1,[%fp+72]");
1362 asm ("st %i2,[%fp+76]");
1363 asm ("st %i3,[%fp+80]");
1364 asm ("st %i4,[%fp+84]");
1366 asm ("st %i5,[%fp+88]");
1367 #ifdef NEED_TYPE_COMMAND
1368 asm (".type __builtin_saveregs,#function");
1369 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1371 #else /* not __sparc__ */
1372 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1376 asm (" .set nomips16");
1378 asm (" .ent __builtin_saveregs");
1379 asm (" .globl __builtin_saveregs");
1380 asm ("__builtin_saveregs:");
1381 asm (" sw $4,0($30)");
1382 asm (" sw $5,4($30)");
1383 asm (" sw $6,8($30)");
1384 asm (" sw $7,12($30)");
1386 asm (" .end __builtin_saveregs");
1387 #else /* not __mips__, etc. */
1390 __builtin_saveregs ()
1395 #endif /* not __mips__ */
1396 #endif /* not __sparc__ */
1397 #endif /* not __i860__ */
1401 #ifndef inhibit_libc
1403 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1405 /* This is used by the `assert' macro. */
1407 __eprintf (const char *string
, const char *expression
,
1408 unsigned int line
, const char *filename
)
1410 fprintf (stderr
, string
, expression
, line
, filename
);
1420 /* Structure emitted by -a */
1424 const char *filename
;
1428 const unsigned long *addresses
;
1430 /* Older GCC's did not emit these fields. */
1432 const char **functions
;
1433 const long *line_nums
;
1434 const char **filenames
;
1438 #ifdef BLOCK_PROFILER_CODE
1441 #ifndef inhibit_libc
1443 /* Simple minded basic block profiling output dumper for
1444 systems that don't provide tcov support. At present,
1445 it requires atexit and stdio. */
1447 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1451 #include "gbl-ctors.h"
1452 #include "gcov-io.h"
1455 static struct bb
*bb_head
;
1457 /* Return the number of digits needed to print a value */
1458 /* __inline__ */ static int num_digits (long value
, int base
)
1460 int minus
= (value
< 0 && base
!= 16);
1461 unsigned long v
= (minus
) ? -value
: value
;
1475 __bb_exit_func (void)
1477 FILE *da_file
, *file
;
1484 i
= strlen (bb_head
->filename
) - 3;
1486 if (!strcmp (bb_head
->filename
+i
, ".da"))
1488 /* Must be -fprofile-arcs not -a.
1489 Dump data in a form that gcov expects. */
1493 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1495 /* If the file exists, and the number of counts in it is the same,
1496 then merge them in. */
1498 if ((da_file
= fopen (ptr
->filename
, "r")) != 0)
1502 if (__read_long (&n_counts
, da_file
, 8) != 0)
1504 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1509 if (n_counts
== ptr
->ncounts
)
1513 for (i
= 0; i
< n_counts
; i
++)
1517 if (__read_long (&v
, da_file
, 8) != 0)
1519 fprintf (stderr
, "arc profiling: Can't read output file %s.\n",
1523 ptr
->counts
[i
] += v
;
1527 if (fclose (da_file
) == EOF
)
1528 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1531 if ((da_file
= fopen (ptr
->filename
, "w")) == 0)
1533 fprintf (stderr
, "arc profiling: Can't open output file %s.\n",
1538 /* ??? Should first write a header to the file. Preferably, a 4 byte
1539 magic number, 4 bytes containing the time the program was
1540 compiled, 4 bytes containing the last modification time of the
1541 source file, and 4 bytes indicating the compiler options used.
1543 That way we can easily verify that the proper source/executable/
1544 data file combination is being used from gcov. */
1546 if (__write_long (ptr
->ncounts
, da_file
, 8) != 0)
1549 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1555 long *count_ptr
= ptr
->counts
;
1557 for (j
= ptr
->ncounts
; j
> 0; j
--)
1559 if (__write_long (*count_ptr
, da_file
, 8) != 0)
1567 fprintf (stderr
, "arc profiling: Error writing output file %s.\n",
1571 if (fclose (da_file
) == EOF
)
1572 fprintf (stderr
, "arc profiling: Error closing output file %s.\n",
1579 /* Must be basic block profiling. Emit a human readable output file. */
1581 file
= fopen ("bb.out", "a");
1590 /* This is somewhat type incorrect, but it avoids worrying about
1591 exactly where time.h is included from. It should be ok unless
1592 a void * differs from other pointer formats, or if sizeof (long)
1593 is < sizeof (time_t). It would be nice if we could assume the
1594 use of rationale standards here. */
1596 time ((void *) &time_value
);
1597 fprintf (file
, "Basic block profiling finished on %s\n", ctime ((void *) &time_value
));
1599 /* We check the length field explicitly in order to allow compatibility
1600 with older GCC's which did not provide it. */
1602 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1605 int func_p
= (ptr
->nwords
>= sizeof (struct bb
)
1606 && ptr
->nwords
<= 1000
1608 int line_p
= (func_p
&& ptr
->line_nums
);
1609 int file_p
= (func_p
&& ptr
->filenames
);
1610 int addr_p
= (ptr
->addresses
!= 0);
1611 long ncounts
= ptr
->ncounts
;
1617 int blk_len
= num_digits (ncounts
, 10);
1622 fprintf (file
, "File %s, %ld basic blocks \n\n",
1623 ptr
->filename
, ncounts
);
1625 /* Get max values for each field. */
1626 for (i
= 0; i
< ncounts
; i
++)
1631 if (cnt_max
< ptr
->counts
[i
])
1632 cnt_max
= ptr
->counts
[i
];
1634 if (addr_p
&& addr_max
< ptr
->addresses
[i
])
1635 addr_max
= ptr
->addresses
[i
];
1637 if (line_p
&& line_max
< ptr
->line_nums
[i
])
1638 line_max
= ptr
->line_nums
[i
];
1642 p
= (ptr
->functions
[i
]) ? (ptr
->functions
[i
]) : "<none>";
1650 p
= (ptr
->filenames
[i
]) ? (ptr
->filenames
[i
]) : "<none>";
1657 addr_len
= num_digits (addr_max
, 16);
1658 cnt_len
= num_digits (cnt_max
, 10);
1659 line_len
= num_digits (line_max
, 10);
1661 /* Now print out the basic block information. */
1662 for (i
= 0; i
< ncounts
; i
++)
1665 " Block #%*d: executed %*ld time(s)",
1667 cnt_len
, ptr
->counts
[i
]);
1670 fprintf (file
, " address= 0x%.*lx", addr_len
,
1674 fprintf (file
, " function= %-*s", func_len
,
1675 (ptr
->functions
[i
]) ? ptr
->functions
[i
] : "<none>");
1678 fprintf (file
, " line= %*ld", line_len
, ptr
->line_nums
[i
]);
1681 fprintf (file
, " file= %s",
1682 (ptr
->filenames
[i
]) ? ptr
->filenames
[i
] : "<none>");
1684 fprintf (file
, "\n");
1687 fprintf (file
, "\n");
1691 fprintf (file
, "\n\n");
1697 __bb_init_func (struct bb
*blocks
)
1699 /* User is supposed to check whether the first word is non-0,
1700 but just in case.... */
1702 if (blocks
->zero_word
)
1706 /* Initialize destructor. */
1708 ON_EXIT (__bb_exit_func
, 0);
1711 /* Set up linked list. */
1712 blocks
->zero_word
= 1;
1713 blocks
->next
= bb_head
;
1717 #ifndef MACHINE_STATE_SAVE
1718 #define MACHINE_STATE_SAVE(ID)
1720 #ifndef MACHINE_STATE_RESTORE
1721 #define MACHINE_STATE_RESTORE(ID)
1724 /* Number of buckets in hashtable of basic block addresses. */
1726 #define BB_BUCKETS 311
1728 /* Maximum length of string in file bb.in. */
1730 #define BBINBUFSIZE 500
1732 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1733 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1735 #define BBINBUFSIZESTR "499"
1739 struct bb_edge
*next
;
1740 unsigned long src_addr
;
1741 unsigned long dst_addr
;
1742 unsigned long count
;
1747 TRACE_KEEP
= 0, TRACE_ON
= 1, TRACE_OFF
= 2
1752 struct bb_func
*next
;
1755 enum bb_func_mode mode
;
1758 /* This is the connection to the outside world.
1759 The BLOCK_PROFILER macro must set __bb.blocks
1760 and __bb.blockno. */
1763 unsigned long blockno
;
1767 /* Vars to store addrs of source and destination basic blocks
1770 static unsigned long bb_src
= 0;
1771 static unsigned long bb_dst
= 0;
1773 static FILE *bb_tracefile
= (FILE *) 0;
1774 static struct bb_edge
**bb_hashbuckets
= (struct bb_edge
**) 0;
1775 static struct bb_func
*bb_func_head
= (struct bb_func
*) 0;
1776 static unsigned long bb_callcount
= 0;
1777 static int bb_mode
= 0;
1779 static unsigned long *bb_stack
= (unsigned long *) 0;
1780 static size_t bb_stacksize
= 0;
1782 static int reported
= 0;
1785 Always : Print execution frequencies of basic blocks
1787 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1788 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1789 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1790 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1795 /*#include <sys/types.h>*/
1796 #include <sys/stat.h>
1797 /*#include <malloc.h>*/
1799 /* Commands executed by gopen. */
1801 #define GOPENDECOMPRESS "gzip -cd "
1802 #define GOPENCOMPRESS "gzip -c >"
1804 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1805 If it does not compile, simply replace gopen by fopen and delete
1806 '.gz' from any first parameter to gopen. */
1809 gopen (char *fn
, char *mode
)
1817 if (mode
[0] != 'r' && mode
[0] != 'w')
1820 p
= fn
+ strlen (fn
)-1;
1821 use_gzip
= ((p
[-1] == '.' && (p
[0] == 'Z' || p
[0] == 'z'))
1822 || (p
[-2] == '.' && p
[-1] == 'g' && p
[0] == 'z'));
1829 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1830 + sizeof (GOPENDECOMPRESS
));
1831 strcpy (s
, GOPENDECOMPRESS
);
1832 strcpy (s
+ (sizeof (GOPENDECOMPRESS
)-1), fn
);
1833 f
= popen (s
, mode
);
1841 char *s
= (char *) malloc (sizeof (char) * strlen (fn
)
1842 + sizeof (GOPENCOMPRESS
));
1843 strcpy (s
, GOPENCOMPRESS
);
1844 strcpy (s
+ (sizeof (GOPENCOMPRESS
)-1), fn
);
1845 if (!(f
= popen (s
, mode
)))
1846 f
= fopen (s
, mode
);
1853 return fopen (fn
, mode
);
1863 if (!fstat (fileno (f
), &buf
) && S_ISFIFO (buf
.st_mode
))
1871 #endif /* HAVE_POPEN */
1873 /* Called once per program. */
1876 __bb_exit_trace_func ()
1878 FILE *file
= fopen ("bb.out", "a");
1891 gclose (bb_tracefile
);
1893 fclose (bb_tracefile
);
1894 #endif /* HAVE_POPEN */
1897 /* Check functions in `bb.in'. */
1902 const struct bb_func
*p
;
1903 int printed_something
= 0;
1907 /* This is somewhat type incorrect. */
1908 time ((void *) &time_value
);
1910 for (p
= bb_func_head
; p
!= (struct bb_func
*) 0; p
= p
->next
)
1912 for (ptr
= bb_head
; ptr
!= (struct bb
*) 0; ptr
= ptr
->next
)
1914 if (!ptr
->filename
|| (p
->filename
!= (char *) 0 && strcmp (p
->filename
, ptr
->filename
)))
1916 for (blk
= 0; blk
< ptr
->ncounts
; blk
++)
1918 if (!strcmp (p
->funcname
, ptr
->functions
[blk
]))
1923 if (!printed_something
)
1925 fprintf (file
, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value
));
1926 printed_something
= 1;
1929 fprintf (file
, "\tFunction %s", p
->funcname
);
1931 fprintf (file
, " of file %s", p
->filename
);
1932 fprintf (file
, "\n" );
1937 if (printed_something
)
1938 fprintf (file
, "\n");
1944 if (!bb_hashbuckets
)
1948 fprintf (stderr
, "Profiler: out of memory\n");
1958 unsigned long addr_max
= 0;
1959 unsigned long cnt_max
= 0;
1963 /* This is somewhat type incorrect, but it avoids worrying about
1964 exactly where time.h is included from. It should be ok unless
1965 a void * differs from other pointer formats, or if sizeof (long)
1966 is < sizeof (time_t). It would be nice if we could assume the
1967 use of rationale standards here. */
1969 time ((void *) &time_value
);
1970 fprintf (file
, "Basic block jump tracing");
1972 switch (bb_mode
& 12)
1975 fprintf (file
, " (with call)");
1979 /* Print nothing. */
1983 fprintf (file
, " (with call & ret)");
1987 fprintf (file
, " (with ret)");
1991 fprintf (file
, " finished on %s\n", ctime ((void *) &time_value
));
1993 for (i
= 0; i
< BB_BUCKETS
; i
++)
1995 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
1996 for ( ; bucket
; bucket
= bucket
->next
)
1998 if (addr_max
< bucket
->src_addr
)
1999 addr_max
= bucket
->src_addr
;
2000 if (addr_max
< bucket
->dst_addr
)
2001 addr_max
= bucket
->dst_addr
;
2002 if (cnt_max
< bucket
->count
)
2003 cnt_max
= bucket
->count
;
2006 addr_len
= num_digits (addr_max
, 16);
2007 cnt_len
= num_digits (cnt_max
, 10);
2009 for ( i
= 0; i
< BB_BUCKETS
; i
++)
2011 struct bb_edge
*bucket
= bb_hashbuckets
[i
];
2012 for ( ; bucket
; bucket
= bucket
->next
)
2014 fprintf (file
, "Jump from block 0x%.*lx to "
2015 "block 0x%.*lx executed %*lu time(s)\n",
2016 addr_len
, bucket
->src_addr
,
2017 addr_len
, bucket
->dst_addr
,
2018 cnt_len
, bucket
->count
);
2022 fprintf (file
, "\n");
2030 /* Free allocated memory. */
2035 struct bb_func
*old
= f
;
2038 if (old
->funcname
) free (old
->funcname
);
2039 if (old
->filename
) free (old
->filename
);
2050 for (i
= 0; i
< BB_BUCKETS
; i
++)
2052 struct bb_edge
*old
, *bucket
= bb_hashbuckets
[i
];
2057 bucket
= bucket
->next
;
2061 free (bb_hashbuckets
);
2064 for (b
= bb_head
; b
; b
= b
->next
)
2065 if (b
->flags
) free (b
->flags
);
2068 /* Called once per program. */
2075 char buf
[BBINBUFSIZE
];
2078 enum bb_func_mode m
;
2081 /* Initialize destructor. */
2082 ON_EXIT (__bb_exit_func
, 0);
2085 if (!(file
= fopen ("bb.in", "r")))
2088 while(fscanf (file
, " %" BBINBUFSIZESTR
"s ", buf
) != EOF
)
2100 if (!strcmp (p
, "__bb_trace__"))
2102 else if (!strcmp (p
, "__bb_jumps__"))
2104 else if (!strcmp (p
, "__bb_hidecall__"))
2106 else if (!strcmp (p
, "__bb_showret__"))
2110 struct bb_func
*f
= (struct bb_func
*) malloc (sizeof (struct bb_func
));
2114 f
->next
= bb_func_head
;
2115 if ((pos
= strchr (p
, ':')))
2117 if (!(f
->funcname
= (char *) malloc (strlen (pos
+1)+1)))
2119 strcpy (f
->funcname
, pos
+1);
2121 if ((f
->filename
= (char *) malloc (l
+1)))
2123 strncpy (f
->filename
, p
, l
);
2124 f
->filename
[l
] = '\0';
2127 f
->filename
= (char *) 0;
2131 if (!(f
->funcname
= (char *) malloc (strlen (p
)+1)))
2133 strcpy (f
->funcname
, p
);
2134 f
->filename
= (char *) 0;
2146 bb_tracefile
= gopen ("bbtrace.gz", "w");
2151 bb_tracefile
= fopen ("bbtrace", "w");
2153 #endif /* HAVE_POPEN */
2157 bb_hashbuckets
= (struct bb_edge
**)
2158 malloc (BB_BUCKETS
* sizeof (struct bb_edge
*));
2160 memset (bb_hashbuckets
, 0, BB_BUCKETS
* sizeof (struct bb_edge
*));
2166 bb_stack
= (unsigned long *) malloc (bb_stacksize
* sizeof (*bb_stack
));
2170 /* Initialize destructor. */
2171 ON_EXIT (__bb_exit_trace_func
, 0);
2176 /* Called upon entering a basic block. */
2181 struct bb_edge
*bucket
;
2183 MACHINE_STATE_SAVE("1")
2185 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2188 bb_dst
= __bb
.blocks
->addresses
[__bb
.blockno
];
2189 __bb
.blocks
->counts
[__bb
.blockno
]++;
2193 fwrite (&bb_dst
, sizeof (unsigned long), 1, bb_tracefile
);
2198 struct bb_edge
**startbucket
, **oldnext
;
2200 oldnext
= startbucket
2201 = & bb_hashbuckets
[ (((int) bb_src
*8) ^ (int) bb_dst
) % BB_BUCKETS
];
2202 bucket
= *startbucket
;
2204 for (bucket
= *startbucket
; bucket
;
2205 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2207 if (bucket
->src_addr
== bb_src
2208 && bucket
->dst_addr
== bb_dst
)
2211 *oldnext
= bucket
->next
;
2212 bucket
->next
= *startbucket
;
2213 *startbucket
= bucket
;
2218 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2224 fprintf (stderr
, "Profiler: out of memory\n");
2231 bucket
->src_addr
= bb_src
;
2232 bucket
->dst_addr
= bb_dst
;
2233 bucket
->next
= *startbucket
;
2234 *startbucket
= bucket
;
2245 MACHINE_STATE_RESTORE("1")
2249 /* Called when returning from a function and `__bb_showret__' is set. */
2252 __bb_trace_func_ret ()
2254 struct bb_edge
*bucket
;
2256 if (!bb_callcount
|| (__bb
.blocks
->flags
&& (__bb
.blocks
->flags
[__bb
.blockno
] & TRACE_OFF
)))
2261 struct bb_edge
**startbucket
, **oldnext
;
2263 oldnext
= startbucket
2264 = & bb_hashbuckets
[ (((int) bb_dst
* 8) ^ (int) bb_src
) % BB_BUCKETS
];
2265 bucket
= *startbucket
;
2267 for (bucket
= *startbucket
; bucket
;
2268 oldnext
= &(bucket
->next
), bucket
= *oldnext
)
2270 if (bucket
->src_addr
== bb_dst
2271 && bucket
->dst_addr
== bb_src
)
2274 *oldnext
= bucket
->next
;
2275 bucket
->next
= *startbucket
;
2276 *startbucket
= bucket
;
2281 bucket
= (struct bb_edge
*) malloc (sizeof (struct bb_edge
));
2287 fprintf (stderr
, "Profiler: out of memory\n");
2294 bucket
->src_addr
= bb_dst
;
2295 bucket
->dst_addr
= bb_src
;
2296 bucket
->next
= *startbucket
;
2297 *startbucket
= bucket
;
2310 /* Called upon entering the first function of a file. */
2313 __bb_init_file (struct bb
*blocks
)
2316 const struct bb_func
*p
;
2317 long blk
, ncounts
= blocks
->ncounts
;
2318 const char **functions
= blocks
->functions
;
2320 /* Set up linked list. */
2321 blocks
->zero_word
= 1;
2322 blocks
->next
= bb_head
;
2327 || !(blocks
->flags
= (char *) malloc (sizeof (char) * blocks
->ncounts
)))
2330 for (blk
= 0; blk
< ncounts
; blk
++)
2331 blocks
->flags
[blk
] = 0;
2333 for (blk
= 0; blk
< ncounts
; blk
++)
2335 for (p
= bb_func_head
; p
; p
= p
->next
)
2337 if (!strcmp (p
->funcname
, functions
[blk
])
2338 && (!p
->filename
|| !strcmp (p
->filename
, blocks
->filename
)))
2340 blocks
->flags
[blk
] |= p
->mode
;
2347 /* Called when exiting from a function. */
2353 MACHINE_STATE_SAVE("2")
2357 if ((bb_mode
& 12) && bb_stacksize
> bb_callcount
)
2359 bb_src
= bb_stack
[bb_callcount
];
2361 __bb_trace_func_ret ();
2367 MACHINE_STATE_RESTORE("2")
2371 /* Called when entering a function. */
2374 __bb_init_trace_func (struct bb
*blocks
, unsigned long blockno
)
2376 static int trace_init
= 0;
2378 MACHINE_STATE_SAVE("3")
2380 if (!blocks
->zero_word
)
2387 __bb_init_file (blocks
);
2397 if (bb_callcount
>= bb_stacksize
)
2399 size_t newsize
= bb_callcount
+ 100;
2401 bb_stack
= (unsigned long *) realloc (bb_stack
, newsize
);
2406 fprintf (stderr
, "Profiler: out of memory\n");
2410 goto stack_overflow
;
2412 bb_stacksize
= newsize
;
2414 bb_stack
[bb_callcount
] = bb_src
;
2425 else if (blocks
->flags
&& (blocks
->flags
[blockno
] & TRACE_ON
))
2431 bb_stack
[bb_callcount
] = bb_src
;
2434 MACHINE_STATE_RESTORE("3")
2437 #endif /* not inhibit_libc */
2438 #endif /* not BLOCK_PROFILER_CODE */
2442 unsigned int __shtab
[] = {
2443 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2444 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2445 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2446 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2447 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2448 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2449 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2450 0x10000000, 0x20000000, 0x40000000, 0x80000000
2454 #ifdef L_clear_cache
2455 /* Clear part of an instruction cache. */
2457 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2460 __clear_cache (char *beg
, char *end
)
2462 #ifdef CLEAR_INSN_CACHE
2463 CLEAR_INSN_CACHE (beg
, end
);
2465 #ifdef INSN_CACHE_SIZE
2466 static char array
[INSN_CACHE_SIZE
+ INSN_CACHE_PLANE_SIZE
+ INSN_CACHE_LINE_WIDTH
];
2467 static int initialized
;
2471 typedef (*function_ptr
) ();
2473 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2474 /* It's cheaper to clear the whole cache.
2475 Put in a series of jump instructions so that calling the beginning
2476 of the cache will clear the whole thing. */
2480 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2481 & -INSN_CACHE_LINE_WIDTH
);
2482 int end_ptr
= ptr
+ INSN_CACHE_SIZE
;
2484 while (ptr
< end_ptr
)
2486 *(INSTRUCTION_TYPE
*)ptr
2487 = JUMP_AHEAD_INSTRUCTION
+ INSN_CACHE_LINE_WIDTH
;
2488 ptr
+= INSN_CACHE_LINE_WIDTH
;
2490 *(INSTRUCTION_TYPE
*) (ptr
- INSN_CACHE_LINE_WIDTH
) = RETURN_INSTRUCTION
;
2495 /* Call the beginning of the sequence. */
2496 (((function_ptr
) (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2497 & -INSN_CACHE_LINE_WIDTH
))
2500 #else /* Cache is large. */
2504 int ptr
= (((int) array
+ INSN_CACHE_LINE_WIDTH
- 1)
2505 & -INSN_CACHE_LINE_WIDTH
);
2507 while (ptr
< (int) array
+ sizeof array
)
2509 *(INSTRUCTION_TYPE
*)ptr
= RETURN_INSTRUCTION
;
2510 ptr
+= INSN_CACHE_LINE_WIDTH
;
2516 /* Find the location in array that occupies the same cache line as BEG. */
2518 offset
= ((int) beg
& -INSN_CACHE_LINE_WIDTH
) & (INSN_CACHE_PLANE_SIZE
- 1);
2519 start_addr
= (((int) (array
+ INSN_CACHE_PLANE_SIZE
- 1)
2520 & -INSN_CACHE_PLANE_SIZE
)
2523 /* Compute the cache alignment of the place to stop clearing. */
2524 #if 0 /* This is not needed for gcc's purposes. */
2525 /* If the block to clear is bigger than a cache plane,
2526 we clear the entire cache, and OFFSET is already correct. */
2527 if (end
< beg
+ INSN_CACHE_PLANE_SIZE
)
2529 offset
= (((int) (end
+ INSN_CACHE_LINE_WIDTH
- 1)
2530 & -INSN_CACHE_LINE_WIDTH
)
2531 & (INSN_CACHE_PLANE_SIZE
- 1));
2533 #if INSN_CACHE_DEPTH > 1
2534 end_addr
= (start_addr
& -INSN_CACHE_PLANE_SIZE
) + offset
;
2535 if (end_addr
<= start_addr
)
2536 end_addr
+= INSN_CACHE_PLANE_SIZE
;
2538 for (plane
= 0; plane
< INSN_CACHE_DEPTH
; plane
++)
2540 int addr
= start_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2541 int stop
= end_addr
+ plane
* INSN_CACHE_PLANE_SIZE
;
2543 while (addr
!= stop
)
2545 /* Call the return instruction at ADDR. */
2546 ((function_ptr
) addr
) ();
2548 addr
+= INSN_CACHE_LINE_WIDTH
;
2551 #else /* just one plane */
2554 /* Call the return instruction at START_ADDR. */
2555 ((function_ptr
) start_addr
) ();
2557 start_addr
+= INSN_CACHE_LINE_WIDTH
;
2559 while ((start_addr
% INSN_CACHE_SIZE
) != offset
);
2560 #endif /* just one plane */
2561 #endif /* Cache is large */
2562 #endif /* Cache exists */
2563 #endif /* CLEAR_INSN_CACHE */
2566 #endif /* L_clear_cache */
2570 /* Jump to a trampoline, loading the static chain address. */
2572 #if defined(WINNT) && ! defined(__CYGWIN32__)
2584 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2588 mprotect (char *addr
, int len
, int prot
)
2605 if (VirtualProtect (addr
, len
, np
, &op
))
2613 #ifdef TRANSFER_FROM_TRAMPOLINE
2614 TRANSFER_FROM_TRAMPOLINE
2617 #if defined (NeXT) && defined (__MACH__)
2619 /* Make stack executable so we can call trampolines on stack.
2620 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2624 #include <mach/mach.h>
2628 __enable_execute_stack (char *addr
)
2631 char *eaddr
= addr
+ TRAMPOLINE_SIZE
;
2632 vm_address_t a
= (vm_address_t
) addr
;
2634 /* turn on execute access on stack */
2635 r
= vm_protect (task_self (), a
, TRAMPOLINE_SIZE
, FALSE
, VM_PROT_ALL
);
2636 if (r
!= KERN_SUCCESS
)
2638 mach_error("vm_protect VM_PROT_ALL", r
);
2642 /* We inline the i-cache invalidation for speed */
2644 #ifdef CLEAR_INSN_CACHE
2645 CLEAR_INSN_CACHE (addr
, eaddr
);
2647 __clear_cache ((int) addr
, (int) eaddr
);
2651 #endif /* defined (NeXT) && defined (__MACH__) */
2655 /* Make stack executable so we can call trampolines on stack.
2656 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2658 #include <sys/mman.h>
2659 #include <sys/vmparam.h>
2660 #include <machine/machparam.h>
2663 __enable_execute_stack ()
2666 static unsigned lowest
= USRSTACK
;
2667 unsigned current
= (unsigned) &fp
& -NBPG
;
2669 if (lowest
> current
)
2671 unsigned len
= lowest
- current
;
2672 mremap (current
, &len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
2676 /* Clear instruction cache in case an old trampoline is in it. */
2679 #endif /* __convex__ */
2683 /* Modified from the convex -code above. */
2685 #include <sys/param.h>
2687 #include <sys/m88kbcs.h>
2690 __enable_execute_stack ()
2693 static unsigned long lowest
= USRSTACK
;
2694 unsigned long current
= (unsigned long) &save_errno
& -NBPC
;
2696 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2697 address is seen as 'negative'. That is the case with the stack. */
2700 if (lowest
> current
)
2702 unsigned len
=lowest
-current
;
2703 memctl(current
,len
,MCT_TEXT
);
2707 memctl(current
,NBPC
,MCT_TEXT
);
2711 #endif /* __sysV88__ */
2715 #include <sys/signal.h>
2718 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2719 so define it here, because we need it in __clear_insn_cache below */
2720 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2721 hence we enable this stuff only if MCT_TEXT is #define'd. */
2736 /* Clear instruction cache so we can call trampolines on stack.
2737 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2740 __clear_insn_cache ()
2745 /* Preserve errno, because users would be surprised to have
2746 errno changing without explicitly calling any system-call. */
2749 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2750 No need to use an address derived from _start or %sp, as 0 works also. */
2751 memctl(0, 4096, MCT_TEXT
);
2756 #endif /* __sysV68__ */
2760 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2762 #include <sys/mman.h>
2763 #include <sys/types.h>
2764 #include <sys/param.h>
2765 #include <sys/vmmac.h>
2767 /* Modified from the convex -code above.
2768 mremap promises to clear the i-cache. */
2771 __enable_execute_stack ()
2774 if (mprotect (((unsigned int)&fp
/PAGSIZ
)*PAGSIZ
, PAGSIZ
,
2775 PROT_READ
|PROT_WRITE
|PROT_EXEC
))
2777 perror ("mprotect in __enable_execute_stack");
2782 #endif /* __pyr__ */
2784 #if defined (sony_news) && defined (SYSTYPE_BSD)
2787 #include <sys/types.h>
2788 #include <sys/param.h>
2789 #include <syscall.h>
2790 #include <machine/sysnews.h>
2792 /* cacheflush function for NEWS-OS 4.2.
2793 This function is called from trampoline-initialize code
2794 defined in config/mips/mips.h. */
2797 cacheflush (char *beg
, int size
, int flag
)
2799 if (syscall (SYS_sysnews
, NEWS_CACHEFLUSH
, beg
, size
, FLUSH_BCACHE
))
2801 perror ("cache_flush");
2807 #endif /* sony_news */
2808 #endif /* L_trampoline */
2810 #ifndef __CYGWIN32__
2813 #include "gbl-ctors.h"
2814 /* Some systems use __main in a way incompatible with its use in gcc, in these
2815 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2816 give the same symbol without quotes for an alternative entry point. You
2817 must define both, or neither. */
2819 #define NAME__MAIN "__main"
2820 #define SYMBOL__MAIN __main
2823 #ifdef INIT_SECTION_ASM_OP
2824 #undef HAS_INIT_SECTION
2825 #define HAS_INIT_SECTION
2828 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2829 /* Run all the global destructors on exit from the program. */
2832 __do_global_dtors ()
2834 #ifdef DO_GLOBAL_DTORS_BODY
2835 DO_GLOBAL_DTORS_BODY
;
2837 static func_ptr
*p
= __DTOR_LIST__
+ 1;
2847 #ifndef HAS_INIT_SECTION
2848 /* Run all the global constructors on entry to the program. */
2851 #define ON_EXIT(a, b)
2853 /* Make sure the exit routine is pulled in to define the globals as
2854 bss symbols, just in case the linker does not automatically pull
2855 bss definitions from the library. */
2857 extern int _exit_dummy_decl
;
2858 int *_exit_dummy_ref
= &_exit_dummy_decl
;
2859 #endif /* ON_EXIT */
2862 __do_global_ctors ()
2864 DO_GLOBAL_CTORS_BODY
;
2865 ON_EXIT (__do_global_dtors
, 0);
2867 #endif /* no HAS_INIT_SECTION */
2869 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2870 /* Subroutine called automatically by `main'.
2871 Compiling a global function named `main'
2872 produces an automatic call to this function at the beginning.
2874 For many systems, this routine calls __do_global_ctors.
2875 For systems which support a .init section we use the .init section
2876 to run __do_global_ctors, so we need not do anything here. */
2881 /* Support recursive calls to `main': run initializers just once. */
2882 static int initialized
;
2886 __do_global_ctors ();
2889 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2891 #endif /* L__main */
2892 #endif /* __CYGWIN32__ */
2896 #include "gbl-ctors.h"
2898 /* Provide default definitions for the lists of constructors and
2899 destructors, so that we don't get linker errors. These symbols are
2900 intentionally bss symbols, so that gld and/or collect will provide
2901 the right values. */
2903 /* We declare the lists here with two elements each,
2904 so that they are valid empty lists if no other definition is loaded. */
2905 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2906 #if defined(__NeXT__) || defined(_AIX)
2907 /* After 2.3, try this definition on all systems. */
2908 func_ptr __CTOR_LIST__
[2] = {0, 0};
2909 func_ptr __DTOR_LIST__
[2] = {0, 0};
2911 func_ptr __CTOR_LIST__
[2];
2912 func_ptr __DTOR_LIST__
[2];
2914 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2915 #endif /* L_ctors */
2919 #include "gbl-ctors.h"
2925 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
2933 static func_ptr
*atexit_chain
= 0;
2934 static long atexit_chain_length
= 0;
2935 static volatile long last_atexit_chain_slot
= -1;
2937 int atexit (func_ptr func
)
2939 if (++last_atexit_chain_slot
== atexit_chain_length
)
2941 atexit_chain_length
+= 32;
2943 atexit_chain
= (func_ptr
*) realloc (atexit_chain
, atexit_chain_length
2944 * sizeof (func_ptr
));
2946 atexit_chain
= (func_ptr
*) malloc (atexit_chain_length
2947 * sizeof (func_ptr
));
2950 atexit_chain_length
= 0;
2951 last_atexit_chain_slot
= -1;
2956 atexit_chain
[last_atexit_chain_slot
] = func
;
2959 #endif /* NEED_ATEXIT */
2961 /* If we have no known way of registering our own __do_global_dtors
2962 routine so that it will be invoked at program exit time, then we
2963 have to define our own exit routine which will get this to happen. */
2965 extern void __do_global_dtors ();
2966 extern void __bb_exit_func ();
2967 extern void _cleanup ();
2968 extern void _exit () __attribute__ ((noreturn
));
2973 #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
2977 for ( ; last_atexit_chain_slot
-- >= 0; )
2979 (*atexit_chain
[last_atexit_chain_slot
+ 1]) ();
2980 atexit_chain
[last_atexit_chain_slot
+ 1] = 0;
2982 free (atexit_chain
);
2985 #else /* No NEED_ATEXIT */
2986 __do_global_dtors ();
2987 #endif /* No NEED_ATEXIT */
2988 #endif /* !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF) */
2989 /* In gbl-ctors.h, ON_EXIT is defined if HAVE_ATEXIT is defined. In
2990 __bb_init_func and _bb_init_prg, __bb_exit_func is registered with
2991 ON_EXIT if ON_EXIT is defined. Thus we must not call __bb_exit_func here
2992 if HAVE_ATEXIT is defined. */
2994 #ifndef inhibit_libc
2997 #endif /* !HAVE_ATEXIT */
3006 #else /* ON_EXIT defined */
3007 int _exit_dummy_decl
= 0; /* prevent compiler & linker warnings */
3009 # ifndef HAVE_ATEXIT
3010 /* Provide a fake for atexit() using ON_EXIT. */
3011 int atexit (func_ptr func
)
3013 return ON_EXIT (func
, NULL
);
3015 # endif /* HAVE_ATEXIT */
3016 #endif /* ON_EXIT defined */
3024 /* Shared exception handling support routines. */
3027 __default_terminate ()
3032 void (*__terminate_func
)() = __default_terminate
;
3037 (*__terminate_func
)();
3041 __throw_type_match (void *catch_type
, void *throw_type
, void *obj
)
3044 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3045 catch_type
, throw_type
);
3047 if (strcmp ((const char *)catch_type
, (const char *)throw_type
) == 0)
3058 /* Include definitions of EH context and table layout */
3060 #include "eh-common.h"
3061 #ifndef inhibit_libc
3065 /* This is a safeguard for dynamic handler chain. */
3067 static void *top_elt
[2];
3069 /* Allocate and return a new EH context structure. */
3071 extern void __throw ();
3076 struct eh_context
*eh
= (struct eh_context
*) malloc (sizeof *eh
);
3080 memset (eh
, 0, sizeof *eh
);
3082 eh
->dynamic_handler_chain
= top_elt
;
3088 static __gthread_key_t eh_context_key
;
3090 /* Destructor for struct eh_context. */
3092 eh_context_free (void *ptr
)
3094 __gthread_key_dtor (eh_context_key
, ptr
);
3100 /* Pointer to function to return EH context. */
3102 static struct eh_context
*eh_context_initialize ();
3103 static struct eh_context
*eh_context_static ();
3105 static struct eh_context
*eh_context_specific ();
3108 static struct eh_context
*(*get_eh_context
) () = &eh_context_initialize
;
3110 /* Routine to get EH context.
3111 This one will simply call the function pointer. */
3116 return (void *) (*get_eh_context
) ();
3119 /* Get and set the language specific info pointer. */
3124 struct eh_context
*eh
= (*get_eh_context
) ();
3130 eh_threads_initialize ()
3132 /* Try to create the key. If it fails, revert to static method,
3133 otherwise start using thread specific EH contexts. */
3134 if (__gthread_key_create (&eh_context_key
, &eh_context_free
) == 0)
3135 get_eh_context
= &eh_context_specific
;
3137 get_eh_context
= &eh_context_static
;
3139 #endif /* no __GTHREADS */
3141 /* Initialize EH context.
3142 This will be called only once, since we change GET_EH_CONTEXT
3143 pointer to another routine. */
3145 static struct eh_context
*
3146 eh_context_initialize ()
3150 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
3151 /* Make sure that get_eh_context does not point to us anymore.
3152 Some systems have dummy thread routines in their libc that
3153 return a success (Solaris 2.6 for example). */
3154 if (__gthread_once (&once
, eh_threads_initialize
) != 0
3155 || get_eh_context
== &eh_context_initialize
)
3157 /* Use static version of EH context. */
3158 get_eh_context
= &eh_context_static
;
3161 #else /* no __GTHREADS */
3163 /* Use static version of EH context. */
3164 get_eh_context
= &eh_context_static
;
3166 #endif /* no __GTHREADS */
3168 return (*get_eh_context
) ();
3171 /* Return a static EH context. */
3173 static struct eh_context
*
3174 eh_context_static ()
3176 static struct eh_context
*eh
;
3178 eh
= new_eh_context ();
3183 /* Return a thread specific EH context. */
3185 static struct eh_context
*
3186 eh_context_specific ()
3188 struct eh_context
*eh
;
3189 eh
= (struct eh_context
*) __gthread_getspecific (eh_context_key
);
3192 eh
= new_eh_context ();
3193 if (__gthread_setspecific (eh_context_key
, (void *) eh
) != 0)
3201 /* Support routines for setjmp/longjmp exception handling. */
3203 /* Calls to __sjthrow are generated by the compiler when an exception
3204 is raised when using the setjmp/longjmp exception handling codegen
3207 #ifdef DONT_USE_BUILTIN_SETJMP
3208 extern void longjmp (void *, int);
3211 /* Routine to get the head of the current thread's dynamic handler chain
3212 use for exception handling. */
3215 __get_dynamic_handler_chain ()
3217 struct eh_context
*eh
= (*get_eh_context
) ();
3218 return &eh
->dynamic_handler_chain
;
3221 /* This is used to throw an exception when the setjmp/longjmp codegen
3222 method is used for exception handling.
3224 We call __terminate if there are no handlers left. Otherwise we run the
3225 cleanup actions off the dynamic cleanup stack, and pop the top of the
3226 dynamic handler chain, and use longjmp to transfer back to the associated
3232 struct eh_context
*eh
= (*get_eh_context
) ();
3233 void ***dhc
= &eh
->dynamic_handler_chain
;
3235 void (*func
)(void *, int);
3239 /* The cleanup chain is one word into the buffer. Get the cleanup
3241 cleanup
= (void***)&(*dhc
)[1];
3243 /* If there are any cleanups in the chain, run them now. */
3247 void **buf
= (void**)store
;
3252 #ifdef DONT_USE_BUILTIN_SETJMP
3253 if (! setjmp (&buf
[2]))
3255 if (! __builtin_setjmp (&buf
[2]))
3261 func
= (void(*)(void*, int))cleanup
[0][1];
3262 arg
= (void*)cleanup
[0][2];
3264 /* Update this before running the cleanup. */
3265 cleanup
[0] = (void **)cleanup
[0][0];
3278 /* We must call terminate if we try and rethrow an exception, when
3279 there is no exception currently active and when there are no
3281 if (! eh
->info
|| (*dhc
) == top_elt
)
3284 /* Find the jmpbuf associated with the top element of the dynamic
3285 handler chain. The jumpbuf starts two words into the buffer. */
3286 jmpbuf
= &(*dhc
)[2];
3288 /* Then we pop the top element off the dynamic handler chain. */
3289 *dhc
= (void**)(*dhc
)[0];
3291 /* And then we jump to the handler. */
3293 #ifdef DONT_USE_BUILTIN_SETJMP
3294 longjmp (jmpbuf
, 1);
3296 __builtin_longjmp (jmpbuf
, 1);
3300 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3301 handler, then pop the handler off the dynamic handler stack, and
3302 then throw. This is used to skip the first handler, and transfer
3303 control to the next handler in the dynamic handler stack. */
3308 struct eh_context
*eh
= (*get_eh_context
) ();
3309 void ***dhc
= &eh
->dynamic_handler_chain
;
3310 void (*func
)(void *, int);
3314 /* The cleanup chain is one word into the buffer. Get the cleanup
3316 cleanup
= (void***)&(*dhc
)[1];
3318 /* If there are any cleanups in the chain, run them now. */
3322 void **buf
= (void**)store
;
3327 #ifdef DONT_USE_BUILTIN_SETJMP
3328 if (! setjmp (&buf
[2]))
3330 if (! __builtin_setjmp (&buf
[2]))
3336 func
= (void(*)(void*, int))cleanup
[0][1];
3337 arg
= (void*)cleanup
[0][2];
3339 /* Update this before running the cleanup. */
3340 cleanup
[0] = (void **)cleanup
[0][0];
3353 /* Then we pop the top element off the dynamic handler chain. */
3354 *dhc
= (void**)(*dhc
)[0];
3359 /* Support code for all exception region-based exception handling. */
3362 __eh_rtime_match (void *rtime
)
3365 __eh_matcher matcher
;
3368 info
= *(__get_eh_info ());
3369 matcher
= ((__eh_info
*)info
)->match_function
;
3372 #ifndef inhibit_libc
3373 fprintf (stderr
, "Internal Compiler Bug: No runtime type matcher.");
3377 ret
= (*matcher
) (info
, rtime
, (void *)0);
3378 return (ret
!= NULL
);
3381 /* This value identifies the place from which an exception is being
3384 #ifdef EH_TABLE_LOOKUP
3390 #ifdef DWARF2_UNWIND_INFO
3393 /* Return the table version of an exception descriptor */
3396 __get_eh_table_version (exception_descriptor
*table
)
3398 return table
->lang
.version
;
3401 /* Return the originating table language of an exception descriptor */
3404 __get_eh_table_language (exception_descriptor
*table
)
3406 return table
->lang
.language
;
3409 /* This routine takes a PC and a pointer to the exception region TABLE for
3410 its translation unit, and returns the address of the exception handler
3411 associated with the closest exception table handler entry associated
3412 with that PC, or 0 if there are no table entries the PC fits in.
3414 In the advent of a tie, we have to give the last entry, as it represents
3418 old_find_exception_handler (void *pc
, old_exception_table
*table
)
3425 /* We can't do a binary search because the table isn't guaranteed
3426 to be sorted from function to function. */
3427 for (pos
= 0; table
[pos
].start_region
!= (void *) -1; ++pos
)
3429 if (table
[pos
].start_region
<= pc
&& table
[pos
].end_region
> pc
)
3431 /* This can apply. Make sure it is at least as small as
3432 the previous best. */
3433 if (best
== -1 || (table
[pos
].end_region
<= table
[best
].end_region
3434 && table
[pos
].start_region
>= table
[best
].start_region
))
3437 /* But it is sorted by starting PC within a function. */
3438 else if (best
>= 0 && table
[pos
].start_region
> pc
)
3442 return table
[best
].exception_handler
;
3449 find_exception_handler (void *pc
, exception_descriptor
*table
, void *eh_info
)
3453 /* The new model assumed the table is sorted inner-most out so the
3454 first region we find which matches is the correct one */
3458 exception_table
*tab
= &(table
->table
[0]);
3460 /* Subtract 1 from the PC to avoid hitting the next region */
3463 /* We can't do a binary search because the table is in inner-most
3464 to outermost address ranges within functions */
3465 for (pos
= 0; tab
[pos
].start_region
!= (void *) -1; pos
++)
3467 if (tab
[pos
].start_region
<= pc
&& tab
[pos
].end_region
> pc
)
3469 if (tab
[pos
].match_info
)
3471 __eh_matcher matcher
= ((__eh_info
*)eh_info
)->match_function
;
3472 /* match info but no matcher is NOT a match */
3475 ret
= (*matcher
)(eh_info
, tab
[pos
].match_info
, table
);
3477 return tab
[pos
].exception_handler
;
3481 return tab
[pos
].exception_handler
;
3488 #endif /* DWARF2_UNWIND_INFO */
3489 #endif /* EH_TABLE_LOOKUP */
3491 #ifdef DWARF2_UNWIND_INFO
3492 /* Support code for exception handling using static unwind information. */
3496 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3497 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3498 avoid a warning about casting between int and pointer of different
3501 typedef int ptr_type
__attribute__ ((mode (pointer
)));
3503 #ifdef INCOMING_REGNO
3504 /* Is the saved value for register REG in frame UDATA stored in a register
3505 window in the previous frame? */
3507 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3508 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3509 compiled functions won't work with the frame-unwind stuff here.
3510 Perhaps the entireity of in_reg_window should be conditional on having
3511 seen a DW_CFA_GNU_window_save? */
3512 #define target_flags 0
3515 in_reg_window (int reg
, frame_state
*udata
)
3517 if (udata
->saved
[reg
] == REG_SAVED_REG
)
3518 return INCOMING_REGNO (reg
) == reg
;
3519 if (udata
->saved
[reg
] != REG_SAVED_OFFSET
)
3522 #ifdef STACK_GROWS_DOWNWARD
3523 return udata
->reg_or_offset
[reg
] > 0;
3525 return udata
->reg_or_offset
[reg
] < 0;
3529 static inline int in_reg_window (int reg
, frame_state
*udata
) { return 0; }
3530 #endif /* INCOMING_REGNO */
3532 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3533 frame called by UDATA or 0. */
3536 get_reg_addr (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3538 while (udata
->saved
[reg
] == REG_SAVED_REG
)
3540 reg
= udata
->reg_or_offset
[reg
];
3541 if (in_reg_window (reg
, udata
))
3547 if (udata
->saved
[reg
] == REG_SAVED_OFFSET
)
3548 return (word_type
*)(udata
->cfa
+ udata
->reg_or_offset
[reg
]);
3553 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3554 frame called by UDATA or 0. */
3556 static inline void *
3557 get_reg (unsigned reg
, frame_state
*udata
, frame_state
*sub_udata
)
3559 return (void *)(ptr_type
) *get_reg_addr (reg
, udata
, sub_udata
);
3562 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3565 put_reg (unsigned reg
, void *val
, frame_state
*udata
)
3567 *get_reg_addr (reg
, udata
, NULL
) = (word_type
)(ptr_type
) val
;
3570 /* Copy the saved value for register REG from frame UDATA to frame
3571 TARGET_UDATA. Unlike the previous two functions, this can handle
3572 registers that are not one word large. */
3575 copy_reg (unsigned reg
, frame_state
*udata
, frame_state
*target_udata
)
3577 word_type
*preg
= get_reg_addr (reg
, udata
, NULL
);
3578 word_type
*ptreg
= get_reg_addr (reg
, target_udata
, NULL
);
3580 memcpy (ptreg
, preg
, __builtin_dwarf_reg_size (reg
));
3583 /* Retrieve the return address for frame UDATA. */
3585 static inline void *
3586 get_return_addr (frame_state
*udata
, frame_state
*sub_udata
)
3588 return __builtin_extract_return_addr
3589 (get_reg (udata
->retaddr_column
, udata
, sub_udata
));
3592 /* Overwrite the return address for frame UDATA with VAL. */
3595 put_return_addr (void *val
, frame_state
*udata
)
3597 val
= __builtin_frob_return_addr (val
);
3598 put_reg (udata
->retaddr_column
, val
, udata
);
3601 /* Given the current frame UDATA and its return address PC, return the
3602 information about the calling frame in CALLER_UDATA. */
3605 next_stack_level (void *pc
, frame_state
*udata
, frame_state
*caller_udata
)
3607 caller_udata
= __frame_state_for (pc
, caller_udata
);
3611 /* Now go back to our caller's stack frame. If our caller's CFA register
3612 was saved in our stack frame, restore it; otherwise, assume the CFA
3613 register is SP and restore it to our CFA value. */
3614 if (udata
->saved
[caller_udata
->cfa_reg
])
3615 caller_udata
->cfa
= get_reg (caller_udata
->cfa_reg
, udata
, 0);
3617 caller_udata
->cfa
= udata
->cfa
;
3618 caller_udata
->cfa
+= caller_udata
->cfa_offset
;
3620 return caller_udata
;
3623 /* We first search for an exception handler, and if we don't find
3624 it, we call __terminate on the current stack frame so that we may
3625 use the debugger to walk the stack and understand why no handler
3628 If we find one, then we unwind the frames down to the one that
3629 has the handler and transfer control into the handler. */
3634 struct eh_context
*eh
= (*get_eh_context
) ();
3635 void *saved_pc
, *pc
, *handler
;
3636 frame_state ustruct
, ustruct2
;
3637 frame_state
*udata
= &ustruct
;
3638 frame_state
*sub_udata
= &ustruct2
;
3639 frame_state my_ustruct
, *my_udata
= &my_ustruct
;
3641 int new_exception_model
;
3643 /* This is required for C++ semantics. We must call terminate if we
3644 try and rethrow an exception, when there is no exception currently
3649 /* Start at our stack frame. */
3651 udata
= __frame_state_for (&&label
, udata
);
3655 /* We need to get the value from the CFA register. */
3656 udata
->cfa
= __builtin_dwarf_cfa ();
3658 memcpy (my_udata
, udata
, sizeof (*udata
));
3660 /* Do any necessary initialization to access arbitrary stack frames.
3661 On the SPARC, this means flushing the register windows. */
3662 __builtin_unwind_init ();
3664 /* Now reset pc to the right throw point. */
3665 pc
= __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3671 frame_state
*p
= udata
;
3672 udata
= next_stack_level (pc
, udata
, sub_udata
);
3675 /* If we couldn't find the next frame, we lose. */
3679 if (udata
->eh_ptr
== NULL
)
3680 new_exception_model
= 0;
3682 new_exception_model
= (((exception_descriptor
*)(udata
->eh_ptr
))->
3683 runtime_id_field
== NEW_EH_RUNTIME
);
3685 if (new_exception_model
)
3686 handler
= find_exception_handler (pc
, udata
->eh_ptr
, eh
->info
);
3688 handler
= old_find_exception_handler (pc
, udata
->eh_ptr
);
3690 /* If we found one, we can stop searching. */
3693 args_size
= udata
->args_size
;
3697 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3698 hitting the beginning of the next region. */
3699 pc
= get_return_addr (udata
, sub_udata
) - 1;
3702 /* If we haven't found a handler by now, this is an unhandled
3707 eh
->handler_label
= handler
;
3710 /* We found a handler in the throw context, no need to unwind. */
3716 /* Unwind all the frames between this one and the handler by copying
3717 their saved register values into our register save slots. */
3719 /* Remember the PC where we found the handler. */
3720 void *handler_pc
= pc
;
3722 /* Start from the throw context again. */
3724 memcpy (udata
, my_udata
, sizeof (*udata
));
3726 while (pc
!= handler_pc
)
3728 frame_state
*p
= udata
;
3729 udata
= next_stack_level (pc
, udata
, sub_udata
);
3732 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
3733 if (i
!= udata
->retaddr_column
&& udata
->saved
[i
])
3735 /* If you modify the saved value of the return address
3736 register on the SPARC, you modify the return address for
3737 your caller's frame. Don't do that here, as it will
3738 confuse get_return_addr. */
3739 if (in_reg_window (i
, udata
)
3740 && udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
3741 && udata
->reg_or_offset
[udata
->retaddr_column
] == i
)
3743 copy_reg (i
, udata
, my_udata
);
3746 pc
= get_return_addr (udata
, sub_udata
) - 1;
3749 /* But we do need to update the saved return address register from
3750 the last frame we unwind, or the handler frame will have the wrong
3752 if (udata
->saved
[udata
->retaddr_column
] == REG_SAVED_REG
)
3754 i
= udata
->reg_or_offset
[udata
->retaddr_column
];
3755 if (in_reg_window (i
, udata
))
3756 copy_reg (i
, udata
, my_udata
);
3762 __builtin_eh_return ((void *)eh
,
3763 #ifdef STACK_GROWS_DOWNWARD
3764 udata
->cfa
- my_udata
->cfa
,
3766 my_udata
->cfa
- udata
->cfa
,
3770 /* Epilogue: restore the handler frame's register values and return
3773 #endif /* DWARF2_UNWIND_INFO */
3778 #ifndef inhibit_libc
3779 /* This gets us __GNU_LIBRARY__. */
3780 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
3783 #ifdef __GNU_LIBRARY__
3784 /* Avoid forcing the library's meaning of `write' on the user program
3785 by using the "internal" name (for use within the library) */
3786 #define write(fd, buf, n) __write((fd), (buf), (n))
3788 #endif /* inhibit_libc */
3790 #define MESSAGE "pure virtual method called\n"
3795 #ifndef inhibit_libc
3796 write (2, MESSAGE
, sizeof (MESSAGE
) - 1);