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;
678 UDItype __udivmoddi4 ();
681 __divdi3 (DItype u, DItype v)
692 uu.ll = __negdi2 (uu.ll);
695 vv.ll = __negdi2 (vv.ll);
697 w = __udivmoddi4 (uu.ll, vv.ll, (UDItype *) 0);
706 UDItype __udivmoddi4 ();
708 __moddi3 (DItype u, DItype v)
719 uu.ll = __negdi2 (uu.ll);
721 vv.ll = __negdi2 (vv.ll);
723 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
732 UDItype __udivmoddi4 ();
734 __umoddi3 (UDItype u, UDItype v)
738 (void) __udivmoddi4 (u, v, &w);
745 UDItype __udivmoddi4 ();
747 __udivdi3 (UDItype n, UDItype d)
749 return __udivmoddi4 (n, d, (UDItype *) 0);
755 __cmpdi2 (DItype a, DItype b)
759 au.ll = a, bu.ll = b;
761 if (au.s.high < bu.s.high)
763 else if (au.s.high > bu.s.high)
765 if ((USItype) au.s.low < (USItype) bu.s.low)
767 else if ((USItype) au.s.low > (USItype) bu.s.low)
775 __ucmpdi2 (DItype a, DItype b)
779 au.ll = a, bu.ll = b;
781 if ((USItype) au.s.high < (USItype) bu.s.high)
783 else if ((USItype) au.s.high > (USItype) bu.s.high)
785 if ((USItype) au.s.low < (USItype) bu.s.low)
787 else if ((USItype) au.s.low > (USItype) bu.s.low)
793 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
794 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
795 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
798 __fixunstfdi (TFtype a)
806 /* Compute high word of result, as a flonum. */
807 b = (a / HIGH_WORD_COEFF);
808 /* Convert that to fixed (but not to DItype!),
809 and shift it into the high word. */
812 /* Remove high part from the TFtype, leaving the low part as flonum. */
814 /* Convert that to fixed (but not to DItype!) and add it in.
815 Sometimes A comes out negative. This is significant, since
816 A has more bits than a long int does. */
818 v -= (USItype) (- a);
825 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
830 return - __fixunstfdi (-a);
831 return __fixunstfdi (a);
835 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
836 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
837 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
840 __fixunsxfdi (XFtype a)
848 /* Compute high word of result, as a flonum. */
849 b = (a / HIGH_WORD_COEFF);
850 /* Convert that to fixed (but not to DItype!),
851 and shift it into the high word. */
854 /* Remove high part from the XFtype, leaving the low part as flonum. */
856 /* Convert that to fixed (but not to DItype!) and add it in.
857 Sometimes A comes out negative. This is significant, since
858 A has more bits than a long int does. */
860 v -= (USItype) (- a);
867 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
872 return - __fixunsxfdi (-a);
873 return __fixunsxfdi (a);
878 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
879 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
882 __fixunsdfdi (DFtype a)
890 /* Compute high word of result, as a flonum. */
891 b = (a / HIGH_WORD_COEFF);
892 /* Convert that to fixed (but not to DItype!),
893 and shift it into the high word. */
896 /* Remove high part from the DFtype, leaving the low part as flonum. */
898 /* Convert that to fixed (but not to DItype!) and add it in.
899 Sometimes A comes out negative. This is significant, since
900 A has more bits than a long int does. */
902 v -= (USItype) (- a);
914 return - __fixunsdfdi (-a);
915 return __fixunsdfdi (a);
920 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
921 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
924 __fixunssfdi (SFtype original_a)
926 /* Convert the SFtype to a DFtype, because that is surely not going
927 to lose any bits. Some day someone else can write a faster version
928 that avoids converting to DFtype, and verify it really works right. */
929 DFtype a = original_a;
936 /* Compute high word of result, as a flonum. */
937 b = (a / HIGH_WORD_COEFF);
938 /* Convert that to fixed (but not to DItype!),
939 and shift it into the high word. */
942 /* Remove high part from the DFtype, leaving the low part as flonum. */
944 /* Convert that to fixed (but not to DItype!) and add it in.
945 Sometimes A comes out negative. This is significant, since
946 A has more bits than a long int does. */
948 v -= (USItype) (- a);
960 return - __fixunssfdi (-a);
961 return __fixunssfdi (a);
965 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
966 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
967 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
968 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
971 __floatdixf (DItype u)
975 d = (SItype) (u >> WORD_SIZE);
976 d *= HIGH_HALFWORD_COEFF;
977 d *= HIGH_HALFWORD_COEFF;
978 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
984 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
985 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
986 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
987 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
990 __floatditf (DItype u)
994 d = (SItype) (u >> WORD_SIZE);
995 d *= HIGH_HALFWORD_COEFF;
996 d *= HIGH_HALFWORD_COEFF;
997 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
1004 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1005 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1006 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1009 __floatdidf (DItype u)
1013 d = (SItype) (u >> WORD_SIZE);
1014 d *= HIGH_HALFWORD_COEFF;
1015 d *= HIGH_HALFWORD_COEFF;
1016 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
1023 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1024 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1025 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1026 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1028 /* Define codes for all the float formats that we know of. Note
1029 that this is copied from real.h. */
1031 #define UNKNOWN_FLOAT_FORMAT 0
1032 #define IEEE_FLOAT_FORMAT 1
1033 #define VAX_FLOAT_FORMAT 2
1034 #define IBM_FLOAT_FORMAT 3
1036 /* Default to IEEE float if not specified. Nearly all machines use it. */
1037 #ifndef HOST_FLOAT_FORMAT
1038 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1041 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1046 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1051 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1057 __floatdisf (DItype u)
1059 /* Do the calculation in DFmode
1060 so that we don't lose any of the precision of the high word
1061 while multiplying it. */
1064 /* Protect against double-rounding error.
1065 Represent any low-order bits, that might be truncated in DFmode,
1066 by a bit that won't be lost. The bit can go in anywhere below the
1067 rounding position of the SFmode. A fixed mask and bit position
1068 handles all usual configurations. It doesn't handle the case
1069 of 128-bit DImode, however. */
1070 if (DF_SIZE < DI_SIZE
1071 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1073 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1074 if (! (- ((DItype) 1 << DF_SIZE) < u
1075 && u < ((DItype) 1 << DF_SIZE)))
1077 if ((USItype) u & (REP_BIT - 1))
1081 f = (SItype) (u >> WORD_SIZE);
1082 f *= HIGH_HALFWORD_COEFF;
1083 f *= HIGH_HALFWORD_COEFF;
1084 f += (USItype) (u & (HIGH_WORD_COEFF - 1));
1090 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1091 /* Reenable the normal types, in case limits.h needs them. */
1104 __fixunsxfsi (XFtype a)
1106 if (a >= - (DFtype) LONG_MIN)
1107 return (SItype) (a + LONG_MIN) - LONG_MIN;
1113 /* Reenable the normal types, in case limits.h needs them. */
1126 __fixunsdfsi (DFtype a)
1128 if (a >= - (DFtype) LONG_MIN)
1129 return (SItype) (a + LONG_MIN) - LONG_MIN;
1135 /* Reenable the normal types, in case limits.h needs them. */
1148 __fixunssfsi (SFtype a)
1150 if (a >= - (SFtype) LONG_MIN)
1151 return (SItype) (a + LONG_MIN) - LONG_MIN;
1156 /* From here on down, the routines use normal data types. */
1158 #define SItype bogus_type
1159 #define USItype bogus_type
1160 #define DItype bogus_type
1161 #define UDItype bogus_type
1162 #define SFtype bogus_type
1163 #define DFtype bogus_type
1175 /* Like bcmp except the sign is meaningful.
1176 Result is negative if S1 is less than S2,
1177 positive if S1 is greater, 0 if S1 and S2 are equal. */
1180 __gcc_bcmp (unsigned char *s1, unsigned char *s2, size_t size)
1184 unsigned char c1 = *s1++, c2 = *s2++;
1201 #if defined(__svr4__) || defined(__alliant__)
1205 /* The Alliant needs the added underscore. */
1206 asm (".globl __builtin_saveregs");
1207 asm ("__builtin_saveregs:");
1208 asm (".globl ___builtin_saveregs");
1209 asm ("___builtin_saveregs:");
1211 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1212 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1213 area and also for a new va_list
1215 /* Save all argument registers in the arg reg save area. The
1216 arg reg save area must have the following layout (according
1228 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1229 asm (" fst.q %f12,16(%sp)");
1231 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1232 asm (" st.l %r17,36(%sp)");
1233 asm (" st.l %r18,40(%sp)");
1234 asm (" st.l %r19,44(%sp)");
1235 asm (" st.l %r20,48(%sp)");
1236 asm (" st.l %r21,52(%sp)");
1237 asm (" st.l %r22,56(%sp)");
1238 asm (" st.l %r23,60(%sp)");
1239 asm (" st.l %r24,64(%sp)");
1240 asm (" st.l %r25,68(%sp)");
1241 asm (" st.l %r26,72(%sp)");
1242 asm (" st.l %r27,76(%sp)");
1244 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1245 va_list structure. Put in into
1246 r16 so that it will be returned
1249 /* Initialize all fields of the new va_list structure. This
1250 structure looks like:
1253 unsigned long ireg_used;
1254 unsigned long freg_used;
1260 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1261 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1262 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1263 asm (" bri %r1"); /* delayed return */
1264 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1266 #else /* not __svr4__ */
1267 #if defined(__PARAGON__)
1269 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1270 * and we stand a better chance of hooking into libraries
1271 * compiled by PGI. [andyp@ssd.intel.com]
1275 asm (".globl __builtin_saveregs");
1276 asm ("__builtin_saveregs:");
1277 asm (".globl ___builtin_saveregs");
1278 asm ("___builtin_saveregs:");
1280 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1281 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1282 area and also for a new va_list
1284 /* Save all argument registers in the arg reg save area. The
1285 arg reg save area must have the following layout (according
1297 asm (" fst.q f8, 0(sp)");
1298 asm (" fst.q f12,16(sp)");
1299 asm (" st.l r16,32(sp)");
1300 asm (" st.l r17,36(sp)");
1301 asm (" st.l r18,40(sp)");
1302 asm (" st.l r19,44(sp)");
1303 asm (" st.l r20,48(sp)");
1304 asm (" st.l r21,52(sp)");
1305 asm (" st.l r22,56(sp)");
1306 asm (" st.l r23,60(sp)");
1307 asm (" st.l r24,64(sp)");
1308 asm (" st.l r25,68(sp)");
1309 asm (" st.l r26,72(sp)");
1310 asm (" st.l r27,76(sp)");
1312 asm (" adds 80,sp,r16"); /* compute the address of the new
1313 va_list structure. Put in into
1314 r16 so that it will be returned
1317 /* Initialize all fields of the new va_list structure. This
1318 structure looks like:
1321 unsigned long ireg_used;
1322 unsigned long freg_used;
1328 asm (" st.l r0, 0(r16)"); /* nfixed */
1329 asm (" st.l r0, 4(r16)"); /* nfloating */
1330 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1331 asm (" bri r1"); /* delayed return */
1332 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1333 #else /* not __PARAGON__ */
1337 asm (".globl ___builtin_saveregs");
1338 asm ("___builtin_saveregs:");
1339 asm (" mov sp,r30");
1340 asm (" andnot 0x0f,sp,sp");
1341 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1343 /* Fill in the __va_struct. */
1344 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1345 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1346 asm (" st.l r18, 8(sp)");
1347 asm (" st.l r19,12(sp)");
1348 asm (" st.l r20,16(sp)");
1349 asm (" st.l r21,20(sp)");
1350 asm (" st.l r22,24(sp)");
1351 asm (" st.l r23,28(sp)");
1352 asm (" st.l r24,32(sp)");
1353 asm (" st.l r25,36(sp)");
1354 asm (" st.l r26,40(sp)");
1355 asm (" st.l r27,44(sp)");
1357 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1358 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1360 /* Fill in the __va_ctl. */
1361 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1362 asm (" st.l r28,84(sp)"); /* pointer to more args */
1363 asm (" st.l r0, 88(sp)"); /* nfixed */
1364 asm (" st.l r0, 92(sp)"); /* nfloating */
1366 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1368 asm (" mov r30,sp");
1369 /* recover stack and pass address to start
1371 #endif /* not __PARAGON__ */
1372 #endif /* not __svr4__ */
1373 #else /* not __i860__ */
1375 asm (".global __builtin_saveregs");
1376 asm ("__builtin_saveregs:");
1377 asm (".global ___builtin_saveregs");
1378 asm ("___builtin_saveregs:");
1379 #ifdef NEED_PROC_COMMAND
1382 asm ("st %i0,[%fp+68]");
1383 asm ("st %i1,[%fp+72]");
1384 asm ("st %i2,[%fp+76]");
1385 asm ("st %i3,[%fp+80]");
1386 asm ("st %i4,[%fp+84]");
1388 asm ("st %i5,[%fp+88]");
1389 #ifdef NEED_TYPE_COMMAND
1390 asm (".type __builtin_saveregs,#function");
1391 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1393 #else /* not __sparc__ */
1394 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1398 asm (" .set nomips16");
1400 asm (" .ent __builtin_saveregs");
1401 asm (" .globl __builtin_saveregs");
1402 asm ("__builtin_saveregs:");
1403 asm (" sw $4,0($30)");
1404 asm (" sw $5,4($30)");
1405 asm (" sw $6,8($30)");
1406 asm (" sw $7,12($30)");
1408 asm (" .end __builtin_saveregs");
1409 #else /* not __mips__, etc. */
1412 __builtin_saveregs ()
1417 #endif /* not __mips__ */
1418 #endif /* not __sparc__ */
1419 #endif /* not __i860__ */
1423 #ifndef inhibit_libc
1425 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1427 /* This is used by the `assert' macro. */
1428 extern void __eprintf (const char *, const char *, unsigned int, const char *)
1429 __attribute__ ((__noreturn__));
1432 __eprintf (const char *string, const char *expression,
1433 unsigned int line, const char *filename)
1435 fprintf (stderr, string, expression, line, filename);
1445 /* Structure emitted by -a */
1449 const char *filename;
1453 const unsigned long *addresses;
1455 /* Older GCC's did not emit these fields. */
1457 const char **functions;
1458 const long *line_nums;
1459 const char **filenames;
1463 #ifdef BLOCK_PROFILER_CODE
1466 #ifndef inhibit_libc
1468 /* Simple minded basic block profiling output dumper for
1469 systems that don't provide tcov support. At present,
1470 it requires atexit and stdio. */
1472 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1476 #include "gbl-ctors.h"
1477 #include "gcov-io.h"
1480 static struct bb *bb_head;
1482 static int num_digits (long value, int base) __attribute__ ((const));
1484 /* Return the number of digits needed to print a value */
1485 /* __inline__ */ static int num_digits (long value, int base)
1487 int minus = (value < 0 && base != 16);
1488 unsigned long v = (minus) ? -value : value;
1502 __bb_exit_func (void)
1504 FILE *da_file, *file;
1511 i = strlen (bb_head->filename) - 3;
1513 if (!strcmp (bb_head->filename+i, ".da"))
1515 /* Must be -fprofile-arcs not -a.
1516 Dump data in a form that gcov expects. */
1520 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1522 /* If the file exists, and the number of counts in it is the same,
1523 then merge them in. */
1525 if ((da_file = fopen (ptr->filename, "rb")) != 0)
1529 if (__read_long (&n_counts, da_file, 8) != 0)
1531 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1536 if (n_counts == ptr->ncounts)
1540 for (i = 0; i < n_counts; i++)
1544 if (__read_long (&v, da_file, 8) != 0)
1546 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1550 ptr->counts[i] += v;
1554 if (fclose (da_file) == EOF)
1555 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1558 if ((da_file = fopen (ptr->filename, "wb")) == 0)
1560 fprintf (stderr, "arc profiling: Can't open output file %s.\n",
1565 /* ??? Should first write a header to the file. Preferably, a 4 byte
1566 magic number, 4 bytes containing the time the program was
1567 compiled, 4 bytes containing the last modification time of the
1568 source file, and 4 bytes indicating the compiler options used.
1570 That way we can easily verify that the proper source/executable/
1571 data file combination is being used from gcov. */
1573 if (__write_long (ptr->ncounts, da_file, 8) != 0)
1576 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1582 long *count_ptr = ptr->counts;
1584 for (j = ptr->ncounts; j > 0; j--)
1586 if (__write_long (*count_ptr, da_file, 8) != 0)
1594 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1598 if (fclose (da_file) == EOF)
1599 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1606 /* Must be basic block profiling. Emit a human readable output file. */
1608 file = fopen ("bb.out", "a");
1617 /* This is somewhat type incorrect, but it avoids worrying about
1618 exactly where time.h is included from. It should be ok unless
1619 a void * differs from other pointer formats, or if sizeof (long)
1620 is < sizeof (time_t). It would be nice if we could assume the
1621 use of rationale standards here. */
1623 time ((void *) &time_value);
1624 fprintf (file, "Basic block profiling finished on %s\n", ctime ((void *) &time_value));
1626 /* We check the length field explicitly in order to allow compatibility
1627 with older GCC's which did not provide it. */
1629 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1632 int func_p = (ptr->nwords >= sizeof (struct bb)
1633 && ptr->nwords <= 1000
1635 int line_p = (func_p && ptr->line_nums);
1636 int file_p = (func_p && ptr->filenames);
1637 int addr_p = (ptr->addresses != 0);
1638 long ncounts = ptr->ncounts;
1644 int blk_len = num_digits (ncounts, 10);
1649 fprintf (file, "File %s, %ld basic blocks \n\n",
1650 ptr->filename, ncounts);
1652 /* Get max values for each field. */
1653 for (i = 0; i < ncounts; i++)
1658 if (cnt_max < ptr->counts[i])
1659 cnt_max = ptr->counts[i];
1661 if (addr_p && addr_max < ptr->addresses[i])
1662 addr_max = ptr->addresses[i];
1664 if (line_p && line_max < ptr->line_nums[i])
1665 line_max = ptr->line_nums[i];
1669 p = (ptr->functions[i]) ? (ptr->functions[i]) : "<none>";
1677 p = (ptr->filenames[i]) ? (ptr->filenames[i]) : "<none>";
1684 addr_len = num_digits (addr_max, 16);
1685 cnt_len = num_digits (cnt_max, 10);
1686 line_len = num_digits (line_max, 10);
1688 /* Now print out the basic block information. */
1689 for (i = 0; i < ncounts; i++)
1692 " Block #%*d: executed %*ld time(s)",
1694 cnt_len, ptr->counts[i]);
1697 fprintf (file, " address= 0x%.*lx", addr_len,
1701 fprintf (file, " function= %-*s", func_len,
1702 (ptr->functions[i]) ? ptr->functions[i] : "<none>");
1705 fprintf (file, " line= %*ld", line_len, ptr->line_nums[i]);
1708 fprintf (file, " file= %s",
1709 (ptr->filenames[i]) ? ptr->filenames[i] : "<none>");
1711 fprintf (file, "\n");
1714 fprintf (file, "\n");
1718 fprintf (file, "\n\n");
1724 __bb_init_func (struct bb *blocks)
1726 /* User is supposed to check whether the first word is non-0,
1727 but just in case.... */
1729 if (blocks->zero_word)
1732 /* Initialize destructor. */
1734 atexit (__bb_exit_func);
1736 /* Set up linked list. */
1737 blocks->zero_word = 1;
1738 blocks->next = bb_head;
1742 #ifndef MACHINE_STATE_SAVE
1743 #define MACHINE_STATE_SAVE(ID)
1745 #ifndef MACHINE_STATE_RESTORE
1746 #define MACHINE_STATE_RESTORE(ID)
1749 /* Number of buckets in hashtable of basic block addresses. */
1751 #define BB_BUCKETS 311
1753 /* Maximum length of string in file bb.in. */
1755 #define BBINBUFSIZE 500
1757 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1758 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1760 #define BBINBUFSIZESTR "499"
1764 struct bb_edge *next;
1765 unsigned long src_addr;
1766 unsigned long dst_addr;
1767 unsigned long count;
1772 TRACE_KEEP = 0, TRACE_ON = 1, TRACE_OFF = 2
1777 struct bb_func *next;
1780 enum bb_func_mode mode;
1783 /* This is the connection to the outside world.
1784 The BLOCK_PROFILER macro must set __bb.blocks
1785 and __bb.blockno. */
1788 unsigned long blockno;
1792 /* Vars to store addrs of source and destination basic blocks
1795 static unsigned long bb_src = 0;
1796 static unsigned long bb_dst = 0;
1798 static FILE *bb_tracefile = (FILE *) 0;
1799 static struct bb_edge **bb_hashbuckets = (struct bb_edge **) 0;
1800 static struct bb_func *bb_func_head = (struct bb_func *) 0;
1801 static unsigned long bb_callcount = 0;
1802 static int bb_mode = 0;
1804 static unsigned long *bb_stack = (unsigned long *) 0;
1805 static size_t bb_stacksize = 0;
1807 static int reported = 0;
1810 Always : Print execution frequencies of basic blocks
1812 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1813 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1814 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1815 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1820 /*#include <sys/types.h>*/
1821 #include <sys/stat.h>
1822 /*#include <malloc.h>*/
1824 /* Commands executed by gopen. */
1826 #define GOPENDECOMPRESS "gzip -cd "
1827 #define GOPENCOMPRESS "gzip -c >"
1829 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1830 If it does not compile, simply replace gopen by fopen and delete
1831 '.gz' from any first parameter to gopen. */
1834 gopen (char *fn, char *mode)
1842 if (mode[0] != 'r' && mode[0] != 'w')
1845 p = fn + strlen (fn)-1;
1846 use_gzip = ((p[-1] == '.' && (p[0] == 'Z' || p[0] == 'z'))
1847 || (p[-2] == '.' && p[-1] == 'g' && p[0] == 'z'));
1854 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1855 + sizeof (GOPENDECOMPRESS));
1856 strcpy (s, GOPENDECOMPRESS);
1857 strcpy (s + (sizeof (GOPENDECOMPRESS)-1), fn);
1858 f = popen (s, mode);
1866 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1867 + sizeof (GOPENCOMPRESS));
1868 strcpy (s, GOPENCOMPRESS);
1869 strcpy (s + (sizeof (GOPENCOMPRESS)-1), fn);
1870 if (!(f = popen (s, mode)))
1871 f = fopen (s, mode);
1878 return fopen (fn, mode);
1888 if (!fstat (fileno (f), &buf) && S_ISFIFO (buf.st_mode))
1896 #endif /* HAVE_POPEN */
1898 /* Called once per program. */
1901 __bb_exit_trace_func ()
1903 FILE *file = fopen ("bb.out", "a");
1916 gclose (bb_tracefile);
1918 fclose (bb_tracefile);
1919 #endif /* HAVE_POPEN */
1922 /* Check functions in `bb.in'. */
1927 const struct bb_func *p;
1928 int printed_something = 0;
1932 /* This is somewhat type incorrect. */
1933 time ((void *) &time_value);
1935 for (p = bb_func_head; p != (struct bb_func *) 0; p = p->next)
1937 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1939 if (!ptr->filename || (p->filename != (char *) 0 && strcmp (p->filename, ptr->filename)))
1941 for (blk = 0; blk < ptr->ncounts; blk++)
1943 if (!strcmp (p->funcname, ptr->functions[blk]))
1948 if (!printed_something)
1950 fprintf (file, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value));
1951 printed_something = 1;
1954 fprintf (file, "\tFunction %s", p->funcname);
1956 fprintf (file, " of file %s", p->filename);
1957 fprintf (file, "\n" );
1962 if (printed_something)
1963 fprintf (file, "\n");
1969 if (!bb_hashbuckets)
1973 fprintf (stderr, "Profiler: out of memory\n");
1983 unsigned long addr_max = 0;
1984 unsigned long cnt_max = 0;
1988 /* This is somewhat type incorrect, but it avoids worrying about
1989 exactly where time.h is included from. It should be ok unless
1990 a void * differs from other pointer formats, or if sizeof (long)
1991 is < sizeof (time_t). It would be nice if we could assume the
1992 use of rationale standards here. */
1994 time ((void *) &time_value);
1995 fprintf (file, "Basic block jump tracing");
1997 switch (bb_mode & 12)
2000 fprintf (file, " (with call)");
2004 /* Print nothing. */
2008 fprintf (file, " (with call & ret)");
2012 fprintf (file, " (with ret)");
2016 fprintf (file, " finished on %s\n", ctime ((void *) &time_value));
2018 for (i = 0; i < BB_BUCKETS; i++)
2020 struct bb_edge *bucket = bb_hashbuckets[i];
2021 for ( ; bucket; bucket = bucket->next )
2023 if (addr_max < bucket->src_addr)
2024 addr_max = bucket->src_addr;
2025 if (addr_max < bucket->dst_addr)
2026 addr_max = bucket->dst_addr;
2027 if (cnt_max < bucket->count)
2028 cnt_max = bucket->count;
2031 addr_len = num_digits (addr_max, 16);
2032 cnt_len = num_digits (cnt_max, 10);
2034 for ( i = 0; i < BB_BUCKETS; i++)
2036 struct bb_edge *bucket = bb_hashbuckets[i];
2037 for ( ; bucket; bucket = bucket->next )
2039 fprintf (file, "Jump from block 0x%.*lx to "
2040 "block 0x%.*lx executed %*lu time(s)\n",
2041 addr_len, bucket->src_addr,
2042 addr_len, bucket->dst_addr,
2043 cnt_len, bucket->count);
2047 fprintf (file, "\n");
2055 /* Free allocated memory. */
2060 struct bb_func *old = f;
2063 if (old->funcname) free (old->funcname);
2064 if (old->filename) free (old->filename);
2075 for (i = 0; i < BB_BUCKETS; i++)
2077 struct bb_edge *old, *bucket = bb_hashbuckets[i];
2082 bucket = bucket->next;
2086 free (bb_hashbuckets);
2089 for (b = bb_head; b; b = b->next)
2090 if (b->flags) free (b->flags);
2093 /* Called once per program. */
2099 char buf[BBINBUFSIZE];
2102 enum bb_func_mode m;
2105 /* Initialize destructor. */
2106 atexit (__bb_exit_func);
2108 if (!(file = fopen ("bb.in", "r")))
2111 while(fscanf (file, " %" BBINBUFSIZESTR "s ", buf) != EOF)
2123 if (!strcmp (p, "__bb_trace__"))
2125 else if (!strcmp (p, "__bb_jumps__"))
2127 else if (!strcmp (p, "__bb_hidecall__"))
2129 else if (!strcmp (p, "__bb_showret__"))
2133 struct bb_func *f = (struct bb_func *) malloc (sizeof (struct bb_func));
2137 f->next = bb_func_head;
2138 if ((pos = strchr (p, ':')))
2140 if (!(f->funcname = (char *) malloc (strlen (pos+1)+1)))
2142 strcpy (f->funcname, pos+1);
2144 if ((f->filename = (char *) malloc (l+1)))
2146 strncpy (f->filename, p, l);
2147 f->filename[l] = '\0';
2150 f->filename = (char *) 0;
2154 if (!(f->funcname = (char *) malloc (strlen (p)+1)))
2156 strcpy (f->funcname, p);
2157 f->filename = (char *) 0;
2169 bb_tracefile = gopen ("bbtrace.gz", "w");
2174 bb_tracefile = fopen ("bbtrace", "w");
2176 #endif /* HAVE_POPEN */
2180 bb_hashbuckets = (struct bb_edge **)
2181 malloc (BB_BUCKETS * sizeof (struct bb_edge *));
2183 /* Use a loop here rather than calling bzero to avoid having to
2184 conditionalize its existance. */
2185 for (i = 0; i < BB_BUCKETS; i++)
2186 bb_hashbuckets[i] = 0;
2192 bb_stack = (unsigned long *) malloc (bb_stacksize * sizeof (*bb_stack));
2195 /* Initialize destructor. */
2196 atexit (__bb_exit_trace_func);
2199 /* Called upon entering a basic block. */
2204 struct bb_edge *bucket;
2206 MACHINE_STATE_SAVE("1")
2208 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2211 bb_dst = __bb.blocks->addresses[__bb.blockno];
2212 __bb.blocks->counts[__bb.blockno]++;
2216 fwrite (&bb_dst, sizeof (unsigned long), 1, bb_tracefile);
2221 struct bb_edge **startbucket, **oldnext;
2223 oldnext = startbucket
2224 = & bb_hashbuckets[ (((int) bb_src*8) ^ (int) bb_dst) % BB_BUCKETS ];
2225 bucket = *startbucket;
2227 for (bucket = *startbucket; bucket;
2228 oldnext = &(bucket->next), bucket = *oldnext)
2230 if (bucket->src_addr == bb_src
2231 && bucket->dst_addr == bb_dst)
2234 *oldnext = bucket->next;
2235 bucket->next = *startbucket;
2236 *startbucket = bucket;
2241 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2247 fprintf (stderr, "Profiler: out of memory\n");
2254 bucket->src_addr = bb_src;
2255 bucket->dst_addr = bb_dst;
2256 bucket->next = *startbucket;
2257 *startbucket = bucket;
2268 MACHINE_STATE_RESTORE("1")
2272 /* Called when returning from a function and `__bb_showret__' is set. */
2275 __bb_trace_func_ret ()
2277 struct bb_edge *bucket;
2279 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2284 struct bb_edge **startbucket, **oldnext;
2286 oldnext = startbucket
2287 = & bb_hashbuckets[ (((int) bb_dst * 8) ^ (int) bb_src) % BB_BUCKETS ];
2288 bucket = *startbucket;
2290 for (bucket = *startbucket; bucket;
2291 oldnext = &(bucket->next), bucket = *oldnext)
2293 if (bucket->src_addr == bb_dst
2294 && bucket->dst_addr == bb_src)
2297 *oldnext = bucket->next;
2298 bucket->next = *startbucket;
2299 *startbucket = bucket;
2304 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2310 fprintf (stderr, "Profiler: out of memory\n");
2317 bucket->src_addr = bb_dst;
2318 bucket->dst_addr = bb_src;
2319 bucket->next = *startbucket;
2320 *startbucket = bucket;
2333 /* Called upon entering the first function of a file. */
2336 __bb_init_file (struct bb *blocks)
2339 const struct bb_func *p;
2340 long blk, ncounts = blocks->ncounts;
2341 const char **functions = blocks->functions;
2343 /* Set up linked list. */
2344 blocks->zero_word = 1;
2345 blocks->next = bb_head;
2350 || !(blocks->flags = (char *) malloc (sizeof (char) * blocks->ncounts)))
2353 for (blk = 0; blk < ncounts; blk++)
2354 blocks->flags[blk] = 0;
2356 for (blk = 0; blk < ncounts; blk++)
2358 for (p = bb_func_head; p; p = p->next)
2360 if (!strcmp (p->funcname, functions[blk])
2361 && (!p->filename || !strcmp (p->filename, blocks->filename)))
2363 blocks->flags[blk] |= p->mode;
2370 /* Called when exiting from a function. */
2376 MACHINE_STATE_SAVE("2")
2380 if ((bb_mode & 12) && bb_stacksize > bb_callcount)
2382 bb_src = bb_stack[bb_callcount];
2384 __bb_trace_func_ret ();
2390 MACHINE_STATE_RESTORE("2")
2394 /* Called when entering a function. */
2397 __bb_init_trace_func (struct bb *blocks, unsigned long blockno)
2399 static int trace_init = 0;
2401 MACHINE_STATE_SAVE("3")
2403 if (!blocks->zero_word)
2410 __bb_init_file (blocks);
2420 if (bb_callcount >= bb_stacksize)
2422 size_t newsize = bb_callcount + 100;
2424 bb_stack = (unsigned long *) realloc (bb_stack, newsize);
2429 fprintf (stderr, "Profiler: out of memory\n");
2433 goto stack_overflow;
2435 bb_stacksize = newsize;
2437 bb_stack[bb_callcount] = bb_src;
2448 else if (blocks->flags && (blocks->flags[blockno] & TRACE_ON))
2454 bb_stack[bb_callcount] = bb_src;
2457 MACHINE_STATE_RESTORE("3")
2460 #endif /* not inhibit_libc */
2461 #endif /* not BLOCK_PROFILER_CODE */
2465 unsigned int __shtab[] = {
2466 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2467 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2468 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2469 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2470 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2471 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2472 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2473 0x10000000, 0x20000000, 0x40000000, 0x80000000
2477 #ifdef L_clear_cache
2478 /* Clear part of an instruction cache. */
2480 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2483 __clear_cache (char *beg __attribute__((__unused__)),
2484 char *end __attribute__((__unused__)))
2486 #ifdef CLEAR_INSN_CACHE
2487 CLEAR_INSN_CACHE (beg, end);
2489 #ifdef INSN_CACHE_SIZE
2490 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
2491 static int initialized;
2495 typedef (*function_ptr) ();
2497 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2498 /* It's cheaper to clear the whole cache.
2499 Put in a series of jump instructions so that calling the beginning
2500 of the cache will clear the whole thing. */
2504 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2505 & -INSN_CACHE_LINE_WIDTH);
2506 int end_ptr = ptr + INSN_CACHE_SIZE;
2508 while (ptr < end_ptr)
2510 *(INSTRUCTION_TYPE *)ptr
2511 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
2512 ptr += INSN_CACHE_LINE_WIDTH;
2514 *(INSTRUCTION_TYPE *) (ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
2519 /* Call the beginning of the sequence. */
2520 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2521 & -INSN_CACHE_LINE_WIDTH))
2524 #else /* Cache is large. */
2528 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2529 & -INSN_CACHE_LINE_WIDTH);
2531 while (ptr < (int) array + sizeof array)
2533 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
2534 ptr += INSN_CACHE_LINE_WIDTH;
2540 /* Find the location in array that occupies the same cache line as BEG. */
2542 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
2543 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
2544 & -INSN_CACHE_PLANE_SIZE)
2547 /* Compute the cache alignment of the place to stop clearing. */
2548 #if 0 /* This is not needed for gcc's purposes. */
2549 /* If the block to clear is bigger than a cache plane,
2550 we clear the entire cache, and OFFSET is already correct. */
2551 if (end < beg + INSN_CACHE_PLANE_SIZE)
2553 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
2554 & -INSN_CACHE_LINE_WIDTH)
2555 & (INSN_CACHE_PLANE_SIZE - 1));
2557 #if INSN_CACHE_DEPTH > 1
2558 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
2559 if (end_addr <= start_addr)
2560 end_addr += INSN_CACHE_PLANE_SIZE;
2562 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
2564 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
2565 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
2567 while (addr != stop)
2569 /* Call the return instruction at ADDR. */
2570 ((function_ptr) addr) ();
2572 addr += INSN_CACHE_LINE_WIDTH;
2575 #else /* just one plane */
2578 /* Call the return instruction at START_ADDR. */
2579 ((function_ptr) start_addr) ();
2581 start_addr += INSN_CACHE_LINE_WIDTH;
2583 while ((start_addr % INSN_CACHE_SIZE) != offset);
2584 #endif /* just one plane */
2585 #endif /* Cache is large */
2586 #endif /* Cache exists */
2587 #endif /* CLEAR_INSN_CACHE */
2590 #endif /* L_clear_cache */
2594 /* Jump to a trampoline, loading the static chain address. */
2596 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2608 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2612 mprotect (char *addr, int len, int prot)
2629 if (VirtualProtect (addr, len, np, &op))
2635 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2637 #ifdef TRANSFER_FROM_TRAMPOLINE
2638 TRANSFER_FROM_TRAMPOLINE
2641 #if defined (NeXT) && defined (__MACH__)
2643 /* Make stack executable so we can call trampolines on stack.
2644 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2648 #include <mach/mach.h>
2652 __enable_execute_stack (char *addr)
2655 char *eaddr = addr + TRAMPOLINE_SIZE;
2656 vm_address_t a = (vm_address_t) addr;
2658 /* turn on execute access on stack */
2659 r = vm_protect (task_self (), a, TRAMPOLINE_SIZE, FALSE, VM_PROT_ALL);
2660 if (r != KERN_SUCCESS)
2662 mach_error("vm_protect VM_PROT_ALL", r);
2666 /* We inline the i-cache invalidation for speed */
2668 #ifdef CLEAR_INSN_CACHE
2669 CLEAR_INSN_CACHE (addr, eaddr);
2671 __clear_cache ((int) addr, (int) eaddr);
2675 #endif /* defined (NeXT) && defined (__MACH__) */
2679 /* Make stack executable so we can call trampolines on stack.
2680 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2682 #include <sys/mman.h>
2683 #include <sys/vmparam.h>
2684 #include <machine/machparam.h>
2687 __enable_execute_stack ()
2690 static unsigned lowest = USRSTACK;
2691 unsigned current = (unsigned) &fp & -NBPG;
2693 if (lowest > current)
2695 unsigned len = lowest - current;
2696 mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
2700 /* Clear instruction cache in case an old trampoline is in it. */
2703 #endif /* __convex__ */
2707 /* Modified from the convex -code above. */
2709 #include <sys/param.h>
2711 #include <sys/m88kbcs.h>
2714 __enable_execute_stack ()
2717 static unsigned long lowest = USRSTACK;
2718 unsigned long current = (unsigned long) &save_errno & -NBPC;
2720 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2721 address is seen as 'negative'. That is the case with the stack. */
2724 if (lowest > current)
2726 unsigned len=lowest-current;
2727 memctl(current,len,MCT_TEXT);
2731 memctl(current,NBPC,MCT_TEXT);
2735 #endif /* __sysV88__ */
2739 #include <sys/signal.h>
2742 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2743 so define it here, because we need it in __clear_insn_cache below */
2744 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2745 hence we enable this stuff only if MCT_TEXT is #define'd. */
2760 /* Clear instruction cache so we can call trampolines on stack.
2761 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2764 __clear_insn_cache ()
2769 /* Preserve errno, because users would be surprised to have
2770 errno changing without explicitly calling any system-call. */
2773 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2774 No need to use an address derived from _start or %sp, as 0 works also. */
2775 memctl(0, 4096, MCT_TEXT);
2780 #endif /* __sysV68__ */
2784 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2786 #include <sys/mman.h>
2787 #include <sys/types.h>
2788 #include <sys/param.h>
2789 #include <sys/vmmac.h>
2791 /* Modified from the convex -code above.
2792 mremap promises to clear the i-cache. */
2795 __enable_execute_stack ()
2798 if (mprotect (((unsigned int)&fp/PAGSIZ)*PAGSIZ, PAGSIZ,
2799 PROT_READ|PROT_WRITE|PROT_EXEC))
2801 perror ("mprotect in __enable_execute_stack");
2806 #endif /* __pyr__ */
2808 #if defined (sony_news) && defined (SYSTYPE_BSD)
2811 #include <sys/types.h>
2812 #include <sys/param.h>
2813 #include <syscall.h>
2814 #include <machine/sysnews.h>
2816 /* cacheflush function for NEWS-OS 4.2.
2817 This function is called from trampoline-initialize code
2818 defined in config/mips/mips.h. */
2821 cacheflush (char *beg, int size, int flag)
2823 if (syscall (SYS_sysnews, NEWS_CACHEFLUSH, beg, size, FLUSH_BCACHE))
2825 perror ("cache_flush");
2831 #endif /* sony_news */
2832 #endif /* L_trampoline */
2837 #include "gbl-ctors.h"
2838 /* Some systems use __main in a way incompatible with its use in gcc, in these
2839 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2840 give the same symbol without quotes for an alternative entry point. You
2841 must define both, or neither. */
2843 #define NAME__MAIN "__main"
2844 #define SYMBOL__MAIN __main
2847 #ifdef INIT_SECTION_ASM_OP
2848 #undef HAS_INIT_SECTION
2849 #define HAS_INIT_SECTION
2852 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2854 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2855 code to run constructors. In that case, we need to handle EH here, too. */
2857 #ifdef EH_FRAME_SECTION
2859 extern unsigned char __EH_FRAME_BEGIN__[];
2862 /* Run all the global destructors on exit from the program. */
2865 __do_global_dtors ()
2867 #ifdef DO_GLOBAL_DTORS_BODY
2868 DO_GLOBAL_DTORS_BODY;
2870 static func_ptr *p = __DTOR_LIST__ + 1;
2877 #if defined (EH_FRAME_SECTION) && !defined (HAS_INIT_SECTION)
2879 static int completed = 0;
2883 __deregister_frame_info (__EH_FRAME_BEGIN__);
2890 #ifndef HAS_INIT_SECTION
2891 /* Run all the global constructors on entry to the program. */
2894 __do_global_ctors ()
2896 #ifdef EH_FRAME_SECTION
2898 static struct object object;
2899 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2902 DO_GLOBAL_CTORS_BODY;
2903 atexit (__do_global_dtors);
2905 #endif /* no HAS_INIT_SECTION */
2907 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2908 /* Subroutine called automatically by `main'.
2909 Compiling a global function named `main'
2910 produces an automatic call to this function at the beginning.
2912 For many systems, this routine calls __do_global_ctors.
2913 For systems which support a .init section we use the .init section
2914 to run __do_global_ctors, so we need not do anything here. */
2919 /* Support recursive calls to `main': run initializers just once. */
2920 static int initialized;
2924 __do_global_ctors ();
2927 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2929 #endif /* L__main */
2930 #endif /* __CYGWIN__ */
2934 #include "gbl-ctors.h"
2936 /* Provide default definitions for the lists of constructors and
2937 destructors, so that we don't get linker errors. These symbols are
2938 intentionally bss symbols, so that gld and/or collect will provide
2939 the right values. */
2941 /* We declare the lists here with two elements each,
2942 so that they are valid empty lists if no other definition is loaded.
2944 If we are using the old "set" extensions to have the gnu linker
2945 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2946 must be in the bss/common section.
2948 Long term no port should use those extensions. But many still do. */
2949 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2950 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
2951 func_ptr __CTOR_LIST__[2] = {0, 0};
2952 func_ptr __DTOR_LIST__[2] = {0, 0};
2954 func_ptr __CTOR_LIST__[2];
2955 func_ptr __DTOR_LIST__[2];
2957 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2958 #endif /* L_ctors */
2962 #include "gbl-ctors.h"
2970 static func_ptr *atexit_chain = 0;
2971 static long atexit_chain_length = 0;
2972 static volatile long last_atexit_chain_slot = -1;
2975 atexit (func_ptr func)
2977 if (++last_atexit_chain_slot == atexit_chain_length)
2979 atexit_chain_length += 32;
2981 atexit_chain = (func_ptr *) realloc (atexit_chain, atexit_chain_length
2982 * sizeof (func_ptr));
2984 atexit_chain = (func_ptr *) malloc (atexit_chain_length
2985 * sizeof (func_ptr));
2988 atexit_chain_length = 0;
2989 last_atexit_chain_slot = -1;
2994 atexit_chain[last_atexit_chain_slot] = func;
2998 extern void _cleanup ();
2999 extern void _exit () __attribute__ ((noreturn));
3006 for ( ; last_atexit_chain_slot-- >= 0; )
3008 (*atexit_chain[last_atexit_chain_slot + 1]) ();
3009 atexit_chain[last_atexit_chain_slot + 1] = 0;
3011 free (atexit_chain);
3024 /* Simple; we just need a wrapper for ON_EXIT. */
3026 atexit (func_ptr func)
3028 return ON_EXIT (func);
3031 #endif /* ON_EXIT */
3032 #endif /* NEED_ATEXIT */
3040 /* Shared exception handling support routines. */
3042 extern void __default_terminate (void) __attribute__ ((__noreturn__));
3045 __default_terminate ()
3050 void (*__terminate_func)() = __default_terminate;
3055 (*__terminate_func)();
3059 __throw_type_match (void *catch_type, void *throw_type, void *obj)
3062 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3063 catch_type, throw_type);
3065 if (strcmp ((const char *)catch_type, (const char *)throw_type) == 0)
3076 /* Include definitions of EH context and table layout */
3078 #include "eh-common.h"
3079 #ifndef inhibit_libc
3083 /* Allocate and return a new EH context structure. */
3085 extern void __throw ();
3090 struct eh_full_context {
3091 struct eh_context c;
3093 } *ehfc = (struct eh_full_context *) malloc (sizeof *ehfc);
3098 memset (ehfc, 0, sizeof *ehfc);
3100 ehfc->c.dynamic_handler_chain = (void **) ehfc->top_elt;
3102 /* This should optimize out entirely. This should always be true,
3103 but just in case it ever isn't, don't allow bogus code to be
3106 if ((void*)(&ehfc->c) != (void*)ehfc)
3113 static __gthread_key_t eh_context_key;
3115 /* Destructor for struct eh_context. */
3117 eh_context_free (void *ptr)
3119 __gthread_key_dtor (eh_context_key, ptr);
3125 /* Pointer to function to return EH context. */
3127 static struct eh_context *eh_context_initialize ();
3128 static struct eh_context *eh_context_static ();
3130 static struct eh_context *eh_context_specific ();
3133 static struct eh_context *(*get_eh_context) () = &eh_context_initialize;
3135 /* Routine to get EH context.
3136 This one will simply call the function pointer. */
3141 return (void *) (*get_eh_context) ();
3144 /* Get and set the language specific info pointer. */
3149 struct eh_context *eh = (*get_eh_context) ();
3153 #ifdef DWARF2_UNWIND_INFO
3154 static int dwarf_reg_size_table_initialized = 0;
3155 static char dwarf_reg_size_table[FIRST_PSEUDO_REGISTER];
3158 init_reg_size_table ()
3160 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
3161 dwarf_reg_size_table_initialized = 1;
3167 eh_threads_initialize ()
3169 /* Try to create the key. If it fails, revert to static method,
3170 otherwise start using thread specific EH contexts. */
3171 if (__gthread_key_create (&eh_context_key, &eh_context_free) == 0)
3172 get_eh_context = &eh_context_specific;
3174 get_eh_context = &eh_context_static;
3176 #endif /* no __GTHREADS */
3178 /* Initialize EH context.
3179 This will be called only once, since we change GET_EH_CONTEXT
3180 pointer to another routine. */
3182 static struct eh_context *
3183 eh_context_initialize ()
3187 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
3188 /* Make sure that get_eh_context does not point to us anymore.
3189 Some systems have dummy thread routines in their libc that
3190 return a success (Solaris 2.6 for example). */
3191 if (__gthread_once (&once, eh_threads_initialize) != 0
3192 || get_eh_context == &eh_context_initialize)
3194 /* Use static version of EH context. */
3195 get_eh_context = &eh_context_static;
3197 #ifdef DWARF2_UNWIND_INFO
3199 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
3200 if (__gthread_once (&once_regsizes, init_reg_size_table) != 0
3201 || ! dwarf_reg_size_table_initialized)
3202 init_reg_size_table ();
3206 #else /* no __GTHREADS */
3208 /* Use static version of EH context. */
3209 get_eh_context = &eh_context_static;
3211 #ifdef DWARF2_UNWIND_INFO
3212 init_reg_size_table ();
3215 #endif /* no __GTHREADS */
3217 return (*get_eh_context) ();
3220 /* Return a static EH context. */
3222 static struct eh_context *
3223 eh_context_static ()
3225 static struct eh_context eh;
3226 static int initialized;
3227 static void *top_elt[2];
3232 memset (&eh, 0, sizeof eh);
3233 eh.dynamic_handler_chain = top_elt;
3239 /* Return a thread specific EH context. */
3241 static struct eh_context *
3242 eh_context_specific ()
3244 struct eh_context *eh;
3245 eh = (struct eh_context *) __gthread_getspecific (eh_context_key);
3248 eh = new_eh_context ();
3249 if (__gthread_setspecific (eh_context_key, (void *) eh) != 0)
3257 /* Support routines for setjmp/longjmp exception handling. */
3259 /* Calls to __sjthrow are generated by the compiler when an exception
3260 is raised when using the setjmp/longjmp exception handling codegen
3263 #ifdef DONT_USE_BUILTIN_SETJMP
3264 extern void longjmp (void *, int);
3267 /* Routine to get the head of the current thread's dynamic handler chain
3268 use for exception handling. */
3271 __get_dynamic_handler_chain ()
3273 struct eh_context *eh = (*get_eh_context) ();
3274 return &eh->dynamic_handler_chain;
3277 /* This is used to throw an exception when the setjmp/longjmp codegen
3278 method is used for exception handling.
3280 We call __terminate if there are no handlers left. Otherwise we run the
3281 cleanup actions off the dynamic cleanup stack, and pop the top of the
3282 dynamic handler chain, and use longjmp to transfer back to the associated
3285 extern void __sjthrow (void) __attribute__ ((__noreturn__));
3290 struct eh_context *eh = (*get_eh_context) ();
3291 void ***dhc = &eh->dynamic_handler_chain;
3293 void (*func)(void *, int);
3297 /* The cleanup chain is one word into the buffer. Get the cleanup
3299 cleanup = (void***)&(*dhc)[1];
3301 /* If there are any cleanups in the chain, run them now. */
3305 void **buf = (void**)store;
3310 #ifdef DONT_USE_BUILTIN_SETJMP
3311 if (! setjmp (&buf[2]))
3313 if (! __builtin_setjmp (&buf[2]))
3319 func = (void(*)(void*, int))cleanup[0][1];
3320 arg = (void*)cleanup[0][2];
3322 /* Update this before running the cleanup. */
3323 cleanup[0] = (void **)cleanup[0][0];
3336 /* We must call terminate if we try and rethrow an exception, when
3337 there is no exception currently active and when there are no
3339 if (! eh->info || (*dhc)[0] == 0)
3342 /* Find the jmpbuf associated with the top element of the dynamic
3343 handler chain. The jumpbuf starts two words into the buffer. */
3344 jmpbuf = &(*dhc)[2];
3346 /* Then we pop the top element off the dynamic handler chain. */
3347 *dhc = (void**)(*dhc)[0];
3349 /* And then we jump to the handler. */
3351 #ifdef DONT_USE_BUILTIN_SETJMP
3352 longjmp (jmpbuf, 1);
3354 __builtin_longjmp (jmpbuf, 1);
3358 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3359 handler, then pop the handler off the dynamic handler stack, and
3360 then throw. This is used to skip the first handler, and transfer
3361 control to the next handler in the dynamic handler stack. */
3363 extern void __sjpopnthrow (void) __attribute__ ((__noreturn__));
3368 struct eh_context *eh = (*get_eh_context) ();
3369 void ***dhc = &eh->dynamic_handler_chain;
3370 void (*func)(void *, int);
3374 /* The cleanup chain is one word into the buffer. Get the cleanup
3376 cleanup = (void***)&(*dhc)[1];
3378 /* If there are any cleanups in the chain, run them now. */
3382 void **buf = (void**)store;
3387 #ifdef DONT_USE_BUILTIN_SETJMP
3388 if (! setjmp (&buf[2]))
3390 if (! __builtin_setjmp (&buf[2]))
3396 func = (void(*)(void*, int))cleanup[0][1];
3397 arg = (void*)cleanup[0][2];
3399 /* Update this before running the cleanup. */
3400 cleanup[0] = (void **)cleanup[0][0];
3413 /* Then we pop the top element off the dynamic handler chain. */
3414 *dhc = (void**)(*dhc)[0];
3419 /* Support code for all exception region-based exception handling. */
3422 __eh_rtime_match (void *rtime)
3425 __eh_matcher matcher;
3428 info = *(__get_eh_info ());
3429 matcher = ((__eh_info *)info)->match_function;
3432 #ifndef inhibit_libc
3433 fprintf (stderr, "Internal Compiler Bug: No runtime type matcher.");
3437 ret = (*matcher) (info, rtime, (void *)0);
3438 return (ret != NULL);
3441 /* This value identifies the place from which an exception is being
3444 #ifdef EH_TABLE_LOOKUP
3450 #ifdef DWARF2_UNWIND_INFO
3452 /* Return the table version of an exception descriptor */
3455 __get_eh_table_version (exception_descriptor *table)
3457 return table->lang.version;
3460 /* Return the originating table language of an exception descriptor */
3463 __get_eh_table_language (exception_descriptor *table)
3465 return table->lang.language;
3468 /* This routine takes a PC and a pointer to the exception region TABLE for
3469 its translation unit, and returns the address of the exception handler
3470 associated with the closest exception table handler entry associated
3471 with that PC, or 0 if there are no table entries the PC fits in.
3473 In the advent of a tie, we have to give the last entry, as it represents
3477 old_find_exception_handler (void *pc, old_exception_table *table)
3484 /* We can't do a binary search because the table isn't guaranteed
3485 to be sorted from function to function. */
3486 for (pos = 0; table[pos].start_region != (void *) -1; ++pos)
3488 if (table[pos].start_region <= pc && table[pos].end_region > pc)
3490 /* This can apply. Make sure it is at least as small as
3491 the previous best. */
3492 if (best == -1 || (table[pos].end_region <= table[best].end_region
3493 && table[pos].start_region >= table[best].start_region))
3496 /* But it is sorted by starting PC within a function. */
3497 else if (best >= 0 && table[pos].start_region > pc)
3501 return table[best].exception_handler;
3507 /* find_exception_handler finds the correct handler, if there is one, to
3508 handle an exception.
3509 returns a pointer to the handler which controlled should be transferred
3510 to, or NULL if there is nothing left.
3512 PC - pc where the exception originates. If this is a rethrow,
3513 then this starts out as a pointer to the exception table
3514 entry we wish to rethrow out of.
3515 TABLE - exception table for the current module.
3516 EH_INFO - eh info pointer for this exception.
3517 RETHROW - 1 if this is a rethrow. (see incoming value of PC).
3518 CLEANUP - returned flag indicating whether this is a cleanup handler.
3521 find_exception_handler (void *pc, exception_descriptor *table,
3522 __eh_info *eh_info, int rethrow, int *cleanup)
3525 void *retval = NULL;
3530 /* The new model assumed the table is sorted inner-most out so the
3531 first region we find which matches is the correct one */
3533 exception_table *tab = &(table->table[0]);
3535 /* Subtract 1 from the PC to avoid hitting the next region */
3538 /* pc is actually the region table entry to rethrow out of */
3539 pos = ((exception_table *) pc) - tab;
3540 pc = ((exception_table *) pc)->end_region - 1;
3542 /* The label is always on the LAST handler entry for a region,
3543 so we know the next entry is a different region, even if the
3544 addresses are the same. Make sure its not end of table tho. */
3545 if (tab[pos].start_region != (void *) -1)
3551 /* We can't do a binary search because the table is in inner-most
3552 to outermost address ranges within functions */
3553 for ( ; tab[pos].start_region != (void *) -1; pos++)
3555 if (tab[pos].start_region <= pc && tab[pos].end_region > pc)
3557 if (tab[pos].match_info)
3559 __eh_matcher matcher = eh_info->match_function;
3560 /* match info but no matcher is NOT a match */
3563 void *ret = (*matcher)((void *) eh_info,
3564 tab[pos].match_info, table);
3568 retval = tab[pos].exception_handler;
3577 retval = tab[pos].exception_handler;
3584 #endif /* DWARF2_UNWIND_INFO */
3585 #endif /* EH_TABLE_LOOKUP */
3587 #ifdef DWARF2_UNWIND_INFO
3588 /* Support code for exception handling using static unwind information. */
3592 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3593 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3594 avoid a warning about casting between int and pointer of different
3597 typedef int ptr_type __attribute__ ((mode (pointer)));
3599 #ifdef INCOMING_REGNO
3600 /* Is the saved value for register REG in frame UDATA stored in a register
3601 window in the previous frame? */
3603 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3604 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3605 compiled functions won't work with the frame-unwind stuff here.
3606 Perhaps the entireity of in_reg_window should be conditional on having
3607 seen a DW_CFA_GNU_window_save? */
3608 #define target_flags 0
3611 in_reg_window (int reg, frame_state *udata)
3613 if (udata->saved[reg] == REG_SAVED_REG)
3614 return INCOMING_REGNO (reg) == reg;
3615 if (udata->saved[reg] != REG_SAVED_OFFSET)
3618 #ifdef STACK_GROWS_DOWNWARD
3619 return udata->reg_or_offset[reg] > 0;
3621 return udata->reg_or_offset[reg] < 0;
3625 static inline int in_reg_window (int reg, frame_state *udata) { return 0; }
3626 #endif /* INCOMING_REGNO */
3628 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3629 frame called by UDATA or 0. */
3632 get_reg_addr (unsigned reg, frame_state *udata, frame_state *sub_udata)
3634 while (udata->saved[reg] == REG_SAVED_REG)
3636 reg = udata->reg_or_offset[reg];
3637 if (in_reg_window (reg, udata))
3643 if (udata->saved[reg] == REG_SAVED_OFFSET)
3644 return (word_type *)(udata->cfa + udata->reg_or_offset[reg]);
3649 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3650 frame called by UDATA or 0. */
3652 static inline void *
3653 get_reg (unsigned reg, frame_state *udata, frame_state *sub_udata)
3655 return (void *)(ptr_type) *get_reg_addr (reg, udata, sub_udata);
3658 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3661 put_reg (unsigned reg, void *val, frame_state *udata)
3663 *get_reg_addr (reg, udata, NULL) = (word_type)(ptr_type) val;
3666 /* Copy the saved value for register REG from frame UDATA to frame
3667 TARGET_UDATA. Unlike the previous two functions, this can handle
3668 registers that are not one word large. */
3671 copy_reg (unsigned reg, frame_state *udata, frame_state *target_udata)
3673 word_type *preg = get_reg_addr (reg, udata, NULL);
3674 word_type *ptreg = get_reg_addr (reg, target_udata, NULL);
3676 memcpy (ptreg, preg, dwarf_reg_size_table [reg]);
3679 /* Retrieve the return address for frame UDATA. */
3681 static inline void *
3682 get_return_addr (frame_state *udata, frame_state *sub_udata)
3684 return __builtin_extract_return_addr
3685 (get_reg (udata->retaddr_column, udata, sub_udata));
3688 /* Overwrite the return address for frame UDATA with VAL. */
3691 put_return_addr (void *val, frame_state *udata)
3693 val = __builtin_frob_return_addr (val);
3694 put_reg (udata->retaddr_column, val, udata);
3697 /* Given the current frame UDATA and its return address PC, return the
3698 information about the calling frame in CALLER_UDATA. */
3701 next_stack_level (void *pc, frame_state *udata, frame_state *caller_udata)
3703 caller_udata = __frame_state_for (pc, caller_udata);
3707 /* Now go back to our caller's stack frame. If our caller's CFA register
3708 was saved in our stack frame, restore it; otherwise, assume the CFA
3709 register is SP and restore it to our CFA value. */
3710 if (udata->saved[caller_udata->cfa_reg])
3711 caller_udata->cfa = get_reg (caller_udata->cfa_reg, udata, 0);
3713 caller_udata->cfa = udata->cfa;
3714 caller_udata->cfa += caller_udata->cfa_offset;
3716 return caller_udata;
3719 /* Hook to call before __terminate if only cleanup handlers remain. */
3721 __unwinding_cleanup ()
3725 /* throw_helper performs some of the common grunt work for a throw. This
3726 routine is called by throw and rethrows. This is pretty much split
3727 out from the old __throw routine. An addition has been added which allows
3728 for a dummy call to a routine __unwinding_cleanup() when there are nothing
3729 but cleanups remaining. This allows a debugger to examine the state
3730 at which the throw was executed, before any cleanups, rather than
3731 at the terminate point after the stack has been unwound.
3733 EH is the current eh_context structure.
3734 PC is the address of the call to __throw.
3735 MY_UDATA is the unwind information for __throw.
3736 OFFSET_P is where we return the SP adjustment offset. */
3739 throw_helper (eh, pc, my_udata, offset_p)
3740 struct eh_context *eh;
3742 frame_state *my_udata;
3745 frame_state ustruct2, *udata = &ustruct2;
3746 frame_state ustruct;
3747 frame_state *sub_udata = &ustruct;
3748 void *saved_pc = pc;
3752 frame_state saved_ustruct;
3755 int only_cleanup = 0;
3757 int saved_state = 0;
3759 __eh_info *eh_info = (__eh_info *)eh->info;
3761 /* Do we find a handler based on a re-throw PC? */
3762 if (eh->table_index != (void *) 0)
3765 memcpy (udata, my_udata, sizeof (*udata));
3767 handler = (void *) 0;
3770 frame_state *p = udata;
3771 udata = next_stack_level (pc, udata, sub_udata);
3774 /* If we couldn't find the next frame, we lose. */
3778 if (udata->eh_ptr == NULL)
3781 new_eh_model = (((exception_descriptor *)(udata->eh_ptr))->
3782 runtime_id_field == NEW_EH_RUNTIME);
3787 handler = find_exception_handler (eh->table_index, udata->eh_ptr,
3788 eh_info, 1, &cleanup);
3789 eh->table_index = (void *)0;
3793 handler = find_exception_handler (pc, udata->eh_ptr, eh_info,
3796 handler = old_find_exception_handler (pc, udata->eh_ptr);
3798 /* If we found one, we can stop searching, if its not a cleanup.
3799 for cleanups, we save the state, and keep looking. This allows
3800 us to call a debug hook if there are nothing but cleanups left. */
3807 saved_ustruct = *udata;
3808 handler_p = handler;
3821 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3822 hitting the beginning of the next region. */
3823 pc = get_return_addr (udata, sub_udata) - 1;
3828 udata = &saved_ustruct;
3829 handler = handler_p;
3832 __unwinding_cleanup ();
3835 /* If we haven't found a handler by now, this is an unhandled
3840 eh->handler_label = handler;
3842 args_size = udata->args_size;
3845 /* We found a handler in the throw context, no need to unwind. */
3851 /* Unwind all the frames between this one and the handler by copying
3852 their saved register values into our register save slots. */
3854 /* Remember the PC where we found the handler. */
3855 void *handler_pc = pc;
3857 /* Start from the throw context again. */
3859 memcpy (udata, my_udata, sizeof (*udata));
3861 while (pc != handler_pc)
3863 frame_state *p = udata;
3864 udata = next_stack_level (pc, udata, sub_udata);
3867 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
3868 if (i != udata->retaddr_column && udata->saved[i])
3870 /* If you modify the saved value of the return address
3871 register on the SPARC, you modify the return address for
3872 your caller's frame. Don't do that here, as it will
3873 confuse get_return_addr. */
3874 if (in_reg_window (i, udata)
3875 && udata->saved[udata->retaddr_column] == REG_SAVED_REG
3876 && udata->reg_or_offset[udata->retaddr_column] == i)
3878 copy_reg (i, udata, my_udata);
3881 pc = get_return_addr (udata, sub_udata) - 1;
3884 /* But we do need to update the saved return address register from
3885 the last frame we unwind, or the handler frame will have the wrong
3887 if (udata->saved[udata->retaddr_column] == REG_SAVED_REG)
3889 i = udata->reg_or_offset[udata->retaddr_column];
3890 if (in_reg_window (i, udata))
3891 copy_reg (i, udata, my_udata);
3894 /* udata now refers to the frame called by the handler frame. */
3896 /* We adjust SP by the difference between __throw's CFA and the CFA for
3897 the frame called by the handler frame, because those CFAs correspond
3898 to the SP values at the two call sites. We need to further adjust by
3899 the args_size of the handler frame itself to get the handler frame's
3900 SP from before the args were pushed for that call. */
3901 #ifdef STACK_GROWS_DOWNWARD
3902 *offset_p = udata->cfa - my_udata->cfa + args_size;
3904 *offset_p = my_udata->cfa - udata->cfa - args_size;
3911 /* We first search for an exception handler, and if we don't find
3912 it, we call __terminate on the current stack frame so that we may
3913 use the debugger to walk the stack and understand why no handler
3916 If we find one, then we unwind the frames down to the one that
3917 has the handler and transfer control into the handler. */
3919 /*extern void __throw(void) __attribute__ ((__noreturn__));*/
3924 struct eh_context *eh = (*get_eh_context) ();
3928 /* XXX maybe make my_ustruct static so we don't have to look it up for
3930 frame_state my_ustruct, *my_udata = &my_ustruct;
3932 /* This is required for C++ semantics. We must call terminate if we
3933 try and rethrow an exception, when there is no exception currently
3938 /* Start at our stack frame. */
3940 my_udata = __frame_state_for (&&label, my_udata);
3944 /* We need to get the value from the CFA register. */
3945 my_udata->cfa = __builtin_dwarf_cfa ();
3947 /* Do any necessary initialization to access arbitrary stack frames.
3948 On the SPARC, this means flushing the register windows. */
3949 __builtin_unwind_init ();
3951 /* Now reset pc to the right throw point. */
3952 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3954 handler = throw_helper (eh, pc, my_udata, &offset);
3958 __builtin_eh_return ((void *)eh, offset, handler);
3960 /* Epilogue: restore the handler frame's register values and return
3964 /*extern void __rethrow(void *) __attribute__ ((__noreturn__));*/
3970 struct eh_context *eh = (*get_eh_context) ();
3974 /* XXX maybe make my_ustruct static so we don't have to look it up for
3976 frame_state my_ustruct, *my_udata = &my_ustruct;
3978 /* This is required for C++ semantics. We must call terminate if we
3979 try and rethrow an exception, when there is no exception currently
3984 /* This is the table index we want to rethrow from. The value of
3985 the END_REGION label is used for the PC of the throw, and the
3986 search begins with the next table entry. */
3987 eh->table_index = index;
3989 /* Start at our stack frame. */
3991 my_udata = __frame_state_for (&&label, my_udata);
3995 /* We need to get the value from the CFA register. */
3996 my_udata->cfa = __builtin_dwarf_cfa ();
3998 /* Do any necessary initialization to access arbitrary stack frames.
3999 On the SPARC, this means flushing the register windows. */
4000 __builtin_unwind_init ();
4002 /* Now reset pc to the right throw point. */
4003 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
4005 handler = throw_helper (eh, pc, my_udata, &offset);
4009 __builtin_eh_return ((void *)eh, offset, handler);
4011 /* Epilogue: restore the handler frame's register values and return
4014 #endif /* DWARF2_UNWIND_INFO */
4019 #ifndef inhibit_libc
4020 /* This gets us __GNU_LIBRARY__. */
4021 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
4024 #ifdef __GNU_LIBRARY__
4025 /* Avoid forcing the library's meaning of `write' on the user program
4026 by using the "internal" name (for use within the library) */
4027 #define write(fd, buf, n) __write((fd), (buf), (n))
4029 #endif /* inhibit_libc */
4031 #define MESSAGE "pure virtual method called\n"
4033 extern void __terminate (void) __attribute__ ((__noreturn__));
4038 #ifndef inhibit_libc
4039 write (2, MESSAGE, sizeof (MESSAGE) - 1);