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