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 (define-vop (save-dynamic-state)
44 (:results (catch :scs (descriptor-reg))
45 (alien-stack :scs (descriptor-reg)))
47 (load-symbol-value catch *current-catch-block*)
48 (load-symbol-value alien-stack *alien-stack*)))
50 (define-vop (restore-dynamic-state)
51 (:args (catch :scs (descriptor-reg))
52 (alien-stack :scs (descriptor-reg)))
54 (store-symbol-value catch *current-catch-block*)
55 (store-symbol-value alien-stack *alien-stack*)))
57 (define-vop (current-stack-pointer)
58 (:results (res :scs (any-reg control-stack)))
62 (define-vop (current-binding-pointer)
63 (:results (res :scs (any-reg descriptor-reg)))
65 (load-symbol-value res *binding-stack-pointer*)))
67 ;;;; unwind block hackery
69 ;;; Compute the address of the catch block from its TN, then store into the
70 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
71 (define-vop (make-unwind-block)
74 (:temporary (:sc unsigned-reg) temp)
75 (:results (block :scs (any-reg)))
77 (inst lea block (catch-block-ea tn))
78 (load-symbol-value temp *current-unwind-protect-block*)
79 (storew temp block unwind-block-current-uwp-slot)
80 (storew ebp-tn block unwind-block-current-cont-slot)
81 (storew (make-fixup nil :code-object entry-label)
82 block catch-block-entry-pc-slot)))
84 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
85 ;;; tag, and link the block into the CURRENT-CATCH list
86 (define-vop (make-catch-block)
88 (tag :scs (any-reg descriptor-reg) :to (:result 1)))
90 (:results (block :scs (any-reg)))
91 (:temporary (:sc descriptor-reg) temp)
93 (inst lea block (catch-block-ea tn))
94 (load-symbol-value temp *current-unwind-protect-block*)
95 (storew temp block unwind-block-current-uwp-slot)
96 (storew ebp-tn block unwind-block-current-cont-slot)
97 (storew (make-fixup nil :code-object entry-label)
98 block catch-block-entry-pc-slot)
99 (storew tag block catch-block-tag-slot)
100 (load-symbol-value temp *current-catch-block*)
101 (storew temp block catch-block-previous-catch-slot)
102 (store-symbol-value block *current-catch-block*)))
104 ;;; Just set the current unwind-protect to TN's address. This instantiates an
105 ;;; unwind block as an unwind-protect.
106 (define-vop (set-unwind-protect)
108 (:temporary (:sc unsigned-reg) new-uwp)
110 (inst lea new-uwp (catch-block-ea tn))
111 (store-symbol-value new-uwp *current-unwind-protect-block*)))
113 (define-vop (unlink-catch-block)
114 (:temporary (:sc unsigned-reg) block)
116 (:translate %catch-breakup)
118 (load-symbol-value block *current-catch-block*)
119 (loadw block block catch-block-previous-catch-slot)
120 (store-symbol-value block *current-catch-block*)))
122 (define-vop (unlink-unwind-protect)
123 (:temporary (:sc unsigned-reg) block)
125 (:translate %unwind-protect-breakup)
127 (load-symbol-value block *current-unwind-protect-block*)
128 (loadw block block unwind-block-current-uwp-slot)
129 (store-symbol-value block *current-unwind-protect-block*)))
132 (define-vop (nlx-entry)
133 ;; Note: we can't list an sc-restriction, 'cause any load vops would
134 ;; be inserted before the return-pc label.
138 (:results (values :more t))
139 (:temporary (:sc descriptor-reg) move-temp)
141 (:save-p :force-to-stack)
145 (note-this-location vop :non-local-entry)
146 (cond ((zerop nvals))
148 (let ((no-values (gen-label)))
149 (inst mov (tn-ref-tn values) nil-value)
150 (inst jecxz no-values)
151 (loadw (tn-ref-tn values) start -1)
152 (emit-label no-values)))
154 (collect ((defaults))
156 (tn-ref values (tn-ref-across tn-ref)))
158 (let ((default-lab (gen-label))
159 (tn (tn-ref-tn tn-ref)))
160 (defaults (cons default-lab tn))
162 (inst cmp count (fixnumize i))
163 (inst jmp :le default-lab)
165 ((descriptor-reg any-reg)
166 (loadw tn start (- (1+ i))))
168 (loadw move-temp start (- (1+ i)))
169 (inst mov tn move-temp)))))
170 (let ((defaulting-done (gen-label)))
171 (emit-label defaulting-done)
172 (assemble (*elsewhere*)
173 (dolist (def (defaults))
174 (emit-label (car def))
175 (inst mov (cdr def) nil-value))
176 (inst jmp defaulting-done))))))
177 (inst mov esp-tn sp)))
179 (define-vop (nlx-entry-multiple)
183 ;; Again, no SC restrictions for the args, 'cause the loading would
184 ;; happen before the entry label.
186 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
187 (:temporary (:sc unsigned-reg :offset esi-offset) esi)
188 (:temporary (:sc unsigned-reg :offset edi-offset) edi)
189 (:results (result :scs (any-reg) :from (:argument 0))
190 (num :scs (any-reg control-stack)))
191 (:save-p :force-to-stack)
195 (note-this-location vop :non-local-entry)
197 (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
198 ;; The 'top' arg contains the %esp value saved at the time the
199 ;; catch block was created and points to where the thrown values
204 (inst sub edi n-word-bytes)
205 (move ecx count) ; fixnum words == bytes
207 (inst shr ecx word-shift) ; word count for <rep movs>
208 ;; If we got zero, we be done.
216 ;; Reset the CSP at last moved arg.
217 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
220 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
221 (define-vop (uwp-entry)
223 (:save-p :force-to-stack)
224 (:results (block) (start) (count))
225 (:ignore block start count)
229 (note-this-location vop :non-local-entry)))