0.7.4.19:
[sbcl.git] / src / compiler / ppc / nlx.lisp
1 ;;; Written by Rob MacLachlan
2 ;;;
3 (in-package "SB!VM")
4
5 ;;; MAKE-NLX-SP-TN  --  Interface
6 ;;;
7 ;;;    Make an environment-live stack TN for saving the SP for NLX entry.
8 ;;;
9 (!def-vm-support-routine make-nlx-sp-tn (env)
10   (physenv-live-tn
11    (make-representation-tn *fixnum-primitive-type* immediate-arg-scn)
12    env))
13
14 ;;; Make-NLX-Entry-Arg-Start-Location  --  Interface
15 ;;;
16 ;;;    Make a TN for the argument count passing location for a
17 ;;; non-local entry.
18 ;;;
19 (!def-vm-support-routine make-nlx-entry-arg-start-location ()
20   (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ocfp-offset))
21
22 \f
23 ;;; Save and restore dynamic environment.
24 ;;;
25 ;;;    These VOPs are used in the reentered function to restore the appropriate
26 ;;; dynamic environment.  Currently we only save the Current-Catch and binding
27 ;;; stack pointer.  We don't need to save/restore the current unwind-protect,
28 ;;; since unwind-protects are implicitly processed during unwinding.  If there
29 ;;; were any additional stacks, then this would be the place to restore the top
30 ;;; pointers.
31
32
33 ;;; Make-Dynamic-State-TNs  --  Interface
34 ;;;
35 ;;;    Return a list of TNs that can be used to snapshot the dynamic state for
36 ;;; use with the Save/Restore-Dynamic-Environment VOPs.
37 ;;;
38 (!def-vm-support-routine make-dynamic-state-tns ()
39   (make-n-tns 4 *backend-t-primitive-type*))
40
41 (define-vop (save-dynamic-state)
42   (:results (catch :scs (descriptor-reg))
43             (nfp :scs (descriptor-reg))
44             (nsp :scs (descriptor-reg)))
45   (:vop-var vop)
46   (:generator 13
47     (load-symbol-value catch *current-catch-block*)
48     (let ((cur-nfp (current-nfp-tn vop)))
49       (when cur-nfp
50         (move nfp cur-nfp)))
51     (move nsp nsp-tn)))
52
53 (define-vop (restore-dynamic-state)
54   (:args (catch :scs (descriptor-reg))
55          (nfp :scs (descriptor-reg))
56          (nsp :scs (descriptor-reg)))
57   (:vop-var vop)
58   (:generator 10
59     (store-symbol-value catch *current-catch-block*)
60     (let ((cur-nfp (current-nfp-tn vop)))
61       (when cur-nfp
62         (move cur-nfp nfp)))
63     (move nsp-tn nsp)))
64
65 (define-vop (current-stack-pointer)
66   (:results (res :scs (any-reg descriptor-reg)))
67   (:generator 1
68     (move res csp-tn)))
69
70 (define-vop (current-binding-pointer)
71   (:results (res :scs (any-reg descriptor-reg)))
72   (:generator 1
73     (move res bsp-tn)))
74
75
76 \f
77 ;;;; Unwind block hackery:
78
79 ;;; Compute the address of the catch block from its TN, then store into the
80 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
81 ;;;
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 addi block cfp-tn (* (tn-offset tn) sb!vm:n-word-bytes))
90     (load-symbol-value temp *current-unwind-protect-block*)
91     (storew temp block sb!vm:unwind-block-current-uwp-slot)
92     (storew cfp-tn block sb!vm:unwind-block-current-cont-slot)
93     (storew code-tn block sb!vm:unwind-block-current-code-slot)
94     (inst compute-lra-from-code temp code-tn entry-label ndescr)
95     (storew temp block sb!vm: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 ;;;
101 (define-vop (make-catch-block)
102   (:args (tn)
103          (tag :scs (any-reg descriptor-reg)))
104   (:info entry-label)
105   (:results (block :scs (any-reg)))
106   (:temporary (:scs (descriptor-reg)) temp)
107   (:temporary (:scs (descriptor-reg) :target block :to (:result 0)) result)
108   (:temporary (:scs (non-descriptor-reg)) ndescr)
109   (:generator 44
110     (inst addi result cfp-tn (* (tn-offset tn) sb!vm:n-word-bytes))
111     (load-symbol-value temp *current-unwind-protect-block*)
112     (storew temp result sb!vm:catch-block-current-uwp-slot)
113     (storew cfp-tn result sb!vm:catch-block-current-cont-slot)
114     (storew code-tn result sb!vm:catch-block-current-code-slot)
115     (inst compute-lra-from-code temp code-tn entry-label ndescr)
116     (storew temp result sb!vm:catch-block-entry-pc-slot)
117
118     (storew tag result sb!vm:catch-block-tag-slot)
119     (load-symbol-value temp *current-catch-block*)
120     (storew temp result sb!vm:catch-block-previous-catch-slot)
121     (store-symbol-value result *current-catch-block*)
122
123     (move block result)))
124
125
126 ;;; Just set the current unwind-protect to TN's address.  This instantiates an
127 ;;; unwind block as an unwind-protect.
128 ;;;
129 (define-vop (set-unwind-protect)
130   (:args (tn))
131   (:temporary (:scs (descriptor-reg)) new-uwp)
132   (:generator 7
133     (inst addi new-uwp cfp-tn (* (tn-offset tn) sb!vm:n-word-bytes))
134     (store-symbol-value new-uwp *current-unwind-protect-block*)))
135
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
155 \f
156 ;;;; NLX entry VOPs:
157
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   (:info label nvals)
167   (:save-p :force-to-stack)
168   (:vop-var vop)
169   (:generator 30
170     (emit-return-pc label)
171     (note-this-location vop :non-local-entry)
172     (cond ((zerop nvals))
173           ((= nvals 1)
174            (let ((no-values (gen-label)))
175              (inst cmpwi count 0)
176              (move (tn-ref-tn values) null-tn)
177              (inst beq no-values)
178              (loadw (tn-ref-tn values) start)
179              (emit-label no-values)))
180           (t
181            (collect ((defaults))
182              (inst addic. count count (- (fixnumize 1)))
183              (do ((i 0 (1+ i))
184                   (tn-ref values (tn-ref-across tn-ref)))
185                  ((null tn-ref))
186                (let ((default-lab (gen-label))
187                      (tn (tn-ref-tn tn-ref)))
188                  (defaults (cons default-lab tn))
189                  
190                  (inst subi count count (fixnumize 1))
191                  (inst blt 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                  (inst cmpwi count 0)))
199              
200              (let ((defaulting-done (gen-label)))
201                
202                (emit-label defaulting-done)
203                
204                (assemble (*elsewhere*)
205                  (dolist (def (defaults))
206                    (emit-label (car def))
207                    (let ((tn (cdr def)))
208                      (sc-case tn
209                               ((descriptor-reg any-reg)
210                                (move tn null-tn))
211                               (control-stack
212                                (store-stack-tn tn null-tn)))))
213                  (inst b defaulting-done))))))
214     (load-stack-tn csp-tn sp)))
215
216
217 (define-vop (nlx-entry-multiple)
218   (:args (top :target result) (src) (count))
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)) dst)
223   (:temporary (:scs (descriptor-reg)) temp)
224   (:results (result :scs (any-reg) :from (:argument 0))
225             (num :scs (any-reg) :from (:argument 0)))
226   (:save-p :force-to-stack)
227   (:vop-var vop)
228   (:generator 30
229     (emit-return-pc label)
230     (note-this-location vop :non-local-entry)
231     (let ((loop (gen-label))
232           (done (gen-label)))
233
234       ;; Setup results, and test for the zero value case.
235       (load-stack-tn result top)
236       (inst cmpwi count 0)
237       (inst li num 0)
238       (inst beq done)
239
240       ;; Compute dst as one slot down from result, because we inc the index
241       ;; before we use it.
242       (inst subi dst result 4)
243
244       ;; Copy stuff down the stack.
245       (emit-label loop)
246       (inst lwzx temp src num)
247       (inst addi num num (fixnumize 1))
248       (inst cmpw num count)
249       (inst stwx temp dst num)
250       (inst bne loop)
251
252       ;; Reset the CSP.
253       (emit-label done)
254       (inst add csp-tn result num))))
255
256
257 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
258 ;;;
259 (define-vop (uwp-entry)
260   (:info label)
261   (:save-p :force-to-stack)
262   (:results (block) (start) (count))
263   (:ignore block start count)
264   (:vop-var vop)
265   (:generator 0
266     (emit-return-pc label)
267     (note-this-location vop :non-local-entry)))
268