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