1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* As a special exception, if you link this library with files
22 compiled with GCC to produce an executable, this does not cause
23 the resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why
25 the executable file might be covered by the GNU General Public License. */
27 /* It is incorrect to include config.h here, because this file is being
28 compiled for the target, and hence definitions concerning only the host
37 /* Don't use `fancy_abort' here even if config.h says to use it. */
42 /* In the first part of this file, we are interfacing to calls generated
43 by the compiler itself. These calls pass values into these routines
44 which have very specific modes (rather than very specific types), and
45 these compiler-generated calls also expect any return values to have
46 very specific modes (rather than very specific types). Thus, we need
47 to avoid using regular C language type names in this part of the file
48 because the sizes for those types can be configured to be anything.
49 Instead we use the following special type names. */
51 typedef unsigned int UQItype __attribute__ ((mode (QI)));
52 typedef int SItype __attribute__ ((mode (SI)));
53 typedef unsigned int USItype __attribute__ ((mode (SI)));
54 typedef int DItype __attribute__ ((mode (DI)));
55 typedef unsigned int UDItype __attribute__ ((mode (DI)));
56 typedef float SFtype __attribute__ ((mode (SF)));
57 typedef float DFtype __attribute__ ((mode (DF)));
59 typedef float XFtype __attribute__ ((mode (XF)));
61 #if LONG_DOUBLE_TYPE_SIZE == 128
62 typedef float TFtype __attribute__ ((mode (TF)));
66 typedef int word_type __attribute__ ((mode (HI)));
69 typedef int word_type __attribute__ ((mode (SI)));
72 typedef int word_type __attribute__ ((mode (DI)));
75 /* Make sure that we don't accidentally use any normal C language built-in
76 type names in the first part of this file. Instead we want to use *only*
77 the type names defined above. The following macro definitions insure
78 that if we *do* accidentally use some normal C language built-in type name,
79 we will get a syntax error. */
81 #define char bogus_type
82 #define short bogus_type
83 #define int bogus_type
84 #define long bogus_type
85 #define unsigned bogus_type
86 #define float bogus_type
87 #define double bogus_type
89 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
91 /* DIstructs are pairs of SItype values in the order determined by
95 struct DIstruct {SItype high, low;};
97 struct DIstruct {SItype low, high;};
100 /* We need this union to unpack/pack DImode values, since we don't have
101 any arithmetic yet. Incoming DImode parameters are stored into the
102 `ll' field, and the unpacked result is read from the struct `s'. */
110 #if defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)
112 #include "longlong.h"
114 #endif /* udiv or mul */
116 extern DItype __fixunssfdi (SFtype a);
117 extern DItype __fixunsdfdi (DFtype a);
119 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
120 #if defined (L_divdi3) || defined (L_moddi3)
133 w.s.high = -uu.s.high - ((USItype) w.s.low > 0);
154 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
158 w.s.high = (USItype)uu.s.low << -bm;
162 USItype carries = (USItype)uu.s.low >> bm;
163 w.s.low = (USItype)uu.s.low << b;
164 w.s.high = ((USItype)uu.s.high << b) | carries;
186 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
190 w.s.low = (USItype)uu.s.high >> -bm;
194 USItype carries = (USItype)uu.s.high << bm;
195 w.s.high = (USItype)uu.s.high >> b;
196 w.s.low = ((USItype)uu.s.low >> b) | carries;
218 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
222 w.s.high = (USItype)uu.s.low << -bm;
226 USItype carries = (USItype)uu.s.low >> bm;
227 w.s.low = (USItype)uu.s.low << b;
228 w.s.high = ((USItype)uu.s.high << b) | carries;
250 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
253 /* w.s.high = 1..1 or 0..0 */
254 w.s.high = uu.s.high >> (sizeof (SItype) * BITS_PER_UNIT - 1);
255 w.s.low = uu.s.high >> -bm;
259 USItype carries = (USItype)uu.s.high << bm;
260 w.s.high = uu.s.high >> b;
261 w.s.low = ((USItype)uu.s.low >> b) | carries;
276 w.s.low = ffs (uu.s.low);
279 w.s.low = ffs (uu.s.high);
282 w.s.low += BITS_PER_UNIT * sizeof (SItype);
300 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
301 w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
302 + (USItype) uu.s.high * (USItype) vv.s.low);
310 __udiv_w_sdiv (rp, a1, a0, d)
311 USItype *rp, a1, a0, d;
318 if (a1 < d - a1 - (a0 >> (SI_TYPE_SIZE - 1)))
320 /* dividend, divisor, and quotient are nonnegative */
321 sdiv_qrnnd (q, r, a1, a0, d);
325 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
326 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (SI_TYPE_SIZE - 1));
327 /* Divide (c1*2^32 + c0) by d */
328 sdiv_qrnnd (q, r, c1, c0, d);
329 /* Add 2^31 to quotient */
330 q += (USItype) 1 << (SI_TYPE_SIZE - 1);
335 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
336 c1 = a1 >> 1; /* A/2 */
337 c0 = (a1 << (SI_TYPE_SIZE - 1)) + (a0 >> 1);
339 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
341 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
343 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
360 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
363 c0 = ~c0; /* logical NOT */
365 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
367 q = ~q; /* (A/2)/b1 */
370 r = 2*r + (a0 & 1); /* A/(2*b1) */
388 else /* Implies c1 = b1 */
389 { /* Hence a1 = d - 1 = 2*b1 - 1 */
409 static const UQItype __clz_tab[] =
411 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,
412 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,
413 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,
414 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,
415 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,
416 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,
417 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,
418 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,
422 __udivmoddi4 (n, d, rp)
429 USItype d0, d1, n0, n1, n2;
441 #if !UDIV_NEEDS_NORMALIZATION
448 udiv_qrnnd (q0, n0, n1, n0, d0);
451 /* Remainder in n0. */
458 d0 = 1 / d0; /* Divide intentionally by zero. */
460 udiv_qrnnd (q1, n1, 0, n1, d0);
461 udiv_qrnnd (q0, n0, n1, n0, d0);
463 /* Remainder in n0. */
474 #else /* UDIV_NEEDS_NORMALIZATION */
482 count_leading_zeros (bm, d0);
486 /* Normalize, i.e. make the most significant bit of the
490 n1 = (n1 << bm) | (n0 >> (SI_TYPE_SIZE - bm));
494 udiv_qrnnd (q0, n0, n1, n0, d0);
497 /* Remainder in n0 >> bm. */
504 d0 = 1 / d0; /* Divide intentionally by zero. */
506 count_leading_zeros (bm, d0);
510 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
511 conclude (the most significant bit of n1 is set) /\ (the
512 leading quotient digit q1 = 1).
514 This special case is necessary, not an optimization.
515 (Shifts counts of SI_TYPE_SIZE are undefined.) */
524 b = SI_TYPE_SIZE - bm;
528 n1 = (n1 << bm) | (n0 >> b);
531 udiv_qrnnd (q1, n1, n2, n1, d0);
536 udiv_qrnnd (q0, n0, n1, n0, d0);
538 /* Remainder in n0 >> bm. */
548 #endif /* UDIV_NEEDS_NORMALIZATION */
559 /* Remainder in n1n0. */
571 count_leading_zeros (bm, d1);
574 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
575 conclude (the most significant bit of n1 is set) /\ (the
576 quotient digit q0 = 0 or 1).
578 This special case is necessary, not an optimization. */
580 /* The condition on the next line takes advantage of that
581 n1 >= d1 (true due to program flow). */
582 if (n1 > d1 || n0 >= d0)
585 sub_ddmmss (n1, n0, n1, n0, d1, d0);
604 b = SI_TYPE_SIZE - bm;
606 d1 = (d1 << bm) | (d0 >> b);
609 n1 = (n1 << bm) | (n0 >> b);
612 udiv_qrnnd (q0, n1, n2, n1, d1);
613 umul_ppmm (m1, m0, q0, d0);
615 if (m1 > n1 || (m1 == n1 && m0 > n0))
618 sub_ddmmss (m1, m0, m1, m0, d1, d0);
623 /* Remainder in (n1n0 - m1m0) >> bm. */
626 sub_ddmmss (n1, n0, n1, n0, m1, m0);
627 rr.s.low = (n1 << b) | (n0 >> bm);
628 rr.s.high = n1 >> bm;
642 UDItype __udivmoddi4 ();
656 uu.ll = __negdi2 (uu.ll);
659 vv.ll = __negdi2 (vv.ll);
661 w = __udivmoddi4 (uu.ll, vv.ll, (UDItype *) 0);
670 UDItype __udivmoddi4 ();
684 uu.ll = __negdi2 (uu.ll);
686 vv.ll = __negdi2 (vv.ll);
688 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
697 UDItype __udivmoddi4 ();
704 (void) __udivmoddi4 (u, v, &w);
711 UDItype __udivmoddi4 ();
716 return __udivmoddi4 (n, d, (UDItype *) 0);
727 au.ll = a, bu.ll = b;
729 if (au.s.high < bu.s.high)
731 else if (au.s.high > bu.s.high)
733 if ((USItype) au.s.low < (USItype) bu.s.low)
735 else if ((USItype) au.s.low > (USItype) bu.s.low)
748 au.ll = a, bu.ll = b;
750 if ((USItype) au.s.high < (USItype) bu.s.high)
752 else if ((USItype) au.s.high > (USItype) bu.s.high)
754 if ((USItype) au.s.low < (USItype) bu.s.low)
756 else if ((USItype) au.s.low > (USItype) bu.s.low)
762 #if defined(L_fixunstfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
763 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
764 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
776 /* Compute high word of result, as a flonum. */
777 b = (a / HIGH_WORD_COEFF);
778 /* Convert that to fixed (but not to DItype!),
779 and shift it into the high word. */
782 /* Remove high part from the TFtype, leaving the low part as flonum. */
784 /* Convert that to fixed (but not to DItype!) and add it in.
785 Sometimes A comes out negative. This is significant, since
786 A has more bits than a long int does. */
788 v -= (USItype) (- a);
795 #if defined(L_fixtfdi) && (LONG_DOUBLE_TYPE_SIZE == 128)
801 return - __fixunstfdi (-a);
802 return __fixunstfdi (a);
807 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
808 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
820 /* Compute high word of result, as a flonum. */
821 b = (a / HIGH_WORD_COEFF);
822 /* Convert that to fixed (but not to DItype!),
823 and shift it into the high word. */
826 /* Remove high part from the DFtype, leaving the low part as flonum. */
828 /* Convert that to fixed (but not to DItype!) and add it in.
829 Sometimes A comes out negative. This is significant, since
830 A has more bits than a long int does. */
832 v -= (USItype) (- a);
845 return - __fixunsdfdi (-a);
846 return __fixunsdfdi (a);
851 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
852 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
855 __fixunssfdi (SFtype original_a)
857 /* Convert the SFtype to a DFtype, because that is surely not going
858 to lose any bits. Some day someone else can write a faster version
859 that avoids converting to DFtype, and verify it really works right. */
860 DFtype a = original_a;
867 /* Compute high word of result, as a flonum. */
868 b = (a / HIGH_WORD_COEFF);
869 /* Convert that to fixed (but not to DItype!),
870 and shift it into the high word. */
873 /* Remove high part from the DFtype, leaving the low part as flonum. */
875 /* Convert that to fixed (but not to DItype!) and add it in.
876 Sometimes A comes out negative. This is significant, since
877 A has more bits than a long int does. */
879 v -= (USItype) (- a);
891 return - __fixunssfdi (-a);
892 return __fixunssfdi (a);
896 #if defined(L_floatditf) && (LONG_DOUBLE_TYPE_SIZE == 128)
897 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
898 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
899 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
911 d = (USItype) (u >> WORD_SIZE);
912 d *= HIGH_HALFWORD_COEFF;
913 d *= HIGH_HALFWORD_COEFF;
914 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
916 return (negate ? -d : d);
921 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
922 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
923 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
935 d = (USItype) (u >> WORD_SIZE);
936 d *= HIGH_HALFWORD_COEFF;
937 d *= HIGH_HALFWORD_COEFF;
938 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
940 return (negate ? -d : d);
945 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
946 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
947 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
959 f = (USItype) (u >> WORD_SIZE);
960 f *= HIGH_HALFWORD_COEFF;
961 f *= HIGH_HALFWORD_COEFF;
962 f += (USItype) (u & (HIGH_WORD_COEFF - 1));
964 return (negate ? -f : f);
975 if (a >= - (DFtype) LONG_MIN)
976 return (SItype) (a + LONG_MIN) - LONG_MIN;
985 __fixunssfsi (SFtype a)
987 if (a >= - (SFtype) LONG_MIN)
988 return (SItype) (a + LONG_MIN) - LONG_MIN;
993 /* From here on down, the routines use normal data types. */
995 #define SItype bogus_type
996 #define USItype bogus_type
997 #define DItype bogus_type
998 #define UDItype bogus_type
999 #define SFtype bogus_type
1000 #define DFtype bogus_type
1012 /* Like bcmp except the sign is meaningful.
1013 Reult is negative if S1 is less than S2,
1014 positive if S1 is greater, 0 if S1 and S2 are equal. */
1017 __gcc_bcmp (s1, s2, size)
1018 unsigned char *s1, *s2;
1023 unsigned char c1 = *s1++, c2 = *s2++;
1035 #if defined(__svr4__) || defined(__alliant__)
1039 /* The Alliant needs the added underscore. */
1040 asm (".globl __builtin_saveregs");
1041 asm ("__builtin_saveregs:");
1042 asm (".globl ___builtin_saveregs");
1043 asm ("___builtin_saveregs:");
1045 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1046 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1047 area and also for a new va_list
1049 /* Save all argument registers in the arg reg save area. The
1050 arg reg save area must have the following layout (according
1062 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1063 asm (" fst.q %f12,16(%sp)");
1065 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1066 asm (" st.l %r17,36(%sp)");
1067 asm (" st.l %r18,40(%sp)");
1068 asm (" st.l %r19,44(%sp)");
1069 asm (" st.l %r20,48(%sp)");
1070 asm (" st.l %r21,52(%sp)");
1071 asm (" st.l %r22,56(%sp)");
1072 asm (" st.l %r23,60(%sp)");
1073 asm (" st.l %r24,64(%sp)");
1074 asm (" st.l %r25,68(%sp)");
1075 asm (" st.l %r26,72(%sp)");
1076 asm (" st.l %r27,76(%sp)");
1078 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1079 va_list structure. Put in into
1080 r16 so that it will be returned
1083 /* Initialize all fields of the new va_list structure. This
1084 structure looks like:
1087 unsigned long ireg_used;
1088 unsigned long freg_used;
1094 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1095 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1096 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1097 asm (" bri %r1"); /* delayed return */
1098 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1100 #else /* not __SVR4__ */
1104 asm (".globl ___builtin_saveregs");
1105 asm ("___builtin_saveregs:");
1106 asm (" mov sp,r30");
1107 asm (" andnot 0x0f,sp,sp");
1108 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1110 /* Fill in the __va_struct. */
1111 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1112 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1113 asm (" st.l r18, 8(sp)");
1114 asm (" st.l r19,12(sp)");
1115 asm (" st.l r20,16(sp)");
1116 asm (" st.l r21,20(sp)");
1117 asm (" st.l r22,24(sp)");
1118 asm (" st.l r23,28(sp)");
1119 asm (" st.l r24,32(sp)");
1120 asm (" st.l r25,36(sp)");
1121 asm (" st.l r26,40(sp)");
1122 asm (" st.l r27,44(sp)");
1124 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1125 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1127 /* Fill in the __va_ctl. */
1128 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1129 asm (" st.l r28,84(sp)"); /* pointer to more args */
1130 asm (" st.l r0, 88(sp)"); /* nfixed */
1131 asm (" st.l r0, 92(sp)"); /* nfloating */
1133 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1135 asm (" mov r30,sp");
1136 /* recover stack and pass address to start
1138 #endif /* not __SVR4__ */
1139 #else /* not __i860__ */
1141 asm (".global __builtin_saveregs");
1142 asm ("__builtin_saveregs:");
1143 asm (".global ___builtin_saveregs");
1144 asm ("___builtin_saveregs:");
1145 #ifdef NEED_PROC_COMMAND
1148 asm ("st %i0,[%fp+68]");
1149 asm ("st %i1,[%fp+72]");
1150 asm ("st %i2,[%fp+76]");
1151 asm ("st %i3,[%fp+80]");
1152 asm ("st %i4,[%fp+84]");
1154 asm ("st %i5,[%fp+88]");
1155 #ifdef NEED_TYPE_COMMAND
1156 asm (".type __builtin_saveregs,#function");
1157 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1159 #else /* not __sparc__ */
1160 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1163 asm (" .ent __builtin_saveregs");
1164 asm (" .globl __builtin_saveregs");
1165 asm ("__builtin_saveregs:");
1166 asm (" sw $4,0($30)");
1167 asm (" sw $5,4($30)");
1168 asm (" sw $6,8($30)");
1169 asm (" sw $7,12($30)");
1171 asm (" .end __builtin_saveregs");
1172 #else /* not __mips__, etc. */
1173 __builtin_saveregs ()
1177 #endif /* not __mips__ */
1178 #endif /* not __sparc__ */
1179 #endif /* not __i860__ */
1183 #ifndef inhibit_eprintf
1185 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1187 /* This is used by the `assert' macro. */
1189 __eprintf (string, expression, line, filename)
1191 const char *expression;
1193 const char *filename;
1195 fprintf (stderr, string, expression, line, filename);
1204 /* Avoid warning from ranlib about empty object file. */
1206 __bb_avoid_warning ()
1209 #if defined (__sun__) && defined (__mc68000__)
1220 extern int ___tcov_init;
1222 __bb_init_func (blocks)
1226 ___tcov_init_func ();
1228 ___bb_link (blocks->filename, blocks->counts, blocks->ncounts);
1234 /* frills for C++ */
1237 typedef void (*vfp)(void);
1239 extern vfp __new_handler;
1241 /* void * operator new (size_t sz) */
1243 __builtin_new (size_t sz)
1247 /* malloc (0) is unpredictable; avoid it. */
1250 p = (void *) malloc (sz);
1252 (*__new_handler) ();
1255 #endif /* L_op_new */
1257 #ifdef L_new_handler
1259 /* This gets us __GNU_LIBRARY__. */
1260 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1263 #ifdef __GNU_LIBRARY__
1264 /* Avoid forcing the library's meaning of `write' on the user program
1265 by using the "internal" name (for use within the library) */
1266 #define write(fd, buf, n) __write((fd), (buf), (n))
1269 typedef void (*vfp)(void);
1271 extern void *__builtin_new (size_t);
1272 static void default_new_handler (void);
1274 vfp __new_handler = default_new_handler;
1277 __set_new_handler (handler)
1282 prev_handler = __new_handler;
1283 if (handler == 0) handler = default_new_handler;
1284 __new_handler = handler;
1285 return prev_handler;
1289 set_new_handler (handler)
1292 return __set_new_handler (handler);
1295 #define MESSAGE "Virtual memory exceeded in `new'\n"
1298 default_new_handler ()
1300 /* don't use fprintf (stderr, ...) because it may need to call malloc. */
1301 /* This should really print the name of the program, but that is hard to
1302 do. We need a standard, clean way to get at the name. */
1303 write (2, MESSAGE, sizeof (MESSAGE));
1304 /* don't call exit () because that may call global destructors which
1305 may cause a loop. */
1311 /* void operator delete (void *ptr) */
1313 __builtin_delete (void *ptr)
1321 unsigned int __shtab[] = {
1322 0x00000001, 0x00000002, 0x00000004, 0x00000008,
1323 0x00000010, 0x00000020, 0x00000040, 0x00000080,
1324 0x00000100, 0x00000200, 0x00000400, 0x00000800,
1325 0x00001000, 0x00002000, 0x00004000, 0x00008000,
1326 0x00010000, 0x00020000, 0x00040000, 0x00080000,
1327 0x00100000, 0x00200000, 0x00400000, 0x00800000,
1328 0x01000000, 0x02000000, 0x04000000, 0x08000000,
1329 0x10000000, 0x20000000, 0x40000000, 0x80000000
1333 #ifdef L_clear_cache
1334 /* Clear part of an instruction cache. */
1336 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
1339 __clear_cache (beg, end)
1342 #ifdef INSN_CACHE_SIZE
1343 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
1344 static int initialized = 0;
1348 typedef (*function_ptr) ();
1350 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
1351 /* It's cheaper to clear the whole cache.
1352 Put in a series of jump instructions so that calling the beginning
1353 of the cache will clear the whole thing. */
1357 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
1358 & -INSN_CACHE_LINE_WIDTH);
1359 int end_ptr = ptr + INSN_CACHE_SIZE;
1361 while (ptr < end_ptr)
1363 *(INSTRUCTION_TYPE *)ptr
1364 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
1365 ptr += INSN_CACHE_LINE_WIDTH;
1367 *(INSTRUCTION_TYPE *)(ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
1372 /* Call the beginning of the sequence. */
1373 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
1374 & -INSN_CACHE_LINE_WIDTH))
1377 #else /* Cache is large. */
1381 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
1382 & -INSN_CACHE_LINE_WIDTH);
1384 while (ptr < (int) array + sizeof array)
1386 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
1387 ptr += INSN_CACHE_LINE_WIDTH;
1393 /* Find the location in array that occupies the same cache line as BEG. */
1395 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
1396 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
1397 & -INSN_CACHE_PLANE_SIZE)
1400 /* Compute the cache alignment of the place to stop clearing. */
1401 #if 0 /* This is not needed for gcc's purposes. */
1402 /* If the block to clear is bigger than a cache plane,
1403 we clear the entire cache, and OFFSET is already correct. */
1404 if (end < beg + INSN_CACHE_PLANE_SIZE)
1406 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
1407 & -INSN_CACHE_LINE_WIDTH)
1408 & (INSN_CACHE_PLANE_SIZE - 1));
1410 #if INSN_CACHE_DEPTH > 1
1411 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
1412 if (end_addr <= start_addr)
1413 end_addr += INSN_CACHE_PLANE_SIZE;
1415 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
1417 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
1418 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
1420 while (addr != stop)
1422 /* Call the return instruction at ADDR. */
1423 ((function_ptr) addr) ();
1425 addr += INSN_CACHE_LINE_WIDTH;
1428 #else /* just one plane */
1431 /* Call the return instruction at START_ADDR. */
1432 ((function_ptr) start_addr) ();
1434 start_addr += INSN_CACHE_LINE_WIDTH;
1436 while ((start_addr % INSN_CACHE_SIZE) != offset);
1437 #endif /* just one plane */
1438 #endif /* Cache is large */
1439 #endif /* Cache exists */
1442 #endif /* L_clear_cache */
1446 /* Jump to a trampoline, loading the static chain address. */
1448 #ifdef TRANSFER_FROM_TRAMPOLINE
1449 TRANSFER_FROM_TRAMPOLINE
1454 /* Make stack executable so we can call trampolines on stack.
1455 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
1457 #include <sys/mman.h>
1458 #include <sys/vmparam.h>
1459 #include <machine/machparam.h>
1462 __enable_execute_stack ()
1465 static unsigned lowest = USRSTACK;
1466 unsigned current = (unsigned) &fp & -NBPG;
1468 if (lowest > current)
1470 unsigned len = lowest - current;
1471 mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
1475 /* Clear instruction cache in case an old trampoline is in it. */
1478 #endif /* __convex__ */
1482 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1484 #include <sys/mman.h>
1485 #include <sys/types.h>
1486 #include <sys/param.h>
1487 #include <sys/vmmac.h>
1489 /* Modified from the convex -code above.
1490 mremap promises to clear the i-cache. */
1493 __enable_execute_stack ()
1496 if (mprotect (((unsigned int)&fp/PAGSIZ)*PAGSIZ, PAGSIZ,
1497 PROT_READ|PROT_WRITE|PROT_EXEC))
1499 perror ("mprotect in __enable_execute_stack");
1504 #endif /* __pyr__ */
1505 #endif /* L_trampoline */
1509 #include "gbl-ctors.h"
1511 /* Run all the global destructors on exit from the program. */
1514 __do_global_dtors ()
1516 #ifdef DO_GLOBAL_DTORS_BODY
1517 DO_GLOBAL_DTORS_BODY;
1519 unsigned nptrs = (unsigned HOST_WIDE_INT) __DTOR_LIST__[0];
1522 /* Some systems place the number of pointers
1523 in the first word of the table.
1524 On other systems, that word is -1.
1525 In all cases, the table is null-terminated. */
1527 /* If the length is not recorded, count up to the null. */
1529 for (nptrs = 0; __DTOR_LIST__[nptrs + 1] != 0; nptrs++);
1531 /* GNU LD format. */
1532 for (i = nptrs; i >= 1; i--)
1533 __DTOR_LIST__[i] ();
1537 #ifndef INIT_SECTION_ASM_OP
1538 /* Run all the global constructors on entry to the program. */
1541 #define ON_EXIT(a, b)
1543 /* Make sure the exit routine is pulled in to define the globals as
1544 bss symbols, just in case the linker does not automatically pull
1545 bss definitions from the library. */
1547 extern int _exit_dummy_decl;
1548 int *_exit_dummy_ref = &_exit_dummy_decl;
1549 #endif /* ON_EXIT */
1552 __do_global_ctors ()
1554 DO_GLOBAL_CTORS_BODY;
1555 ON_EXIT (__do_global_dtors, 0);
1557 #endif /* no INIT_SECTION_ASM_OP */
1559 #if !defined (INIT_SECTION_ASM_OP) || defined (INVOKE__main)
1560 /* Subroutine called automatically by `main'.
1561 Compiling a global function named `main'
1562 produces an automatic call to this function at the beginning.
1564 For many systems, this routine calls __do_global_ctors.
1565 For systems which support a .init section we use the .init section
1566 to run __do_global_ctors, so we need not do anything here. */
1571 /* Support recursive calls to `main': run initializers just once. */
1572 static int initialized = 0;
1576 __do_global_ctors ();
1579 #endif /* no INIT_SECTION_ASM_OP or INVOKE__main */
1581 #endif /* L__main */
1585 #include "gbl-ctors.h"
1587 /* Provide default definitions for the lists of constructors and
1588 destructors, so that we don't get linker errors. These symbols are
1589 intentionally bss symbols, so that gld and/or collect will provide
1590 the right values. */
1592 /* We declare the lists here with two elements each,
1593 so that they are valid empty lists if no other definition is loaded. */
1594 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
1596 /* After 2.3, try this definition on all systems. */
1597 func_ptr __CTOR_LIST__[2] = {0, 0};
1598 func_ptr __DTOR_LIST__[2] = {0, 0};
1600 func_ptr __CTOR_LIST__[2];
1601 func_ptr __DTOR_LIST__[2];
1603 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
1604 #endif /* L_ctors */
1608 #include "gbl-ctors.h"
1612 /* If we have no known way of registering our own __do_global_dtors
1613 routine so that it will be invoked at program exit time, then we
1614 have to define our own exit routine which will get this to happen. */
1616 extern void __do_global_dtors ();
1617 extern void _cleanup ();
1618 extern volatile void _exit ();
1624 __do_global_dtors ();
1634 int _exit_dummy_decl = 0; /* prevent compiler & linker warnings */
1639 /* In a.out systems, we need to have these dummy constructor and destructor
1640 lists in the library.
1642 When using `collect', the first link will resolve __CTOR_LIST__
1643 and __DTOR_LIST__ to these symbols. We will then run "nm" on the
1644 result, build the correct __CTOR_LIST__ and __DTOR_LIST__, and relink.
1645 Since we don't do the second link if no constructors existed, these
1646 dummies must be fully functional empty lists.
1648 When using `gnu ld', these symbols will be used if there are no
1649 constructors. If there are constructors, the N_SETV symbol defined
1650 by the linker from the N_SETT's in input files will define __CTOR_LIST__
1651 and __DTOR_LIST__ rather than its being allocated as common storage
1652 by the definitions below.
1654 When using a linker that supports constructor and destructor segments,
1655 these definitions will not be used, since crtbegin.o and crtend.o
1656 (from crtstuff.c) will have already defined __CTOR_LIST__ and
1657 __DTOR_LIST__. The crt*.o files are passed directly to the linker
1658 on its command line, by gcc. */
1660 /* The list needs two elements: one is ignored (the old count); the
1661 second is the terminating zero. Since both values are zero, this
1662 declaration is not initialized, and it becomes `common'. */
1665 #include "gbl-ctors.h"
1666 func_ptr __CTOR_LIST__[2];
1670 #include "gbl-ctors.h"
1671 func_ptr __DTOR_LIST__[2];