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