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