home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / setjmp.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  167 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * the Systems Programming Group of the University of Utah Computer
  7.  * Science Department.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39.     .asciz "@(#)setjmp.s    5.1 (Berkeley) 5/12/90"
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. /*
  43.  * C library -- setjmp, longjmp
  44.  *
  45.  *    longjmp(a,v)
  46.  * will generate a "return(v)" from
  47.  * the last call to
  48.  *    setjmp(a)
  49.  * by restoring registers from the stack,
  50.  * and a struct sigcontext, see <signal.h>
  51.  */
  52.  
  53. #include "defs.h"
  54.  
  55. ENTRY(sigsetjmp)
  56. asm("
  57.     movl    sp@(4),a0
  58.     movl    sp@(8),a0@
  59.     addl    #4,a0
  60.     movl    a0,sp@(4)
  61.     tstl    a0@(-4)
  62.     jne    _setjmp
  63. ");
  64.  
  65. ENTRY(_setjmp)
  66. asm("
  67.     moveq    #0,d0        /* dummy signal mask */
  68.     movel    d0,d1        /*  and onstack */
  69.     jra    setup_setjmp
  70. ");
  71.  
  72. asm("
  73.     /* routine to obtain current values of onstack/sigmask */
  74. get_onstack_sigmask:
  75.     subql    #8,sp        /* space for sigstack args/rvals */
  76.     clrl    sp@        /* don't change it... */
  77.     movl    sp,sp@(4)    /* ...but return the current val */
  78.     jsr    _sigstack    /* note: onstack returned in sp@(4) */
  79.     clrl    sp@        /* don't change mask, just return */
  80.     jsr    _sigblock    /*   old value */
  81.     movl    sp@(4),d1    /* old onstack value */
  82.     addql    #8,sp
  83.     rts            /* d0 = sigmask, d1 = onstack */
  84. ");
  85.  
  86. ENTRY(setjmp)
  87. asm("
  88.     jsr    get_onstack_sigmask
  89.  
  90. setup_setjmp:
  91.     movl    sp@(4),a0    /* save area pointer */
  92.     movl    d1,a0@+        /* save old onstack value */
  93.     movl    d0,a0@+        /* save old signal mask */
  94.     lea    sp@(4),a1    /* adjust saved SP since we won't rts */
  95.     movl    a1,a0@+        /* save old SP */
  96.     movl    a5,a0@+        /* save old FP */
  97.  
  98.     movel    4:w,a1
  99.     movew    a1@(0x126),a0@(2) /* use TDNestCnt and IDNestCnt from Sysbase ! */
  100.     lea    a0@(4),a0    /* skip 4 bytes (the first two bytes used to */
  101.                 /*     contain task specific flags) */
  102.     movl    sp@,a0@+    /* save old PC */
  103.     clrl    a0@+        /* clean PS */
  104.     moveml    d2-d7/a2-a4/a6,a0@    /* save remaining non-scratch regs */
  105.     clrl    d0        /* return 0 */
  106.     rts
  107. ");
  108.  
  109. /* _clean_longjmp, do NOT change onstack/sigmask, do NOT change stackframe */
  110. ENTRY(_clean_longjmp)
  111. asm("
  112.     jsr    get_onstack_sigmask
  113.     movl    sp@(4),a0    /* get area pointer */
  114.     movl    d1,a0@+        /* save current onstack value */
  115.     movl    d0,a0@        /* save current signal mask */
  116.  
  117.     movl    sp@(4),a0    /* save area pointer */
  118.     movel    a0@(8),d0    /* ensure non-zero SP */
  119.     jeq    botch        /* oops! */
  120.     movl    sp@(8),d1    /* grab return value */
  121.     jne    ok2        /* non-zero ok */
  122.     moveq    #1,d1        /* else make non-zero */
  123.     jra    ok2
  124. ");
  125.  
  126. ENTRY(siglongjmp)
  127. asm("
  128.     movl    sp@(4),a0
  129.     addl    #4,a0
  130.     movl    a0,sp@(4)
  131.     tstl    a0@(-4)
  132.     jne    _longjmp
  133. ");
  134.  
  135. /* _longjmp, do NOT change onstack/sigmask */
  136. ENTRY(_longjmp)
  137. asm("
  138.     jsr    get_onstack_sigmask
  139.     movl    sp@(4),a0    /* get area pointer */
  140.     movl    d1,a0@+        /* save current onstack value */
  141.     movl    d0,a0@        /* save current signal mask */
  142.  
  143.     /* fall through */
  144. ");
  145.  
  146. ENTRY(longjmp)
  147. asm("
  148.     movl    sp@(4),a0    /* save area pointer */
  149.     movl    a0@(8),d0    /* ensure non-zero SP */
  150.     jeq    botch        /* oops! */
  151.     movl    sp@(8),d1    /* grab return value */
  152.     jne    ok1        /* non-zero ok */
  153.     moveq    #1,d1        /* else make non-zero */
  154. ok1:
  155.     jbsr    ___stkrst    /* Go to correct stackframe, d0 should */
  156.                 /* contain the SP */
  157. ok2:
  158.     movl    d1,d0
  159.     moveml    a0@(28),d2-d7/a2-a4/a6    /* restore non-scratch regs */
  160.     movl    a0,sp@-        /* let sigreturn */
  161.     jsr    _sigreturn    /*   finish for us */
  162.  
  163. botch:
  164.     jsr    _longjmperror
  165.     stop    #0
  166. ");
  167.