1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 92, 93, 94, 95, 96, 97, 98, 1999, 2000
4 Free Software Foundation, Inc.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 /* It is incorrect to include config.h here, because this file is being
31 compiled for the target, and hence definitions concerning only the host
40 /* Don't use `fancy_abort' here even if config.h says to use it. */
45 /* In a cross-compilation situation, default to inhibiting compilation
46 of routines that use libc. */
48 #if defined(CROSS_COMPILE) && !defined(inhibit_libc)
52 /* Permit the tm.h file to select the endianness to use just for this
53 file. This is used when the endianness is determined when the
56 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
57 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
60 #ifndef LIBGCC2_LONG_DOUBLE_TYPE_SIZE
61 #define LIBGCC2_LONG_DOUBLE_TYPE_SIZE LONG_DOUBLE_TYPE_SIZE
64 /* In the first part of this file, we are interfacing to calls generated
65 by the compiler itself. These calls pass values into these routines
66 which have very specific modes (rather than very specific types), and
67 these compiler-generated calls also expect any return values to have
68 very specific modes (rather than very specific types). Thus, we need
69 to avoid using regular C language type names in this part of the file
70 because the sizes for those types can be configured to be anything.
71 Instead we use the following special type names. */
73 typedef int QItype __attribute__ ((mode (QI)));
74 typedef unsigned int UQItype __attribute__ ((mode (QI)));
75 typedef int HItype __attribute__ ((mode (HI)));
76 typedef unsigned int UHItype __attribute__ ((mode (HI)));
77 #if UNITS_PER_WORD > 1
78 /* These typedefs are usually forbidden on dsp's with UNITS_PER_WORD 1 */
79 typedef int SItype __attribute__ ((mode (SI)));
80 typedef unsigned int USItype __attribute__ ((mode (SI)));
81 #if UNITS_PER_WORD > 2
82 /* These typedefs are usually forbidden on archs with UNITS_PER_WORD 2 */
83 typedef int DItype __attribute__ ((mode (DI)));
84 typedef unsigned int UDItype __attribute__ ((mode (DI)));
88 #if BITS_PER_UNIT == 8
90 typedef float SFtype __attribute__ ((mode (SF)));
91 typedef float DFtype __attribute__ ((mode (DF)));
93 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
94 typedef float XFtype __attribute__ ((mode (XF)));
96 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
97 typedef float TFtype __attribute__ ((mode (TF)));
100 #else /* BITS_PER_UNIT != 8 */
102 /* On dsp's there are usually qf/hf/tqf modes used instead of the above.
103 For now we don't support them in libgcc2.c. */
121 #endif /* BITS_PER_UNIT != 8 */
123 typedef int word_type __attribute__ ((mode (__word__)));
125 /* Make sure that we don't accidentally use any normal C language built-in
126 type names in the first part of this file. Instead we want to use *only*
127 the type names defined above. The following macro definitions insure
128 that if we *do* accidentally use some normal C language built-in type name,
129 we will get a syntax error. */
131 #define char bogus_type
132 #define short bogus_type
133 #define int bogus_type
134 #define long bogus_type
135 #define unsigned bogus_type
136 #define float bogus_type
137 #define double bogus_type
139 #if UNITS_PER_WORD > 2
140 #define W_TYPE_SIZE (4 * BITS_PER_UNIT)
142 #define UWtype USItype
143 #define HWtype SItype
144 #define UHWtype USItype
145 #define DWtype DItype
146 #define UDWtype UDItype
147 #define __NW(a,b) __ ## a ## si ## b
148 #define __NDW(a,b) __ ## a ## di ## b
149 #elif UNITS_PER_WORD > 1
150 #define W_TYPE_SIZE (2 * BITS_PER_UNIT)
152 #define UWtype UHItype
153 #define HWtype HItype
154 #define UHWtype UHItype
155 #define DWtype SItype
156 #define UDWtype USItype
157 #define __NW(a,b) __ ## a ## hi ## b
158 #define __NDW(a,b) __ ## a ## si ## b
160 #define W_TYPE_SIZE BITS_PER_UNIT
162 #define UWtype UQItype
163 #define HWtype QItype
164 #define UHWtype UQItype
165 #define DWtype HItype
166 #define UDWtype UHItype
167 #define __NW(a,b) __ ## a ## qi ## b
168 #define __NDW(a,b) __ ## a ## hi ## b
171 #define __muldi3 __NDW(mul,3)
172 #define __divdi3 __NDW(div,3)
173 #define __udivdi3 __NDW(udiv,3)
174 #define __moddi3 __NDW(mod,3)
175 #define __umoddi3 __NDW(umod,3)
176 #define __negdi2 __NDW(neg,2)
177 #define __lshrdi3 __NDW(lshr,3)
178 #define __ashldi3 __NDW(ashl,3)
179 #define __ashrdi3 __NDW(ashr,3)
180 #define __ffsdi2 __NDW(ffs,2)
181 #define __cmpdi2 __NDW(cmp,2)
182 #define __ucmpdi2 __NDW(ucmp,2)
183 #define __udivmoddi4 __NDW(udivmod,4)
184 #define __fixunstfdi __NDW(fixunstf,)
185 #define __fixtfdi __NDW(fixtf,)
186 #define __fixunsxfdi __NDW(fixunsxf,)
187 #define __fixxfdi __NDW(fixxf,)
188 #define __fixunsdfdi __NDW(fixunsdf,)
189 #define __fixdfdi __NDW(fixdf,)
190 #define __fixunssfdi __NDW(fixunssf,)
191 #define __fixsfdi __NDW(fixsf,)
192 #define __floatdixf __NDW(float,xf)
193 #define __floatditf __NDW(float,tf)
194 #define __floatdidf __NDW(float,df)
195 #define __floatdisf __NDW(float,sf)
196 #define __fixunsxfsi __NW(fixunsxf,)
197 #define __fixunstfsi __NW(fixunstf,)
198 #define __fixunsdfsi __NW(fixunsdf,)
199 #define __fixunssfsi __NW(fixunssf,)
201 /* DWstructs are pairs of Wtype values in the order determined by
202 LIBGCC2_WORDS_BIG_ENDIAN. */
204 #if LIBGCC2_WORDS_BIG_ENDIAN
205 struct DWstruct {Wtype high, low;};
207 struct DWstruct {Wtype low, high;};
210 /* We need this union to unpack/pack DImode values, since we don't have
211 any arithmetic yet. Incoming DImode parameters are stored into the
212 `ll' field, and the unpacked result is read from the struct `s'. */
220 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
221 || defined (L_divdi3) || defined (L_udivdi3) \
222 || defined (L_moddi3) || defined (L_umoddi3))
224 #include "longlong.h"
226 #endif /* udiv or mul */
229 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
230 #if defined (L_divdi3) || defined (L_moddi3)
242 w.s.high = -uu.s.high - ((UWtype) w.s.low > 0);
248 /* Unless shift functions are defined whith full ANSI prototypes,
249 parameter b will be promoted to int if word_type is smaller than an int. */
252 __lshrdi3 (DWtype u, word_type b)
263 bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
267 w.s.low = (UWtype)uu.s.high >> -bm;
271 UWtype carries = (UWtype)uu.s.high << bm;
272 w.s.high = (UWtype)uu.s.high >> b;
273 w.s.low = ((UWtype)uu.s.low >> b) | carries;
282 __ashldi3 (DWtype u, word_type b)
293 bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
297 w.s.high = (UWtype)uu.s.low << -bm;
301 UWtype carries = (UWtype)uu.s.low >> bm;
302 w.s.low = (UWtype)uu.s.low << b;
303 w.s.high = ((UWtype)uu.s.high << b) | carries;
312 __ashrdi3 (DWtype u, word_type b)
323 bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
326 /* w.s.high = 1..1 or 0..0 */
327 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
328 w.s.low = uu.s.high >> -bm;
332 UWtype carries = (UWtype)uu.s.high << bm;
333 w.s.high = uu.s.high >> b;
334 w.s.low = ((UWtype)uu.s.low >> b) | carries;
348 w.s.low = ffs (uu.s.low);
351 w.s.low = ffs (uu.s.high);
354 w.s.low += BITS_PER_UNIT * sizeof (Wtype);
363 __muldi3 (DWtype u, DWtype v)
371 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
372 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
373 + (UWtype) uu.s.high * (UWtype) vv.s.low);
380 #if defined (sdiv_qrnnd)
382 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
389 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
391 /* dividend, divisor, and quotient are nonnegative */
392 sdiv_qrnnd (q, r, a1, a0, d);
396 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
397 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
398 /* Divide (c1*2^32 + c0) by d */
399 sdiv_qrnnd (q, r, c1, c0, d);
400 /* Add 2^31 to quotient */
401 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
406 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
407 c1 = a1 >> 1; /* A/2 */
408 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
410 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
412 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
414 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
431 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
434 c0 = ~c0; /* logical NOT */
436 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
438 q = ~q; /* (A/2)/b1 */
441 r = 2*r + (a0 & 1); /* A/(2*b1) */
459 else /* Implies c1 = b1 */
460 { /* Hence a1 = d - 1 = 2*b1 - 1 */
478 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
480 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
481 UWtype a1 __attribute__ ((__unused__)),
482 UWtype a0 __attribute__ ((__unused__)),
483 UWtype d __attribute__ ((__unused__)))
490 #if (defined (L_udivdi3) || defined (L_divdi3) || \
491 defined (L_umoddi3) || defined (L_moddi3))
496 static const UQItype __clz_tab[] =
498 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,
499 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,
500 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,
501 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,
502 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,
503 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,
504 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,
505 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,
508 #if (defined (L_udivdi3) || defined (L_divdi3) || \
509 defined (L_umoddi3) || defined (L_moddi3))
513 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
518 UWtype d0, d1, n0, n1, n2;
530 #if !UDIV_NEEDS_NORMALIZATION
537 udiv_qrnnd (q0, n0, n1, n0, d0);
540 /* Remainder in n0. */
547 d0 = 1 / d0; /* Divide intentionally by zero. */
549 udiv_qrnnd (q1, n1, 0, n1, d0);
550 udiv_qrnnd (q0, n0, n1, n0, d0);
552 /* Remainder in n0. */
563 #else /* UDIV_NEEDS_NORMALIZATION */
571 count_leading_zeros (bm, d0);
575 /* Normalize, i.e. make the most significant bit of the
579 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
583 udiv_qrnnd (q0, n0, n1, n0, d0);
586 /* Remainder in n0 >> bm. */
593 d0 = 1 / d0; /* Divide intentionally by zero. */
595 count_leading_zeros (bm, d0);
599 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
600 conclude (the most significant bit of n1 is set) /\ (the
601 leading quotient digit q1 = 1).
603 This special case is necessary, not an optimization.
604 (Shifts counts of W_TYPE_SIZE are undefined.) */
613 b = W_TYPE_SIZE - bm;
617 n1 = (n1 << bm) | (n0 >> b);
620 udiv_qrnnd (q1, n1, n2, n1, d0);
625 udiv_qrnnd (q0, n0, n1, n0, d0);
627 /* Remainder in n0 >> bm. */
637 #endif /* UDIV_NEEDS_NORMALIZATION */
648 /* Remainder in n1n0. */
660 count_leading_zeros (bm, d1);
663 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
664 conclude (the most significant bit of n1 is set) /\ (the
665 quotient digit q0 = 0 or 1).
667 This special case is necessary, not an optimization. */
669 /* The condition on the next line takes advantage of that
670 n1 >= d1 (true due to program flow). */
671 if (n1 > d1 || n0 >= d0)
674 sub_ddmmss (n1, n0, n1, n0, d1, d0);
693 b = W_TYPE_SIZE - bm;
695 d1 = (d1 << bm) | (d0 >> b);
698 n1 = (n1 << bm) | (n0 >> b);
701 udiv_qrnnd (q0, n1, n2, n1, d1);
702 umul_ppmm (m1, m0, q0, d0);
704 if (m1 > n1 || (m1 == n1 && m0 > n0))
707 sub_ddmmss (m1, m0, m1, m0, d1, d0);
712 /* Remainder in (n1n0 - m1m0) >> bm. */
715 sub_ddmmss (n1, n0, n1, n0, m1, m0);
716 rr.s.low = (n1 << b) | (n0 >> bm);
717 rr.s.high = n1 >> bm;
732 __divdi3 (DWtype u, DWtype v)
743 uu.ll = __negdi2 (uu.ll);
746 vv.ll = __negdi2 (vv.ll);
748 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
758 __moddi3 (DWtype u, DWtype v)
769 uu.ll = __negdi2 (uu.ll);
771 vv.ll = __negdi2 (vv.ll);
773 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
783 __umoddi3 (UDWtype u, UDWtype v)
787 (void) __udivmoddi4 (u, v, &w);
795 __udivdi3 (UDWtype n, UDWtype d)
797 return __udivmoddi4 (n, d, (UDWtype *) 0);
803 __cmpdi2 (DWtype a, DWtype b)
807 au.ll = a, bu.ll = b;
809 if (au.s.high < bu.s.high)
811 else if (au.s.high > bu.s.high)
813 if ((UWtype) au.s.low < (UWtype) bu.s.low)
815 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
823 __ucmpdi2 (DWtype a, DWtype b)
827 au.ll = a, bu.ll = b;
829 if ((UWtype) au.s.high < (UWtype) bu.s.high)
831 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
833 if ((UWtype) au.s.low < (UWtype) bu.s.low)
835 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
841 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
842 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
843 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
846 __fixunstfdi (TFtype a)
854 /* Compute high word of result, as a flonum. */
855 b = (a / HIGH_WORD_COEFF);
856 /* Convert that to fixed (but not to DWtype!),
857 and shift it into the high word. */
860 /* Remove high part from the TFtype, leaving the low part as flonum. */
862 /* Convert that to fixed (but not to DWtype!) and add it in.
863 Sometimes A comes out negative. This is significant, since
864 A has more bits than a long int does. */
873 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
874 extern DWtype __fixunstfdi (TFtype a);
880 return - __fixunstfdi (-a);
881 return __fixunstfdi (a);
885 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
886 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
887 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
890 __fixunsxfdi (XFtype a)
898 /* Compute high word of result, as a flonum. */
899 b = (a / HIGH_WORD_COEFF);
900 /* Convert that to fixed (but not to DWtype!),
901 and shift it into the high word. */
904 /* Remove high part from the XFtype, leaving the low part as flonum. */
906 /* Convert that to fixed (but not to DWtype!) and add it in.
907 Sometimes A comes out negative. This is significant, since
908 A has more bits than a long int does. */
917 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
918 extern DWtype __fixunsxfdi (XFtype a);
924 return - __fixunsxfdi (-a);
925 return __fixunsxfdi (a);
930 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
931 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
934 __fixunsdfdi (DFtype a)
942 /* Compute high word of result, as a flonum. */
943 b = (a / HIGH_WORD_COEFF);
944 /* Convert that to fixed (but not to DWtype!),
945 and shift it into the high word. */
948 /* Remove high part from the DFtype, leaving the low part as flonum. */
950 /* Convert that to fixed (but not to DWtype!) and add it in.
951 Sometimes A comes out negative. This is significant, since
952 A has more bits than a long int does. */
962 extern DWtype __fixunsdfdi (DFtype a);
968 return - __fixunsdfdi (-a);
969 return __fixunsdfdi (a);
974 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
975 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
978 __fixunssfdi (SFtype original_a)
980 /* Convert the SFtype to a DFtype, because that is surely not going
981 to lose any bits. Some day someone else can write a faster version
982 that avoids converting to DFtype, and verify it really works right. */
983 DFtype a = original_a;
990 /* Compute high word of result, as a flonum. */
991 b = (a / HIGH_WORD_COEFF);
992 /* Convert that to fixed (but not to DWtype!),
993 and shift it into the high word. */
996 /* Remove high part from the DFtype, leaving the low part as flonum. */
998 /* Convert that to fixed (but not to DWtype!) and add it in.
999 Sometimes A comes out negative. This is significant, since
1000 A has more bits than a long int does. */
1002 v -= (UWtype) (- a);
1010 extern DWtype __fixunssfdi (SFtype a);
1013 __fixsfdi (SFtype a)
1016 return - __fixunssfdi (-a);
1017 return __fixunssfdi (a);
1021 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
1022 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1023 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1024 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1027 __floatdixf (DWtype u)
1031 d = (Wtype) (u >> WORD_SIZE);
1032 d *= HIGH_HALFWORD_COEFF;
1033 d *= HIGH_HALFWORD_COEFF;
1034 d += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1040 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
1041 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1042 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1043 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1046 __floatditf (DWtype u)
1050 d = (Wtype) (u >> WORD_SIZE);
1051 d *= HIGH_HALFWORD_COEFF;
1052 d *= HIGH_HALFWORD_COEFF;
1053 d += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1060 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1061 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1062 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1065 __floatdidf (DWtype u)
1069 d = (Wtype) (u >> WORD_SIZE);
1070 d *= HIGH_HALFWORD_COEFF;
1071 d *= HIGH_HALFWORD_COEFF;
1072 d += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1079 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1080 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1081 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1082 #define DI_SIZE (sizeof (DWtype) * BITS_PER_UNIT)
1084 /* Define codes for all the float formats that we know of. Note
1085 that this is copied from real.h. */
1087 #define UNKNOWN_FLOAT_FORMAT 0
1088 #define IEEE_FLOAT_FORMAT 1
1089 #define VAX_FLOAT_FORMAT 2
1090 #define IBM_FLOAT_FORMAT 3
1092 /* Default to IEEE float if not specified. Nearly all machines use it. */
1093 #ifndef HOST_FLOAT_FORMAT
1094 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1097 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1102 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1107 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1113 __floatdisf (DWtype u)
1115 /* Do the calculation in DFmode
1116 so that we don't lose any of the precision of the high word
1117 while multiplying it. */
1120 /* Protect against double-rounding error.
1121 Represent any low-order bits, that might be truncated in DFmode,
1122 by a bit that won't be lost. The bit can go in anywhere below the
1123 rounding position of the SFmode. A fixed mask and bit position
1124 handles all usual configurations. It doesn't handle the case
1125 of 128-bit DImode, however. */
1126 if (DF_SIZE < DI_SIZE
1127 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1129 #define REP_BIT ((UWtype) 1 << (DI_SIZE - DF_SIZE))
1130 if (! (- ((DWtype) 1 << DF_SIZE) < u
1131 && u < ((DWtype) 1 << DF_SIZE)))
1133 if ((UWtype) u & (REP_BIT - 1))
1137 f = (Wtype) (u >> WORD_SIZE);
1138 f *= HIGH_HALFWORD_COEFF;
1139 f *= HIGH_HALFWORD_COEFF;
1140 f += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1146 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1147 /* Reenable the normal types, in case limits.h needs them. */
1160 __fixunsxfsi (XFtype a)
1162 if (a >= - (DFtype) LONG_MIN)
1163 return (Wtype) (a + LONG_MIN) - LONG_MIN;
1169 /* Reenable the normal types, in case limits.h needs them. */
1182 __fixunsdfsi (DFtype a)
1184 if (a >= - (DFtype) LONG_MIN)
1185 return (Wtype) (a + LONG_MIN) - LONG_MIN;
1191 /* Reenable the normal types, in case limits.h needs them. */
1204 __fixunssfsi (SFtype a)
1206 if (a >= - (SFtype) LONG_MIN)
1207 return (Wtype) (a + LONG_MIN) - LONG_MIN;
1212 /* From here on down, the routines use normal data types. */
1214 #define SItype bogus_type
1215 #define USItype bogus_type
1216 #define DItype bogus_type
1217 #define UDItype bogus_type
1218 #define SFtype bogus_type
1219 #define DFtype bogus_type
1237 /* Like bcmp except the sign is meaningful.
1238 Result is negative if S1 is less than S2,
1239 positive if S1 is greater, 0 if S1 and S2 are equal. */
1242 __gcc_bcmp (unsigned char *s1, unsigned char *s2, size_t size)
1246 unsigned char c1 = *s1++, c2 = *s2++;
1263 #if defined(__svr4__) || defined(__alliant__)
1267 /* The Alliant needs the added underscore. */
1268 asm (".globl __builtin_saveregs");
1269 asm ("__builtin_saveregs:");
1270 asm (".globl ___builtin_saveregs");
1271 asm ("___builtin_saveregs:");
1273 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1274 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1275 area and also for a new va_list
1277 /* Save all argument registers in the arg reg save area. The
1278 arg reg save area must have the following layout (according
1290 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1291 asm (" fst.q %f12,16(%sp)");
1293 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1294 asm (" st.l %r17,36(%sp)");
1295 asm (" st.l %r18,40(%sp)");
1296 asm (" st.l %r19,44(%sp)");
1297 asm (" st.l %r20,48(%sp)");
1298 asm (" st.l %r21,52(%sp)");
1299 asm (" st.l %r22,56(%sp)");
1300 asm (" st.l %r23,60(%sp)");
1301 asm (" st.l %r24,64(%sp)");
1302 asm (" st.l %r25,68(%sp)");
1303 asm (" st.l %r26,72(%sp)");
1304 asm (" st.l %r27,76(%sp)");
1306 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1307 va_list structure. Put in into
1308 r16 so that it will be returned
1311 /* Initialize all fields of the new va_list structure. This
1312 structure looks like:
1315 unsigned long ireg_used;
1316 unsigned long freg_used;
1322 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1323 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1324 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1325 asm (" bri %r1"); /* delayed return */
1326 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1328 #else /* not __svr4__ */
1329 #if defined(__PARAGON__)
1331 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1332 * and we stand a better chance of hooking into libraries
1333 * compiled by PGI. [andyp@ssd.intel.com]
1337 asm (".globl __builtin_saveregs");
1338 asm ("__builtin_saveregs:");
1339 asm (".globl ___builtin_saveregs");
1340 asm ("___builtin_saveregs:");
1342 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1343 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1344 area and also for a new va_list
1346 /* Save all argument registers in the arg reg save area. The
1347 arg reg save area must have the following layout (according
1359 asm (" fst.q f8, 0(sp)");
1360 asm (" fst.q f12,16(sp)");
1361 asm (" st.l r16,32(sp)");
1362 asm (" st.l r17,36(sp)");
1363 asm (" st.l r18,40(sp)");
1364 asm (" st.l r19,44(sp)");
1365 asm (" st.l r20,48(sp)");
1366 asm (" st.l r21,52(sp)");
1367 asm (" st.l r22,56(sp)");
1368 asm (" st.l r23,60(sp)");
1369 asm (" st.l r24,64(sp)");
1370 asm (" st.l r25,68(sp)");
1371 asm (" st.l r26,72(sp)");
1372 asm (" st.l r27,76(sp)");
1374 asm (" adds 80,sp,r16"); /* compute the address of the new
1375 va_list structure. Put in into
1376 r16 so that it will be returned
1379 /* Initialize all fields of the new va_list structure. This
1380 structure looks like:
1383 unsigned long ireg_used;
1384 unsigned long freg_used;
1390 asm (" st.l r0, 0(r16)"); /* nfixed */
1391 asm (" st.l r0, 4(r16)"); /* nfloating */
1392 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1393 asm (" bri r1"); /* delayed return */
1394 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1395 #else /* not __PARAGON__ */
1399 asm (".globl ___builtin_saveregs");
1400 asm ("___builtin_saveregs:");
1401 asm (" mov sp,r30");
1402 asm (" andnot 0x0f,sp,sp");
1403 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1405 /* Fill in the __va_struct. */
1406 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1407 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1408 asm (" st.l r18, 8(sp)");
1409 asm (" st.l r19,12(sp)");
1410 asm (" st.l r20,16(sp)");
1411 asm (" st.l r21,20(sp)");
1412 asm (" st.l r22,24(sp)");
1413 asm (" st.l r23,28(sp)");
1414 asm (" st.l r24,32(sp)");
1415 asm (" st.l r25,36(sp)");
1416 asm (" st.l r26,40(sp)");
1417 asm (" st.l r27,44(sp)");
1419 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1420 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1422 /* Fill in the __va_ctl. */
1423 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1424 asm (" st.l r28,84(sp)"); /* pointer to more args */
1425 asm (" st.l r0, 88(sp)"); /* nfixed */
1426 asm (" st.l r0, 92(sp)"); /* nfloating */
1428 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1430 asm (" mov r30,sp");
1431 /* recover stack and pass address to start
1433 #endif /* not __PARAGON__ */
1434 #endif /* not __svr4__ */
1435 #else /* not __i860__ */
1437 asm (".global __builtin_saveregs");
1438 asm ("__builtin_saveregs:");
1439 asm (".global ___builtin_saveregs");
1440 asm ("___builtin_saveregs:");
1441 #ifdef NEED_PROC_COMMAND
1444 asm ("st %i0,[%fp+68]");
1445 asm ("st %i1,[%fp+72]");
1446 asm ("st %i2,[%fp+76]");
1447 asm ("st %i3,[%fp+80]");
1448 asm ("st %i4,[%fp+84]");
1450 asm ("st %i5,[%fp+88]");
1451 #ifdef NEED_TYPE_COMMAND
1452 asm (".type __builtin_saveregs,#function");
1453 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1455 #else /* not __sparc__ */
1456 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1460 asm (" .set nomips16");
1462 asm (" .ent __builtin_saveregs");
1463 asm (" .globl __builtin_saveregs");
1464 asm ("__builtin_saveregs:");
1465 asm (" sw $4,0($30)");
1466 asm (" sw $5,4($30)");
1467 asm (" sw $6,8($30)");
1468 asm (" sw $7,12($30)");
1470 asm (" .end __builtin_saveregs");
1471 #else /* not __mips__, etc. */
1474 __builtin_saveregs (void)
1479 #endif /* not __mips__ */
1480 #endif /* not __sparc__ */
1481 #endif /* not __i860__ */
1485 #ifndef inhibit_libc
1487 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1489 /* This is used by the `assert' macro. */
1490 extern void __eprintf (const char *, const char *, unsigned int, const char *)
1491 __attribute__ ((__noreturn__));
1494 __eprintf (const char *string, const char *expression,
1495 unsigned int line, const char *filename)
1497 fprintf (stderr, string, expression, line, filename);
1507 /* Structure emitted by -a */
1511 const char *filename;
1515 const unsigned long *addresses;
1517 /* Older GCC's did not emit these fields. */
1519 const char **functions;
1520 const long *line_nums;
1521 const char **filenames;
1525 #ifdef BLOCK_PROFILER_CODE
1528 #ifndef inhibit_libc
1530 /* Simple minded basic block profiling output dumper for
1531 systems that don't provide tcov support. At present,
1532 it requires atexit and stdio. */
1534 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1538 #include "gbl-ctors.h"
1539 #include "gcov-io.h"
1541 #ifdef TARGET_HAS_F_SETLKW
1546 static struct bb *bb_head;
1548 static int num_digits (long value, int base) __attribute__ ((const));
1550 /* Return the number of digits needed to print a value */
1551 /* __inline__ */ static int num_digits (long value, int base)
1553 int minus = (value < 0 && base != 16);
1554 unsigned long v = (minus) ? -value : value;
1568 __bb_exit_func (void)
1570 FILE *da_file, *file;
1577 i = strlen (bb_head->filename) - 3;
1579 if (!strcmp (bb_head->filename+i, ".da"))
1581 /* Must be -fprofile-arcs not -a.
1582 Dump data in a form that gcov expects. */
1586 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1590 /* Make sure the output file exists -
1591 but don't clobber exiting data. */
1592 if ((da_file = fopen (ptr->filename, "a")) != 0)
1595 /* Need to re-open in order to be able to write from the start. */
1596 da_file = fopen (ptr->filename, "r+b");
1597 /* Some old systems might not allow the 'b' mode modifier.
1598 Therefore, try to open without it. This can lead to a race
1599 condition so that when you delete and re-create the file, the
1600 file might be opened in text mode, but then, you shouldn't
1601 delete the file in the first place. */
1603 da_file = fopen (ptr->filename, "r+");
1606 fprintf (stderr, "arc profiling: Can't open output file %s.\n",
1611 /* After a fork, another process might try to read and/or write
1612 the same file simultanously. So if we can, lock the file to
1613 avoid race conditions. */
1614 #if defined (TARGET_HAS_F_SETLKW)
1616 struct flock s_flock;
1618 s_flock.l_type = F_WRLCK;
1619 s_flock.l_whence = SEEK_SET;
1620 s_flock.l_start = 0;
1622 s_flock.l_pid = getpid ();
1624 while (fcntl (fileno (da_file), F_SETLKW, &s_flock)
1629 /* If the file is not empty, and the number of counts in it is the
1630 same, then merge them in. */
1631 firstchar = fgetc (da_file);
1632 if (firstchar == EOF)
1634 if (ferror (da_file))
1636 fprintf (stderr, "arc profiling: Can't read output file ");
1637 perror (ptr->filename);
1644 if (ungetc (firstchar, da_file) == EOF)
1646 if (__read_long (&n_counts, da_file, 8) != 0)
1648 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1653 if (n_counts == ptr->ncounts)
1657 for (i = 0; i < n_counts; i++)
1661 if (__read_long (&v, da_file, 8) != 0)
1663 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1667 ptr->counts[i] += v;
1675 /* ??? Should first write a header to the file. Preferably, a 4 byte
1676 magic number, 4 bytes containing the time the program was
1677 compiled, 4 bytes containing the last modification time of the
1678 source file, and 4 bytes indicating the compiler options used.
1680 That way we can easily verify that the proper source/executable/
1681 data file combination is being used from gcov. */
1683 if (__write_long (ptr->ncounts, da_file, 8) != 0)
1686 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1692 long *count_ptr = ptr->counts;
1694 for (j = ptr->ncounts; j > 0; j--)
1696 if (__write_long (*count_ptr, da_file, 8) != 0)
1704 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1708 if (fclose (da_file) == EOF)
1709 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1716 /* Must be basic block profiling. Emit a human readable output file. */
1718 file = fopen ("bb.out", "a");
1727 /* This is somewhat type incorrect, but it avoids worrying about
1728 exactly where time.h is included from. It should be ok unless
1729 a void * differs from other pointer formats, or if sizeof (long)
1730 is < sizeof (time_t). It would be nice if we could assume the
1731 use of rationale standards here. */
1733 time ((void *) &time_value);
1734 fprintf (file, "Basic block profiling finished on %s\n", ctime ((void *) &time_value));
1736 /* We check the length field explicitly in order to allow compatibility
1737 with older GCC's which did not provide it. */
1739 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1742 int func_p = (ptr->nwords >= (long) sizeof (struct bb)
1743 && ptr->nwords <= 1000
1745 int line_p = (func_p && ptr->line_nums);
1746 int file_p = (func_p && ptr->filenames);
1747 int addr_p = (ptr->addresses != 0);
1748 long ncounts = ptr->ncounts;
1754 int blk_len = num_digits (ncounts, 10);
1759 fprintf (file, "File %s, %ld basic blocks \n\n",
1760 ptr->filename, ncounts);
1762 /* Get max values for each field. */
1763 for (i = 0; i < ncounts; i++)
1768 if (cnt_max < ptr->counts[i])
1769 cnt_max = ptr->counts[i];
1771 if (addr_p && (unsigned long) addr_max < ptr->addresses[i])
1772 addr_max = ptr->addresses[i];
1774 if (line_p && line_max < ptr->line_nums[i])
1775 line_max = ptr->line_nums[i];
1779 p = (ptr->functions[i]) ? (ptr->functions[i]) : "<none>";
1787 p = (ptr->filenames[i]) ? (ptr->filenames[i]) : "<none>";
1794 addr_len = num_digits (addr_max, 16);
1795 cnt_len = num_digits (cnt_max, 10);
1796 line_len = num_digits (line_max, 10);
1798 /* Now print out the basic block information. */
1799 for (i = 0; i < ncounts; i++)
1802 " Block #%*d: executed %*ld time(s)",
1804 cnt_len, ptr->counts[i]);
1807 fprintf (file, " address= 0x%.*lx", addr_len,
1811 fprintf (file, " function= %-*s", func_len,
1812 (ptr->functions[i]) ? ptr->functions[i] : "<none>");
1815 fprintf (file, " line= %*ld", line_len, ptr->line_nums[i]);
1818 fprintf (file, " file= %s",
1819 (ptr->filenames[i]) ? ptr->filenames[i] : "<none>");
1821 fprintf (file, "\n");
1824 fprintf (file, "\n");
1828 fprintf (file, "\n\n");
1834 __bb_init_func (struct bb *blocks)
1836 /* User is supposed to check whether the first word is non-0,
1837 but just in case.... */
1839 if (blocks->zero_word)
1842 /* Initialize destructor. */
1844 atexit (__bb_exit_func);
1846 /* Set up linked list. */
1847 blocks->zero_word = 1;
1848 blocks->next = bb_head;
1852 /* Called before fork or exec - write out profile information gathered so
1853 far and reset it to zero. This avoids duplication or loss of the
1854 profile information gathered so far. */
1856 __bb_fork_func (void)
1861 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1864 for (i = ptr->ncounts - 1; i >= 0; i--)
1869 #ifndef MACHINE_STATE_SAVE
1870 #define MACHINE_STATE_SAVE(ID)
1872 #ifndef MACHINE_STATE_RESTORE
1873 #define MACHINE_STATE_RESTORE(ID)
1876 /* Number of buckets in hashtable of basic block addresses. */
1878 #define BB_BUCKETS 311
1880 /* Maximum length of string in file bb.in. */
1882 #define BBINBUFSIZE 500
1884 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1885 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1887 #define BBINBUFSIZESTR "499"
1891 struct bb_edge *next;
1892 unsigned long src_addr;
1893 unsigned long dst_addr;
1894 unsigned long count;
1899 TRACE_KEEP = 0, TRACE_ON = 1, TRACE_OFF = 2
1904 struct bb_func *next;
1907 enum bb_func_mode mode;
1910 /* This is the connection to the outside world.
1911 The BLOCK_PROFILER macro must set __bb.blocks
1912 and __bb.blockno. */
1915 unsigned long blockno;
1919 /* Vars to store addrs of source and destination basic blocks
1922 static unsigned long bb_src = 0;
1923 static unsigned long bb_dst = 0;
1925 static FILE *bb_tracefile = (FILE *) 0;
1926 static struct bb_edge **bb_hashbuckets = (struct bb_edge **) 0;
1927 static struct bb_func *bb_func_head = (struct bb_func *) 0;
1928 static unsigned long bb_callcount = 0;
1929 static int bb_mode = 0;
1931 static unsigned long *bb_stack = (unsigned long *) 0;
1932 static size_t bb_stacksize = 0;
1934 static int reported = 0;
1937 Always : Print execution frequencies of basic blocks
1939 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1940 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1941 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1942 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1947 /*#include <sys/types.h>*/
1948 #include <sys/stat.h>
1949 /*#include <malloc.h>*/
1951 /* Commands executed by gopen. */
1953 #define GOPENDECOMPRESS "gzip -cd "
1954 #define GOPENCOMPRESS "gzip -c >"
1956 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1957 If it does not compile, simply replace gopen by fopen and delete
1958 '.gz' from any first parameter to gopen. */
1961 gopen (char *fn, char *mode)
1969 if (mode[0] != 'r' && mode[0] != 'w')
1972 p = fn + strlen (fn)-1;
1973 use_gzip = ((p[-1] == '.' && (p[0] == 'Z' || p[0] == 'z'))
1974 || (p[-2] == '.' && p[-1] == 'g' && p[0] == 'z'));
1981 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1982 + sizeof (GOPENDECOMPRESS));
1983 strcpy (s, GOPENDECOMPRESS);
1984 strcpy (s + (sizeof (GOPENDECOMPRESS)-1), fn);
1985 f = popen (s, mode);
1993 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1994 + sizeof (GOPENCOMPRESS));
1995 strcpy (s, GOPENCOMPRESS);
1996 strcpy (s + (sizeof (GOPENCOMPRESS)-1), fn);
1997 if (!(f = popen (s, mode)))
1998 f = fopen (s, mode);
2005 return fopen (fn, mode);
2015 if (!fstat (fileno (f), &buf) && S_ISFIFO (buf.st_mode))
2023 #endif /* HAVE_POPEN */
2025 /* Called once per program. */
2028 __bb_exit_trace_func (void)
2030 FILE *file = fopen ("bb.out", "a");
2043 gclose (bb_tracefile);
2045 fclose (bb_tracefile);
2046 #endif /* HAVE_POPEN */
2049 /* Check functions in `bb.in'. */
2054 const struct bb_func *p;
2055 int printed_something = 0;
2059 /* This is somewhat type incorrect. */
2060 time ((void *) &time_value);
2062 for (p = bb_func_head; p != (struct bb_func *) 0; p = p->next)
2064 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
2066 if (!ptr->filename || (p->filename != (char *) 0 && strcmp (p->filename, ptr->filename)))
2068 for (blk = 0; blk < ptr->ncounts; blk++)
2070 if (!strcmp (p->funcname, ptr->functions[blk]))
2075 if (!printed_something)
2077 fprintf (file, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value));
2078 printed_something = 1;
2081 fprintf (file, "\tFunction %s", p->funcname);
2083 fprintf (file, " of file %s", p->filename);
2084 fprintf (file, "\n" );
2089 if (printed_something)
2090 fprintf (file, "\n");
2096 if (!bb_hashbuckets)
2100 fprintf (stderr, "Profiler: out of memory\n");
2110 unsigned long addr_max = 0;
2111 unsigned long cnt_max = 0;
2115 /* This is somewhat type incorrect, but it avoids worrying about
2116 exactly where time.h is included from. It should be ok unless
2117 a void * differs from other pointer formats, or if sizeof (long)
2118 is < sizeof (time_t). It would be nice if we could assume the
2119 use of rationale standards here. */
2121 time ((void *) &time_value);
2122 fprintf (file, "Basic block jump tracing");
2124 switch (bb_mode & 12)
2127 fprintf (file, " (with call)");
2131 /* Print nothing. */
2135 fprintf (file, " (with call & ret)");
2139 fprintf (file, " (with ret)");
2143 fprintf (file, " finished on %s\n", ctime ((void *) &time_value));
2145 for (i = 0; i < BB_BUCKETS; i++)
2147 struct bb_edge *bucket = bb_hashbuckets[i];
2148 for ( ; bucket; bucket = bucket->next )
2150 if (addr_max < bucket->src_addr)
2151 addr_max = bucket->src_addr;
2152 if (addr_max < bucket->dst_addr)
2153 addr_max = bucket->dst_addr;
2154 if (cnt_max < bucket->count)
2155 cnt_max = bucket->count;
2158 addr_len = num_digits (addr_max, 16);
2159 cnt_len = num_digits (cnt_max, 10);
2161 for ( i = 0; i < BB_BUCKETS; i++)
2163 struct bb_edge *bucket = bb_hashbuckets[i];
2164 for ( ; bucket; bucket = bucket->next )
2166 fprintf (file, "Jump from block 0x%.*lx to "
2167 "block 0x%.*lx executed %*lu time(s)\n",
2168 addr_len, bucket->src_addr,
2169 addr_len, bucket->dst_addr,
2170 cnt_len, bucket->count);
2174 fprintf (file, "\n");
2182 /* Free allocated memory. */
2187 struct bb_func *old = f;
2190 if (old->funcname) free (old->funcname);
2191 if (old->filename) free (old->filename);
2202 for (i = 0; i < BB_BUCKETS; i++)
2204 struct bb_edge *old, *bucket = bb_hashbuckets[i];
2209 bucket = bucket->next;
2213 free (bb_hashbuckets);
2216 for (b = bb_head; b; b = b->next)
2217 if (b->flags) free (b->flags);
2220 /* Called once per program. */
2223 __bb_init_prg (void)
2226 char buf[BBINBUFSIZE];
2229 enum bb_func_mode m;
2232 /* Initialize destructor. */
2233 atexit (__bb_exit_func);
2235 if (!(file = fopen ("bb.in", "r")))
2238 while(fscanf (file, " %" BBINBUFSIZESTR "s ", buf) != EOF)
2250 if (!strcmp (p, "__bb_trace__"))
2252 else if (!strcmp (p, "__bb_jumps__"))
2254 else if (!strcmp (p, "__bb_hidecall__"))
2256 else if (!strcmp (p, "__bb_showret__"))
2260 struct bb_func *f = (struct bb_func *) malloc (sizeof (struct bb_func));
2264 f->next = bb_func_head;
2265 if ((pos = strchr (p, ':')))
2267 if (!(f->funcname = (char *) malloc (strlen (pos+1)+1)))
2269 strcpy (f->funcname, pos+1);
2271 if ((f->filename = (char *) malloc (l+1)))
2273 strncpy (f->filename, p, l);
2274 f->filename[l] = '\0';
2277 f->filename = (char *) 0;
2281 if (!(f->funcname = (char *) malloc (strlen (p)+1)))
2283 strcpy (f->funcname, p);
2284 f->filename = (char *) 0;
2296 bb_tracefile = gopen ("bbtrace.gz", "w");
2301 bb_tracefile = fopen ("bbtrace", "w");
2303 #endif /* HAVE_POPEN */
2307 bb_hashbuckets = (struct bb_edge **)
2308 malloc (BB_BUCKETS * sizeof (struct bb_edge *));
2310 /* Use a loop here rather than calling bzero to avoid having to
2311 conditionalize its existance. */
2312 for (i = 0; i < BB_BUCKETS; i++)
2313 bb_hashbuckets[i] = 0;
2319 bb_stack = (unsigned long *) malloc (bb_stacksize * sizeof (*bb_stack));
2322 /* Initialize destructor. */
2323 atexit (__bb_exit_trace_func);
2326 /* Called upon entering a basic block. */
2329 __bb_trace_func (void)
2331 struct bb_edge *bucket;
2333 MACHINE_STATE_SAVE("1")
2335 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2338 bb_dst = __bb.blocks->addresses[__bb.blockno];
2339 __bb.blocks->counts[__bb.blockno]++;
2343 fwrite (&bb_dst, sizeof (unsigned long), 1, bb_tracefile);
2348 struct bb_edge **startbucket, **oldnext;
2350 oldnext = startbucket
2351 = & bb_hashbuckets[ (((int) bb_src*8) ^ (int) bb_dst) % BB_BUCKETS ];
2352 bucket = *startbucket;
2354 for (bucket = *startbucket; bucket;
2355 oldnext = &(bucket->next), bucket = *oldnext)
2357 if (bucket->src_addr == bb_src
2358 && bucket->dst_addr == bb_dst)
2361 *oldnext = bucket->next;
2362 bucket->next = *startbucket;
2363 *startbucket = bucket;
2368 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2374 fprintf (stderr, "Profiler: out of memory\n");
2381 bucket->src_addr = bb_src;
2382 bucket->dst_addr = bb_dst;
2383 bucket->next = *startbucket;
2384 *startbucket = bucket;
2395 MACHINE_STATE_RESTORE("1")
2399 /* Called when returning from a function and `__bb_showret__' is set. */
2402 __bb_trace_func_ret (void)
2404 struct bb_edge *bucket;
2406 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2411 struct bb_edge **startbucket, **oldnext;
2413 oldnext = startbucket
2414 = & bb_hashbuckets[ (((int) bb_dst * 8) ^ (int) bb_src) % BB_BUCKETS ];
2415 bucket = *startbucket;
2417 for (bucket = *startbucket; bucket;
2418 oldnext = &(bucket->next), bucket = *oldnext)
2420 if (bucket->src_addr == bb_dst
2421 && bucket->dst_addr == bb_src)
2424 *oldnext = bucket->next;
2425 bucket->next = *startbucket;
2426 *startbucket = bucket;
2431 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2437 fprintf (stderr, "Profiler: out of memory\n");
2444 bucket->src_addr = bb_dst;
2445 bucket->dst_addr = bb_src;
2446 bucket->next = *startbucket;
2447 *startbucket = bucket;
2460 /* Called upon entering the first function of a file. */
2463 __bb_init_file (struct bb *blocks)
2466 const struct bb_func *p;
2467 long blk, ncounts = blocks->ncounts;
2468 const char **functions = blocks->functions;
2470 /* Set up linked list. */
2471 blocks->zero_word = 1;
2472 blocks->next = bb_head;
2477 || !(blocks->flags = (char *) malloc (sizeof (char) * blocks->ncounts)))
2480 for (blk = 0; blk < ncounts; blk++)
2481 blocks->flags[blk] = 0;
2483 for (blk = 0; blk < ncounts; blk++)
2485 for (p = bb_func_head; p; p = p->next)
2487 if (!strcmp (p->funcname, functions[blk])
2488 && (!p->filename || !strcmp (p->filename, blocks->filename)))
2490 blocks->flags[blk] |= p->mode;
2497 /* Called when exiting from a function. */
2500 __bb_trace_ret (void)
2503 MACHINE_STATE_SAVE("2")
2507 if ((bb_mode & 12) && bb_stacksize > bb_callcount)
2509 bb_src = bb_stack[bb_callcount];
2511 __bb_trace_func_ret ();
2517 MACHINE_STATE_RESTORE("2")
2521 /* Called when entering a function. */
2524 __bb_init_trace_func (struct bb *blocks, unsigned long blockno)
2526 static int trace_init = 0;
2528 MACHINE_STATE_SAVE("3")
2530 if (!blocks->zero_word)
2537 __bb_init_file (blocks);
2547 if (bb_callcount >= bb_stacksize)
2549 size_t newsize = bb_callcount + 100;
2551 bb_stack = (unsigned long *) realloc (bb_stack, newsize);
2556 fprintf (stderr, "Profiler: out of memory\n");
2560 goto stack_overflow;
2562 bb_stacksize = newsize;
2564 bb_stack[bb_callcount] = bb_src;
2575 else if (blocks->flags && (blocks->flags[blockno] & TRACE_ON))
2581 bb_stack[bb_callcount] = bb_src;
2584 MACHINE_STATE_RESTORE("3")
2587 #endif /* not inhibit_libc */
2588 #endif /* not BLOCK_PROFILER_CODE */
2592 unsigned int __shtab[] = {
2593 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2594 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2595 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2596 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2597 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2598 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2599 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2600 0x10000000, 0x20000000, 0x40000000, 0x80000000
2604 #ifdef L_clear_cache
2605 /* Clear part of an instruction cache. */
2607 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2610 __clear_cache (char *beg __attribute__((__unused__)),
2611 char *end __attribute__((__unused__)))
2613 #ifdef CLEAR_INSN_CACHE
2614 CLEAR_INSN_CACHE (beg, end);
2616 #ifdef INSN_CACHE_SIZE
2617 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
2618 static int initialized;
2622 typedef (*function_ptr) (void);
2624 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2625 /* It's cheaper to clear the whole cache.
2626 Put in a series of jump instructions so that calling the beginning
2627 of the cache will clear the whole thing. */
2631 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2632 & -INSN_CACHE_LINE_WIDTH);
2633 int end_ptr = ptr + INSN_CACHE_SIZE;
2635 while (ptr < end_ptr)
2637 *(INSTRUCTION_TYPE *)ptr
2638 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
2639 ptr += INSN_CACHE_LINE_WIDTH;
2641 *(INSTRUCTION_TYPE *) (ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
2646 /* Call the beginning of the sequence. */
2647 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2648 & -INSN_CACHE_LINE_WIDTH))
2651 #else /* Cache is large. */
2655 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2656 & -INSN_CACHE_LINE_WIDTH);
2658 while (ptr < (int) array + sizeof array)
2660 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
2661 ptr += INSN_CACHE_LINE_WIDTH;
2667 /* Find the location in array that occupies the same cache line as BEG. */
2669 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
2670 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
2671 & -INSN_CACHE_PLANE_SIZE)
2674 /* Compute the cache alignment of the place to stop clearing. */
2675 #if 0 /* This is not needed for gcc's purposes. */
2676 /* If the block to clear is bigger than a cache plane,
2677 we clear the entire cache, and OFFSET is already correct. */
2678 if (end < beg + INSN_CACHE_PLANE_SIZE)
2680 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
2681 & -INSN_CACHE_LINE_WIDTH)
2682 & (INSN_CACHE_PLANE_SIZE - 1));
2684 #if INSN_CACHE_DEPTH > 1
2685 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
2686 if (end_addr <= start_addr)
2687 end_addr += INSN_CACHE_PLANE_SIZE;
2689 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
2691 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
2692 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
2694 while (addr != stop)
2696 /* Call the return instruction at ADDR. */
2697 ((function_ptr) addr) ();
2699 addr += INSN_CACHE_LINE_WIDTH;
2702 #else /* just one plane */
2705 /* Call the return instruction at START_ADDR. */
2706 ((function_ptr) start_addr) ();
2708 start_addr += INSN_CACHE_LINE_WIDTH;
2710 while ((start_addr % INSN_CACHE_SIZE) != offset);
2711 #endif /* just one plane */
2712 #endif /* Cache is large */
2713 #endif /* Cache exists */
2714 #endif /* CLEAR_INSN_CACHE */
2717 #endif /* L_clear_cache */
2721 /* Jump to a trampoline, loading the static chain address. */
2723 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2736 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2740 mprotect (char *addr, int len, int prot)
2757 if (VirtualProtect (addr, len, np, &op))
2763 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2765 #ifdef TRANSFER_FROM_TRAMPOLINE
2766 TRANSFER_FROM_TRAMPOLINE
2769 #if defined (NeXT) && defined (__MACH__)
2771 /* Make stack executable so we can call trampolines on stack.
2772 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2776 #include <mach/mach.h>
2780 __enable_execute_stack (char *addr)
2783 char *eaddr = addr + TRAMPOLINE_SIZE;
2784 vm_address_t a = (vm_address_t) addr;
2786 /* turn on execute access on stack */
2787 r = vm_protect (task_self (), a, TRAMPOLINE_SIZE, FALSE, VM_PROT_ALL);
2788 if (r != KERN_SUCCESS)
2790 mach_error("vm_protect VM_PROT_ALL", r);
2794 /* We inline the i-cache invalidation for speed */
2796 #ifdef CLEAR_INSN_CACHE
2797 CLEAR_INSN_CACHE (addr, eaddr);
2799 __clear_cache ((int) addr, (int) eaddr);
2803 #endif /* defined (NeXT) && defined (__MACH__) */
2807 /* Make stack executable so we can call trampolines on stack.
2808 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2810 #include <sys/mman.h>
2811 #include <sys/vmparam.h>
2812 #include <machine/machparam.h>
2815 __enable_execute_stack (void)
2818 static unsigned lowest = USRSTACK;
2819 unsigned current = (unsigned) &fp & -NBPG;
2821 if (lowest > current)
2823 unsigned len = lowest - current;
2824 mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
2828 /* Clear instruction cache in case an old trampoline is in it. */
2831 #endif /* __convex__ */
2835 /* Modified from the convex -code above. */
2837 #include <sys/param.h>
2839 #include <sys/m88kbcs.h>
2842 __enable_execute_stack (void)
2845 static unsigned long lowest = USRSTACK;
2846 unsigned long current = (unsigned long) &save_errno & -NBPC;
2848 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2849 address is seen as 'negative'. That is the case with the stack. */
2852 if (lowest > current)
2854 unsigned len=lowest-current;
2855 memctl(current,len,MCT_TEXT);
2859 memctl(current,NBPC,MCT_TEXT);
2863 #endif /* __sysV88__ */
2867 #include <sys/signal.h>
2870 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2871 so define it here, because we need it in __clear_insn_cache below */
2872 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2873 hence we enable this stuff only if MCT_TEXT is #define'd. */
2888 /* Clear instruction cache so we can call trampolines on stack.
2889 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2892 __clear_insn_cache (void)
2897 /* Preserve errno, because users would be surprised to have
2898 errno changing without explicitly calling any system-call. */
2901 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2902 No need to use an address derived from _start or %sp, as 0 works also. */
2903 memctl(0, 4096, MCT_TEXT);
2908 #endif /* __sysV68__ */
2912 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2914 #include <sys/mman.h>
2915 #include <sys/types.h>
2916 #include <sys/param.h>
2917 #include <sys/vmmac.h>
2919 /* Modified from the convex -code above.
2920 mremap promises to clear the i-cache. */
2923 __enable_execute_stack (void)
2926 if (mprotect (((unsigned int)&fp/PAGSIZ)*PAGSIZ, PAGSIZ,
2927 PROT_READ|PROT_WRITE|PROT_EXEC))
2929 perror ("mprotect in __enable_execute_stack");
2934 #endif /* __pyr__ */
2936 #if defined (sony_news) && defined (SYSTYPE_BSD)
2939 #include <sys/types.h>
2940 #include <sys/param.h>
2941 #include <syscall.h>
2942 #include <machine/sysnews.h>
2944 /* cacheflush function for NEWS-OS 4.2.
2945 This function is called from trampoline-initialize code
2946 defined in config/mips/mips.h. */
2949 cacheflush (char *beg, int size, int flag)
2951 if (syscall (SYS_sysnews, NEWS_CACHEFLUSH, beg, size, FLUSH_BCACHE))
2953 perror ("cache_flush");
2959 #endif /* sony_news */
2960 #endif /* L_trampoline */
2965 #include "gbl-ctors.h"
2966 /* Some systems use __main in a way incompatible with its use in gcc, in these
2967 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2968 give the same symbol without quotes for an alternative entry point. You
2969 must define both, or neither. */
2971 #define NAME__MAIN "__main"
2972 #define SYMBOL__MAIN __main
2975 #ifdef INIT_SECTION_ASM_OP
2976 #undef HAS_INIT_SECTION
2977 #define HAS_INIT_SECTION
2980 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2982 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2983 code to run constructors. In that case, we need to handle EH here, too. */
2985 #ifdef EH_FRAME_SECTION
2987 extern unsigned char __EH_FRAME_BEGIN__[];
2990 /* Run all the global destructors on exit from the program. */
2993 __do_global_dtors (void)
2995 #ifdef DO_GLOBAL_DTORS_BODY
2996 DO_GLOBAL_DTORS_BODY;
2998 static func_ptr *p = __DTOR_LIST__ + 1;
3005 #if defined (EH_FRAME_SECTION) && !defined (HAS_INIT_SECTION)
3007 static int completed = 0;
3011 __deregister_frame_info (__EH_FRAME_BEGIN__);
3018 #ifndef HAS_INIT_SECTION
3019 /* Run all the global constructors on entry to the program. */
3022 __do_global_ctors (void)
3024 #ifdef EH_FRAME_SECTION
3026 static struct object object;
3027 __register_frame_info (__EH_FRAME_BEGIN__, &object);
3030 DO_GLOBAL_CTORS_BODY;
3031 atexit (__do_global_dtors);
3033 #endif /* no HAS_INIT_SECTION */
3035 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
3036 /* Subroutine called automatically by `main'.
3037 Compiling a global function named `main'
3038 produces an automatic call to this function at the beginning.
3040 For many systems, this routine calls __do_global_ctors.
3041 For systems which support a .init section we use the .init section
3042 to run __do_global_ctors, so we need not do anything here. */
3047 /* Support recursive calls to `main': run initializers just once. */
3048 static int initialized;
3052 __do_global_ctors ();
3055 #endif /* no HAS_INIT_SECTION or INVOKE__main */
3057 #endif /* L__main */
3058 #endif /* __CYGWIN__ */
3062 #include "gbl-ctors.h"
3064 /* Provide default definitions for the lists of constructors and
3065 destructors, so that we don't get linker errors. These symbols are
3066 intentionally bss symbols, so that gld and/or collect will provide
3067 the right values. */
3069 /* We declare the lists here with two elements each,
3070 so that they are valid empty lists if no other definition is loaded.
3072 If we are using the old "set" extensions to have the gnu linker
3073 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
3074 must be in the bss/common section.
3076 Long term no port should use those extensions. But many still do. */
3077 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
3078 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
3079 func_ptr __CTOR_LIST__[2] = {0, 0};
3080 func_ptr __DTOR_LIST__[2] = {0, 0};
3082 func_ptr __CTOR_LIST__[2];
3083 func_ptr __DTOR_LIST__[2];
3085 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
3086 #endif /* L_ctors */
3090 #include "gbl-ctors.h"
3098 static func_ptr *atexit_chain = 0;
3099 static long atexit_chain_length = 0;
3100 static volatile long last_atexit_chain_slot = -1;
3103 atexit (func_ptr func)
3105 if (++last_atexit_chain_slot == atexit_chain_length)
3107 atexit_chain_length += 32;
3109 atexit_chain = (func_ptr *) realloc (atexit_chain, atexit_chain_length
3110 * sizeof (func_ptr));
3112 atexit_chain = (func_ptr *) malloc (atexit_chain_length
3113 * sizeof (func_ptr));
3116 atexit_chain_length = 0;
3117 last_atexit_chain_slot = -1;
3122 atexit_chain[last_atexit_chain_slot] = func;
3126 extern void _cleanup (void);
3127 extern void _exit (int) __attribute__ ((__noreturn__));
3134 for ( ; last_atexit_chain_slot-- >= 0; )
3136 (*atexit_chain[last_atexit_chain_slot + 1]) ();
3137 atexit_chain[last_atexit_chain_slot + 1] = 0;
3139 free (atexit_chain);
3152 /* Simple; we just need a wrapper for ON_EXIT. */
3154 atexit (func_ptr func)
3156 return ON_EXIT (func);
3159 #endif /* ON_EXIT */
3160 #endif /* NEED_ATEXIT */
3168 /* Shared exception handling support routines. */
3170 extern void __default_terminate (void) __attribute__ ((__noreturn__));
3173 __default_terminate (void)
3178 void (*__terminate_func)(void) __attribute__ ((__noreturn__)) =
3179 __default_terminate;
3184 (*__terminate_func)();
3188 __throw_type_match (void *catch_type, void *throw_type, void *obj)
3191 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3192 catch_type, throw_type);
3194 if (strcmp ((const char *)catch_type, (const char *)throw_type) == 0)
3205 /* Include definitions of EH context and table layout */
3207 #include "eh-common.h"
3208 #ifndef inhibit_libc
3212 /* Allocate and return a new EH context structure. */
3214 extern void __throw (void);
3218 new_eh_context (void)
3220 struct eh_full_context {
3221 struct eh_context c;
3223 } *ehfc = (struct eh_full_context *) malloc (sizeof *ehfc);
3228 memset (ehfc, 0, sizeof *ehfc);
3230 ehfc->c.dynamic_handler_chain = (void **) ehfc->top_elt;
3232 /* This should optimize out entirely. This should always be true,
3233 but just in case it ever isn't, don't allow bogus code to be
3236 if ((void*)(&ehfc->c) != (void*)ehfc)
3242 static __gthread_key_t eh_context_key;
3244 /* Destructor for struct eh_context. */
3246 eh_context_free (void *ptr)
3248 __gthread_key_dtor (eh_context_key, ptr);
3254 /* Pointer to function to return EH context. */
3256 static struct eh_context *eh_context_initialize (void);
3257 static struct eh_context *eh_context_static (void);
3259 static struct eh_context *eh_context_specific (void);
3262 static struct eh_context *(*get_eh_context) (void) = &eh_context_initialize;
3264 /* Routine to get EH context.
3265 This one will simply call the function pointer. */
3268 __get_eh_context (void)
3270 return (void *) (*get_eh_context) ();
3273 /* Get and set the language specific info pointer. */
3276 __get_eh_info (void)
3278 struct eh_context *eh = (*get_eh_context) ();
3282 #ifdef DWARF2_UNWIND_INFO
3283 static int dwarf_reg_size_table_initialized = 0;
3284 static char dwarf_reg_size_table[DWARF_FRAME_REGISTERS];
3287 init_reg_size_table (void)
3289 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
3290 dwarf_reg_size_table_initialized = 1;
3296 eh_threads_initialize (void)
3298 /* Try to create the key. If it fails, revert to static method,
3299 otherwise start using thread specific EH contexts. */
3300 if (__gthread_key_create (&eh_context_key, &eh_context_free) == 0)
3301 get_eh_context = &eh_context_specific;
3303 get_eh_context = &eh_context_static;
3305 #endif /* no __GTHREADS */
3307 /* Initialize EH context.
3308 This will be called only once, since we change GET_EH_CONTEXT
3309 pointer to another routine. */
3311 static struct eh_context *
3312 eh_context_initialize (void)
3316 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
3317 /* Make sure that get_eh_context does not point to us anymore.
3318 Some systems have dummy thread routines in their libc that
3319 return a success (Solaris 2.6 for example). */
3320 if (__gthread_once (&once, eh_threads_initialize) != 0
3321 || get_eh_context == &eh_context_initialize)
3323 /* Use static version of EH context. */
3324 get_eh_context = &eh_context_static;
3326 #ifdef DWARF2_UNWIND_INFO
3328 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
3329 if (__gthread_once (&once_regsizes, init_reg_size_table) != 0
3330 || ! dwarf_reg_size_table_initialized)
3331 init_reg_size_table ();
3335 #else /* no __GTHREADS */
3337 /* Use static version of EH context. */
3338 get_eh_context = &eh_context_static;
3340 #ifdef DWARF2_UNWIND_INFO
3341 init_reg_size_table ();
3344 #endif /* no __GTHREADS */
3346 return (*get_eh_context) ();
3349 /* Return a static EH context. */
3351 static struct eh_context *
3352 eh_context_static (void)
3354 static struct eh_context eh;
3355 static int initialized;
3356 static void *top_elt[2];
3361 memset (&eh, 0, sizeof eh);
3362 eh.dynamic_handler_chain = top_elt;
3368 /* Return a thread specific EH context. */
3370 static struct eh_context *
3371 eh_context_specific (void)
3373 struct eh_context *eh;
3374 eh = (struct eh_context *) __gthread_getspecific (eh_context_key);
3377 eh = new_eh_context ();
3378 if (__gthread_setspecific (eh_context_key, (void *) eh) != 0)
3386 /* Support routines for setjmp/longjmp exception handling. */
3388 /* Calls to __sjthrow are generated by the compiler when an exception
3389 is raised when using the setjmp/longjmp exception handling codegen
3392 #ifdef DONT_USE_BUILTIN_SETJMP
3393 extern void longjmp (void *, int);
3396 /* Routine to get the head of the current thread's dynamic handler chain
3397 use for exception handling. */
3400 __get_dynamic_handler_chain (void)
3402 struct eh_context *eh = (*get_eh_context) ();
3403 return &eh->dynamic_handler_chain;
3406 /* This is used to throw an exception when the setjmp/longjmp codegen
3407 method is used for exception handling.
3409 We call __terminate if there are no handlers left. Otherwise we run the
3410 cleanup actions off the dynamic cleanup stack, and pop the top of the
3411 dynamic handler chain, and use longjmp to transfer back to the associated
3414 extern void __sjthrow (void) __attribute__ ((__noreturn__));
3419 struct eh_context *eh = (*get_eh_context) ();
3420 void ***dhc = &eh->dynamic_handler_chain;
3422 void (*func)(void *, int);
3424 /* The cleanup chain is one word into the buffer. Get the cleanup chain. */
3425 void ***cleanup = (void***)&(*dhc)[1];
3427 /* If there are any cleanups in the chain, run them now. */
3431 void **buf = (void**)store;
3436 #ifdef DONT_USE_BUILTIN_SETJMP
3437 if (! setjmp (&buf[2]))
3439 if (! __builtin_setjmp (&buf[2]))
3445 func = (void(*)(void*, int))cleanup[0][1];
3446 arg = (void*)cleanup[0][2];
3448 /* Update this before running the cleanup. */
3449 cleanup[0] = (void **)cleanup[0][0];
3462 /* We must call terminate if we try and rethrow an exception, when
3463 there is no exception currently active and when there are no
3465 if (! eh->info || (*dhc)[0] == 0)
3468 /* Find the jmpbuf associated with the top element of the dynamic
3469 handler chain. The jumpbuf starts two words into the buffer. */
3470 jmpbuf = &(*dhc)[2];
3472 /* Then we pop the top element off the dynamic handler chain. */
3473 *dhc = (void**)(*dhc)[0];
3475 /* And then we jump to the handler. */
3477 #ifdef DONT_USE_BUILTIN_SETJMP
3478 longjmp (jmpbuf, 1);
3480 __builtin_longjmp (jmpbuf, 1);
3484 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3485 handler, then pop the handler off the dynamic handler stack, and
3486 then throw. This is used to skip the first handler, and transfer
3487 control to the next handler in the dynamic handler stack. */
3489 extern void __sjpopnthrow (void) __attribute__ ((__noreturn__));
3492 __sjpopnthrow (void)
3494 struct eh_context *eh = (*get_eh_context) ();
3495 void ***dhc = &eh->dynamic_handler_chain;
3496 void (*func)(void *, int);
3498 /* The cleanup chain is one word into the buffer. Get the cleanup chain. */
3499 void ***cleanup = (void***)&(*dhc)[1];
3501 /* If there are any cleanups in the chain, run them now. */
3505 void **buf = (void**)store;
3510 #ifdef DONT_USE_BUILTIN_SETJMP
3511 if (! setjmp (&buf[2]))
3513 if (! __builtin_setjmp (&buf[2]))
3519 func = (void(*)(void*, int))cleanup[0][1];
3520 arg = (void*)cleanup[0][2];
3522 /* Update this before running the cleanup. */
3523 cleanup[0] = (void **)cleanup[0][0];
3536 /* Then we pop the top element off the dynamic handler chain. */
3537 *dhc = (void**)(*dhc)[0];
3542 /* Support code for all exception region-based exception handling. */
3545 __eh_rtime_match (void *rtime)
3548 __eh_matcher matcher;
3551 info = *(__get_eh_info ());
3552 matcher = ((__eh_info *)info)->match_function;
3555 #ifndef inhibit_libc
3556 fprintf (stderr, "Internal Compiler Bug: No runtime type matcher.");
3560 ret = (*matcher) (info, rtime, (void *)0);
3561 return (ret != NULL);
3564 /* This value identifies the place from which an exception is being
3567 #ifdef EH_TABLE_LOOKUP
3573 #ifdef DWARF2_UNWIND_INFO
3575 /* Return the table version of an exception descriptor */
3578 __get_eh_table_version (exception_descriptor *table)
3580 return table->lang.version;
3583 /* Return the originating table language of an exception descriptor */
3586 __get_eh_table_language (exception_descriptor *table)
3588 return table->lang.language;
3591 /* This routine takes a PC and a pointer to the exception region TABLE for
3592 its translation unit, and returns the address of the exception handler
3593 associated with the closest exception table handler entry associated
3594 with that PC, or 0 if there are no table entries the PC fits in.
3596 In the advent of a tie, we have to give the last entry, as it represents
3600 old_find_exception_handler (void *pc, old_exception_table *table)
3607 /* We can't do a binary search because the table isn't guaranteed
3608 to be sorted from function to function. */
3609 for (pos = 0; table[pos].start_region != (void *) -1; ++pos)
3611 if (table[pos].start_region <= pc && table[pos].end_region > pc)
3613 /* This can apply. Make sure it is at least as small as
3614 the previous best. */
3615 if (best == -1 || (table[pos].end_region <= table[best].end_region
3616 && table[pos].start_region >= table[best].start_region))
3619 /* But it is sorted by starting PC within a function. */
3620 else if (best >= 0 && table[pos].start_region > pc)
3624 return table[best].exception_handler;
3630 /* find_exception_handler finds the correct handler, if there is one, to
3631 handle an exception.
3632 returns a pointer to the handler which controlled should be transferred
3633 to, or NULL if there is nothing left.
3635 PC - pc where the exception originates. If this is a rethrow,
3636 then this starts out as a pointer to the exception table
3637 entry we wish to rethrow out of.
3638 TABLE - exception table for the current module.
3639 EH_INFO - eh info pointer for this exception.
3640 RETHROW - 1 if this is a rethrow. (see incoming value of PC).
3641 CLEANUP - returned flag indicating whether this is a cleanup handler.
3644 find_exception_handler (void *pc, exception_descriptor *table,
3645 __eh_info *eh_info, int rethrow, int *cleanup)
3648 void *retval = NULL;
3653 /* The new model assumed the table is sorted inner-most out so the
3654 first region we find which matches is the correct one */
3656 exception_table *tab = &(table->table[0]);
3658 /* Subtract 1 from the PC to avoid hitting the next region */
3661 /* pc is actually the region table entry to rethrow out of */
3662 pos = ((exception_table *) pc) - tab;
3663 pc = ((exception_table *) pc)->end_region - 1;
3665 /* The label is always on the LAST handler entry for a region,
3666 so we know the next entry is a different region, even if the
3667 addresses are the same. Make sure its not end of table tho. */
3668 if (tab[pos].start_region != (void *) -1)
3674 /* We can't do a binary search because the table is in inner-most
3675 to outermost address ranges within functions */
3676 for ( ; tab[pos].start_region != (void *) -1; pos++)
3678 if (tab[pos].start_region <= pc && tab[pos].end_region > pc)
3680 if (tab[pos].match_info)
3682 __eh_matcher matcher = eh_info->match_function;
3683 /* match info but no matcher is NOT a match */
3686 void *ret = (*matcher)((void *) eh_info,
3687 tab[pos].match_info, table);
3691 retval = tab[pos].exception_handler;
3700 retval = tab[pos].exception_handler;
3707 #endif /* DWARF2_UNWIND_INFO */
3708 #endif /* EH_TABLE_LOOKUP */
3710 #ifdef DWARF2_UNWIND_INFO
3711 /* Support code for exception handling using static unwind information. */
3715 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3716 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3717 avoid a warning about casting between int and pointer of different
3720 typedef int ptr_type __attribute__ ((mode (pointer)));
3722 #ifdef INCOMING_REGNO
3723 /* Is the saved value for register REG in frame UDATA stored in a register
3724 window in the previous frame? */
3726 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3727 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3728 compiled functions won't work with the frame-unwind stuff here.
3729 Perhaps the entireity of in_reg_window should be conditional on having
3730 seen a DW_CFA_GNU_window_save? */
3731 #define target_flags 0
3734 in_reg_window (int reg, frame_state *udata)
3736 if (udata->saved[reg] == REG_SAVED_REG)
3737 return INCOMING_REGNO (reg) == reg;
3738 if (udata->saved[reg] != REG_SAVED_OFFSET)
3741 #ifdef STACK_GROWS_DOWNWARD
3742 return udata->reg_or_offset[reg] > 0;
3744 return udata->reg_or_offset[reg] < 0;
3749 in_reg_window (int reg __attribute__ ((__unused__)),
3750 frame_state *udata __attribute__ ((__unused__)))
3754 #endif /* INCOMING_REGNO */
3756 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3757 frame called by UDATA or 0. */
3760 get_reg_addr (unsigned reg, frame_state *udata, frame_state *sub_udata)
3762 while (udata->saved[reg] == REG_SAVED_REG)
3764 reg = udata->reg_or_offset[reg];
3765 if (in_reg_window (reg, udata))
3771 if (udata->saved[reg] == REG_SAVED_OFFSET)
3772 return (word_type *)(udata->cfa + udata->reg_or_offset[reg]);
3777 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3778 frame called by UDATA or 0. */
3780 static inline void *
3781 get_reg (unsigned reg, frame_state *udata, frame_state *sub_udata)
3783 return (void *)(ptr_type) *get_reg_addr (reg, udata, sub_udata);
3786 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3789 put_reg (unsigned reg, void *val, frame_state *udata)
3791 *get_reg_addr (reg, udata, NULL) = (word_type)(ptr_type) val;
3794 /* Copy the saved value for register REG from frame UDATA to frame
3795 TARGET_UDATA. Unlike the previous two functions, this can handle
3796 registers that are not one word large. */
3799 copy_reg (unsigned reg, frame_state *udata, frame_state *target_udata)
3801 word_type *preg = get_reg_addr (reg, udata, NULL);
3802 word_type *ptreg = get_reg_addr (reg, target_udata, NULL);
3804 memcpy (ptreg, preg, dwarf_reg_size_table [reg]);
3807 /* Retrieve the return address for frame UDATA. */
3809 static inline void *
3810 get_return_addr (frame_state *udata, frame_state *sub_udata)
3812 return __builtin_extract_return_addr
3813 (get_reg (udata->retaddr_column, udata, sub_udata));
3816 /* Overwrite the return address for frame UDATA with VAL. */
3819 put_return_addr (void *val, frame_state *udata)
3821 val = __builtin_frob_return_addr (val);
3822 put_reg (udata->retaddr_column, val, udata);
3825 /* Given the current frame UDATA and its return address PC, return the
3826 information about the calling frame in CALLER_UDATA. */
3829 next_stack_level (void *pc, frame_state *udata, frame_state *caller_udata)
3831 caller_udata = __frame_state_for (pc, caller_udata);
3835 /* Now go back to our caller's stack frame. If our caller's CFA register
3836 was saved in our stack frame, restore it; otherwise, assume the CFA
3837 register is SP and restore it to our CFA value. */
3838 if (udata->saved[caller_udata->cfa_reg])
3839 caller_udata->cfa = get_reg (caller_udata->cfa_reg, udata, 0);
3841 caller_udata->cfa = udata->cfa;
3842 caller_udata->cfa += caller_udata->cfa_offset;
3844 return caller_udata;
3847 /* Hook to call before __terminate if only cleanup handlers remain. */
3849 __unwinding_cleanup (void)
3853 /* throw_helper performs some of the common grunt work for a throw. This
3854 routine is called by throw and rethrows. This is pretty much split
3855 out from the old __throw routine. An addition has been added which allows
3856 for a dummy call to a routine __unwinding_cleanup() when there are nothing
3857 but cleanups remaining. This allows a debugger to examine the state
3858 at which the throw was executed, before any cleanups, rather than
3859 at the terminate point after the stack has been unwound.
3861 EH is the current eh_context structure.
3862 PC is the address of the call to __throw.
3863 MY_UDATA is the unwind information for __throw.
3864 OFFSET_P is where we return the SP adjustment offset. */
3867 throw_helper (struct eh_context *eh, void *pc, frame_state *my_udata,
3870 frame_state ustruct2, *udata = &ustruct2;
3871 frame_state ustruct;
3872 frame_state *sub_udata = &ustruct;
3873 void *saved_pc = pc;
3875 void *handler_p = 0;
3877 frame_state saved_ustruct;
3880 int only_cleanup = 0;
3882 int saved_state = 0;
3884 __eh_info *eh_info = (__eh_info *)eh->info;
3886 /* Do we find a handler based on a re-throw PC? */
3887 if (eh->table_index != (void *) 0)
3890 memcpy (udata, my_udata, sizeof (*udata));
3892 handler = (void *) 0;
3895 frame_state *p = udata;
3896 udata = next_stack_level (pc, udata, sub_udata);
3899 /* If we couldn't find the next frame, we lose. */
3903 if (udata->eh_ptr == NULL)
3906 new_eh_model = (((exception_descriptor *)(udata->eh_ptr))->
3907 runtime_id_field == NEW_EH_RUNTIME);
3912 handler = find_exception_handler (eh->table_index, udata->eh_ptr,
3913 eh_info, 1, &cleanup);
3914 eh->table_index = (void *)0;
3918 handler = find_exception_handler (pc, udata->eh_ptr, eh_info,
3921 handler = old_find_exception_handler (pc, udata->eh_ptr);
3923 /* If we found one, we can stop searching, if its not a cleanup.
3924 for cleanups, we save the state, and keep looking. This allows
3925 us to call a debug hook if there are nothing but cleanups left. */
3932 saved_ustruct = *udata;
3933 handler_p = handler;
3946 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3947 hitting the beginning of the next region. */
3948 pc = get_return_addr (udata, sub_udata) - 1;
3953 udata = &saved_ustruct;
3954 handler = handler_p;
3957 __unwinding_cleanup ();
3960 /* If we haven't found a handler by now, this is an unhandled
3965 eh->handler_label = handler;
3967 args_size = udata->args_size;
3970 /* We found a handler in the throw context, no need to unwind. */
3976 /* Unwind all the frames between this one and the handler by copying
3977 their saved register values into our register save slots. */
3979 /* Remember the PC where we found the handler. */
3980 void *handler_pc = pc;
3982 /* Start from the throw context again. */
3984 memcpy (udata, my_udata, sizeof (*udata));
3986 while (pc != handler_pc)
3988 frame_state *p = udata;
3989 udata = next_stack_level (pc, udata, sub_udata);
3992 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
3993 if (i != udata->retaddr_column && udata->saved[i])
3995 /* If you modify the saved value of the return address
3996 register on the SPARC, you modify the return address for
3997 your caller's frame. Don't do that here, as it will
3998 confuse get_return_addr. */
3999 if (in_reg_window (i, udata)
4000 && udata->saved[udata->retaddr_column] == REG_SAVED_REG
4001 && udata->reg_or_offset[udata->retaddr_column] == i)
4003 copy_reg (i, udata, my_udata);
4006 pc = get_return_addr (udata, sub_udata) - 1;
4009 /* But we do need to update the saved return address register from
4010 the last frame we unwind, or the handler frame will have the wrong
4012 if (udata->saved[udata->retaddr_column] == REG_SAVED_REG)
4014 i = udata->reg_or_offset[udata->retaddr_column];
4015 if (in_reg_window (i, udata))
4016 copy_reg (i, udata, my_udata);
4019 /* udata now refers to the frame called by the handler frame. */
4021 /* We adjust SP by the difference between __throw's CFA and the CFA for
4022 the frame called by the handler frame, because those CFAs correspond
4023 to the SP values at the two call sites. We need to further adjust by
4024 the args_size of the handler frame itself to get the handler frame's
4025 SP from before the args were pushed for that call. */
4026 #ifdef STACK_GROWS_DOWNWARD
4027 *offset_p = udata->cfa - my_udata->cfa + args_size;
4029 *offset_p = my_udata->cfa - udata->cfa - args_size;
4036 /* We first search for an exception handler, and if we don't find
4037 it, we call __terminate on the current stack frame so that we may
4038 use the debugger to walk the stack and understand why no handler
4041 If we find one, then we unwind the frames down to the one that
4042 has the handler and transfer control into the handler. */
4044 /*extern void __throw(void) __attribute__ ((__noreturn__));*/
4049 struct eh_context *eh = (*get_eh_context) ();
4053 /* XXX maybe make my_ustruct static so we don't have to look it up for
4055 frame_state my_ustruct, *my_udata = &my_ustruct;
4057 /* This is required for C++ semantics. We must call terminate if we
4058 try and rethrow an exception, when there is no exception currently
4063 /* Start at our stack frame. */
4065 my_udata = __frame_state_for (&&label, my_udata);
4069 /* We need to get the value from the CFA register. */
4070 my_udata->cfa = __builtin_dwarf_cfa ();
4072 /* Do any necessary initialization to access arbitrary stack frames.
4073 On the SPARC, this means flushing the register windows. */
4074 __builtin_unwind_init ();
4076 /* Now reset pc to the right throw point. */
4077 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
4079 handler = throw_helper (eh, pc, my_udata, &offset);
4083 __builtin_eh_return ((void *)eh, offset, handler);
4085 /* Epilogue: restore the handler frame's register values and return
4089 /*extern void __rethrow(void *) __attribute__ ((__noreturn__));*/
4092 __rethrow (void *index)
4094 struct eh_context *eh = (*get_eh_context) ();
4098 /* XXX maybe make my_ustruct static so we don't have to look it up for
4100 frame_state my_ustruct, *my_udata = &my_ustruct;
4102 /* This is required for C++ semantics. We must call terminate if we
4103 try and rethrow an exception, when there is no exception currently
4108 /* This is the table index we want to rethrow from. The value of
4109 the END_REGION label is used for the PC of the throw, and the
4110 search begins with the next table entry. */
4111 eh->table_index = index;
4113 /* Start at our stack frame. */
4115 my_udata = __frame_state_for (&&label, my_udata);
4119 /* We need to get the value from the CFA register. */
4120 my_udata->cfa = __builtin_dwarf_cfa ();
4122 /* Do any necessary initialization to access arbitrary stack frames.
4123 On the SPARC, this means flushing the register windows. */
4124 __builtin_unwind_init ();
4126 /* Now reset pc to the right throw point. */
4127 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
4129 handler = throw_helper (eh, pc, my_udata, &offset);
4133 __builtin_eh_return ((void *)eh, offset, handler);
4135 /* Epilogue: restore the handler frame's register values and return
4138 #endif /* DWARF2_UNWIND_INFO */
4143 #ifndef inhibit_libc
4144 /* This gets us __GNU_LIBRARY__. */
4145 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
4148 #ifdef __GNU_LIBRARY__
4149 /* Avoid forcing the library's meaning of `write' on the user program
4150 by using the "internal" name (for use within the library) */
4151 #define write(fd, buf, n) __write((fd), (buf), (n))
4153 #endif /* inhibit_libc */
4155 #define MESSAGE "pure virtual method called\n"
4157 extern void __terminate (void) __attribute__ ((__noreturn__));
4160 __pure_virtual (void)
4162 #ifndef inhibit_libc
4163 write (2, MESSAGE, sizeof (MESSAGE) - 1);