Rename slots in unwind-block and catch-block.
[sbcl.git] / src / compiler / x86 / nlx.lisp
bloba29e81d6548aeb3c2ce44f1c1c1e749206871de9
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
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!VM")
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 ebx-offset))
18 (defun catch-block-ea (tn)
19 (aver (sc-is tn catch-block))
20 (make-ea :dword :base ebp-tn
21 :disp (frame-byte-offset (+ -1 (tn-offset tn) catch-block-size))))
24 ;;;; Save and restore dynamic environment.
25 ;;;;
26 ;;;; These VOPs are used in the reentered function to restore the
27 ;;;; appropriate dynamic environment. Currently we only save the
28 ;;;; Current-Catch. (Before sbcl-0.7.0,
29 ;;;; when there were IR1 and byte interpreters, we had to save
30 ;;;; the interpreter "eval stack" too.)
31 ;;;;
32 ;;;; We don't need to save/restore the current UNWIND-PROTECT, since
33 ;;;; UNWIND-PROTECTs are implicitly processed during unwinding.
34 ;;;;
35 ;;;; We don't need to save the BSP, because that is handled automatically.
37 (define-vop (save-dynamic-state)
38 (:results (catch :scs (descriptor-reg)))
39 (:generator 13
40 (load-tl-symbol-value catch *current-catch-block*)))
42 (define-vop (restore-dynamic-state)
43 (:args (catch :scs (descriptor-reg)))
44 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
45 (:generator 10
46 (store-tl-symbol-value catch *current-catch-block* temp)))
48 (define-vop (current-stack-pointer)
49 (:results (res :scs (any-reg control-stack)))
50 (:generator 1
51 (move res esp-tn)))
53 (define-vop (current-binding-pointer)
54 (:results (res :scs (any-reg descriptor-reg)))
55 (:generator 1
56 (load-binding-stack-pointer res)))
58 ;;;; unwind block hackery
60 ;;; Compute the address of the catch block from its TN, then store into the
61 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
62 (define-vop (make-unwind-block)
63 (:args (tn))
64 (:info entry-label)
65 (:temporary (:sc unsigned-reg) temp)
66 (:results (block :scs (any-reg)))
67 (:generator 22
68 (inst lea block (catch-block-ea tn))
69 (load-tl-symbol-value temp *current-unwind-protect-block*)
70 (storew temp block unwind-block-uwp-slot)
71 (storew ebp-tn block unwind-block-cfp-slot)
72 (storew (make-fixup nil :code-object entry-label)
73 block catch-block-entry-pc-slot)
74 #!+win32
75 (progn
76 (inst mov temp (make-ea :dword :disp 0) :fs)
77 (storew temp block unwind-block-next-seh-frame-slot))))
79 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
80 ;;; tag, and link the block into the CURRENT-CATCH list
81 (define-vop (make-catch-block)
82 (:args (tn)
83 (tag :scs (any-reg descriptor-reg) :to (:result 1)))
84 (:info entry-label)
85 (:results (block :scs (any-reg)))
86 (:temporary (:sc descriptor-reg) temp)
87 (:generator 44
88 (inst lea block (catch-block-ea tn))
89 (load-tl-symbol-value temp *current-unwind-protect-block*)
90 (storew temp block unwind-block-uwp-slot)
91 (storew ebp-tn block unwind-block-cfp-slot)
92 (storew (make-fixup nil :code-object entry-label)
93 block catch-block-entry-pc-slot)
94 #!+win32
95 (progn
96 (inst mov temp (make-ea :dword :disp 0) :fs)
97 (storew temp block unwind-block-next-seh-frame-slot))
98 (storew tag block catch-block-tag-slot)
99 (load-tl-symbol-value temp *current-catch-block*)
100 (storew temp block catch-block-previous-catch-slot)
101 (store-tl-symbol-value block *current-catch-block* temp)))
103 ;;; Just set the current unwind-protect to TN's address. This instantiates an
104 ;;; unwind block as an unwind-protect.
105 (define-vop (set-unwind-protect)
106 (:args (tn))
107 (:temporary (:sc unsigned-reg) new-uwp #!+sb-thread tls #!+win32 seh-frame)
108 (:generator 7
109 (inst lea new-uwp (catch-block-ea tn))
110 #!+win32
111 (progn
112 (storew (make-fixup 'uwp-seh-handler :assembly-routine)
113 new-uwp unwind-block-seh-frame-handler-slot)
114 (inst lea seh-frame
115 (make-ea-for-object-slot new-uwp
116 unwind-block-next-seh-frame-slot 0))
117 (inst mov (make-ea :dword :disp 0) seh-frame :fs))
118 (store-tl-symbol-value new-uwp *current-unwind-protect-block* tls)))
120 (define-vop (unlink-catch-block)
121 (:temporary (:sc unsigned-reg) #!+sb-thread tls block)
122 (:policy :fast-safe)
123 (:translate %catch-breakup)
124 (:generator 17
125 (load-tl-symbol-value block *current-catch-block*)
126 (loadw block block catch-block-previous-catch-slot)
127 (store-tl-symbol-value block *current-catch-block* tls)))
129 (define-vop (unlink-unwind-protect)
130 ;; NOTE: When we have both #!+sb-thread and #!+win32, we only need one temp
131 (:temporary (:sc unsigned-reg) block #!+sb-thread tls #!+win32 seh-frame)
132 (:policy :fast-safe)
133 (:translate %unwind-protect-breakup)
134 (:generator 17
135 (load-tl-symbol-value block *current-unwind-protect-block*)
136 #!+win32
137 (progn
138 (loadw seh-frame block unwind-block-next-seh-frame-slot)
139 (inst mov (make-ea :dword :disp 0) seh-frame :fs))
140 (loadw block block unwind-block-uwp-slot)
141 (store-tl-symbol-value block *current-unwind-protect-block* tls)))
143 ;;;; NLX entry VOPs
144 (define-vop (nlx-entry)
145 ;; Note: we can't list an sc-restriction, 'cause any load vops would
146 ;; be inserted before the return-pc label.
147 (:args (sp)
148 (start)
149 (count))
150 (:results (values :more t))
151 (:temporary (:sc descriptor-reg) move-temp)
152 (:info label nvals)
153 (:save-p :force-to-stack)
154 (:vop-var vop)
155 (:generator 30
156 (emit-label label)
157 (note-this-location vop :non-local-entry)
158 (cond ((zerop nvals))
159 ((= nvals 1)
160 (let ((no-values (gen-label)))
161 (inst mov (tn-ref-tn values) nil-value)
162 (inst jecxz no-values)
163 (loadw (tn-ref-tn values) start -1)
164 (emit-label no-values)))
166 ;; FIXME: this is mostly copied from
167 ;; DEFAULT-UNKNOWN-VALUES.
168 (collect ((defaults))
169 (do ((i 0 (1+ i))
170 (tn-ref values (tn-ref-across tn-ref)))
171 ((null tn-ref))
172 (let ((default-lab (gen-label))
173 (tn (tn-ref-tn tn-ref))
174 (first-stack-arg-p (= i register-arg-count)))
175 (defaults (cons default-lab (cons tn first-stack-arg-p)))
176 (inst cmp count (fixnumize i))
177 (inst jmp :le default-lab)
178 (when first-stack-arg-p
179 (storew edx-tn ebx-tn -1))
180 (sc-case tn
181 ((descriptor-reg any-reg)
182 (loadw tn start (frame-word-offset (+ sp->fp-offset i))))
183 ((control-stack)
184 (loadw move-temp start
185 (frame-word-offset (+ sp->fp-offset i)))
186 (inst mov tn move-temp)))))
187 (let ((defaulting-done (gen-label)))
188 (emit-label defaulting-done)
189 (assemble (*elsewhere*)
190 (dolist (default (defaults))
191 (emit-label (car default))
192 (when (cddr default)
193 (inst push edx-tn))
194 (inst mov (second default) nil-value))
195 (inst jmp defaulting-done))))))
196 (inst mov esp-tn sp)))
198 (define-vop (nlx-entry-multiple)
199 (:args (top)
200 (source)
201 (count :target ecx))
202 ;; Again, no SC restrictions for the args, 'cause the loading would
203 ;; happen before the entry label.
204 (:info label)
205 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
206 (:temporary (:sc unsigned-reg :offset esi-offset) esi)
207 (:temporary (:sc unsigned-reg :offset edi-offset) edi)
208 (:results (result :scs (any-reg) :from (:argument 0))
209 (num :scs (any-reg control-stack)))
210 (:save-p :force-to-stack)
211 (:vop-var vop)
212 (:generator 30
213 (emit-label label)
214 (note-this-location vop :non-local-entry)
216 (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
217 ;; The 'top' arg contains the %esp value saved at the time the
218 ;; catch block was created and points to where the thrown values
219 ;; should sit.
220 (move edi top)
221 (move result edi)
223 (inst sub edi n-word-bytes)
224 (move ecx count) ; fixnum words == bytes
225 (move num ecx)
226 (inst shr ecx word-shift) ; word count for <rep movs>
227 ;; If we got zero, we be done.
228 (inst jecxz DONE)
229 ;; Copy them down.
230 (inst std)
231 (inst rep)
232 (inst movs :dword)
233 (inst cld)
234 DONE
235 ;; Reset the CSP at last moved arg.
236 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
239 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
240 (define-vop (uwp-entry)
241 (:info label)
242 (:save-p :force-to-stack)
243 (:results (block) (start) (count))
244 (:ignore block start count)
245 (:vop-var vop)
246 (:generator 0
247 (emit-label label)
248 (note-this-location vop :non-local-entry)))
250 (define-vop (unwind-to-frame-and-call)
251 (:args (ofp :scs (descriptor-reg))
252 (uwp :scs (descriptor-reg))
253 (function :scs (descriptor-reg) :to :load :target saved-function))
254 (:arg-types system-area-pointer system-area-pointer t)
255 (:temporary (:sc sap-reg) temp)
256 (:temporary (:sc descriptor-reg :offset ebx-offset) saved-function)
257 (:temporary (:sc unsigned-reg :offset eax-offset) block)
258 (:generator 22
259 ;; Store the function into a non-stack location, since we'll be
260 ;; unwinding the stack and destroying register contents before we
261 ;; use it. It turns out that EBX is preserved as part of the
262 ;; normal multiple-value handling of an unwind, so use that.
263 (move saved-function function)
265 ;; Allocate space for magic UWP block.
266 (inst sub esp-tn (* unwind-block-size n-word-bytes))
267 ;; Set up magic catch / UWP block.
268 (move block esp-tn)
269 (loadw temp uwp sap-pointer-slot other-pointer-lowtag)
270 (storew temp block unwind-block-uwp-slot)
271 (loadw temp ofp sap-pointer-slot other-pointer-lowtag)
272 (storew temp block unwind-block-cfp-slot)
274 (storew (make-fixup nil :code-object entry-label)
275 block
276 catch-block-entry-pc-slot)
278 ;; Run any required UWPs.
279 (inst jmp (make-fixup 'unwind :assembly-routine))
280 ENTRY-LABEL
282 ;; Move our saved function to where we want it now.
283 (move block saved-function)
285 ;; No parameters
286 (inst xor ecx-tn ecx-tn)
288 ;; Clear the stack
289 (inst lea esp-tn
290 (make-ea :dword :base ebp-tn
291 :disp (* (- sp->fp-offset 3) n-word-bytes)))
293 ;; Push the return-pc so it looks like we just called.
294 (pushw ebp-tn (frame-word-offset return-pc-save-offset))
296 ;; Call it
297 (inst jmp (make-ea :dword :base block
298 :disp (- (* closure-fun-slot n-word-bytes)
299 fun-pointer-lowtag)))))