4553b66bf25106f039ade178d2720d791838081f
[sbcl.git] / src / compiler / sparc / nlx.lisp
1 ;;;; the definitions of VOPs used for non-local exit (throw, lexical
2 ;;;; exit, etc.)
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!VM")
14
15 ;;; Make an environment-live stack TN for saving the SP for NLX entry.
16 (!def-vm-support-routine make-nlx-sp-tn (env)
17   (physenv-live-tn
18    (make-representation-tn *fixnum-primitive-type* immediate-arg-scn)
19    env))
20
21 ;;; Make a TN for the argument count passing location for a non-local
22 ;;; entry.
23 (!def-vm-support-routine make-nlx-entry-arg-start-location ()
24   (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ocfp-offset))
25 \f
26 ;;; save and restore dynamic environment.
27 ;;;
28 ;;; These VOPs are used in the reentered function to restore the
29 ;;; appropriate dynamic environment.  Currently we only save the
30 ;;; CURRENT-CATCH and binding stack pointer.  We don't need to
31 ;;; save/restore the current UNWIND-PROTECT, since UNWIND-PROTECTs are
32 ;;; implicitly processed during unwinding.  If there were any
33 ;;; additional stacks, then this would be the place to restore the top
34 ;;; pointers.
35
36
37 ;;; Return a list of TNs that can be used to snapshot the dynamic
38 ;;; state for use with the Save/Restore-Dynamic-Environment VOPs.
39 (!def-vm-support-routine make-dynamic-state-tns ()
40   (make-n-tns 4 *backend-t-primitive-type*))
41
42 (define-vop (save-dynamic-state)
43     (:results (catch :scs (descriptor-reg))
44               (nfp :scs (descriptor-reg))
45               (nsp :scs (descriptor-reg)))
46   (:vop-var vop)
47   (:generator 13
48               (load-symbol-value catch *current-catch-block*)
49               (let ((cur-nfp (current-nfp-tn vop)))
50                 (when cur-nfp
51                   (move nfp cur-nfp)))
52               (move nsp nsp-tn)))
53
54 (define-vop (restore-dynamic-state)
55     (:args (catch :scs (descriptor-reg))
56            (nfp :scs (descriptor-reg))
57            (nsp :scs (descriptor-reg)))
58   (:vop-var vop)
59   (:generator 10
60               (store-symbol-value catch *current-catch-block*)
61               (let ((cur-nfp (current-nfp-tn vop)))
62                 (when cur-nfp
63                   (move cur-nfp nfp)))
64               (move nsp-tn nsp)))
65
66 (define-vop (current-stack-pointer)
67     (:results (res :scs (any-reg descriptor-reg)))
68   (:generator 1
69               (move res csp-tn)))
70
71 (define-vop (current-binding-pointer)
72     (:results (res :scs (any-reg descriptor-reg)))
73   (:generator 1
74               (move res bsp-tn)))
75
76 \f
77 ;;;; unwind block hackery:
78
79 ;;; Compute the address of the catch block from its TN, then store
80 ;;; into the block the current Fp, Env, Unwind-Protect, and the entry
81 ;;; PC.
82 (define-vop (make-unwind-block)
83   (:args (tn))
84   (:info entry-label)
85   (:results (block :scs (any-reg)))
86   (:temporary (:scs (descriptor-reg)) temp)
87   (:temporary (:scs (non-descriptor-reg)) ndescr)
88   (:generator 22
89     (inst add block cfp-tn (* (tn-offset tn) n-word-bytes))
90     (load-symbol-value temp *current-unwind-protect-block*)
91     (storew temp block unwind-block-current-uwp-slot)
92     (storew cfp-tn block unwind-block-current-cont-slot)
93     (storew code-tn block unwind-block-current-code-slot)
94     (inst compute-lra-from-code temp code-tn entry-label ndescr)
95     (storew temp block catch-block-entry-pc-slot)))
96
97
98 ;;; Like Make-Unwind-Block, except that we also store in the specified tag, and
99 ;;; link the block into the Current-Catch list.
100 (define-vop (make-catch-block)
101   (:args (tn)
102          (tag :scs (any-reg descriptor-reg)))
103   (:info entry-label)
104   (:results (block :scs (any-reg)))
105   (:temporary (:scs (descriptor-reg)) temp)
106   (:temporary (:scs (descriptor-reg) :target block :to (:result 0)) result)
107   (:temporary (:scs (non-descriptor-reg)) ndescr)
108   (:generator 44
109     (inst add result cfp-tn (* (tn-offset tn) n-word-bytes))
110     (load-symbol-value temp *current-unwind-protect-block*)
111     (storew temp result catch-block-current-uwp-slot)
112     (storew cfp-tn result catch-block-current-cont-slot)
113     (storew code-tn result catch-block-current-code-slot)
114     (inst compute-lra-from-code temp code-tn entry-label ndescr)
115     (storew temp result catch-block-entry-pc-slot)
116
117     (storew tag result catch-block-tag-slot)
118     (load-symbol-value temp *current-catch-block*)
119     (storew temp result catch-block-previous-catch-slot)
120     (store-symbol-value result *current-catch-block*)
121
122     (move block result)))
123
124
125 ;;; Just set the current unwind-protect to TN's address.  This instantiates an
126 ;;; unwind block as an unwind-protect.
127 (define-vop (set-unwind-protect)
128   (:args (tn))
129   (:temporary (:scs (descriptor-reg)) new-uwp)
130   (:generator 7
131     (inst add new-uwp cfp-tn (* (tn-offset tn) n-word-bytes))
132     (store-symbol-value new-uwp *current-unwind-protect-block*)))
133
134
135 (define-vop (unlink-catch-block)
136   (:temporary (:scs (any-reg)) block)
137   (:policy :fast-safe)
138   (:translate %catch-breakup)
139   (:generator 17
140     (load-symbol-value block *current-catch-block*)
141     (loadw block block catch-block-previous-catch-slot)
142     (store-symbol-value block *current-catch-block*)))
143
144 (define-vop (unlink-unwind-protect)
145   (:temporary (:scs (any-reg)) block)
146   (:policy :fast-safe)
147   (:translate %unwind-protect-breakup)
148   (:generator 17
149     (load-symbol-value block *current-unwind-protect-block*)
150     (loadw block block unwind-block-current-uwp-slot)
151     (store-symbol-value block *current-unwind-protect-block*)))
152
153 \f
154 ;;;; NLX entry VOPs:
155
156
157 (define-vop (nlx-entry)
158   (:args (sp) ; Note: we can't list an sc-restriction, 'cause any load vops
159               ; would be inserted before the LRA.
160          (start)
161          (count))
162   (:results (values :more t))
163   (:temporary (:scs (descriptor-reg)) move-temp)
164   (:info label nvals)
165   (:save-p :force-to-stack)
166   (:vop-var vop)
167   (:generator 30
168     (emit-return-pc label)
169     (note-this-location vop :non-local-entry)
170     (cond ((zerop nvals))
171           ((= nvals 1)
172            (let ((no-values (gen-label)))
173              (inst cmp count)
174              (inst b :eq no-values)
175              (move (tn-ref-tn values) null-tn)
176              (loadw (tn-ref-tn values) start)
177              (emit-label no-values)))
178           (t
179            (collect ((defaults))
180              (inst subcc count (fixnumize 1))
181              (do ((i 0 (1+ i))
182                   (tn-ref values (tn-ref-across tn-ref)))
183                  ((null tn-ref))
184                (let ((default-lab (gen-label))
185                      (tn (tn-ref-tn tn-ref)))
186                  (defaults (cons default-lab tn))
187                  
188                  (inst b :lt default-lab)
189                  (inst subcc count (fixnumize 1))
190                  (sc-case tn
191                           ((descriptor-reg any-reg)
192                            (loadw tn start i))
193                           (control-stack
194                            (loadw move-temp start i)
195                            (store-stack-tn tn move-temp)))))
196              
197              (let ((defaulting-done (gen-label)))
198                
199                (emit-label defaulting-done)
200                
201                (assemble (*elsewhere*)
202                  (dolist (def (defaults))
203                    (emit-label (car def))
204                    (let ((tn (cdr def)))
205                      (sc-case tn
206                               ((descriptor-reg any-reg)
207                                (move tn null-tn))
208                               (control-stack
209                                (store-stack-tn tn null-tn)))))
210                  (inst b defaulting-done)
211                  (inst nop))))))
212     (load-stack-tn csp-tn sp)))
213
214
215 (define-vop (nlx-entry-multiple)
216   (:args (top :target result) (src) (count))
217   ;; Again, no SC restrictions for the args, 'cause the loading would
218   ;; happen before the entry label.
219   (:info label)
220   (:temporary (:scs (any-reg)) dst)
221   (:temporary (:scs (descriptor-reg)) temp)
222   (:results (result :scs (any-reg) :from (:argument 0))
223             (num :scs (any-reg) :from (:argument 0)))
224   (:save-p :force-to-stack)
225   (:vop-var vop)
226   (:generator 30
227     (emit-return-pc label)
228     (note-this-location vop :non-local-entry)
229     (let ((loop (gen-label))
230           (done (gen-label)))
231
232       ;; Setup results, and test for the zero value case.
233       (load-stack-tn result top)
234       (inst cmp count)
235       (inst b :eq done)
236       (inst li num 0)
237
238       ;; Compute dst as one slot down from result, because we inc the index
239       ;; before we use it.
240       (inst sub dst result 4)
241
242       ;; Copy stuff down the stack.
243       (emit-label loop)
244       (inst ld temp src num)
245       (inst add num (fixnumize 1))
246       (inst cmp num count)
247       (inst b :ne loop)
248       (inst st temp dst num)
249
250       ;; Reset the CSP.
251       (emit-label done)
252       (inst add csp-tn result num))))
253
254
255 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
256 ;;;
257 (define-vop (uwp-entry)
258   (:info label)
259   (:save-p :force-to-stack)
260   (:results (block) (start) (count))
261   (:ignore block start count)
262   (:vop-var vop)
263   (:generator 0
264     (emit-return-pc label)
265     (note-this-location vop :non-local-entry)))
266