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