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 (alien-stack :scs (descriptor-reg)))
52 (load-symbol-value catch *current-catch-block*)
53 (load-symbol-value alien-stack *alien-stack*)))
55 (define-vop (restore-dynamic-state)
56 (:args (catch :scs (descriptor-reg))
57 (alien-stack :scs (descriptor-reg)))
59 (store-symbol-value catch *current-catch-block*)
60 (store-symbol-value alien-stack *alien-stack*)))
62 (define-vop (current-stack-pointer)
63 (:results (res :scs (any-reg control-stack)))
67 (define-vop (current-binding-pointer)
68 (:results (res :scs (any-reg descriptor-reg)))
70 (load-symbol-value res *binding-stack-pointer*)))
72 ;;;; unwind block hackery
74 ;;; Compute the address of the catch block from its TN, then store into the
75 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
76 (define-vop (make-unwind-block)
79 (:temporary (:sc unsigned-reg) temp)
80 (:results (block :scs (any-reg)))
82 (inst lea block (catch-block-ea tn))
83 (load-symbol-value temp *current-unwind-protect-block*)
84 (storew temp block unwind-block-current-uwp-slot)
85 (storew ebp-tn block unwind-block-current-cont-slot)
86 (storew (make-fixup nil :code-object entry-label)
87 block catch-block-entry-pc-slot)))
89 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
90 ;;; tag, and link the block into the CURRENT-CATCH list
91 (define-vop (make-catch-block)
93 (tag :scs (any-reg descriptor-reg) :to (:result 1)))
95 (:results (block :scs (any-reg)))
96 (:temporary (:sc descriptor-reg) temp)
98 (inst lea block (catch-block-ea tn))
99 (load-symbol-value temp *current-unwind-protect-block*)
100 (storew temp block unwind-block-current-uwp-slot)
101 (storew ebp-tn block unwind-block-current-cont-slot)
102 (storew (make-fixup nil :code-object entry-label)
103 block catch-block-entry-pc-slot)
104 (storew tag block catch-block-tag-slot)
105 (load-symbol-value temp *current-catch-block*)
106 (storew temp block catch-block-previous-catch-slot)
107 (store-symbol-value block *current-catch-block*)))
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)
115 (inst lea new-uwp (catch-block-ea tn))
116 (store-symbol-value new-uwp *current-unwind-protect-block*)))
118 (define-vop (unlink-catch-block)
119 (:temporary (:sc unsigned-reg) block)
121 (:translate %catch-breakup)
123 (load-symbol-value block *current-catch-block*)
124 (loadw block block catch-block-previous-catch-slot)
125 (store-symbol-value block *current-catch-block*)))
127 (define-vop (unlink-unwind-protect)
128 (:temporary (:sc unsigned-reg) block)
130 (:translate %unwind-protect-breakup)
132 (load-symbol-value block *current-unwind-protect-block*)
133 (loadw block block unwind-block-current-uwp-slot)
134 (store-symbol-value block *current-unwind-protect-block*)))
137 (define-vop (nlx-entry)
138 ;; Note: we can't list an sc-restriction, 'cause any load vops would
139 ;; be inserted before the return-pc label.
143 (:results (values :more t))
144 (:temporary (:sc descriptor-reg) move-temp)
146 (:save-p :force-to-stack)
150 (note-this-location vop :non-local-entry)
151 (cond ((zerop nvals))
153 (let ((no-values (gen-label)))
154 (inst mov (tn-ref-tn values) nil-value)
155 (inst jecxz no-values)
156 (loadw (tn-ref-tn values) start -1)
157 (emit-label no-values)))
159 (collect ((defaults))
161 (tn-ref values (tn-ref-across tn-ref)))
163 (let ((default-lab (gen-label))
164 (tn (tn-ref-tn tn-ref)))
165 (defaults (cons default-lab tn))
167 (inst cmp count (fixnumize i))
168 (inst jmp :le default-lab)
170 ((descriptor-reg any-reg)
171 (loadw tn start (- (1+ i))))
173 (loadw move-temp start (- (1+ i)))
174 (inst mov tn move-temp)))))
175 (let ((defaulting-done (gen-label)))
176 (emit-label defaulting-done)
177 (assemble (*elsewhere*)
178 (dolist (def (defaults))
179 (emit-label (car def))
180 (inst mov (cdr def) nil-value))
181 (inst jmp defaulting-done))))))
182 (inst mov esp-tn sp)))
184 (define-vop (nlx-entry-multiple)
188 ;; Again, no SC restrictions for the args, 'cause the loading would
189 ;; happen before the entry label.
191 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
192 (:temporary (:sc unsigned-reg :offset esi-offset) esi)
193 (:temporary (:sc unsigned-reg :offset edi-offset) edi)
194 (:results (result :scs (any-reg) :from (:argument 0))
195 (num :scs (any-reg control-stack)))
196 (:save-p :force-to-stack)
200 (note-this-location vop :non-local-entry)
202 (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
203 ;; The 'top' arg contains the %esp value saved at the time the
204 ;; catch block was created and points to where the thrown values
209 (inst sub edi n-word-bytes)
210 (move ecx count) ; fixnum words == bytes
212 (inst shr ecx word-shift) ; word count for <rep movs>
213 ;; If we got zero, we be done.
221 ;; Reset the CSP at last moved arg.
222 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
225 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
226 (define-vop (uwp-entry)
228 (:save-p :force-to-stack)
229 (:results (block) (start) (count))
230 (:ignore block start count)
234 (note-this-location vop :non-local-entry)))