1 ;;;; the definition of non-local exit for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;; Make a TN for the argument count passing location for a non-local entry.
15 (defun make-nlx-entry-arg-start-location ()
16 (make-wired-tn *fixnum-primitive-type
* any-reg-sc-number ebx-offset
))
18 (defun catch-block-ea (tn)
19 (aver (sc-is tn catch-block
))
20 (make-ea :dword
:base ebp-tn
21 :disp
(frame-byte-offset (+ -
1 (tn-offset tn
) catch-block-size
))))
23 (defun unwind-block-ea (tn)
24 (aver (sc-is tn unwind-block
))
25 (make-ea :dword
:base ebp-tn
26 :disp
(frame-byte-offset (+ -
1 (tn-offset tn
) unwind-block-size
))))
29 ;;;; Save and restore dynamic environment.
31 ;;;; These VOPs are used in the reentered function to restore the
32 ;;;; appropriate dynamic environment. Currently we only save the
33 ;;;; Current-Catch. (Before sbcl-0.7.0,
34 ;;;; when there were IR1 and byte interpreters, we had to save
35 ;;;; the interpreter "eval stack" too.)
37 ;;;; We don't need to save/restore the current UNWIND-PROTECT, since
38 ;;;; UNWIND-PROTECTs are implicitly processed during unwinding.
40 ;;;; We don't need to save the BSP, because that is handled automatically.
42 (define-vop (save-dynamic-state)
43 (:results
(catch :scs
(descriptor-reg)))
45 (load-tl-symbol-value catch
*current-catch-block
*)))
47 (define-vop (restore-dynamic-state)
48 (:args
(catch :scs
(descriptor-reg)))
49 #!+sb-thread
(:temporary
(:sc unsigned-reg
) temp
)
51 (store-tl-symbol-value catch
*current-catch-block
* temp
)))
53 (define-vop (current-stack-pointer)
54 (:results
(res :scs
(any-reg control-stack
)))
58 (define-vop (current-binding-pointer)
59 (:results
(res :scs
(any-reg descriptor-reg
)))
61 (load-binding-stack-pointer res
)))
63 ;;;; unwind block hackery
65 ;;; Compute the address of the catch block from its TN, then store into the
66 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
67 (define-vop (make-unwind-block)
70 (:temporary
(:sc unsigned-reg
) temp
)
71 (:results
(block :scs
(any-reg)))
73 (inst lea block
(unwind-block-ea tn
))
74 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
75 (storew temp block unwind-block-uwp-slot
)
76 (storew ebp-tn block unwind-block-cfp-slot
)
77 (storew (make-fixup nil
:code-object entry-label
)
78 block catch-block-entry-pc-slot
)
81 (inst mov temp
(make-ea :dword
:disp
0) :fs
)
82 (storew temp block unwind-block-next-seh-frame-slot
))))
84 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
85 ;;; tag, and link the block into the CURRENT-CATCH list
86 (define-vop (make-catch-block)
88 (tag :scs
(any-reg descriptor-reg
) :to
(:result
1)))
90 (:results
(block :scs
(any-reg)))
91 (:temporary
(:sc descriptor-reg
) temp
)
93 (inst lea block
(catch-block-ea tn
))
94 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
95 (storew temp block unwind-block-uwp-slot
)
96 (storew ebp-tn block unwind-block-cfp-slot
)
97 (storew (make-fixup nil
:code-object entry-label
)
98 block catch-block-entry-pc-slot
)
101 (inst mov temp
(make-ea :dword
:disp
0) :fs
)
102 (storew temp block unwind-block-next-seh-frame-slot
))
103 (storew tag block catch-block-tag-slot
)
104 (load-tl-symbol-value temp
*current-catch-block
*)
105 (storew temp block catch-block-previous-catch-slot
)
106 (store-tl-symbol-value block
*current-catch-block
* temp
)))
108 ;;; Just set the current unwind-protect to TN's address. This instantiates an
109 ;;; unwind block as an unwind-protect.
110 (define-vop (set-unwind-protect)
112 (:temporary
(:sc unsigned-reg
) new-uwp
#!+sb-thread tls
#!+win32 seh-frame
)
114 (inst lea new-uwp
(unwind-block-ea tn
))
117 (storew (make-fixup 'uwp-seh-handler
:assembly-routine
)
118 new-uwp unwind-block-seh-frame-handler-slot
)
120 (make-ea-for-object-slot new-uwp
121 unwind-block-next-seh-frame-slot
0))
122 (inst mov
(make-ea :dword
:disp
0) seh-frame
:fs
))
123 (store-tl-symbol-value new-uwp
*current-unwind-protect-block
* tls
)))
125 (define-vop (unlink-catch-block)
126 (:temporary
(:sc unsigned-reg
) #!+sb-thread tls block
)
128 (:translate %catch-breakup
)
130 (load-tl-symbol-value block
*current-catch-block
*)
131 (loadw block block catch-block-previous-catch-slot
)
132 (store-tl-symbol-value block
*current-catch-block
* tls
)))
134 (define-vop (unlink-unwind-protect)
135 ;; NOTE: When we have both #!+sb-thread and #!+win32, we only need one temp
136 (:temporary
(:sc unsigned-reg
) block
#!+sb-thread tls
#!+win32 seh-frame
)
138 (:translate %unwind-protect-breakup
)
140 (load-tl-symbol-value block
*current-unwind-protect-block
*)
143 (loadw seh-frame block unwind-block-next-seh-frame-slot
)
144 (inst mov
(make-ea :dword
:disp
0) seh-frame
:fs
))
145 (loadw block block unwind-block-uwp-slot
)
146 (store-tl-symbol-value block
*current-unwind-protect-block
* tls
)))
149 (define-vop (nlx-entry)
150 ;; Note: we can't list an sc-restriction, 'cause any load vops would
151 ;; be inserted before the return-pc label.
155 (:results
(values :more t
))
156 (:temporary
(:sc descriptor-reg
) move-temp
)
158 (:save-p
:force-to-stack
)
162 (note-this-location vop
:non-local-entry
)
163 (cond ((zerop nvals
))
165 (let ((no-values (gen-label)))
166 (inst mov
(tn-ref-tn values
) nil-value
)
167 (inst jecxz no-values
)
168 (loadw (tn-ref-tn values
) start -
1)
169 (emit-label no-values
)))
171 ;; FIXME: this is mostly copied from
172 ;; DEFAULT-UNKNOWN-VALUES.
173 (collect ((defaults))
175 (tn-ref values
(tn-ref-across tn-ref
)))
177 (let ((default-lab (gen-label))
178 (tn (tn-ref-tn tn-ref
))
179 (first-stack-arg-p (= i register-arg-count
)))
180 (defaults (cons default-lab
(cons tn first-stack-arg-p
)))
181 (inst cmp count
(fixnumize i
))
182 (inst jmp
:le default-lab
)
183 (when first-stack-arg-p
184 (storew edx-tn ebx-tn -
1))
186 ((descriptor-reg any-reg
)
187 (loadw tn start
(frame-word-offset (+ sp-
>fp-offset i
))))
189 (loadw move-temp start
190 (frame-word-offset (+ sp-
>fp-offset i
)))
191 (inst mov tn move-temp
)))))
192 (let ((defaulting-done (gen-label)))
193 (emit-label defaulting-done
)
194 (assemble (*elsewhere
*)
195 (dolist (default (defaults))
196 (emit-label (car default
))
199 (inst mov
(second default
) nil-value
))
200 (inst jmp defaulting-done
))))))
201 (inst mov esp-tn sp
)))
203 (define-vop (nlx-entry-multiple)
207 ;; Again, no SC restrictions for the args, 'cause the loading would
208 ;; happen before the entry label.
210 (:temporary
(:sc unsigned-reg
:offset ecx-offset
:from
(:argument
2)) ecx
)
211 (:temporary
(:sc unsigned-reg
:offset esi-offset
) esi
)
212 (:temporary
(:sc unsigned-reg
:offset edi-offset
) edi
)
213 (:results
(result :scs
(any-reg) :from
(:argument
0))
214 (num :scs
(any-reg control-stack
)))
215 (:save-p
:force-to-stack
)
219 (note-this-location vop
:non-local-entry
)
221 (inst lea esi
(make-ea :dword
:base source
:disp
(- n-word-bytes
)))
222 ;; The 'top' arg contains the %esp value saved at the time the
223 ;; catch block was created and points to where the thrown values
228 (inst sub edi n-word-bytes
)
229 (move ecx count
) ; fixnum words == bytes
231 (inst shr ecx word-shift
) ; word count for <rep movs>
232 ;; If we got zero, we be done.
240 ;; Reset the CSP at last moved arg.
241 (inst lea esp-tn
(make-ea :dword
:base edi
:disp n-word-bytes
))))
244 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
245 (define-vop (uwp-entry)
247 (:save-p
:force-to-stack
)
248 (:results
(block) (start) (count))
249 (:ignore block start count
)
253 (note-this-location vop
:non-local-entry
)))
255 (define-vop (unwind-to-frame-and-call)
256 (:args
(ofp :scs
(descriptor-reg))
257 (uwp :scs
(descriptor-reg))
258 (function :scs
(descriptor-reg) :to
:load
:target saved-function
))
259 (:arg-types system-area-pointer system-area-pointer t
)
260 (:temporary
(:sc sap-reg
) temp
)
261 (:temporary
(:sc descriptor-reg
:offset ebx-offset
) saved-function
)
262 (:temporary
(:sc unsigned-reg
:offset eax-offset
) block
)
264 ;; Store the function into a non-stack location, since we'll be
265 ;; unwinding the stack and destroying register contents before we
266 ;; use it. It turns out that EBX is preserved as part of the
267 ;; normal multiple-value handling of an unwind, so use that.
268 (move saved-function function
)
270 ;; Allocate space for magic UWP block.
271 (inst sub esp-tn
(* unwind-block-size n-word-bytes
))
272 ;; Set up magic catch / UWP block.
274 (loadw temp uwp sap-pointer-slot other-pointer-lowtag
)
275 (storew temp block unwind-block-uwp-slot
)
276 (loadw temp ofp sap-pointer-slot other-pointer-lowtag
)
277 (storew temp block unwind-block-cfp-slot
)
279 (storew (make-fixup nil
:code-object entry-label
)
281 catch-block-entry-pc-slot
)
283 ;; Run any required UWPs.
284 (inst jmp
(make-fixup 'unwind
:assembly-routine
))
287 ;; Move our saved function to where we want it now.
288 (move block saved-function
)
291 (inst xor ecx-tn ecx-tn
)
295 (make-ea :dword
:base ebp-tn
296 :disp
(* (- sp-
>fp-offset
3) n-word-bytes
)))
298 ;; Push the return-pc so it looks like we just called.
299 (pushw ebp-tn
(frame-word-offset return-pc-save-offset
))
302 (inst jmp
(make-ea :dword
:base block
303 :disp
(- (* closure-fun-slot n-word-bytes
)
304 fun-pointer-lowtag
)))))