home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / compiler / rt / nlx.lisp < prev    next >
Encoding:
Text File  |  1992-01-01  |  9.3 KB  |  303 lines

  1. ;;; -*- Package: RT -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the Spice Lisp project at
  5. ;;; Carnegie-Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of Spice Lisp, please contact
  7. ;;; Scott Fahlman (FAHLMAN@CMUC). 
  8. ;;; **********************************************************************
  9. ;;;
  10. ;;; $Header: nlx.lisp,v 1.4 92/01/01 15:07:09 ram Exp $
  11. ;;;
  12. ;;; This file contains the definitions of VOPs used for non-local exit (throw,
  13. ;;; lexical exit, etc.)
  14. ;;;
  15. ;;; Written by Rob MacLachlan
  16. ;;; Converted to IBM RT by William Lott and Bill Chiles.
  17. ;;;
  18.  
  19. (in-package "RT")
  20.  
  21.  
  22. ;;; MAKE-NLX-SP-TN  --  Interface.
  23. ;;;
  24. ;;; Make an environment-live stack TN for saving the SP for NLX entry.
  25. ;;;
  26. (def-vm-support-routine make-nlx-sp-tn (env)
  27.   (environment-live-tn
  28.    (make-representation-tn *word-pointer-type*
  29.                (sc-number-or-lose 'word-pointer-reg *backend*))
  30.    env))
  31.  
  32.  
  33.  
  34. ;;; Save and restore dynamic environment.
  35. ;;;
  36. ;;;    These VOPs are used in the reentered function to restore the appropriate
  37. ;;; dynamic environment.  Currently we only save the Current-Catch and binding
  38. ;;; stack pointer.  We don't need to save/restore the current unwind-protect,
  39. ;;; since unwind-protects are implicitly processed during unwinding.  If there
  40. ;;; were any additional stacks, then this would be the place to restore the top
  41. ;;; pointers.
  42.  
  43.  
  44. ;;; MAKE-DYNAMIC-STATE-TNS  --  Interface.
  45. ;;;
  46. ;;; Return a list of TNs that can be used to snapshot the dynamic state for use
  47. ;;; with the Save/Restore-Dynamic-Environment VOPs.
  48. ;;;
  49. (def-vm-support-routine make-dynamic-state-tns ()
  50.   (make-n-tns 4 *fixnum-primitive-type*))
  51.  
  52. (define-vop (save-dynamic-state)
  53.   (:results (catch :scs (any-reg))
  54.         (nfp :scs (any-reg))
  55.         (nsp :scs (any-reg))
  56.         (eval :scs (any-reg)))
  57.   (:vop-var vop)
  58.   (:generator 13
  59.     (load-symbol-value catch lisp::*current-catch-block*)
  60.     (let ((cur-nfp (current-nfp-tn vop)))
  61.       (if cur-nfp
  62.       (move nfp cur-nfp)
  63.       (inst li nfp 0)))
  64.     (move nsp nsp-tn)
  65.     (load-symbol-value eval lisp::*eval-stack-top*)))
  66.  
  67. (define-vop (restore-dynamic-state)
  68.   (:args (catch :scs (any-reg))
  69.      (nfp :scs (any-reg))
  70.      (nsp :scs (any-reg))
  71.      (eval :scs (any-reg)))
  72.   (:temporary (:scs (descriptor-reg) :from (:eval 0)) symbol value)
  73.   (:temporary (:scs (word-pointer-reg) :from (:eval 0)) bsp)
  74.   (:vop-var vop)
  75.   (:generator 10
  76.     (store-symbol-value catch lisp::*current-catch-block*)
  77.     (store-symbol-value eval lisp::*eval-stack-top*)
  78.     (let ((cur-nfp (current-nfp-tn vop)))
  79.       (when cur-nfp
  80.     (move cur-nfp nfp)))
  81.     (move nsp-tn nsp)))
  82.  
  83. (define-vop (current-stack-pointer)
  84.   (:results (res :scs (any-reg word-pointer-reg descriptor-reg)))
  85.   (:generator 1
  86.     (move res csp-tn)))
  87.  
  88. (define-vop (current-binding-pointer)
  89.   (:results (res :scs (any-reg word-pointer-reg descriptor-reg)))
  90.   (:generator 1
  91.     (load-symbol-value res *binding-stack-pointer*)))
  92.  
  93.  
  94.  
  95. ;;;; Unwind block hackery:
  96.  
  97. ;;; MAKE-UNWIND-BLOCK -- VOP.
  98. ;;;
  99. ;;; Compute the address of the catch block from its TN, then store into the
  100. ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
  101. ;;;
  102. (define-vop (make-unwind-block)
  103.   (:args (tn))
  104.   (:info entry-label)
  105.   (:results (block :scs (word-pointer-reg)))
  106.   (:temporary (:scs (word-pointer-reg) :target block) block-ptr)
  107.   (:temporary (:scs (descriptor-reg)) temp)
  108.   (:generator 22
  109.     (inst cal block-ptr cfp-tn (* (tn-offset tn) vm:word-bytes))
  110.     (load-symbol-value temp lisp::*current-unwind-protect-block*)
  111.     (storew temp block-ptr vm:unwind-block-current-uwp-slot)
  112.     (storew cfp-tn block-ptr vm:unwind-block-current-cont-slot)
  113.     (storew code-tn block-ptr vm:unwind-block-current-code-slot)
  114.     (inst compute-lra-from-code temp code-tn entry-label)
  115.     (storew temp block-ptr vm:catch-block-entry-pc-slot)
  116.     (move block block-ptr)))
  117.  
  118.  
  119. ;;; MAKE-CATCH-BLOCK -- VOP.
  120. ;;;
  121. ;;; Like MAKE-UNWIND-BLOCK, except that we also store in the specified tag, and
  122. ;;; link the block into the Current-Catch list.
  123. ;;;
  124. (define-vop (make-catch-block)
  125.   (:args (tn)
  126.      (tag :scs (descriptor-reg)))
  127.   (:info entry-label)
  128.   (:results (block :scs (word-pointer-reg)))
  129.   (:temporary (:scs (descriptor-reg)) temp)
  130.   (:temporary (:scs (word-pointer-reg) :target block :to (:result 0)) result)
  131.   (:generator 44
  132.     (inst cal result cfp-tn (* (tn-offset tn) vm:word-bytes))
  133.     (load-symbol-value temp lisp::*current-unwind-protect-block*)
  134.     (storew temp result vm:catch-block-current-uwp-slot)
  135.     (storew cfp-tn result vm:catch-block-current-cont-slot)
  136.     (storew code-tn result vm:catch-block-current-code-slot)
  137.     (inst compute-lra-from-code temp code-tn entry-label)
  138.     (storew temp result vm:catch-block-entry-pc-slot)
  139.  
  140.     (storew tag result vm:catch-block-tag-slot)
  141.     (load-symbol-value temp lisp::*current-catch-block*)
  142.     (storew temp result vm:catch-block-previous-catch-slot)
  143.     (store-symbol-value result lisp::*current-catch-block*)
  144.  
  145.     (move block result)))
  146.  
  147.  
  148. ;;; SET-UNWIND-PROTECT -- VOP.
  149. ;;;
  150. ;;; Just set the current unwind-protect to TN's address.  This instantiates an
  151. ;;; unwind block as an unwind-protect.
  152. ;;;
  153. (define-vop (set-unwind-protect)
  154.   (:args (tn))
  155.   (:temporary (:scs (descriptor-reg)) new-uwp)
  156.   (:generator 7
  157.     (inst cal new-uwp cfp-tn (* (tn-offset tn) vm:word-bytes))
  158.     (store-symbol-value new-uwp lisp::*current-unwind-protect-block*)))
  159.  
  160. ;;; UNLINK-CATCH-BLOCK -- VOP.
  161. ;;;
  162. ;;; Remove the catch block from the chain of catches.  This happens when
  163. ;;; we drop out of a catch instead of throwing.
  164. ;;; 
  165. (define-vop (unlink-catch-block)
  166.   (:temporary (:scs (word-pointer-reg)) block)
  167.   (:policy :fast-safe)
  168.   (:translate %catch-breakup)
  169.   (:generator 17
  170.     (load-symbol-value block lisp::*current-catch-block*)
  171.     (loadw block block vm:catch-block-previous-catch-slot)
  172.     (store-symbol-value block lisp::*current-catch-block*)))
  173.  
  174. ;;; UNLINK-UNWIND-PROTECT -- VOP.
  175. ;;;
  176. ;;; Same thing with unwind protects.
  177. ;;; 
  178. (define-vop (unlink-unwind-protect)
  179.   (:temporary (:scs (word-pointer-reg)) block)
  180.   (:policy :fast-safe)
  181.   (:translate %unwind-protect-breakup)
  182.   (:generator 17
  183.     (load-symbol-value block lisp::*current-unwind-protect-block*)
  184.     (loadw block block vm:unwind-block-current-uwp-slot)
  185.     (store-symbol-value block lisp::*current-unwind-protect-block*)))
  186.  
  187.  
  188. ;;;; NLX entry VOPs:
  189.  
  190.  
  191. ;;; NLX-ENTRY -- VOP.
  192. ;;;
  193. ;;; We were just thrown to, so load up the results.
  194. ;;; 
  195. (define-vop (nlx-entry)
  196.   (:args (sp) ; Note: we can't list an sc-restriction, 'cause any load vops
  197.           ; would be inserted before the LRA.
  198.      (start)
  199.      (count))
  200.   (:results (values :more t))
  201.   (:temporary (:scs (descriptor-reg)) move-temp)
  202.   (:info label nvals)
  203.   (:save-p :force-to-stack)
  204.   (:generator 30
  205.     (emit-return-pc label)
  206.     (cond ((zerop nvals))
  207.       ((= nvals 1)
  208.        (let ((no-values (gen-label)))
  209.          (inst c count 0)
  210.          (inst bcx :eq no-values)
  211.          (move (tn-ref-tn values) null-tn)
  212.          (loadw (tn-ref-tn values) start)
  213.          (emit-label no-values)))
  214.       (t
  215.        (collect ((defaults))
  216.          (inst c count 0)
  217.          (do ((i 0 (1+ i))
  218.           (tn-ref values (tn-ref-across tn-ref)))
  219.          ((null tn-ref))
  220.            (let ((default-lab (gen-label))
  221.              (tn (tn-ref-tn tn-ref)))
  222.          (defaults (cons default-lab tn))
  223.          
  224.          (inst bc :eq default-lab)
  225.          (inst s count (fixnum 1))
  226.          (sc-case tn
  227.            ((descriptor-reg any-reg)
  228.             (loadw tn start i))
  229.            (control-stack
  230.             (loadw move-temp start i)
  231.             (store-stack-tn move-temp tn)))))
  232.          
  233.          (let ((defaulting-done (gen-label)))
  234.            (emit-label defaulting-done)
  235.            (assemble (*elsewhere*)
  236.          (dolist (def (defaults))
  237.            (emit-label (car def))
  238.            (let ((tn (cdr def)))
  239.              (sc-case tn
  240.                ((descriptor-reg any-reg)
  241.             (move tn null-tn))
  242.                (control-stack
  243.             (store-stack-tn null-tn tn)))))
  244.          (inst b defaulting-done))))))
  245.     (load-stack-tn csp-tn sp)))
  246.  
  247.  
  248. (define-vop (nlx-entry-multiple)
  249.   ;; Again, no SC restrictions for the args, 'cause the loading would
  250.   ;; happen before the entry label.  But we know that start and count will
  251.   ;; be in registers due to the way this vop is used.
  252.   (:args (top :target dst)
  253.      (start :target src)
  254.      (count :target num))
  255.   (:info label)
  256.   (:temporary (:scs (any-reg) :from (:argument 0)) dst)
  257.   (:temporary (:scs (any-reg) :from (:argument 1)) src)
  258.   (:temporary (:scs (any-reg) :from (:argument 2)) num)
  259.   (:temporary (:scs (descriptor-reg)) temp)
  260.   (:results (new-start) (new-count))
  261.   (:save-p :force-to-stack)
  262.   (:generator 30
  263.     (emit-return-pc label)
  264.     (let ((loop (gen-label))
  265.       (done (gen-label)))
  266.  
  267.       ;; Copy args.
  268.       (load-stack-tn dst top)
  269.       (move src start)
  270.       (inst a num count 0)
  271.  
  272.       ;; Establish results.
  273.       (sc-case new-start
  274.     ((any-reg word-pointer-reg) (move new-start dst))
  275.     (control-stack (store-stack-tn dst new-start)))
  276.       (inst bcx :eq done)
  277.       (sc-case new-count
  278.     (any-reg (inst move new-count num))
  279.     (control-stack (store-stack-tn num new-count)))
  280.  
  281.       ;; Copy stuff on stack.
  282.       (emit-label loop)
  283.       (loadw temp src)
  284.       (inst inc src vm:word-bytes)
  285.       (storew temp dst)
  286.       (inst s num num (fixnum 1))
  287.       (inst bncx :eq loop)
  288.       (inst inc dst vm:word-bytes)
  289.  
  290.       (emit-label done)
  291.       (move csp-tn dst))))
  292.  
  293.  
  294. ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
  295. ;;;
  296. (define-vop (uwp-entry)
  297.   (:info label)
  298.   (:save-p :force-to-stack)
  299.   (:results (block) (start) (count))
  300.   (:ignore block start count)
  301.   (:generator 0
  302.     (emit-return-pc label)))
  303.