6f333025b4eac37ba1d4de5c5dd3cc7c0b09f3cd
[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-arg-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 (as e.g. there was an interpreter "eval stack"
35 ;;;; before sbcl-0.7.0), then this would be the place to restore the
36 ;;;; top pointers.
37
38 ;;; Return a list of TNs that can be used to snapshot the dynamic
39 ;;; state for use with the SAVE- and 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   (:vop-var vop)
51   (:generator 13
52     (load-symbol-value catch *current-catch-block*)
53     (let ((cur-nfp (current-nfp-tn vop)))
54       (when cur-nfp
55         (inst mskll cur-nfp 4 nfp)))
56     (inst mskll nsp-tn 4 nsp)))
57
58 (define-vop (restore-dynamic-state)
59   (:args (catch :scs (descriptor-reg))
60          (nfp :scs (descriptor-reg))
61          (nsp :scs (descriptor-reg)))
62   (:vop-var vop)
63   (:temporary (:sc any-reg) temp)
64   (:generator 10
65     (store-symbol-value catch *current-catch-block*)
66     (inst mskll nsp-tn 0 temp)
67     (let ((cur-nfp (current-nfp-tn vop)))
68       (when cur-nfp
69         (inst bis nfp temp cur-nfp)))
70     (inst bis nsp temp nsp-tn)))
71
72 (define-vop (current-stack-pointer)
73   (:results (res :scs (any-reg descriptor-reg)))
74   (:generator 1
75     (move csp-tn res)))
76
77 (define-vop (current-binding-pointer)
78   (:results (res :scs (any-reg descriptor-reg)))
79   (:generator 1
80     (move bsp-tn res)))
81 \f
82 ;;;; unwind block hackery
83
84 ;;; Compute the address of the catch block from its TN, then store
85 ;;; into the block the current Fp, Env, Unwind-Protect, and the entry PC.
86 (define-vop (make-unwind-block)
87   (:args (tn))
88   (:info entry-label)
89   (:results (block :scs (any-reg)))
90   (:temporary (:scs (descriptor-reg)) temp)
91   (:temporary (:scs (non-descriptor-reg)) ndescr)
92   (:generator 22
93     (inst lda block (* (tn-offset tn) sb!vm:n-word-bytes) cfp-tn)
94     (load-symbol-value temp *current-unwind-protect-block*)
95     (storew temp block sb!vm:unwind-block-current-uwp-slot)
96     (storew cfp-tn block sb!vm:unwind-block-current-cont-slot)
97     (storew code-tn block sb!vm:unwind-block-current-code-slot)
98     (inst compute-lra-from-code temp code-tn entry-label ndescr)
99     (storew temp block sb!vm:catch-block-entry-pc-slot)))
100
101
102 ;;; This is like Make-Unwind-Block, except that we also store in the
103 ;;; specified tag, and link the block into the Current-Catch list.
104 (define-vop (make-catch-block)
105   (:args (tn)
106          (tag :scs (any-reg descriptor-reg)))
107   (:info entry-label)
108   (:results (block :scs (any-reg)))
109   (:temporary (:scs (descriptor-reg)) temp)
110   (:temporary (:scs (descriptor-reg) :target block :to (:result 0)) result)
111   (:temporary (:scs (non-descriptor-reg)) ndescr)
112   (:generator 44
113     (inst lda result (* (tn-offset tn) sb!vm:n-word-bytes) cfp-tn)
114     (load-symbol-value temp *current-unwind-protect-block*)
115     (storew temp result sb!vm:catch-block-current-uwp-slot)
116     (storew cfp-tn result sb!vm:catch-block-current-cont-slot)
117     (storew code-tn result sb!vm:catch-block-current-code-slot)
118     (inst compute-lra-from-code temp code-tn entry-label ndescr)
119     (storew temp result sb!vm:catch-block-entry-pc-slot)
120
121     (storew tag result sb!vm:catch-block-tag-slot)
122     (load-symbol-value temp *current-catch-block*)
123     (storew temp result sb!vm:catch-block-previous-catch-slot)
124     (store-symbol-value result *current-catch-block*)
125
126     (move result block)))
127
128 ;;; Just set the current unwind-protect to TN's address. This
129 ;;; instantiates an unwind block as an unwind-protect.
130 (define-vop (set-unwind-protect)
131   (:args (tn))
132   (:temporary (:scs (descriptor-reg)) new-uwp)
133   (:generator 7
134     (inst lda new-uwp (* (tn-offset tn) sb!vm:n-word-bytes) cfp-tn)
135     (store-symbol-value new-uwp *current-unwind-protect-block*)))
136
137 (define-vop (unlink-catch-block)
138   (:temporary (:scs (any-reg)) block)
139   (:policy :fast-safe)
140   (:translate %catch-breakup)
141   (:generator 17
142     (load-symbol-value block *current-catch-block*)
143     (loadw block block sb!vm:catch-block-previous-catch-slot)
144     (store-symbol-value block *current-catch-block*)))
145
146 (define-vop (unlink-unwind-protect)
147   (:temporary (:scs (any-reg)) block)
148   (:policy :fast-safe)
149   (:translate %unwind-protect-breakup)
150   (:generator 17
151     (load-symbol-value block *current-unwind-protect-block*)
152     (loadw block block sb!vm:unwind-block-current-uwp-slot)
153     (store-symbol-value block *current-unwind-protect-block*)))
154 \f
155 ;;;; NLX entry VOPs
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   (:temporary (:sc non-descriptor-reg) temp)
165   (:info label nvals)
166   (:save-p :force-to-stack)
167   (:vop-var vop)
168   (:generator 30
169     (emit-return-pc label)
170     (note-this-location vop :non-local-entry)
171     (cond ((zerop nvals))
172           ((= nvals 1)
173            (let ((no-values (gen-label)))
174              (move null-tn (tn-ref-tn values))
175              (inst beq count no-values)
176              (loadw (tn-ref-tn values) start)
177              (emit-label no-values)))
178           (t
179            (collect ((defaults))
180              (do ((i 0 (1+ i))
181                   (tn-ref values (tn-ref-across tn-ref)))
182                  ((null tn-ref))
183                (let ((default-lab (gen-label))
184                      (tn (tn-ref-tn tn-ref)))
185                  (defaults (cons default-lab tn))
186                  
187                  (inst move count temp)
188                  (inst lda count (fixnumize -1) count)
189                  (inst beq temp default-lab)
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 null-tn tn))
208                               (control-stack
209                                (store-stack-tn tn null-tn)))))
210                  (inst br zero-tn defaulting-done))))))
211     (load-stack-tn csp-tn sp)))
212
213 (define-vop (nlx-entry-multiple)
214   (:args (top :target dst) (start :target src) (count :target num))
215   ;; Again, no SC restrictions for the args, 'cause the loading would
216   ;; happen before the entry label.
217   (:info label)
218   (:temporary (:scs (any-reg) :from (:argument 0)) dst)
219   (:temporary (:scs (any-reg) :from (:argument 1)) src)
220   (:temporary (:scs (any-reg) :from (:argument 2)) num)
221   (:temporary (:scs (descriptor-reg)) temp)
222   (:results (new-start) (new-count))
223   (:save-p :force-to-stack)
224   (:vop-var vop)
225   (:generator 30
226     (emit-return-pc label)
227     (note-this-location vop :non-local-entry)
228     (let ((loop (gen-label))
229           (done (gen-label)))
230
231       ;; Copy args.
232       (load-stack-tn dst top)
233       (move start src)
234       (move count num)
235
236       ;; Establish results.
237       (sc-case new-start
238         (any-reg (move dst new-start))
239         (control-stack (store-stack-tn new-start dst)))
240       (sc-case new-count
241         (any-reg (inst move num new-count))
242         (control-stack (store-stack-tn new-count num)))
243       (inst beq num done)
244
245       ;; Copy stuff on stack.
246       (emit-label loop)
247       (loadw temp src)
248       (inst lda src sb!vm:n-word-bytes src)
249       (storew temp dst)
250       (inst lda num (fixnumize -1) num)
251       (inst lda dst sb!vm:n-word-bytes dst)
252       (inst bne num loop)
253
254       (emit-label done)
255       (inst move dst csp-tn))))
256
257 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
258 (define-vop (uwp-entry)
259   (:info label)
260   (:save-p :force-to-stack)
261   (:results (block) (start) (count))
262   (:ignore block start count)
263   (:vop-var vop)
264   (:generator 0
265     (emit-return-pc label)
266     (note-this-location vop :non-local-entry)))