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
))))
24 ;;;; Save and restore dynamic environment.
26 ;;;; These VOPs are used in the reentered function to restore the
27 ;;;; appropriate dynamic environment. Currently we only save the
28 ;;;; Current-Catch. (Before sbcl-0.7.0,
29 ;;;; when there were IR1 and byte interpreters, we had to save
30 ;;;; the interpreter "eval stack" too.)
32 ;;;; We don't need to save/restore the current UNWIND-PROTECT, since
33 ;;;; UNWIND-PROTECTs are implicitly processed during unwinding.
35 ;;;; We don't need to save the BSP, because that is handled automatically.
37 (define-vop (save-dynamic-state)
38 (:results
(catch :scs
(descriptor-reg)))
40 (load-tl-symbol-value catch
*current-catch-block
*)))
42 (define-vop (restore-dynamic-state)
43 (:args
(catch :scs
(descriptor-reg)))
44 #!+sb-thread
(:temporary
(:sc unsigned-reg
) temp
)
46 (store-tl-symbol-value catch
*current-catch-block
* temp
)))
48 (define-vop (current-stack-pointer)
49 (:results
(res :scs
(any-reg control-stack
)))
53 (define-vop (current-binding-pointer)
54 (:results
(res :scs
(any-reg descriptor-reg
)))
56 (load-binding-stack-pointer res
)))
58 ;;;; unwind block hackery
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 (define-vop (make-unwind-block)
65 (:temporary
(:sc unsigned-reg
) temp
)
66 (:results
(block :scs
(any-reg)))
68 (inst lea block
(catch-block-ea tn
))
69 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
70 (storew temp block unwind-block-uwp-slot
)
71 (storew ebp-tn block unwind-block-cfp-slot
)
72 (storew (make-fixup nil
:code-object entry-label
)
73 block catch-block-entry-pc-slot
)
76 (inst mov temp
(make-ea :dword
:disp
0) :fs
)
77 (storew temp block unwind-block-next-seh-frame-slot
))))
79 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
80 ;;; tag, and link the block into the CURRENT-CATCH list
81 (define-vop (make-catch-block)
83 (tag :scs
(any-reg descriptor-reg
) :to
(:result
1)))
85 (:results
(block :scs
(any-reg)))
86 (:temporary
(:sc descriptor-reg
) temp
)
88 (inst lea block
(catch-block-ea tn
))
89 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
90 (storew temp block unwind-block-uwp-slot
)
91 (storew ebp-tn block unwind-block-cfp-slot
)
92 (storew (make-fixup nil
:code-object entry-label
)
93 block catch-block-entry-pc-slot
)
96 (inst mov temp
(make-ea :dword
:disp
0) :fs
)
97 (storew temp block unwind-block-next-seh-frame-slot
))
98 (storew tag block catch-block-tag-slot
)
99 (load-tl-symbol-value temp
*current-catch-block
*)
100 (storew temp block catch-block-previous-catch-slot
)
101 (store-tl-symbol-value block
*current-catch-block
* temp
)))
103 ;;; Just set the current unwind-protect to TN's address. This instantiates an
104 ;;; unwind block as an unwind-protect.
105 (define-vop (set-unwind-protect)
107 (:temporary
(:sc unsigned-reg
) new-uwp
#!+sb-thread tls
#!+win32 seh-frame
)
109 (inst lea new-uwp
(catch-block-ea tn
))
112 (storew (make-fixup 'uwp-seh-handler
:assembly-routine
)
113 new-uwp unwind-block-seh-frame-handler-slot
)
115 (make-ea-for-object-slot new-uwp
116 unwind-block-next-seh-frame-slot
0))
117 (inst mov
(make-ea :dword
:disp
0) seh-frame
:fs
))
118 (store-tl-symbol-value new-uwp
*current-unwind-protect-block
* tls
)))
120 (define-vop (unlink-catch-block)
121 (:temporary
(:sc unsigned-reg
) #!+sb-thread tls block
)
123 (:translate %catch-breakup
)
125 (load-tl-symbol-value block
*current-catch-block
*)
126 (loadw block block catch-block-previous-catch-slot
)
127 (store-tl-symbol-value block
*current-catch-block
* tls
)))
129 (define-vop (unlink-unwind-protect)
130 ;; NOTE: When we have both #!+sb-thread and #!+win32, we only need one temp
131 (:temporary
(:sc unsigned-reg
) block
#!+sb-thread tls
#!+win32 seh-frame
)
133 (:translate %unwind-protect-breakup
)
135 (load-tl-symbol-value block
*current-unwind-protect-block
*)
138 (loadw seh-frame block unwind-block-next-seh-frame-slot
)
139 (inst mov
(make-ea :dword
:disp
0) seh-frame
:fs
))
140 (loadw block block unwind-block-uwp-slot
)
141 (store-tl-symbol-value block
*current-unwind-protect-block
* tls
)))
144 (define-vop (nlx-entry)
145 ;; Note: we can't list an sc-restriction, 'cause any load vops would
146 ;; be inserted before the return-pc label.
150 (:results
(values :more t
))
151 (:temporary
(:sc descriptor-reg
) move-temp
)
153 (:save-p
:force-to-stack
)
157 (note-this-location vop
:non-local-entry
)
158 (cond ((zerop nvals
))
160 (let ((no-values (gen-label)))
161 (inst mov
(tn-ref-tn values
) nil-value
)
162 (inst jecxz no-values
)
163 (loadw (tn-ref-tn values
) start -
1)
164 (emit-label no-values
)))
166 ;; FIXME: this is mostly copied from
167 ;; DEFAULT-UNKNOWN-VALUES.
168 (collect ((defaults))
170 (tn-ref values
(tn-ref-across tn-ref
)))
172 (let ((default-lab (gen-label))
173 (tn (tn-ref-tn tn-ref
))
174 (first-stack-arg-p (= i register-arg-count
)))
175 (defaults (cons default-lab
(cons tn first-stack-arg-p
)))
176 (inst cmp count
(fixnumize i
))
177 (inst jmp
:le default-lab
)
178 (when first-stack-arg-p
179 (storew edx-tn ebx-tn -
1))
181 ((descriptor-reg any-reg
)
182 (loadw tn start
(frame-word-offset (+ sp-
>fp-offset i
))))
184 (loadw move-temp start
185 (frame-word-offset (+ sp-
>fp-offset i
)))
186 (inst mov tn move-temp
)))))
187 (let ((defaulting-done (gen-label)))
188 (emit-label defaulting-done
)
189 (assemble (*elsewhere
*)
190 (dolist (default (defaults))
191 (emit-label (car default
))
194 (inst mov
(second default
) nil-value
))
195 (inst jmp defaulting-done
))))))
196 (inst mov esp-tn sp
)))
198 (define-vop (nlx-entry-multiple)
202 ;; Again, no SC restrictions for the args, 'cause the loading would
203 ;; happen before the entry label.
205 (:temporary
(:sc unsigned-reg
:offset ecx-offset
:from
(:argument
2)) ecx
)
206 (:temporary
(:sc unsigned-reg
:offset esi-offset
) esi
)
207 (:temporary
(:sc unsigned-reg
:offset edi-offset
) edi
)
208 (:results
(result :scs
(any-reg) :from
(:argument
0))
209 (num :scs
(any-reg control-stack
)))
210 (:save-p
:force-to-stack
)
214 (note-this-location vop
:non-local-entry
)
216 (inst lea esi
(make-ea :dword
:base source
:disp
(- n-word-bytes
)))
217 ;; The 'top' arg contains the %esp value saved at the time the
218 ;; catch block was created and points to where the thrown values
223 (inst sub edi n-word-bytes
)
224 (move ecx count
) ; fixnum words == bytes
226 (inst shr ecx word-shift
) ; word count for <rep movs>
227 ;; If we got zero, we be done.
235 ;; Reset the CSP at last moved arg.
236 (inst lea esp-tn
(make-ea :dword
:base edi
:disp n-word-bytes
))))
239 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
240 (define-vop (uwp-entry)
242 (:save-p
:force-to-stack
)
243 (:results
(block) (start) (count))
244 (:ignore block start count
)
248 (note-this-location vop
:non-local-entry
)))
250 (define-vop (unwind-to-frame-and-call)
251 (:args
(ofp :scs
(descriptor-reg))
252 (uwp :scs
(descriptor-reg))
253 (function :scs
(descriptor-reg) :to
:load
:target saved-function
))
254 (:arg-types system-area-pointer system-area-pointer t
)
255 (:temporary
(:sc sap-reg
) temp
)
256 (:temporary
(:sc descriptor-reg
:offset ebx-offset
) saved-function
)
257 (:temporary
(:sc unsigned-reg
:offset eax-offset
) block
)
259 ;; Store the function into a non-stack location, since we'll be
260 ;; unwinding the stack and destroying register contents before we
261 ;; use it. It turns out that EBX is preserved as part of the
262 ;; normal multiple-value handling of an unwind, so use that.
263 (move saved-function function
)
265 ;; Allocate space for magic UWP block.
266 (inst sub esp-tn
(* unwind-block-size n-word-bytes
))
267 ;; Set up magic catch / UWP block.
269 (loadw temp uwp sap-pointer-slot other-pointer-lowtag
)
270 (storew temp block unwind-block-uwp-slot
)
271 (loadw temp ofp sap-pointer-slot other-pointer-lowtag
)
272 (storew temp block unwind-block-cfp-slot
)
274 (storew (make-fixup nil
:code-object entry-label
)
276 catch-block-entry-pc-slot
)
278 ;; Run any required UWPs.
279 (inst jmp
(make-fixup 'unwind
:assembly-routine
))
282 ;; Move our saved function to where we want it now.
283 (move block saved-function
)
286 (inst xor ecx-tn ecx-tn
)
290 (make-ea :dword
:base ebp-tn
291 :disp
(* (- sp-
>fp-offset
3) n-word-bytes
)))
293 ;; Push the return-pc so it looks like we just called.
294 (pushw ebp-tn
(frame-word-offset return-pc-save-offset
))
297 (inst jmp
(make-ea :dword
:base block
298 :disp
(- (* closure-fun-slot n-word-bytes
)
299 fun-pointer-lowtag
)))))