PR optimization/2903
[official-gcc.git] / gcc / libgcc2.c
blobdea803def0d7f3a395f360892d93e0f0cffd91c9
1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 02111-1307, USA. */
32 /* It is incorrect to include config.h here, because this file is being
33 compiled for the target, and hence definitions concerning only the host
34 do not apply. */
36 #include "tconfig.h"
37 #include "tsystem.h"
39 /* Don't use `fancy_abort' here even if config.h says to use it. */
40 #ifdef abort
41 #undef abort
42 #endif
44 #include "libgcc2.h"
46 #ifdef DECLARE_LIBRARY_RENAMES
47 DECLARE_LIBRARY_RENAMES
48 #endif
50 #if defined (L_negdi2)
51 DWtype
52 __negdi2 (DWtype u)
54 DWunion w;
55 DWunion uu;
57 uu.ll = u;
59 w.s.low = -uu.s.low;
60 w.s.high = -uu.s.high - ((UWtype) w.s.low > 0);
62 return w.ll;
64 #endif
66 #ifdef L_addvsi3
67 Wtype
68 __addvsi3 (Wtype a, Wtype b)
70 Wtype w;
72 w = a + b;
74 if (b >= 0 ? w < a : w > a)
75 abort ();
77 return w;
79 #endif
81 #ifdef L_addvdi3
82 DWtype
83 __addvdi3 (DWtype a, DWtype b)
85 DWtype w;
87 w = a + b;
89 if (b >= 0 ? w < a : w > a)
90 abort ();
92 return w;
94 #endif
96 #ifdef L_subvsi3
97 Wtype
98 __subvsi3 (Wtype a, Wtype b)
100 #ifdef L_addvsi3
101 return __addvsi3 (a, (-b));
102 #else
103 DWtype w;
105 w = a - b;
107 if (b >= 0 ? w > a : w < a)
108 abort ();
110 return w;
111 #endif
113 #endif
115 #ifdef L_subvdi3
116 DWtype
117 __subvdi3 (DWtype a, DWtype b)
119 #ifdef L_addvdi3
120 return (a, (-b));
121 #else
122 DWtype w;
124 w = a - b;
126 if (b >= 0 ? w > a : w < a)
127 abort ();
129 return w;
130 #endif
132 #endif
134 #ifdef L_mulvsi3
135 Wtype
136 __mulvsi3 (Wtype a, Wtype b)
138 DWtype w;
140 w = a * b;
142 if (((a >= 0) == (b >= 0)) ? w < 0 : w > 0)
143 abort ();
145 return w;
147 #endif
149 #ifdef L_negvsi2
150 Wtype
151 __negvsi2 (Wtype a)
153 Wtype w;
155 w = -a;
157 if (a >= 0 ? w > 0 : w < 0)
158 abort ();
160 return w;
162 #endif
164 #ifdef L_negvdi2
165 DWtype
166 __negvdi2 (DWtype a)
168 DWtype w;
170 w = -a;
172 if (a >= 0 ? w > 0 : w < 0)
173 abort ();
175 return w;
177 #endif
179 #ifdef L_absvsi2
180 Wtype
181 __absvsi2 (Wtype a)
183 Wtype w = a;
185 if (a < 0)
186 #ifdef L_negvsi2
187 w = __negvsi2 (a);
188 #else
189 w = -a;
191 if (w < 0)
192 abort ();
193 #endif
195 return w;
197 #endif
199 #ifdef L_absvdi2
200 DWtype
201 __absvdi2 (DWtype a)
203 DWtype w = a;
205 if (a < 0)
206 #ifdef L_negvsi2
207 w = __negvsi2 (a);
208 #else
209 w = -a;
211 if (w < 0)
212 abort ();
213 #endif
215 return w;
217 #endif
219 #ifdef L_mulvdi3
220 DWtype
221 __mulvdi3 (DWtype u, DWtype v)
223 DWtype w;
225 w = u * v;
227 if (((u >= 0) == (v >= 0)) ? w < 0 : w > 0)
228 abort ();
230 return w;
232 #endif
235 /* Unless shift functions are defined whith full ANSI prototypes,
236 parameter b will be promoted to int if word_type is smaller than an int. */
237 #ifdef L_lshrdi3
238 DWtype
239 __lshrdi3 (DWtype u, word_type b)
241 DWunion w;
242 word_type bm;
243 DWunion uu;
245 if (b == 0)
246 return u;
248 uu.ll = u;
250 bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
251 if (bm <= 0)
253 w.s.high = 0;
254 w.s.low = (UWtype) uu.s.high >> -bm;
256 else
258 UWtype carries = (UWtype) uu.s.high << bm;
260 w.s.high = (UWtype) uu.s.high >> b;
261 w.s.low = ((UWtype) uu.s.low >> b) | carries;
264 return w.ll;
266 #endif
268 #ifdef L_ashldi3
269 DWtype
270 __ashldi3 (DWtype u, word_type b)
272 DWunion w;
273 word_type bm;
274 DWunion uu;
276 if (b == 0)
277 return u;
279 uu.ll = u;
281 bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
282 if (bm <= 0)
284 w.s.low = 0;
285 w.s.high = (UWtype) uu.s.low << -bm;
287 else
289 UWtype carries = (UWtype) uu.s.low >> bm;
291 w.s.low = (UWtype) uu.s.low << b;
292 w.s.high = ((UWtype) uu.s.high << b) | carries;
295 return w.ll;
297 #endif
299 #ifdef L_ashrdi3
300 DWtype
301 __ashrdi3 (DWtype u, word_type b)
303 DWunion w;
304 word_type bm;
305 DWunion uu;
307 if (b == 0)
308 return u;
310 uu.ll = u;
312 bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
313 if (bm <= 0)
315 /* w.s.high = 1..1 or 0..0 */
316 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
317 w.s.low = uu.s.high >> -bm;
319 else
321 UWtype carries = (UWtype) uu.s.high << bm;
323 w.s.high = uu.s.high >> b;
324 w.s.low = ((UWtype) uu.s.low >> b) | carries;
327 return w.ll;
329 #endif
331 #ifdef L_ffsdi2
332 DWtype
333 __ffsdi2 (DWtype u)
335 DWunion uu;
336 UWtype word, count, add;
338 uu.ll = u;
339 if (uu.s.low != 0)
340 word = uu.s.low, add = 0;
341 else if (uu.s.high != 0)
342 word = uu.s.high, add = BITS_PER_UNIT * sizeof (Wtype);
343 else
344 return 0;
346 count_trailing_zeros (count, word);
347 return count + add + 1;
349 #endif
351 #ifdef L_muldi3
352 DWtype
353 __muldi3 (DWtype u, DWtype v)
355 DWunion w;
356 DWunion uu, vv;
358 uu.ll = u,
359 vv.ll = v;
361 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
362 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
363 + (UWtype) uu.s.high * (UWtype) vv.s.low);
365 return w.ll;
367 #endif
369 #if (defined (L_udivdi3) || defined (L_divdi3) || \
370 defined (L_umoddi3) || defined (L_moddi3))
371 #if defined (sdiv_qrnnd)
372 #define L_udiv_w_sdiv
373 #endif
374 #endif
376 #ifdef L_udiv_w_sdiv
377 #if defined (sdiv_qrnnd)
378 #if (defined (L_udivdi3) || defined (L_divdi3) || \
379 defined (L_umoddi3) || defined (L_moddi3))
380 static inline __attribute__ ((__always_inline__))
381 #endif
382 UWtype
383 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
385 UWtype q, r;
386 UWtype c0, c1, b1;
388 if ((Wtype) d >= 0)
390 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
392 /* dividend, divisor, and quotient are nonnegative */
393 sdiv_qrnnd (q, r, a1, a0, d);
395 else
397 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
398 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
399 /* Divide (c1*2^32 + c0) by d */
400 sdiv_qrnnd (q, r, c1, c0, d);
401 /* Add 2^31 to quotient */
402 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
405 else
407 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
408 c1 = a1 >> 1; /* A/2 */
409 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
411 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
413 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
415 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
416 if ((d & 1) != 0)
418 if (r >= q)
419 r = r - q;
420 else if (q - r <= d)
422 r = r - q + d;
423 q--;
425 else
427 r = r - q + 2*d;
428 q -= 2;
432 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
434 c1 = (b1 - 1) - c1;
435 c0 = ~c0; /* logical NOT */
437 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
439 q = ~q; /* (A/2)/b1 */
440 r = (b1 - 1) - r;
442 r = 2*r + (a0 & 1); /* A/(2*b1) */
444 if ((d & 1) != 0)
446 if (r >= q)
447 r = r - q;
448 else if (q - r <= d)
450 r = r - q + d;
451 q--;
453 else
455 r = r - q + 2*d;
456 q -= 2;
460 else /* Implies c1 = b1 */
461 { /* Hence a1 = d - 1 = 2*b1 - 1 */
462 if (a0 >= -d)
464 q = -1;
465 r = a0 + d;
467 else
469 q = -2;
470 r = a0 + 2*d;
475 *rp = r;
476 return q;
478 #else
479 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
480 UWtype
481 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
482 UWtype a1 __attribute__ ((__unused__)),
483 UWtype a0 __attribute__ ((__unused__)),
484 UWtype d __attribute__ ((__unused__)))
486 return 0;
488 #endif
489 #endif
491 #if (defined (L_udivdi3) || defined (L_divdi3) || \
492 defined (L_umoddi3) || defined (L_moddi3))
493 #define L_udivmoddi4
494 #endif
496 #ifdef L_clz
497 const UQItype __clz_tab[] =
499 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,
500 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,
501 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
502 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,
503 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
504 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
505 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
506 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
508 #endif
510 #ifdef L_udivmoddi4
512 #if (defined (L_udivdi3) || defined (L_divdi3) || \
513 defined (L_umoddi3) || defined (L_moddi3))
514 static inline __attribute__ ((__always_inline__))
515 #endif
516 UDWtype
517 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
519 DWunion ww;
520 DWunion nn, dd;
521 DWunion rr;
522 UWtype d0, d1, n0, n1, n2;
523 UWtype q0, q1;
524 UWtype b, bm;
526 nn.ll = n;
527 dd.ll = d;
529 d0 = dd.s.low;
530 d1 = dd.s.high;
531 n0 = nn.s.low;
532 n1 = nn.s.high;
534 #if !UDIV_NEEDS_NORMALIZATION
535 if (d1 == 0)
537 if (d0 > n1)
539 /* 0q = nn / 0D */
541 udiv_qrnnd (q0, n0, n1, n0, d0);
542 q1 = 0;
544 /* Remainder in n0. */
546 else
548 /* qq = NN / 0d */
550 if (d0 == 0)
551 d0 = 1 / d0; /* Divide intentionally by zero. */
553 udiv_qrnnd (q1, n1, 0, n1, d0);
554 udiv_qrnnd (q0, n0, n1, n0, d0);
556 /* Remainder in n0. */
559 if (rp != 0)
561 rr.s.low = n0;
562 rr.s.high = 0;
563 *rp = rr.ll;
567 #else /* UDIV_NEEDS_NORMALIZATION */
569 if (d1 == 0)
571 if (d0 > n1)
573 /* 0q = nn / 0D */
575 count_leading_zeros (bm, d0);
577 if (bm != 0)
579 /* Normalize, i.e. make the most significant bit of the
580 denominator set. */
582 d0 = d0 << bm;
583 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
584 n0 = n0 << bm;
587 udiv_qrnnd (q0, n0, n1, n0, d0);
588 q1 = 0;
590 /* Remainder in n0 >> bm. */
592 else
594 /* qq = NN / 0d */
596 if (d0 == 0)
597 d0 = 1 / d0; /* Divide intentionally by zero. */
599 count_leading_zeros (bm, d0);
601 if (bm == 0)
603 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
604 conclude (the most significant bit of n1 is set) /\ (the
605 leading quotient digit q1 = 1).
607 This special case is necessary, not an optimization.
608 (Shifts counts of W_TYPE_SIZE are undefined.) */
610 n1 -= d0;
611 q1 = 1;
613 else
615 /* Normalize. */
617 b = W_TYPE_SIZE - bm;
619 d0 = d0 << bm;
620 n2 = n1 >> b;
621 n1 = (n1 << bm) | (n0 >> b);
622 n0 = n0 << bm;
624 udiv_qrnnd (q1, n1, n2, n1, d0);
627 /* n1 != d0... */
629 udiv_qrnnd (q0, n0, n1, n0, d0);
631 /* Remainder in n0 >> bm. */
634 if (rp != 0)
636 rr.s.low = n0 >> bm;
637 rr.s.high = 0;
638 *rp = rr.ll;
641 #endif /* UDIV_NEEDS_NORMALIZATION */
643 else
645 if (d1 > n1)
647 /* 00 = nn / DD */
649 q0 = 0;
650 q1 = 0;
652 /* Remainder in n1n0. */
653 if (rp != 0)
655 rr.s.low = n0;
656 rr.s.high = n1;
657 *rp = rr.ll;
660 else
662 /* 0q = NN / dd */
664 count_leading_zeros (bm, d1);
665 if (bm == 0)
667 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
668 conclude (the most significant bit of n1 is set) /\ (the
669 quotient digit q0 = 0 or 1).
671 This special case is necessary, not an optimization. */
673 /* The condition on the next line takes advantage of that
674 n1 >= d1 (true due to program flow). */
675 if (n1 > d1 || n0 >= d0)
677 q0 = 1;
678 sub_ddmmss (n1, n0, n1, n0, d1, d0);
680 else
681 q0 = 0;
683 q1 = 0;
685 if (rp != 0)
687 rr.s.low = n0;
688 rr.s.high = n1;
689 *rp = rr.ll;
692 else
694 UWtype m1, m0;
695 /* Normalize. */
697 b = W_TYPE_SIZE - bm;
699 d1 = (d1 << bm) | (d0 >> b);
700 d0 = d0 << bm;
701 n2 = n1 >> b;
702 n1 = (n1 << bm) | (n0 >> b);
703 n0 = n0 << bm;
705 udiv_qrnnd (q0, n1, n2, n1, d1);
706 umul_ppmm (m1, m0, q0, d0);
708 if (m1 > n1 || (m1 == n1 && m0 > n0))
710 q0--;
711 sub_ddmmss (m1, m0, m1, m0, d1, d0);
714 q1 = 0;
716 /* Remainder in (n1n0 - m1m0) >> bm. */
717 if (rp != 0)
719 sub_ddmmss (n1, n0, n1, n0, m1, m0);
720 rr.s.low = (n1 << b) | (n0 >> bm);
721 rr.s.high = n1 >> bm;
722 *rp = rr.ll;
728 ww.s.low = q0;
729 ww.s.high = q1;
730 return ww.ll;
732 #endif
734 #ifdef L_divdi3
735 DWtype
736 __divdi3 (DWtype u, DWtype v)
738 word_type c = 0;
739 DWunion uu, vv;
740 DWtype w;
742 uu.ll = u;
743 vv.ll = v;
745 if (uu.s.high < 0)
746 c = ~c,
747 uu.ll = -uu.ll;
748 if (vv.s.high < 0)
749 c = ~c,
750 vv.ll = -vv.ll;
752 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
753 if (c)
754 w = -w;
756 return w;
758 #endif
760 #ifdef L_moddi3
761 DWtype
762 __moddi3 (DWtype u, DWtype v)
764 word_type c = 0;
765 DWunion uu, vv;
766 DWtype w;
768 uu.ll = u;
769 vv.ll = v;
771 if (uu.s.high < 0)
772 c = ~c,
773 uu.ll = -uu.ll;
774 if (vv.s.high < 0)
775 vv.ll = -vv.ll;
777 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
778 if (c)
779 w = -w;
781 return w;
783 #endif
785 #ifdef L_umoddi3
786 UDWtype
787 __umoddi3 (UDWtype u, UDWtype v)
789 UDWtype w;
791 (void) __udivmoddi4 (u, v, &w);
793 return w;
795 #endif
797 #ifdef L_udivdi3
798 UDWtype
799 __udivdi3 (UDWtype n, UDWtype d)
801 return __udivmoddi4 (n, d, (UDWtype *) 0);
803 #endif
805 #ifdef L_cmpdi2
806 word_type
807 __cmpdi2 (DWtype a, DWtype b)
809 DWunion au, bu;
811 au.ll = a, bu.ll = b;
813 if (au.s.high < bu.s.high)
814 return 0;
815 else if (au.s.high > bu.s.high)
816 return 2;
817 if ((UWtype) au.s.low < (UWtype) bu.s.low)
818 return 0;
819 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
820 return 2;
821 return 1;
823 #endif
825 #ifdef L_ucmpdi2
826 word_type
827 __ucmpdi2 (DWtype a, DWtype b)
829 DWunion au, bu;
831 au.ll = a, bu.ll = b;
833 if ((UWtype) au.s.high < (UWtype) bu.s.high)
834 return 0;
835 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
836 return 2;
837 if ((UWtype) au.s.low < (UWtype) bu.s.low)
838 return 0;
839 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
840 return 2;
841 return 1;
843 #endif
845 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
846 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
847 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
849 DWtype
850 __fixunstfDI (TFtype a)
852 TFtype b;
853 UDWtype v;
855 if (a < 0)
856 return 0;
858 /* Compute high word of result, as a flonum. */
859 b = (a / HIGH_WORD_COEFF);
860 /* Convert that to fixed (but not to DWtype!),
861 and shift it into the high word. */
862 v = (UWtype) b;
863 v <<= WORD_SIZE;
864 /* Remove high part from the TFtype, leaving the low part as flonum. */
865 a -= (TFtype)v;
866 /* Convert that to fixed (but not to DWtype!) and add it in.
867 Sometimes A comes out negative. This is significant, since
868 A has more bits than a long int does. */
869 if (a < 0)
870 v -= (UWtype) (- a);
871 else
872 v += (UWtype) a;
873 return v;
875 #endif
877 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
878 DWtype
879 __fixtfdi (TFtype a)
881 if (a < 0)
882 return - __fixunstfDI (-a);
883 return __fixunstfDI (a);
885 #endif
887 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
888 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
889 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
891 DWtype
892 __fixunsxfDI (XFtype a)
894 XFtype b;
895 UDWtype v;
897 if (a < 0)
898 return 0;
900 /* Compute high word of result, as a flonum. */
901 b = (a / HIGH_WORD_COEFF);
902 /* Convert that to fixed (but not to DWtype!),
903 and shift it into the high word. */
904 v = (UWtype) b;
905 v <<= WORD_SIZE;
906 /* Remove high part from the XFtype, leaving the low part as flonum. */
907 a -= (XFtype)v;
908 /* Convert that to fixed (but not to DWtype!) and add it in.
909 Sometimes A comes out negative. This is significant, since
910 A has more bits than a long int does. */
911 if (a < 0)
912 v -= (UWtype) (- a);
913 else
914 v += (UWtype) a;
915 return v;
917 #endif
919 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
920 DWtype
921 __fixxfdi (XFtype a)
923 if (a < 0)
924 return - __fixunsxfDI (-a);
925 return __fixunsxfDI (a);
927 #endif
929 #ifdef L_fixunsdfdi
930 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
931 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
933 DWtype
934 __fixunsdfDI (DFtype a)
936 DFtype b;
937 UDWtype v;
939 if (a < 0)
940 return 0;
942 /* Compute high word of result, as a flonum. */
943 b = (a / HIGH_WORD_COEFF);
944 /* Convert that to fixed (but not to DWtype!),
945 and shift it into the high word. */
946 v = (UWtype) b;
947 v <<= WORD_SIZE;
948 /* Remove high part from the DFtype, leaving the low part as flonum. */
949 a -= (DFtype)v;
950 /* Convert that to fixed (but not to DWtype!) and add it in.
951 Sometimes A comes out negative. This is significant, since
952 A has more bits than a long int does. */
953 if (a < 0)
954 v -= (UWtype) (- a);
955 else
956 v += (UWtype) a;
957 return v;
959 #endif
961 #ifdef L_fixdfdi
962 DWtype
963 __fixdfdi (DFtype a)
965 if (a < 0)
966 return - __fixunsdfDI (-a);
967 return __fixunsdfDI (a);
969 #endif
971 #ifdef L_fixunssfdi
972 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
973 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
975 DWtype
976 __fixunssfDI (SFtype original_a)
978 /* Convert the SFtype to a DFtype, because that is surely not going
979 to lose any bits. Some day someone else can write a faster version
980 that avoids converting to DFtype, and verify it really works right. */
981 DFtype a = original_a;
982 DFtype b;
983 UDWtype v;
985 if (a < 0)
986 return 0;
988 /* Compute high word of result, as a flonum. */
989 b = (a / HIGH_WORD_COEFF);
990 /* Convert that to fixed (but not to DWtype!),
991 and shift it into the high word. */
992 v = (UWtype) b;
993 v <<= WORD_SIZE;
994 /* Remove high part from the DFtype, leaving the low part as flonum. */
995 a -= (DFtype) v;
996 /* Convert that to fixed (but not to DWtype!) and add it in.
997 Sometimes A comes out negative. This is significant, since
998 A has more bits than a long int does. */
999 if (a < 0)
1000 v -= (UWtype) (- a);
1001 else
1002 v += (UWtype) a;
1003 return v;
1005 #endif
1007 #ifdef L_fixsfdi
1008 DWtype
1009 __fixsfdi (SFtype a)
1011 if (a < 0)
1012 return - __fixunssfDI (-a);
1013 return __fixunssfDI (a);
1015 #endif
1017 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
1018 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1019 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1020 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1022 XFtype
1023 __floatdixf (DWtype u)
1025 XFtype d;
1027 d = (Wtype) (u >> WORD_SIZE);
1028 d *= HIGH_HALFWORD_COEFF;
1029 d *= HIGH_HALFWORD_COEFF;
1030 d += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1032 return d;
1034 #endif
1036 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
1037 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1038 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1039 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1041 TFtype
1042 __floatditf (DWtype u)
1044 TFtype d;
1046 d = (Wtype) (u >> WORD_SIZE);
1047 d *= HIGH_HALFWORD_COEFF;
1048 d *= HIGH_HALFWORD_COEFF;
1049 d += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1051 return d;
1053 #endif
1055 #ifdef L_floatdidf
1056 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1057 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1058 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1060 DFtype
1061 __floatdidf (DWtype u)
1063 DFtype d;
1065 d = (Wtype) (u >> WORD_SIZE);
1066 d *= HIGH_HALFWORD_COEFF;
1067 d *= HIGH_HALFWORD_COEFF;
1068 d += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1070 return d;
1072 #endif
1074 #ifdef L_floatdisf
1075 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
1076 #define HIGH_HALFWORD_COEFF (((UDWtype) 1) << (WORD_SIZE / 2))
1077 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
1079 #define DI_SIZE (sizeof (DWtype) * BITS_PER_UNIT)
1080 #define DF_SIZE DBL_MANT_DIG
1081 #define SF_SIZE FLT_MANT_DIG
1083 SFtype
1084 __floatdisf (DWtype u)
1086 /* Do the calculation in DFmode
1087 so that we don't lose any of the precision of the high word
1088 while multiplying it. */
1089 DFtype f;
1091 /* Protect against double-rounding error.
1092 Represent any low-order bits, that might be truncated in DFmode,
1093 by a bit that won't be lost. The bit can go in anywhere below the
1094 rounding position of the SFmode. A fixed mask and bit position
1095 handles all usual configurations. It doesn't handle the case
1096 of 128-bit DImode, however. */
1097 if (DF_SIZE < DI_SIZE
1098 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1100 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - DF_SIZE))
1101 if (! (- ((DWtype) 1 << DF_SIZE) < u
1102 && u < ((DWtype) 1 << DF_SIZE)))
1104 if ((UDWtype) u & (REP_BIT - 1))
1106 u &= ~ (REP_BIT - 1);
1107 u |= REP_BIT;
1111 f = (Wtype) (u >> WORD_SIZE);
1112 f *= HIGH_HALFWORD_COEFF;
1113 f *= HIGH_HALFWORD_COEFF;
1114 f += (UWtype) (u & (HIGH_WORD_COEFF - 1));
1116 return (SFtype) f;
1118 #endif
1120 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1121 /* Reenable the normal types, in case limits.h needs them. */
1122 #undef char
1123 #undef short
1124 #undef int
1125 #undef long
1126 #undef unsigned
1127 #undef float
1128 #undef double
1129 #undef MIN
1130 #undef MAX
1131 #include <limits.h>
1133 UWtype
1134 __fixunsxfSI (XFtype a)
1136 if (a >= - (DFtype) Wtype_MIN)
1137 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1138 return (Wtype) a;
1140 #endif
1142 #ifdef L_fixunsdfsi
1143 /* Reenable the normal types, in case limits.h needs them. */
1144 #undef char
1145 #undef short
1146 #undef int
1147 #undef long
1148 #undef unsigned
1149 #undef float
1150 #undef double
1151 #undef MIN
1152 #undef MAX
1153 #include <limits.h>
1155 UWtype
1156 __fixunsdfSI (DFtype a)
1158 if (a >= - (DFtype) Wtype_MIN)
1159 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1160 return (Wtype) a;
1162 #endif
1164 #ifdef L_fixunssfsi
1165 /* Reenable the normal types, in case limits.h needs them. */
1166 #undef char
1167 #undef short
1168 #undef int
1169 #undef long
1170 #undef unsigned
1171 #undef float
1172 #undef double
1173 #undef MIN
1174 #undef MAX
1175 #include <limits.h>
1177 UWtype
1178 __fixunssfSI (SFtype a)
1180 if (a >= - (SFtype) Wtype_MIN)
1181 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1182 return (Wtype) a;
1184 #endif
1186 /* From here on down, the routines use normal data types. */
1188 #define SItype bogus_type
1189 #define USItype bogus_type
1190 #define DItype bogus_type
1191 #define UDItype bogus_type
1192 #define SFtype bogus_type
1193 #define DFtype bogus_type
1194 #undef Wtype
1195 #undef UWtype
1196 #undef HWtype
1197 #undef UHWtype
1198 #undef DWtype
1199 #undef UDWtype
1201 #undef char
1202 #undef short
1203 #undef int
1204 #undef long
1205 #undef unsigned
1206 #undef float
1207 #undef double
1209 #ifdef L__gcc_bcmp
1211 /* Like bcmp except the sign is meaningful.
1212 Result is negative if S1 is less than S2,
1213 positive if S1 is greater, 0 if S1 and S2 are equal. */
1216 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1218 while (size > 0)
1220 unsigned char c1 = *s1++, c2 = *s2++;
1221 if (c1 != c2)
1222 return c1 - c2;
1223 size--;
1225 return 0;
1228 #endif
1230 /* __eprintf used to be used by GCC's private version of <assert.h>.
1231 We no longer provide that header, but this routine remains in libgcc.a
1232 for binary backward compatibility. Note that it is not included in
1233 the shared version of libgcc. */
1234 #ifdef L_eprintf
1235 #ifndef inhibit_libc
1237 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1238 #include <stdio.h>
1240 void
1241 __eprintf (const char *string, const char *expression,
1242 unsigned int line, const char *filename)
1244 fprintf (stderr, string, expression, line, filename);
1245 fflush (stderr);
1246 abort ();
1249 #endif
1250 #endif
1252 #ifdef L_bb
1254 struct bb_function_info {
1255 long checksum;
1256 int arc_count;
1257 const char *name;
1260 /* Structure emitted by --profile-arcs */
1261 struct bb
1263 long zero_word;
1264 const char *filename;
1265 gcov_type *counts;
1266 long ncounts;
1267 struct bb *next;
1269 /* Older GCC's did not emit these fields. */
1270 long sizeof_bb;
1271 struct bb_function_info *function_infos;
1274 #ifndef inhibit_libc
1276 /* Arc profile dumper. Requires atexit and stdio. */
1278 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1279 #include <stdio.h>
1281 #include "gcov-io.h"
1282 #include <string.h>
1283 #ifdef TARGET_HAS_F_SETLKW
1284 #include <fcntl.h>
1285 #include <errno.h>
1286 #endif
1288 /* Chain of per-object file bb structures. */
1289 static struct bb *bb_head;
1291 /* Dump the coverage counts. We merge with existing counts when
1292 possible, to avoid growing the .da files ad infinitum. */
1294 void
1295 __bb_exit_func (void)
1297 struct bb *ptr;
1298 int i;
1299 gcov_type program_sum = 0;
1300 gcov_type program_max = 0;
1301 long program_arcs = 0;
1302 gcov_type merged_sum = 0;
1303 gcov_type merged_max = 0;
1304 long merged_arcs = 0;
1306 #if defined (TARGET_HAS_F_SETLKW)
1307 struct flock s_flock;
1309 s_flock.l_type = F_WRLCK;
1310 s_flock.l_whence = SEEK_SET;
1311 s_flock.l_start = 0;
1312 s_flock.l_len = 0; /* Until EOF. */
1313 s_flock.l_pid = getpid ();
1314 #endif
1316 /* Non-merged stats for this program. */
1317 for (ptr = bb_head; ptr; ptr = ptr->next)
1319 for (i = 0; i < ptr->ncounts; i++)
1321 program_sum += ptr->counts[i];
1323 if (ptr->counts[i] > program_max)
1324 program_max = ptr->counts[i];
1326 program_arcs += ptr->ncounts;
1329 for (ptr = bb_head; ptr; ptr = ptr->next)
1331 FILE *da_file;
1332 gcov_type object_max = 0;
1333 gcov_type object_sum = 0;
1334 long object_functions = 0;
1335 int merging = 0;
1336 int error = 0;
1337 struct bb_function_info *fn_info;
1338 gcov_type *count_ptr;
1340 /* Open for modification */
1341 da_file = fopen (ptr->filename, "r+b");
1343 if (da_file)
1344 merging = 1;
1345 else
1347 /* Try for appending */
1348 da_file = fopen (ptr->filename, "ab");
1349 /* Some old systems might not allow the 'b' mode modifier.
1350 Therefore, try to open without it. This can lead to a
1351 race condition so that when you delete and re-create the
1352 file, the file might be opened in text mode, but then,
1353 you shouldn't delete the file in the first place. */
1354 if (!da_file)
1355 da_file = fopen (ptr->filename, "a");
1358 if (!da_file)
1360 fprintf (stderr, "arc profiling: Can't open output file %s.\n",
1361 ptr->filename);
1362 ptr->filename = 0;
1363 continue;
1366 #if defined (TARGET_HAS_F_SETLKW)
1367 /* After a fork, another process might try to read and/or write
1368 the same file simultanously. So if we can, lock the file to
1369 avoid race conditions. */
1370 while (fcntl (fileno (da_file), F_SETLKW, &s_flock)
1371 && errno == EINTR)
1372 continue;
1373 #endif
1374 for (fn_info = ptr->function_infos; fn_info->arc_count != -1; fn_info++)
1375 object_functions++;
1377 if (merging)
1379 /* Merge data from file. */
1380 long tmp_long;
1381 gcov_type tmp_gcov;
1383 if (/* magic */
1384 (__read_long (&tmp_long, da_file, 4) || tmp_long != -123l)
1385 /* functions in object file. */
1386 || (__read_long (&tmp_long, da_file, 4)
1387 || tmp_long != object_functions)
1388 /* extension block, skipped */
1389 || (__read_long (&tmp_long, da_file, 4)
1390 || fseek (da_file, tmp_long, SEEK_CUR)))
1392 read_error:;
1393 fprintf (stderr, "arc profiling: Error merging output file %s.\n",
1394 ptr->filename);
1395 clearerr (da_file);
1397 else
1399 /* Merge execution counts for each function. */
1400 count_ptr = ptr->counts;
1402 for (fn_info = ptr->function_infos; fn_info->arc_count != -1;
1403 fn_info++)
1405 if (/* function name delim */
1406 (__read_long (&tmp_long, da_file, 4)
1407 || tmp_long != -1)
1408 /* function name length */
1409 || (__read_long (&tmp_long, da_file, 4)
1410 || tmp_long != (long) strlen (fn_info->name))
1411 /* skip string */
1412 || fseek (da_file, ((tmp_long + 1) + 3) & ~3, SEEK_CUR)
1413 /* function name delim */
1414 || (__read_long (&tmp_long, da_file, 4)
1415 || tmp_long != -1))
1416 goto read_error;
1418 if (/* function checksum */
1419 (__read_long (&tmp_long, da_file, 4)
1420 || tmp_long != fn_info->checksum)
1421 /* arc count */
1422 || (__read_long (&tmp_long, da_file, 4)
1423 || tmp_long != fn_info->arc_count))
1424 goto read_error;
1426 for (i = fn_info->arc_count; i > 0; i--, count_ptr++)
1427 if (__read_gcov_type (&tmp_gcov, da_file, 8))
1428 goto read_error;
1429 else
1430 *count_ptr += tmp_gcov;
1433 fseek (da_file, 0, SEEK_SET);
1436 /* Calculate the per-object statistics. */
1437 for (i = 0; i < ptr->ncounts; i++)
1439 object_sum += ptr->counts[i];
1441 if (ptr->counts[i] > object_max)
1442 object_max = ptr->counts[i];
1444 merged_sum += object_sum;
1445 if (merged_max < object_max)
1446 merged_max = object_max;
1447 merged_arcs += ptr->ncounts;
1449 /* Write out the data. */
1450 if (/* magic */
1451 __write_long (-123, da_file, 4)
1452 /* number of functions in object file. */
1453 || __write_long (object_functions, da_file, 4)
1454 /* length of extra data in bytes. */
1455 || __write_long ((4 + 8 + 8) + (4 + 8 + 8), da_file, 4)
1457 /* whole program statistics. If merging write per-object
1458 now, rewrite later */
1459 /* number of instrumented arcs. */
1460 || __write_long (merging ? ptr->ncounts : program_arcs, da_file, 4)
1461 /* sum of counters. */
1462 || __write_gcov_type (merging ? object_sum : program_sum, da_file, 8)
1463 /* maximal counter. */
1464 || __write_gcov_type (merging ? object_max : program_max, da_file, 8)
1466 /* per-object statistics. */
1467 /* number of counters. */
1468 || __write_long (ptr->ncounts, da_file, 4)
1469 /* sum of counters. */
1470 || __write_gcov_type (object_sum, da_file, 8)
1471 /* maximal counter. */
1472 || __write_gcov_type (object_max, da_file, 8))
1474 write_error:;
1475 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1476 ptr->filename);
1477 error = 1;
1479 else
1481 /* Write execution counts for each function. */
1482 count_ptr = ptr->counts;
1484 for (fn_info = ptr->function_infos; fn_info->arc_count != -1;
1485 fn_info++)
1487 if (__write_gcov_string (fn_info->name,
1488 strlen (fn_info->name), da_file, -1)
1489 || __write_long (fn_info->checksum, da_file, 4)
1490 || __write_long (fn_info->arc_count, da_file, 4))
1491 goto write_error;
1493 for (i = fn_info->arc_count; i > 0; i--, count_ptr++)
1494 if (__write_gcov_type (*count_ptr, da_file, 8))
1495 goto write_error; /* RIP Edsger Dijkstra */
1499 if (fclose (da_file))
1501 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1502 ptr->filename);
1503 error = 1;
1505 if (error || !merging)
1506 ptr->filename = 0;
1509 /* Upate whole program statistics. */
1510 for (ptr = bb_head; ptr; ptr = ptr->next)
1511 if (ptr->filename)
1513 FILE *da_file;
1515 da_file = fopen (ptr->filename, "r+b");
1516 if (!da_file)
1518 fprintf (stderr, "arc profiling: Cannot reopen %s.\n",
1519 ptr->filename);
1520 continue;
1523 #if defined (TARGET_HAS_F_SETLKW)
1524 while (fcntl (fileno (da_file), F_SETLKW, &s_flock)
1525 && errno == EINTR)
1526 continue;
1527 #endif
1529 if (fseek (da_file, 4 * 3, SEEK_SET)
1530 /* number of instrumented arcs. */
1531 || __write_long (merged_arcs, da_file, 4)
1532 /* sum of counters. */
1533 || __write_gcov_type (merged_sum, da_file, 8)
1534 /* maximal counter. */
1535 || __write_gcov_type (merged_max, da_file, 8))
1536 fprintf (stderr, "arc profiling: Error updating program header %s.\n",
1537 ptr->filename);
1538 if (fclose (da_file))
1539 fprintf (stderr, "arc profiling: Error reclosing %s\n",
1540 ptr->filename);
1544 /* Add a new object file onto the bb chain. Invoked automatically
1545 when running an object file's global ctors. */
1547 void
1548 __bb_init_func (struct bb *blocks)
1550 if (blocks->zero_word)
1551 return;
1553 /* Initialize destructor and per-thread data. */
1554 if (!bb_head)
1555 atexit (__bb_exit_func);
1557 /* Set up linked list. */
1558 blocks->zero_word = 1;
1559 blocks->next = bb_head;
1560 bb_head = blocks;
1563 /* Called before fork or exec - write out profile information gathered so
1564 far and reset it to zero. This avoids duplication or loss of the
1565 profile information gathered so far. */
1567 void
1568 __bb_fork_func (void)
1570 struct bb *ptr;
1572 __bb_exit_func ();
1573 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1575 long i;
1576 for (i = ptr->ncounts - 1; i >= 0; i--)
1577 ptr->counts[i] = 0;
1581 #endif /* not inhibit_libc */
1582 #endif /* L_bb */
1584 #ifdef L_clear_cache
1585 /* Clear part of an instruction cache. */
1587 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
1589 void
1590 __clear_cache (char *beg __attribute__((__unused__)),
1591 char *end __attribute__((__unused__)))
1593 #ifdef CLEAR_INSN_CACHE
1594 CLEAR_INSN_CACHE (beg, end);
1595 #else
1596 #ifdef INSN_CACHE_SIZE
1597 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
1598 static int initialized;
1599 int offset;
1600 void *start_addr
1601 void *end_addr;
1602 typedef (*function_ptr) (void);
1604 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
1605 /* It's cheaper to clear the whole cache.
1606 Put in a series of jump instructions so that calling the beginning
1607 of the cache will clear the whole thing. */
1609 if (! initialized)
1611 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
1612 & -INSN_CACHE_LINE_WIDTH);
1613 int end_ptr = ptr + INSN_CACHE_SIZE;
1615 while (ptr < end_ptr)
1617 *(INSTRUCTION_TYPE *)ptr
1618 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
1619 ptr += INSN_CACHE_LINE_WIDTH;
1621 *(INSTRUCTION_TYPE *) (ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
1623 initialized = 1;
1626 /* Call the beginning of the sequence. */
1627 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
1628 & -INSN_CACHE_LINE_WIDTH))
1629 ());
1631 #else /* Cache is large. */
1633 if (! initialized)
1635 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
1636 & -INSN_CACHE_LINE_WIDTH);
1638 while (ptr < (int) array + sizeof array)
1640 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
1641 ptr += INSN_CACHE_LINE_WIDTH;
1644 initialized = 1;
1647 /* Find the location in array that occupies the same cache line as BEG. */
1649 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
1650 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
1651 & -INSN_CACHE_PLANE_SIZE)
1652 + offset);
1654 /* Compute the cache alignment of the place to stop clearing. */
1655 #if 0 /* This is not needed for gcc's purposes. */
1656 /* If the block to clear is bigger than a cache plane,
1657 we clear the entire cache, and OFFSET is already correct. */
1658 if (end < beg + INSN_CACHE_PLANE_SIZE)
1659 #endif
1660 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
1661 & -INSN_CACHE_LINE_WIDTH)
1662 & (INSN_CACHE_PLANE_SIZE - 1));
1664 #if INSN_CACHE_DEPTH > 1
1665 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
1666 if (end_addr <= start_addr)
1667 end_addr += INSN_CACHE_PLANE_SIZE;
1669 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
1671 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
1672 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
1674 while (addr != stop)
1676 /* Call the return instruction at ADDR. */
1677 ((function_ptr) addr) ();
1679 addr += INSN_CACHE_LINE_WIDTH;
1682 #else /* just one plane */
1685 /* Call the return instruction at START_ADDR. */
1686 ((function_ptr) start_addr) ();
1688 start_addr += INSN_CACHE_LINE_WIDTH;
1690 while ((start_addr % INSN_CACHE_SIZE) != offset);
1691 #endif /* just one plane */
1692 #endif /* Cache is large */
1693 #endif /* Cache exists */
1694 #endif /* CLEAR_INSN_CACHE */
1697 #endif /* L_clear_cache */
1699 #ifdef L_trampoline
1701 /* Jump to a trampoline, loading the static chain address. */
1703 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
1705 long
1706 getpagesize (void)
1708 #ifdef _ALPHA_
1709 return 8192;
1710 #else
1711 return 4096;
1712 #endif
1715 #ifdef __i386__
1716 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
1717 #endif
1720 mprotect (char *addr, int len, int prot)
1722 int np, op;
1724 if (prot == 7)
1725 np = 0x40;
1726 else if (prot == 5)
1727 np = 0x20;
1728 else if (prot == 4)
1729 np = 0x10;
1730 else if (prot == 3)
1731 np = 0x04;
1732 else if (prot == 1)
1733 np = 0x02;
1734 else if (prot == 0)
1735 np = 0x01;
1737 if (VirtualProtect (addr, len, np, &op))
1738 return 0;
1739 else
1740 return -1;
1743 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
1745 #ifdef TRANSFER_FROM_TRAMPOLINE
1746 TRANSFER_FROM_TRAMPOLINE
1747 #endif
1749 #ifdef __sysV68__
1751 #include <sys/signal.h>
1752 #include <errno.h>
1754 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
1755 so define it here, because we need it in __clear_insn_cache below */
1756 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
1757 hence we enable this stuff only if MCT_TEXT is #define'd. */
1759 #ifdef MCT_TEXT
1760 asm("\n\
1761 global memctl\n\
1762 memctl:\n\
1763 movq &75,%d0\n\
1764 trap &0\n\
1765 bcc.b noerror\n\
1766 jmp cerror%\n\
1767 noerror:\n\
1768 movq &0,%d0\n\
1769 rts");
1770 #endif
1772 /* Clear instruction cache so we can call trampolines on stack.
1773 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
1775 void
1776 __clear_insn_cache (void)
1778 #ifdef MCT_TEXT
1779 int save_errno;
1781 /* Preserve errno, because users would be surprised to have
1782 errno changing without explicitly calling any system-call. */
1783 save_errno = errno;
1785 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
1786 No need to use an address derived from _start or %sp, as 0 works also. */
1787 memctl(0, 4096, MCT_TEXT);
1788 errno = save_errno;
1789 #endif
1792 #endif /* __sysV68__ */
1793 #endif /* L_trampoline */
1795 #ifndef __CYGWIN__
1796 #ifdef L__main
1798 #include "gbl-ctors.h"
1799 /* Some systems use __main in a way incompatible with its use in gcc, in these
1800 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
1801 give the same symbol without quotes for an alternative entry point. You
1802 must define both, or neither. */
1803 #ifndef NAME__MAIN
1804 #define NAME__MAIN "__main"
1805 #define SYMBOL__MAIN __main
1806 #endif
1808 #ifdef INIT_SECTION_ASM_OP
1809 #undef HAS_INIT_SECTION
1810 #define HAS_INIT_SECTION
1811 #endif
1813 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
1815 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
1816 code to run constructors. In that case, we need to handle EH here, too. */
1818 #ifdef EH_FRAME_SECTION_NAME
1819 #include "unwind-dw2-fde.h"
1820 extern unsigned char __EH_FRAME_BEGIN__[];
1821 #endif
1823 /* Run all the global destructors on exit from the program. */
1825 void
1826 __do_global_dtors (void)
1828 #ifdef DO_GLOBAL_DTORS_BODY
1829 DO_GLOBAL_DTORS_BODY;
1830 #else
1831 static func_ptr *p = __DTOR_LIST__ + 1;
1832 while (*p)
1834 p++;
1835 (*(p-1)) ();
1837 #endif
1838 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
1840 static int completed = 0;
1841 if (! completed)
1843 completed = 1;
1844 __deregister_frame_info (__EH_FRAME_BEGIN__);
1847 #endif
1849 #endif
1851 #ifndef HAS_INIT_SECTION
1852 /* Run all the global constructors on entry to the program. */
1854 void
1855 __do_global_ctors (void)
1857 #ifdef EH_FRAME_SECTION_NAME
1859 static struct object object;
1860 __register_frame_info (__EH_FRAME_BEGIN__, &object);
1862 #endif
1863 DO_GLOBAL_CTORS_BODY;
1864 atexit (__do_global_dtors);
1866 #endif /* no HAS_INIT_SECTION */
1868 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
1869 /* Subroutine called automatically by `main'.
1870 Compiling a global function named `main'
1871 produces an automatic call to this function at the beginning.
1873 For many systems, this routine calls __do_global_ctors.
1874 For systems which support a .init section we use the .init section
1875 to run __do_global_ctors, so we need not do anything here. */
1877 void
1878 SYMBOL__MAIN ()
1880 /* Support recursive calls to `main': run initializers just once. */
1881 static int initialized;
1882 if (! initialized)
1884 initialized = 1;
1885 __do_global_ctors ();
1888 #endif /* no HAS_INIT_SECTION or INVOKE__main */
1890 #endif /* L__main */
1891 #endif /* __CYGWIN__ */
1893 #ifdef L_ctors
1895 #include "gbl-ctors.h"
1897 /* Provide default definitions for the lists of constructors and
1898 destructors, so that we don't get linker errors. These symbols are
1899 intentionally bss symbols, so that gld and/or collect will provide
1900 the right values. */
1902 /* We declare the lists here with two elements each,
1903 so that they are valid empty lists if no other definition is loaded.
1905 If we are using the old "set" extensions to have the gnu linker
1906 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
1907 must be in the bss/common section.
1909 Long term no port should use those extensions. But many still do. */
1910 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
1911 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
1912 func_ptr __CTOR_LIST__[2] = {0, 0};
1913 func_ptr __DTOR_LIST__[2] = {0, 0};
1914 #else
1915 func_ptr __CTOR_LIST__[2];
1916 func_ptr __DTOR_LIST__[2];
1917 #endif
1918 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
1919 #endif /* L_ctors */
1921 #ifdef L_exit
1923 #include "gbl-ctors.h"
1925 #ifdef NEED_ATEXIT
1927 #ifndef ON_EXIT
1929 # include <errno.h>
1931 static func_ptr *atexit_chain = 0;
1932 static long atexit_chain_length = 0;
1933 static volatile long last_atexit_chain_slot = -1;
1936 atexit (func_ptr func)
1938 if (++last_atexit_chain_slot == atexit_chain_length)
1940 atexit_chain_length += 32;
1941 if (atexit_chain)
1942 atexit_chain = (func_ptr *) realloc (atexit_chain, atexit_chain_length
1943 * sizeof (func_ptr));
1944 else
1945 atexit_chain = (func_ptr *) malloc (atexit_chain_length
1946 * sizeof (func_ptr));
1947 if (! atexit_chain)
1949 atexit_chain_length = 0;
1950 last_atexit_chain_slot = -1;
1951 errno = ENOMEM;
1952 return (-1);
1955 atexit_chain[last_atexit_chain_slot] = func;
1956 return (0);
1959 extern void _cleanup (void);
1960 extern void _exit (int) __attribute__ ((__noreturn__));
1962 void
1963 exit (int status)
1965 if (atexit_chain)
1967 for ( ; last_atexit_chain_slot-- >= 0; )
1969 (*atexit_chain[last_atexit_chain_slot + 1]) ();
1970 atexit_chain[last_atexit_chain_slot + 1] = 0;
1972 free (atexit_chain);
1973 atexit_chain = 0;
1975 #ifdef EXIT_BODY
1976 EXIT_BODY;
1977 #else
1978 _cleanup ();
1979 #endif
1980 _exit (status);
1983 #else /* ON_EXIT */
1985 /* Simple; we just need a wrapper for ON_EXIT. */
1987 atexit (func_ptr func)
1989 return ON_EXIT (func);
1992 #endif /* ON_EXIT */
1993 #endif /* NEED_ATEXIT */
1995 #endif /* L_exit */