0c2af51e196835eb09e60520e3ede43f02c1fff1
[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-argument-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, the eval stack pointer, and the alien stack
35 ;;;; pointer.
36 ;;;;
37 ;;;; We don't need to save/restore the current unwind-protect, since
38 ;;;; unwind-protects are implicitly processed during unwinding.
39 ;;;;
40 ;;;; We don't need to save the BSP, because that is handled automatically.
41
42 ;;; Return a list of TNs that can be used to snapshot the dynamic state for
43 ;;; use with the Save/Restore-Dynamic-Environment VOPs.
44 (!def-vm-support-routine make-dynamic-state-tns ()
45   (make-n-tns 3 *backend-t-primitive-type*))
46
47 (define-vop (save-dynamic-state)
48   (:results (catch :scs (descriptor-reg))
49             (eval :scs (descriptor-reg))
50             (alien-stack :scs (descriptor-reg)))
51   (:generator 13
52     (load-symbol-value catch *current-catch-block*)
53     (load-symbol-value eval *eval-stack-top*)
54     (load-symbol-value alien-stack *alien-stack*)))
55
56 (define-vop (restore-dynamic-state)
57   (:args (catch :scs (descriptor-reg))
58          (eval :scs (descriptor-reg))
59          (alien-stack :scs (descriptor-reg)))
60   (:generator 10
61     (store-symbol-value catch *current-catch-block*)
62     (store-symbol-value eval *eval-stack-top*)
63     (store-symbol-value alien-stack *alien-stack*)))
64
65 (define-vop (current-stack-pointer)
66   (:results (res :scs (any-reg control-stack)))
67   (:generator 1
68     (move res esp-tn)))
69
70 (define-vop (current-binding-pointer)
71   (:results (res :scs (any-reg descriptor-reg)))
72   (:generator 1
73     (load-symbol-value res *binding-stack-pointer*)))
74 \f
75 ;;;; unwind block hackery
76
77 ;;; Compute the address of the catch block from its TN, then store into the
78 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
79 (define-vop (make-unwind-block)
80   (:args (tn))
81   (:info entry-label)
82   (:temporary (:sc unsigned-reg) temp)
83   (:results (block :scs (any-reg)))
84   (:generator 22
85     (inst lea block (catch-block-ea tn))
86     (load-symbol-value temp *current-unwind-protect-block*)
87     (storew temp block unwind-block-current-uwp-slot)
88     (storew ebp-tn block unwind-block-current-cont-slot)
89     (storew (make-fixup nil :code-object entry-label)
90             block catch-block-entry-pc-slot)))
91
92 ;;; Like Make-Unwind-Block, except that we also store in the specified tag, and
93 ;;; link the block into the Current-Catch list.
94 (define-vop (make-catch-block)
95   (:args (tn)
96          (tag :scs (descriptor-reg) :to (:result 1)))
97   (:info entry-label)
98   (:results (block :scs (any-reg)))
99   (:temporary (:sc descriptor-reg) temp)
100   (:generator 44
101     (inst lea block (catch-block-ea tn))
102     (load-symbol-value temp *current-unwind-protect-block*)
103     (storew temp block  unwind-block-current-uwp-slot)
104     (storew ebp-tn block  unwind-block-current-cont-slot)
105     (storew (make-fixup nil :code-object entry-label)
106             block catch-block-entry-pc-slot)
107     (storew tag block catch-block-tag-slot)
108     (load-symbol-value temp *current-catch-block*)
109     (storew temp block catch-block-previous-catch-slot)
110     (store-symbol-value block *current-catch-block*)))
111
112 ;;; Just set the current unwind-protect to TN's address. This instantiates an
113 ;;; unwind block as an unwind-protect.
114 (define-vop (set-unwind-protect)
115   (:args (tn))
116   (:temporary (:sc unsigned-reg) new-uwp)
117   (:generator 7
118     (inst lea new-uwp (catch-block-ea tn))
119     (store-symbol-value new-uwp *current-unwind-protect-block*)))
120
121 (define-vop (unlink-catch-block)
122   (:temporary (:sc unsigned-reg) block)
123   (:policy :fast-safe)
124   (:translate %catch-breakup)
125   (:generator 17
126     (load-symbol-value block *current-catch-block*)
127     (loadw block block catch-block-previous-catch-slot)
128     (store-symbol-value block *current-catch-block*)))
129
130 (define-vop (unlink-unwind-protect)
131     (:temporary (:sc unsigned-reg) block)
132   (:policy :fast-safe)
133   (:translate %unwind-protect-breakup)
134   (:generator 17
135     (load-symbol-value block *current-unwind-protect-block*)
136     (loadw block block unwind-block-current-uwp-slot)
137     (store-symbol-value block *current-unwind-protect-block*)))
138 \f
139 ;;;; NLX entry VOPs
140 (define-vop (nlx-entry)
141   ;; Note: we can't list an sc-restriction, 'cause any load vops would
142   ;; be inserted before the return-pc label.
143   (:args (sp)
144          (start)
145          (count))
146   (:results (values :more t))
147   (:temporary (:sc descriptor-reg) move-temp)
148   (:info label nvals)
149   (:save-p :force-to-stack)
150   (:vop-var vop)
151   (:generator 30
152     (emit-label label)
153     (note-this-location vop :non-local-entry)
154     (cond ((zerop nvals))
155           ((= nvals 1)
156            (let ((no-values (gen-label)))
157              (inst mov (tn-ref-tn values) nil-value)
158              (inst jecxz no-values)
159              (loadw (tn-ref-tn values) start -1)
160              (emit-label no-values)))
161           (t
162            (collect ((defaults))
163              (do ((i 0 (1+ i))
164                   (tn-ref values (tn-ref-across tn-ref)))
165                  ((null tn-ref))
166                (let ((default-lab (gen-label))
167                      (tn (tn-ref-tn tn-ref)))
168                  (defaults (cons default-lab tn))
169
170                  (inst cmp count (fixnumize i))
171                  (inst jmp :le default-lab)
172                  (sc-case tn
173                    ((descriptor-reg any-reg)
174                     (loadw tn start (- (1+ i))))
175                    ((control-stack)
176                     (loadw move-temp start (- (1+ i)))
177                     (inst mov tn move-temp)))))
178              (let ((defaulting-done (gen-label)))
179                (emit-label defaulting-done)
180                (assemble (*elsewhere*)
181                  (dolist (def (defaults))
182                    (emit-label (car def))
183                    (inst mov (cdr def) nil-value))
184                  (inst jmp defaulting-done))))))
185     (inst mov esp-tn sp)))
186
187 (define-vop (nlx-entry-multiple)
188   (:args (top)
189          (source)
190          (count :target ecx))
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 ecx-offset :from (:argument 2)) ecx)
195   (:temporary (:sc unsigned-reg :offset esi-offset) esi)
196   (:temporary (:sc unsigned-reg :offset edi-offset) edi)
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 esi (make-ea :dword :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 edi top)
210     (move result edi)
211
212     (inst sub edi n-word-bytes)
213     (move ecx count)                    ; fixnum words == bytes
214     (move num ecx)
215     (inst shr ecx word-shift)           ; word count for <rep movs>
216     ;; If we got zero, we be done.
217     (inst jecxz done)
218     ;; Copy them down.
219     (inst std)
220     (inst rep)
221     (inst movs :dword)
222
223     DONE
224     ;; Reset the CSP at last moved arg.
225     (inst lea esp-tn (make-ea :dword :base edi :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)))