1.0.6.24: a more sophisticated UNWIND-TO-FRAME-AND-CALL for x86 and x86-64
[sbcl.git] / src / compiler / x86 / 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 ebx-offset))
23
24 (defun catch-block-ea (tn)
25   (aver (sc-is tn catch-block))
26   (make-ea :dword :base ebp-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 esp-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 ebp-tn block unwind-block-current-cont-slot)
82     (storew (make-fixup nil :code-object entry-label)
83             block catch-block-entry-pc-slot)
84     #!+win32
85     (progn
86       (inst fs-segment-prefix)
87       (inst mov temp (make-ea :dword :disp 0))
88       (storew temp block unwind-block-next-seh-frame-slot))))
89
90 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
91 ;;; tag, and link the block into the CURRENT-CATCH list
92 (define-vop (make-catch-block)
93   (:args (tn)
94          (tag :scs (any-reg descriptor-reg) :to (:result 1)))
95   (:info entry-label)
96   (:results (block :scs (any-reg)))
97   (:temporary (:sc descriptor-reg) temp)
98   (:generator 44
99     (inst lea block (catch-block-ea tn))
100     (load-tl-symbol-value temp *current-unwind-protect-block*)
101     (storew temp block  unwind-block-current-uwp-slot)
102     (storew ebp-tn block  unwind-block-current-cont-slot)
103     (storew (make-fixup nil :code-object entry-label)
104             block catch-block-entry-pc-slot)
105     #!+win32
106     (progn
107       (inst fs-segment-prefix)
108       (inst mov temp (make-ea :dword :disp 0))
109       (storew temp block unwind-block-next-seh-frame-slot))
110     (storew tag block catch-block-tag-slot)
111     (load-tl-symbol-value temp *current-catch-block*)
112     (storew temp block catch-block-previous-catch-slot)
113     (store-tl-symbol-value block *current-catch-block* temp)))
114
115 ;;; Just set the current unwind-protect to TN's address. This instantiates an
116 ;;; unwind block as an unwind-protect.
117 (define-vop (set-unwind-protect)
118   (:args (tn))
119   (:temporary (:sc unsigned-reg) new-uwp #!+sb-thread tls #!+win32 seh-frame)
120   (:generator 7
121     (inst lea new-uwp (catch-block-ea tn))
122     #!+win32
123     (progn
124       (storew (make-fixup 'uwp-seh-handler :assembly-routine)
125               new-uwp unwind-block-seh-frame-handler-slot)
126       (inst lea seh-frame
127             (make-ea-for-object-slot new-uwp
128                                      unwind-block-next-seh-frame-slot 0))
129       (inst fs-segment-prefix)
130       (inst mov (make-ea :dword :disp 0) seh-frame))
131     (store-tl-symbol-value new-uwp *current-unwind-protect-block* tls)))
132
133 (define-vop (unlink-catch-block)
134   (:temporary (:sc unsigned-reg) #!+sb-thread tls block)
135   (:policy :fast-safe)
136   (:translate %catch-breakup)
137   (:generator 17
138     (load-tl-symbol-value block *current-catch-block*)
139     (loadw block block catch-block-previous-catch-slot)
140     (store-tl-symbol-value block *current-catch-block* tls)))
141
142 (define-vop (unlink-unwind-protect)
143     ;; NOTE: When we have both #!+sb-thread and #!+win32, we only need one temp
144     (:temporary (:sc unsigned-reg) block #!+sb-thread tls #!+win32 seh-frame)
145   (:policy :fast-safe)
146   (:translate %unwind-protect-breakup)
147   (:generator 17
148     (load-tl-symbol-value block *current-unwind-protect-block*)
149     #!+win32
150     (progn
151       (loadw seh-frame block unwind-block-next-seh-frame-slot)
152       (inst fs-segment-prefix)
153       (inst mov (make-ea :dword :disp 0) seh-frame))
154     (loadw block block unwind-block-current-uwp-slot)
155     (store-tl-symbol-value block *current-unwind-protect-block* tls)))
156 \f
157 ;;;; NLX entry VOPs
158 (define-vop (nlx-entry)
159   ;; Note: we can't list an sc-restriction, 'cause any load vops would
160   ;; be inserted before the return-pc label.
161   (:args (sp)
162          (start)
163          (count))
164   (:results (values :more t))
165   (:temporary (:sc descriptor-reg) move-temp)
166   (:info label nvals)
167   (:save-p :force-to-stack)
168   (:vop-var vop)
169   (:generator 30
170     (emit-label label)
171     (note-this-location vop :non-local-entry)
172     (cond ((zerop nvals))
173           ((= nvals 1)
174            (let ((no-values (gen-label)))
175              (inst mov (tn-ref-tn values) nil-value)
176              (inst jecxz no-values)
177              (loadw (tn-ref-tn values) start -1)
178              (emit-label no-values)))
179           (t
180            (collect ((defaults))
181              (do ((i 0 (1+ i))
182                   (tn-ref values (tn-ref-across tn-ref)))
183                  ((null tn-ref))
184                (let ((default-lab (gen-label))
185                      (tn (tn-ref-tn tn-ref)))
186                  (defaults (cons default-lab tn))
187
188                  (inst cmp count (fixnumize i))
189                  (inst jmp :le default-lab)
190                  (sc-case tn
191                    ((descriptor-reg any-reg)
192                     (loadw tn start (frame-word-offset i)))
193                    ((control-stack)
194                     (loadw move-temp start (frame-word-offset i))
195                     (inst mov tn move-temp)))))
196              (let ((defaulting-done (gen-label)))
197                (emit-label defaulting-done)
198                (assemble (*elsewhere*)
199                  (dolist (def (defaults))
200                    (emit-label (car def))
201                    (inst mov (cdr def) nil-value))
202                  (inst jmp defaulting-done))))))
203     (inst mov esp-tn sp)))
204
205 (define-vop (nlx-entry-multiple)
206   (:args (top)
207          (source)
208          (count :target ecx))
209   ;; Again, no SC restrictions for the args, 'cause the loading would
210   ;; happen before the entry label.
211   (:info label)
212   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
213   (:temporary (:sc unsigned-reg :offset esi-offset) esi)
214   (:temporary (:sc unsigned-reg :offset edi-offset) edi)
215   (:results (result :scs (any-reg) :from (:argument 0))
216             (num :scs (any-reg control-stack)))
217   (:save-p :force-to-stack)
218   (:vop-var vop)
219   (:generator 30
220     (emit-label label)
221     (note-this-location vop :non-local-entry)
222
223     (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
224     ;; The 'top' arg contains the %esp value saved at the time the
225     ;; catch block was created and points to where the thrown values
226     ;; should sit.
227     (move edi top)
228     (move result edi)
229
230     (inst sub edi n-word-bytes)
231     (move ecx count)                    ; fixnum words == bytes
232     (move num ecx)
233     (inst shr ecx word-shift)           ; word count for <rep movs>
234     ;; If we got zero, we be done.
235     (inst jecxz done)
236     ;; Copy them down.
237     (inst std)
238     (inst rep)
239     (inst movs :dword)
240
241     DONE
242     ;; Reset the CSP at last moved arg.
243     (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
244
245
246 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
247 (define-vop (uwp-entry)
248   (:info label)
249   (:save-p :force-to-stack)
250   (:results (block) (start) (count))
251   (:ignore block start count)
252   (:vop-var vop)
253   (:generator 0
254     (emit-label label)
255     (note-this-location vop :non-local-entry)))
256
257 (define-vop (unwind-to-frame-and-call)
258     (:args (ofp :scs (descriptor-reg))
259            (uwp :scs (descriptor-reg))
260            (function :scs (descriptor-reg)))
261   (:arg-types system-area-pointer system-area-pointer t)
262   (:temporary (:sc sap-reg) temp)
263   (:temporary (:sc unsigned-reg :offset eax-offset) block)
264   (:generator 22
265     ;; Store the function into a non-stack location, since we'll be
266     ;; unwinding the stack and destroying register contents before we
267     ;; use it.
268     (store-tl-symbol-value function
269                            *unwind-to-frame-function*
270                            temp)
271
272     ;; Allocate space for magic UWP block.
273     (inst sub esp-tn unwind-block-size)
274     ;; Set up magic catch / UWP block.
275     (move block esp-tn)
276     (loadw temp uwp sap-pointer-slot other-pointer-lowtag)
277     (storew temp block unwind-block-current-uwp-slot)
278     (loadw temp ofp sap-pointer-slot other-pointer-lowtag)
279     (storew temp block unwind-block-current-cont-slot)
280
281     (storew (make-fixup nil :code-object entry-label)
282             block
283             catch-block-entry-pc-slot)
284
285     ;; Run any required UWPs.
286     (inst jmp (make-fixup 'unwind :assembly-routine))
287     ENTRY-LABEL
288
289     ;; Load function from symbol
290     (load-tl-symbol-value block *unwind-to-frame-function*)
291
292     ;; No parameters
293     (inst xor ecx-tn ecx-tn)
294
295     ;; Clear the stack
296     (inst lea esp-tn
297           (make-ea :dword :base ebp-tn :disp (* -3 n-word-bytes)))
298
299     ;; Push the return-pc so it looks like we just called.
300     (pushw ebp-tn -2)
301
302     ;; Call it
303     (inst jmp (make-ea :dword :base block
304                        :disp (- (* closure-fun-slot n-word-bytes)
305                                 fun-pointer-lowtag)))))