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 an environment-live stack TN for saving the SP for NLX entry.
15 (defun make-nlx-sp-tn (env)
17 (make-representation-tn *fixnum-primitive-type
* any-reg-sc-number
)
20 ;;; Make a TN for the argument count passing location for a non-local entry.
21 (defun make-nlx-entry-arg-start-location ()
22 (make-wired-tn *fixnum-primitive-type
* any-reg-sc-number ebx-offset
))
24 (defun catch-block-ea (tn)
25 (aver (sc-is tn catch-block
))
26 (make-ea :dword
:base ebp-tn
27 :disp
(frame-byte-offset (+ -
1 (tn-offset tn
) catch-block-size
))))
30 ;;;; Save and restore dynamic environment.
32 ;;;; These VOPs are used in the reentered function to restore the
33 ;;;; appropriate dynamic environment. Currently we only save the
34 ;;;; Current-Catch. (Before sbcl-0.7.0,
35 ;;;; when there were IR1 and byte interpreters, we had to save
36 ;;;; the interpreter "eval stack" too.)
38 ;;;; We don't need to save/restore the current UNWIND-PROTECT, since
39 ;;;; UNWIND-PROTECTs are implicitly processed during unwinding.
41 ;;;; We don't need to save the BSP, because that is handled automatically.
43 (define-vop (save-dynamic-state)
44 (:results
(catch :scs
(descriptor-reg)))
46 (load-tl-symbol-value catch
*current-catch-block
*)))
48 (define-vop (restore-dynamic-state)
49 (:args
(catch :scs
(descriptor-reg)))
50 #!+sb-thread
(:temporary
(:sc unsigned-reg
) temp
)
52 (store-tl-symbol-value catch
*current-catch-block
* temp
)))
54 (define-vop (current-stack-pointer)
55 (:results
(res :scs
(any-reg control-stack
)))
59 (define-vop (current-binding-pointer)
60 (:results
(res :scs
(any-reg descriptor-reg
)))
62 (load-binding-stack-pointer res
)))
64 ;;;; unwind block hackery
66 ;;; Compute the address of the catch block from its TN, then store into the
67 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
68 (define-vop (make-unwind-block)
71 (:temporary
(:sc unsigned-reg
) temp
)
72 (:results
(block :scs
(any-reg)))
74 (inst lea block
(catch-block-ea tn
))
75 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
76 (storew temp block unwind-block-current-uwp-slot
)
77 (storew ebp-tn block unwind-block-current-cont-slot
)
78 (storew (make-fixup nil
:code-object entry-label
)
79 block catch-block-entry-pc-slot
)
82 (inst mov temp
(make-ea :dword
:disp
0) :fs
)
83 (storew temp block unwind-block-next-seh-frame-slot
))))
85 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
86 ;;; tag, and link the block into the CURRENT-CATCH list
87 (define-vop (make-catch-block)
89 (tag :scs
(any-reg descriptor-reg
) :to
(:result
1)))
91 (:results
(block :scs
(any-reg)))
92 (:temporary
(:sc descriptor-reg
) temp
)
94 (inst lea block
(catch-block-ea tn
))
95 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
96 (storew temp block unwind-block-current-uwp-slot
)
97 (storew ebp-tn block unwind-block-current-cont-slot
)
98 (storew (make-fixup nil
:code-object entry-label
)
99 block catch-block-entry-pc-slot
)
102 (inst mov temp
(make-ea :dword
:disp
0) :fs
)
103 (storew temp block unwind-block-next-seh-frame-slot
))
104 (storew tag block catch-block-tag-slot
)
105 (load-tl-symbol-value temp
*current-catch-block
*)
106 (storew temp block catch-block-previous-catch-slot
)
107 (store-tl-symbol-value block
*current-catch-block
* temp
)))
109 ;;; Just set the current unwind-protect to TN's address. This instantiates an
110 ;;; unwind block as an unwind-protect.
111 (define-vop (set-unwind-protect)
113 (:temporary
(:sc unsigned-reg
) new-uwp
#!+sb-thread tls
#!+win32 seh-frame
)
115 (inst lea new-uwp
(catch-block-ea tn
))
118 (storew (make-fixup 'uwp-seh-handler
:assembly-routine
)
119 new-uwp unwind-block-seh-frame-handler-slot
)
121 (make-ea-for-object-slot new-uwp
122 unwind-block-next-seh-frame-slot
0))
123 (inst mov
(make-ea :dword
:disp
0) seh-frame
:fs
))
124 (store-tl-symbol-value new-uwp
*current-unwind-protect-block
* tls
)))
126 (define-vop (unlink-catch-block)
127 (:temporary
(:sc unsigned-reg
) #!+sb-thread tls block
)
129 (:translate %catch-breakup
)
131 (load-tl-symbol-value block
*current-catch-block
*)
132 (loadw block block catch-block-previous-catch-slot
)
133 (store-tl-symbol-value block
*current-catch-block
* tls
)))
135 (define-vop (unlink-unwind-protect)
136 ;; NOTE: When we have both #!+sb-thread and #!+win32, we only need one temp
137 (:temporary
(:sc unsigned-reg
) block
#!+sb-thread tls
#!+win32 seh-frame
)
139 (:translate %unwind-protect-breakup
)
141 (load-tl-symbol-value block
*current-unwind-protect-block
*)
144 (loadw seh-frame block unwind-block-next-seh-frame-slot
)
145 (inst mov
(make-ea :dword
:disp
0) seh-frame
:fs
))
146 (loadw block block unwind-block-current-uwp-slot
)
147 (store-tl-symbol-value block
*current-unwind-protect-block
* tls
)))
150 (define-vop (nlx-entry)
151 ;; Note: we can't list an sc-restriction, 'cause any load vops would
152 ;; be inserted before the return-pc label.
156 (:results
(values :more t
))
157 (:temporary
(:sc descriptor-reg
) move-temp
)
159 (:save-p
:force-to-stack
)
163 (note-this-location vop
:non-local-entry
)
164 (cond ((zerop nvals
))
166 (let ((no-values (gen-label)))
167 (inst mov
(tn-ref-tn values
) nil-value
)
168 (inst jecxz no-values
)
169 (loadw (tn-ref-tn values
) start -
1)
170 (emit-label no-values
)))
172 ;; FIXME: this is mostly copied from
173 ;; DEFAULT-UNKNOWN-VALUES.
174 (collect ((defaults))
176 (tn-ref values
(tn-ref-across tn-ref
)))
178 (let ((default-lab (gen-label))
179 (tn (tn-ref-tn tn-ref
))
180 (first-stack-arg-p (= i register-arg-count
)))
181 (defaults (cons default-lab
(cons tn first-stack-arg-p
)))
182 (inst cmp count
(fixnumize i
))
183 (inst jmp
:le default-lab
)
184 (when first-stack-arg-p
185 (storew edx-tn ebx-tn -
1))
187 ((descriptor-reg any-reg
)
188 (loadw tn start
(frame-word-offset (+ sp-
>fp-offset i
))))
190 (loadw move-temp start
191 (frame-word-offset (+ sp-
>fp-offset i
)))
192 (inst mov tn move-temp
)))))
193 (let ((defaulting-done (gen-label)))
194 (emit-label defaulting-done
)
195 (assemble (*elsewhere
*)
196 (dolist (default (defaults))
197 (emit-label (car default
))
200 (inst mov
(second default
) nil-value
))
201 (inst jmp defaulting-done
))))))
202 (inst mov esp-tn sp
)))
204 (define-vop (nlx-entry-multiple)
208 ;; Again, no SC restrictions for the args, 'cause the loading would
209 ;; happen before the entry label.
211 (:temporary
(:sc unsigned-reg
:offset ecx-offset
:from
(:argument
2)) ecx
)
212 (:temporary
(:sc unsigned-reg
:offset esi-offset
) esi
)
213 (:temporary
(:sc unsigned-reg
:offset edi-offset
) edi
)
214 (:results
(result :scs
(any-reg) :from
(:argument
0))
215 (num :scs
(any-reg control-stack
)))
216 (:save-p
:force-to-stack
)
220 (note-this-location vop
:non-local-entry
)
222 (inst lea esi
(make-ea :dword
:base source
:disp
(- n-word-bytes
)))
223 ;; The 'top' arg contains the %esp value saved at the time the
224 ;; catch block was created and points to where the thrown values
229 (inst sub edi n-word-bytes
)
230 (move ecx count
) ; fixnum words == bytes
232 (inst shr ecx word-shift
) ; word count for <rep movs>
233 ;; If we got zero, we be done.
241 ;; Reset the CSP at last moved arg.
242 (inst lea esp-tn
(make-ea :dword
:base edi
:disp n-word-bytes
))))
245 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
246 (define-vop (uwp-entry)
248 (:save-p
:force-to-stack
)
249 (:results
(block) (start) (count))
250 (:ignore block start count
)
254 (note-this-location vop
:non-local-entry
)))
256 (define-vop (unwind-to-frame-and-call)
257 (:args
(ofp :scs
(descriptor-reg))
258 (uwp :scs
(descriptor-reg))
259 (function :scs
(descriptor-reg) :to
:load
:target saved-function
))
260 (:arg-types system-area-pointer system-area-pointer t
)
261 (:temporary
(:sc sap-reg
) temp
)
262 (:temporary
(:sc descriptor-reg
:offset ebx-offset
) saved-function
)
263 (:temporary
(:sc unsigned-reg
:offset eax-offset
) block
)
265 ;; Store the function into a non-stack location, since we'll be
266 ;; unwinding the stack and destroying register contents before we
267 ;; use it. It turns out that EBX is preserved as part of the
268 ;; normal multiple-value handling of an unwind, so use that.
269 (move saved-function function
)
271 ;; Allocate space for magic UWP block.
272 (inst sub esp-tn
(* unwind-block-size n-word-bytes
))
273 ;; Set up magic catch / UWP block.
275 (loadw temp uwp sap-pointer-slot other-pointer-lowtag
)
276 (storew temp block unwind-block-current-uwp-slot
)
277 (loadw temp ofp sap-pointer-slot other-pointer-lowtag
)
278 (storew temp block unwind-block-current-cont-slot
)
280 (storew (make-fixup nil
:code-object entry-label
)
282 catch-block-entry-pc-slot
)
284 ;; Run any required UWPs.
285 (inst jmp
(make-fixup 'unwind
:assembly-routine
))
288 ;; Move our saved function to where we want it now.
289 (move block saved-function
)
292 (inst xor ecx-tn ecx-tn
)
296 (make-ea :dword
:base ebp-tn
297 :disp
(* (- sp-
>fp-offset
3) n-word-bytes
)))
299 ;; Push the return-pc so it looks like we just called.
300 (pushw ebp-tn
(frame-word-offset return-pc-save-offset
))
303 (inst jmp
(make-ea :dword
:base block
304 :disp
(- (* closure-fun-slot n-word-bytes
)
305 fun-pointer-lowtag
)))))