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