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. */
31 /* It is incorrect to include config.h here, because this file is being
32 compiled for the target, and hence definitions concerning only the host
37 /* We disable this when inhibit_libc, so that gcc can still be built without
38 needing header files first. */
39 /* ??? This is not a good solution, since prototypes may be required in
40 some cases for correct code. See also frame.c. */
42 /* fixproto guarantees these system headers exist. */
53 /* Don't use `fancy_abort' here even if config.h says to use it. */
58 #if (SUPPORTS_WEAK == 1) && (defined (ASM_OUTPUT_DEF) || defined (ASM_OUTPUT_WEAK_ALIAS))
62 /* In a cross-compilation situation, default to inhibiting compilation
63 of routines that use libc. */
65 #if defined(CROSS_COMPILE) && !defined(inhibit_libc)
69 /* Permit the tm.h file to select the endianness to use just for this
70 file. This is used when the endianness is determined when the
73 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
74 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
77 #ifndef LIBGCC2_LONG_DOUBLE_TYPE_SIZE
78 #define LIBGCC2_LONG_DOUBLE_TYPE_SIZE LONG_DOUBLE_TYPE_SIZE
81 /* In the first part of this file, we are interfacing to calls generated
82 by the compiler itself. These calls pass values into these routines
83 which have very specific modes (rather than very specific types), and
84 these compiler-generated calls also expect any return values to have
85 very specific modes (rather than very specific types). Thus, we need
86 to avoid using regular C language type names in this part of the file
87 because the sizes for those types can be configured to be anything.
88 Instead we use the following special type names. */
90 typedef unsigned int UQItype __attribute__ ((mode (QI)));
91 typedef int SItype __attribute__ ((mode (SI)));
92 typedef unsigned int USItype __attribute__ ((mode (SI)));
93 typedef int DItype __attribute__ ((mode (DI)));
94 typedef unsigned int UDItype __attribute__ ((mode (DI)));
96 typedef float SFtype __attribute__ ((mode (SF)));
97 typedef float DFtype __attribute__ ((mode (DF)));
99 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
100 typedef float XFtype __attribute__ ((mode (XF)));
102 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
103 typedef float TFtype __attribute__ ((mode (TF)));
106 typedef int word_type __attribute__ ((mode (__word__)));
108 /* Make sure that we don't accidentally use any normal C language built-in
109 type names in the first part of this file. Instead we want to use *only*
110 the type names defined above. The following macro definitions insure
111 that if we *do* accidentally use some normal C language built-in type name,
112 we will get a syntax error. */
114 #define char bogus_type
115 #define short bogus_type
116 #define int bogus_type
117 #define long bogus_type
118 #define unsigned bogus_type
119 #define float bogus_type
120 #define double bogus_type
122 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
124 /* DIstructs are pairs of SItype values in the order determined by
125 LIBGCC2_WORDS_BIG_ENDIAN. */
127 #if LIBGCC2_WORDS_BIG_ENDIAN
128 struct DIstruct {SItype high, low;};
130 struct DIstruct {SItype low, high;};
133 /* We need this union to unpack/pack DImode values, since we don't have
134 any arithmetic yet. Incoming DImode parameters are stored into the
135 `ll' field, and the unpacked result is read from the struct `s'. */
143 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
144 || defined (L_divdi3) || defined (L_udivdi3) \
145 || defined (L_moddi3) || defined (L_umoddi3))
147 #include "longlong.h"
149 #endif /* udiv or mul */
151 extern DItype __fixunssfdi (SFtype a);
152 extern DItype __fixunsdfdi (DFtype a);
153 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
154 extern DItype __fixunsxfdi (XFtype a);
156 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
157 extern DItype __fixunstfdi (TFtype a);
160 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
161 #if defined (L_divdi3) || defined (L_moddi3)
173 w.s.high = -uu.s.high - ((USItype) w.s.low > 0);
179 /* Unless shift functions are defined whith full ANSI prototypes,
180 parameter b will be promoted to int if word_type is smaller than an int. */
183 __lshrdi3 (DItype u, word_type b)
194 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
198 w.s.low = (USItype)uu.s.high >> -bm;
202 USItype carries = (USItype)uu.s.high << bm;
203 w.s.high = (USItype)uu.s.high >> b;
204 w.s.low = ((USItype)uu.s.low >> b) | carries;
213 __ashldi3 (DItype u, word_type b)
224 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
228 w.s.high = (USItype)uu.s.low << -bm;
232 USItype carries = (USItype)uu.s.low >> bm;
233 w.s.low = (USItype)uu.s.low << b;
234 w.s.high = ((USItype)uu.s.high << b) | carries;
243 __ashrdi3 (DItype u, word_type b)
254 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
257 /* w.s.high = 1..1 or 0..0 */
258 w.s.high = uu.s.high >> (sizeof (SItype) * BITS_PER_UNIT - 1);
259 w.s.low = uu.s.high >> -bm;
263 USItype carries = (USItype)uu.s.high << bm;
264 w.s.high = uu.s.high >> b;
265 w.s.low = ((USItype)uu.s.low >> b) | carries;
279 w.s.low = ffs (uu.s.low);
282 w.s.low = ffs (uu.s.high);
285 w.s.low += BITS_PER_UNIT * sizeof (SItype);
294 __muldi3 (DItype u, DItype v)
302 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
303 w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
304 + (USItype) uu.s.high * (USItype) vv.s.low);
311 #if defined (sdiv_qrnnd)
313 __udiv_w_sdiv (USItype *rp, USItype a1, USItype a0, USItype d)
320 if (a1 < d - a1 - (a0 >> (SI_TYPE_SIZE - 1)))
322 /* dividend, divisor, and quotient are nonnegative */
323 sdiv_qrnnd (q, r, a1, a0, d);
327 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
328 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (SI_TYPE_SIZE - 1));
329 /* Divide (c1*2^32 + c0) by d */
330 sdiv_qrnnd (q, r, c1, c0, d);
331 /* Add 2^31 to quotient */
332 q += (USItype) 1 << (SI_TYPE_SIZE - 1);
337 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
338 c1 = a1 >> 1; /* A/2 */
339 c0 = (a1 << (SI_TYPE_SIZE - 1)) + (a0 >> 1);
341 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
343 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
345 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
362 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
365 c0 = ~c0; /* logical NOT */
367 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
369 q = ~q; /* (A/2)/b1 */
372 r = 2*r + (a0 & 1); /* A/(2*b1) */
390 else /* Implies c1 = b1 */
391 { /* Hence a1 = d - 1 = 2*b1 - 1 */
409 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
411 __udiv_w_sdiv (USItype *rp __attribute__ ((__unused__)),
412 USItype a1 __attribute__ ((__unused__)),
413 USItype a0 __attribute__ ((__unused__)),
414 USItype d __attribute__ ((__unused__)))
421 #if (defined (L_udivdi3) || defined (L_divdi3) || \
422 defined (L_umoddi3) || defined (L_moddi3))
427 static const UQItype __clz_tab[] =
429 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,
430 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,
431 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,
432 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,
433 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
434 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
435 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,
436 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,
439 #if (defined (L_udivdi3) || defined (L_divdi3) || \
440 defined (L_umoddi3) || defined (L_moddi3))
444 __udivmoddi4 (UDItype n, UDItype d, UDItype *rp)
449 USItype d0, d1, n0, n1, n2;
461 #if !UDIV_NEEDS_NORMALIZATION
468 udiv_qrnnd (q0, n0, n1, n0, d0);
471 /* Remainder in n0. */
478 d0 = 1 / d0; /* Divide intentionally by zero. */
480 udiv_qrnnd (q1, n1, 0, n1, d0);
481 udiv_qrnnd (q0, n0, n1, n0, d0);
483 /* Remainder in n0. */
494 #else /* UDIV_NEEDS_NORMALIZATION */
502 count_leading_zeros (bm, d0);
506 /* Normalize, i.e. make the most significant bit of the
510 n1 = (n1 << bm) | (n0 >> (SI_TYPE_SIZE - bm));
514 udiv_qrnnd (q0, n0, n1, n0, d0);
517 /* Remainder in n0 >> bm. */
524 d0 = 1 / d0; /* Divide intentionally by zero. */
526 count_leading_zeros (bm, d0);
530 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
531 conclude (the most significant bit of n1 is set) /\ (the
532 leading quotient digit q1 = 1).
534 This special case is necessary, not an optimization.
535 (Shifts counts of SI_TYPE_SIZE are undefined.) */
544 b = SI_TYPE_SIZE - bm;
548 n1 = (n1 << bm) | (n0 >> b);
551 udiv_qrnnd (q1, n1, n2, n1, d0);
556 udiv_qrnnd (q0, n0, n1, n0, d0);
558 /* Remainder in n0 >> bm. */
568 #endif /* UDIV_NEEDS_NORMALIZATION */
579 /* Remainder in n1n0. */
591 count_leading_zeros (bm, d1);
594 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
595 conclude (the most significant bit of n1 is set) /\ (the
596 quotient digit q0 = 0 or 1).
598 This special case is necessary, not an optimization. */
600 /* The condition on the next line takes advantage of that
601 n1 >= d1 (true due to program flow). */
602 if (n1 > d1 || n0 >= d0)
605 sub_ddmmss (n1, n0, n1, n0, d1, d0);
624 b = SI_TYPE_SIZE - bm;
626 d1 = (d1 << bm) | (d0 >> b);
629 n1 = (n1 << bm) | (n0 >> b);
632 udiv_qrnnd (q0, n1, n2, n1, d1);
633 umul_ppmm (m1, m0, q0, d0);
635 if (m1 > n1 || (m1 == n1 && m0 > n0))
638 sub_ddmmss (m1, m0, m1, m0, d1, d0);
643 /* Remainder in (n1n0 - m1m0) >> bm. */
646 sub_ddmmss (n1, n0, n1, n0, m1, m0);
647 rr.s.low = (n1 << b) | (n0 >> bm);
648 rr.s.high = n1 >> bm;
662 UDItype __udivmoddi4 ();
665 __divdi3 (DItype u, DItype v)
676 uu.ll = __negdi2 (uu.ll);
679 vv.ll = __negdi2 (vv.ll);
681 w = __udivmoddi4 (uu.ll, vv.ll, (UDItype *) 0);
690 UDItype __udivmoddi4 ();
692 __moddi3 (DItype u, DItype v)
703 uu.ll = __negdi2 (uu.ll);
705 vv.ll = __negdi2 (vv.ll);
707 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
716 UDItype __udivmoddi4 ();
718 __umoddi3 (UDItype u, UDItype v)
722 (void) __udivmoddi4 (u, v, &w);
729 UDItype __udivmoddi4 ();
731 __udivdi3 (UDItype n, UDItype d)
733 return __udivmoddi4 (n, d, (UDItype *) 0);
739 __cmpdi2 (DItype a, DItype b)
743 au.ll = a, bu.ll = b;
745 if (au.s.high < bu.s.high)
747 else if (au.s.high > bu.s.high)
749 if ((USItype) au.s.low < (USItype) bu.s.low)
751 else if ((USItype) au.s.low > (USItype) bu.s.low)
759 __ucmpdi2 (DItype a, DItype b)
763 au.ll = a, bu.ll = b;
765 if ((USItype) au.s.high < (USItype) bu.s.high)
767 else if ((USItype) au.s.high > (USItype) bu.s.high)
769 if ((USItype) au.s.low < (USItype) bu.s.low)
771 else if ((USItype) au.s.low > (USItype) bu.s.low)
777 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
778 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
779 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
782 __fixunstfdi (TFtype a)
790 /* Compute high word of result, as a flonum. */
791 b = (a / HIGH_WORD_COEFF);
792 /* Convert that to fixed (but not to DItype!),
793 and shift it into the high word. */
796 /* Remove high part from the TFtype, leaving the low part as flonum. */
798 /* Convert that to fixed (but not to DItype!) and add it in.
799 Sometimes A comes out negative. This is significant, since
800 A has more bits than a long int does. */
802 v -= (USItype) (- a);
809 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
814 return - __fixunstfdi (-a);
815 return __fixunstfdi (a);
819 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
820 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
821 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
824 __fixunsxfdi (XFtype a)
832 /* Compute high word of result, as a flonum. */
833 b = (a / HIGH_WORD_COEFF);
834 /* Convert that to fixed (but not to DItype!),
835 and shift it into the high word. */
838 /* Remove high part from the XFtype, leaving the low part as flonum. */
840 /* Convert that to fixed (but not to DItype!) and add it in.
841 Sometimes A comes out negative. This is significant, since
842 A has more bits than a long int does. */
844 v -= (USItype) (- a);
851 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
856 return - __fixunsxfdi (-a);
857 return __fixunsxfdi (a);
862 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
863 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
866 __fixunsdfdi (DFtype a)
874 /* Compute high word of result, as a flonum. */
875 b = (a / HIGH_WORD_COEFF);
876 /* Convert that to fixed (but not to DItype!),
877 and shift it into the high word. */
880 /* Remove high part from the DFtype, leaving the low part as flonum. */
882 /* Convert that to fixed (but not to DItype!) and add it in.
883 Sometimes A comes out negative. This is significant, since
884 A has more bits than a long int does. */
886 v -= (USItype) (- a);
898 return - __fixunsdfdi (-a);
899 return __fixunsdfdi (a);
904 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
905 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
908 __fixunssfdi (SFtype original_a)
910 /* Convert the SFtype to a DFtype, because that is surely not going
911 to lose any bits. Some day someone else can write a faster version
912 that avoids converting to DFtype, and verify it really works right. */
913 DFtype a = original_a;
920 /* Compute high word of result, as a flonum. */
921 b = (a / HIGH_WORD_COEFF);
922 /* Convert that to fixed (but not to DItype!),
923 and shift it into the high word. */
926 /* Remove high part from the DFtype, leaving the low part as flonum. */
928 /* Convert that to fixed (but not to DItype!) and add it in.
929 Sometimes A comes out negative. This is significant, since
930 A has more bits than a long int does. */
932 v -= (USItype) (- a);
944 return - __fixunssfdi (-a);
945 return __fixunssfdi (a);
949 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
950 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
951 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
952 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
955 __floatdixf (DItype u)
959 d = (SItype) (u >> WORD_SIZE);
960 d *= HIGH_HALFWORD_COEFF;
961 d *= HIGH_HALFWORD_COEFF;
962 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
968 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
969 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
970 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
971 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
974 __floatditf (DItype u)
978 d = (SItype) (u >> WORD_SIZE);
979 d *= HIGH_HALFWORD_COEFF;
980 d *= HIGH_HALFWORD_COEFF;
981 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
988 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
989 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
990 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
993 __floatdidf (DItype u)
997 d = (SItype) (u >> WORD_SIZE);
998 d *= HIGH_HALFWORD_COEFF;
999 d *= HIGH_HALFWORD_COEFF;
1000 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
1007 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1008 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1009 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1010 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1012 /* Define codes for all the float formats that we know of. Note
1013 that this is copied from real.h. */
1015 #define UNKNOWN_FLOAT_FORMAT 0
1016 #define IEEE_FLOAT_FORMAT 1
1017 #define VAX_FLOAT_FORMAT 2
1018 #define IBM_FLOAT_FORMAT 3
1020 /* Default to IEEE float if not specified. Nearly all machines use it. */
1021 #ifndef HOST_FLOAT_FORMAT
1022 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1025 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1030 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1035 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1041 __floatdisf (DItype u)
1043 /* Do the calculation in DFmode
1044 so that we don't lose any of the precision of the high word
1045 while multiplying it. */
1048 /* Protect against double-rounding error.
1049 Represent any low-order bits, that might be truncated in DFmode,
1050 by a bit that won't be lost. The bit can go in anywhere below the
1051 rounding position of the SFmode. A fixed mask and bit position
1052 handles all usual configurations. It doesn't handle the case
1053 of 128-bit DImode, however. */
1054 if (DF_SIZE < DI_SIZE
1055 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1057 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1058 if (! (- ((DItype) 1 << DF_SIZE) < u
1059 && u < ((DItype) 1 << DF_SIZE)))
1061 if ((USItype) u & (REP_BIT - 1))
1065 f = (SItype) (u >> WORD_SIZE);
1066 f *= HIGH_HALFWORD_COEFF;
1067 f *= HIGH_HALFWORD_COEFF;
1068 f += (USItype) (u & (HIGH_WORD_COEFF - 1));
1074 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1075 /* Reenable the normal types, in case limits.h needs them. */
1088 __fixunsxfsi (XFtype a)
1090 if (a >= - (DFtype) LONG_MIN)
1091 return (SItype) (a + LONG_MIN) - LONG_MIN;
1097 /* Reenable the normal types, in case limits.h needs them. */
1110 __fixunsdfsi (DFtype a)
1112 if (a >= - (DFtype) LONG_MIN)
1113 return (SItype) (a + LONG_MIN) - LONG_MIN;
1119 /* Reenable the normal types, in case limits.h needs them. */
1132 __fixunssfsi (SFtype a)
1134 if (a >= - (SFtype) LONG_MIN)
1135 return (SItype) (a + LONG_MIN) - LONG_MIN;
1140 /* From here on down, the routines use normal data types. */
1142 #define SItype bogus_type
1143 #define USItype bogus_type
1144 #define DItype bogus_type
1145 #define UDItype bogus_type
1146 #define SFtype bogus_type
1147 #define DFtype bogus_type
1159 /* Like bcmp except the sign is meaningful.
1160 Result is negative if S1 is less than S2,
1161 positive if S1 is greater, 0 if S1 and S2 are equal. */
1164 __gcc_bcmp (unsigned char *s1, unsigned char *s2, size_t size)
1168 unsigned char c1 = *s1++, c2 = *s2++;
1185 #if defined(__svr4__) || defined(__alliant__)
1189 /* The Alliant needs the added underscore. */
1190 asm (".globl __builtin_saveregs");
1191 asm ("__builtin_saveregs:");
1192 asm (".globl ___builtin_saveregs");
1193 asm ("___builtin_saveregs:");
1195 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1196 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1197 area and also for a new va_list
1199 /* Save all argument registers in the arg reg save area. The
1200 arg reg save area must have the following layout (according
1212 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1213 asm (" fst.q %f12,16(%sp)");
1215 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1216 asm (" st.l %r17,36(%sp)");
1217 asm (" st.l %r18,40(%sp)");
1218 asm (" st.l %r19,44(%sp)");
1219 asm (" st.l %r20,48(%sp)");
1220 asm (" st.l %r21,52(%sp)");
1221 asm (" st.l %r22,56(%sp)");
1222 asm (" st.l %r23,60(%sp)");
1223 asm (" st.l %r24,64(%sp)");
1224 asm (" st.l %r25,68(%sp)");
1225 asm (" st.l %r26,72(%sp)");
1226 asm (" st.l %r27,76(%sp)");
1228 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1229 va_list structure. Put in into
1230 r16 so that it will be returned
1233 /* Initialize all fields of the new va_list structure. This
1234 structure looks like:
1237 unsigned long ireg_used;
1238 unsigned long freg_used;
1244 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1245 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1246 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1247 asm (" bri %r1"); /* delayed return */
1248 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1250 #else /* not __svr4__ */
1251 #if defined(__PARAGON__)
1253 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1254 * and we stand a better chance of hooking into libraries
1255 * compiled by PGI. [andyp@ssd.intel.com]
1259 asm (".globl __builtin_saveregs");
1260 asm ("__builtin_saveregs:");
1261 asm (".globl ___builtin_saveregs");
1262 asm ("___builtin_saveregs:");
1264 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1265 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1266 area and also for a new va_list
1268 /* Save all argument registers in the arg reg save area. The
1269 arg reg save area must have the following layout (according
1281 asm (" fst.q f8, 0(sp)");
1282 asm (" fst.q f12,16(sp)");
1283 asm (" st.l r16,32(sp)");
1284 asm (" st.l r17,36(sp)");
1285 asm (" st.l r18,40(sp)");
1286 asm (" st.l r19,44(sp)");
1287 asm (" st.l r20,48(sp)");
1288 asm (" st.l r21,52(sp)");
1289 asm (" st.l r22,56(sp)");
1290 asm (" st.l r23,60(sp)");
1291 asm (" st.l r24,64(sp)");
1292 asm (" st.l r25,68(sp)");
1293 asm (" st.l r26,72(sp)");
1294 asm (" st.l r27,76(sp)");
1296 asm (" adds 80,sp,r16"); /* compute the address of the new
1297 va_list structure. Put in into
1298 r16 so that it will be returned
1301 /* Initialize all fields of the new va_list structure. This
1302 structure looks like:
1305 unsigned long ireg_used;
1306 unsigned long freg_used;
1312 asm (" st.l r0, 0(r16)"); /* nfixed */
1313 asm (" st.l r0, 4(r16)"); /* nfloating */
1314 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1315 asm (" bri r1"); /* delayed return */
1316 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1317 #else /* not __PARAGON__ */
1321 asm (".globl ___builtin_saveregs");
1322 asm ("___builtin_saveregs:");
1323 asm (" mov sp,r30");
1324 asm (" andnot 0x0f,sp,sp");
1325 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1327 /* Fill in the __va_struct. */
1328 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1329 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1330 asm (" st.l r18, 8(sp)");
1331 asm (" st.l r19,12(sp)");
1332 asm (" st.l r20,16(sp)");
1333 asm (" st.l r21,20(sp)");
1334 asm (" st.l r22,24(sp)");
1335 asm (" st.l r23,28(sp)");
1336 asm (" st.l r24,32(sp)");
1337 asm (" st.l r25,36(sp)");
1338 asm (" st.l r26,40(sp)");
1339 asm (" st.l r27,44(sp)");
1341 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1342 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1344 /* Fill in the __va_ctl. */
1345 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1346 asm (" st.l r28,84(sp)"); /* pointer to more args */
1347 asm (" st.l r0, 88(sp)"); /* nfixed */
1348 asm (" st.l r0, 92(sp)"); /* nfloating */
1350 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1352 asm (" mov r30,sp");
1353 /* recover stack and pass address to start
1355 #endif /* not __PARAGON__ */
1356 #endif /* not __svr4__ */
1357 #else /* not __i860__ */
1359 asm (".global __builtin_saveregs");
1360 asm ("__builtin_saveregs:");
1361 asm (".global ___builtin_saveregs");
1362 asm ("___builtin_saveregs:");
1363 #ifdef NEED_PROC_COMMAND
1366 asm ("st %i0,[%fp+68]");
1367 asm ("st %i1,[%fp+72]");
1368 asm ("st %i2,[%fp+76]");
1369 asm ("st %i3,[%fp+80]");
1370 asm ("st %i4,[%fp+84]");
1372 asm ("st %i5,[%fp+88]");
1373 #ifdef NEED_TYPE_COMMAND
1374 asm (".type __builtin_saveregs,#function");
1375 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1377 #else /* not __sparc__ */
1378 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1382 asm (" .set nomips16");
1384 asm (" .ent __builtin_saveregs");
1385 asm (" .globl __builtin_saveregs");
1386 asm ("__builtin_saveregs:");
1387 asm (" sw $4,0($30)");
1388 asm (" sw $5,4($30)");
1389 asm (" sw $6,8($30)");
1390 asm (" sw $7,12($30)");
1392 asm (" .end __builtin_saveregs");
1393 #else /* not __mips__, etc. */
1396 __builtin_saveregs ()
1401 #endif /* not __mips__ */
1402 #endif /* not __sparc__ */
1403 #endif /* not __i860__ */
1407 #ifndef inhibit_libc
1409 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1411 /* This is used by the `assert' macro. */
1412 extern void __eprintf (const char *, const char *, unsigned int, const char *)
1413 __attribute__ ((__noreturn__));
1416 __eprintf (const char *string, const char *expression,
1417 unsigned int line, const char *filename)
1419 fprintf (stderr, string, expression, line, filename);
1429 /* Structure emitted by -a */
1433 const char *filename;
1437 const unsigned long *addresses;
1439 /* Older GCC's did not emit these fields. */
1441 const char **functions;
1442 const long *line_nums;
1443 const char **filenames;
1447 #ifdef BLOCK_PROFILER_CODE
1450 #ifndef inhibit_libc
1452 /* Simple minded basic block profiling output dumper for
1453 systems that don't provide tcov support. At present,
1454 it requires atexit and stdio. */
1456 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1460 #include "gbl-ctors.h"
1461 #include "gcov-io.h"
1464 static struct bb *bb_head;
1466 /* Return the number of digits needed to print a value */
1467 /* __inline__ */ static int num_digits (long value, int base)
1469 int minus = (value < 0 && base != 16);
1470 unsigned long v = (minus) ? -value : value;
1484 __bb_exit_func (void)
1486 FILE *da_file, *file;
1493 i = strlen (bb_head->filename) - 3;
1495 if (!strcmp (bb_head->filename+i, ".da"))
1497 /* Must be -fprofile-arcs not -a.
1498 Dump data in a form that gcov expects. */
1502 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1504 /* If the file exists, and the number of counts in it is the same,
1505 then merge them in. */
1507 if ((da_file = fopen (ptr->filename, "r")) != 0)
1511 if (__read_long (&n_counts, da_file, 8) != 0)
1513 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1518 if (n_counts == ptr->ncounts)
1522 for (i = 0; i < n_counts; i++)
1526 if (__read_long (&v, da_file, 8) != 0)
1528 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1532 ptr->counts[i] += v;
1536 if (fclose (da_file) == EOF)
1537 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1540 if ((da_file = fopen (ptr->filename, "w")) == 0)
1542 fprintf (stderr, "arc profiling: Can't open output file %s.\n",
1547 /* ??? Should first write a header to the file. Preferably, a 4 byte
1548 magic number, 4 bytes containing the time the program was
1549 compiled, 4 bytes containing the last modification time of the
1550 source file, and 4 bytes indicating the compiler options used.
1552 That way we can easily verify that the proper source/executable/
1553 data file combination is being used from gcov. */
1555 if (__write_long (ptr->ncounts, da_file, 8) != 0)
1558 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1564 long *count_ptr = ptr->counts;
1566 for (j = ptr->ncounts; j > 0; j--)
1568 if (__write_long (*count_ptr, da_file, 8) != 0)
1576 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1580 if (fclose (da_file) == EOF)
1581 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1588 /* Must be basic block profiling. Emit a human readable output file. */
1590 file = fopen ("bb.out", "a");
1599 /* This is somewhat type incorrect, but it avoids worrying about
1600 exactly where time.h is included from. It should be ok unless
1601 a void * differs from other pointer formats, or if sizeof (long)
1602 is < sizeof (time_t). It would be nice if we could assume the
1603 use of rationale standards here. */
1605 time ((void *) &time_value);
1606 fprintf (file, "Basic block profiling finished on %s\n", ctime ((void *) &time_value));
1608 /* We check the length field explicitly in order to allow compatibility
1609 with older GCC's which did not provide it. */
1611 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1614 int func_p = (ptr->nwords >= sizeof (struct bb)
1615 && ptr->nwords <= 1000
1617 int line_p = (func_p && ptr->line_nums);
1618 int file_p = (func_p && ptr->filenames);
1619 int addr_p = (ptr->addresses != 0);
1620 long ncounts = ptr->ncounts;
1626 int blk_len = num_digits (ncounts, 10);
1631 fprintf (file, "File %s, %ld basic blocks \n\n",
1632 ptr->filename, ncounts);
1634 /* Get max values for each field. */
1635 for (i = 0; i < ncounts; i++)
1640 if (cnt_max < ptr->counts[i])
1641 cnt_max = ptr->counts[i];
1643 if (addr_p && addr_max < ptr->addresses[i])
1644 addr_max = ptr->addresses[i];
1646 if (line_p && line_max < ptr->line_nums[i])
1647 line_max = ptr->line_nums[i];
1651 p = (ptr->functions[i]) ? (ptr->functions[i]) : "<none>";
1659 p = (ptr->filenames[i]) ? (ptr->filenames[i]) : "<none>";
1666 addr_len = num_digits (addr_max, 16);
1667 cnt_len = num_digits (cnt_max, 10);
1668 line_len = num_digits (line_max, 10);
1670 /* Now print out the basic block information. */
1671 for (i = 0; i < ncounts; i++)
1674 " Block #%*d: executed %*ld time(s)",
1676 cnt_len, ptr->counts[i]);
1679 fprintf (file, " address= 0x%.*lx", addr_len,
1683 fprintf (file, " function= %-*s", func_len,
1684 (ptr->functions[i]) ? ptr->functions[i] : "<none>");
1687 fprintf (file, " line= %*ld", line_len, ptr->line_nums[i]);
1690 fprintf (file, " file= %s",
1691 (ptr->filenames[i]) ? ptr->filenames[i] : "<none>");
1693 fprintf (file, "\n");
1696 fprintf (file, "\n");
1700 fprintf (file, "\n\n");
1706 __bb_init_func (struct bb *blocks)
1708 /* User is supposed to check whether the first word is non-0,
1709 but just in case.... */
1711 if (blocks->zero_word)
1715 /* Initialize destructor. */
1717 ON_EXIT (__bb_exit_func, 0);
1720 /* Set up linked list. */
1721 blocks->zero_word = 1;
1722 blocks->next = bb_head;
1726 #ifndef MACHINE_STATE_SAVE
1727 #define MACHINE_STATE_SAVE(ID)
1729 #ifndef MACHINE_STATE_RESTORE
1730 #define MACHINE_STATE_RESTORE(ID)
1733 /* Number of buckets in hashtable of basic block addresses. */
1735 #define BB_BUCKETS 311
1737 /* Maximum length of string in file bb.in. */
1739 #define BBINBUFSIZE 500
1741 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1742 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1744 #define BBINBUFSIZESTR "499"
1748 struct bb_edge *next;
1749 unsigned long src_addr;
1750 unsigned long dst_addr;
1751 unsigned long count;
1756 TRACE_KEEP = 0, TRACE_ON = 1, TRACE_OFF = 2
1761 struct bb_func *next;
1764 enum bb_func_mode mode;
1767 /* This is the connection to the outside world.
1768 The BLOCK_PROFILER macro must set __bb.blocks
1769 and __bb.blockno. */
1772 unsigned long blockno;
1776 /* Vars to store addrs of source and destination basic blocks
1779 static unsigned long bb_src = 0;
1780 static unsigned long bb_dst = 0;
1782 static FILE *bb_tracefile = (FILE *) 0;
1783 static struct bb_edge **bb_hashbuckets = (struct bb_edge **) 0;
1784 static struct bb_func *bb_func_head = (struct bb_func *) 0;
1785 static unsigned long bb_callcount = 0;
1786 static int bb_mode = 0;
1788 static unsigned long *bb_stack = (unsigned long *) 0;
1789 static size_t bb_stacksize = 0;
1791 static int reported = 0;
1794 Always : Print execution frequencies of basic blocks
1796 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1797 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1798 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1799 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1804 /*#include <sys/types.h>*/
1805 #include <sys/stat.h>
1806 /*#include <malloc.h>*/
1808 /* Commands executed by gopen. */
1810 #define GOPENDECOMPRESS "gzip -cd "
1811 #define GOPENCOMPRESS "gzip -c >"
1813 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1814 If it does not compile, simply replace gopen by fopen and delete
1815 '.gz' from any first parameter to gopen. */
1818 gopen (char *fn, char *mode)
1826 if (mode[0] != 'r' && mode[0] != 'w')
1829 p = fn + strlen (fn)-1;
1830 use_gzip = ((p[-1] == '.' && (p[0] == 'Z' || p[0] == 'z'))
1831 || (p[-2] == '.' && p[-1] == 'g' && p[0] == 'z'));
1838 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1839 + sizeof (GOPENDECOMPRESS));
1840 strcpy (s, GOPENDECOMPRESS);
1841 strcpy (s + (sizeof (GOPENDECOMPRESS)-1), fn);
1842 f = popen (s, mode);
1850 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1851 + sizeof (GOPENCOMPRESS));
1852 strcpy (s, GOPENCOMPRESS);
1853 strcpy (s + (sizeof (GOPENCOMPRESS)-1), fn);
1854 if (!(f = popen (s, mode)))
1855 f = fopen (s, mode);
1862 return fopen (fn, mode);
1872 if (!fstat (fileno (f), &buf) && S_ISFIFO (buf.st_mode))
1880 #endif /* HAVE_POPEN */
1882 /* Called once per program. */
1885 __bb_exit_trace_func ()
1887 FILE *file = fopen ("bb.out", "a");
1900 gclose (bb_tracefile);
1902 fclose (bb_tracefile);
1903 #endif /* HAVE_POPEN */
1906 /* Check functions in `bb.in'. */
1911 const struct bb_func *p;
1912 int printed_something = 0;
1916 /* This is somewhat type incorrect. */
1917 time ((void *) &time_value);
1919 for (p = bb_func_head; p != (struct bb_func *) 0; p = p->next)
1921 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1923 if (!ptr->filename || (p->filename != (char *) 0 && strcmp (p->filename, ptr->filename)))
1925 for (blk = 0; blk < ptr->ncounts; blk++)
1927 if (!strcmp (p->funcname, ptr->functions[blk]))
1932 if (!printed_something)
1934 fprintf (file, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value));
1935 printed_something = 1;
1938 fprintf (file, "\tFunction %s", p->funcname);
1940 fprintf (file, " of file %s", p->filename);
1941 fprintf (file, "\n" );
1946 if (printed_something)
1947 fprintf (file, "\n");
1953 if (!bb_hashbuckets)
1957 fprintf (stderr, "Profiler: out of memory\n");
1967 unsigned long addr_max = 0;
1968 unsigned long cnt_max = 0;
1972 /* This is somewhat type incorrect, but it avoids worrying about
1973 exactly where time.h is included from. It should be ok unless
1974 a void * differs from other pointer formats, or if sizeof (long)
1975 is < sizeof (time_t). It would be nice if we could assume the
1976 use of rationale standards here. */
1978 time ((void *) &time_value);
1979 fprintf (file, "Basic block jump tracing");
1981 switch (bb_mode & 12)
1984 fprintf (file, " (with call)");
1988 /* Print nothing. */
1992 fprintf (file, " (with call & ret)");
1996 fprintf (file, " (with ret)");
2000 fprintf (file, " finished on %s\n", ctime ((void *) &time_value));
2002 for (i = 0; i < BB_BUCKETS; i++)
2004 struct bb_edge *bucket = bb_hashbuckets[i];
2005 for ( ; bucket; bucket = bucket->next )
2007 if (addr_max < bucket->src_addr)
2008 addr_max = bucket->src_addr;
2009 if (addr_max < bucket->dst_addr)
2010 addr_max = bucket->dst_addr;
2011 if (cnt_max < bucket->count)
2012 cnt_max = bucket->count;
2015 addr_len = num_digits (addr_max, 16);
2016 cnt_len = num_digits (cnt_max, 10);
2018 for ( i = 0; i < BB_BUCKETS; i++)
2020 struct bb_edge *bucket = bb_hashbuckets[i];
2021 for ( ; bucket; bucket = bucket->next )
2023 fprintf (file, "Jump from block 0x%.*lx to "
2024 "block 0x%.*lx executed %*lu time(s)\n",
2025 addr_len, bucket->src_addr,
2026 addr_len, bucket->dst_addr,
2027 cnt_len, bucket->count);
2031 fprintf (file, "\n");
2039 /* Free allocated memory. */
2044 struct bb_func *old = f;
2047 if (old->funcname) free (old->funcname);
2048 if (old->filename) free (old->filename);
2059 for (i = 0; i < BB_BUCKETS; i++)
2061 struct bb_edge *old, *bucket = bb_hashbuckets[i];
2066 bucket = bucket->next;
2070 free (bb_hashbuckets);
2073 for (b = bb_head; b; b = b->next)
2074 if (b->flags) free (b->flags);
2077 /* Called once per program. */
2084 char buf[BBINBUFSIZE];
2087 enum bb_func_mode m;
2090 /* Initialize destructor. */
2091 ON_EXIT (__bb_exit_func, 0);
2094 if (!(file = fopen ("bb.in", "r")))
2097 while(fscanf (file, " %" BBINBUFSIZESTR "s ", buf) != EOF)
2109 if (!strcmp (p, "__bb_trace__"))
2111 else if (!strcmp (p, "__bb_jumps__"))
2113 else if (!strcmp (p, "__bb_hidecall__"))
2115 else if (!strcmp (p, "__bb_showret__"))
2119 struct bb_func *f = (struct bb_func *) malloc (sizeof (struct bb_func));
2123 f->next = bb_func_head;
2124 if ((pos = strchr (p, ':')))
2126 if (!(f->funcname = (char *) malloc (strlen (pos+1)+1)))
2128 strcpy (f->funcname, pos+1);
2130 if ((f->filename = (char *) malloc (l+1)))
2132 strncpy (f->filename, p, l);
2133 f->filename[l] = '\0';
2136 f->filename = (char *) 0;
2140 if (!(f->funcname = (char *) malloc (strlen (p)+1)))
2142 strcpy (f->funcname, p);
2143 f->filename = (char *) 0;
2155 bb_tracefile = gopen ("bbtrace.gz", "w");
2160 bb_tracefile = fopen ("bbtrace", "w");
2162 #endif /* HAVE_POPEN */
2166 bb_hashbuckets = (struct bb_edge **)
2167 malloc (BB_BUCKETS * sizeof (struct bb_edge *));
2169 memset (bb_hashbuckets, 0, BB_BUCKETS * sizeof (struct bb_edge *));
2175 bb_stack = (unsigned long *) malloc (bb_stacksize * sizeof (*bb_stack));
2179 /* Initialize destructor. */
2180 ON_EXIT (__bb_exit_trace_func, 0);
2185 /* Called upon entering a basic block. */
2190 struct bb_edge *bucket;
2192 MACHINE_STATE_SAVE("1")
2194 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2197 bb_dst = __bb.blocks->addresses[__bb.blockno];
2198 __bb.blocks->counts[__bb.blockno]++;
2202 fwrite (&bb_dst, sizeof (unsigned long), 1, bb_tracefile);
2207 struct bb_edge **startbucket, **oldnext;
2209 oldnext = startbucket
2210 = & bb_hashbuckets[ (((int) bb_src*8) ^ (int) bb_dst) % BB_BUCKETS ];
2211 bucket = *startbucket;
2213 for (bucket = *startbucket; bucket;
2214 oldnext = &(bucket->next), bucket = *oldnext)
2216 if (bucket->src_addr == bb_src
2217 && bucket->dst_addr == bb_dst)
2220 *oldnext = bucket->next;
2221 bucket->next = *startbucket;
2222 *startbucket = bucket;
2227 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2233 fprintf (stderr, "Profiler: out of memory\n");
2240 bucket->src_addr = bb_src;
2241 bucket->dst_addr = bb_dst;
2242 bucket->next = *startbucket;
2243 *startbucket = bucket;
2254 MACHINE_STATE_RESTORE("1")
2258 /* Called when returning from a function and `__bb_showret__' is set. */
2261 __bb_trace_func_ret ()
2263 struct bb_edge *bucket;
2265 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2270 struct bb_edge **startbucket, **oldnext;
2272 oldnext = startbucket
2273 = & bb_hashbuckets[ (((int) bb_dst * 8) ^ (int) bb_src) % BB_BUCKETS ];
2274 bucket = *startbucket;
2276 for (bucket = *startbucket; bucket;
2277 oldnext = &(bucket->next), bucket = *oldnext)
2279 if (bucket->src_addr == bb_dst
2280 && bucket->dst_addr == bb_src)
2283 *oldnext = bucket->next;
2284 bucket->next = *startbucket;
2285 *startbucket = bucket;
2290 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2296 fprintf (stderr, "Profiler: out of memory\n");
2303 bucket->src_addr = bb_dst;
2304 bucket->dst_addr = bb_src;
2305 bucket->next = *startbucket;
2306 *startbucket = bucket;
2319 /* Called upon entering the first function of a file. */
2322 __bb_init_file (struct bb *blocks)
2325 const struct bb_func *p;
2326 long blk, ncounts = blocks->ncounts;
2327 const char **functions = blocks->functions;
2329 /* Set up linked list. */
2330 blocks->zero_word = 1;
2331 blocks->next = bb_head;
2336 || !(blocks->flags = (char *) malloc (sizeof (char) * blocks->ncounts)))
2339 for (blk = 0; blk < ncounts; blk++)
2340 blocks->flags[blk] = 0;
2342 for (blk = 0; blk < ncounts; blk++)
2344 for (p = bb_func_head; p; p = p->next)
2346 if (!strcmp (p->funcname, functions[blk])
2347 && (!p->filename || !strcmp (p->filename, blocks->filename)))
2349 blocks->flags[blk] |= p->mode;
2356 /* Called when exiting from a function. */
2362 MACHINE_STATE_SAVE("2")
2366 if ((bb_mode & 12) && bb_stacksize > bb_callcount)
2368 bb_src = bb_stack[bb_callcount];
2370 __bb_trace_func_ret ();
2376 MACHINE_STATE_RESTORE("2")
2380 /* Called when entering a function. */
2383 __bb_init_trace_func (struct bb *blocks, unsigned long blockno)
2385 static int trace_init = 0;
2387 MACHINE_STATE_SAVE("3")
2389 if (!blocks->zero_word)
2396 __bb_init_file (blocks);
2406 if (bb_callcount >= bb_stacksize)
2408 size_t newsize = bb_callcount + 100;
2410 bb_stack = (unsigned long *) realloc (bb_stack, newsize);
2415 fprintf (stderr, "Profiler: out of memory\n");
2419 goto stack_overflow;
2421 bb_stacksize = newsize;
2423 bb_stack[bb_callcount] = bb_src;
2434 else if (blocks->flags && (blocks->flags[blockno] & TRACE_ON))
2440 bb_stack[bb_callcount] = bb_src;
2443 MACHINE_STATE_RESTORE("3")
2446 #endif /* not inhibit_libc */
2447 #endif /* not BLOCK_PROFILER_CODE */
2451 unsigned int __shtab[] = {
2452 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2453 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2454 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2455 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2456 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2457 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2458 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2459 0x10000000, 0x20000000, 0x40000000, 0x80000000
2463 #ifdef L_clear_cache
2464 /* Clear part of an instruction cache. */
2466 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2469 __clear_cache (char *beg, char *end)
2471 #ifdef CLEAR_INSN_CACHE
2472 CLEAR_INSN_CACHE (beg, end);
2474 #ifdef INSN_CACHE_SIZE
2475 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
2476 static int initialized;
2480 typedef (*function_ptr) ();
2482 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2483 /* It's cheaper to clear the whole cache.
2484 Put in a series of jump instructions so that calling the beginning
2485 of the cache will clear the whole thing. */
2489 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2490 & -INSN_CACHE_LINE_WIDTH);
2491 int end_ptr = ptr + INSN_CACHE_SIZE;
2493 while (ptr < end_ptr)
2495 *(INSTRUCTION_TYPE *)ptr
2496 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
2497 ptr += INSN_CACHE_LINE_WIDTH;
2499 *(INSTRUCTION_TYPE *) (ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
2504 /* Call the beginning of the sequence. */
2505 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2506 & -INSN_CACHE_LINE_WIDTH))
2509 #else /* Cache is large. */
2513 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2514 & -INSN_CACHE_LINE_WIDTH);
2516 while (ptr < (int) array + sizeof array)
2518 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
2519 ptr += INSN_CACHE_LINE_WIDTH;
2525 /* Find the location in array that occupies the same cache line as BEG. */
2527 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
2528 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
2529 & -INSN_CACHE_PLANE_SIZE)
2532 /* Compute the cache alignment of the place to stop clearing. */
2533 #if 0 /* This is not needed for gcc's purposes. */
2534 /* If the block to clear is bigger than a cache plane,
2535 we clear the entire cache, and OFFSET is already correct. */
2536 if (end < beg + INSN_CACHE_PLANE_SIZE)
2538 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
2539 & -INSN_CACHE_LINE_WIDTH)
2540 & (INSN_CACHE_PLANE_SIZE - 1));
2542 #if INSN_CACHE_DEPTH > 1
2543 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
2544 if (end_addr <= start_addr)
2545 end_addr += INSN_CACHE_PLANE_SIZE;
2547 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
2549 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
2550 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
2552 while (addr != stop)
2554 /* Call the return instruction at ADDR. */
2555 ((function_ptr) addr) ();
2557 addr += INSN_CACHE_LINE_WIDTH;
2560 #else /* just one plane */
2563 /* Call the return instruction at START_ADDR. */
2564 ((function_ptr) start_addr) ();
2566 start_addr += INSN_CACHE_LINE_WIDTH;
2568 while ((start_addr % INSN_CACHE_SIZE) != offset);
2569 #endif /* just one plane */
2570 #endif /* Cache is large */
2571 #endif /* Cache exists */
2572 #endif /* CLEAR_INSN_CACHE */
2575 #endif /* L_clear_cache */
2579 /* Jump to a trampoline, loading the static chain address. */
2581 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2593 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2597 mprotect (char *addr, int len, int prot)
2614 if (VirtualProtect (addr, len, np, &op))
2620 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2622 #ifdef TRANSFER_FROM_TRAMPOLINE
2623 TRANSFER_FROM_TRAMPOLINE
2626 #if defined (NeXT) && defined (__MACH__)
2628 /* Make stack executable so we can call trampolines on stack.
2629 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2633 #include <mach/mach.h>
2637 __enable_execute_stack (char *addr)
2640 char *eaddr = addr + TRAMPOLINE_SIZE;
2641 vm_address_t a = (vm_address_t) addr;
2643 /* turn on execute access on stack */
2644 r = vm_protect (task_self (), a, TRAMPOLINE_SIZE, FALSE, VM_PROT_ALL);
2645 if (r != KERN_SUCCESS)
2647 mach_error("vm_protect VM_PROT_ALL", r);
2651 /* We inline the i-cache invalidation for speed */
2653 #ifdef CLEAR_INSN_CACHE
2654 CLEAR_INSN_CACHE (addr, eaddr);
2656 __clear_cache ((int) addr, (int) eaddr);
2660 #endif /* defined (NeXT) && defined (__MACH__) */
2664 /* Make stack executable so we can call trampolines on stack.
2665 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2667 #include <sys/mman.h>
2668 #include <sys/vmparam.h>
2669 #include <machine/machparam.h>
2672 __enable_execute_stack ()
2675 static unsigned lowest = USRSTACK;
2676 unsigned current = (unsigned) &fp & -NBPG;
2678 if (lowest > current)
2680 unsigned len = lowest - current;
2681 mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
2685 /* Clear instruction cache in case an old trampoline is in it. */
2688 #endif /* __convex__ */
2692 /* Modified from the convex -code above. */
2694 #include <sys/param.h>
2696 #include <sys/m88kbcs.h>
2699 __enable_execute_stack ()
2702 static unsigned long lowest = USRSTACK;
2703 unsigned long current = (unsigned long) &save_errno & -NBPC;
2705 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2706 address is seen as 'negative'. That is the case with the stack. */
2709 if (lowest > current)
2711 unsigned len=lowest-current;
2712 memctl(current,len,MCT_TEXT);
2716 memctl(current,NBPC,MCT_TEXT);
2720 #endif /* __sysV88__ */
2724 #include <sys/signal.h>
2727 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2728 so define it here, because we need it in __clear_insn_cache below */
2729 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2730 hence we enable this stuff only if MCT_TEXT is #define'd. */
2745 /* Clear instruction cache so we can call trampolines on stack.
2746 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2749 __clear_insn_cache ()
2754 /* Preserve errno, because users would be surprised to have
2755 errno changing without explicitly calling any system-call. */
2758 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2759 No need to use an address derived from _start or %sp, as 0 works also. */
2760 memctl(0, 4096, MCT_TEXT);
2765 #endif /* __sysV68__ */
2769 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2771 #include <sys/mman.h>
2772 #include <sys/types.h>
2773 #include <sys/param.h>
2774 #include <sys/vmmac.h>
2776 /* Modified from the convex -code above.
2777 mremap promises to clear the i-cache. */
2780 __enable_execute_stack ()
2783 if (mprotect (((unsigned int)&fp/PAGSIZ)*PAGSIZ, PAGSIZ,
2784 PROT_READ|PROT_WRITE|PROT_EXEC))
2786 perror ("mprotect in __enable_execute_stack");
2791 #endif /* __pyr__ */
2793 #if defined (sony_news) && defined (SYSTYPE_BSD)
2796 #include <sys/types.h>
2797 #include <sys/param.h>
2798 #include <syscall.h>
2799 #include <machine/sysnews.h>
2801 /* cacheflush function for NEWS-OS 4.2.
2802 This function is called from trampoline-initialize code
2803 defined in config/mips/mips.h. */
2806 cacheflush (char *beg, int size, int flag)
2808 if (syscall (SYS_sysnews, NEWS_CACHEFLUSH, beg, size, FLUSH_BCACHE))
2810 perror ("cache_flush");
2816 #endif /* sony_news */
2817 #endif /* L_trampoline */
2822 #include "gbl-ctors.h"
2823 /* Some systems use __main in a way incompatible with its use in gcc, in these
2824 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2825 give the same symbol without quotes for an alternative entry point. You
2826 must define both, or neither. */
2828 #define NAME__MAIN "__main"
2829 #define SYMBOL__MAIN __main
2832 #ifdef INIT_SECTION_ASM_OP
2833 #undef HAS_INIT_SECTION
2834 #define HAS_INIT_SECTION
2837 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2838 /* Run all the global destructors on exit from the program. */
2841 __do_global_dtors ()
2843 #ifdef DO_GLOBAL_DTORS_BODY
2844 DO_GLOBAL_DTORS_BODY;
2846 static func_ptr *p = __DTOR_LIST__ + 1;
2856 #ifndef HAS_INIT_SECTION
2857 /* Run all the global constructors on entry to the program. */
2860 #define ON_EXIT(a, b)
2862 /* Make sure the exit routine is pulled in to define the globals as
2863 bss symbols, just in case the linker does not automatically pull
2864 bss definitions from the library. */
2866 extern int _exit_dummy_decl;
2867 int *_exit_dummy_ref = &_exit_dummy_decl;
2868 #endif /* ON_EXIT */
2871 __do_global_ctors ()
2873 DO_GLOBAL_CTORS_BODY;
2874 ON_EXIT (__do_global_dtors, 0);
2876 #endif /* no HAS_INIT_SECTION */
2878 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2879 /* Subroutine called automatically by `main'.
2880 Compiling a global function named `main'
2881 produces an automatic call to this function at the beginning.
2883 For many systems, this routine calls __do_global_ctors.
2884 For systems which support a .init section we use the .init section
2885 to run __do_global_ctors, so we need not do anything here. */
2890 /* Support recursive calls to `main': run initializers just once. */
2891 static int initialized;
2895 __do_global_ctors ();
2898 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2900 #endif /* L__main */
2901 #endif /* __CYGWIN__ */
2905 #include "gbl-ctors.h"
2907 /* Provide default definitions for the lists of constructors and
2908 destructors, so that we don't get linker errors. These symbols are
2909 intentionally bss symbols, so that gld and/or collect will provide
2910 the right values. */
2912 /* We declare the lists here with two elements each,
2913 so that they are valid empty lists if no other definition is loaded.
2915 If we are using the old "set" extensions to have the gnu linker
2916 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2917 must be in the bss/common section.
2919 Long term no port should use those extensions. But many still do. */
2920 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2921 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
2922 func_ptr __CTOR_LIST__[2] = {0, 0};
2923 func_ptr __DTOR_LIST__[2] = {0, 0};
2925 func_ptr __CTOR_LIST__[2];
2926 func_ptr __DTOR_LIST__[2];
2928 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2929 #endif /* L_ctors */
2933 #include "gbl-ctors.h"
2939 int _exit_dummy_decl = 0; /* prevent compiler & linker warnings */
2947 static func_ptr *atexit_chain = 0;
2948 static long atexit_chain_length = 0;
2949 static volatile long last_atexit_chain_slot = -1;
2951 int atexit (func_ptr func)
2953 if (++last_atexit_chain_slot == atexit_chain_length)
2955 atexit_chain_length += 32;
2957 atexit_chain = (func_ptr *) realloc (atexit_chain, atexit_chain_length
2958 * sizeof (func_ptr));
2960 atexit_chain = (func_ptr *) malloc (atexit_chain_length
2961 * sizeof (func_ptr));
2964 atexit_chain_length = 0;
2965 last_atexit_chain_slot = -1;
2970 atexit_chain[last_atexit_chain_slot] = func;
2973 #endif /* NEED_ATEXIT */
2975 /* If we have no known way of registering our own __do_global_dtors
2976 routine so that it will be invoked at program exit time, then we
2977 have to define our own exit routine which will get this to happen. */
2979 extern void __do_global_dtors ();
2980 extern void __bb_exit_func ();
2981 extern void _cleanup ();
2982 extern void _exit () __attribute__ ((noreturn));
2987 #if !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF)
2991 for ( ; last_atexit_chain_slot-- >= 0; )
2993 (*atexit_chain[last_atexit_chain_slot + 1]) ();
2994 atexit_chain[last_atexit_chain_slot + 1] = 0;
2996 free (atexit_chain);
2999 #else /* No NEED_ATEXIT */
3000 __do_global_dtors ();
3001 #endif /* No NEED_ATEXIT */
3002 #endif /* !defined (INIT_SECTION_ASM_OP) || !defined (OBJECT_FORMAT_ELF) */
3003 /* In gbl-ctors.h, ON_EXIT is defined if HAVE_ATEXIT is defined. In
3004 __bb_init_func and _bb_init_prg, __bb_exit_func is registered with
3005 ON_EXIT if ON_EXIT is defined. Thus we must not call __bb_exit_func here
3006 if HAVE_ATEXIT is defined. */
3008 #ifndef inhibit_libc
3011 #endif /* !HAVE_ATEXIT */
3020 #else /* ON_EXIT defined */
3021 int _exit_dummy_decl = 0; /* prevent compiler & linker warnings */
3023 # ifndef HAVE_ATEXIT
3024 /* Provide a fake for atexit() using ON_EXIT. */
3025 int atexit (func_ptr func)
3027 return ON_EXIT (func, NULL);
3029 # endif /* HAVE_ATEXIT */
3030 #endif /* ON_EXIT defined */
3038 /* Shared exception handling support routines. */
3040 extern void __default_terminate (void) __attribute__ ((__noreturn__));
3043 __default_terminate ()
3048 void (*__terminate_func)() = __default_terminate;
3053 (*__terminate_func)();
3057 __throw_type_match (void *catch_type, void *throw_type, void *obj)
3060 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3061 catch_type, throw_type);
3063 if (strcmp ((const char *)catch_type, (const char *)throw_type) == 0)
3074 /* Include definitions of EH context and table layout */
3076 #include "eh-common.h"
3077 #ifndef inhibit_libc
3081 /* Allocate and return a new EH context structure. */
3083 extern void __throw ();
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)
3111 static __gthread_key_t eh_context_key;
3113 /* Destructor for struct eh_context. */
3115 eh_context_free (void *ptr)
3117 __gthread_key_dtor (eh_context_key, ptr);
3123 /* Pointer to function to return EH context. */
3125 static struct eh_context *eh_context_initialize ();
3126 static struct eh_context *eh_context_static ();
3128 static struct eh_context *eh_context_specific ();
3131 static struct eh_context *(*get_eh_context) () = &eh_context_initialize;
3133 /* Routine to get EH context.
3134 This one will simply call the function pointer. */
3139 return (void *) (*get_eh_context) ();
3142 /* Get and set the language specific info pointer. */
3147 struct eh_context *eh = (*get_eh_context) ();
3153 eh_threads_initialize ()
3155 /* Try to create the key. If it fails, revert to static method,
3156 otherwise start using thread specific EH contexts. */
3157 if (__gthread_key_create (&eh_context_key, &eh_context_free) == 0)
3158 get_eh_context = &eh_context_specific;
3160 get_eh_context = &eh_context_static;
3162 #endif /* no __GTHREADS */
3164 /* Initialize EH context.
3165 This will be called only once, since we change GET_EH_CONTEXT
3166 pointer to another routine. */
3168 static struct eh_context *
3169 eh_context_initialize ()
3173 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
3174 /* Make sure that get_eh_context does not point to us anymore.
3175 Some systems have dummy thread routines in their libc that
3176 return a success (Solaris 2.6 for example). */
3177 if (__gthread_once (&once, eh_threads_initialize) != 0
3178 || get_eh_context == &eh_context_initialize)
3180 /* Use static version of EH context. */
3181 get_eh_context = &eh_context_static;
3184 #else /* no __GTHREADS */
3186 /* Use static version of EH context. */
3187 get_eh_context = &eh_context_static;
3189 #endif /* no __GTHREADS */
3191 return (*get_eh_context) ();
3194 /* Return a static EH context. */
3196 static struct eh_context *
3197 eh_context_static ()
3199 static struct eh_context eh;
3200 static int initialized;
3201 static void *top_elt[2];
3206 memset (&eh, 0, sizeof eh);
3207 eh.dynamic_handler_chain = top_elt;
3213 /* Return a thread specific EH context. */
3215 static struct eh_context *
3216 eh_context_specific ()
3218 struct eh_context *eh;
3219 eh = (struct eh_context *) __gthread_getspecific (eh_context_key);
3222 eh = new_eh_context ();
3223 if (__gthread_setspecific (eh_context_key, (void *) eh) != 0)
3231 /* Support routines for setjmp/longjmp exception handling. */
3233 /* Calls to __sjthrow are generated by the compiler when an exception
3234 is raised when using the setjmp/longjmp exception handling codegen
3237 #ifdef DONT_USE_BUILTIN_SETJMP
3238 extern void longjmp (void *, int);
3241 /* Routine to get the head of the current thread's dynamic handler chain
3242 use for exception handling. */
3245 __get_dynamic_handler_chain ()
3247 struct eh_context *eh = (*get_eh_context) ();
3248 return &eh->dynamic_handler_chain;
3251 /* This is used to throw an exception when the setjmp/longjmp codegen
3252 method is used for exception handling.
3254 We call __terminate if there are no handlers left. Otherwise we run the
3255 cleanup actions off the dynamic cleanup stack, and pop the top of the
3256 dynamic handler chain, and use longjmp to transfer back to the associated
3259 extern void __sjthrow (void) __attribute__ ((__noreturn__));
3264 struct eh_context *eh = (*get_eh_context) ();
3265 void ***dhc = &eh->dynamic_handler_chain;
3267 void (*func)(void *, int);
3271 /* The cleanup chain is one word into the buffer. Get the cleanup
3273 cleanup = (void***)&(*dhc)[1];
3275 /* If there are any cleanups in the chain, run them now. */
3279 void **buf = (void**)store;
3284 #ifdef DONT_USE_BUILTIN_SETJMP
3285 if (! setjmp (&buf[2]))
3287 if (! __builtin_setjmp (&buf[2]))
3293 func = (void(*)(void*, int))cleanup[0][1];
3294 arg = (void*)cleanup[0][2];
3296 /* Update this before running the cleanup. */
3297 cleanup[0] = (void **)cleanup[0][0];
3310 /* We must call terminate if we try and rethrow an exception, when
3311 there is no exception currently active and when there are no
3313 if (! eh->info || (*dhc)[0] == 0)
3316 /* Find the jmpbuf associated with the top element of the dynamic
3317 handler chain. The jumpbuf starts two words into the buffer. */
3318 jmpbuf = &(*dhc)[2];
3320 /* Then we pop the top element off the dynamic handler chain. */
3321 *dhc = (void**)(*dhc)[0];
3323 /* And then we jump to the handler. */
3325 #ifdef DONT_USE_BUILTIN_SETJMP
3326 longjmp (jmpbuf, 1);
3328 __builtin_longjmp (jmpbuf, 1);
3332 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3333 handler, then pop the handler off the dynamic handler stack, and
3334 then throw. This is used to skip the first handler, and transfer
3335 control to the next handler in the dynamic handler stack. */
3337 extern void __sjpopnthrow (void) __attribute__ ((__noreturn__));
3342 struct eh_context *eh = (*get_eh_context) ();
3343 void ***dhc = &eh->dynamic_handler_chain;
3344 void (*func)(void *, int);
3348 /* The cleanup chain is one word into the buffer. Get the cleanup
3350 cleanup = (void***)&(*dhc)[1];
3352 /* If there are any cleanups in the chain, run them now. */
3356 void **buf = (void**)store;
3361 #ifdef DONT_USE_BUILTIN_SETJMP
3362 if (! setjmp (&buf[2]))
3364 if (! __builtin_setjmp (&buf[2]))
3370 func = (void(*)(void*, int))cleanup[0][1];
3371 arg = (void*)cleanup[0][2];
3373 /* Update this before running the cleanup. */
3374 cleanup[0] = (void **)cleanup[0][0];
3387 /* Then we pop the top element off the dynamic handler chain. */
3388 *dhc = (void**)(*dhc)[0];
3393 /* Support code for all exception region-based exception handling. */
3396 __eh_rtime_match (void *rtime)
3399 __eh_matcher matcher;
3402 info = *(__get_eh_info ());
3403 matcher = ((__eh_info *)info)->match_function;
3406 #ifndef inhibit_libc
3407 fprintf (stderr, "Internal Compiler Bug: No runtime type matcher.");
3411 ret = (*matcher) (info, rtime, (void *)0);
3412 return (ret != NULL);
3415 /* This value identifies the place from which an exception is being
3418 #ifdef EH_TABLE_LOOKUP
3424 #ifdef DWARF2_UNWIND_INFO
3427 /* Return the table version of an exception descriptor */
3430 __get_eh_table_version (exception_descriptor *table)
3432 return table->lang.version;
3435 /* Return the originating table language of an exception descriptor */
3438 __get_eh_table_language (exception_descriptor *table)
3440 return table->lang.language;
3443 /* This routine takes a PC and a pointer to the exception region TABLE for
3444 its translation unit, and returns the address of the exception handler
3445 associated with the closest exception table handler entry associated
3446 with that PC, or 0 if there are no table entries the PC fits in.
3448 In the advent of a tie, we have to give the last entry, as it represents
3452 old_find_exception_handler (void *pc, old_exception_table *table)
3459 /* We can't do a binary search because the table isn't guaranteed
3460 to be sorted from function to function. */
3461 for (pos = 0; table[pos].start_region != (void *) -1; ++pos)
3463 if (table[pos].start_region <= pc && table[pos].end_region > pc)
3465 /* This can apply. Make sure it is at least as small as
3466 the previous best. */
3467 if (best == -1 || (table[pos].end_region <= table[best].end_region
3468 && table[pos].start_region >= table[best].start_region))
3471 /* But it is sorted by starting PC within a function. */
3472 else if (best >= 0 && table[pos].start_region > pc)
3476 return table[best].exception_handler;
3482 /* find_exception_handler finds the correct handler, if there is one, to
3483 handle an exception.
3484 returns a pointer to the handler which controlled should be transferred
3485 to, or NULL if there is nothing left.
3487 PC - pc where the exception originates. If this is a rethrow,
3488 then this starts out as a pointer to the exception table
3489 entry we wish to rethrow out of.
3490 TABLE - exception table for the current module.
3491 EH_INFO - eh info pointer for this exception.
3492 RETHROW - 1 if this is a rethrow. (see incoming value of PC).
3493 CLEANUP - returned flag indicating whether this is a cleanup handler.
3496 find_exception_handler (void *pc, exception_descriptor *table,
3497 __eh_info *eh_info, int rethrow, int *cleanup)
3500 void *retval = NULL;
3505 /* The new model assumed the table is sorted inner-most out so the
3506 first region we find which matches is the correct one */
3508 exception_table *tab = &(table->table[0]);
3510 /* Subtract 1 from the PC to avoid hitting the next region */
3513 /* pc is actually the region table entry to rethrow out of */
3514 pos = ((exception_table *) pc) - tab;
3515 pc = ((exception_table *) pc)->end_region - 1;
3517 /* The label is always on the LAST handler entry for a region,
3518 so we know the next entry is a different region, even if the
3519 addresses are the same. Make sure its not end of table tho. */
3520 if (tab[pos].start_region != (void *) -1)
3526 /* We can't do a binary search because the table is in inner-most
3527 to outermost address ranges within functions */
3528 for ( ; tab[pos].start_region != (void *) -1; pos++)
3530 if (tab[pos].start_region <= pc && tab[pos].end_region > pc)
3532 if (tab[pos].match_info)
3534 __eh_matcher matcher = eh_info->match_function;
3535 /* match info but no matcher is NOT a match */
3538 void *ret = (*matcher)((void *) eh_info,
3539 tab[pos].match_info, table);
3543 retval = tab[pos].exception_handler;
3552 retval = tab[pos].exception_handler;
3559 #endif /* DWARF2_UNWIND_INFO */
3560 #endif /* EH_TABLE_LOOKUP */
3562 #ifdef DWARF2_UNWIND_INFO
3563 /* Support code for exception handling using static unwind information. */
3567 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3568 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3569 avoid a warning about casting between int and pointer of different
3572 typedef int ptr_type __attribute__ ((mode (pointer)));
3574 #ifdef INCOMING_REGNO
3575 /* Is the saved value for register REG in frame UDATA stored in a register
3576 window in the previous frame? */
3578 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3579 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3580 compiled functions won't work with the frame-unwind stuff here.
3581 Perhaps the entireity of in_reg_window should be conditional on having
3582 seen a DW_CFA_GNU_window_save? */
3583 #define target_flags 0
3586 in_reg_window (int reg, frame_state *udata)
3588 if (udata->saved[reg] == REG_SAVED_REG)
3589 return INCOMING_REGNO (reg) == reg;
3590 if (udata->saved[reg] != REG_SAVED_OFFSET)
3593 #ifdef STACK_GROWS_DOWNWARD
3594 return udata->reg_or_offset[reg] > 0;
3596 return udata->reg_or_offset[reg] < 0;
3600 static inline int in_reg_window (int reg, frame_state *udata) { return 0; }
3601 #endif /* INCOMING_REGNO */
3603 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3604 frame called by UDATA or 0. */
3607 get_reg_addr (unsigned reg, frame_state *udata, frame_state *sub_udata)
3609 while (udata->saved[reg] == REG_SAVED_REG)
3611 reg = udata->reg_or_offset[reg];
3612 if (in_reg_window (reg, udata))
3618 if (udata->saved[reg] == REG_SAVED_OFFSET)
3619 return (word_type *)(udata->cfa + udata->reg_or_offset[reg]);
3624 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3625 frame called by UDATA or 0. */
3627 static inline void *
3628 get_reg (unsigned reg, frame_state *udata, frame_state *sub_udata)
3630 return (void *)(ptr_type) *get_reg_addr (reg, udata, sub_udata);
3633 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3636 put_reg (unsigned reg, void *val, frame_state *udata)
3638 *get_reg_addr (reg, udata, NULL) = (word_type)(ptr_type) val;
3641 /* Copy the saved value for register REG from frame UDATA to frame
3642 TARGET_UDATA. Unlike the previous two functions, this can handle
3643 registers that are not one word large. */
3646 copy_reg (unsigned reg, frame_state *udata, frame_state *target_udata)
3648 word_type *preg = get_reg_addr (reg, udata, NULL);
3649 word_type *ptreg = get_reg_addr (reg, target_udata, NULL);
3651 memcpy (ptreg, preg, __builtin_dwarf_reg_size (reg));
3654 /* Retrieve the return address for frame UDATA. */
3656 static inline void *
3657 get_return_addr (frame_state *udata, frame_state *sub_udata)
3659 return __builtin_extract_return_addr
3660 (get_reg (udata->retaddr_column, udata, sub_udata));
3663 /* Overwrite the return address for frame UDATA with VAL. */
3666 put_return_addr (void *val, frame_state *udata)
3668 val = __builtin_frob_return_addr (val);
3669 put_reg (udata->retaddr_column, val, udata);
3672 /* Given the current frame UDATA and its return address PC, return the
3673 information about the calling frame in CALLER_UDATA. */
3676 next_stack_level (void *pc, frame_state *udata, frame_state *caller_udata)
3678 caller_udata = __frame_state_for (pc, caller_udata);
3682 /* Now go back to our caller's stack frame. If our caller's CFA register
3683 was saved in our stack frame, restore it; otherwise, assume the CFA
3684 register is SP and restore it to our CFA value. */
3685 if (udata->saved[caller_udata->cfa_reg])
3686 caller_udata->cfa = get_reg (caller_udata->cfa_reg, udata, 0);
3688 caller_udata->cfa = udata->cfa;
3689 caller_udata->cfa += caller_udata->cfa_offset;
3691 return caller_udata;
3694 /* Hook to call before __terminate if only cleanup handlers remain. */
3696 __unwinding_cleanup ()
3700 /* throw_helper performs some of the common grunt work for a throw. This
3701 routine is called by throw and rethrows. This is pretty much split
3702 out from the old __throw routine. An addition has been added which allows
3703 for a dummy call to a routine __unwinding_cleanup() when there are nothing
3704 but cleanups remaining. This allows a debugger to examine the state
3705 at which the throw was executed, before any cleanups, rather than
3706 at the terminate point after the stack has been unwound.
3708 EH is the current eh_context structure.
3709 PC is the address of the call to __throw.
3710 MY_UDATA is the unwind information for __throw.
3711 OFFSET_P is where we return the SP adjustment offset. */
3714 throw_helper (eh, pc, my_udata, offset_p)
3715 struct eh_context *eh;
3717 frame_state *my_udata;
3720 frame_state ustruct2, *udata = &ustruct2;
3721 frame_state ustruct;
3722 frame_state *sub_udata = &ustruct;
3723 void *saved_pc = pc;
3727 frame_state saved_ustruct;
3730 int only_cleanup = 0;
3732 int saved_state = 0;
3734 __eh_info *eh_info = (__eh_info *)eh->info;
3736 /* Do we find a handler based on a re-throw PC? */
3737 if (eh->table_index != (void *) 0)
3740 memcpy (udata, my_udata, sizeof (*udata));
3742 handler = (void *) 0;
3745 frame_state *p = udata;
3746 udata = next_stack_level (pc, udata, sub_udata);
3749 /* If we couldn't find the next frame, we lose. */
3753 if (udata->eh_ptr == NULL)
3756 new_eh_model = (((exception_descriptor *)(udata->eh_ptr))->
3757 runtime_id_field == NEW_EH_RUNTIME);
3762 handler = find_exception_handler (eh->table_index, udata->eh_ptr,
3763 eh_info, 1, &cleanup);
3764 eh->table_index = (void *)0;
3768 handler = find_exception_handler (pc, udata->eh_ptr, eh_info,
3771 handler = old_find_exception_handler (pc, udata->eh_ptr);
3773 /* If we found one, we can stop searching, if its not a cleanup.
3774 for cleanups, we save the state, and keep looking. This allows
3775 us to call a debug hook if there are nothing but cleanups left. */
3782 saved_ustruct = *udata;
3783 handler_p = handler;
3796 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3797 hitting the beginning of the next region. */
3798 pc = get_return_addr (udata, sub_udata) - 1;
3803 udata = &saved_ustruct;
3804 handler = handler_p;
3807 __unwinding_cleanup ();
3810 /* If we haven't found a handler by now, this is an unhandled
3815 eh->handler_label = handler;
3817 args_size = udata->args_size;
3820 /* We found a handler in the throw context, no need to unwind. */
3826 /* Unwind all the frames between this one and the handler by copying
3827 their saved register values into our register save slots. */
3829 /* Remember the PC where we found the handler. */
3830 void *handler_pc = pc;
3832 /* Start from the throw context again. */
3834 memcpy (udata, my_udata, sizeof (*udata));
3836 while (pc != handler_pc)
3838 frame_state *p = udata;
3839 udata = next_stack_level (pc, udata, sub_udata);
3842 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
3843 if (i != udata->retaddr_column && udata->saved[i])
3845 /* If you modify the saved value of the return address
3846 register on the SPARC, you modify the return address for
3847 your caller's frame. Don't do that here, as it will
3848 confuse get_return_addr. */
3849 if (in_reg_window (i, udata)
3850 && udata->saved[udata->retaddr_column] == REG_SAVED_REG
3851 && udata->reg_or_offset[udata->retaddr_column] == i)
3853 copy_reg (i, udata, my_udata);
3856 pc = get_return_addr (udata, sub_udata) - 1;
3859 /* But we do need to update the saved return address register from
3860 the last frame we unwind, or the handler frame will have the wrong
3862 if (udata->saved[udata->retaddr_column] == REG_SAVED_REG)
3864 i = udata->reg_or_offset[udata->retaddr_column];
3865 if (in_reg_window (i, udata))
3866 copy_reg (i, udata, my_udata);
3869 /* udata now refers to the frame called by the handler frame. */
3871 /* We adjust SP by the difference between __throw's CFA and the CFA for
3872 the frame called by the handler frame, because those CFAs correspond
3873 to the SP values at the two call sites. We need to further adjust by
3874 the args_size of the handler frame itself to get the handler frame's
3875 SP from before the args were pushed for that call. */
3876 #ifdef STACK_GROWS_DOWNWARD
3877 *offset_p = udata->cfa - my_udata->cfa + args_size;
3879 *offset_p = my_udata->cfa - udata->cfa - args_size;
3886 /* We first search for an exception handler, and if we don't find
3887 it, we call __terminate on the current stack frame so that we may
3888 use the debugger to walk the stack and understand why no handler
3891 If we find one, then we unwind the frames down to the one that
3892 has the handler and transfer control into the handler. */
3894 /*extern void __throw(void) __attribute__ ((__noreturn__));*/
3899 struct eh_context *eh = (*get_eh_context) ();
3903 /* XXX maybe make my_ustruct static so we don't have to look it up for
3905 frame_state my_ustruct, *my_udata = &my_ustruct;
3907 /* This is required for C++ semantics. We must call terminate if we
3908 try and rethrow an exception, when there is no exception currently
3913 /* Start at our stack frame. */
3915 my_udata = __frame_state_for (&&label, my_udata);
3919 /* We need to get the value from the CFA register. */
3920 my_udata->cfa = __builtin_dwarf_cfa ();
3922 /* Do any necessary initialization to access arbitrary stack frames.
3923 On the SPARC, this means flushing the register windows. */
3924 __builtin_unwind_init ();
3926 /* Now reset pc to the right throw point. */
3927 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3929 handler = throw_helper (eh, pc, my_udata, &offset);
3933 __builtin_eh_return ((void *)eh, offset, handler);
3935 /* Epilogue: restore the handler frame's register values and return
3939 /*extern void __rethrow(void *) __attribute__ ((__noreturn__));*/
3945 struct eh_context *eh = (*get_eh_context) ();
3949 /* XXX maybe make my_ustruct static so we don't have to look it up for
3951 frame_state my_ustruct, *my_udata = &my_ustruct;
3953 /* This is required for C++ semantics. We must call terminate if we
3954 try and rethrow an exception, when there is no exception currently
3959 /* This is the table index we want to rethrow from. The value of
3960 the END_REGION label is used for the PC of the throw, and the
3961 search begins with the next table entry. */
3962 eh->table_index = index;
3964 /* Start at our stack frame. */
3966 my_udata = __frame_state_for (&&label, my_udata);
3970 /* We need to get the value from the CFA register. */
3971 my_udata->cfa = __builtin_dwarf_cfa ();
3973 /* Do any necessary initialization to access arbitrary stack frames.
3974 On the SPARC, this means flushing the register windows. */
3975 __builtin_unwind_init ();
3977 /* Now reset pc to the right throw point. */
3978 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3980 handler = throw_helper (eh, pc, my_udata, &offset);
3984 __builtin_eh_return ((void *)eh, offset, handler);
3986 /* Epilogue: restore the handler frame's register values and return
3989 #endif /* DWARF2_UNWIND_INFO */
3994 #ifndef inhibit_libc
3995 /* This gets us __GNU_LIBRARY__. */
3996 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
3999 #ifdef __GNU_LIBRARY__
4000 /* Avoid forcing the library's meaning of `write' on the user program
4001 by using the "internal" name (for use within the library) */
4002 #define write(fd, buf, n) __write((fd), (buf), (n))
4004 #endif /* inhibit_libc */
4006 #define MESSAGE "pure virtual method called\n"
4011 #ifndef inhibit_libc
4012 write (2, MESSAGE, sizeof (MESSAGE) - 1);