Do not include stddef.h if building trampoline support and inhibit_libc is defined
[official-gcc.git] / gcc / libgcc2.c
bloba05d7fd52aa267420bd2b662504481e5bb61b428
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)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
29 /* It is incorrect to include config.h here, because this file is being
30 compiled for the target, and hence definitions concerning only the host
31 do not apply. */
33 #include "tconfig.h"
35 /* We disable this when inhibit_libc, so that gcc can still be built without
36 needing header files first. */
37 /* ??? This is not a good solution, since prototypes may be required in
38 some cases for correct code. See also frame.c. */
39 #ifndef inhibit_libc
40 /* fixproto guarantees these system headers exist. */
41 #include <stdlib.h>
42 #include <unistd.h>
44 #else
45 #ifndef L_trampoline
46 #include <stddef.h>
47 #ifndef malloc
48 extern void *malloc (size_t);
49 #endif
50 #ifndef free
51 extern void free (void *);
52 #endif
53 #ifndef atexit
54 extern int atexit(void (*)(void));
55 #endif
56 #endif
57 #endif
59 #include "machmode.h"
60 #include "defaults.h"
61 #ifndef L_trampoline
62 #include <stddef.h>
63 #endif
65 /* Don't use `fancy_abort' here even if config.h says to use it. */
66 #ifdef abort
67 #undef abort
68 #endif
70 /* In a cross-compilation situation, default to inhibiting compilation
71 of routines that use libc. */
73 #if defined(CROSS_COMPILE) && !defined(inhibit_libc)
74 #define inhibit_libc
75 #endif
77 /* Permit the tm.h file to select the endianness to use just for this
78 file. This is used when the endianness is determined when the
79 compiler is run. */
81 #ifndef LIBGCC2_WORDS_BIG_ENDIAN
82 #define LIBGCC2_WORDS_BIG_ENDIAN WORDS_BIG_ENDIAN
83 #endif
85 #ifndef LIBGCC2_LONG_DOUBLE_TYPE_SIZE
86 #define LIBGCC2_LONG_DOUBLE_TYPE_SIZE LONG_DOUBLE_TYPE_SIZE
87 #endif
89 /* In the first part of this file, we are interfacing to calls generated
90 by the compiler itself. These calls pass values into these routines
91 which have very specific modes (rather than very specific types), and
92 these compiler-generated calls also expect any return values to have
93 very specific modes (rather than very specific types). Thus, we need
94 to avoid using regular C language type names in this part of the file
95 because the sizes for those types can be configured to be anything.
96 Instead we use the following special type names. */
98 typedef unsigned int UQItype __attribute__ ((mode (QI)));
99 typedef int SItype __attribute__ ((mode (SI)));
100 typedef unsigned int USItype __attribute__ ((mode (SI)));
101 typedef int DItype __attribute__ ((mode (DI)));
102 typedef unsigned int UDItype __attribute__ ((mode (DI)));
104 typedef float SFtype __attribute__ ((mode (SF)));
105 typedef float DFtype __attribute__ ((mode (DF)));
107 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
108 typedef float XFtype __attribute__ ((mode (XF)));
109 #endif
110 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
111 typedef float TFtype __attribute__ ((mode (TF)));
112 #endif
114 typedef int word_type __attribute__ ((mode (__word__)));
116 /* Make sure that we don't accidentally use any normal C language built-in
117 type names in the first part of this file. Instead we want to use *only*
118 the type names defined above. The following macro definitions insure
119 that if we *do* accidentally use some normal C language built-in type name,
120 we will get a syntax error. */
122 #define char bogus_type
123 #define short bogus_type
124 #define int bogus_type
125 #define long bogus_type
126 #define unsigned bogus_type
127 #define float bogus_type
128 #define double bogus_type
130 #define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
132 /* DIstructs are pairs of SItype values in the order determined by
133 LIBGCC2_WORDS_BIG_ENDIAN. */
135 #if LIBGCC2_WORDS_BIG_ENDIAN
136 struct DIstruct {SItype high, low;};
137 #else
138 struct DIstruct {SItype low, high;};
139 #endif
141 /* We need this union to unpack/pack DImode values, since we don't have
142 any arithmetic yet. Incoming DImode parameters are stored into the
143 `ll' field, and the unpacked result is read from the struct `s'. */
145 typedef union
147 struct DIstruct s;
148 DItype ll;
149 } DIunion;
151 #if (defined (L_udivmoddi4) || defined (L_muldi3) || defined (L_udiv_w_sdiv)\
152 || defined (L_divdi3) || defined (L_udivdi3) \
153 || defined (L_moddi3) || defined (L_umoddi3))
155 #include "longlong.h"
157 #endif /* udiv or mul */
159 extern DItype __fixunssfdi (SFtype a);
160 extern DItype __fixunsdfdi (DFtype a);
161 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
162 extern DItype __fixunsxfdi (XFtype a);
163 #endif
164 #if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
165 extern DItype __fixunstfdi (TFtype a);
166 #endif
168 #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
169 #if defined (L_divdi3) || defined (L_moddi3)
170 static inline
171 #endif
172 DItype
173 __negdi2 (DItype u)
175 DIunion w;
176 DIunion uu;
178 uu.ll = u;
180 w.s.low = -uu.s.low;
181 w.s.high = -uu.s.high - ((USItype) w.s.low > 0);
183 return w.ll;
185 #endif
187 /* Unless shift functions are defined whith full ANSI prototypes,
188 parameter b will be promoted to int if word_type is smaller than an int. */
189 #ifdef L_lshrdi3
190 DItype
191 __lshrdi3 (DItype u, word_type b)
193 DIunion w;
194 word_type bm;
195 DIunion uu;
197 if (b == 0)
198 return u;
200 uu.ll = u;
202 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
203 if (bm <= 0)
205 w.s.high = 0;
206 w.s.low = (USItype)uu.s.high >> -bm;
208 else
210 USItype carries = (USItype)uu.s.high << bm;
211 w.s.high = (USItype)uu.s.high >> b;
212 w.s.low = ((USItype)uu.s.low >> b) | carries;
215 return w.ll;
217 #endif
219 #ifdef L_ashldi3
220 DItype
221 __ashldi3 (DItype u, word_type b)
223 DIunion w;
224 word_type bm;
225 DIunion uu;
227 if (b == 0)
228 return u;
230 uu.ll = u;
232 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
233 if (bm <= 0)
235 w.s.low = 0;
236 w.s.high = (USItype)uu.s.low << -bm;
238 else
240 USItype carries = (USItype)uu.s.low >> bm;
241 w.s.low = (USItype)uu.s.low << b;
242 w.s.high = ((USItype)uu.s.high << b) | carries;
245 return w.ll;
247 #endif
249 #ifdef L_ashrdi3
250 DItype
251 __ashrdi3 (DItype u, word_type b)
253 DIunion w;
254 word_type bm;
255 DIunion uu;
257 if (b == 0)
258 return u;
260 uu.ll = u;
262 bm = (sizeof (SItype) * BITS_PER_UNIT) - b;
263 if (bm <= 0)
265 /* w.s.high = 1..1 or 0..0 */
266 w.s.high = uu.s.high >> (sizeof (SItype) * BITS_PER_UNIT - 1);
267 w.s.low = uu.s.high >> -bm;
269 else
271 USItype carries = (USItype)uu.s.high << bm;
272 w.s.high = uu.s.high >> b;
273 w.s.low = ((USItype)uu.s.low >> b) | carries;
276 return w.ll;
278 #endif
280 #ifdef L_ffsdi2
281 DItype
282 __ffsdi2 (DItype u)
284 DIunion uu, w;
285 uu.ll = u;
286 w.s.high = 0;
287 w.s.low = ffs (uu.s.low);
288 if (w.s.low != 0)
289 return w.ll;
290 w.s.low = ffs (uu.s.high);
291 if (w.s.low != 0)
293 w.s.low += BITS_PER_UNIT * sizeof (SItype);
294 return w.ll;
296 return w.ll;
298 #endif
300 #ifdef L_muldi3
301 DItype
302 __muldi3 (DItype u, DItype v)
304 DIunion w;
305 DIunion uu, vv;
307 uu.ll = u,
308 vv.ll = v;
310 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
311 w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
312 + (USItype) uu.s.high * (USItype) vv.s.low);
314 return w.ll;
316 #endif
318 #ifdef L_udiv_w_sdiv
319 #if defined (sdiv_qrnnd)
320 USItype
321 __udiv_w_sdiv (USItype *rp, USItype a1, USItype a0, USItype d)
323 USItype q, r;
324 USItype c0, c1, b1;
326 if ((SItype) d >= 0)
328 if (a1 < d - a1 - (a0 >> (SI_TYPE_SIZE - 1)))
330 /* dividend, divisor, and quotient are nonnegative */
331 sdiv_qrnnd (q, r, a1, a0, d);
333 else
335 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
336 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (SI_TYPE_SIZE - 1));
337 /* Divide (c1*2^32 + c0) by d */
338 sdiv_qrnnd (q, r, c1, c0, d);
339 /* Add 2^31 to quotient */
340 q += (USItype) 1 << (SI_TYPE_SIZE - 1);
343 else
345 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
346 c1 = a1 >> 1; /* A/2 */
347 c0 = (a1 << (SI_TYPE_SIZE - 1)) + (a0 >> 1);
349 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
351 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
353 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
354 if ((d & 1) != 0)
356 if (r >= q)
357 r = r - q;
358 else if (q - r <= d)
360 r = r - q + d;
361 q--;
363 else
365 r = r - q + 2*d;
366 q -= 2;
370 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
372 c1 = (b1 - 1) - c1;
373 c0 = ~c0; /* logical NOT */
375 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
377 q = ~q; /* (A/2)/b1 */
378 r = (b1 - 1) - r;
380 r = 2*r + (a0 & 1); /* A/(2*b1) */
382 if ((d & 1) != 0)
384 if (r >= q)
385 r = r - q;
386 else if (q - r <= d)
388 r = r - q + d;
389 q--;
391 else
393 r = r - q + 2*d;
394 q -= 2;
398 else /* Implies c1 = b1 */
399 { /* Hence a1 = d - 1 = 2*b1 - 1 */
400 if (a0 >= -d)
402 q = -1;
403 r = a0 + d;
405 else
407 q = -2;
408 r = a0 + 2*d;
413 *rp = r;
414 return q;
416 #else
417 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
418 USItype
419 __udiv_w_sdiv (USItype *rp __attribute__ ((__unused__)),
420 USItype a1 __attribute__ ((__unused__)),
421 USItype a0 __attribute__ ((__unused__)),
422 USItype d __attribute__ ((__unused__)))
424 return 0;
426 #endif
427 #endif
429 #if (defined (L_udivdi3) || defined (L_divdi3) || \
430 defined (L_umoddi3) || defined (L_moddi3))
431 #define L_udivmoddi4
432 #endif
434 #ifdef L_udivmoddi4
435 static const UQItype __clz_tab[] =
437 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,
438 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,
439 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,
440 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,
441 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,
442 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,
443 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,
444 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,
447 #if (defined (L_udivdi3) || defined (L_divdi3) || \
448 defined (L_umoddi3) || defined (L_moddi3))
449 static inline
450 #endif
451 UDItype
452 __udivmoddi4 (UDItype n, UDItype d, UDItype *rp)
454 DIunion ww;
455 DIunion nn, dd;
456 DIunion rr;
457 USItype d0, d1, n0, n1, n2;
458 USItype q0, q1;
459 USItype b, bm;
461 nn.ll = n;
462 dd.ll = d;
464 d0 = dd.s.low;
465 d1 = dd.s.high;
466 n0 = nn.s.low;
467 n1 = nn.s.high;
469 #if !UDIV_NEEDS_NORMALIZATION
470 if (d1 == 0)
472 if (d0 > n1)
474 /* 0q = nn / 0D */
476 udiv_qrnnd (q0, n0, n1, n0, d0);
477 q1 = 0;
479 /* Remainder in n0. */
481 else
483 /* qq = NN / 0d */
485 if (d0 == 0)
486 d0 = 1 / d0; /* Divide intentionally by zero. */
488 udiv_qrnnd (q1, n1, 0, n1, d0);
489 udiv_qrnnd (q0, n0, n1, n0, d0);
491 /* Remainder in n0. */
494 if (rp != 0)
496 rr.s.low = n0;
497 rr.s.high = 0;
498 *rp = rr.ll;
502 #else /* UDIV_NEEDS_NORMALIZATION */
504 if (d1 == 0)
506 if (d0 > n1)
508 /* 0q = nn / 0D */
510 count_leading_zeros (bm, d0);
512 if (bm != 0)
514 /* Normalize, i.e. make the most significant bit of the
515 denominator set. */
517 d0 = d0 << bm;
518 n1 = (n1 << bm) | (n0 >> (SI_TYPE_SIZE - bm));
519 n0 = n0 << bm;
522 udiv_qrnnd (q0, n0, n1, n0, d0);
523 q1 = 0;
525 /* Remainder in n0 >> bm. */
527 else
529 /* qq = NN / 0d */
531 if (d0 == 0)
532 d0 = 1 / d0; /* Divide intentionally by zero. */
534 count_leading_zeros (bm, d0);
536 if (bm == 0)
538 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
539 conclude (the most significant bit of n1 is set) /\ (the
540 leading quotient digit q1 = 1).
542 This special case is necessary, not an optimization.
543 (Shifts counts of SI_TYPE_SIZE are undefined.) */
545 n1 -= d0;
546 q1 = 1;
548 else
550 /* Normalize. */
552 b = SI_TYPE_SIZE - bm;
554 d0 = d0 << bm;
555 n2 = n1 >> b;
556 n1 = (n1 << bm) | (n0 >> b);
557 n0 = n0 << bm;
559 udiv_qrnnd (q1, n1, n2, n1, d0);
562 /* n1 != d0... */
564 udiv_qrnnd (q0, n0, n1, n0, d0);
566 /* Remainder in n0 >> bm. */
569 if (rp != 0)
571 rr.s.low = n0 >> bm;
572 rr.s.high = 0;
573 *rp = rr.ll;
576 #endif /* UDIV_NEEDS_NORMALIZATION */
578 else
580 if (d1 > n1)
582 /* 00 = nn / DD */
584 q0 = 0;
585 q1 = 0;
587 /* Remainder in n1n0. */
588 if (rp != 0)
590 rr.s.low = n0;
591 rr.s.high = n1;
592 *rp = rr.ll;
595 else
597 /* 0q = NN / dd */
599 count_leading_zeros (bm, d1);
600 if (bm == 0)
602 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
603 conclude (the most significant bit of n1 is set) /\ (the
604 quotient digit q0 = 0 or 1).
606 This special case is necessary, not an optimization. */
608 /* The condition on the next line takes advantage of that
609 n1 >= d1 (true due to program flow). */
610 if (n1 > d1 || n0 >= d0)
612 q0 = 1;
613 sub_ddmmss (n1, n0, n1, n0, d1, d0);
615 else
616 q0 = 0;
618 q1 = 0;
620 if (rp != 0)
622 rr.s.low = n0;
623 rr.s.high = n1;
624 *rp = rr.ll;
627 else
629 USItype m1, m0;
630 /* Normalize. */
632 b = SI_TYPE_SIZE - bm;
634 d1 = (d1 << bm) | (d0 >> b);
635 d0 = d0 << bm;
636 n2 = n1 >> b;
637 n1 = (n1 << bm) | (n0 >> b);
638 n0 = n0 << bm;
640 udiv_qrnnd (q0, n1, n2, n1, d1);
641 umul_ppmm (m1, m0, q0, d0);
643 if (m1 > n1 || (m1 == n1 && m0 > n0))
645 q0--;
646 sub_ddmmss (m1, m0, m1, m0, d1, d0);
649 q1 = 0;
651 /* Remainder in (n1n0 - m1m0) >> bm. */
652 if (rp != 0)
654 sub_ddmmss (n1, n0, n1, n0, m1, m0);
655 rr.s.low = (n1 << b) | (n0 >> bm);
656 rr.s.high = n1 >> bm;
657 *rp = rr.ll;
663 ww.s.low = q0;
664 ww.s.high = q1;
665 return ww.ll;
667 #endif
669 #ifdef L_divdi3
670 UDItype __udivmoddi4 ();
672 DItype
673 __divdi3 (DItype u, DItype v)
675 word_type c = 0;
676 DIunion uu, vv;
677 DItype w;
679 uu.ll = u;
680 vv.ll = v;
682 if (uu.s.high < 0)
683 c = ~c,
684 uu.ll = __negdi2 (uu.ll);
685 if (vv.s.high < 0)
686 c = ~c,
687 vv.ll = __negdi2 (vv.ll);
689 w = __udivmoddi4 (uu.ll, vv.ll, (UDItype *) 0);
690 if (c)
691 w = __negdi2 (w);
693 return w;
695 #endif
697 #ifdef L_moddi3
698 UDItype __udivmoddi4 ();
699 DItype
700 __moddi3 (DItype u, DItype v)
702 word_type c = 0;
703 DIunion uu, vv;
704 DItype w;
706 uu.ll = u;
707 vv.ll = v;
709 if (uu.s.high < 0)
710 c = ~c,
711 uu.ll = __negdi2 (uu.ll);
712 if (vv.s.high < 0)
713 vv.ll = __negdi2 (vv.ll);
715 (void) __udivmoddi4 (uu.ll, vv.ll, &w);
716 if (c)
717 w = __negdi2 (w);
719 return w;
721 #endif
723 #ifdef L_umoddi3
724 UDItype __udivmoddi4 ();
725 UDItype
726 __umoddi3 (UDItype u, UDItype v)
728 UDItype w;
730 (void) __udivmoddi4 (u, v, &w);
732 return w;
734 #endif
736 #ifdef L_udivdi3
737 UDItype __udivmoddi4 ();
738 UDItype
739 __udivdi3 (UDItype n, UDItype d)
741 return __udivmoddi4 (n, d, (UDItype *) 0);
743 #endif
745 #ifdef L_cmpdi2
746 word_type
747 __cmpdi2 (DItype a, DItype b)
749 DIunion au, bu;
751 au.ll = a, bu.ll = b;
753 if (au.s.high < bu.s.high)
754 return 0;
755 else if (au.s.high > bu.s.high)
756 return 2;
757 if ((USItype) au.s.low < (USItype) bu.s.low)
758 return 0;
759 else if ((USItype) au.s.low > (USItype) bu.s.low)
760 return 2;
761 return 1;
763 #endif
765 #ifdef L_ucmpdi2
766 word_type
767 __ucmpdi2 (DItype a, DItype b)
769 DIunion au, bu;
771 au.ll = a, bu.ll = b;
773 if ((USItype) au.s.high < (USItype) bu.s.high)
774 return 0;
775 else if ((USItype) au.s.high > (USItype) bu.s.high)
776 return 2;
777 if ((USItype) au.s.low < (USItype) bu.s.low)
778 return 0;
779 else if ((USItype) au.s.low > (USItype) bu.s.low)
780 return 2;
781 return 1;
783 #endif
785 #if defined(L_fixunstfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
786 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
787 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
789 DItype
790 __fixunstfdi (TFtype a)
792 TFtype b;
793 UDItype v;
795 if (a < 0)
796 return 0;
798 /* Compute high word of result, as a flonum. */
799 b = (a / HIGH_WORD_COEFF);
800 /* Convert that to fixed (but not to DItype!),
801 and shift it into the high word. */
802 v = (USItype) b;
803 v <<= WORD_SIZE;
804 /* Remove high part from the TFtype, leaving the low part as flonum. */
805 a -= (TFtype)v;
806 /* Convert that to fixed (but not to DItype!) and add it in.
807 Sometimes A comes out negative. This is significant, since
808 A has more bits than a long int does. */
809 if (a < 0)
810 v -= (USItype) (- a);
811 else
812 v += (USItype) a;
813 return v;
815 #endif
817 #if defined(L_fixtfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
818 DItype
819 __fixtfdi (TFtype a)
821 if (a < 0)
822 return - __fixunstfdi (-a);
823 return __fixunstfdi (a);
825 #endif
827 #if defined(L_fixunsxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
828 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
829 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
831 DItype
832 __fixunsxfdi (XFtype a)
834 XFtype b;
835 UDItype v;
837 if (a < 0)
838 return 0;
840 /* Compute high word of result, as a flonum. */
841 b = (a / HIGH_WORD_COEFF);
842 /* Convert that to fixed (but not to DItype!),
843 and shift it into the high word. */
844 v = (USItype) b;
845 v <<= WORD_SIZE;
846 /* Remove high part from the XFtype, leaving the low part as flonum. */
847 a -= (XFtype)v;
848 /* Convert that to fixed (but not to DItype!) and add it in.
849 Sometimes A comes out negative. This is significant, since
850 A has more bits than a long int does. */
851 if (a < 0)
852 v -= (USItype) (- a);
853 else
854 v += (USItype) a;
855 return v;
857 #endif
859 #if defined(L_fixxfdi) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
860 DItype
861 __fixxfdi (XFtype a)
863 if (a < 0)
864 return - __fixunsxfdi (-a);
865 return __fixunsxfdi (a);
867 #endif
869 #ifdef L_fixunsdfdi
870 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
871 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
873 DItype
874 __fixunsdfdi (DFtype a)
876 DFtype b;
877 UDItype v;
879 if (a < 0)
880 return 0;
882 /* Compute high word of result, as a flonum. */
883 b = (a / HIGH_WORD_COEFF);
884 /* Convert that to fixed (but not to DItype!),
885 and shift it into the high word. */
886 v = (USItype) b;
887 v <<= WORD_SIZE;
888 /* Remove high part from the DFtype, leaving the low part as flonum. */
889 a -= (DFtype)v;
890 /* Convert that to fixed (but not to DItype!) and add it in.
891 Sometimes A comes out negative. This is significant, since
892 A has more bits than a long int does. */
893 if (a < 0)
894 v -= (USItype) (- a);
895 else
896 v += (USItype) a;
897 return v;
899 #endif
901 #ifdef L_fixdfdi
902 DItype
903 __fixdfdi (DFtype a)
905 if (a < 0)
906 return - __fixunsdfdi (-a);
907 return __fixunsdfdi (a);
909 #endif
911 #ifdef L_fixunssfdi
912 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
913 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
915 DItype
916 __fixunssfdi (SFtype original_a)
918 /* Convert the SFtype to a DFtype, because that is surely not going
919 to lose any bits. Some day someone else can write a faster version
920 that avoids converting to DFtype, and verify it really works right. */
921 DFtype a = original_a;
922 DFtype b;
923 UDItype v;
925 if (a < 0)
926 return 0;
928 /* Compute high word of result, as a flonum. */
929 b = (a / HIGH_WORD_COEFF);
930 /* Convert that to fixed (but not to DItype!),
931 and shift it into the high word. */
932 v = (USItype) b;
933 v <<= WORD_SIZE;
934 /* Remove high part from the DFtype, leaving the low part as flonum. */
935 a -= (DFtype)v;
936 /* Convert that to fixed (but not to DItype!) and add it in.
937 Sometimes A comes out negative. This is significant, since
938 A has more bits than a long int does. */
939 if (a < 0)
940 v -= (USItype) (- a);
941 else
942 v += (USItype) a;
943 return v;
945 #endif
947 #ifdef L_fixsfdi
948 DItype
949 __fixsfdi (SFtype a)
951 if (a < 0)
952 return - __fixunssfdi (-a);
953 return __fixunssfdi (a);
955 #endif
957 #if defined(L_floatdixf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96)
958 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
959 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
960 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
962 XFtype
963 __floatdixf (DItype u)
965 XFtype d;
967 d = (SItype) (u >> WORD_SIZE);
968 d *= HIGH_HALFWORD_COEFF;
969 d *= HIGH_HALFWORD_COEFF;
970 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
972 return d;
974 #endif
976 #if defined(L_floatditf) && (LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128)
977 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
978 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
979 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
981 TFtype
982 __floatditf (DItype u)
984 TFtype d;
986 d = (SItype) (u >> WORD_SIZE);
987 d *= HIGH_HALFWORD_COEFF;
988 d *= HIGH_HALFWORD_COEFF;
989 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
991 return d;
993 #endif
995 #ifdef L_floatdidf
996 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
997 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
998 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1000 DFtype
1001 __floatdidf (DItype u)
1003 DFtype d;
1005 d = (SItype) (u >> WORD_SIZE);
1006 d *= HIGH_HALFWORD_COEFF;
1007 d *= HIGH_HALFWORD_COEFF;
1008 d += (USItype) (u & (HIGH_WORD_COEFF - 1));
1010 return d;
1012 #endif
1014 #ifdef L_floatdisf
1015 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
1016 #define HIGH_HALFWORD_COEFF (((UDItype) 1) << (WORD_SIZE / 2))
1017 #define HIGH_WORD_COEFF (((UDItype) 1) << WORD_SIZE)
1018 #define DI_SIZE (sizeof (DItype) * BITS_PER_UNIT)
1020 /* Define codes for all the float formats that we know of. Note
1021 that this is copied from real.h. */
1023 #define UNKNOWN_FLOAT_FORMAT 0
1024 #define IEEE_FLOAT_FORMAT 1
1025 #define VAX_FLOAT_FORMAT 2
1026 #define IBM_FLOAT_FORMAT 3
1028 /* Default to IEEE float if not specified. Nearly all machines use it. */
1029 #ifndef HOST_FLOAT_FORMAT
1030 #define HOST_FLOAT_FORMAT IEEE_FLOAT_FORMAT
1031 #endif
1033 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1034 #define DF_SIZE 53
1035 #define SF_SIZE 24
1036 #endif
1038 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1039 #define DF_SIZE 56
1040 #define SF_SIZE 24
1041 #endif
1043 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
1044 #define DF_SIZE 56
1045 #define SF_SIZE 24
1046 #endif
1048 SFtype
1049 __floatdisf (DItype u)
1051 /* Do the calculation in DFmode
1052 so that we don't lose any of the precision of the high word
1053 while multiplying it. */
1054 DFtype f;
1056 /* Protect against double-rounding error.
1057 Represent any low-order bits, that might be truncated in DFmode,
1058 by a bit that won't be lost. The bit can go in anywhere below the
1059 rounding position of the SFmode. A fixed mask and bit position
1060 handles all usual configurations. It doesn't handle the case
1061 of 128-bit DImode, however. */
1062 if (DF_SIZE < DI_SIZE
1063 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1065 #define REP_BIT ((USItype) 1 << (DI_SIZE - DF_SIZE))
1066 if (! (- ((DItype) 1 << DF_SIZE) < u
1067 && u < ((DItype) 1 << DF_SIZE)))
1069 if ((USItype) u & (REP_BIT - 1))
1070 u |= REP_BIT;
1073 f = (SItype) (u >> WORD_SIZE);
1074 f *= HIGH_HALFWORD_COEFF;
1075 f *= HIGH_HALFWORD_COEFF;
1076 f += (USItype) (u & (HIGH_WORD_COEFF - 1));
1078 return (SFtype) f;
1080 #endif
1082 #if defined(L_fixunsxfsi) && LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 96
1083 /* Reenable the normal types, in case limits.h needs them. */
1084 #undef char
1085 #undef short
1086 #undef int
1087 #undef long
1088 #undef unsigned
1089 #undef float
1090 #undef double
1091 #undef MIN
1092 #undef MAX
1093 #include <limits.h>
1095 USItype
1096 __fixunsxfsi (XFtype a)
1098 if (a >= - (DFtype) LONG_MIN)
1099 return (SItype) (a + LONG_MIN) - LONG_MIN;
1100 return (SItype) a;
1102 #endif
1104 #ifdef L_fixunsdfsi
1105 /* Reenable the normal types, in case limits.h needs them. */
1106 #undef char
1107 #undef short
1108 #undef int
1109 #undef long
1110 #undef unsigned
1111 #undef float
1112 #undef double
1113 #undef MIN
1114 #undef MAX
1115 #include <limits.h>
1117 USItype
1118 __fixunsdfsi (DFtype a)
1120 if (a >= - (DFtype) LONG_MIN)
1121 return (SItype) (a + LONG_MIN) - LONG_MIN;
1122 return (SItype) a;
1124 #endif
1126 #ifdef L_fixunssfsi
1127 /* Reenable the normal types, in case limits.h needs them. */
1128 #undef char
1129 #undef short
1130 #undef int
1131 #undef long
1132 #undef unsigned
1133 #undef float
1134 #undef double
1135 #undef MIN
1136 #undef MAX
1137 #include <limits.h>
1139 USItype
1140 __fixunssfsi (SFtype a)
1142 if (a >= - (SFtype) LONG_MIN)
1143 return (SItype) (a + LONG_MIN) - LONG_MIN;
1144 return (SItype) a;
1146 #endif
1148 /* From here on down, the routines use normal data types. */
1150 #define SItype bogus_type
1151 #define USItype bogus_type
1152 #define DItype bogus_type
1153 #define UDItype bogus_type
1154 #define SFtype bogus_type
1155 #define DFtype bogus_type
1157 #undef char
1158 #undef short
1159 #undef int
1160 #undef long
1161 #undef unsigned
1162 #undef float
1163 #undef double
1165 #ifdef L__gcc_bcmp
1167 /* Like bcmp except the sign is meaningful.
1168 Result is negative if S1 is less than S2,
1169 positive if S1 is greater, 0 if S1 and S2 are equal. */
1172 __gcc_bcmp (unsigned char *s1, unsigned char *s2, size_t size)
1174 while (size > 0)
1176 unsigned char c1 = *s1++, c2 = *s2++;
1177 if (c1 != c2)
1178 return c1 - c2;
1179 size--;
1181 return 0;
1184 #endif
1185 \f\f
1186 #ifdef L__dummy
1187 void
1188 __dummy () {}
1189 #endif
1191 #ifdef L_varargs
1192 #ifdef __i860__
1193 #if defined(__svr4__) || defined(__alliant__)
1194 asm (" .text");
1195 asm (" .align 4");
1197 /* The Alliant needs the added underscore. */
1198 asm (".globl __builtin_saveregs");
1199 asm ("__builtin_saveregs:");
1200 asm (".globl ___builtin_saveregs");
1201 asm ("___builtin_saveregs:");
1203 asm (" andnot 0x0f,%sp,%sp"); /* round down to 16-byte boundary */
1204 asm (" adds -96,%sp,%sp"); /* allocate stack space for reg save
1205 area and also for a new va_list
1206 structure */
1207 /* Save all argument registers in the arg reg save area. The
1208 arg reg save area must have the following layout (according
1209 to the svr4 ABI):
1211 struct {
1212 union {
1213 float freg[8];
1214 double dreg[4];
1215 } float_regs;
1216 long ireg[12];
1220 asm (" fst.q %f8, 0(%sp)"); /* save floating regs (f8-f15) */
1221 asm (" fst.q %f12,16(%sp)");
1223 asm (" st.l %r16,32(%sp)"); /* save integer regs (r16-r27) */
1224 asm (" st.l %r17,36(%sp)");
1225 asm (" st.l %r18,40(%sp)");
1226 asm (" st.l %r19,44(%sp)");
1227 asm (" st.l %r20,48(%sp)");
1228 asm (" st.l %r21,52(%sp)");
1229 asm (" st.l %r22,56(%sp)");
1230 asm (" st.l %r23,60(%sp)");
1231 asm (" st.l %r24,64(%sp)");
1232 asm (" st.l %r25,68(%sp)");
1233 asm (" st.l %r26,72(%sp)");
1234 asm (" st.l %r27,76(%sp)");
1236 asm (" adds 80,%sp,%r16"); /* compute the address of the new
1237 va_list structure. Put in into
1238 r16 so that it will be returned
1239 to the caller. */
1241 /* Initialize all fields of the new va_list structure. This
1242 structure looks like:
1244 typedef struct {
1245 unsigned long ireg_used;
1246 unsigned long freg_used;
1247 long *reg_base;
1248 long *mem_ptr;
1249 } va_list;
1252 asm (" st.l %r0, 0(%r16)"); /* nfixed */
1253 asm (" st.l %r0, 4(%r16)"); /* nfloating */
1254 asm (" st.l %sp, 8(%r16)"); /* __va_ctl points to __va_struct. */
1255 asm (" bri %r1"); /* delayed return */
1256 asm (" st.l %r28,12(%r16)"); /* pointer to overflow args */
1258 #else /* not __svr4__ */
1259 #if defined(__PARAGON__)
1261 * we'll use SVR4-ish varargs but need SVR3.2 assembler syntax,
1262 * and we stand a better chance of hooking into libraries
1263 * compiled by PGI. [andyp@ssd.intel.com]
1265 asm (" .text");
1266 asm (" .align 4");
1267 asm (".globl __builtin_saveregs");
1268 asm ("__builtin_saveregs:");
1269 asm (".globl ___builtin_saveregs");
1270 asm ("___builtin_saveregs:");
1272 asm (" andnot 0x0f,sp,sp"); /* round down to 16-byte boundary */
1273 asm (" adds -96,sp,sp"); /* allocate stack space for reg save
1274 area and also for a new va_list
1275 structure */
1276 /* Save all argument registers in the arg reg save area. The
1277 arg reg save area must have the following layout (according
1278 to the svr4 ABI):
1280 struct {
1281 union {
1282 float freg[8];
1283 double dreg[4];
1284 } float_regs;
1285 long ireg[12];
1289 asm (" fst.q f8, 0(sp)");
1290 asm (" fst.q f12,16(sp)");
1291 asm (" st.l r16,32(sp)");
1292 asm (" st.l r17,36(sp)");
1293 asm (" st.l r18,40(sp)");
1294 asm (" st.l r19,44(sp)");
1295 asm (" st.l r20,48(sp)");
1296 asm (" st.l r21,52(sp)");
1297 asm (" st.l r22,56(sp)");
1298 asm (" st.l r23,60(sp)");
1299 asm (" st.l r24,64(sp)");
1300 asm (" st.l r25,68(sp)");
1301 asm (" st.l r26,72(sp)");
1302 asm (" st.l r27,76(sp)");
1304 asm (" adds 80,sp,r16"); /* compute the address of the new
1305 va_list structure. Put in into
1306 r16 so that it will be returned
1307 to the caller. */
1309 /* Initialize all fields of the new va_list structure. This
1310 structure looks like:
1312 typedef struct {
1313 unsigned long ireg_used;
1314 unsigned long freg_used;
1315 long *reg_base;
1316 long *mem_ptr;
1317 } va_list;
1320 asm (" st.l r0, 0(r16)"); /* nfixed */
1321 asm (" st.l r0, 4(r16)"); /* nfloating */
1322 asm (" st.l sp, 8(r16)"); /* __va_ctl points to __va_struct. */
1323 asm (" bri r1"); /* delayed return */
1324 asm (" st.l r28,12(r16)"); /* pointer to overflow args */
1325 #else /* not __PARAGON__ */
1326 asm (" .text");
1327 asm (" .align 4");
1329 asm (".globl ___builtin_saveregs");
1330 asm ("___builtin_saveregs:");
1331 asm (" mov sp,r30");
1332 asm (" andnot 0x0f,sp,sp");
1333 asm (" adds -96,sp,sp"); /* allocate sufficient space on the stack */
1335 /* Fill in the __va_struct. */
1336 asm (" st.l r16, 0(sp)"); /* save integer regs (r16-r27) */
1337 asm (" st.l r17, 4(sp)"); /* int fixed[12] */
1338 asm (" st.l r18, 8(sp)");
1339 asm (" st.l r19,12(sp)");
1340 asm (" st.l r20,16(sp)");
1341 asm (" st.l r21,20(sp)");
1342 asm (" st.l r22,24(sp)");
1343 asm (" st.l r23,28(sp)");
1344 asm (" st.l r24,32(sp)");
1345 asm (" st.l r25,36(sp)");
1346 asm (" st.l r26,40(sp)");
1347 asm (" st.l r27,44(sp)");
1349 asm (" fst.q f8, 48(sp)"); /* save floating regs (f8-f15) */
1350 asm (" fst.q f12,64(sp)"); /* int floating[8] */
1352 /* Fill in the __va_ctl. */
1353 asm (" st.l sp, 80(sp)"); /* __va_ctl points to __va_struct. */
1354 asm (" st.l r28,84(sp)"); /* pointer to more args */
1355 asm (" st.l r0, 88(sp)"); /* nfixed */
1356 asm (" st.l r0, 92(sp)"); /* nfloating */
1358 asm (" adds 80,sp,r16"); /* return address of the __va_ctl. */
1359 asm (" bri r1");
1360 asm (" mov r30,sp");
1361 /* recover stack and pass address to start
1362 of data. */
1363 #endif /* not __PARAGON__ */
1364 #endif /* not __svr4__ */
1365 #else /* not __i860__ */
1366 #ifdef __sparc__
1367 asm (".global __builtin_saveregs");
1368 asm ("__builtin_saveregs:");
1369 asm (".global ___builtin_saveregs");
1370 asm ("___builtin_saveregs:");
1371 #ifdef NEED_PROC_COMMAND
1372 asm (".proc 020");
1373 #endif
1374 asm ("st %i0,[%fp+68]");
1375 asm ("st %i1,[%fp+72]");
1376 asm ("st %i2,[%fp+76]");
1377 asm ("st %i3,[%fp+80]");
1378 asm ("st %i4,[%fp+84]");
1379 asm ("retl");
1380 asm ("st %i5,[%fp+88]");
1381 #ifdef NEED_TYPE_COMMAND
1382 asm (".type __builtin_saveregs,#function");
1383 asm (".size __builtin_saveregs,.-__builtin_saveregs");
1384 #endif
1385 #else /* not __sparc__ */
1386 #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
1388 asm (" .text");
1389 #ifdef __mips16
1390 asm (" .set nomips16");
1391 #endif
1392 asm (" .ent __builtin_saveregs");
1393 asm (" .globl __builtin_saveregs");
1394 asm ("__builtin_saveregs:");
1395 asm (" sw $4,0($30)");
1396 asm (" sw $5,4($30)");
1397 asm (" sw $6,8($30)");
1398 asm (" sw $7,12($30)");
1399 asm (" j $31");
1400 asm (" .end __builtin_saveregs");
1401 #else /* not __mips__, etc. */
1403 void *
1404 __builtin_saveregs ()
1406 abort ();
1409 #endif /* not __mips__ */
1410 #endif /* not __sparc__ */
1411 #endif /* not __i860__ */
1412 #endif
1414 #ifdef L_eprintf
1415 #ifndef inhibit_libc
1417 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1418 #include <stdio.h>
1419 /* This is used by the `assert' macro. */
1420 extern void __eprintf (const char *, const char *, unsigned int, const char *)
1421 __attribute__ ((__noreturn__));
1423 void
1424 __eprintf (const char *string, const char *expression,
1425 unsigned int line, const char *filename)
1427 fprintf (stderr, string, expression, line, filename);
1428 fflush (stderr);
1429 abort ();
1432 #endif
1433 #endif
1435 #ifdef L_bb
1437 /* Structure emitted by -a */
1438 struct bb
1440 long zero_word;
1441 const char *filename;
1442 long *counts;
1443 long ncounts;
1444 struct bb *next;
1445 const unsigned long *addresses;
1447 /* Older GCC's did not emit these fields. */
1448 long nwords;
1449 const char **functions;
1450 const long *line_nums;
1451 const char **filenames;
1452 char *flags;
1455 #ifdef BLOCK_PROFILER_CODE
1456 BLOCK_PROFILER_CODE
1457 #else
1458 #ifndef inhibit_libc
1460 /* Simple minded basic block profiling output dumper for
1461 systems that don't provide tcov support. At present,
1462 it requires atexit and stdio. */
1464 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1465 #include <stdio.h>
1466 char *ctime ();
1468 #include "gbl-ctors.h"
1469 #include "gcov-io.h"
1470 #include <string.h>
1472 static struct bb *bb_head;
1474 static int num_digits (long value, int base) __attribute__ ((const));
1476 /* Return the number of digits needed to print a value */
1477 /* __inline__ */ static int num_digits (long value, int base)
1479 int minus = (value < 0 && base != 16);
1480 unsigned long v = (minus) ? -value : value;
1481 int ret = minus;
1485 v /= base;
1486 ret++;
1488 while (v);
1490 return ret;
1493 void
1494 __bb_exit_func (void)
1496 FILE *da_file, *file;
1497 long time_value;
1498 int i;
1500 if (bb_head == 0)
1501 return;
1503 i = strlen (bb_head->filename) - 3;
1505 if (!strcmp (bb_head->filename+i, ".da"))
1507 /* Must be -fprofile-arcs not -a.
1508 Dump data in a form that gcov expects. */
1510 struct bb *ptr;
1512 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1514 /* If the file exists, and the number of counts in it is the same,
1515 then merge them in. */
1517 if ((da_file = fopen (ptr->filename, "r")) != 0)
1519 long n_counts = 0;
1521 if (__read_long (&n_counts, da_file, 8) != 0)
1523 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1524 ptr->filename);
1525 continue;
1528 if (n_counts == ptr->ncounts)
1530 int i;
1532 for (i = 0; i < n_counts; i++)
1534 long v = 0;
1536 if (__read_long (&v, da_file, 8) != 0)
1538 fprintf (stderr, "arc profiling: Can't read output file %s.\n",
1539 ptr->filename);
1540 break;
1542 ptr->counts[i] += v;
1546 if (fclose (da_file) == EOF)
1547 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1548 ptr->filename);
1550 if ((da_file = fopen (ptr->filename, "w")) == 0)
1552 fprintf (stderr, "arc profiling: Can't open output file %s.\n",
1553 ptr->filename);
1554 continue;
1557 /* ??? Should first write a header to the file. Preferably, a 4 byte
1558 magic number, 4 bytes containing the time the program was
1559 compiled, 4 bytes containing the last modification time of the
1560 source file, and 4 bytes indicating the compiler options used.
1562 That way we can easily verify that the proper source/executable/
1563 data file combination is being used from gcov. */
1565 if (__write_long (ptr->ncounts, da_file, 8) != 0)
1568 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1569 ptr->filename);
1571 else
1573 int j;
1574 long *count_ptr = ptr->counts;
1575 int ret = 0;
1576 for (j = ptr->ncounts; j > 0; j--)
1578 if (__write_long (*count_ptr, da_file, 8) != 0)
1580 ret=1;
1581 break;
1583 count_ptr++;
1585 if (ret)
1586 fprintf (stderr, "arc profiling: Error writing output file %s.\n",
1587 ptr->filename);
1590 if (fclose (da_file) == EOF)
1591 fprintf (stderr, "arc profiling: Error closing output file %s.\n",
1592 ptr->filename);
1595 return;
1598 /* Must be basic block profiling. Emit a human readable output file. */
1600 file = fopen ("bb.out", "a");
1602 if (!file)
1603 perror ("bb.out");
1605 else
1607 struct bb *ptr;
1609 /* This is somewhat type incorrect, but it avoids worrying about
1610 exactly where time.h is included from. It should be ok unless
1611 a void * differs from other pointer formats, or if sizeof (long)
1612 is < sizeof (time_t). It would be nice if we could assume the
1613 use of rationale standards here. */
1615 time ((void *) &time_value);
1616 fprintf (file, "Basic block profiling finished on %s\n", ctime ((void *) &time_value));
1618 /* We check the length field explicitly in order to allow compatibility
1619 with older GCC's which did not provide it. */
1621 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1623 int i;
1624 int func_p = (ptr->nwords >= sizeof (struct bb)
1625 && ptr->nwords <= 1000
1626 && ptr->functions);
1627 int line_p = (func_p && ptr->line_nums);
1628 int file_p = (func_p && ptr->filenames);
1629 int addr_p = (ptr->addresses != 0);
1630 long ncounts = ptr->ncounts;
1631 long cnt_max = 0;
1632 long line_max = 0;
1633 long addr_max = 0;
1634 int file_len = 0;
1635 int func_len = 0;
1636 int blk_len = num_digits (ncounts, 10);
1637 int cnt_len;
1638 int line_len;
1639 int addr_len;
1641 fprintf (file, "File %s, %ld basic blocks \n\n",
1642 ptr->filename, ncounts);
1644 /* Get max values for each field. */
1645 for (i = 0; i < ncounts; i++)
1647 const char *p;
1648 int len;
1650 if (cnt_max < ptr->counts[i])
1651 cnt_max = ptr->counts[i];
1653 if (addr_p && addr_max < ptr->addresses[i])
1654 addr_max = ptr->addresses[i];
1656 if (line_p && line_max < ptr->line_nums[i])
1657 line_max = ptr->line_nums[i];
1659 if (func_p)
1661 p = (ptr->functions[i]) ? (ptr->functions[i]) : "<none>";
1662 len = strlen (p);
1663 if (func_len < len)
1664 func_len = len;
1667 if (file_p)
1669 p = (ptr->filenames[i]) ? (ptr->filenames[i]) : "<none>";
1670 len = strlen (p);
1671 if (file_len < len)
1672 file_len = len;
1676 addr_len = num_digits (addr_max, 16);
1677 cnt_len = num_digits (cnt_max, 10);
1678 line_len = num_digits (line_max, 10);
1680 /* Now print out the basic block information. */
1681 for (i = 0; i < ncounts; i++)
1683 fprintf (file,
1684 " Block #%*d: executed %*ld time(s)",
1685 blk_len, i+1,
1686 cnt_len, ptr->counts[i]);
1688 if (addr_p)
1689 fprintf (file, " address= 0x%.*lx", addr_len,
1690 ptr->addresses[i]);
1692 if (func_p)
1693 fprintf (file, " function= %-*s", func_len,
1694 (ptr->functions[i]) ? ptr->functions[i] : "<none>");
1696 if (line_p)
1697 fprintf (file, " line= %*ld", line_len, ptr->line_nums[i]);
1699 if (file_p)
1700 fprintf (file, " file= %s",
1701 (ptr->filenames[i]) ? ptr->filenames[i] : "<none>");
1703 fprintf (file, "\n");
1706 fprintf (file, "\n");
1707 fflush (file);
1710 fprintf (file, "\n\n");
1711 fclose (file);
1715 void
1716 __bb_init_func (struct bb *blocks)
1718 /* User is supposed to check whether the first word is non-0,
1719 but just in case.... */
1721 if (blocks->zero_word)
1722 return;
1724 /* Initialize destructor. */
1725 if (!bb_head)
1726 atexit (__bb_exit_func);
1728 /* Set up linked list. */
1729 blocks->zero_word = 1;
1730 blocks->next = bb_head;
1731 bb_head = blocks;
1734 #ifndef MACHINE_STATE_SAVE
1735 #define MACHINE_STATE_SAVE(ID)
1736 #endif
1737 #ifndef MACHINE_STATE_RESTORE
1738 #define MACHINE_STATE_RESTORE(ID)
1739 #endif
1741 /* Number of buckets in hashtable of basic block addresses. */
1743 #define BB_BUCKETS 311
1745 /* Maximum length of string in file bb.in. */
1747 #define BBINBUFSIZE 500
1749 /* BBINBUFSIZE-1 with double quotes. We could use #BBINBUFSIZE or
1750 "BBINBUFSIZE" but want to avoid trouble with preprocessors. */
1752 #define BBINBUFSIZESTR "499"
1754 struct bb_edge
1756 struct bb_edge *next;
1757 unsigned long src_addr;
1758 unsigned long dst_addr;
1759 unsigned long count;
1762 enum bb_func_mode
1764 TRACE_KEEP = 0, TRACE_ON = 1, TRACE_OFF = 2
1767 struct bb_func
1769 struct bb_func *next;
1770 char *funcname;
1771 char *filename;
1772 enum bb_func_mode mode;
1775 /* This is the connection to the outside world.
1776 The BLOCK_PROFILER macro must set __bb.blocks
1777 and __bb.blockno. */
1779 struct {
1780 unsigned long blockno;
1781 struct bb *blocks;
1782 } __bb;
1784 /* Vars to store addrs of source and destination basic blocks
1785 of a jump. */
1787 static unsigned long bb_src = 0;
1788 static unsigned long bb_dst = 0;
1790 static FILE *bb_tracefile = (FILE *) 0;
1791 static struct bb_edge **bb_hashbuckets = (struct bb_edge **) 0;
1792 static struct bb_func *bb_func_head = (struct bb_func *) 0;
1793 static unsigned long bb_callcount = 0;
1794 static int bb_mode = 0;
1796 static unsigned long *bb_stack = (unsigned long *) 0;
1797 static size_t bb_stacksize = 0;
1799 static int reported = 0;
1801 /* Trace modes:
1802 Always : Print execution frequencies of basic blocks
1803 to file bb.out.
1804 bb_mode & 1 != 0 : Dump trace of basic blocks to file bbtrace[.gz]
1805 bb_mode & 2 != 0 : Print jump frequencies to file bb.out.
1806 bb_mode & 4 != 0 : Cut call instructions from basic block flow.
1807 bb_mode & 8 != 0 : Insert return instructions in basic block flow.
1810 #ifdef HAVE_POPEN
1812 /*#include <sys/types.h>*/
1813 #include <sys/stat.h>
1814 /*#include <malloc.h>*/
1816 /* Commands executed by gopen. */
1818 #define GOPENDECOMPRESS "gzip -cd "
1819 #define GOPENCOMPRESS "gzip -c >"
1821 /* Like fopen but pipes through gzip. mode may only be "r" or "w".
1822 If it does not compile, simply replace gopen by fopen and delete
1823 '.gz' from any first parameter to gopen. */
1825 static FILE *
1826 gopen (char *fn, char *mode)
1828 int use_gzip;
1829 char *p;
1831 if (mode[1])
1832 return (FILE *) 0;
1834 if (mode[0] != 'r' && mode[0] != 'w')
1835 return (FILE *) 0;
1837 p = fn + strlen (fn)-1;
1838 use_gzip = ((p[-1] == '.' && (p[0] == 'Z' || p[0] == 'z'))
1839 || (p[-2] == '.' && p[-1] == 'g' && p[0] == 'z'));
1841 if (use_gzip)
1843 if (mode[0]=='r')
1845 FILE *f;
1846 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1847 + sizeof (GOPENDECOMPRESS));
1848 strcpy (s, GOPENDECOMPRESS);
1849 strcpy (s + (sizeof (GOPENDECOMPRESS)-1), fn);
1850 f = popen (s, mode);
1851 free (s);
1852 return f;
1855 else
1857 FILE *f;
1858 char *s = (char *) malloc (sizeof (char) * strlen (fn)
1859 + sizeof (GOPENCOMPRESS));
1860 strcpy (s, GOPENCOMPRESS);
1861 strcpy (s + (sizeof (GOPENCOMPRESS)-1), fn);
1862 if (!(f = popen (s, mode)))
1863 f = fopen (s, mode);
1864 free (s);
1865 return f;
1869 else
1870 return fopen (fn, mode);
1873 static int
1874 gclose (FILE *f)
1876 struct stat buf;
1878 if (f != 0)
1880 if (!fstat (fileno (f), &buf) && S_ISFIFO (buf.st_mode))
1881 return pclose (f);
1883 return fclose (f);
1885 return 0;
1888 #endif /* HAVE_POPEN */
1890 /* Called once per program. */
1892 static void
1893 __bb_exit_trace_func ()
1895 FILE *file = fopen ("bb.out", "a");
1896 struct bb_func *f;
1897 struct bb *b;
1899 if (!file)
1900 perror ("bb.out");
1902 if (bb_mode & 1)
1904 if (!bb_tracefile)
1905 perror ("bbtrace");
1906 else
1907 #ifdef HAVE_POPEN
1908 gclose (bb_tracefile);
1909 #else
1910 fclose (bb_tracefile);
1911 #endif /* HAVE_POPEN */
1914 /* Check functions in `bb.in'. */
1916 if (file)
1918 long time_value;
1919 const struct bb_func *p;
1920 int printed_something = 0;
1921 struct bb *ptr;
1922 long blk;
1924 /* This is somewhat type incorrect. */
1925 time ((void *) &time_value);
1927 for (p = bb_func_head; p != (struct bb_func *) 0; p = p->next)
1929 for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
1931 if (!ptr->filename || (p->filename != (char *) 0 && strcmp (p->filename, ptr->filename)))
1932 continue;
1933 for (blk = 0; blk < ptr->ncounts; blk++)
1935 if (!strcmp (p->funcname, ptr->functions[blk]))
1936 goto found;
1940 if (!printed_something)
1942 fprintf (file, "Functions in `bb.in' not executed during basic block profiling on %s\n", ctime ((void *) &time_value));
1943 printed_something = 1;
1946 fprintf (file, "\tFunction %s", p->funcname);
1947 if (p->filename)
1948 fprintf (file, " of file %s", p->filename);
1949 fprintf (file, "\n" );
1951 found: ;
1954 if (printed_something)
1955 fprintf (file, "\n");
1959 if (bb_mode & 2)
1961 if (!bb_hashbuckets)
1963 if (!reported)
1965 fprintf (stderr, "Profiler: out of memory\n");
1966 reported = 1;
1968 return;
1971 else if (file)
1973 long time_value;
1974 int i;
1975 unsigned long addr_max = 0;
1976 unsigned long cnt_max = 0;
1977 int cnt_len;
1978 int addr_len;
1980 /* This is somewhat type incorrect, but it avoids worrying about
1981 exactly where time.h is included from. It should be ok unless
1982 a void * differs from other pointer formats, or if sizeof (long)
1983 is < sizeof (time_t). It would be nice if we could assume the
1984 use of rationale standards here. */
1986 time ((void *) &time_value);
1987 fprintf (file, "Basic block jump tracing");
1989 switch (bb_mode & 12)
1991 case 0:
1992 fprintf (file, " (with call)");
1993 break;
1995 case 4:
1996 /* Print nothing. */
1997 break;
1999 case 8:
2000 fprintf (file, " (with call & ret)");
2001 break;
2003 case 12:
2004 fprintf (file, " (with ret)");
2005 break;
2008 fprintf (file, " finished on %s\n", ctime ((void *) &time_value));
2010 for (i = 0; i < BB_BUCKETS; i++)
2012 struct bb_edge *bucket = bb_hashbuckets[i];
2013 for ( ; bucket; bucket = bucket->next )
2015 if (addr_max < bucket->src_addr)
2016 addr_max = bucket->src_addr;
2017 if (addr_max < bucket->dst_addr)
2018 addr_max = bucket->dst_addr;
2019 if (cnt_max < bucket->count)
2020 cnt_max = bucket->count;
2023 addr_len = num_digits (addr_max, 16);
2024 cnt_len = num_digits (cnt_max, 10);
2026 for ( i = 0; i < BB_BUCKETS; i++)
2028 struct bb_edge *bucket = bb_hashbuckets[i];
2029 for ( ; bucket; bucket = bucket->next )
2031 fprintf (file, "Jump from block 0x%.*lx to "
2032 "block 0x%.*lx executed %*lu time(s)\n",
2033 addr_len, bucket->src_addr,
2034 addr_len, bucket->dst_addr,
2035 cnt_len, bucket->count);
2039 fprintf (file, "\n");
2044 if (file)
2045 fclose (file);
2047 /* Free allocated memory. */
2049 f = bb_func_head;
2050 while (f)
2052 struct bb_func *old = f;
2054 f = f->next;
2055 if (old->funcname) free (old->funcname);
2056 if (old->filename) free (old->filename);
2057 free (old);
2060 if (bb_stack)
2061 free (bb_stack);
2063 if (bb_hashbuckets)
2065 int i;
2067 for (i = 0; i < BB_BUCKETS; i++)
2069 struct bb_edge *old, *bucket = bb_hashbuckets[i];
2071 while (bucket)
2073 old = bucket;
2074 bucket = bucket->next;
2075 free (old);
2078 free (bb_hashbuckets);
2081 for (b = bb_head; b; b = b->next)
2082 if (b->flags) free (b->flags);
2085 /* Called once per program. */
2087 static void
2088 __bb_init_prg ()
2090 FILE *file;
2091 char buf[BBINBUFSIZE];
2092 const char *p;
2093 const char *pos;
2094 enum bb_func_mode m;
2095 int i;
2097 /* Initialize destructor. */
2098 atexit (__bb_exit_func);
2100 if (!(file = fopen ("bb.in", "r")))
2101 return;
2103 while(fscanf (file, " %" BBINBUFSIZESTR "s ", buf) != EOF)
2105 p = buf;
2106 if (*p == '-')
2108 m = TRACE_OFF;
2109 p++;
2111 else
2113 m = TRACE_ON;
2115 if (!strcmp (p, "__bb_trace__"))
2116 bb_mode |= 1;
2117 else if (!strcmp (p, "__bb_jumps__"))
2118 bb_mode |= 2;
2119 else if (!strcmp (p, "__bb_hidecall__"))
2120 bb_mode |= 4;
2121 else if (!strcmp (p, "__bb_showret__"))
2122 bb_mode |= 8;
2123 else
2125 struct bb_func *f = (struct bb_func *) malloc (sizeof (struct bb_func));
2126 if (f)
2128 unsigned long l;
2129 f->next = bb_func_head;
2130 if ((pos = strchr (p, ':')))
2132 if (!(f->funcname = (char *) malloc (strlen (pos+1)+1)))
2133 continue;
2134 strcpy (f->funcname, pos+1);
2135 l = pos-p;
2136 if ((f->filename = (char *) malloc (l+1)))
2138 strncpy (f->filename, p, l);
2139 f->filename[l] = '\0';
2141 else
2142 f->filename = (char *) 0;
2144 else
2146 if (!(f->funcname = (char *) malloc (strlen (p)+1)))
2147 continue;
2148 strcpy (f->funcname, p);
2149 f->filename = (char *) 0;
2151 f->mode = m;
2152 bb_func_head = f;
2156 fclose (file);
2158 #ifdef HAVE_POPEN
2160 if (bb_mode & 1)
2161 bb_tracefile = gopen ("bbtrace.gz", "w");
2163 #else
2165 if (bb_mode & 1)
2166 bb_tracefile = fopen ("bbtrace", "w");
2168 #endif /* HAVE_POPEN */
2170 if (bb_mode & 2)
2172 bb_hashbuckets = (struct bb_edge **)
2173 malloc (BB_BUCKETS * sizeof (struct bb_edge *));
2174 if (bb_hashbuckets)
2175 /* Use a loop here rather than calling bzero to avoid having to
2176 conditionalize its existance. */
2177 for (i = 0; i < BB_BUCKETS; i++)
2178 bb_hashbuckets[i] = 0;
2181 if (bb_mode & 12)
2183 bb_stacksize = 10;
2184 bb_stack = (unsigned long *) malloc (bb_stacksize * sizeof (*bb_stack));
2187 /* Initialize destructor. */
2188 atexit (__bb_exit_trace_func);
2191 /* Called upon entering a basic block. */
2193 void
2194 __bb_trace_func ()
2196 struct bb_edge *bucket;
2198 MACHINE_STATE_SAVE("1")
2200 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2201 goto skip;
2203 bb_dst = __bb.blocks->addresses[__bb.blockno];
2204 __bb.blocks->counts[__bb.blockno]++;
2206 if (bb_tracefile)
2208 fwrite (&bb_dst, sizeof (unsigned long), 1, bb_tracefile);
2211 if (bb_hashbuckets)
2213 struct bb_edge **startbucket, **oldnext;
2215 oldnext = startbucket
2216 = & bb_hashbuckets[ (((int) bb_src*8) ^ (int) bb_dst) % BB_BUCKETS ];
2217 bucket = *startbucket;
2219 for (bucket = *startbucket; bucket;
2220 oldnext = &(bucket->next), bucket = *oldnext)
2222 if (bucket->src_addr == bb_src
2223 && bucket->dst_addr == bb_dst)
2225 bucket->count++;
2226 *oldnext = bucket->next;
2227 bucket->next = *startbucket;
2228 *startbucket = bucket;
2229 goto ret;
2233 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2235 if (!bucket)
2237 if (!reported)
2239 fprintf (stderr, "Profiler: out of memory\n");
2240 reported = 1;
2244 else
2246 bucket->src_addr = bb_src;
2247 bucket->dst_addr = bb_dst;
2248 bucket->next = *startbucket;
2249 *startbucket = bucket;
2250 bucket->count = 1;
2254 ret:
2255 bb_src = bb_dst;
2257 skip:
2260 MACHINE_STATE_RESTORE("1")
2264 /* Called when returning from a function and `__bb_showret__' is set. */
2266 static void
2267 __bb_trace_func_ret ()
2269 struct bb_edge *bucket;
2271 if (!bb_callcount || (__bb.blocks->flags && (__bb.blocks->flags[__bb.blockno] & TRACE_OFF)))
2272 goto skip;
2274 if (bb_hashbuckets)
2276 struct bb_edge **startbucket, **oldnext;
2278 oldnext = startbucket
2279 = & bb_hashbuckets[ (((int) bb_dst * 8) ^ (int) bb_src) % BB_BUCKETS ];
2280 bucket = *startbucket;
2282 for (bucket = *startbucket; bucket;
2283 oldnext = &(bucket->next), bucket = *oldnext)
2285 if (bucket->src_addr == bb_dst
2286 && bucket->dst_addr == bb_src)
2288 bucket->count++;
2289 *oldnext = bucket->next;
2290 bucket->next = *startbucket;
2291 *startbucket = bucket;
2292 goto ret;
2296 bucket = (struct bb_edge *) malloc (sizeof (struct bb_edge));
2298 if (!bucket)
2300 if (!reported)
2302 fprintf (stderr, "Profiler: out of memory\n");
2303 reported = 1;
2307 else
2309 bucket->src_addr = bb_dst;
2310 bucket->dst_addr = bb_src;
2311 bucket->next = *startbucket;
2312 *startbucket = bucket;
2313 bucket->count = 1;
2317 ret:
2318 bb_dst = bb_src;
2320 skip:
2325 /* Called upon entering the first function of a file. */
2327 static void
2328 __bb_init_file (struct bb *blocks)
2331 const struct bb_func *p;
2332 long blk, ncounts = blocks->ncounts;
2333 const char **functions = blocks->functions;
2335 /* Set up linked list. */
2336 blocks->zero_word = 1;
2337 blocks->next = bb_head;
2338 bb_head = blocks;
2340 blocks->flags = 0;
2341 if (!bb_func_head
2342 || !(blocks->flags = (char *) malloc (sizeof (char) * blocks->ncounts)))
2343 return;
2345 for (blk = 0; blk < ncounts; blk++)
2346 blocks->flags[blk] = 0;
2348 for (blk = 0; blk < ncounts; blk++)
2350 for (p = bb_func_head; p; p = p->next)
2352 if (!strcmp (p->funcname, functions[blk])
2353 && (!p->filename || !strcmp (p->filename, blocks->filename)))
2355 blocks->flags[blk] |= p->mode;
2362 /* Called when exiting from a function. */
2364 void
2365 __bb_trace_ret ()
2368 MACHINE_STATE_SAVE("2")
2370 if (bb_callcount)
2372 if ((bb_mode & 12) && bb_stacksize > bb_callcount)
2374 bb_src = bb_stack[bb_callcount];
2375 if (bb_mode & 8)
2376 __bb_trace_func_ret ();
2379 bb_callcount -= 1;
2382 MACHINE_STATE_RESTORE("2")
2386 /* Called when entering a function. */
2388 void
2389 __bb_init_trace_func (struct bb *blocks, unsigned long blockno)
2391 static int trace_init = 0;
2393 MACHINE_STATE_SAVE("3")
2395 if (!blocks->zero_word)
2397 if (!trace_init)
2399 trace_init = 1;
2400 __bb_init_prg ();
2402 __bb_init_file (blocks);
2405 if (bb_callcount)
2408 bb_callcount += 1;
2410 if (bb_mode & 12)
2412 if (bb_callcount >= bb_stacksize)
2414 size_t newsize = bb_callcount + 100;
2416 bb_stack = (unsigned long *) realloc (bb_stack, newsize);
2417 if (! bb_stack)
2419 if (!reported)
2421 fprintf (stderr, "Profiler: out of memory\n");
2422 reported = 1;
2424 bb_stacksize = 0;
2425 goto stack_overflow;
2427 bb_stacksize = newsize;
2429 bb_stack[bb_callcount] = bb_src;
2431 if (bb_mode & 4)
2432 bb_src = 0;
2436 stack_overflow:;
2440 else if (blocks->flags && (blocks->flags[blockno] & TRACE_ON))
2442 bb_callcount = 1;
2443 bb_src = 0;
2445 if (bb_stack)
2446 bb_stack[bb_callcount] = bb_src;
2449 MACHINE_STATE_RESTORE("3")
2452 #endif /* not inhibit_libc */
2453 #endif /* not BLOCK_PROFILER_CODE */
2454 #endif /* L_bb */
2456 #ifdef L_shtab
2457 unsigned int __shtab[] = {
2458 0x00000001, 0x00000002, 0x00000004, 0x00000008,
2459 0x00000010, 0x00000020, 0x00000040, 0x00000080,
2460 0x00000100, 0x00000200, 0x00000400, 0x00000800,
2461 0x00001000, 0x00002000, 0x00004000, 0x00008000,
2462 0x00010000, 0x00020000, 0x00040000, 0x00080000,
2463 0x00100000, 0x00200000, 0x00400000, 0x00800000,
2464 0x01000000, 0x02000000, 0x04000000, 0x08000000,
2465 0x10000000, 0x20000000, 0x40000000, 0x80000000
2467 #endif
2469 #ifdef L_clear_cache
2470 /* Clear part of an instruction cache. */
2472 #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
2474 void
2475 __clear_cache (char *beg __attribute__((__unused__)),
2476 char *end __attribute__((__unused__)))
2478 #ifdef CLEAR_INSN_CACHE
2479 CLEAR_INSN_CACHE (beg, end);
2480 #else
2481 #ifdef INSN_CACHE_SIZE
2482 static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
2483 static int initialized;
2484 int offset;
2485 void *start_addr
2486 void *end_addr;
2487 typedef (*function_ptr) ();
2489 #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
2490 /* It's cheaper to clear the whole cache.
2491 Put in a series of jump instructions so that calling the beginning
2492 of the cache will clear the whole thing. */
2494 if (! initialized)
2496 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2497 & -INSN_CACHE_LINE_WIDTH);
2498 int end_ptr = ptr + INSN_CACHE_SIZE;
2500 while (ptr < end_ptr)
2502 *(INSTRUCTION_TYPE *)ptr
2503 = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
2504 ptr += INSN_CACHE_LINE_WIDTH;
2506 *(INSTRUCTION_TYPE *) (ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
2508 initialized = 1;
2511 /* Call the beginning of the sequence. */
2512 (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2513 & -INSN_CACHE_LINE_WIDTH))
2514 ());
2516 #else /* Cache is large. */
2518 if (! initialized)
2520 int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
2521 & -INSN_CACHE_LINE_WIDTH);
2523 while (ptr < (int) array + sizeof array)
2525 *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
2526 ptr += INSN_CACHE_LINE_WIDTH;
2529 initialized = 1;
2532 /* Find the location in array that occupies the same cache line as BEG. */
2534 offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
2535 start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
2536 & -INSN_CACHE_PLANE_SIZE)
2537 + offset);
2539 /* Compute the cache alignment of the place to stop clearing. */
2540 #if 0 /* This is not needed for gcc's purposes. */
2541 /* If the block to clear is bigger than a cache plane,
2542 we clear the entire cache, and OFFSET is already correct. */
2543 if (end < beg + INSN_CACHE_PLANE_SIZE)
2544 #endif
2545 offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
2546 & -INSN_CACHE_LINE_WIDTH)
2547 & (INSN_CACHE_PLANE_SIZE - 1));
2549 #if INSN_CACHE_DEPTH > 1
2550 end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
2551 if (end_addr <= start_addr)
2552 end_addr += INSN_CACHE_PLANE_SIZE;
2554 for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
2556 int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
2557 int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
2559 while (addr != stop)
2561 /* Call the return instruction at ADDR. */
2562 ((function_ptr) addr) ();
2564 addr += INSN_CACHE_LINE_WIDTH;
2567 #else /* just one plane */
2570 /* Call the return instruction at START_ADDR. */
2571 ((function_ptr) start_addr) ();
2573 start_addr += INSN_CACHE_LINE_WIDTH;
2575 while ((start_addr % INSN_CACHE_SIZE) != offset);
2576 #endif /* just one plane */
2577 #endif /* Cache is large */
2578 #endif /* Cache exists */
2579 #endif /* CLEAR_INSN_CACHE */
2582 #endif /* L_clear_cache */
2584 #ifdef L_trampoline
2586 /* Jump to a trampoline, loading the static chain address. */
2588 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
2590 long getpagesize()
2592 #ifdef _ALPHA_
2593 return 8192;
2594 #else
2595 return 4096;
2596 #endif
2599 #ifdef i386
2600 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
2601 #endif
2604 mprotect (char *addr, int len, int prot)
2606 int np, op;
2608 if (prot == 7)
2609 np = 0x40;
2610 else if (prot == 5)
2611 np = 0x20;
2612 else if (prot == 4)
2613 np = 0x10;
2614 else if (prot == 3)
2615 np = 0x04;
2616 else if (prot == 1)
2617 np = 0x02;
2618 else if (prot == 0)
2619 np = 0x01;
2621 if (VirtualProtect (addr, len, np, &op))
2622 return 0;
2623 else
2624 return -1;
2627 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2629 #ifdef TRANSFER_FROM_TRAMPOLINE
2630 TRANSFER_FROM_TRAMPOLINE
2631 #endif
2633 #if defined (NeXT) && defined (__MACH__)
2635 /* Make stack executable so we can call trampolines on stack.
2636 This is called from INITIALIZE_TRAMPOLINE in next.h. */
2637 #ifdef NeXTStep21
2638 #include <mach.h>
2639 #else
2640 #include <mach/mach.h>
2641 #endif
2643 void
2644 __enable_execute_stack (char *addr)
2646 kern_return_t r;
2647 char *eaddr = addr + TRAMPOLINE_SIZE;
2648 vm_address_t a = (vm_address_t) addr;
2650 /* turn on execute access on stack */
2651 r = vm_protect (task_self (), a, TRAMPOLINE_SIZE, FALSE, VM_PROT_ALL);
2652 if (r != KERN_SUCCESS)
2654 mach_error("vm_protect VM_PROT_ALL", r);
2655 exit(1);
2658 /* We inline the i-cache invalidation for speed */
2660 #ifdef CLEAR_INSN_CACHE
2661 CLEAR_INSN_CACHE (addr, eaddr);
2662 #else
2663 __clear_cache ((int) addr, (int) eaddr);
2664 #endif
2667 #endif /* defined (NeXT) && defined (__MACH__) */
2669 #ifdef __convex__
2671 /* Make stack executable so we can call trampolines on stack.
2672 This is called from INITIALIZE_TRAMPOLINE in convex.h. */
2674 #include <sys/mman.h>
2675 #include <sys/vmparam.h>
2676 #include <machine/machparam.h>
2678 void
2679 __enable_execute_stack ()
2681 int fp;
2682 static unsigned lowest = USRSTACK;
2683 unsigned current = (unsigned) &fp & -NBPG;
2685 if (lowest > current)
2687 unsigned len = lowest - current;
2688 mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
2689 lowest = current;
2692 /* Clear instruction cache in case an old trampoline is in it. */
2693 asm ("pich");
2695 #endif /* __convex__ */
2697 #ifdef __sysV88__
2699 /* Modified from the convex -code above. */
2701 #include <sys/param.h>
2702 #include <errno.h>
2703 #include <sys/m88kbcs.h>
2705 void
2706 __enable_execute_stack ()
2708 int save_errno;
2709 static unsigned long lowest = USRSTACK;
2710 unsigned long current = (unsigned long) &save_errno & -NBPC;
2712 /* Ignore errno being set. memctl sets errno to EINVAL whenever the
2713 address is seen as 'negative'. That is the case with the stack. */
2715 save_errno=errno;
2716 if (lowest > current)
2718 unsigned len=lowest-current;
2719 memctl(current,len,MCT_TEXT);
2720 lowest = current;
2722 else
2723 memctl(current,NBPC,MCT_TEXT);
2724 errno=save_errno;
2727 #endif /* __sysV88__ */
2729 #ifdef __sysV68__
2731 #include <sys/signal.h>
2732 #include <errno.h>
2734 /* Motorola forgot to put memctl.o in the libp version of libc881.a,
2735 so define it here, because we need it in __clear_insn_cache below */
2736 /* On older versions of this OS, no memctl or MCT_TEXT are defined;
2737 hence we enable this stuff only if MCT_TEXT is #define'd. */
2739 #ifdef MCT_TEXT
2740 asm("\n\
2741 global memctl\n\
2742 memctl:\n\
2743 movq &75,%d0\n\
2744 trap &0\n\
2745 bcc.b noerror\n\
2746 jmp cerror%\n\
2747 noerror:\n\
2748 movq &0,%d0\n\
2749 rts");
2750 #endif
2752 /* Clear instruction cache so we can call trampolines on stack.
2753 This is called from FINALIZE_TRAMPOLINE in mot3300.h. */
2755 void
2756 __clear_insn_cache ()
2758 #ifdef MCT_TEXT
2759 int save_errno;
2761 /* Preserve errno, because users would be surprised to have
2762 errno changing without explicitly calling any system-call. */
2763 save_errno = errno;
2765 /* Keep it simple : memctl (MCT_TEXT) always fully clears the insn cache.
2766 No need to use an address derived from _start or %sp, as 0 works also. */
2767 memctl(0, 4096, MCT_TEXT);
2768 errno = save_errno;
2769 #endif
2772 #endif /* __sysV68__ */
2774 #ifdef __pyr__
2776 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2777 #include <stdio.h>
2778 #include <sys/mman.h>
2779 #include <sys/types.h>
2780 #include <sys/param.h>
2781 #include <sys/vmmac.h>
2783 /* Modified from the convex -code above.
2784 mremap promises to clear the i-cache. */
2786 void
2787 __enable_execute_stack ()
2789 int fp;
2790 if (mprotect (((unsigned int)&fp/PAGSIZ)*PAGSIZ, PAGSIZ,
2791 PROT_READ|PROT_WRITE|PROT_EXEC))
2793 perror ("mprotect in __enable_execute_stack");
2794 fflush (stderr);
2795 abort ();
2798 #endif /* __pyr__ */
2800 #if defined (sony_news) && defined (SYSTYPE_BSD)
2802 #include <stdio.h>
2803 #include <sys/types.h>
2804 #include <sys/param.h>
2805 #include <syscall.h>
2806 #include <machine/sysnews.h>
2808 /* cacheflush function for NEWS-OS 4.2.
2809 This function is called from trampoline-initialize code
2810 defined in config/mips/mips.h. */
2812 void
2813 cacheflush (char *beg, int size, int flag)
2815 if (syscall (SYS_sysnews, NEWS_CACHEFLUSH, beg, size, FLUSH_BCACHE))
2817 perror ("cache_flush");
2818 fflush (stderr);
2819 abort ();
2823 #endif /* sony_news */
2824 #endif /* L_trampoline */
2826 #ifndef __CYGWIN__
2827 #ifdef L__main
2829 #include "gbl-ctors.h"
2830 /* Some systems use __main in a way incompatible with its use in gcc, in these
2831 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2832 give the same symbol without quotes for an alternative entry point. You
2833 must define both, or neither. */
2834 #ifndef NAME__MAIN
2835 #define NAME__MAIN "__main"
2836 #define SYMBOL__MAIN __main
2837 #endif
2839 #ifdef INIT_SECTION_ASM_OP
2840 #undef HAS_INIT_SECTION
2841 #define HAS_INIT_SECTION
2842 #endif
2844 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2846 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2847 code to run constructors. In that case, we need to handle EH here, too. */
2849 #ifdef EH_FRAME_SECTION
2850 #include "frame.h"
2851 extern unsigned char __EH_FRAME_BEGIN__[];
2852 #endif
2854 /* Run all the global destructors on exit from the program. */
2856 void
2857 __do_global_dtors ()
2859 #ifdef DO_GLOBAL_DTORS_BODY
2860 DO_GLOBAL_DTORS_BODY;
2861 #else
2862 static func_ptr *p = __DTOR_LIST__ + 1;
2863 while (*p)
2865 p++;
2866 (*(p-1)) ();
2868 #endif
2869 #ifdef EH_FRAME_SECTION
2870 __deregister_frame_info (__EH_FRAME_BEGIN__);
2871 #endif
2873 #endif
2875 #ifndef HAS_INIT_SECTION
2876 /* Run all the global constructors on entry to the program. */
2878 void
2879 __do_global_ctors ()
2881 #ifdef EH_FRAME_SECTION
2883 static struct object object;
2884 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2886 #endif
2887 DO_GLOBAL_CTORS_BODY;
2888 atexit (__do_global_dtors);
2890 #endif /* no HAS_INIT_SECTION */
2892 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2893 /* Subroutine called automatically by `main'.
2894 Compiling a global function named `main'
2895 produces an automatic call to this function at the beginning.
2897 For many systems, this routine calls __do_global_ctors.
2898 For systems which support a .init section we use the .init section
2899 to run __do_global_ctors, so we need not do anything here. */
2901 void
2902 SYMBOL__MAIN ()
2904 /* Support recursive calls to `main': run initializers just once. */
2905 static int initialized;
2906 if (! initialized)
2908 initialized = 1;
2909 __do_global_ctors ();
2912 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2914 #endif /* L__main */
2915 #endif /* __CYGWIN__ */
2917 #ifdef L_ctors
2919 #include "gbl-ctors.h"
2921 /* Provide default definitions for the lists of constructors and
2922 destructors, so that we don't get linker errors. These symbols are
2923 intentionally bss symbols, so that gld and/or collect will provide
2924 the right values. */
2926 /* We declare the lists here with two elements each,
2927 so that they are valid empty lists if no other definition is loaded.
2929 If we are using the old "set" extensions to have the gnu linker
2930 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2931 must be in the bss/common section.
2933 Long term no port should use those extensions. But many still do. */
2934 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2935 #if defined (ASM_OUTPUT_CONSTRUCTOR) || defined (USE_COLLECT2)
2936 func_ptr __CTOR_LIST__[2] = {0, 0};
2937 func_ptr __DTOR_LIST__[2] = {0, 0};
2938 #else
2939 func_ptr __CTOR_LIST__[2];
2940 func_ptr __DTOR_LIST__[2];
2941 #endif
2942 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2943 #endif /* L_ctors */
2945 #ifdef L_exit
2947 #include "gbl-ctors.h"
2949 #ifdef NEED_ATEXIT
2951 #ifndef ON_EXIT
2953 # include <errno.h>
2955 static func_ptr *atexit_chain = 0;
2956 static long atexit_chain_length = 0;
2957 static volatile long last_atexit_chain_slot = -1;
2960 atexit (func_ptr func)
2962 if (++last_atexit_chain_slot == atexit_chain_length)
2964 atexit_chain_length += 32;
2965 if (atexit_chain)
2966 atexit_chain = (func_ptr *) realloc (atexit_chain, atexit_chain_length
2967 * sizeof (func_ptr));
2968 else
2969 atexit_chain = (func_ptr *) malloc (atexit_chain_length
2970 * sizeof (func_ptr));
2971 if (! atexit_chain)
2973 atexit_chain_length = 0;
2974 last_atexit_chain_slot = -1;
2975 errno = ENOMEM;
2976 return (-1);
2979 atexit_chain[last_atexit_chain_slot] = func;
2980 return (0);
2983 extern void _cleanup ();
2984 extern void _exit () __attribute__ ((noreturn));
2986 void
2987 exit (int status)
2989 if (atexit_chain)
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);
2997 atexit_chain = 0;
2999 #ifdef EXIT_BODY
3000 EXIT_BODY;
3001 #else
3002 _cleanup ();
3003 #endif
3004 _exit (status);
3007 #else /* ON_EXIT */
3009 /* Simple; we just need a wrapper for ON_EXIT. */
3011 atexit (func_ptr func)
3013 return ON_EXIT (func);
3016 #endif /* ON_EXIT */
3017 #endif /* NEED_ATEXIT */
3019 #endif /* L_exit */
3021 #ifdef L_eh
3023 #include "gthr.h"
3025 /* Shared exception handling support routines. */
3027 extern void __default_terminate (void) __attribute__ ((__noreturn__));
3029 void
3030 __default_terminate ()
3032 abort ();
3035 void (*__terminate_func)() = __default_terminate;
3037 void
3038 __terminate ()
3040 (*__terminate_func)();
3043 void *
3044 __throw_type_match (void *catch_type, void *throw_type, void *obj)
3046 #if 0
3047 printf ("__throw_type_match (): catch_type = %s, throw_type = %s\n",
3048 catch_type, throw_type);
3049 #endif
3050 if (strcmp ((const char *)catch_type, (const char *)throw_type) == 0)
3051 return obj;
3052 return 0;
3055 void
3056 __empty ()
3061 /* Include definitions of EH context and table layout */
3063 #include "eh-common.h"
3064 #ifndef inhibit_libc
3065 #include <stdio.h>
3066 #endif
3068 /* Allocate and return a new EH context structure. */
3070 extern void __throw ();
3072 static void *
3073 new_eh_context ()
3075 struct eh_full_context {
3076 struct eh_context c;
3077 void *top_elt[2];
3078 } *ehfc = (struct eh_full_context *) malloc (sizeof *ehfc);
3080 if (! ehfc)
3081 __terminate ();
3083 memset (ehfc, 0, sizeof *ehfc);
3085 ehfc->c.dynamic_handler_chain = (void **) ehfc->top_elt;
3087 /* This should optimize out entirely. This should always be true,
3088 but just in case it ever isn't, don't allow bogus code to be
3089 generated. */
3091 if ((void*)(&ehfc->c) != (void*)ehfc)
3092 __terminate ();
3094 return &ehfc->c;
3097 #if __GTHREADS
3098 static __gthread_key_t eh_context_key;
3100 /* Destructor for struct eh_context. */
3101 static void
3102 eh_context_free (void *ptr)
3104 __gthread_key_dtor (eh_context_key, ptr);
3105 if (ptr)
3106 free (ptr);
3108 #endif
3110 /* Pointer to function to return EH context. */
3112 static struct eh_context *eh_context_initialize ();
3113 static struct eh_context *eh_context_static ();
3114 #if __GTHREADS
3115 static struct eh_context *eh_context_specific ();
3116 #endif
3118 static struct eh_context *(*get_eh_context) () = &eh_context_initialize;
3120 /* Routine to get EH context.
3121 This one will simply call the function pointer. */
3123 void *
3124 __get_eh_context ()
3126 return (void *) (*get_eh_context) ();
3129 /* Get and set the language specific info pointer. */
3131 void **
3132 __get_eh_info ()
3134 struct eh_context *eh = (*get_eh_context) ();
3135 return &eh->info;
3138 #ifdef DWARF2_UNWIND_INFO
3139 static int dwarf_reg_size_table_initialized = 0;
3140 static char dwarf_reg_size_table[FIRST_PSEUDO_REGISTER];
3142 static void
3143 init_reg_size_table ()
3145 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
3146 dwarf_reg_size_table_initialized = 1;
3148 #endif
3150 #if __GTHREADS
3151 static void
3152 eh_threads_initialize ()
3154 /* Try to create the key. If it fails, revert to static method,
3155 otherwise start using thread specific EH contexts. */
3156 if (__gthread_key_create (&eh_context_key, &eh_context_free) == 0)
3157 get_eh_context = &eh_context_specific;
3158 else
3159 get_eh_context = &eh_context_static;
3161 #endif /* no __GTHREADS */
3163 /* Initialize EH context.
3164 This will be called only once, since we change GET_EH_CONTEXT
3165 pointer to another routine. */
3167 static struct eh_context *
3168 eh_context_initialize ()
3170 #if __GTHREADS
3172 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
3173 /* Make sure that get_eh_context does not point to us anymore.
3174 Some systems have dummy thread routines in their libc that
3175 return a success (Solaris 2.6 for example). */
3176 if (__gthread_once (&once, eh_threads_initialize) != 0
3177 || get_eh_context == &eh_context_initialize)
3179 /* Use static version of EH context. */
3180 get_eh_context = &eh_context_static;
3182 #ifdef DWARF2_UNWIND_INFO
3184 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
3185 if (__gthread_once (&once_regsizes, init_reg_size_table) != 0
3186 || ! dwarf_reg_size_table_initialized)
3187 init_reg_size_table ();
3189 #endif
3191 #else /* no __GTHREADS */
3193 /* Use static version of EH context. */
3194 get_eh_context = &eh_context_static;
3196 #ifdef DWARF2_UNWIND_INFO
3197 init_reg_size_table ();
3198 #endif
3200 #endif /* no __GTHREADS */
3202 return (*get_eh_context) ();
3205 /* Return a static EH context. */
3207 static struct eh_context *
3208 eh_context_static ()
3210 static struct eh_context eh;
3211 static int initialized;
3212 static void *top_elt[2];
3214 if (! initialized)
3216 initialized = 1;
3217 memset (&eh, 0, sizeof eh);
3218 eh.dynamic_handler_chain = top_elt;
3220 return &eh;
3223 #if __GTHREADS
3224 /* Return a thread specific EH context. */
3226 static struct eh_context *
3227 eh_context_specific ()
3229 struct eh_context *eh;
3230 eh = (struct eh_context *) __gthread_getspecific (eh_context_key);
3231 if (! eh)
3233 eh = new_eh_context ();
3234 if (__gthread_setspecific (eh_context_key, (void *) eh) != 0)
3235 __terminate ();
3238 return eh;
3240 #endif __GTHREADS
3242 /* Support routines for setjmp/longjmp exception handling. */
3244 /* Calls to __sjthrow are generated by the compiler when an exception
3245 is raised when using the setjmp/longjmp exception handling codegen
3246 method. */
3248 #ifdef DONT_USE_BUILTIN_SETJMP
3249 extern void longjmp (void *, int);
3250 #endif
3252 /* Routine to get the head of the current thread's dynamic handler chain
3253 use for exception handling. */
3255 void ***
3256 __get_dynamic_handler_chain ()
3258 struct eh_context *eh = (*get_eh_context) ();
3259 return &eh->dynamic_handler_chain;
3262 /* This is used to throw an exception when the setjmp/longjmp codegen
3263 method is used for exception handling.
3265 We call __terminate if there are no handlers left. Otherwise we run the
3266 cleanup actions off the dynamic cleanup stack, and pop the top of the
3267 dynamic handler chain, and use longjmp to transfer back to the associated
3268 handler. */
3270 extern void __sjthrow (void) __attribute__ ((__noreturn__));
3272 void
3273 __sjthrow ()
3275 struct eh_context *eh = (*get_eh_context) ();
3276 void ***dhc = &eh->dynamic_handler_chain;
3277 void *jmpbuf;
3278 void (*func)(void *, int);
3279 void *arg;
3280 void ***cleanup;
3282 /* The cleanup chain is one word into the buffer. Get the cleanup
3283 chain. */
3284 cleanup = (void***)&(*dhc)[1];
3286 /* If there are any cleanups in the chain, run them now. */
3287 if (cleanup[0])
3289 double store[200];
3290 void **buf = (void**)store;
3291 buf[1] = 0;
3292 buf[0] = (*dhc);
3294 /* try { */
3295 #ifdef DONT_USE_BUILTIN_SETJMP
3296 if (! setjmp (&buf[2]))
3297 #else
3298 if (! __builtin_setjmp (&buf[2]))
3299 #endif
3301 *dhc = buf;
3302 while (cleanup[0])
3304 func = (void(*)(void*, int))cleanup[0][1];
3305 arg = (void*)cleanup[0][2];
3307 /* Update this before running the cleanup. */
3308 cleanup[0] = (void **)cleanup[0][0];
3310 (*func)(arg, 2);
3312 *dhc = buf[0];
3314 /* catch (...) */
3315 else
3317 __terminate ();
3321 /* We must call terminate if we try and rethrow an exception, when
3322 there is no exception currently active and when there are no
3323 handlers left. */
3324 if (! eh->info || (*dhc)[0] == 0)
3325 __terminate ();
3327 /* Find the jmpbuf associated with the top element of the dynamic
3328 handler chain. The jumpbuf starts two words into the buffer. */
3329 jmpbuf = &(*dhc)[2];
3331 /* Then we pop the top element off the dynamic handler chain. */
3332 *dhc = (void**)(*dhc)[0];
3334 /* And then we jump to the handler. */
3336 #ifdef DONT_USE_BUILTIN_SETJMP
3337 longjmp (jmpbuf, 1);
3338 #else
3339 __builtin_longjmp (jmpbuf, 1);
3340 #endif
3343 /* Run cleanups on the dynamic cleanup stack for the current dynamic
3344 handler, then pop the handler off the dynamic handler stack, and
3345 then throw. This is used to skip the first handler, and transfer
3346 control to the next handler in the dynamic handler stack. */
3348 extern void __sjpopnthrow (void) __attribute__ ((__noreturn__));
3350 void
3351 __sjpopnthrow ()
3353 struct eh_context *eh = (*get_eh_context) ();
3354 void ***dhc = &eh->dynamic_handler_chain;
3355 void (*func)(void *, int);
3356 void *arg;
3357 void ***cleanup;
3359 /* The cleanup chain is one word into the buffer. Get the cleanup
3360 chain. */
3361 cleanup = (void***)&(*dhc)[1];
3363 /* If there are any cleanups in the chain, run them now. */
3364 if (cleanup[0])
3366 double store[200];
3367 void **buf = (void**)store;
3368 buf[1] = 0;
3369 buf[0] = (*dhc);
3371 /* try { */
3372 #ifdef DONT_USE_BUILTIN_SETJMP
3373 if (! setjmp (&buf[2]))
3374 #else
3375 if (! __builtin_setjmp (&buf[2]))
3376 #endif
3378 *dhc = buf;
3379 while (cleanup[0])
3381 func = (void(*)(void*, int))cleanup[0][1];
3382 arg = (void*)cleanup[0][2];
3384 /* Update this before running the cleanup. */
3385 cleanup[0] = (void **)cleanup[0][0];
3387 (*func)(arg, 2);
3389 *dhc = buf[0];
3391 /* catch (...) */
3392 else
3394 __terminate ();
3398 /* Then we pop the top element off the dynamic handler chain. */
3399 *dhc = (void**)(*dhc)[0];
3401 __sjthrow ();
3404 /* Support code for all exception region-based exception handling. */
3407 __eh_rtime_match (void *rtime)
3409 void *info;
3410 __eh_matcher matcher;
3411 void *ret;
3413 info = *(__get_eh_info ());
3414 matcher = ((__eh_info *)info)->match_function;
3415 if (! matcher)
3417 #ifndef inhibit_libc
3418 fprintf (stderr, "Internal Compiler Bug: No runtime type matcher.");
3419 #endif
3420 return 0;
3422 ret = (*matcher) (info, rtime, (void *)0);
3423 return (ret != NULL);
3426 /* This value identifies the place from which an exception is being
3427 thrown. */
3429 #ifdef EH_TABLE_LOOKUP
3431 EH_TABLE_LOOKUP
3433 #else
3435 #ifdef DWARF2_UNWIND_INFO
3437 /* Return the table version of an exception descriptor */
3439 short
3440 __get_eh_table_version (exception_descriptor *table)
3442 return table->lang.version;
3445 /* Return the originating table language of an exception descriptor */
3447 short
3448 __get_eh_table_language (exception_descriptor *table)
3450 return table->lang.language;
3453 /* This routine takes a PC and a pointer to the exception region TABLE for
3454 its translation unit, and returns the address of the exception handler
3455 associated with the closest exception table handler entry associated
3456 with that PC, or 0 if there are no table entries the PC fits in.
3458 In the advent of a tie, we have to give the last entry, as it represents
3459 an inner block. */
3461 static void *
3462 old_find_exception_handler (void *pc, old_exception_table *table)
3464 if (table)
3466 int pos;
3467 int best = -1;
3469 /* We can't do a binary search because the table isn't guaranteed
3470 to be sorted from function to function. */
3471 for (pos = 0; table[pos].start_region != (void *) -1; ++pos)
3473 if (table[pos].start_region <= pc && table[pos].end_region > pc)
3475 /* This can apply. Make sure it is at least as small as
3476 the previous best. */
3477 if (best == -1 || (table[pos].end_region <= table[best].end_region
3478 && table[pos].start_region >= table[best].start_region))
3479 best = pos;
3481 /* But it is sorted by starting PC within a function. */
3482 else if (best >= 0 && table[pos].start_region > pc)
3483 break;
3485 if (best != -1)
3486 return table[best].exception_handler;
3489 return (void *) 0;
3492 /* find_exception_handler finds the correct handler, if there is one, to
3493 handle an exception.
3494 returns a pointer to the handler which controlled should be transferred
3495 to, or NULL if there is nothing left.
3496 Parameters:
3497 PC - pc where the exception originates. If this is a rethrow,
3498 then this starts out as a pointer to the exception table
3499 entry we wish to rethrow out of.
3500 TABLE - exception table for the current module.
3501 EH_INFO - eh info pointer for this exception.
3502 RETHROW - 1 if this is a rethrow. (see incoming value of PC).
3503 CLEANUP - returned flag indicating whether this is a cleanup handler.
3505 static void *
3506 find_exception_handler (void *pc, exception_descriptor *table,
3507 __eh_info *eh_info, int rethrow, int *cleanup)
3510 void *retval = NULL;
3511 *cleanup = 1;
3512 if (table)
3514 int pos = 0;
3515 /* The new model assumed the table is sorted inner-most out so the
3516 first region we find which matches is the correct one */
3518 exception_table *tab = &(table->table[0]);
3520 /* Subtract 1 from the PC to avoid hitting the next region */
3521 if (rethrow)
3523 /* pc is actually the region table entry to rethrow out of */
3524 pos = ((exception_table *) pc) - tab;
3525 pc = ((exception_table *) pc)->end_region - 1;
3527 /* The label is always on the LAST handler entry for a region,
3528 so we know the next entry is a different region, even if the
3529 addresses are the same. Make sure its not end of table tho. */
3530 if (tab[pos].start_region != (void *) -1)
3531 pos++;
3533 else
3534 pc--;
3536 /* We can't do a binary search because the table is in inner-most
3537 to outermost address ranges within functions */
3538 for ( ; tab[pos].start_region != (void *) -1; pos++)
3540 if (tab[pos].start_region <= pc && tab[pos].end_region > pc)
3542 if (tab[pos].match_info)
3544 __eh_matcher matcher = eh_info->match_function;
3545 /* match info but no matcher is NOT a match */
3546 if (matcher)
3548 void *ret = (*matcher)((void *) eh_info,
3549 tab[pos].match_info, table);
3550 if (ret)
3552 if (retval == NULL)
3553 retval = tab[pos].exception_handler;
3554 *cleanup = 0;
3555 break;
3559 else
3561 if (retval == NULL)
3562 retval = tab[pos].exception_handler;
3567 return retval;
3569 #endif /* DWARF2_UNWIND_INFO */
3570 #endif /* EH_TABLE_LOOKUP */
3572 #ifdef DWARF2_UNWIND_INFO
3573 /* Support code for exception handling using static unwind information. */
3575 #include "frame.h"
3577 /* This type is used in get_reg and put_reg to deal with ABIs where a void*
3578 is smaller than a word, such as the Irix 6 n32 ABI. We cast twice to
3579 avoid a warning about casting between int and pointer of different
3580 sizes. */
3582 typedef int ptr_type __attribute__ ((mode (pointer)));
3584 #ifdef INCOMING_REGNO
3585 /* Is the saved value for register REG in frame UDATA stored in a register
3586 window in the previous frame? */
3588 /* ??? The Sparc INCOMING_REGNO references TARGET_FLAT. This allows us
3589 to use the macro here. One wonders, though, that perhaps TARGET_FLAT
3590 compiled functions won't work with the frame-unwind stuff here.
3591 Perhaps the entireity of in_reg_window should be conditional on having
3592 seen a DW_CFA_GNU_window_save? */
3593 #define target_flags 0
3595 static int
3596 in_reg_window (int reg, frame_state *udata)
3598 if (udata->saved[reg] == REG_SAVED_REG)
3599 return INCOMING_REGNO (reg) == reg;
3600 if (udata->saved[reg] != REG_SAVED_OFFSET)
3601 return 0;
3603 #ifdef STACK_GROWS_DOWNWARD
3604 return udata->reg_or_offset[reg] > 0;
3605 #else
3606 return udata->reg_or_offset[reg] < 0;
3607 #endif
3609 #else
3610 static inline int in_reg_window (int reg, frame_state *udata) { return 0; }
3611 #endif /* INCOMING_REGNO */
3613 /* Get the address of register REG as saved in UDATA, where SUB_UDATA is a
3614 frame called by UDATA or 0. */
3616 static word_type *
3617 get_reg_addr (unsigned reg, frame_state *udata, frame_state *sub_udata)
3619 while (udata->saved[reg] == REG_SAVED_REG)
3621 reg = udata->reg_or_offset[reg];
3622 if (in_reg_window (reg, udata))
3624 udata = sub_udata;
3625 sub_udata = NULL;
3628 if (udata->saved[reg] == REG_SAVED_OFFSET)
3629 return (word_type *)(udata->cfa + udata->reg_or_offset[reg]);
3630 else
3631 abort ();
3634 /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
3635 frame called by UDATA or 0. */
3637 static inline void *
3638 get_reg (unsigned reg, frame_state *udata, frame_state *sub_udata)
3640 return (void *)(ptr_type) *get_reg_addr (reg, udata, sub_udata);
3643 /* Overwrite the saved value for register REG in frame UDATA with VAL. */
3645 static inline void
3646 put_reg (unsigned reg, void *val, frame_state *udata)
3648 *get_reg_addr (reg, udata, NULL) = (word_type)(ptr_type) val;
3651 /* Copy the saved value for register REG from frame UDATA to frame
3652 TARGET_UDATA. Unlike the previous two functions, this can handle
3653 registers that are not one word large. */
3655 static void
3656 copy_reg (unsigned reg, frame_state *udata, frame_state *target_udata)
3658 word_type *preg = get_reg_addr (reg, udata, NULL);
3659 word_type *ptreg = get_reg_addr (reg, target_udata, NULL);
3661 memcpy (ptreg, preg, dwarf_reg_size_table [reg]);
3664 /* Retrieve the return address for frame UDATA. */
3666 static inline void *
3667 get_return_addr (frame_state *udata, frame_state *sub_udata)
3669 return __builtin_extract_return_addr
3670 (get_reg (udata->retaddr_column, udata, sub_udata));
3673 /* Overwrite the return address for frame UDATA with VAL. */
3675 static inline void
3676 put_return_addr (void *val, frame_state *udata)
3678 val = __builtin_frob_return_addr (val);
3679 put_reg (udata->retaddr_column, val, udata);
3682 /* Given the current frame UDATA and its return address PC, return the
3683 information about the calling frame in CALLER_UDATA. */
3685 static void *
3686 next_stack_level (void *pc, frame_state *udata, frame_state *caller_udata)
3688 caller_udata = __frame_state_for (pc, caller_udata);
3689 if (! caller_udata)
3690 return 0;
3692 /* Now go back to our caller's stack frame. If our caller's CFA register
3693 was saved in our stack frame, restore it; otherwise, assume the CFA
3694 register is SP and restore it to our CFA value. */
3695 if (udata->saved[caller_udata->cfa_reg])
3696 caller_udata->cfa = get_reg (caller_udata->cfa_reg, udata, 0);
3697 else
3698 caller_udata->cfa = udata->cfa;
3699 caller_udata->cfa += caller_udata->cfa_offset;
3701 return caller_udata;
3704 /* Hook to call before __terminate if only cleanup handlers remain. */
3705 void
3706 __unwinding_cleanup ()
3710 /* throw_helper performs some of the common grunt work for a throw. This
3711 routine is called by throw and rethrows. This is pretty much split
3712 out from the old __throw routine. An addition has been added which allows
3713 for a dummy call to a routine __unwinding_cleanup() when there are nothing
3714 but cleanups remaining. This allows a debugger to examine the state
3715 at which the throw was executed, before any cleanups, rather than
3716 at the terminate point after the stack has been unwound.
3718 EH is the current eh_context structure.
3719 PC is the address of the call to __throw.
3720 MY_UDATA is the unwind information for __throw.
3721 OFFSET_P is where we return the SP adjustment offset. */
3723 static void *
3724 throw_helper (eh, pc, my_udata, offset_p)
3725 struct eh_context *eh;
3726 void *pc;
3727 frame_state *my_udata;
3728 long *offset_p;
3730 frame_state ustruct2, *udata = &ustruct2;
3731 frame_state ustruct;
3732 frame_state *sub_udata = &ustruct;
3733 void *saved_pc = pc;
3734 void *handler;
3735 void *handler_p;
3736 void *pc_p;
3737 frame_state saved_ustruct;
3738 int new_eh_model;
3739 int cleanup = 0;
3740 int only_cleanup = 0;
3741 int rethrow = 0;
3742 int saved_state = 0;
3743 long args_size;
3744 __eh_info *eh_info = (__eh_info *)eh->info;
3746 /* Do we find a handler based on a re-throw PC? */
3747 if (eh->table_index != (void *) 0)
3748 rethrow = 1;
3750 memcpy (udata, my_udata, sizeof (*udata));
3752 handler = (void *) 0;
3753 for (;;)
3755 frame_state *p = udata;
3756 udata = next_stack_level (pc, udata, sub_udata);
3757 sub_udata = p;
3759 /* If we couldn't find the next frame, we lose. */
3760 if (! udata)
3761 break;
3763 if (udata->eh_ptr == NULL)
3764 new_eh_model = 0;
3765 else
3766 new_eh_model = (((exception_descriptor *)(udata->eh_ptr))->
3767 runtime_id_field == NEW_EH_RUNTIME);
3769 if (rethrow)
3771 rethrow = 0;
3772 handler = find_exception_handler (eh->table_index, udata->eh_ptr,
3773 eh_info, 1, &cleanup);
3774 eh->table_index = (void *)0;
3776 else
3777 if (new_eh_model)
3778 handler = find_exception_handler (pc, udata->eh_ptr, eh_info,
3779 0, &cleanup);
3780 else
3781 handler = old_find_exception_handler (pc, udata->eh_ptr);
3783 /* If we found one, we can stop searching, if its not a cleanup.
3784 for cleanups, we save the state, and keep looking. This allows
3785 us to call a debug hook if there are nothing but cleanups left. */
3786 if (handler)
3788 if (cleanup)
3790 if (!saved_state)
3792 saved_ustruct = *udata;
3793 handler_p = handler;
3794 pc_p = pc;
3795 saved_state = 1;
3796 only_cleanup = 1;
3799 else
3801 only_cleanup = 0;
3802 break;
3806 /* Otherwise, we continue searching. We subtract 1 from PC to avoid
3807 hitting the beginning of the next region. */
3808 pc = get_return_addr (udata, sub_udata) - 1;
3811 if (saved_state)
3813 udata = &saved_ustruct;
3814 handler = handler_p;
3815 pc = pc_p;
3816 if (only_cleanup)
3817 __unwinding_cleanup ();
3820 /* If we haven't found a handler by now, this is an unhandled
3821 exception. */
3822 if (! handler)
3823 __terminate();
3825 eh->handler_label = handler;
3827 args_size = udata->args_size;
3829 if (pc == saved_pc)
3830 /* We found a handler in the throw context, no need to unwind. */
3831 udata = my_udata;
3832 else
3834 int i;
3836 /* Unwind all the frames between this one and the handler by copying
3837 their saved register values into our register save slots. */
3839 /* Remember the PC where we found the handler. */
3840 void *handler_pc = pc;
3842 /* Start from the throw context again. */
3843 pc = saved_pc;
3844 memcpy (udata, my_udata, sizeof (*udata));
3846 while (pc != handler_pc)
3848 frame_state *p = udata;
3849 udata = next_stack_level (pc, udata, sub_udata);
3850 sub_udata = p;
3852 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
3853 if (i != udata->retaddr_column && udata->saved[i])
3855 /* If you modify the saved value of the return address
3856 register on the SPARC, you modify the return address for
3857 your caller's frame. Don't do that here, as it will
3858 confuse get_return_addr. */
3859 if (in_reg_window (i, udata)
3860 && udata->saved[udata->retaddr_column] == REG_SAVED_REG
3861 && udata->reg_or_offset[udata->retaddr_column] == i)
3862 continue;
3863 copy_reg (i, udata, my_udata);
3866 pc = get_return_addr (udata, sub_udata) - 1;
3869 /* But we do need to update the saved return address register from
3870 the last frame we unwind, or the handler frame will have the wrong
3871 return address. */
3872 if (udata->saved[udata->retaddr_column] == REG_SAVED_REG)
3874 i = udata->reg_or_offset[udata->retaddr_column];
3875 if (in_reg_window (i, udata))
3876 copy_reg (i, udata, my_udata);
3879 /* udata now refers to the frame called by the handler frame. */
3881 /* We adjust SP by the difference between __throw's CFA and the CFA for
3882 the frame called by the handler frame, because those CFAs correspond
3883 to the SP values at the two call sites. We need to further adjust by
3884 the args_size of the handler frame itself to get the handler frame's
3885 SP from before the args were pushed for that call. */
3886 #ifdef STACK_GROWS_DOWNWARD
3887 *offset_p = udata->cfa - my_udata->cfa + args_size;
3888 #else
3889 *offset_p = my_udata->cfa - udata->cfa - args_size;
3890 #endif
3892 return handler;
3896 /* We first search for an exception handler, and if we don't find
3897 it, we call __terminate on the current stack frame so that we may
3898 use the debugger to walk the stack and understand why no handler
3899 was found.
3901 If we find one, then we unwind the frames down to the one that
3902 has the handler and transfer control into the handler. */
3904 /*extern void __throw(void) __attribute__ ((__noreturn__));*/
3906 void
3907 __throw ()
3909 struct eh_context *eh = (*get_eh_context) ();
3910 void *pc, *handler;
3911 long offset;
3913 /* XXX maybe make my_ustruct static so we don't have to look it up for
3914 each throw. */
3915 frame_state my_ustruct, *my_udata = &my_ustruct;
3917 /* This is required for C++ semantics. We must call terminate if we
3918 try and rethrow an exception, when there is no exception currently
3919 active. */
3920 if (! eh->info)
3921 __terminate ();
3923 /* Start at our stack frame. */
3924 label:
3925 my_udata = __frame_state_for (&&label, my_udata);
3926 if (! my_udata)
3927 __terminate ();
3929 /* We need to get the value from the CFA register. */
3930 my_udata->cfa = __builtin_dwarf_cfa ();
3932 /* Do any necessary initialization to access arbitrary stack frames.
3933 On the SPARC, this means flushing the register windows. */
3934 __builtin_unwind_init ();
3936 /* Now reset pc to the right throw point. */
3937 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3939 handler = throw_helper (eh, pc, my_udata, &offset);
3941 /* Now go! */
3943 __builtin_eh_return ((void *)eh, offset, handler);
3945 /* Epilogue: restore the handler frame's register values and return
3946 to the stub. */
3949 /*extern void __rethrow(void *) __attribute__ ((__noreturn__));*/
3951 void
3952 __rethrow (index)
3953 void *index;
3955 struct eh_context *eh = (*get_eh_context) ();
3956 void *pc, *handler;
3957 long offset;
3959 /* XXX maybe make my_ustruct static so we don't have to look it up for
3960 each throw. */
3961 frame_state my_ustruct, *my_udata = &my_ustruct;
3963 /* This is required for C++ semantics. We must call terminate if we
3964 try and rethrow an exception, when there is no exception currently
3965 active. */
3966 if (! eh->info)
3967 __terminate ();
3969 /* This is the table index we want to rethrow from. The value of
3970 the END_REGION label is used for the PC of the throw, and the
3971 search begins with the next table entry. */
3972 eh->table_index = index;
3974 /* Start at our stack frame. */
3975 label:
3976 my_udata = __frame_state_for (&&label, my_udata);
3977 if (! my_udata)
3978 __terminate ();
3980 /* We need to get the value from the CFA register. */
3981 my_udata->cfa = __builtin_dwarf_cfa ();
3983 /* Do any necessary initialization to access arbitrary stack frames.
3984 On the SPARC, this means flushing the register windows. */
3985 __builtin_unwind_init ();
3987 /* Now reset pc to the right throw point. */
3988 pc = __builtin_extract_return_addr (__builtin_return_address (0)) - 1;
3990 handler = throw_helper (eh, pc, my_udata, &offset);
3992 /* Now go! */
3994 __builtin_eh_return ((void *)eh, offset, handler);
3996 /* Epilogue: restore the handler frame's register values and return
3997 to the stub. */
3999 #endif /* DWARF2_UNWIND_INFO */
4001 #endif /* L_eh */
4003 #ifdef L_pure
4004 #ifndef inhibit_libc
4005 /* This gets us __GNU_LIBRARY__. */
4006 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
4007 #include <stdio.h>
4009 #ifdef __GNU_LIBRARY__
4010 /* Avoid forcing the library's meaning of `write' on the user program
4011 by using the "internal" name (for use within the library) */
4012 #define write(fd, buf, n) __write((fd), (buf), (n))
4013 #endif
4014 #endif /* inhibit_libc */
4016 #define MESSAGE "pure virtual method called\n"
4018 extern void __terminate (void) __attribute__ ((__noreturn__));
4020 void
4021 __pure_virtual ()
4023 #ifndef inhibit_libc
4024 write (2, MESSAGE, sizeof (MESSAGE) - 1);
4025 #endif
4026 __terminate ();
4028 #endif