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