1.0.13.44: bug #414 has disappeared
[sbcl/simd.git] / src / assembly / x86 / assem-rtns.lisp
blob9465ceb21e6f6b6cbbbcbd4d97c2c61a06952bbd
1 ;;;; the machine specific support routines needed by the file assembler
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 ;;;; RETURN-MULTIPLE
16 ;;; For RETURN-MULTIPLE, we have to move the results from the end of
17 ;;; the frame for the function that is returning to the end of the
18 ;;; frame for the function being returned to.
20 #+sb-assembling ;; We don't want a vop for this one.
21 (define-assembly-routine
22 (return-multiple (:return-style :none))
23 (;; These four are really arguments.
24 (:temp eax unsigned-reg eax-offset)
25 (:temp ebx unsigned-reg ebx-offset)
26 (:temp ecx unsigned-reg ecx-offset)
27 (:temp esi unsigned-reg esi-offset)
29 ;; These we need as temporaries.
30 (:temp edx unsigned-reg edx-offset)
31 (:temp edi unsigned-reg edi-offset))
33 ;; Pick off the cases where everything fits in register args.
34 (inst jecxz zero-values)
35 (inst cmp ecx (fixnumize 1))
36 (inst jmp :e one-value)
37 (inst cmp ecx (fixnumize 2))
38 (inst jmp :e two-values)
39 (inst cmp ecx (fixnumize 3))
40 (inst jmp :e three-values)
42 ;; Save the count, because the loop is going to destroy it.
43 (inst mov edx ecx)
45 ;; Blit the values down the stack. Note: there might be overlap, so
46 ;; we have to be careful not to clobber values before we've read
47 ;; them. Because the stack builds down, we are coping to a larger
48 ;; address. Therefore, we need to iterate from larger addresses to
49 ;; smaller addresses. pfw-this says copy ecx words from esi to edi
50 ;; counting down.
51 (inst shr ecx 2) ; fixnum to raw word count
52 (inst std) ; count down
53 (inst sub esi 4) ; ?
54 (inst lea edi (make-ea :dword :base ebx :disp (- n-word-bytes)))
55 (inst rep)
56 (inst movs :dword)
58 ;; solaris requires DF being zero.
59 #!+sunos (inst cld)
61 ;; Restore the count.
62 (inst mov ecx edx)
64 ;; Set the stack top to the last result.
65 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
67 ;; Load the register args.
68 (loadw edx ebx -1)
69 (loadw edi ebx -2)
70 (loadw esi ebx -3)
72 ;; And back we go.
73 (inst stc)
74 (inst jmp eax)
76 ;; Handle the register arg cases.
77 ZERO-VALUES
78 (move esp-tn ebx)
79 (inst mov edx nil-value)
80 (inst mov edi edx)
81 (inst mov esi edx)
82 (inst stc)
83 (inst jmp eax)
85 ONE-VALUE ; Note: we can get this, because the return-multiple vop
86 ; doesn't check for this case when size > speed.
87 (loadw edx esi -1)
88 (inst mov esp-tn ebx)
89 (inst clc)
90 (inst jmp eax)
92 TWO-VALUES
93 (loadw edx esi -1)
94 (loadw edi esi -2)
95 (inst mov esi nil-value)
96 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -2 n-word-bytes)))
97 (inst stc)
98 (inst jmp eax)
100 THREE-VALUES
101 (loadw edx esi -1)
102 (loadw edi esi -2)
103 (loadw esi esi -3)
104 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -3 n-word-bytes)))
105 (inst stc)
106 (inst jmp eax))
108 ;;;; TAIL-CALL-VARIABLE
110 ;;; For tail-call-variable, we have to copy the arguments from the end
111 ;;; of our stack frame (were args are produced) to the start of our
112 ;;; stack frame (were args are expected).
114 ;;; We take the function to call in EAX and a pointer to the arguments in
115 ;;; ESI. EBP says the same over the jump, and the old frame pointer is
116 ;;; still saved in the first stack slot. The return-pc is saved in
117 ;;; the second stack slot, so we have to push it to make it look like
118 ;;; we actually called. We also have to compute ECX from the difference
119 ;;; between ESI and the stack top.
120 #+sb-assembling ;; No vop for this one either.
121 (define-assembly-routine
122 (tail-call-variable
123 (:return-style :none))
125 ((:temp eax unsigned-reg eax-offset)
126 (:temp ebx unsigned-reg ebx-offset)
127 (:temp ecx unsigned-reg ecx-offset)
128 (:temp edx unsigned-reg edx-offset)
129 (:temp edi unsigned-reg edi-offset)
130 (:temp esi unsigned-reg esi-offset))
132 ;; Calculate NARGS (as a fixnum)
133 (move ecx esi)
134 (inst sub ecx esp-tn)
136 ;; Check for all the args fitting the registers.
137 (inst cmp ecx (fixnumize 3))
138 (inst jmp :le REGISTER-ARGS)
140 ;; Save the OLD-FP and RETURN-PC because the blit it going to trash
141 ;; those stack locations. Save the ECX, because the loop is going
142 ;; to trash it.
143 (pushw ebp-tn -1)
144 (loadw ebx ebp-tn -2)
145 (inst push ecx)
147 ;; Do the blit. Because we are coping from smaller addresses to
148 ;; larger addresses, we have to start at the largest pair and work
149 ;; our way down.
150 (inst shr ecx 2) ; fixnum to raw words
151 (inst std) ; count down
152 (inst lea edi (make-ea :dword :base ebp-tn :disp (- n-word-bytes)))
153 (inst sub esi (fixnumize 1))
154 (inst rep)
155 (inst movs :dword)
157 ;; solaris requires DF being zero.
158 #!+sunos (inst cld)
160 ;; Load the register arguments carefully.
161 (loadw edx ebp-tn -1)
163 ;; Restore OLD-FP and ECX.
164 (inst pop ecx)
165 (popw ebp-tn -1) ; overwrites a0
167 ;; Blow off the stack above the arguments.
168 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
170 ;; remaining register args
171 (loadw edi ebp-tn -2)
172 (loadw esi ebp-tn -3)
174 ;; Push the (saved) return-pc so it looks like we just called.
175 (inst push ebx)
177 ;; And jump into the function.
178 (inst jmp (make-ea-for-object-slot eax closure-fun-slot fun-pointer-lowtag))
180 ;; All the arguments fit in registers, so load them.
181 REGISTER-ARGS
182 (loadw edx esi -1)
183 (loadw edi esi -2)
184 (loadw esi esi -3)
186 ;; Clear most of the stack.
187 (inst lea esp-tn
188 (make-ea :dword :base ebp-tn :disp (* -3 n-word-bytes)))
190 ;; Push the return-pc so it looks like we just called.
191 (pushw ebp-tn -2)
193 ;; And away we go.
194 (inst jmp (make-ea-for-object-slot eax closure-fun-slot fun-pointer-lowtag)))
196 (define-assembly-routine (throw
197 (:return-style :none))
198 ((:arg target (descriptor-reg any-reg) edx-offset)
199 (:arg start any-reg ebx-offset)
200 (:arg count any-reg ecx-offset)
201 (:temp catch any-reg eax-offset))
203 (declare (ignore start count))
205 (load-tl-symbol-value catch *current-catch-block*)
207 LOOP
209 (let ((error (generate-error-code nil unseen-throw-tag-error target)))
210 (inst or catch catch) ; check for NULL pointer
211 (inst jmp :z error))
213 (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
214 (inst jmp :e exit)
216 (loadw catch catch catch-block-previous-catch-slot)
217 (inst jmp loop)
219 EXIT
221 ;; Here EAX points to catch block containing symbol pointed to by EDX.
222 (inst jmp (make-fixup 'unwind :assembly-routine)))
224 ;;;; non-local exit noise
226 #!-win32
227 (define-assembly-routine (unwind
228 (:return-style :none)
229 (:translate %continue-unwind)
230 (:policy :fast-safe))
231 ((:arg block (any-reg descriptor-reg) eax-offset)
232 (:arg start (any-reg descriptor-reg) ebx-offset)
233 (:arg count (any-reg descriptor-reg) ecx-offset)
234 (:temp uwp unsigned-reg esi-offset))
235 (declare (ignore start count))
237 (let ((error (generate-error-code nil invalid-unwind-error)))
238 (inst or block block) ; check for NULL pointer
239 (inst jmp :z error))
241 (load-tl-symbol-value uwp *current-unwind-protect-block*)
243 ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
244 ;; argument's CURRENT-UWP-SLOT?
245 (inst cmp uwp
246 (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
247 ;; If a match, return to context in arg block.
248 (inst jmp :e do-exit)
250 ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
251 ;; Important! Must save (and return) the arg 'block' for later use!!
252 (move edx-tn block)
253 (move block uwp)
254 ;; Set next unwind protect context.
255 (loadw uwp uwp unwind-block-current-uwp-slot)
256 ;; we're about to reload ebp anyway, so let's borrow it here as a
257 ;; temporary. Hope this works
258 (store-tl-symbol-value uwp *current-unwind-protect-block* ebp-tn)
260 DO-EXIT
262 (loadw ebp-tn block unwind-block-current-cont-slot)
264 ;; Uwp-entry expects some things in known locations so that they can
265 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
266 ;; count in ecx-tn.
268 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
271 ;;;; Win32 non-local exit noise
273 #!+win32
274 (define-assembly-routine (unwind
275 (:return-style :none)
276 (:policy :fast-safe))
277 ((:arg block (any-reg descriptor-reg) eax-offset)
278 (:arg start (any-reg descriptor-reg) ebx-offset)
279 (:arg count (any-reg descriptor-reg) ecx-offset))
280 (declare (ignore start count))
282 (let ((error (generate-error-code nil invalid-unwind-error)))
283 (inst or block block) ; check for NULL pointer
284 (inst jmp :z error))
286 ;; Save all our registers, as we're about to clobber them.
287 (inst pusha)
289 ;; Find the SEH frame surrounding our target.
290 (loadw ecx-tn block unwind-block-next-seh-frame-slot)
292 ;; This section copied from VOP CALL-OUT.
293 ;; Setup the NPX for C; all the FP registers need to be
294 ;; empty; pop them all.
295 (dotimes (i 8)
296 (inst fstp fr0-tn))
298 ;; I'm unlikely to ever forget this again.
299 (inst cld)
301 ;; Set up a bogus stack frame for RtlUnwind to pick its return
302 ;; address from. (Yes, this is how RtlUnwind works.)
303 (inst push (make-fixup 'win32-unwind-tail :assembly-routine))
304 (inst push ebp-tn)
305 (inst mov ebp-tn esp-tn)
307 ;; Actually call out for the unwind.
308 (inst push 0)
309 (inst push 0)
310 (inst push 0)
311 (inst push ecx-tn)
312 (inst call (make-fixup "RtlUnwind@16" :foreign)))
314 ;; We want no VOP for this one and for it to only happen on Win32
315 ;; targets. Hence the following disaster.
316 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
317 (define-assembly-routine
318 (win32-unwind-tail (:return-style :none))
319 ((:temp block unsigned-reg eax-offset))
321 ;; The unwind returns here. Had to use a VOP for this because
322 ;; PUSH won't accept a label as an argument.
324 ;; Clean up the bogus stack frame we pushed for the unwind.
325 (inst pop ebp-tn)
326 (inst pop esi-tn) ;; Random scratch register.
328 ;; This section based on VOP CALL-OUT.
329 ;; Restore the NPX for lisp; ensure no regs are empty
330 (dotimes (i 8)
331 (inst fldz))
333 ;; Restore our regs.
334 (inst popa)
336 ;; By now we've unwound all the UWP frames required, so we
337 ;; just jump to our target block.
338 (loadw ebp-tn block unwind-block-current-cont-slot)
340 ;; Nlx-entry expects the arg start in ebx-tn and the arg count
341 ;; in ecx-tn. Fortunately, that's where they are already.
342 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
345 ;;;; Win32 UWP block SEH interface.
347 ;; We want no VOP for this one and for it to only happen on Win32
348 ;; targets. Hence the following disaster.
349 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
350 (define-assembly-routine
351 (uwp-seh-handler (:return-style :none))
352 ((:temp block unsigned-reg eax-offset))
354 ;; We get called for any exception which happens within our
355 ;; dynamic contour that isn't handled below us, and for
356 ;; unwinding.
358 ;; For the exceptions we just return ExceptionContinueSearch.
360 ;; Find the exception record.
361 (inst mov eax-tn (make-ea :dword :base esp-tn :disp 4))
363 ;; Check unwind flags.
364 (inst test (make-ea :byte :base eax-tn :disp 4) 6) ; EH_UNWINDING | EH_EXIT_UNWIND
366 ;; To see if we're unwinding or not.
367 (inst jmp :nz UNWINDING)
369 ;; We're not unwinding, so we're not interested.
370 (inst mov eax-tn 1) ;; exception-continue-search
371 (inst ret)
373 ;; For the unwinds we establish a basic environment as per
374 ;; call_into_lisp, but without the extra SEH frame (the theory
375 ;; being that we're already in a Lisp SEH context), and invoke
376 ;; our UWP block to unwind itself.
378 ;; FIXME: Do we need to establish an SEH frame anyway? And do
379 ;; we need to do the same stack frame hackery for the debugger
380 ;; as we do for the main exception handler?
382 ;; When the UWP block calls %continue-unwind, we come back to
383 ;; the next assembly routine, below, which reinitializes for C
384 ;; and returns to the Win32 unwind machinery.
386 ;; If the UWP block sees fit to do a non-local exit, things
387 ;; Just Work, thanks to the Win32 API being sanely designed
388 ;; and our complying with it.
390 ;; We also must update *current-unwind-protect-block* before
391 ;; calling the cleanup function.
393 UNWINDING
395 ;; Save all registers (overkill)
396 (inst pusha)
398 ;; Establish our stack frame.
399 (inst mov ebp-tn esp-tn)
401 ;; This section based on VOP CALL-OUT.
402 ;; Restore the NPX for lisp; ensure no regs are empty
403 (dotimes (i 8)
404 (inst fldz))
406 ;; Find our unwind-block by way of our SEH frame.
407 (inst mov block (make-ea :dword :base ebp-tn :disp #x28))
408 (inst lea block (make-ea :dword :base block
409 :disp (- (* unwind-block-next-seh-frame-slot
410 n-word-bytes))))
412 ;; Update *CURRENT-UNWIND-PROTECT-BLOCK*.
413 (loadw ebx-tn block unwind-block-current-uwp-slot)
414 (store-tl-symbol-value ebx-tn *current-unwind-protect-block* ecx-tn)
416 ;; Uwp-entry expects some things in known locations so that they can
417 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
418 ;; count in ecx-tn. We don't actually have any of that here, but we
419 ;; do need to have access to our own stack frame, so we hijack the
420 ;; known locations to cover our own state.
422 (inst xor ebx-tn ebx-tn)
423 (inst xor ecx-tn ecx-tn)
424 (inst mov ebx-tn ebp-tn)
425 (loadw ebp-tn block unwind-block-current-cont-slot)
426 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
428 #!+win32
429 (define-assembly-routine (continue-unwind
430 (:return-style :none)
431 (:translate %continue-unwind)
432 (:policy :fast-safe))
433 ((:arg block (any-reg descriptor-reg) eax-offset)
434 (:arg start (any-reg descriptor-reg) ebx-offset)
435 (:arg count (any-reg descriptor-reg) ecx-offset))
436 (declare (ignore block count))
437 ;; The args here are mostly ignored because we're using the
438 ;; win32 unwind mechanism and keep all that elsewhere. The
439 ;; exception is START, which we use to pass the saved EBP for
440 ;; our exception handler.
442 ;; "All" we have to do here is reload our EBP, reestablish a C
443 ;; environment, and return ExceptionContinueSearch. The OS
444 ;; handles the rest.
446 ;; Restore our frame pointer.
447 (inst mov esp-tn start)
449 ;; This section copied from VOP CALL-OUT.
450 ;; Setup the NPX for C; all the FP registers need to be
451 ;; empty; pop them all.
452 (dotimes (i 8)
453 (inst fstp fr0-tn))
455 ;; I'm unlikely to ever forget this again.
456 (inst cld)
458 ;; Restore our saved registers
459 (inst popa)
461 ;; And we're done.
462 (inst mov eax-tn 1) ;; exception-continue-search
463 (inst ret))