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