1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 92-98, 1999 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
29 /* It is incorrect to include config.h here, because this file is being
30 compiled for the target, and hence definitions concerning only the host
35 /* We disable this when inhibit_libc, so that gcc can still be built without
36 needing header files first. */
37 /* ??? This is not a good solution, since prototypes may be required in
38 some cases for correct code. See also frame.c. */
40 /* fixproto guarantees these system headers exist. */
48 extern void *malloc (size_t);
51 extern void free (void *);
54 extern int atexit(void (*)(void));
65 /* Don't use `fancy_abort' here even if config.h says to use it. */
70 /* In a cross-compilation situation, default to inhibiting compilation
71 of routines that use libc. */
73 #if defined(CROSS_COMPILE) && !defined(inhibit_libc)
77 /* Permit the tm.h file to select the endianness to use just for this
78 file. This is used when the endianness is determined when the
81 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
82 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
85 #ifndef LIBGCC2_LONG_DOUBLE_TYPE_SIZE
86 #define LIBGCC2_LONG_DOUBLE_TYPE_SIZE LONG_DOUBLE_TYPE_SIZE
89 /* In the first part of this file, we are interfacing to calls generated
90 by the compiler itself. These calls pass values into these routines
91 which have very specific modes (rather than very specific types), and
92 these compiler-generated calls also expect any return values to have
93 very specific modes (rather than very specific types). Thus, we need
94 to avoid using regular C language type names in this part of the file
95 because the sizes for those types can be configured to be anything.
96 Instead we use the following special type names. */
98 typedef unsigned int UQItype __attribute__ ((mode (QI)));
99 typedef int SItype __attribute__ ((mode (SI)));
100 typedef unsigned int USItype __attribute__ ((mode (SI)));
101 typedef int DItype __attribute__ ((mode (DI)));
102 typedef unsigned int UDItype __attribute__ ((mode (DI)));
104 typedef float SFtype __attribute__ ((mode (SF)));
105 typedef float DFtype __attribute__ ((mode (DF)));
107 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
108 typedef float XFtype __attribute__ ((mode (XF)));
110 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
111 typedef float TFtype __attribute__ ((mode (TF)));
114 typedef int word_type __attribute__ ((mode (__word__)));
116 /* Make sure that we don't accidentally use any normal C language built-in
117 type names in the first part of this file. Instead we want to use *only*
118 the type names defined above. The following macro definitions insure
119 that if we *do* accidentally use some normal C language built-in type name,
120 we will get a syntax error. */
122 #define char bogus_type
123 #define short bogus_type
124 #define int bogus_type
125 #define long bogus_type
126 #define unsigned bogus_type
127 #define float bogus_type
128 #define double bogus_type
130 #if BITS_PER_UNIT == 8
131 #define SI_TYPE_SIZE 32
133 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
135 #define W_TYPE_SIZE SI_TYPE_SIZE
136 #define UWtype USItype
137 #define UHWtype USItype
138 #define UDWtype UDItype
140 /* DIstructs are pairs of SItype values in the order determined by
141 LIBGCC2_WORDS_BIG_ENDIAN. */
143 #if LIBGCC2_WORDS_BIG_ENDIAN
144 struct DIstruct {SItype high, low;};
146 struct DIstruct {SItype low, high;};
149 /* We need this union to unpack/pack DImode values, since we don't have
150 any arithmetic yet. Incoming DImode parameters are stored into the
151 `ll' field, and the unpacked result is read from the struct `s'. */
159 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
160 || defined (L_divdi3) || defined (L_udivdi3) \
161 || defined (L_moddi3) || defined (L_umoddi3))
163 #include "longlong.h"
165 #endif /* udiv or mul */
167 extern DItype __fixunssfdi (SFtype a);
168 extern DItype __fixunsdfdi (DFtype a);
169 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
170 extern DItype __fixunsxfdi (XFtype a);
172 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
173 extern DItype __fixunstfdi (TFtype a);
176 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
177 #if defined (L_divdi3) || defined (L_moddi3)
189 w.s.high = -uu.s.high - ((USItype) w.s.low > 0);
195 /* Unless shift functions are defined whith full ANSI prototypes,
196 parameter b will be promoted to int if word_type is smaller than an int. */
199 __lshrdi3 (DItype u, word_type b)
210 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
214 w.s.low = (USItype)uu.s.high >> -bm;
218 USItype carries = (USItype)uu.s.high << bm;
219 w.s.high = (USItype)uu.s.high >> b;
220 w.s.low = ((USItype)uu.s.low >> b) | carries;
229 __ashldi3 (DItype u, word_type b)
240 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
244 w.s.high = (USItype)uu.s.low << -bm;
248 USItype carries = (USItype)uu.s.low >> bm;
249 w.s.low = (USItype)uu.s.low << b;
250 w.s.high = ((USItype)uu.s.high << b) | carries;
259 __ashrdi3 (DItype u, word_type b)
270 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
273 /* w.s.high = 1..1 or 0..0 */
274 w.s.high = uu.s.high >> (sizeof (SItype) * BITS_PER_UNIT - 1);
275 w.s.low = uu.s.high >> -bm;
279 USItype carries = (USItype)uu.s.high << bm;
280 w.s.high = uu.s.high >> b;
281 w.s.low = ((USItype)uu.s.low >> b) | carries;
295 w.s.low = ffs (uu.s.low);
298 w.s.low = ffs (uu.s.high);
301 w.s.low += BITS_PER_UNIT * sizeof (SItype);
310 __muldi3 (DItype u, DItype v)
318 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
319 w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
320 + (USItype) uu.s.high * (USItype) vv.s.low);
327 #if defined (sdiv_qrnnd)
329 __udiv_w_sdiv (USItype *rp, USItype a1, USItype a0, USItype d)
336 if (a1 < d - a1 - (a0 >> (SI_TYPE_SIZE - 1)))
338 /* dividend, divisor, and quotient are nonnegative */
339 sdiv_qrnnd (q, r, a1, a0, d);
343 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
344 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (SI_TYPE_SIZE - 1));
345 /* Divide (c1*2^32 + c0) by d */
346 sdiv_qrnnd (q, r, c1, c0, d);
347 /* Add 2^31 to quotient */
348 q += (USItype) 1 << (SI_TYPE_SIZE - 1);
353 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
354 c1 = a1 >> 1; /* A/2 */
355 c0 = (a1 << (SI_TYPE_SIZE - 1)) + (a0 >> 1);
357 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
359 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
361 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
378 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
381 c0 = ~c0; /* logical NOT */
383 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
385 q = ~q; /* (A/2)/b1 */
388 r = 2*r + (a0 & 1); /* A/(2*b1) */
406 else /* Implies c1 = b1 */
407 { /* Hence a1 = d - 1 = 2*b1 - 1 */
425 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
427 __udiv_w_sdiv (USItype *rp __attribute__ ((__unused__)),
428 USItype a1 __attribute__ ((__unused__)),
429 USItype a0 __attribute__ ((__unused__)),
430 USItype d __attribute__ ((__unused__)))
437 #if (defined (L_udivdi3) || defined (L_divdi3) || \
438 defined (L_umoddi3) || defined (L_moddi3))
443 static const UQItype __clz_tab[] =
445 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,
446 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,
447 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,
448 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,
449 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,
450 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,
451 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,
452 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,
455 #if (defined (L_udivdi3) || defined (L_divdi3) || \
456 defined (L_umoddi3) || defined (L_moddi3))
460 __udivmoddi4 (UDItype n, UDItype d, UDItype *rp)
465 USItype d0, d1, n0, n1, n2;
477 #if !UDIV_NEEDS_NORMALIZATION
484 udiv_qrnnd (q0, n0, n1, n0, d0);
487 /* Remainder in n0. */
494 d0 = 1 / d0; /* Divide intentionally by zero. */
496 udiv_qrnnd (q1, n1, 0, n1, d0);
497 udiv_qrnnd (q0, n0, n1, n0, d0);
499 /* Remainder in n0. */
510 #else /* UDIV_NEEDS_NORMALIZATION */
518 count_leading_zeros (bm, d0);
522 /* Normalize, i.e. make the most significant bit of the
526 n1 = (n1 << bm) | (n0 >> (SI_TYPE_SIZE - bm));
530 udiv_qrnnd (q0, n0, n1, n0, d0);
533 /* Remainder in n0 >> bm. */
540 d0 = 1 / d0; /* Divide intentionally by zero. */
542 count_leading_zeros (bm, d0);
546 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
547 conclude (the most significant bit of n1 is set) /\ (the
548 leading quotient digit q1 = 1).
550 This special case is necessary, not an optimization.
551 (Shifts counts of SI_TYPE_SIZE are undefined.) */
560 b = SI_TYPE_SIZE - bm;
564 n1 = (n1 << bm) | (n0 >> b);
567 udiv_qrnnd (q1, n1, n2, n1, d0);
572 udiv_qrnnd (q0, n0, n1, n0, d0);
574 /* Remainder in n0 >> bm. */
584 #endif /* UDIV_NEEDS_NORMALIZATION */
595 /* Remainder in n1n0. */
607 count_leading_zeros (bm, d1);
610 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
611 conclude (the most significant bit of n1 is set) /\ (the
612 quotient digit q0 = 0 or 1).
614 This special case is necessary, not an optimization. */
616 /* The condition on the next line takes advantage of that
617 n1 >= d1 (true due to program flow). */
618 if (n1 > d1 || n0 >= d0)
621 sub_ddmmss (n1, n0, n1, n0, d1, d0);
640 b = SI_TYPE_SIZE - bm;
642 d1 = (d1 << bm) | (d0 >> b);
645 n1 = (n1 << bm) | (n0 >> b);
648 udiv_qrnnd (q0, n1, n2, n1, d1);
649 umul_ppmm (m1, m0, q0, d0);
651 if (m1 > n1 || (m1 == n1 && m0 > n0))
654 sub_ddmmss (m1, m0, m1, m0, d1, d0);
659 /* Remainder in (n1n0 - m1m0) >> bm. */
662 sub_ddmmss (n1, n0, n1, n0, m1, m0);
663 rr.s.low = (n1 << b) | (n0 >> bm);
664 rr.s.high = n1 >> bm;
679 __divdi3 (DItype u, DItype v)
690 uu.ll = __negdi2 (uu.ll);
693 vv.ll = __negdi2 (vv.ll);
695 w = __udivmoddi4 (uu.ll, vv.ll, (UDItype *) 0);
705 __moddi3 (DItype u, DItype v)
716 uu.ll = __negdi2 (uu.ll);
718 vv.ll = __negdi2 (vv.ll);
720 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
730 __umoddi3 (UDItype u, UDItype v)
734 (void) __udivmoddi4 (u, v, &w);
742 __udivdi3 (UDItype n, UDItype d)
744 return __udivmoddi4 (n, d, (UDItype *) 0);
750 __cmpdi2 (DItype a, DItype b)
754 au.ll = a, bu.ll = b;
756 if (au.s.high < bu.s.high)
758 else if (au.s.high > bu.s.high)
760 if ((USItype) au.s.low < (USItype) bu.s.low)
762 else if ((USItype) au.s.low > (USItype) bu.s.low)
770 __ucmpdi2 (DItype a, DItype b)
774 au.ll = a, bu.ll = b;
776 if ((USItype) au.s.high < (USItype) bu.s.high)
778 else if ((USItype) au.s.high > (USItype) bu.s.high)
780 if ((USItype) au.s.low < (USItype) bu.s.low)
782 else if ((USItype) au.s.low > (USItype) bu.s.low)
788 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
789 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
790 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
793 __fixunstfdi (TFtype a)
801 /* Compute high word of result, as a flonum. */
802 b = (a / HIGH_WORD_COEFF);
803 /* Convert that to fixed (but not to DItype!),
804 and shift it into the high word. */
807 /* Remove high part from the TFtype, leaving the low part as flonum. */
809 /* Convert that to fixed (but not to DItype!) and add it in.
810 Sometimes A comes out negative. This is significant, since
811 A has more bits than a long int does. */
813 v -= (USItype) (- a);
820 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
825 return - __fixunstfdi (-a);
826 return __fixunstfdi (a);
830 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
831 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
832 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
835 __fixunsxfdi (XFtype a)
843 /* Compute high word of result, as a flonum. */
844 b = (a / HIGH_WORD_COEFF);
845 /* Convert that to fixed (but not to DItype!),
846 and shift it into the high word. */
849 /* Remove high part from the XFtype, leaving the low part as flonum. */
851 /* Convert that to fixed (but not to DItype!) and add it in.
852 Sometimes A comes out negative. This is significant, since
853 A has more bits than a long int does. */
855 v -= (USItype) (- a);
862 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
867 return - __fixunsxfdi (-a);
868 return __fixunsxfdi (a);
873 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
874 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
877 __fixunsdfdi (DFtype a)
885 /* Compute high word of result, as a flonum. */
886 b = (a / HIGH_WORD_COEFF);
887 /* Convert that to fixed (but not to DItype!),
888 and shift it into the high word. */
891 /* Remove high part from the DFtype, leaving the low part as flonum. */
893 /* Convert that to fixed (but not to DItype!) and add it in.
894 Sometimes A comes out negative. This is significant, since
895 A has more bits than a long int does. */
897 v -= (USItype) (- a);
909 return - __fixunsdfdi (-a);
910 return __fixunsdfdi (a);
915 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
916 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
919 __fixunssfdi (SFtype original_a)
921 /* Convert the SFtype to a DFtype, because that is surely not going
922 to lose any bits. Some day someone else can write a faster version
923 that avoids converting to DFtype, and verify it really works right. */
924 DFtype a = original_a;
931 /* Compute high word of result, as a flonum. */
932 b = (a / HIGH_WORD_COEFF);
933 /* Convert that to fixed (but not to DItype!),
934 and shift it into the high word. */
937 /* Remove high part from the DFtype, leaving the low part as flonum. */
939 /* Convert that to fixed (but not to DItype!) and add it in.
940 Sometimes A comes out negative. This is significant, since
941 A has more bits than a long int does. */
943 v -= (USItype) (- a);
955 return - __fixunssfdi (-a);
956 return __fixunssfdi (a);
960 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
961 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
962 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
963 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
966 __floatdixf (DItype u)
970 d = (SItype) (u >> WORD_SIZE);
971 d *= HIGH_HALFWORD_COEFF;
972 d *= HIGH_HALFWORD_COEFF;
973 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
979 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
980 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
981 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
982 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
985 __floatditf (DItype u)
989 d = (SItype) (u >> WORD_SIZE);
990 d *= HIGH_HALFWORD_COEFF;
991 d *= HIGH_HALFWORD_COEFF;
992 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
999 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1000 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1001 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1004 __floatdidf (DItype u)
1008 d = (SItype) (u >> WORD_SIZE);
1009 d *= HIGH_HALFWORD_COEFF;
1010 d *= HIGH_HALFWORD_COEFF;
1011 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
1018 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1019 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1020 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1021 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1023 /* Define codes for all the float formats that we know of. Note
1024 that this is copied from real.h. */
1026 #define UNKNOWN_FLOAT_FORMAT 0
1027 #define IEEE_FLOAT_FORMAT 1
1028 #define VAX_FLOAT_FORMAT 2
1029 #define IBM_FLOAT_FORMAT 3
1031 /* Default to IEEE float if not specified. Nearly all machines use it. */
1032 #ifndef HOST_FLOAT_FORMAT
1033 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1036 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1041 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1046 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1052 __floatdisf (DItype u)
1054 /* Do the calculation in DFmode
1055 so that we don't lose any of the precision of the high word
1056 while multiplying it. */
1059 /* Protect against double-rounding error.
1060 Represent any low-order bits, that might be truncated in DFmode,
1061 by a bit that won't be lost. The bit can go in anywhere below the
1062 rounding position of the SFmode. A fixed mask and bit position
1063 handles all usual configurations. It doesn't handle the case
1064 of 128-bit DImode, however. */
1065 if (DF_SIZE < DI_SIZE
1066 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1068 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1069 if (! (- ((DItype) 1 << DF_SIZE) < u
1070 && u < ((DItype) 1 << DF_SIZE)))
1072 if ((USItype) u & (REP_BIT - 1))
1076 f = (SItype) (u >> WORD_SIZE);
1077 f *= HIGH_HALFWORD_COEFF;
1078 f *= HIGH_HALFWORD_COEFF;
1079 f += (USItype) (u & (HIGH_WORD_COEFF - 1));
1085 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1086 /* Reenable the normal types, in case limits.h needs them. */
1099 __fixunsxfsi (XFtype a)
1101 if (a >= - (DFtype) LONG_MIN)
1102 return (SItype) (a + LONG_MIN) - LONG_MIN;
1108 /* Reenable the normal types, in case limits.h needs them. */
1121 __fixunsdfsi (DFtype a)
1123 if (a >= - (DFtype) LONG_MIN)
1124 return (SItype) (a + LONG_MIN) - LONG_MIN;
1130 /* Reenable the normal types, in case limits.h needs them. */
1143 __fixunssfsi (SFtype a)
1145 if (a >= - (SFtype) LONG_MIN)
1146 return (SItype) (a + LONG_MIN) - LONG_MIN;
1151 /* From here on down, the routines use normal data types. */
1153 #define SItype bogus_type
1154 #define USItype bogus_type
1155 #define DItype bogus_type
1156 #define UDItype bogus_type
1157 #define SFtype bogus_type
1158 #define DFtype bogus_type
1170 /* Like bcmp except the sign is meaningful.
1171 Result is negative if S1 is less than S2,
1172 positive if S1 is greater, 0 if S1 and S2 are equal. */
1175 __gcc_bcmp (unsigned char *s1, unsigned char *s2, size_t size)
1179 unsigned char c1 = *s1++, c2 = *s2++;
1196 #if defined(__svr4__) || defined(__alliant__)
1200 /* The Alliant needs the added underscore. */
1201 asm (".globl __builtin_saveregs");
1202 asm ("__builtin_saveregs:");
1203 asm (".globl ___builtin_saveregs");
1204 asm ("___builtin_saveregs:");
1206 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1207 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1208 area and also for a new va_list
1210 /* Save all argument registers in the arg reg save area. The
1211 arg reg save area must have the following layout (according
1223 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1224 asm (" fst.q %f12,16(%sp)");
1226 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1227 asm (" st.l %r17,36(%sp)");
1228 asm (" st.l %r18,40(%sp)");
1229 asm (" st.l %r19,44(%sp)");
1230 asm (" st.l %r20,48(%sp)");
1231 asm (" st.l %r21,52(%sp)");
1232 asm (" st.l %r22,56(%sp)");
1233 asm (" st.l %r23,60(%sp)");
1234 asm (" st.l %r24,64(%sp)");
1235 asm (" st.l %r25,68(%sp)");
1236 asm (" st.l %r26,72(%sp)");
1237 asm (" st.l %r27,76(%sp)");
1239 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1240 va_list structure. Put in into
1241 r16 so that it will be returned
1244 /* Initialize all fields of the new va_list structure. This
1245 structure looks like:
1248 unsigned long ireg_used;
1249 unsigned long freg_used;
1255 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1256 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1257 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1258 asm (" bri %r1"); /* delayed return */
1259 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1261 #else /* not __svr4__ */
1262 #if defined(__PARAGON__)
1264 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1265 * and we stand a better chance of hooking into libraries
1266 * compiled by PGI. [andyp@ssd.intel.com]
1270 asm (".globl __builtin_saveregs");
1271 asm ("__builtin_saveregs:");
1272 asm (".globl ___builtin_saveregs");
1273 asm ("___builtin_saveregs:");
1275 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1276 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1277 area and also for a new va_list
1279 /* Save all argument registers in the arg reg save area. The
1280 arg reg save area must have the following layout (according
1292 asm (" fst.q f8, 0(sp)");
1293 asm (" fst.q f12,16(sp)");
1294 asm (" st.l r16,32(sp)");
1295 asm (" st.l r17,36(sp)");
1296 asm (" st.l r18,40(sp)");
1297 asm (" st.l r19,44(sp)");
1298 asm (" st.l r20,48(sp)");
1299 asm (" st.l r21,52(sp)");
1300 asm (" st.l r22,56(sp)");
1301 asm (" st.l r23,60(sp)");
1302 asm (" st.l r24,64(sp)");
1303 asm (" st.l r25,68(sp)");
1304 asm (" st.l r26,72(sp)");
1305 asm (" st.l r27,76(sp)");
1307 asm (" adds 80,sp,r16"); /* compute the address of the new
1308 va_list structure. Put in into
1309 r16 so that it will be returned
1312 /* Initialize all fields of the new va_list structure. This
1313 structure looks like:
1316 unsigned long ireg_used;
1317 unsigned long freg_used;
1323 asm (" st.l r0, 0(r16)"); /* nfixed */
1324 asm (" st.l r0, 4(r16)"); /* nfloating */
1325 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1326 asm (" bri r1"); /* delayed return */
1327 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1328 #else /* not __PARAGON__ */
1332 asm (".globl ___builtin_saveregs");
1333 asm ("___builtin_saveregs:");
1334 asm (" mov sp,r30");
1335 asm (" andnot 0x0f,sp,sp");
1336 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1338 /* Fill in the __va_struct. */
1339 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1340 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1341 asm (" st.l r18, 8(sp)");
1342 asm (" st.l r19,12(sp)");
1343 asm (" st.l r20,16(sp)");
1344 asm (" st.l r21,20(sp)");
1345 asm (" st.l r22,24(sp)");
1346 asm (" st.l r23,28(sp)");
1347 asm (" st.l r24,32(sp)");
1348 asm (" st.l r25,36(sp)");
1349 asm (" st.l r26,40(sp)");
1350 asm (" st.l r27,44(sp)");
1352 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1353 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1355 /* Fill in the __va_ctl. */
1356 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1357 asm (" st.l r28,84(sp)"); /* pointer to more args */
1358 asm (" st.l r0, 88(sp)"); /* nfixed */
1359 asm (" st.l r0, 92(sp)"); /* nfloating */
1361 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1363 asm (" mov r30,sp");
1364 /* recover stack and pass address to start
1366 #endif /* not __PARAGON__ */
1367 #endif /* not __svr4__ */
1368 #else /* not __i860__ */
1370 asm (".global __builtin_saveregs");
1371 asm ("__builtin_saveregs:");
1372 asm (".global ___builtin_saveregs");
1373 asm ("___builtin_saveregs:");
1374 #ifdef NEED_PROC_COMMAND
1377 asm ("st %i0,[%fp+68]");
1378 asm ("st %i1,[%fp+72]");
1379 asm ("st %i2,[%fp+76]");
1380 asm ("st %i3,[%fp+80]");
1381 asm ("st %i4,[%fp+84]");
1383 asm ("st %i5,[%fp+88]");
1384 #ifdef NEED_TYPE_COMMAND
1385 asm (".type __builtin_saveregs,#function");
1386 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1388 #else /* not __sparc__ */
1389 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1393 asm (" .set nomips16");
1395 asm (" .ent __builtin_saveregs");
1396 asm (" .globl __builtin_saveregs");
1397 asm ("__builtin_saveregs:");
1398 asm (" sw $4,0($30)");
1399 asm (" sw $5,4($30)");
1400 asm (" sw $6,8($30)");
1401 asm (" sw $7,12($30)");
1403 asm (" .end __builtin_saveregs");
1404 #else /* not __mips__, etc. */
1407 __builtin_saveregs (void)
1412 #endif /* not __mips__ */
1413 #endif /* not __sparc__ */
1414 #endif /* not __i860__ */
1418 #ifndef inhibit_libc
1420 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1422 /* This is used by the `assert' macro. */
1423 extern void __eprintf (const char *, const char *, unsigned int, const char *)
1424 __attribute__ ((__noreturn__));
1427 __eprintf (const char *string, const char *expression,
1428 unsigned int line, const char *filename)
1430 fprintf (stderr, string, expression, line, filename);
1440 /* Structure emitted by -a */
1444 const char *filename;
1448 const unsigned long *addresses;
1450 /* Older GCC's did not emit these fields. */
1452 const char **functions;
1453 const long *line_nums;
1454 const char **filenames;
1458 #ifdef BLOCK_PROFILER_CODE
1461 #ifndef inhibit_libc
1463 /* Simple minded basic block profiling output dumper for
1464 systems that don't provide tcov support. At present,
1465 it requires atexit and stdio. */
1467 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1471 #include "gbl-ctors.h"
1472 #include "gcov-io.h"
1475 static struct bb *bb_head;
1477 static int num_digits (long value, int base) __attribute__ ((const));
1479 /* Return the number of digits needed to print a value */
1480 /* __inline__ */ static int num_digits (long value, int base)
1482 int minus = (value < 0 && base != 16);
1483 unsigned long v = (minus) ? -value : value;
1497 __bb_exit_func (void)
1499 FILE *da_file, *file;
1506 i = strlen (bb_head->filename) - 3;
1508 if (!strcmp (bb_head->filename+i, ".da"))
1510 /* Must be -fprofile-arcs not -a.
1511 Dump data in a form that gcov expects. */
1515 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1517 /* If the file exists, and the number of counts in it is the same,
1518 then merge them in. */
1520 if ((da_file = fopen (ptr->filename, "rb")) != 0)
1524 if (__read_long (&n_counts, da_file, 8) != 0)
1526 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1531 if (n_counts == ptr->ncounts)
1535 for (i = 0; i < n_counts; i++)
1539 if (__read_long (&v, da_file, 8) != 0)
1541 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1545 ptr->counts[i] += v;
1549 if (fclose (da_file) == EOF)
1550 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1553 if ((da_file = fopen (ptr->filename, "wb")) == 0)
1555 fprintf (stderr, "arc profiling: Can't open output file %s.\n",
1560 /* ??? Should first write a header to the file. Preferably, a 4 byte
1561 magic number, 4 bytes containing the time the program was
1562 compiled, 4 bytes containing the last modification time of the
1563 source file, and 4 bytes indicating the compiler options used.
1565 That way we can easily verify that the proper source/executable/
1566 data file combination is being used from gcov. */
1568 if (__write_long (ptr->ncounts, da_file, 8) != 0)
1571 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1577 long *count_ptr = ptr->counts;
1579 for (j = ptr->ncounts; j > 0; j--)
1581 if (__write_long (*count_ptr, da_file, 8) != 0)
1589 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1593 if (fclose (da_file) == EOF)
1594 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1601 /* Must be basic block profiling. Emit a human readable output file. */
1603 file = fopen ("bb.out", "a");
1612 /* This is somewhat type incorrect, but it avoids worrying about
1613 exactly where time.h is included from. It should be ok unless
1614 a void * differs from other pointer formats, or if sizeof (long)
1615 is < sizeof (time_t). It would be nice if we could assume the
1616 use of rationale standards here. */
1618 time ((void *) &time_value);
1619 fprintf (file, "Basic block profiling finished on %s\n", ctime ((void *) &time_value));
1621 /* We check the length field explicitly in order to allow compatibility
1622 with older GCC's which did not provide it. */
1624 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1627 int func_p = (ptr->nwords >= (long) sizeof (struct bb)
1628 && ptr->nwords <= 1000
1630 int line_p = (func_p && ptr->line_nums);
1631 int file_p = (func_p && ptr->filenames);
1632 int addr_p = (ptr->addresses != 0);
1633 long ncounts = ptr->ncounts;
1639 int blk_len = num_digits (ncounts, 10);
1644 fprintf (file, "File %s, %ld basic blocks \n\n",
1645 ptr->filename, ncounts);
1647 /* Get max values for each field. */
1648 for (i = 0; i < ncounts; i++)
1653 if (cnt_max < ptr->counts[i])
1654 cnt_max = ptr->counts[i];
1656 if (addr_p && (unsigned long) addr_max < ptr->addresses[i])
1657 addr_max = ptr->addresses[i];
1659 if (line_p && line_max < ptr->line_nums[i])
1660 line_max = ptr->line_nums[i];
1664 p = (ptr->functions[i]) ? (ptr->functions[i]) : "<none>";
1672 p = (ptr->filenames[i]) ? (ptr->filenames[i]) : "<none>";
1679 addr_len = num_digits (addr_max, 16);
1680 cnt_len = num_digits (cnt_max, 10);
1681 line_len = num_digits (line_max, 10);
1683 /* Now print out the basic block information. */
1684 for (i = 0; i < ncounts; i++)
1687 " Block #%*d: executed %*ld time(s)",
1689 cnt_len, ptr->counts[i]);
1692 fprintf (file, " address= 0x%.*lx", addr_len,
1696 fprintf (file, " function= %-*s", func_len,
1697 (ptr->functions[i]) ? ptr->functions[i] : "<none>");
1700 fprintf (file, " line= %*ld", line_len, ptr->line_nums[i]);
1703 fprintf (file, " file= %s",
1704 (ptr->filenames[i]) ? ptr->filenames[i] : "<none>");
1706 fprintf (file, "\n");
1709 fprintf (file, "\n");
1713 fprintf (file, "\n\n");
1719 __bb_init_func (struct bb *blocks)
1721 /* User is supposed to check whether the first word is non-0,
1722 but just in case.... */
1724 if (blocks->zero_word)
1727 /* Initialize destructor. */
1729 atexit (__bb_exit_func);
1731 /* Set up linked list. */
1732 blocks->zero_word = 1;
1733 blocks->next = bb_head;
1737 #ifndef MACHINE_STATE_SAVE
1738 #define MACHINE_STATE_SAVE(ID)
1740 #ifndef MACHINE_STATE_RESTORE
1741 #define MACHINE_STATE_RESTORE(ID)
1744 /* Number of buckets in hashtable of basic block addresses. */
1746 #define BB_BUCKETS 311
1748 /* Maximum length of string in file bb.in. */
1750 #define BBINBUFSIZE 500
1752 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1753 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1755 #define BBINBUFSIZESTR "499"
1759 struct bb_edge *next;
1760 unsigned long src_addr;
1761 unsigned long dst_addr;
1762 unsigned long count;
1767 TRACE_KEEP = 0, TRACE_ON = 1, TRACE_OFF = 2
1772 struct bb_func *next;
1775 enum bb_func_mode mode;
1778 /* This is the connection to the outside world.
1779 The BLOCK_PROFILER macro must set __bb.blocks
1780 and __bb.blockno. */
1783 unsigned long blockno;
1787 /* Vars to store addrs of source and destination basic blocks
1790 static unsigned long bb_src = 0;
1791 static unsigned long bb_dst = 0;
1793 static FILE *bb_tracefile = (FILE *) 0;
1794 static struct bb_edge **bb_hashbuckets = (struct bb_edge **) 0;
1795 static struct bb_func *bb_func_head = (struct bb_func *) 0;
1796 static unsigned long bb_callcount = 0;
1797 static int bb_mode = 0;
1799 static unsigned long *bb_stack = (unsigned long *) 0;
1800 static size_t bb_stacksize = 0;
1802 static int reported = 0;
1805 Always : Print execution frequencies of basic blocks
1807 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1808 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1809 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1810 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1815 /*#include <sys/types.h>*/
1816 #include <sys/stat.h>
1817 /*#include <malloc.h>*/
1819 /* Commands executed by gopen. */
1821 #define GOPENDECOMPRESS "gzip -cd "
1822 #define GOPENCOMPRESS "gzip -c >"
1824 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1825 If it does not compile, simply replace gopen by fopen and delete
1826 '.gz' from any first parameter to gopen. */
1829 gopen (char *fn, char *mode)
1837 if (mode[0] != 'r' && mode[0] != 'w')
1840 p = fn + strlen (fn)-1;
1841 use_gzip = ((p[-1] == '.' && (p[0] == 'Z' || p[0] == 'z'))
1842 || (p[-2] == '.' && p[-1] == 'g' && p[0] == 'z'));
1849 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1850 + sizeof (GOPENDECOMPRESS));
1851 strcpy (s, GOPENDECOMPRESS);
1852 strcpy (s + (sizeof (GOPENDECOMPRESS)-1), fn);
1853 f = popen (s, mode);
1861 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1862 + sizeof (GOPENCOMPRESS));
1863 strcpy (s, GOPENCOMPRESS);
1864 strcpy (s + (sizeof (GOPENCOMPRESS)-1), fn);
1865 if (!(f = popen (s, mode)))
1866 f = fopen (s, mode);
1873 return fopen (fn, mode);
1883 if (!fstat (fileno (f), &buf) && S_ISFIFO (buf.st_mode))
1891 #endif /* HAVE_POPEN */
1893 /* Called once per program. */
1896 __bb_exit_trace_func (void)
1898 FILE *file = fopen ("bb.out", "a");
1911 gclose (bb_tracefile);
1913 fclose (bb_tracefile);
1914 #endif /* HAVE_POPEN */
1917 /* Check functions in `bb.in'. */
1922 const struct bb_func *p;
1923 int printed_something = 0;
1927 /* This is somewhat type incorrect. */
1928 time ((void *) &time_value);
1930 for (p = bb_func_head; p != (struct bb_func *) 0; p = p->next)
1932 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1934 if (!ptr->filename || (p->filename != (char *) 0 && strcmp (p->filename, ptr->filename)))
1936 for (blk = 0; blk < ptr->ncounts; blk++)
1938 if (!strcmp (p->funcname, ptr->functions[blk]))
1943 if (!printed_something)
1945 fprintf (file, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value));
1946 printed_something = 1;
1949 fprintf (file, "\tFunction %s", p->funcname);
1951 fprintf (file, " of file %s", p->filename);
1952 fprintf (file, "\n" );
1957 if (printed_something)
1958 fprintf (file, "\n");
1964 if (!bb_hashbuckets)
1968 fprintf (stderr, "Profiler: out of memory\n");
1978 unsigned long addr_max = 0;
1979 unsigned long cnt_max = 0;
1983 /* This is somewhat type incorrect, but it avoids worrying about
1984 exactly where time.h is included from. It should be ok unless
1985 a void * differs from other pointer formats, or if sizeof (long)
1986 is < sizeof (time_t). It would be nice if we could assume the
1987 use of rationale standards here. */
1989 time ((void *) &time_value);
1990 fprintf (file, "Basic block jump tracing");
1992 switch (bb_mode & 12)
1995 fprintf (file, " (with call)");
1999 /* Print nothing. */
2003 fprintf (file, " (with call & ret)");
2007 fprintf (file, " (with ret)");
2011 fprintf (file, " finished on %s\n", ctime ((void *) &time_value));
2013 for (i = 0; i < BB_BUCKETS; i++)
2015 struct bb_edge *bucket = bb_hashbuckets[i];
2016 for ( ; bucket; bucket = bucket->next )
2018 if (addr_max < bucket->src_addr)
2019 addr_max = bucket->src_addr;
2020 if (addr_max < bucket->dst_addr)
2021 addr_max = bucket->dst_addr;
2022 if (cnt_max < bucket->count)
2023 cnt_max = bucket->count;
2026 addr_len = num_digits (addr_max, 16);
2027 cnt_len = num_digits (cnt_max, 10);
2029 for ( i = 0; i < BB_BUCKETS; i++)
2031 struct bb_edge *bucket = bb_hashbuckets[i];
2032 for ( ; bucket; bucket = bucket->next )
2034 fprintf (file, "Jump from block 0x%.*lx to "
2035 "block 0x%.*lx executed %*lu time(s)\n",
2036 addr_len, bucket->src_addr,
2037 addr_len, bucket->dst_addr,
2038 cnt_len, bucket->count);
2042 fprintf (file, "\n");
2050 /* Free allocated memory. */
2055 struct bb_func *old = f;
2058 if (old->funcname) free (old->funcname);
2059 if (old->filename) free (old->filename);
2070 for (i = 0; i < BB_BUCKETS; i++)
2072 struct bb_edge *old, *bucket = bb_hashbuckets[i];
2077 bucket = bucket->next;
2081 free (bb_hashbuckets);
2084 for (b = bb_head; b; b = b->next)
2085 if (b->flags) free (b->flags);
2088 /* Called once per program. */
2091 __bb_init_prg (void)
2094 char buf[BBINBUFSIZE];
2097 enum bb_func_mode m;
2100 /* Initialize destructor. */
2101 atexit (__bb_exit_func);
2103 if (!(file = fopen ("bb.in", "r")))
2106 while(fscanf (file, " %" BBINBUFSIZESTR "s ", buf) != EOF)
2118 if (!strcmp (p, "__bb_trace__"))
2120 else if (!strcmp (p, "__bb_jumps__"))
2122 else if (!strcmp (p, "__bb_hidecall__"))
2124 else if (!strcmp (p, "__bb_showret__"))
2128 struct bb_func *f = (struct bb_func *) malloc (sizeof (struct bb_func));
2132 f->next = bb_func_head;
2133 if ((pos = strchr (p, ':')))
2135 if (!(f->funcname = (char *) malloc (strlen (pos+1)+1)))
2137 strcpy (f->funcname, pos+1);
2139 if ((f->filename = (char *) malloc (l+1)))
2141 strncpy (f->filename, p, l);
2142 f->filename[l] = '\0';
2145 f->filename = (char *) 0;
2149 if (!(f->funcname = (char *) malloc (strlen (p)+1)))
2151 strcpy (f->funcname, p);
2152 f->filename = (char *) 0;
2164 bb_tracefile = gopen ("bbtrace.gz", "w");
2169 bb_tracefile = fopen ("bbtrace", "w");
2171 #endif /* HAVE_POPEN */
2175 bb_hashbuckets = (struct bb_edge **)
2176 malloc (BB_BUCKETS * sizeof (struct bb_edge *));
2178 /* Use a loop here rather than calling bzero to avoid having to
2179 conditionalize its existance. */
2180 for (i = 0; i < BB_BUCKETS; i++)
2181 bb_hashbuckets[i] = 0;
2187 bb_stack = (unsigned long *) malloc (bb_stacksize * sizeof (*bb_stack));
2190 /* Initialize destructor. */
2191 atexit (__bb_exit_trace_func);
2194 /* Called upon entering a basic block. */
2197 __bb_trace_func (void)
2199 struct bb_edge *bucket;
2201 MACHINE_STATE_SAVE("1")
2203 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2206 bb_dst = __bb.blocks->addresses[__bb.blockno];
2207 __bb.blocks->counts[__bb.blockno]++;
2211 fwrite (&bb_dst, sizeof (unsigned long), 1, bb_tracefile);
2216 struct bb_edge **startbucket, **oldnext;
2218 oldnext = startbucket
2219 = & bb_hashbuckets[ (((int) bb_src*8) ^ (int) bb_dst) % BB_BUCKETS ];
2220 bucket = *startbucket;
2222 for (bucket = *startbucket; bucket;
2223 oldnext = &(bucket->next), bucket = *oldnext)
2225 if (bucket->src_addr == bb_src
2226 && bucket->dst_addr == bb_dst)
2229 *oldnext = bucket->next;
2230 bucket->next = *startbucket;
2231 *startbucket = bucket;
2236 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2242 fprintf (stderr, "Profiler: out of memory\n");
2249 bucket->src_addr = bb_src;
2250 bucket->dst_addr = bb_dst;
2251 bucket->next = *startbucket;
2252 *startbucket = bucket;
2263 MACHINE_STATE_RESTORE("1")
2267 /* Called when returning from a function and `__bb_showret__' is set. */
2270 __bb_trace_func_ret (void)
2272 struct bb_edge *bucket;
2274 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2279 struct bb_edge **startbucket, **oldnext;
2281 oldnext = startbucket
2282 = & bb_hashbuckets[ (((int) bb_dst * 8) ^ (int) bb_src) % BB_BUCKETS ];
2283 bucket = *startbucket;
2285 for (bucket = *startbucket; bucket;
2286 oldnext = &(bucket->next), bucket = *oldnext)
2288 if (bucket->src_addr == bb_dst
2289 && bucket->dst_addr == bb_src)
2292 *oldnext = bucket->next;
2293 bucket->next = *startbucket;
2294 *startbucket = bucket;
2299 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2305 fprintf (stderr, "Profiler: out of memory\n");
2312 bucket->src_addr = bb_dst;
2313 bucket->dst_addr = bb_src;
2314 bucket->next = *startbucket;
2315 *startbucket = bucket;
2328 /* Called upon entering the first function of a file. */
2331 __bb_init_file (struct bb *blocks)
2334 const struct bb_func *p;
2335 long blk, ncounts = blocks->ncounts;
2336 const char **functions = blocks->functions;
2338 /* Set up linked list. */
2339 blocks->zero_word = 1;
2340 blocks->next = bb_head;
2345 || !(blocks->flags = (char *) malloc (sizeof (char) * blocks->ncounts)))
2348 for (blk = 0; blk < ncounts; blk++)
2349 blocks->flags[blk] = 0;
2351 for (blk = 0; blk < ncounts; blk++)
2353 for (p = bb_func_head; p; p = p->next)
2355 if (!strcmp (p->funcname, functions[blk])
2356 && (!p->filename || !strcmp (p->filename, blocks->filename)))
2358 blocks->flags[blk] |= p->mode;
2365 /* Called when exiting from a function. */
2368 __bb_trace_ret (void)
2371 MACHINE_STATE_SAVE("2")
2375 if ((bb_mode & 12) && bb_stacksize > bb_callcount)
2377 bb_src = bb_stack[bb_callcount];
2379 __bb_trace_func_ret ();
2385 MACHINE_STATE_RESTORE("2")
2389 /* Called when entering a function. */
2392 __bb_init_trace_func (struct bb *blocks, unsigned long blockno)
2394 static int trace_init = 0;
2396 MACHINE_STATE_SAVE("3")
2398 if (!blocks->zero_word)
2405 __bb_init_file (blocks);
2415 if (bb_callcount >= bb_stacksize)
2417 size_t newsize = bb_callcount + 100;
2419 bb_stack = (unsigned long *) realloc (bb_stack, newsize);
2424 fprintf (stderr, "Profiler: out of memory\n");
2428 goto stack_overflow;
2430 bb_stacksize = newsize;
2432 bb_stack[bb_callcount] = bb_src;
2443 else if (blocks->flags && (blocks->flags[blockno] & TRACE_ON))
2449 bb_stack[bb_callcount] = bb_src;
2452 MACHINE_STATE_RESTORE("3")
2455 #endif /* not inhibit_libc */
2456 #endif /* not BLOCK_PROFILER_CODE */
2460 unsigned int __shtab[] = {
2461 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2462 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2463 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2464 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2465 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2466 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2467 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2468 0x10000000, 0x20000000, 0x40000000, 0x80000000
2472 #ifdef L_clear_cache
2473 /* Clear part of an instruction cache. */
2475 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2478 __clear_cache (char *beg __attribute__((__unused__)),
2479 char *end __attribute__((__unused__)))
2481 #ifdef CLEAR_INSN_CACHE
2482 CLEAR_INSN_CACHE (beg, end);
2484 #ifdef INSN_CACHE_SIZE
2485 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
2486 static int initialized;
2490 typedef (*function_ptr) (void);
2492 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2493 /* It's cheaper to clear the whole cache.
2494 Put in a series of jump instructions so that calling the beginning
2495 of the cache will clear the whole thing. */
2499 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2500 & -INSN_CACHE_LINE_WIDTH);
2501 int end_ptr = ptr + INSN_CACHE_SIZE;
2503 while (ptr < end_ptr)
2505 *(INSTRUCTION_TYPE *)ptr
2506 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
2507 ptr += INSN_CACHE_LINE_WIDTH;
2509 *(INSTRUCTION_TYPE *) (ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
2514 /* Call the beginning of the sequence. */
2515 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2516 & -INSN_CACHE_LINE_WIDTH))
2519 #else /* Cache is large. */
2523 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2524 & -INSN_CACHE_LINE_WIDTH);
2526 while (ptr < (int) array + sizeof array)
2528 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
2529 ptr += INSN_CACHE_LINE_WIDTH;
2535 /* Find the location in array that occupies the same cache line as BEG. */
2537 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
2538 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
2539 & -INSN_CACHE_PLANE_SIZE)
2542 /* Compute the cache alignment of the place to stop clearing. */
2543 #if 0 /* This is not needed for gcc's purposes. */
2544 /* If the block to clear is bigger than a cache plane,
2545 we clear the entire cache, and OFFSET is already correct. */
2546 if (end < beg + INSN_CACHE_PLANE_SIZE)
2548 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
2549 & -INSN_CACHE_LINE_WIDTH)
2550 & (INSN_CACHE_PLANE_SIZE - 1));
2552 #if INSN_CACHE_DEPTH > 1
2553 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
2554 if (end_addr <= start_addr)
2555 end_addr += INSN_CACHE_PLANE_SIZE;
2557 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
2559 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
2560 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
2562 while (addr != stop)
2564 /* Call the return instruction at ADDR. */
2565 ((function_ptr) addr) ();
2567 addr += INSN_CACHE_LINE_WIDTH;
2570 #else /* just one plane */
2573 /* Call the return instruction at START_ADDR. */
2574 ((function_ptr) start_addr) ();
2576 start_addr += INSN_CACHE_LINE_WIDTH;
2578 while ((start_addr % INSN_CACHE_SIZE) != offset);
2579 #endif /* just one plane */
2580 #endif /* Cache is large */
2581 #endif /* Cache exists */
2582 #endif /* CLEAR_INSN_CACHE */
2585 #endif /* L_clear_cache */
2589 /* Jump to a trampoline, loading the static chain address. */
2591 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2604 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2608 mprotect (char *addr, int len, int prot)
2625 if (VirtualProtect (addr, len, np, &op))
2631 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2633 #ifdef TRANSFER_FROM_TRAMPOLINE
2634 TRANSFER_FROM_TRAMPOLINE
2637 #if defined (NeXT) && defined (__MACH__)
2639 /* Make stack executable so we can call trampolines on stack.
2640 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2644 #include <mach/mach.h>
2648 __enable_execute_stack (char *addr)
2651 char *eaddr = addr + TRAMPOLINE_SIZE;
2652 vm_address_t a = (vm_address_t) addr;
2654 /* turn on execute access on stack */
2655 r = vm_protect (task_self (), a, TRAMPOLINE_SIZE, FALSE, VM_PROT_ALL);
2656 if (r != KERN_SUCCESS)
2658 mach_error("vm_protect VM_PROT_ALL", r);
2662 /* We inline the i-cache invalidation for speed */
2664 #ifdef CLEAR_INSN_CACHE
2665 CLEAR_INSN_CACHE (addr, eaddr);
2667 __clear_cache ((int) addr, (int) eaddr);
2671 #endif /* defined (NeXT) && defined (__MACH__) */
2675 /* Make stack executable so we can call trampolines on stack.
2676 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2678 #include <sys/mman.h>
2679 #include <sys/vmparam.h>
2680 #include <machine/machparam.h>
2683 __enable_execute_stack (void)
2686 static unsigned lowest = USRSTACK;
2687 unsigned current = (unsigned) &fp & -NBPG;
2689 if (lowest > current)
2691 unsigned len = lowest - current;
2692 mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
2696 /* Clear instruction cache in case an old trampoline is in it. */
2699 #endif /* __convex__ */
2703 /* Modified from the convex -code above. */
2705 #include <sys/param.h>
2707 #include <sys/m88kbcs.h>
2710 __enable_execute_stack (void)
2713 static unsigned long lowest = USRSTACK;
2714 unsigned long current = (unsigned long) &save_errno & -NBPC;
2716 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2717 address is seen as 'negative'. That is the case with the stack. */
2720 if (lowest > current)
2722 unsigned len=lowest-current;
2723 memctl(current,len,MCT_TEXT);
2727 memctl(current,NBPC,MCT_TEXT);
2731 #endif /* __sysV88__ */
2735 #include <sys/signal.h>
2738 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2739 so define it here, because we need it in __clear_insn_cache below */
2740 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2741 hence we enable this stuff only if MCT_TEXT is #define'd. */
2756 /* Clear instruction cache so we can call trampolines on stack.
2757 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2760 __clear_insn_cache (void)
2765 /* Preserve errno, because users would be surprised to have
2766 errno changing without explicitly calling any system-call. */
2769 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2770 No need to use an address derived from _start or %sp, as 0 works also. */
2771 memctl(0, 4096, MCT_TEXT);
2776 #endif /* __sysV68__ */
2780 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2782 #include <sys/mman.h>
2783 #include <sys/types.h>
2784 #include <sys/param.h>
2785 #include <sys/vmmac.h>
2787 /* Modified from the convex -code above.
2788 mremap promises to clear the i-cache. */
2791 __enable_execute_stack (void)
2794 if (mprotect (((unsigned int)&fp/PAGSIZ)*PAGSIZ, PAGSIZ,
2795 PROT_READ|PROT_WRITE|PROT_EXEC))
2797 perror ("mprotect in __enable_execute_stack");
2802 #endif /* __pyr__ */
2804 #if defined (sony_news) && defined (SYSTYPE_BSD)
2807 #include <sys/types.h>
2808 #include <sys/param.h>
2809 #include <syscall.h>
2810 #include <machine/sysnews.h>
2812 /* cacheflush function for NEWS-OS 4.2.
2813 This function is called from trampoline-initialize code
2814 defined in config/mips/mips.h. */
2817 cacheflush (char *beg, int size, int flag)
2819 if (syscall (SYS_sysnews, NEWS_CACHEFLUSH, beg, size, FLUSH_BCACHE))
2821 perror ("cache_flush");
2827 #endif /* sony_news */
2828 #endif /* L_trampoline */
2833 #include "gbl-ctors.h"
2834 /* Some systems use __main in a way incompatible with its use in gcc, in these
2835 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2836 give the same symbol without quotes for an alternative entry point. You
2837 must define both, or neither. */
2839 #define NAME__MAIN "__main"
2840 #define SYMBOL__MAIN __main
2843 #ifdef INIT_SECTION_ASM_OP
2844 #undef HAS_INIT_SECTION
2845 #define HAS_INIT_SECTION
2848 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2850 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2851 code to run constructors. In that case, we need to handle EH here, too. */
2853 #ifdef EH_FRAME_SECTION
2855 extern unsigned char __EH_FRAME_BEGIN__[];
2858 /* Run all the global destructors on exit from the program. */
2861 __do_global_dtors (void)
2863 #ifdef DO_GLOBAL_DTORS_BODY
2864 DO_GLOBAL_DTORS_BODY;
2866 static func_ptr *p = __DTOR_LIST__ + 1;
2873 #if defined (EH_FRAME_SECTION) && !defined (HAS_INIT_SECTION)
2875 static int completed = 0;
2879 __deregister_frame_info (__EH_FRAME_BEGIN__);
2886 #ifndef HAS_INIT_SECTION
2887 /* Run all the global constructors on entry to the program. */
2890 __do_global_ctors (void)
2892 #ifdef EH_FRAME_SECTION
2894 static struct object object;
2895 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2898 DO_GLOBAL_CTORS_BODY;
2899 atexit (__do_global_dtors);
2901 #endif /* no HAS_INIT_SECTION */
2903 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2904 /* Subroutine called automatically by `main'.
2905 Compiling a global function named `main'
2906 produces an automatic call to this function at the beginning.
2908 For many systems, this routine calls __do_global_ctors.
2909 For systems which support a .init section we use the .init section
2910 to run __do_global_ctors, so we need not do anything here. */
2915 /* Support recursive calls to `main': run initializers just once. */
2916 static int initialized;
2920 __do_global_ctors ();
2923 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2925 #endif /* L__main */
2926 #endif /* __CYGWIN__ */
2930 #include "gbl-ctors.h"
2932 /* Provide default definitions for the lists of constructors and
2933 destructors, so that we don't get linker errors. These symbols are
2934 intentionally bss symbols, so that gld and/or collect will provide
2935 the right values. */
2937 /* We declare the lists here with two elements each,
2938 so that they are valid empty lists if no other definition is loaded.
2940 If we are using the old "set" extensions to have the gnu linker
2941 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2942 must be in the bss/common section.
2944 Long term no port should use those extensions. But many still do. */
2945 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2946 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
2947 func_ptr __CTOR_LIST__[2] = {0, 0};
2948 func_ptr __DTOR_LIST__[2] = {0, 0};
2950 func_ptr __CTOR_LIST__[2];
2951 func_ptr __DTOR_LIST__[2];
2953 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2954 #endif /* L_ctors */
2958 #include "gbl-ctors.h"
2966 static func_ptr *atexit_chain = 0;
2967 static long atexit_chain_length = 0;
2968 static volatile long last_atexit_chain_slot = -1;
2971 atexit (func_ptr func)
2973 if (++last_atexit_chain_slot == atexit_chain_length)
2975 atexit_chain_length += 32;
2977 atexit_chain = (func_ptr *) realloc (atexit_chain, atexit_chain_length
2978 * sizeof (func_ptr));
2980 atexit_chain = (func_ptr *) malloc (atexit_chain_length
2981 * sizeof (func_ptr));
2984 atexit_chain_length = 0;
2985 last_atexit_chain_slot = -1;
2990 atexit_chain[last_atexit_chain_slot] = func;
2994 extern void _cleanup (void);
2995 extern void _exit (int) __attribute__ ((__noreturn__));
3002 for ( ; last_atexit_chain_slot-- >= 0; )
3004 (*atexit_chain[last_atexit_chain_slot + 1]) ();
3005 atexit_chain[last_atexit_chain_slot + 1] = 0;
3007 free (atexit_chain);
3020 /* Simple; we just need a wrapper for ON_EXIT. */
3022 atexit (func_ptr func)
3024 return ON_EXIT (func);
3027 #endif /* ON_EXIT */
3028 #endif /* NEED_ATEXIT */
3036 /* Shared exception handling support routines. */
3038 extern void __default_terminate (void) __attribute__ ((__noreturn__));
3041 __default_terminate (void)
3046 void (*__terminate_func)(void) __attribute__ ((__noreturn__)) =
3047 __default_terminate;
3052 (*__terminate_func)();
3056 __throw_type_match (void *catch_type, void *throw_type, void *obj)
3059 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3060 catch_type, throw_type);
3062 if (strcmp ((const char *)catch_type, (const char *)throw_type) == 0)
3073 /* Include definitions of EH context and table layout */
3075 #include "eh-common.h"
3076 #ifndef inhibit_libc
3080 /* Allocate and return a new EH context structure. */
3082 extern void __throw (void);
3086 new_eh_context (void)
3088 struct eh_full_context {
3089 struct eh_context c;
3091 } *ehfc = (struct eh_full_context *) malloc (sizeof *ehfc);
3096 memset (ehfc, 0, sizeof *ehfc);
3098 ehfc->c.dynamic_handler_chain = (void **) ehfc->top_elt;
3100 /* This should optimize out entirely. This should always be true,
3101 but just in case it ever isn't, don't allow bogus code to be
3104 if ((void*)(&ehfc->c) != (void*)ehfc)
3110 static __gthread_key_t eh_context_key;
3112 /* Destructor for struct eh_context. */
3114 eh_context_free (void *ptr)
3116 __gthread_key_dtor (eh_context_key, ptr);
3122 /* Pointer to function to return EH context. */
3124 static struct eh_context *eh_context_initialize (void);
3125 static struct eh_context *eh_context_static (void);
3127 static struct eh_context *eh_context_specific (void);
3130 static struct eh_context *(*get_eh_context) (void) = &eh_context_initialize;
3132 /* Routine to get EH context.
3133 This one will simply call the function pointer. */
3136 __get_eh_context (void)
3138 return (void *) (*get_eh_context) ();
3141 /* Get and set the language specific info pointer. */
3144 __get_eh_info (void)
3146 struct eh_context *eh = (*get_eh_context) ();
3150 #ifdef DWARF2_UNWIND_INFO
3151 static int dwarf_reg_size_table_initialized = 0;
3152 static char dwarf_reg_size_table[FIRST_PSEUDO_REGISTER];
3155 init_reg_size_table (void)
3157 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
3158 dwarf_reg_size_table_initialized = 1;
3164 eh_threads_initialize (void)
3166 /* Try to create the key. If it fails, revert to static method,
3167 otherwise start using thread specific EH contexts. */
3168 if (__gthread_key_create (&eh_context_key, &eh_context_free) == 0)
3169 get_eh_context = &eh_context_specific;
3171 get_eh_context = &eh_context_static;
3173 #endif /* no __GTHREADS */
3175 /* Initialize EH context.
3176 This will be called only once, since we change GET_EH_CONTEXT
3177 pointer to another routine. */
3179 static struct eh_context *
3180 eh_context_initialize (void)
3184 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
3185 /* Make sure that get_eh_context does not point to us anymore.
3186 Some systems have dummy thread routines in their libc that
3187 return a success (Solaris 2.6 for example). */
3188 if (__gthread_once (&once, eh_threads_initialize) != 0
3189 || get_eh_context == &eh_context_initialize)
3191 /* Use static version of EH context. */
3192 get_eh_context = &eh_context_static;
3194 #ifdef DWARF2_UNWIND_INFO
3196 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
3197 if (__gthread_once (&once_regsizes, init_reg_size_table) != 0
3198 || ! dwarf_reg_size_table_initialized)
3199 init_reg_size_table ();
3203 #else /* no __GTHREADS */
3205 /* Use static version of EH context. */
3206 get_eh_context = &eh_context_static;
3208 #ifdef DWARF2_UNWIND_INFO
3209 init_reg_size_table ();
3212 #endif /* no __GTHREADS */
3214 return (*get_eh_context) ();
3217 /* Return a static EH context. */
3219 static struct eh_context *
3220 eh_context_static (void)
3222 static struct eh_context eh;
3223 static int initialized;
3224 static void *top_elt[2];
3229 memset (&eh, 0, sizeof eh);
3230 eh.dynamic_handler_chain = top_elt;
3236 /* Return a thread specific EH context. */
3238 static struct eh_context *
3239 eh_context_specific (void)
3241 struct eh_context *eh;
3242 eh = (struct eh_context *) __gthread_getspecific (eh_context_key);
3245 eh = new_eh_context ();
3246 if (__gthread_setspecific (eh_context_key, (void *) eh) != 0)
3254 /* Support routines for setjmp/longjmp exception handling. */
3256 /* Calls to __sjthrow are generated by the compiler when an exception
3257 is raised when using the setjmp/longjmp exception handling codegen
3260 #ifdef DONT_USE_BUILTIN_SETJMP
3261 extern void longjmp (void *, int);
3264 /* Routine to get the head of the current thread's dynamic handler chain
3265 use for exception handling. */
3268 __get_dynamic_handler_chain (void)
3270 struct eh_context *eh = (*get_eh_context) ();
3271 return &eh->dynamic_handler_chain;
3274 /* This is used to throw an exception when the setjmp/longjmp codegen
3275 method is used for exception handling.
3277 We call __terminate if there are no handlers left. Otherwise we run the
3278 cleanup actions off the dynamic cleanup stack, and pop the top of the
3279 dynamic handler chain, and use longjmp to transfer back to the associated
3282 extern void __sjthrow (void) __attribute__ ((__noreturn__));
3287 struct eh_context *eh = (*get_eh_context) ();
3288 void ***dhc = &eh->dynamic_handler_chain;
3290 void (*func)(void *, int);
3292 /* The cleanup chain is one word into the buffer. Get the cleanup chain. */
3293 void ***cleanup = (void***)&(*dhc)[1];
3295 /* If there are any cleanups in the chain, run them now. */
3299 void **buf = (void**)store;
3304 #ifdef DONT_USE_BUILTIN_SETJMP
3305 if (! setjmp (&buf[2]))
3307 if (! __builtin_setjmp (&buf[2]))
3313 func = (void(*)(void*, int))cleanup[0][1];
3314 arg = (void*)cleanup[0][2];
3316 /* Update this before running the cleanup. */
3317 cleanup[0] = (void **)cleanup[0][0];
3330 /* We must call terminate if we try and rethrow an exception, when
3331 there is no exception currently active and when there are no
3333 if (! eh->info || (*dhc)[0] == 0)
3336 /* Find the jmpbuf associated with the top element of the dynamic
3337 handler chain. The jumpbuf starts two words into the buffer. */
3338 jmpbuf = &(*dhc)[2];
3340 /* Then we pop the top element off the dynamic handler chain. */
3341 *dhc = (void**)(*dhc)[0];
3343 /* And then we jump to the handler. */
3345 #ifdef DONT_USE_BUILTIN_SETJMP
3346 longjmp (jmpbuf, 1);
3348 __builtin_longjmp (jmpbuf, 1);
3352 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3353 handler, then pop the handler off the dynamic handler stack, and
3354 then throw. This is used to skip the first handler, and transfer
3355 control to the next handler in the dynamic handler stack. */
3357 extern void __sjpopnthrow (void) __attribute__ ((__noreturn__));
3360 __sjpopnthrow (void)
3362 struct eh_context *eh = (*get_eh_context) ();
3363 void ***dhc = &eh->dynamic_handler_chain;
3364 void (*func)(void *, int);
3366 /* The cleanup chain is one word into the buffer. Get the cleanup chain. */
3367 void ***cleanup = (void***)&(*dhc)[1];
3369 /* If there are any cleanups in the chain, run them now. */
3373 void **buf = (void**)store;
3378 #ifdef DONT_USE_BUILTIN_SETJMP
3379 if (! setjmp (&buf[2]))
3381 if (! __builtin_setjmp (&buf[2]))
3387 func = (void(*)(void*, int))cleanup[0][1];
3388 arg = (void*)cleanup[0][2];
3390 /* Update this before running the cleanup. */
3391 cleanup[0] = (void **)cleanup[0][0];
3404 /* Then we pop the top element off the dynamic handler chain. */
3405 *dhc = (void**)(*dhc)[0];
3410 /* Support code for all exception region-based exception handling. */
3413 __eh_rtime_match (void *rtime)
3416 __eh_matcher matcher;
3419 info = *(__get_eh_info ());
3420 matcher = ((__eh_info *)info)->match_function;
3423 #ifndef inhibit_libc
3424 fprintf (stderr, "Internal Compiler Bug: No runtime type matcher.");
3428 ret = (*matcher) (info, rtime, (void *)0);
3429 return (ret != NULL);
3432 /* This value identifies the place from which an exception is being
3435 #ifdef EH_TABLE_LOOKUP
3441 #ifdef DWARF2_UNWIND_INFO
3443 /* Return the table version of an exception descriptor */
3446 __get_eh_table_version (exception_descriptor *table)
3448 return table->lang.version;
3451 /* Return the originating table language of an exception descriptor */
3454 __get_eh_table_language (exception_descriptor *table)
3456 return table->lang.language;
3459 /* This routine takes a PC and a pointer to the exception region TABLE for
3460 its translation unit, and returns the address of the exception handler
3461 associated with the closest exception table handler entry associated
3462 with that PC, or 0 if there are no table entries the PC fits in.
3464 In the advent of a tie, we have to give the last entry, as it represents
3468 old_find_exception_handler (void *pc, old_exception_table *table)
3475 /* We can't do a binary search because the table isn't guaranteed
3476 to be sorted from function to function. */
3477 for (pos = 0; table[pos].start_region != (void *) -1; ++pos)
3479 if (table[pos].start_region <= pc && table[pos].end_region > pc)
3481 /* This can apply. Make sure it is at least as small as
3482 the previous best. */
3483 if (best == -1 || (table[pos].end_region <= table[best].end_region
3484 && table[pos].start_region >= table[best].start_region))
3487 /* But it is sorted by starting PC within a function. */
3488 else if (best >= 0 && table[pos].start_region > pc)
3492 return table[best].exception_handler;
3498 /* find_exception_handler finds the correct handler, if there is one, to
3499 handle an exception.
3500 returns a pointer to the handler which controlled should be transferred
3501 to, or NULL if there is nothing left.
3503 PC - pc where the exception originates. If this is a rethrow,
3504 then this starts out as a pointer to the exception table
3505 entry we wish to rethrow out of.
3506 TABLE - exception table for the current module.
3507 EH_INFO - eh info pointer for this exception.
3508 RETHROW - 1 if this is a rethrow. (see incoming value of PC).
3509 CLEANUP - returned flag indicating whether this is a cleanup handler.
3512 find_exception_handler (void *pc, exception_descriptor *table,
3513 __eh_info *eh_info, int rethrow, int *cleanup)
3516 void *retval = NULL;
3521 /* The new model assumed the table is sorted inner-most out so the
3522 first region we find which matches is the correct one */
3524 exception_table *tab = &(table->table[0]);
3526 /* Subtract 1 from the PC to avoid hitting the next region */
3529 /* pc is actually the region table entry to rethrow out of */
3530 pos = ((exception_table *) pc) - tab;
3531 pc = ((exception_table *) pc)->end_region - 1;
3533 /* The label is always on the LAST handler entry for a region,
3534 so we know the next entry is a different region, even if the
3535 addresses are the same. Make sure its not end of table tho. */
3536 if (tab[pos].start_region != (void *) -1)
3542 /* We can't do a binary search because the table is in inner-most
3543 to outermost address ranges within functions */
3544 for ( ; tab[pos].start_region != (void *) -1; pos++)
3546 if (tab[pos].start_region <= pc && tab[pos].end_region > pc)
3548 if (tab[pos].match_info)
3550 __eh_matcher matcher = eh_info->match_function;
3551 /* match info but no matcher is NOT a match */
3554 void *ret = (*matcher)((void *) eh_info,
3555 tab[pos].match_info, table);
3559 retval = tab[pos].exception_handler;
3568 retval = tab[pos].exception_handler;
3575 #endif /* DWARF2_UNWIND_INFO */
3576 #endif /* EH_TABLE_LOOKUP */
3578 #ifdef DWARF2_UNWIND_INFO
3579 /* Support code for exception handling using static unwind information. */
3583 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3584 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3585 avoid a warning about casting between int and pointer of different
3588 typedef int ptr_type __attribute__ ((mode (pointer)));
3590 #ifdef INCOMING_REGNO
3591 /* Is the saved value for register REG in frame UDATA stored in a register
3592 window in the previous frame? */
3594 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3595 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3596 compiled functions won't work with the frame-unwind stuff here.
3597 Perhaps the entireity of in_reg_window should be conditional on having
3598 seen a DW_CFA_GNU_window_save? */
3599 #define target_flags 0
3602 in_reg_window (int reg, frame_state *udata)
3604 if (udata->saved[reg] == REG_SAVED_REG)
3605 return INCOMING_REGNO (reg) == reg;
3606 if (udata->saved[reg] != REG_SAVED_OFFSET)
3609 #ifdef STACK_GROWS_DOWNWARD
3610 return udata->reg_or_offset[reg] > 0;
3612 return udata->reg_or_offset[reg] < 0;
3617 in_reg_window (int reg __attribute__ ((__unused__)),
3618 frame_state *udata __attribute__ ((__unused__)))
3622 #endif /* INCOMING_REGNO */
3624 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3625 frame called by UDATA or 0. */
3628 get_reg_addr (unsigned reg, frame_state *udata, frame_state *sub_udata)
3630 while (udata->saved[reg] == REG_SAVED_REG)
3632 reg = udata->reg_or_offset[reg];
3633 if (in_reg_window (reg, udata))
3639 if (udata->saved[reg] == REG_SAVED_OFFSET)
3640 return (word_type *)(udata->cfa + udata->reg_or_offset[reg]);
3645 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3646 frame called by UDATA or 0. */
3648 static inline void *
3649 get_reg (unsigned reg, frame_state *udata, frame_state *sub_udata)
3651 return (void *)(ptr_type) *get_reg_addr (reg, udata, sub_udata);
3654 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3657 put_reg (unsigned reg, void *val, frame_state *udata)
3659 *get_reg_addr (reg, udata, NULL) = (word_type)(ptr_type) val;
3662 /* Copy the saved value for register REG from frame UDATA to frame
3663 TARGET_UDATA. Unlike the previous two functions, this can handle
3664 registers that are not one word large. */
3667 copy_reg (unsigned reg, frame_state *udata, frame_state *target_udata)
3669 word_type *preg = get_reg_addr (reg, udata, NULL);
3670 word_type *ptreg = get_reg_addr (reg, target_udata, NULL);
3672 memcpy (ptreg, preg, dwarf_reg_size_table [reg]);
3675 /* Retrieve the return address for frame UDATA. */
3677 static inline void *
3678 get_return_addr (frame_state *udata, frame_state *sub_udata)
3680 return __builtin_extract_return_addr
3681 (get_reg (udata->retaddr_column, udata, sub_udata));
3684 /* Overwrite the return address for frame UDATA with VAL. */
3687 put_return_addr (void *val, frame_state *udata)
3689 val = __builtin_frob_return_addr (val);
3690 put_reg (udata->retaddr_column, val, udata);
3693 /* Given the current frame UDATA and its return address PC, return the
3694 information about the calling frame in CALLER_UDATA. */
3697 next_stack_level (void *pc, frame_state *udata, frame_state *caller_udata)
3699 caller_udata = __frame_state_for (pc, caller_udata);
3703 /* Now go back to our caller's stack frame. If our caller's CFA register
3704 was saved in our stack frame, restore it; otherwise, assume the CFA
3705 register is SP and restore it to our CFA value. */
3706 if (udata->saved[caller_udata->cfa_reg])
3707 caller_udata->cfa = get_reg (caller_udata->cfa_reg, udata, 0);
3709 caller_udata->cfa = udata->cfa;
3710 caller_udata->cfa += caller_udata->cfa_offset;
3712 return caller_udata;
3715 /* Hook to call before __terminate if only cleanup handlers remain. */
3717 __unwinding_cleanup (void)
3721 /* throw_helper performs some of the common grunt work for a throw. This
3722 routine is called by throw and rethrows. This is pretty much split
3723 out from the old __throw routine. An addition has been added which allows
3724 for a dummy call to a routine __unwinding_cleanup() when there are nothing
3725 but cleanups remaining. This allows a debugger to examine the state
3726 at which the throw was executed, before any cleanups, rather than
3727 at the terminate point after the stack has been unwound.
3729 EH is the current eh_context structure.
3730 PC is the address of the call to __throw.
3731 MY_UDATA is the unwind information for __throw.
3732 OFFSET_P is where we return the SP adjustment offset. */
3735 throw_helper (struct eh_context *eh, void *pc, frame_state *my_udata,
3738 frame_state ustruct2, *udata = &ustruct2;
3739 frame_state ustruct;
3740 frame_state *sub_udata = &ustruct;
3741 void *saved_pc = pc;
3743 void *handler_p = 0;
3745 frame_state saved_ustruct;
3748 int only_cleanup = 0;
3750 int saved_state = 0;
3752 __eh_info *eh_info = (__eh_info *)eh->info;
3754 /* Do we find a handler based on a re-throw PC? */
3755 if (eh->table_index != (void *) 0)
3758 memcpy (udata, my_udata, sizeof (*udata));
3760 handler = (void *) 0;
3763 frame_state *p = udata;
3764 udata = next_stack_level (pc, udata, sub_udata);
3767 /* If we couldn't find the next frame, we lose. */
3771 if (udata->eh_ptr == NULL)
3774 new_eh_model = (((exception_descriptor *)(udata->eh_ptr))->
3775 runtime_id_field == NEW_EH_RUNTIME);
3780 handler = find_exception_handler (eh->table_index, udata->eh_ptr,
3781 eh_info, 1, &cleanup);
3782 eh->table_index = (void *)0;
3786 handler = find_exception_handler (pc, udata->eh_ptr, eh_info,
3789 handler = old_find_exception_handler (pc, udata->eh_ptr);
3791 /* If we found one, we can stop searching, if its not a cleanup.
3792 for cleanups, we save the state, and keep looking. This allows
3793 us to call a debug hook if there are nothing but cleanups left. */
3800 saved_ustruct = *udata;
3801 handler_p = handler;
3814 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3815 hitting the beginning of the next region. */
3816 pc = get_return_addr (udata, sub_udata) - 1;
3821 udata = &saved_ustruct;
3822 handler = handler_p;
3825 __unwinding_cleanup ();
3828 /* If we haven't found a handler by now, this is an unhandled
3833 eh->handler_label = handler;
3835 args_size = udata->args_size;
3838 /* We found a handler in the throw context, no need to unwind. */
3844 /* Unwind all the frames between this one and the handler by copying
3845 their saved register values into our register save slots. */
3847 /* Remember the PC where we found the handler. */
3848 void *handler_pc = pc;
3850 /* Start from the throw context again. */
3852 memcpy (udata, my_udata, sizeof (*udata));
3854 while (pc != handler_pc)
3856 frame_state *p = udata;
3857 udata = next_stack_level (pc, udata, sub_udata);
3860 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
3861 if (i != udata->retaddr_column && udata->saved[i])
3863 /* If you modify the saved value of the return address
3864 register on the SPARC, you modify the return address for
3865 your caller's frame. Don't do that here, as it will
3866 confuse get_return_addr. */
3867 if (in_reg_window (i, udata)
3868 && udata->saved[udata->retaddr_column] == REG_SAVED_REG
3869 && udata->reg_or_offset[udata->retaddr_column] == i)
3871 copy_reg (i, udata, my_udata);
3874 pc = get_return_addr (udata, sub_udata) - 1;
3877 /* But we do need to update the saved return address register from
3878 the last frame we unwind, or the handler frame will have the wrong
3880 if (udata->saved[udata->retaddr_column] == REG_SAVED_REG)
3882 i = udata->reg_or_offset[udata->retaddr_column];
3883 if (in_reg_window (i, udata))
3884 copy_reg (i, udata, my_udata);
3887 /* udata now refers to the frame called by the handler frame. */
3889 /* We adjust SP by the difference between __throw's CFA and the CFA for
3890 the frame called by the handler frame, because those CFAs correspond
3891 to the SP values at the two call sites. We need to further adjust by
3892 the args_size of the handler frame itself to get the handler frame's
3893 SP from before the args were pushed for that call. */
3894 #ifdef STACK_GROWS_DOWNWARD
3895 *offset_p = udata->cfa - my_udata->cfa + args_size;
3897 *offset_p = my_udata->cfa - udata->cfa - args_size;
3904 /* We first search for an exception handler, and if we don't find
3905 it, we call __terminate on the current stack frame so that we may
3906 use the debugger to walk the stack and understand why no handler
3909 If we find one, then we unwind the frames down to the one that
3910 has the handler and transfer control into the handler. */
3912 /*extern void __throw(void) __attribute__ ((__noreturn__));*/
3917 struct eh_context *eh = (*get_eh_context) ();
3921 /* XXX maybe make my_ustruct static so we don't have to look it up for
3923 frame_state my_ustruct, *my_udata = &my_ustruct;
3925 /* This is required for C++ semantics. We must call terminate if we
3926 try and rethrow an exception, when there is no exception currently
3931 /* Start at our stack frame. */
3933 my_udata = __frame_state_for (&&label, my_udata);
3937 /* We need to get the value from the CFA register. */
3938 my_udata->cfa = __builtin_dwarf_cfa ();
3940 /* Do any necessary initialization to access arbitrary stack frames.
3941 On the SPARC, this means flushing the register windows. */
3942 __builtin_unwind_init ();
3944 /* Now reset pc to the right throw point. */
3945 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3947 handler = throw_helper (eh, pc, my_udata, &offset);
3951 __builtin_eh_return ((void *)eh, offset, handler);
3953 /* Epilogue: restore the handler frame's register values and return
3957 /*extern void __rethrow(void *) __attribute__ ((__noreturn__));*/
3960 __rethrow (void *index)
3962 struct eh_context *eh = (*get_eh_context) ();
3966 /* XXX maybe make my_ustruct static so we don't have to look it up for
3968 frame_state my_ustruct, *my_udata = &my_ustruct;
3970 /* This is required for C++ semantics. We must call terminate if we
3971 try and rethrow an exception, when there is no exception currently
3976 /* This is the table index we want to rethrow from. The value of
3977 the END_REGION label is used for the PC of the throw, and the
3978 search begins with the next table entry. */
3979 eh->table_index = index;
3981 /* Start at our stack frame. */
3983 my_udata = __frame_state_for (&&label, my_udata);
3987 /* We need to get the value from the CFA register. */
3988 my_udata->cfa = __builtin_dwarf_cfa ();
3990 /* Do any necessary initialization to access arbitrary stack frames.
3991 On the SPARC, this means flushing the register windows. */
3992 __builtin_unwind_init ();
3994 /* Now reset pc to the right throw point. */
3995 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3997 handler = throw_helper (eh, pc, my_udata, &offset);
4001 __builtin_eh_return ((void *)eh, offset, handler);
4003 /* Epilogue: restore the handler frame's register values and return
4006 #endif /* DWARF2_UNWIND_INFO */
4011 #ifndef inhibit_libc
4012 /* This gets us __GNU_LIBRARY__. */
4013 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
4016 #ifdef __GNU_LIBRARY__
4017 /* Avoid forcing the library's meaning of `write' on the user program
4018 by using the "internal" name (for use within the library) */
4019 #define write(fd, buf, n) __write((fd), (buf), (n))
4021 #endif /* inhibit_libc */
4023 #define MESSAGE "pure virtual method called\n"
4025 extern void __terminate (void) __attribute__ ((__noreturn__));
4028 __pure_virtual (void)
4030 #ifndef inhibit_libc
4031 write (2, MESSAGE, sizeof (MESSAGE) - 1);