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 (!def-vm-support-routine 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 (!def-vm-support-routine 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 (- (* (+ (tn-offset tn) catch-block-size) n-word-bytes))))
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 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.)
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 ;;; 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*))
48 (define-vop (save-dynamic-state)
49 (:results (catch :scs (descriptor-reg))
50 (eval :scs (descriptor-reg))
51 (alien-stack :scs (descriptor-reg)))
53 (load-symbol-value catch *current-catch-block*)
54 (load-symbol-value alien-stack *alien-stack*)))
56 (define-vop (restore-dynamic-state)
57 (:args (catch :scs (descriptor-reg))
58 (eval :scs (descriptor-reg))
59 (alien-stack :scs (descriptor-reg)))
61 (store-symbol-value catch *current-catch-block*)
62 (store-symbol-value alien-stack *alien-stack*)))
64 (define-vop (current-stack-pointer)
65 (:results (res :scs (any-reg control-stack)))
69 (define-vop (current-binding-pointer)
70 (:results (res :scs (any-reg descriptor-reg)))
72 (load-symbol-value res *binding-stack-pointer*)))
74 ;;;; unwind block hackery
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)
81 (:temporary (:sc unsigned-reg) temp)
82 (:results (block :scs (any-reg)))
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)))
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)
95 (tag :scs (any-reg descriptor-reg) :to (:result 1)))
97 (:results (block :scs (any-reg)))
98 (:temporary (:sc descriptor-reg) temp)
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*)))
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)
115 (:temporary (:sc unsigned-reg) new-uwp)
117 (inst lea new-uwp (catch-block-ea tn))
118 (store-symbol-value new-uwp *current-unwind-protect-block*)))
120 (define-vop (unlink-catch-block)
121 (:temporary (:sc unsigned-reg) block)
123 (:translate %catch-breakup)
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*)))
129 (define-vop (unlink-unwind-protect)
130 (:temporary (:sc unsigned-reg) block)
132 (:translate %unwind-protect-breakup)
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*)))
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.
145 (:results (values :more t))
146 (:temporary (:sc descriptor-reg) move-temp)
148 (:save-p :force-to-stack)
152 (note-this-location vop :non-local-entry)
153 (cond ((zerop nvals))
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)))
161 (collect ((defaults))
163 (tn-ref values (tn-ref-across tn-ref)))
165 (let ((default-lab (gen-label))
166 (tn (tn-ref-tn tn-ref)))
167 (defaults (cons default-lab tn))
169 (inst cmp count (fixnumize i))
170 (inst jmp :le default-lab)
172 ((descriptor-reg any-reg)
173 (loadw tn start (- (1+ i))))
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)))
186 (define-vop (nlx-entry-multiple)
190 ;; Again, no SC restrictions for the args, 'cause the loading would
191 ;; happen before the entry 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)
202 (note-this-location vop :non-local-entry)
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
211 (inst sub edi n-word-bytes)
212 (move ecx count) ; fixnum words == bytes
214 (inst shr ecx word-shift) ; word count for <rep movs>
215 ;; If we got zero, we be done.
223 ;; Reset the CSP at last moved arg.
224 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
227 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
228 (define-vop (uwp-entry)
230 (:save-p :force-to-stack)
231 (:results (block) (start) (count))
232 (:ignore block start count)
236 (note-this-location vop :non-local-entry)))