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
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.
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.
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
51 (inst shr ecx 2) ; fixnum to raw word count
52 (inst std) ; count down
54 (inst lea edi (make-ea :dword :base ebx :disp (- n-word-bytes)))
62 ;; Set the stack top to the last result.
63 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
65 ;; Load the register args.
74 ;; Handle the register arg cases.
77 (inst mov edx nil-value)
83 ONE-VALUE ; Note: we can get this, because the return-multiple vop
84 ; doesn't check for this case when size > speed.
93 (inst mov esi nil-value)
94 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -2 n-word-bytes)))
102 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -3 n-word-bytes)))
106 ;;;; TAIL-CALL-VARIABLE
108 ;;; For tail-call-variable, we have to copy the arguments from the end
109 ;;; of our stack frame (were args are produced) to the start of our
110 ;;; stack frame (were args are expected).
112 ;;; We take the function to call in EAX and a pointer to the arguments in
113 ;;; ESI. EBP says the same over the jump, and the old frame pointer is
114 ;;; still saved in the first stack slot. The return-pc is saved in
115 ;;; the second stack slot, so we have to push it to make it look like
116 ;;; we actually called. We also have to compute ECX from the difference
117 ;;; between ESI and the stack top.
118 #+sb-assembling ;; No vop for this one either.
119 (define-assembly-routine
121 (:return-style :none))
123 ((:temp eax unsigned-reg eax-offset)
124 (:temp ebx unsigned-reg ebx-offset)
125 (:temp ecx unsigned-reg ecx-offset)
126 (:temp edx unsigned-reg edx-offset)
127 (:temp edi unsigned-reg edi-offset)
128 (:temp esi unsigned-reg esi-offset))
130 ;; Calculate NARGS (as a fixnum)
132 (inst sub ecx esp-tn)
134 ;; Check for all the args fitting the registers.
135 (inst cmp ecx (fixnumize 3))
136 (inst jmp :le REGISTER-ARGS)
138 ;; Save the OLD-FP and RETURN-PC because the blit it going to trash
139 ;; those stack locations. Save the ECX, because the loop is going
142 (loadw ebx ebp-tn -2)
145 ;; Do the blit. Because we are coping from smaller addresses to
146 ;; larger addresses, we have to start at the largest pair and work
148 (inst shr ecx 2) ; fixnum to raw words
149 (inst std) ; count down
150 (inst lea edi (make-ea :dword :base ebp-tn :disp (- n-word-bytes)))
151 (inst sub esi (fixnumize 1))
156 ;; Load the register arguments carefully.
157 (loadw edx ebp-tn -1)
159 ;; Restore OLD-FP and ECX.
161 (popw ebp-tn -1) ; overwrites a0
163 ;; Blow off the stack above the arguments.
164 (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
166 ;; remaining register args
167 (loadw edi ebp-tn -2)
168 (loadw esi ebp-tn -3)
170 ;; Push the (saved) return-pc so it looks like we just called.
173 ;; And jump into the function.
174 (inst jmp (make-ea-for-object-slot eax closure-fun-slot fun-pointer-lowtag))
176 ;; All the arguments fit in registers, so load them.
182 ;; Clear most of the stack.
184 (make-ea :dword :base ebp-tn :disp (* -3 n-word-bytes)))
186 ;; Push the return-pc so it looks like we just called.
190 (inst jmp (make-ea-for-object-slot eax closure-fun-slot fun-pointer-lowtag)))
192 (define-assembly-routine (throw
193 (:return-style :none))
194 ((:arg target (descriptor-reg any-reg) edx-offset)
195 (:arg start any-reg ebx-offset)
196 (:arg count any-reg ecx-offset)
197 (:temp catch any-reg eax-offset))
199 (declare (ignore start count))
201 (load-tl-symbol-value catch *current-catch-block*)
205 (let ((error (generate-error-code nil 'unseen-throw-tag-error target)))
206 (inst or catch catch) ; check for NULL pointer
209 (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
212 (loadw catch catch catch-block-previous-catch-slot)
217 ;; Here EAX points to catch block containing symbol pointed to by EDX.
218 (inst jmp (make-fixup 'unwind :assembly-routine)))
220 ;;;; non-local exit noise
223 (define-assembly-routine (unwind
224 (:return-style :none)
225 (:translate %continue-unwind)
226 (:policy :fast-safe))
227 ((:arg block (any-reg descriptor-reg) eax-offset)
228 (:arg start (any-reg descriptor-reg) ebx-offset)
229 (:arg count (any-reg descriptor-reg) ecx-offset)
230 (:temp uwp unsigned-reg esi-offset))
231 (declare (ignore start count))
233 (let ((error (generate-error-code nil 'invalid-unwind-error)))
234 (inst or block block) ; check for NULL pointer
237 (load-tl-symbol-value uwp *current-unwind-protect-block*)
239 ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
240 ;; argument's CURRENT-UWP-SLOT?
242 (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
243 ;; If a match, return to context in arg block.
244 (inst jmp :e do-exit)
246 ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
247 ;; Important! Must save (and return) the arg 'block' for later use!!
250 ;; Set next unwind protect context.
251 (loadw uwp uwp unwind-block-current-uwp-slot)
252 ;; we're about to reload ebp anyway, so let's borrow it here as a
253 ;; temporary. Hope this works
254 (store-tl-symbol-value uwp *current-unwind-protect-block* ebp-tn)
258 (loadw ebp-tn block unwind-block-current-cont-slot)
260 ;; Uwp-entry expects some things in known locations so that they can
261 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
264 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
267 ;;;; Win32 non-local exit noise
270 (define-assembly-routine (unwind
271 (:return-style :none)
272 (:policy :fast-safe))
273 ((:arg block (any-reg descriptor-reg) eax-offset)
274 (:arg start (any-reg descriptor-reg) ebx-offset)
275 (:arg count (any-reg descriptor-reg) ecx-offset))
276 (declare (ignore start count))
278 (let ((error (generate-error-code nil 'invalid-unwind-error)))
279 (inst or block block) ; check for NULL pointer
282 ;; Save all our registers, as we're about to clobber them.
285 ;; Find the SEH frame surrounding our target.
286 (loadw ecx-tn block unwind-block-next-seh-frame-slot)
288 ;; This section copied from VOP CALL-OUT.
289 ;; Setup the NPX for C; all the FP registers need to be
290 ;; empty; pop them all.
294 ;; I'm unlikely to ever forget this again.
297 ;; Set up a bogus stack frame for RtlUnwind to pick its return
298 ;; address from. (Yes, this is how RtlUnwind works.)
299 (inst push (make-fixup 'win32-unwind-tail :assembly-routine))
301 (inst mov ebp-tn esp-tn)
303 ;; Actually call out for the unwind.
308 (inst call (make-fixup "RtlUnwind@16" :foreign)))
310 ;; We want no VOP for this one and for it to only happen on Win32
311 ;; targets. Hence the following disaster.
312 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
313 (define-assembly-routine
314 (win32-unwind-tail (:return-style :none))
315 ((:temp block unsigned-reg eax-offset))
317 ;; The unwind returns here. Had to use a VOP for this because
318 ;; PUSH won't accept a label as an argument.
320 ;; Clean up the bogus stack frame we pushed for the unwind.
322 (inst pop esi-tn) ;; Random scratch register.
324 ;; This section based on VOP CALL-OUT.
325 ;; Restore the NPX for lisp; ensure no regs are empty
332 ;; By now we've unwound all the UWP frames required, so we
333 ;; just jump to our target block.
334 (loadw ebp-tn block unwind-block-current-cont-slot)
336 ;; Nlx-entry expects the arg start in ebx-tn and the arg count
337 ;; in ecx-tn. Fortunately, that's where they are already.
338 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
341 ;;;; Win32 UWP block SEH interface.
343 ;; We want no VOP for this one and for it to only happen on Win32
344 ;; targets. Hence the following disaster.
345 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
346 (define-assembly-routine
347 (uwp-seh-handler (:return-style :none))
348 ((:temp block unsigned-reg eax-offset))
350 ;; We get called for any exception which happens within our
351 ;; dynamic contour that isn't handled below us, and for
354 ;; For the exceptions we just return ExceptionContinueSearch.
356 ;; Find the exception record.
357 (inst mov eax-tn (make-ea :dword :base esp-tn :disp 4))
359 ;; Check unwind flags.
360 (inst test (make-ea :byte :base eax-tn :disp 4) 6) ; EH_UNWINDING | EH_EXIT_UNWIND
362 ;; To see if we're unwinding or not.
363 (inst jmp :nz UNWINDING)
365 ;; We're not unwinding, so we're not interested.
366 (inst mov eax-tn 1) ;; exception-continue-search
369 ;; For the unwinds we establish a basic environment as per
370 ;; call_into_lisp, but without the extra SEH frame (the theory
371 ;; being that we're already in a Lisp SEH context), and invoke
372 ;; our UWP block to unwind itself.
374 ;; FIXME: Do we need to establish an SEH frame anyway? And do
375 ;; we need to do the same stack frame hackery for the debugger
376 ;; as we do for the main exception handler?
378 ;; When the UWP block calls %continue-unwind, we come back to
379 ;; the next assembly routine, below, which reinitializes for C
380 ;; and returns to the Win32 unwind machinery.
382 ;; If the UWP block sees fit to do a non-local exit, things
383 ;; Just Work, thanks to the Win32 API being sanely designed
384 ;; and our complying with it.
386 ;; We also must update *current-unwind-protect-block* before
387 ;; calling the cleanup function.
391 ;; Save all registers (overkill)
394 ;; Establish our stack frame.
395 (inst mov ebp-tn esp-tn)
397 ;; This section based on VOP CALL-OUT.
398 ;; Restore the NPX for lisp; ensure no regs are empty
402 ;; Find our unwind-block by way of our SEH frame.
403 (inst mov block (make-ea :dword :base ebp-tn :disp #x28))
404 (inst lea block (make-ea :dword :base block
405 :disp (- (* unwind-block-next-seh-frame-slot
408 ;; Update *CURRENT-UNWIND-PROTECT-BLOCK*.
409 (loadw ebx-tn block unwind-block-current-uwp-slot)
410 (store-tl-symbol-value ebx-tn *current-unwind-protect-block* ecx-tn)
412 ;; Uwp-entry expects some things in known locations so that they can
413 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
414 ;; count in ecx-tn. We don't actually have any of that here, but we
415 ;; do need to have access to our own stack frame, so we hijack the
416 ;; known locations to cover our own state.
418 (inst xor ebx-tn ebx-tn)
419 (inst xor ecx-tn ecx-tn)
420 (inst mov ebx-tn ebp-tn)
421 (loadw ebp-tn block unwind-block-current-cont-slot)
422 (inst jmp (make-ea-for-object-slot block unwind-block-entry-pc-slot 0)))
425 (define-assembly-routine (continue-unwind
426 (:return-style :none)
427 (:translate %continue-unwind)
428 (:policy :fast-safe))
429 ((:arg block (any-reg descriptor-reg) eax-offset)
430 (:arg start (any-reg descriptor-reg) ebx-offset)
431 (:arg count (any-reg descriptor-reg) ecx-offset))
432 (declare (ignore block count))
433 ;; The args here are mostly ignored because we're using the
434 ;; win32 unwind mechanism and keep all that elsewhere. The
435 ;; exception is START, which we use to pass the saved EBP for
436 ;; our exception handler.
438 ;; "All" we have to do here is reload our EBP, reestablish a C
439 ;; environment, and return ExceptionContinueSearch. The OS
442 ;; Restore our frame pointer.
443 (inst mov esp-tn start)
445 ;; This section copied from VOP CALL-OUT.
446 ;; Setup the NPX for C; all the FP registers need to be
447 ;; empty; pop them all.
451 ;; I'm unlikely to ever forget this again.
454 ;; Restore our saved registers
458 (inst mov eax-tn 1) ;; exception-continue-search