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 rbx-offset
))
18 (defun catch-block-ea (tn)
19 (aver (sc-is tn catch-block
))
20 (make-ea :qword
:base rbp-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 :qword
:base rbp-tn
26 :disp
(frame-byte-offset (+ -
1 (tn-offset tn
) unwind-block-size
))))
28 ;;;; Save and restore dynamic environment.
30 ;;;; These VOPs are used in the reentered function to restore the
31 ;;;; appropriate dynamic environment. Currently we only save the
32 ;;;; Current-Catch. (Before sbcl-0.7.0,
33 ;;;; when there were IR1 and byte interpreters, we had to save
34 ;;;; the interpreter "eval stack" too.)
36 ;;;; We don't need to save/restore the current UNWIND-PROTECT, since
37 ;;;; UNWIND-PROTECTs are implicitly processed during unwinding.
39 ;;;; We don't need to save the BSP, because that is handled automatically.
41 (define-vop (save-dynamic-state)
42 (:results
(catch :scs
(descriptor-reg)))
44 (load-tl-symbol-value catch
*current-catch-block
*)))
46 (define-vop (restore-dynamic-state)
47 (:args
(catch :scs
(descriptor-reg)))
49 (store-tl-symbol-value catch
*current-catch-block
*)))
51 (define-vop (current-stack-pointer)
52 (:results
(res :scs
(any-reg control-stack
)))
56 (define-vop (current-binding-pointer)
57 (:results
(res :scs
(any-reg descriptor-reg
)))
59 (load-binding-stack-pointer res
)))
61 ;;;; unwind block hackery
63 ;;; Compute the address of the catch block from its TN, then store into the
64 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
65 (define-vop (make-unwind-block)
68 (:temporary
(:sc unsigned-reg
) temp
)
69 (:results
(block :scs
(any-reg)))
71 (inst lea block
(unwind-block-ea tn
))
72 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
73 (storew temp block unwind-block-uwp-slot
)
74 (storew rbp-tn block unwind-block-cfp-slot
)
75 (inst lea temp
(make-fixup nil
:code-object entry-label
))
76 (storew temp block unwind-block-entry-pc-slot
)))
78 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
79 ;;; tag, and link the block into the CURRENT-CATCH list
80 (define-vop (make-catch-block)
82 (tag :scs
(any-reg descriptor-reg
) :to
(:result
1)))
84 (:results
(block :scs
(any-reg)))
85 (:temporary
(:sc descriptor-reg
) temp
)
87 (inst lea block
(catch-block-ea tn
))
88 (load-tl-symbol-value temp
*current-unwind-protect-block
*)
89 (storew temp block catch-block-uwp-slot
)
90 (storew rbp-tn block catch-block-cfp-slot
)
91 (inst lea temp
(make-fixup nil
:code-object entry-label
))
92 (storew temp block catch-block-entry-pc-slot
)
93 (storew tag block catch-block-tag-slot
)
94 (load-tl-symbol-value temp
*current-catch-block
*)
95 (storew temp block catch-block-previous-catch-slot
)
96 (store-tl-symbol-value block
*current-catch-block
*)))
98 ;;; Just set the current unwind-protect to TN's address. This instantiates an
99 ;;; unwind block as an unwind-protect.
100 (define-vop (set-unwind-protect)
102 (:temporary
(:sc unsigned-reg
) new-uwp
)
104 (inst lea new-uwp
(unwind-block-ea tn
))
105 (store-tl-symbol-value new-uwp
*current-unwind-protect-block
*)))
107 (define-vop (unlink-catch-block)
108 (:temporary
(:sc unsigned-reg
) block
)
110 (:translate %catch-breakup
)
112 (load-tl-symbol-value block
*current-catch-block
*)
113 (loadw block block catch-block-previous-catch-slot
)
114 (store-tl-symbol-value block
*current-catch-block
*)))
116 (define-vop (unlink-unwind-protect)
117 (:temporary
(:sc unsigned-reg
) block
)
119 (:translate %unwind-protect-breakup
)
121 (load-tl-symbol-value block
*current-unwind-protect-block
*)
122 (loadw block block unwind-block-uwp-slot
)
123 (store-tl-symbol-value block
*current-unwind-protect-block
*)))
126 (define-vop (nlx-entry)
127 ;; Note: we can't list an sc-restriction, 'cause any load vops would
128 ;; be inserted before the return-pc label.
132 (:results
(values :more t
))
133 (:temporary
(:sc descriptor-reg
) move-temp
)
135 (:save-p
:force-to-stack
)
139 (note-this-location vop
:non-local-entry
)
140 (cond ((zerop nvals
))
142 (let ((no-values (gen-label)))
143 (inst mov
(tn-ref-tn values
) nil-value
)
144 (inst jrcxz no-values
)
145 (loadw (tn-ref-tn values
) start -
1)
146 (emit-label no-values
)))
148 ;; FIXME: this is mostly copied from
149 ;; DEFAULT-UNKNOWN-VALUES.
150 (collect ((defaults))
152 (tn-ref values
(tn-ref-across tn-ref
)))
154 (let ((default-lab (gen-label))
155 (tn (tn-ref-tn tn-ref
))
156 (first-stack-arg-p (= i register-arg-count
)))
157 (defaults (cons default-lab
(cons tn first-stack-arg-p
)))
158 (inst cmp count
(fixnumize i
))
159 (inst jmp
:le default-lab
)
160 (when first-stack-arg-p
161 (storew rdx-tn rbx-tn -
1))
163 ((descriptor-reg any-reg
)
164 (loadw tn start
(frame-word-offset (+ sp-
>fp-offset i
))))
166 (loadw move-temp start
167 (frame-word-offset (+ sp-
>fp-offset i
)))
168 (inst mov tn move-temp
)))))
169 (let ((defaulting-done (gen-label)))
170 (emit-label defaulting-done
)
171 (assemble (*elsewhere
*)
172 (dolist (default (defaults))
173 (emit-label (car default
))
176 (inst mov
(second default
) nil-value
))
177 (inst jmp defaulting-done
))))))
178 (inst mov rsp-tn sp
)))
180 (define-vop (nlx-entry-multiple)
184 ;; Again, no SC restrictions for the args, 'cause the loading would
185 ;; happen before the entry label.
187 (:temporary
(:sc unsigned-reg
:offset rcx-offset
:from
(:argument
2)) rcx
)
188 (:temporary
(:sc unsigned-reg
:offset rsi-offset
) rsi
)
189 (:temporary
(:sc unsigned-reg
:offset rdi-offset
) rdi
)
190 (:results
(result :scs
(any-reg) :from
(:argument
0))
191 (num :scs
(any-reg control-stack
)))
192 (:save-p
:force-to-stack
)
196 (note-this-location vop
:non-local-entry
)
198 (inst lea rsi
(make-ea :qword
:base source
:disp
(- n-word-bytes
)))
199 ;; The 'top' arg contains the %esp value saved at the time the
200 ;; catch block was created and points to where the thrown values
205 (inst sub rdi n-word-bytes
)
206 (move rcx count
) ; fixnum words == bytes
208 (inst shr rcx n-fixnum-tag-bits
) ; word count for <rep movs>
209 ;; If we got zero, we be done.
217 ;; Reset the CSP at last moved arg.
218 (inst lea rsp-tn
(make-ea :qword
:base rdi
:disp n-word-bytes
))))
221 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
222 (define-vop (uwp-entry)
224 (:save-p
:force-to-stack
)
225 (:results
(block) (start) (count))
226 (:ignore block start count
)
230 (note-this-location vop
:non-local-entry
)))
232 (define-vop (unwind-to-frame-and-call)
233 (:args
(ofp :scs
(descriptor-reg))
234 (uwp :scs
(descriptor-reg))
235 (function :scs
(descriptor-reg) :to
:load
:target saved-function
))
236 (:arg-types system-area-pointer system-area-pointer t
)
237 (:temporary
(:sc sap-reg
) temp
)
238 (:temporary
(:sc descriptor-reg
:offset rbx-offset
) saved-function
)
239 (:temporary
(:sc unsigned-reg
:offset rax-offset
) block
)
242 ;; Store the function into a non-stack location, since we'll be
243 ;; unwinding the stack and destroying register contents before we
244 ;; use it. It turns out that RBX is preserved as part of the
245 ;; normal multiple-value handling of an unwind, so use that.
246 (move saved-function function
)
248 ;; Allocate space for magic UWP block.
249 (inst sub rsp-tn
(* unwind-block-size n-word-bytes
))
250 ;; Set up magic catch / UWP block.
252 (loadw temp uwp sap-pointer-slot other-pointer-lowtag
)
253 (storew temp block unwind-block-uwp-slot
)
254 (loadw temp ofp sap-pointer-slot other-pointer-lowtag
)
255 (storew temp block unwind-block-cfp-slot
)
257 (inst lea temp-reg-tn
(make-fixup nil
:code-object entry-label
))
258 (storew temp-reg-tn block unwind-block-entry-pc-slot
)
260 ;; Run any required UWPs.
261 (invoke-asm-routine 'jmp
'unwind vop temp-reg-tn
)
264 ;; Move our saved function to where we want it now.
265 (move block saved-function
)
272 (make-ea :qword
:base rbp-tn
273 :disp
(* (- sp-
>fp-offset
3) n-word-bytes
)))
275 ;; Push the return-pc so it looks like we just called.
276 (pushw rbp-tn
(frame-word-offset return-pc-save-offset
))
279 (inst jmp
(make-ea :qword
:base block
280 :disp
(- (* closure-fun-slot n-word-bytes
)
281 fun-pointer-lowtag
)))))