Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / compiler / x86 / nlx.lisp
blob5ec5c04709e5898be2500c1dd5975536d2aa593c
1 ;;;; the definition of non-local exit for the x86 VM
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.
12 (in-package "SB!VM")
14 ;;; Make an environment-live stack TN for saving the SP for NLX entry.
15 (defun make-nlx-sp-tn (env)
16 (physenv-live-tn
17 (make-representation-tn *fixnum-primitive-type* any-reg-sc-number)
18 env))
20 ;;; Make a TN for the argument count passing location for a non-local entry.
21 (defun make-nlx-entry-arg-start-location ()
22 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ebx-offset))
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))))
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. (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.
43 (define-vop (save-dynamic-state)
44 (:results (catch :scs (descriptor-reg)))
45 (:generator 13
46 (load-tl-symbol-value catch *current-catch-block*)))
48 (define-vop (restore-dynamic-state)
49 (:args (catch :scs (descriptor-reg)))
50 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
51 (:generator 10
52 (store-tl-symbol-value catch *current-catch-block* temp)))
54 (define-vop (current-stack-pointer)
55 (:results (res :scs (any-reg control-stack)))
56 (:generator 1
57 (move res esp-tn)))
59 (define-vop (current-binding-pointer)
60 (:results (res :scs (any-reg descriptor-reg)))
61 (:generator 1
62 (load-binding-stack-pointer res)))
64 ;;;; unwind block hackery
66 ;;; Compute the address of the catch block from its TN, then store into the
67 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
68 (define-vop (make-unwind-block)
69 (:args (tn))
70 (:info entry-label)
71 (:temporary (:sc unsigned-reg) temp)
72 (:results (block :scs (any-reg)))
73 (:generator 22
74 (inst lea block (catch-block-ea tn))
75 (load-tl-symbol-value temp *current-unwind-protect-block*)
76 (storew temp block unwind-block-current-uwp-slot)
77 (storew ebp-tn block unwind-block-current-cont-slot)
78 (storew (make-fixup nil :code-object entry-label)
79 block catch-block-entry-pc-slot)
80 #!+win32
81 (progn
82 (inst mov temp (make-ea :dword :disp 0) :fs)
83 (storew temp block unwind-block-next-seh-frame-slot))))
85 ;;; like MAKE-UNWIND-BLOCK, except that we also store in the specified
86 ;;; tag, and link the block into the CURRENT-CATCH list
87 (define-vop (make-catch-block)
88 (:args (tn)
89 (tag :scs (any-reg descriptor-reg) :to (:result 1)))
90 (:info entry-label)
91 (:results (block :scs (any-reg)))
92 (:temporary (:sc descriptor-reg) temp)
93 (:generator 44
94 (inst lea block (catch-block-ea tn))
95 (load-tl-symbol-value temp *current-unwind-protect-block*)
96 (storew temp block unwind-block-current-uwp-slot)
97 (storew ebp-tn block unwind-block-current-cont-slot)
98 (storew (make-fixup nil :code-object entry-label)
99 block catch-block-entry-pc-slot)
100 #!+win32
101 (progn
102 (inst mov temp (make-ea :dword :disp 0) :fs)
103 (storew temp block unwind-block-next-seh-frame-slot))
104 (storew tag block catch-block-tag-slot)
105 (load-tl-symbol-value temp *current-catch-block*)
106 (storew temp block catch-block-previous-catch-slot)
107 (store-tl-symbol-value block *current-catch-block* temp)))
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 #!+sb-thread tls #!+win32 seh-frame)
114 (:generator 7
115 (inst lea new-uwp (catch-block-ea tn))
116 #!+win32
117 (progn
118 (storew (make-fixup 'uwp-seh-handler :assembly-routine)
119 new-uwp unwind-block-seh-frame-handler-slot)
120 (inst lea seh-frame
121 (make-ea-for-object-slot new-uwp
122 unwind-block-next-seh-frame-slot 0))
123 (inst mov (make-ea :dword :disp 0) seh-frame :fs))
124 (store-tl-symbol-value new-uwp *current-unwind-protect-block* tls)))
126 (define-vop (unlink-catch-block)
127 (:temporary (:sc unsigned-reg) #!+sb-thread tls block)
128 (:policy :fast-safe)
129 (:translate %catch-breakup)
130 (:generator 17
131 (load-tl-symbol-value block *current-catch-block*)
132 (loadw block block catch-block-previous-catch-slot)
133 (store-tl-symbol-value block *current-catch-block* tls)))
135 (define-vop (unlink-unwind-protect)
136 ;; NOTE: When we have both #!+sb-thread and #!+win32, we only need one temp
137 (:temporary (:sc unsigned-reg) block #!+sb-thread tls #!+win32 seh-frame)
138 (:policy :fast-safe)
139 (:translate %unwind-protect-breakup)
140 (:generator 17
141 (load-tl-symbol-value block *current-unwind-protect-block*)
142 #!+win32
143 (progn
144 (loadw seh-frame block unwind-block-next-seh-frame-slot)
145 (inst mov (make-ea :dword :disp 0) seh-frame :fs))
146 (loadw block block unwind-block-current-uwp-slot)
147 (store-tl-symbol-value block *current-unwind-protect-block* tls)))
149 ;;;; NLX entry VOPs
150 (define-vop (nlx-entry)
151 ;; Note: we can't list an sc-restriction, 'cause any load vops would
152 ;; be inserted before the return-pc label.
153 (:args (sp)
154 (start)
155 (count))
156 (:results (values :more t))
157 (:temporary (:sc descriptor-reg) move-temp)
158 (:info label nvals)
159 (:save-p :force-to-stack)
160 (:vop-var vop)
161 (:generator 30
162 (emit-label label)
163 (note-this-location vop :non-local-entry)
164 (cond ((zerop nvals))
165 ((= nvals 1)
166 (let ((no-values (gen-label)))
167 (inst mov (tn-ref-tn values) nil-value)
168 (inst jecxz no-values)
169 (loadw (tn-ref-tn values) start -1)
170 (emit-label no-values)))
172 ;; FIXME: this is mostly copied from
173 ;; DEFAULT-UNKNOWN-VALUES.
174 (collect ((defaults))
175 (do ((i 0 (1+ i))
176 (tn-ref values (tn-ref-across tn-ref)))
177 ((null tn-ref))
178 (let ((default-lab (gen-label))
179 (tn (tn-ref-tn tn-ref))
180 (first-stack-arg-p (= i register-arg-count)))
181 (defaults (cons default-lab (cons tn first-stack-arg-p)))
182 (inst cmp count (fixnumize i))
183 (inst jmp :le default-lab)
184 (when first-stack-arg-p
185 (storew edx-tn ebx-tn -1))
186 (sc-case tn
187 ((descriptor-reg any-reg)
188 (loadw tn start (frame-word-offset (+ sp->fp-offset i))))
189 ((control-stack)
190 (loadw move-temp start
191 (frame-word-offset (+ sp->fp-offset i)))
192 (inst mov tn move-temp)))))
193 (let ((defaulting-done (gen-label)))
194 (emit-label defaulting-done)
195 (assemble (*elsewhere*)
196 (dolist (default (defaults))
197 (emit-label (car default))
198 (when (cddr default)
199 (inst push edx-tn))
200 (inst mov (second default) nil-value))
201 (inst jmp defaulting-done))))))
202 (inst mov esp-tn sp)))
204 (define-vop (nlx-entry-multiple)
205 (:args (top)
206 (source)
207 (count :target ecx))
208 ;; Again, no SC restrictions for the args, 'cause the loading would
209 ;; happen before the entry label.
210 (:info label)
211 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
212 (:temporary (:sc unsigned-reg :offset esi-offset) esi)
213 (:temporary (:sc unsigned-reg :offset edi-offset) edi)
214 (:results (result :scs (any-reg) :from (:argument 0))
215 (num :scs (any-reg control-stack)))
216 (:save-p :force-to-stack)
217 (:vop-var vop)
218 (:generator 30
219 (emit-label label)
220 (note-this-location vop :non-local-entry)
222 (inst lea esi (make-ea :dword :base source :disp (- n-word-bytes)))
223 ;; The 'top' arg contains the %esp value saved at the time the
224 ;; catch block was created and points to where the thrown values
225 ;; should sit.
226 (move edi top)
227 (move result edi)
229 (inst sub edi n-word-bytes)
230 (move ecx count) ; fixnum words == bytes
231 (move num ecx)
232 (inst shr ecx word-shift) ; word count for <rep movs>
233 ;; If we got zero, we be done.
234 (inst jecxz DONE)
235 ;; Copy them down.
236 (inst std)
237 (inst rep)
238 (inst movs :dword)
239 (inst cld)
240 DONE
241 ;; Reset the CSP at last moved arg.
242 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))))
245 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
246 (define-vop (uwp-entry)
247 (:info label)
248 (:save-p :force-to-stack)
249 (:results (block) (start) (count))
250 (:ignore block start count)
251 (:vop-var vop)
252 (:generator 0
253 (emit-label label)
254 (note-this-location vop :non-local-entry)))
256 (define-vop (unwind-to-frame-and-call)
257 (:args (ofp :scs (descriptor-reg))
258 (uwp :scs (descriptor-reg))
259 (function :scs (descriptor-reg) :to :load :target saved-function))
260 (:arg-types system-area-pointer system-area-pointer t)
261 (:temporary (:sc sap-reg) temp)
262 (:temporary (:sc descriptor-reg :offset ebx-offset) saved-function)
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. It turns out that EBX is preserved as part of the
268 ;; normal multiple-value handling of an unwind, so use that.
269 (move saved-function function)
271 ;; Allocate space for magic UWP block.
272 (inst sub esp-tn (* unwind-block-size n-word-bytes))
273 ;; Set up magic catch / UWP block.
274 (move block esp-tn)
275 (loadw temp uwp sap-pointer-slot other-pointer-lowtag)
276 (storew temp block unwind-block-current-uwp-slot)
277 (loadw temp ofp sap-pointer-slot other-pointer-lowtag)
278 (storew temp block unwind-block-current-cont-slot)
280 (storew (make-fixup nil :code-object entry-label)
281 block
282 catch-block-entry-pc-slot)
284 ;; Run any required UWPs.
285 (inst jmp (make-fixup 'unwind :assembly-routine))
286 ENTRY-LABEL
288 ;; Move our saved function to where we want it now.
289 (move block saved-function)
291 ;; No parameters
292 (inst xor ecx-tn ecx-tn)
294 ;; Clear the stack
295 (inst lea esp-tn
296 (make-ea :dword :base ebp-tn
297 :disp (* (- sp->fp-offset 3) n-word-bytes)))
299 ;; Push the return-pc so it looks like we just called.
300 (pushw ebp-tn (frame-word-offset return-pc-save-offset))
302 ;; Call it
303 (inst jmp (make-ea :dword :base block
304 :disp (- (* closure-fun-slot n-word-bytes)
305 fun-pointer-lowtag)))))