c382da72411ffb5fd8205628617ff8a61c7698d1
[sbcl.git] / src / compiler / x86 / nlx.lisp
1 ;;;; the definition of non-local exit for the x86 VM
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13
14 ;;; Make an environment-live stack TN for saving the SP for NLX entry.
15 (!def-vm-support-routine make-nlx-sp-tn (env)
16   (physenv-live-tn
17    (make-representation-tn *fixnum-primitive-type* any-reg-sc-number)
18    env))
19
20 ;;; Make a TN for the argument count passing location for a non-local entry.
21 (!def-vm-support-routine make-nlx-entry-arg-start-location ()
22   (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ebx-offset))
23
24 (defun catch-block-ea (tn)
25   (aver (sc-is tn catch-block))
26   (make-ea :dword :base ebp-tn
27            :disp (- (* (+ (tn-offset tn) catch-block-size) n-word-bytes))))
28
29 \f
30 ;;;; Save and restore dynamic environment.
31 ;;;;
32 ;;;; These VOPs are used in the reentered function to restore the
33 ;;;; appropriate dynamic environment. Currently we only save the
34 ;;;; Current-Catch and the alien stack pointer. (Before sbcl-0.7.0,
35 ;;;; when there were IR1 and byte interpreters, we had to save
36 ;;;; the interpreter "eval stack" too.)
37 ;;;;
38 ;;;; We don't need to save/restore the current UNWIND-PROTECT, since
39 ;;;; UNWIND-PROTECTs are implicitly processed during unwinding.
40 ;;;;
41 ;;;; We don't need to save the BSP, because that is handled automatically.
42
43 ;;; Return a list of TNs that can be used to snapshot the dynamic
44 ;;; state for use with the SAVE- and RESTORE-DYNAMIC-ENVIRONMENT VOPs.
45 (!def-vm-support-routine make-dynamic-state-tns ()
46   (make-n-tns 3 *backend-t-primitive-type*))
47
48 (define-vop (save-dynamic-state)
49   (:results (catch :scs (descriptor-reg))
50             (eval :scs (descriptor-reg))
51             (alien-stack :scs (descriptor-reg)))
52   (:generator 13
53     (load-symbol-value catch *current-catch-block*)
54     (load-symbol-value alien-stack *alien-stack*)))
55
56 (define-vop (restore-dynamic-state)
57   (:args (catch :scs (descriptor-reg))
58          (eval :scs (descriptor-reg))
59          (alien-stack :scs (descriptor-reg)))
60   (:generator 10
61     (store-symbol-value catch *current-catch-block*)
62     (store-symbol-value alien-stack *alien-stack*)))
63
64 (define-vop (current-stack-pointer)
65   (:results (res :scs (any-reg control-stack)))
66   (:generator 1
67     (move res esp-tn)))
68
69 (define-vop (current-binding-pointer)
70   (:results (res :scs (any-reg descriptor-reg)))
71   (:generator 1
72     (load-symbol-value res *binding-stack-pointer*)))
73 \f
74 ;;;; unwind block hackery
75
76 ;;; Compute the address of the catch block from its TN, then store into the
77 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
78 (define-vop (make-unwind-block)
79   (:args (tn))
80   (:info entry-label)
81   (:temporary (:sc unsigned-reg) temp)
82   (:results (block :scs (any-reg)))
83   (:generator 22
84     (inst lea block (catch-block-ea tn))
85     (load-symbol-value temp *current-unwind-protect-block*)
86     (storew temp block unwind-block-current-uwp-slot)
87     (storew ebp-tn block unwind-block-current-cont-slot)
88     (storew (make-fixup nil :code-object entry-label)
89             block catch-block-entry-pc-slot)))
90
91 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
92 ;;; tag, and link the block into the CURRENT-CATCH list
93 (define-vop (make-catch-block)
94   (:args (tn)
95          (tag :scs (any-reg descriptor-reg) :to (:result 1)))
96   (:info entry-label)
97   (:results (block :scs (any-reg)))
98   (:temporary (:sc descriptor-reg) temp)
99   (:generator 44
100     (inst lea block (catch-block-ea tn))
101     (load-symbol-value temp *current-unwind-protect-block*)
102     (storew temp block  unwind-block-current-uwp-slot)
103     (storew ebp-tn block  unwind-block-current-cont-slot)
104     (storew (make-fixup nil :code-object entry-label)
105             block catch-block-entry-pc-slot)
106     (storew tag block catch-block-tag-slot)
107     (load-symbol-value temp *current-catch-block*)
108     (storew temp block catch-block-previous-catch-slot)
109     (store-symbol-value block *current-catch-block*)))
110
111 ;;; Just set the current unwind-protect to TN's address. This instantiates an
112 ;;; unwind block as an unwind-protect.
113 (define-vop (set-unwind-protect)
114   (:args (tn))
115   (:temporary (:sc unsigned-reg) new-uwp)
116   (:generator 7
117     (inst lea new-uwp (catch-block-ea tn))
118     (store-symbol-value new-uwp *current-unwind-protect-block*)))
119
120 (define-vop (unlink-catch-block)
121   (:temporary (:sc unsigned-reg) block)
122   (:policy :fast-safe)
123   (:translate %catch-breakup)
124   (:generator 17
125     (load-symbol-value block *current-catch-block*)
126     (loadw block block catch-block-previous-catch-slot)
127     (store-symbol-value block *current-catch-block*)))
128
129 (define-vop (unlink-unwind-protect)
130     (:temporary (:sc unsigned-reg) block)
131   (:policy :fast-safe)
132   (:translate %unwind-protect-breakup)
133   (:generator 17
134     (load-symbol-value block *current-unwind-protect-block*)
135     (loadw block block unwind-block-current-uwp-slot)
136     (store-symbol-value block *current-unwind-protect-block*)))
137 \f
138 ;;;; NLX entry VOPs
139 (define-vop (nlx-entry)
140   ;; Note: we can't list an sc-restriction, 'cause any load vops would
141   ;; be inserted before the return-pc label.
142   (:args (sp)
143          (start)
144          (count))
145   (:results (values :more t))
146   (:temporary (:sc descriptor-reg) move-temp)
147   (:info label nvals)
148   (:save-p :force-to-stack)
149   (:vop-var vop)
150   (:generator 30
151     (emit-label label)
152     (note-this-location vop :non-local-entry)
153     (cond ((zerop nvals))
154           ((= nvals 1)
155            (let ((no-values (gen-label)))
156              (inst mov (tn-ref-tn values) nil-value)
157              (inst jecxz no-values)
158              (loadw (tn-ref-tn values) start -1)
159              (emit-label no-values)))
160           (t
161            (collect ((defaults))
162              (do ((i 0 (1+ i))
163                   (tn-ref values (tn-ref-across tn-ref)))
164                  ((null tn-ref))
165                (let ((default-lab (gen-label))
166                      (tn (tn-ref-tn tn-ref)))
167                  (defaults (cons default-lab tn))
168
169                  (inst cmp count (fixnumize i))
170                  (inst jmp :le default-lab)
171                  (sc-case tn
172                    ((descriptor-reg any-reg)
173                     (loadw tn start (- (1+ i))))
174                    ((control-stack)
175                     (loadw move-temp start (- (1+ i)))
176                     (inst mov tn move-temp)))))
177              (let ((defaulting-done (gen-label)))
178                (emit-label defaulting-done)
179                (assemble (*elsewhere*)
180                  (dolist (def (defaults))
181                    (emit-label (car def))
182                    (inst mov (cdr def) nil-value))
183                  (inst jmp defaulting-done))))))
184     (inst mov esp-tn sp)))
185
186 (define-vop (nlx-entry-multiple)
187   (:args (top)
188          (source)
189          (count :target ecx))
190   ;; Again, no SC restrictions for the args, 'cause the loading would
191   ;; happen before the entry label.
192   (:info label)
193   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
194   (:temporary (:sc unsigned-reg :offset esi-offset) esi)
195   (:temporary (:sc unsigned-reg :offset edi-offset) edi)
196   (:results (result :scs (any-reg) :from (:argument 0))
197             (num :scs (any-reg control-stack)))
198   (:save-p :force-to-stack)
199   (:vop-var vop)
200   (:generator 30
201     (emit-label label)
202     (note-this-location vop :non-local-entry)
203
204     (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
205     ;; The 'top' arg contains the %esp value saved at the time the
206     ;; catch block was created and points to where the thrown values
207     ;; should sit.
208     (move edi top)
209     (move result edi)
210
211     (inst sub edi n-word-bytes)
212     (move ecx count)                    ; fixnum words == bytes
213     (move num ecx)
214     (inst shr ecx word-shift)           ; word count for <rep movs>
215     ;; If we got zero, we be done.
216     (inst jecxz done)
217     ;; Copy them down.
218     (inst std)
219     (inst rep)
220     (inst movs :dword)
221
222     DONE
223     ;; Reset the CSP at last moved arg.
224     (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
225
226
227 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
228 (define-vop (uwp-entry)
229   (:info label)
230   (:save-p :force-to-stack)
231   (:results (block) (start) (count))
232   (:ignore block start count)
233   (:vop-var vop)
234   (:generator 0
235     (emit-label label)
236     (note-this-location vop :non-local-entry)))