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