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