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)))
58 ;; solaris requires DF being zero.
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.
76 ;; Handle the register arg cases.
79 (inst mov edx nil-value)
85 ONE-VALUE ; Note: we can get this, because the return-multiple vop
86 ; doesn't check for this case when size > speed.
95 (inst mov esi nil-value)
96 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -2 n-word-bytes)))
104 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -3 n-word-bytes)))
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
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)
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
144 (loadw ebx ebp-tn -2)
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
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))
157 ;; solaris requires DF being zero.
160 ;; Load the register arguments carefully.
161 (loadw edx ebp-tn -1)
163 ;; Restore OLD-FP and 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.
177 ;; And jump into the function.
179 (make-ea :byte :base eax
180 :disp (- (* closure-fun-slot n-word-bytes)
181 fun-pointer-lowtag)))
183 ;; All the arguments fit in registers, so load them.
189 ;; Clear most of the stack.
191 (make-ea :dword :base ebp-tn :disp (* -3 n-word-bytes)))
193 ;; Push the return-pc so it looks like we just called.
197 (inst jmp (make-ea :byte :base eax
198 :disp (- (* closure-fun-slot n-word-bytes)
199 fun-pointer-lowtag))))
201 (define-assembly-routine (throw
202 (:return-style :none))
203 ((:arg target (descriptor-reg any-reg) edx-offset)
204 (:arg start any-reg ebx-offset)
205 (:arg count any-reg ecx-offset)
206 (:temp catch any-reg eax-offset))
208 (declare (ignore start count))
210 (load-tl-symbol-value catch *current-catch-block*)
214 (let ((error (generate-error-code nil unseen-throw-tag-error target)))
215 (inst or catch catch) ; check for NULL pointer
218 (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
221 (loadw catch catch catch-block-previous-catch-slot)
226 ;; Here EAX points to catch block containing symbol pointed to by EDX.
227 (inst jmp (make-fixup 'unwind :assembly-routine)))
229 ;;;; non-local exit noise
231 (define-assembly-routine (unwind
232 (:return-style :none)
233 (:translate %continue-unwind)
234 (:policy :fast-safe))
235 ((:arg block (any-reg descriptor-reg) eax-offset)
236 (:arg start (any-reg descriptor-reg) ebx-offset)
237 (:arg count (any-reg descriptor-reg) ecx-offset)
238 (:temp uwp unsigned-reg esi-offset))
239 (declare (ignore start count))
241 (let ((error (generate-error-code nil invalid-unwind-error)))
242 (inst or block block) ; check for NULL pointer
245 (load-tl-symbol-value uwp *current-unwind-protect-block*)
247 ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
248 ;; argument's CURRENT-UWP-SLOT?
250 (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
251 ;; If a match, return to context in arg block.
252 (inst jmp :e do-exit)
254 ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
255 ;; Important! Must save (and return) the arg 'block' for later use!!
259 ;; We need to check for Win32 exception frames before overwriting
260 ;; *C-U-P-B* (if the Win32 frames NLX, we need the UWP to still be
261 ;; live.) As of this writing, we can't take a Win32 NLX across our
262 ;; frames, but the frame can NLX to another foreign frame that
263 ;; doesn't cross ours and then return normally, and if we drop the
264 ;; UWP beforehand then we just broke UWP semantics.
267 (inst fs-segment-prefix)
268 (inst cmp block (make-ea :dword))
269 (inst jmp :le NO-WIN32-UNWIND)
270 (inst call WIN32-UNWIND)
273 ;; Set next unwind protect context.
274 (loadw uwp uwp unwind-block-current-uwp-slot)
275 ;; we're about to reload ebp anyway, so let's borrow it here as a
276 ;; temporary. Hope this works
277 (store-tl-symbol-value uwp *current-unwind-protect-block* ebp-tn)
281 ;; Same as above with *C-U-P-B*, except that this is for our target
282 ;; block, not a UWP. Still need to check for Win32 exception frames.
285 (inst fs-segment-prefix)
286 (inst cmp block (make-ea :dword))
287 (inst jmp :le NO-WIN32-UNWIND)
288 (inst call WIN32-UNWIND)
291 (loadw ebp-tn block unwind-block-current-cont-slot)
293 ;; Uwp-entry expects some things in known locations so that they can
294 ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
297 (inst jmp (make-ea :byte :base block
298 :disp (* unwind-block-entry-pc-slot n-word-bytes)))
302 ;; At this point we need to call RtlUnwind@16 to clear up one or
303 ;; more Win32 exception frames on the stack. This is an unusual FFI
304 ;; in that it kills most of the registers, and it returns to the
305 ;; address at [EBP+4].
308 ;; Regs get clobbered by this process, so save the lot of them.
311 ;; Okay, our current unwind target is in BLOCK (EAX). All of our
312 ;; other regs are on the stack. We need to find the first Win32
313 ;; exception frame that we -aren't- going to unwind.
314 (inst fs-segment-prefix)
315 (inst mov ecx-tn (make-ea :dword))
317 (inst cmp block ecx-tn)
318 (inst jmp :le FOUND-TARGET-FRAME)
319 (inst mov ecx-tn (make-ea :dword :base ecx-tn))
320 (inst jmp FIND-TARGET-FRAME)
323 ;; This section copied from VOP CALL-OUT.
324 ;; Setup the NPX for C; all the FP registers need to be
325 ;; empty; pop them all.
329 ;; I'm unlikely to ever forget this again.
332 ;; Set up a bogus stack frame for RtlUnwind to pick its return
333 ;; address from. (Yes, this is how RtlUnwind works.)
334 (inst push (make-fixup 'win32-unwind-tail :assembly-routine))
336 (inst mov ebp-tn esp-tn)
338 ;; Actually call out for the unwind.
343 (inst call (make-fixup "RtlUnwind@16" :foreign))))
345 ;; We want no VOP for this one and for it to only happen on Win32
346 ;; targets. Hence the following disaster.
350 (define-assembly-routine
351 (win32-unwind-tail (:return-style :none))
354 ;; The unwind returns here. Had to use a VOP for this because
355 ;; PUSH won't accept a label as an argument.
357 ;; Clean up the bogus stack frame we pushed for the unwind.
359 (inst pop esi-tn) ;; Random scratch register.
361 ;; This section based on VOP CALL-OUT.
362 ;; Restore the NPX for lisp; ensure no regs are empty
366 ;; Restore our regs and pick up where we left off.