d220ffe1b8d842fc9e0f30c4824808b9b532d61b
[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 (- (* (+ (tn-offset tn) catch-block-size) n-word-bytes))))
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-symbol-value catch *current-catch-block*)
48     (load-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   (:generator 10
54     (store-symbol-value catch *current-catch-block*)
55     (store-symbol-value alien-stack *alien-stack*)))
56
57 (define-vop (current-stack-pointer)
58   (:results (res :scs (any-reg control-stack)))
59   (:generator 1
60     (move res esp-tn)))
61
62 (define-vop (current-binding-pointer)
63   (:results (res :scs (any-reg descriptor-reg)))
64   (:generator 1
65     (load-symbol-value res *binding-stack-pointer*)))
66 \f
67 ;;;; unwind block hackery
68
69 ;;; Compute the address of the catch block from its TN, then store into the
70 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
71 (define-vop (make-unwind-block)
72   (:args (tn))
73   (:info entry-label)
74   (:temporary (:sc unsigned-reg) temp)
75   (:results (block :scs (any-reg)))
76   (:generator 22
77     (inst lea block (catch-block-ea tn))
78     (load-symbol-value temp *current-unwind-protect-block*)
79     (storew temp block unwind-block-current-uwp-slot)
80     (storew ebp-tn block unwind-block-current-cont-slot)
81     (storew (make-fixup nil :code-object entry-label)
82             block catch-block-entry-pc-slot)))
83
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-symbol-value temp *current-unwind-protect-block*)
95     (storew temp block  unwind-block-current-uwp-slot)
96     (storew ebp-tn block  unwind-block-current-cont-slot)
97     (storew (make-fixup nil :code-object entry-label)
98             block catch-block-entry-pc-slot)
99     (storew tag block catch-block-tag-slot)
100     (load-symbol-value temp *current-catch-block*)
101     (storew temp block catch-block-previous-catch-slot)
102     (store-symbol-value block *current-catch-block*)))
103
104 ;;; Just set the current unwind-protect to TN's address. This instantiates an
105 ;;; unwind block as an unwind-protect.
106 (define-vop (set-unwind-protect)
107   (:args (tn))
108   (:temporary (:sc unsigned-reg) new-uwp)
109   (:generator 7
110     (inst lea new-uwp (catch-block-ea tn))
111     (store-symbol-value new-uwp *current-unwind-protect-block*)))
112
113 (define-vop (unlink-catch-block)
114   (:temporary (:sc unsigned-reg) block)
115   (:policy :fast-safe)
116   (:translate %catch-breakup)
117   (:generator 17
118     (load-symbol-value block *current-catch-block*)
119     (loadw block block catch-block-previous-catch-slot)
120     (store-symbol-value block *current-catch-block*)))
121
122 (define-vop (unlink-unwind-protect)
123     (:temporary (:sc unsigned-reg) block)
124   (:policy :fast-safe)
125   (:translate %unwind-protect-breakup)
126   (:generator 17
127     (load-symbol-value block *current-unwind-protect-block*)
128     (loadw block block unwind-block-current-uwp-slot)
129     (store-symbol-value block *current-unwind-protect-block*)))
130 \f
131 ;;;; NLX entry VOPs
132 (define-vop (nlx-entry)
133   ;; Note: we can't list an sc-restriction, 'cause any load vops would
134   ;; be inserted before the return-pc label.
135   (:args (sp)
136          (start)
137          (count))
138   (:results (values :more t))
139   (:temporary (:sc descriptor-reg) move-temp)
140   (:info label nvals)
141   (:save-p :force-to-stack)
142   (:vop-var vop)
143   (:generator 30
144     (emit-label label)
145     (note-this-location vop :non-local-entry)
146     (cond ((zerop nvals))
147           ((= nvals 1)
148            (let ((no-values (gen-label)))
149              (inst mov (tn-ref-tn values) nil-value)
150              (inst jecxz no-values)
151              (loadw (tn-ref-tn values) start -1)
152              (emit-label no-values)))
153           (t
154            (collect ((defaults))
155              (do ((i 0 (1+ i))
156                   (tn-ref values (tn-ref-across tn-ref)))
157                  ((null tn-ref))
158                (let ((default-lab (gen-label))
159                      (tn (tn-ref-tn tn-ref)))
160                  (defaults (cons default-lab tn))
161
162                  (inst cmp count (fixnumize i))
163                  (inst jmp :le default-lab)
164                  (sc-case tn
165                    ((descriptor-reg any-reg)
166                     (loadw tn start (- (1+ i))))
167                    ((control-stack)
168                     (loadw move-temp start (- (1+ i)))
169                     (inst mov tn move-temp)))))
170              (let ((defaulting-done (gen-label)))
171                (emit-label defaulting-done)
172                (assemble (*elsewhere*)
173                  (dolist (def (defaults))
174                    (emit-label (car def))
175                    (inst mov (cdr def) nil-value))
176                  (inst jmp defaulting-done))))))
177     (inst mov esp-tn sp)))
178
179 (define-vop (nlx-entry-multiple)
180   (:args (top)
181          (source)
182          (count :target ecx))
183   ;; Again, no SC restrictions for the args, 'cause the loading would
184   ;; happen before the entry label.
185   (:info label)
186   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
187   (:temporary (:sc unsigned-reg :offset esi-offset) esi)
188   (:temporary (:sc unsigned-reg :offset edi-offset) edi)
189   (:results (result :scs (any-reg) :from (:argument 0))
190             (num :scs (any-reg control-stack)))
191   (:save-p :force-to-stack)
192   (:vop-var vop)
193   (:generator 30
194     (emit-label label)
195     (note-this-location vop :non-local-entry)
196
197     (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
198     ;; The 'top' arg contains the %esp value saved at the time the
199     ;; catch block was created and points to where the thrown values
200     ;; should sit.
201     (move edi top)
202     (move result edi)
203
204     (inst sub edi n-word-bytes)
205     (move ecx count)                    ; fixnum words == bytes
206     (move num ecx)
207     (inst shr ecx word-shift)           ; word count for <rep movs>
208     ;; If we got zero, we be done.
209     (inst jecxz done)
210     ;; Copy them down.
211     (inst std)
212     (inst rep)
213     (inst movs :dword)
214
215     DONE
216     ;; Reset the CSP at last moved arg.
217     (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
218
219
220 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
221 (define-vop (uwp-entry)
222   (:info label)
223   (:save-p :force-to-stack)
224   (:results (block) (start) (count))
225   (:ignore block start count)
226   (:vop-var vop)
227   (:generator 0
228     (emit-label label)
229     (note-this-location vop :non-local-entry)))