1.0.4.8:
[sbcl.git] / src / assembly / x86 / assem-rtns.lisp
1 ;;;; the machine specific support routines needed by the file assembler
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 \f
14 ;;;; RETURN-MULTIPLE
15
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.
19
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)
28
29      ;; These we need as temporaries.
30      (:temp edx unsigned-reg edx-offset)
31      (:temp edi unsigned-reg edi-offset))
32
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)
41
42   ;; Save the count, because the loop is going to destroy it.
43   (inst mov edx ecx)
44
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)
57
58   ;; solaris requires DF being zero.
59   #!+sunos (inst cld)
60
61   ;; Restore the count.
62   (inst mov ecx edx)
63
64   ;; Set the stack top to the last result.
65   (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
66
67   ;; Load the register args.
68   (loadw edx ebx -1)
69   (loadw edi ebx -2)
70   (loadw esi ebx -3)
71
72   ;; And back we go.
73   (inst stc)
74   (inst jmp eax)
75
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)
84
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)
91
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)
99
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))
107 \f
108 ;;;; TAIL-CALL-VARIABLE
109
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).
113 ;;;
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))
124
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))
131
132   ;; Calculate NARGS (as a fixnum)
133   (move ecx esi)
134   (inst sub ecx esp-tn)
135
136   ;; Check for all the args fitting the registers.
137   (inst cmp ecx (fixnumize 3))
138   (inst jmp :le REGISTER-ARGS)
139
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)
146
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)
156
157   ;; solaris requires DF being zero.
158   #!+sunos (inst cld)
159
160   ;; Load the register arguments carefully.
161   (loadw edx ebp-tn -1)
162
163   ;; Restore OLD-FP and ECX.
164   (inst pop ecx)
165   (popw ebp-tn -1)                      ; overwrites a0
166
167   ;; Blow off the stack above the arguments.
168   (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
169
170   ;; remaining register args
171   (loadw edi ebp-tn -2)
172   (loadw esi ebp-tn -3)
173
174   ;; Push the (saved) return-pc so it looks like we just called.
175   (inst push ebx)
176
177   ;; And jump into the function.
178     (inst jmp
179           (make-ea :byte :base eax
180                    :disp (- (* closure-fun-slot n-word-bytes)
181                             fun-pointer-lowtag)))
182
183   ;; All the arguments fit in registers, so load them.
184   REGISTER-ARGS
185   (loadw edx esi -1)
186   (loadw edi esi -2)
187   (loadw esi esi -3)
188
189   ;; Clear most of the stack.
190   (inst lea esp-tn
191         (make-ea :dword :base ebp-tn :disp (* -3 n-word-bytes)))
192
193   ;; Push the return-pc so it looks like we just called.
194   (pushw ebp-tn -2)
195
196   ;; And away we go.
197   (inst jmp (make-ea :byte :base eax
198                      :disp (- (* closure-fun-slot n-word-bytes)
199                               fun-pointer-lowtag))))
200 \f
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))
207
208   (declare (ignore start count))
209
210   (load-tl-symbol-value catch *current-catch-block*)
211
212   LOOP
213
214   (let ((error (generate-error-code nil unseen-throw-tag-error target)))
215     (inst or catch catch)               ; check for NULL pointer
216     (inst jmp :z error))
217
218   (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
219   (inst jmp :e exit)
220
221   (loadw catch catch catch-block-previous-catch-slot)
222   (inst jmp loop)
223
224   EXIT
225
226   ;; Here EAX points to catch block containing symbol pointed to by EDX.
227   (inst jmp (make-fixup 'unwind :assembly-routine)))
228
229 ;;;; non-local exit noise
230
231 #!-win32
232 (define-assembly-routine (unwind
233                           (:return-style :none)
234                           (:translate %continue-unwind)
235                           (:policy :fast-safe))
236                          ((:arg block (any-reg descriptor-reg) eax-offset)
237                           (:arg start (any-reg descriptor-reg) ebx-offset)
238                           (:arg count (any-reg descriptor-reg) ecx-offset)
239                           (:temp uwp unsigned-reg esi-offset))
240   (declare (ignore start count))
241
242   (let ((error (generate-error-code nil invalid-unwind-error)))
243     (inst or block block)               ; check for NULL pointer
244     (inst jmp :z error))
245
246   (load-tl-symbol-value uwp *current-unwind-protect-block*)
247
248   ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
249   ;; argument's CURRENT-UWP-SLOT?
250   (inst cmp uwp
251         (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
252   ;; If a match, return to context in arg block.
253   (inst jmp :e do-exit)
254
255   ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
256   ;; Important! Must save (and return) the arg 'block' for later use!!
257   (move edx-tn block)
258   (move block uwp)
259   ;; Set next unwind protect context.
260   (loadw uwp uwp unwind-block-current-uwp-slot)
261   ;; we're about to reload ebp anyway, so let's borrow it here as a
262   ;; temporary.  Hope this works
263   (store-tl-symbol-value uwp *current-unwind-protect-block* ebp-tn)
264
265   DO-EXIT
266
267   (loadw ebp-tn block unwind-block-current-cont-slot)
268
269   ;; Uwp-entry expects some things in known locations so that they can
270   ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
271   ;; count in ecx-tn.
272
273   (inst jmp (make-ea :byte :base block
274                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))
275
276
277 ;;;; Win32 non-local exit noise
278
279 #!+win32
280 (define-assembly-routine (unwind
281                           (:return-style :none)
282                           (:policy :fast-safe))
283                          ((:arg block (any-reg descriptor-reg) eax-offset)
284                           (:arg start (any-reg descriptor-reg) ebx-offset)
285                           (:arg count (any-reg descriptor-reg) ecx-offset))
286   (declare (ignore start count))
287
288   (let ((error (generate-error-code nil invalid-unwind-error)))
289     (inst or block block)               ; check for NULL pointer
290     (inst jmp :z error))
291
292   ;; Save all our registers, as we're about to clobber them.
293   (inst pusha)
294
295   ;; Find the SEH frame surrounding our target.
296   (loadw ecx-tn block unwind-block-next-seh-frame-slot)
297
298   ;; This section copied from VOP CALL-OUT.
299   ;; Setup the NPX for C; all the FP registers need to be
300   ;; empty; pop them all.
301   (dotimes (i 8)
302     (inst fstp fr0-tn))
303
304   ;; I'm unlikely to ever forget this again.
305   (inst cld)
306
307   ;; Set up a bogus stack frame for RtlUnwind to pick its return
308   ;; address from.  (Yes, this is how RtlUnwind works.)
309   (inst push (make-fixup 'win32-unwind-tail :assembly-routine))
310   (inst push ebp-tn)
311   (inst mov ebp-tn esp-tn)
312
313   ;; Actually call out for the unwind.
314   (inst push 0)
315   (inst push 0)
316   (inst push 0)
317   (inst push ecx-tn)
318   (inst call (make-fixup "RtlUnwind@16" :foreign)))
319
320 ;; We want no VOP for this one and for it to only happen on Win32
321 ;; targets.  Hence the following disaster.
322 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
323 (define-assembly-routine
324     (win32-unwind-tail (:return-style :none))
325     ((:temp block unsigned-reg eax-offset))
326
327   ;; The unwind returns here.  Had to use a VOP for this because
328   ;; PUSH won't accept a label as an argument.
329
330   ;; Clean up the bogus stack frame we pushed for the unwind.
331   (inst pop ebp-tn)
332   (inst pop esi-tn) ;; Random scratch register.
333
334   ;; This section based on VOP CALL-OUT.
335   ;; Restore the NPX for lisp; ensure no regs are empty
336   (dotimes (i 8)
337     (inst fldz))
338
339   ;; Restore our regs.
340   (inst popa)
341
342   ;; By now we've unwound all the UWP frames required, so we
343   ;; just jump to our target block.
344   (loadw ebp-tn block unwind-block-current-cont-slot)
345
346   ;; Nlx-entry expects the arg start in ebx-tn and the arg count
347   ;; in ecx-tn.  Fortunately, that's where they are already.
348   (inst jmp (make-ea :byte :base block
349                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))
350
351
352 ;;;; Win32 UWP block SEH interface.
353
354 ;; We want no VOP for this one and for it to only happen on Win32
355 ;; targets.  Hence the following disaster.
356 #!+#.(cl:if (cl:member sb-assembling cl:*features*) win32 '(or))
357 (define-assembly-routine
358     (uwp-seh-handler (:return-style :none))
359     ((:temp block unsigned-reg eax-offset))
360
361   ;; We get called for any exception which happens within our
362   ;; dynamic contour that isn't handled below us, and for
363   ;; unwinding.
364
365   ;; For the exceptions we just return ExceptionContinueSearch.
366
367   ;; Find the exception record.
368   (inst mov eax-tn (make-ea :dword :base esp-tn :disp 4))
369
370   ;; Check unwind flags.
371   (inst test (make-ea :byte :base eax-tn :disp 4) 6) ; EH_UNWINDING | EH_EXIT_UNWIND
372
373   ;; To see if we're unwinding or not.
374   (inst jmp :nz UNWINDING)
375
376   ;; We're not unwinding, so we're not interested.
377   (inst mov eax-tn 1) ;; exception-continue-search
378   (inst ret)
379
380   ;; For the unwinds we establish a basic environment as per
381   ;; call_into_lisp, but without the extra SEH frame (the theory
382   ;; being that we're already in a Lisp SEH context), and invoke
383   ;; our UWP block to unwind itself.
384
385   ;; FIXME: Do we need to establish an SEH frame anyway?  And do
386   ;; we need to do the same stack frame hackery for the debugger
387   ;; as we do for the main exception handler?
388
389   ;; When the UWP block calls %continue-unwind, we come back to
390   ;; the next assembly routine, below, which reinitializes for C
391   ;; and returns to the Win32 unwind machinery.
392
393   ;; If the UWP block sees fit to do a non-local exit, things
394   ;; Just Work, thanks to the Win32 API being sanely designed
395   ;; and our complying with it.
396
397   ;; We also must update *current-unwind-protect-block* before
398   ;; calling the cleanup function.
399
400   UNWINDING
401
402   ;; Save all registers (overkill)
403   (inst pusha)
404
405   ;; Establish our stack frame.
406   (inst mov ebp-tn esp-tn)
407
408   ;; This section based on VOP CALL-OUT.
409   ;; Restore the NPX for lisp; ensure no regs are empty
410   (dotimes (i 8)
411     (inst fldz))
412
413   ;; Find our unwind-block by way of our SEH frame.
414   (inst mov block (make-ea :dword :base ebp-tn :disp #x28))
415   (inst lea block (make-ea :dword :base block
416                            :disp (- (* unwind-block-next-seh-frame-slot
417                                        n-word-bytes))))
418
419   ;; Update *CURRENT-UNWIND-PROTECT-BLOCK*.
420   (loadw ebx-tn block unwind-block-current-uwp-slot)
421   (store-tl-symbol-value ebx-tn *current-unwind-protect-block* ecx-tn)
422
423   ;; Uwp-entry expects some things in known locations so that they can
424   ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
425   ;; count in ecx-tn.  We don't actually have any of that here, but we
426   ;; do need to have access to our own stack frame, so we hijack the
427   ;; known locations to cover our own state.
428
429   (inst xor ebx-tn ebx-tn)
430   (inst xor ecx-tn ecx-tn)
431   (inst mov ebx-tn ebp-tn)
432   (loadw ebp-tn block unwind-block-current-cont-slot)
433   (inst jmp (make-ea :byte :base block
434                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))
435
436 #!+win32
437 (define-assembly-routine (continue-unwind
438                           (:return-style :none)
439                           (:translate %continue-unwind)
440                           (:policy :fast-safe))
441                          ((:arg block (any-reg descriptor-reg) eax-offset)
442                           (:arg start (any-reg descriptor-reg) ebx-offset)
443                           (:arg count (any-reg descriptor-reg) ecx-offset))
444   (declare (ignore block count))
445   ;; The args here are mostly ignored because we're using the
446   ;; win32 unwind mechanism and keep all that elsewhere.  The
447   ;; exception is START, which we use to pass the saved EBP for
448   ;; our exception handler.
449
450   ;; "All" we have to do here is reload our EBP, reestablish a C
451   ;; environment, and return ExceptionContinueSearch.  The OS
452   ;; handles the rest.
453
454   ;; Restore our frame pointer.
455   (inst mov esp-tn start)
456
457   ;; This section copied from VOP CALL-OUT.
458   ;; Setup the NPX for C; all the FP registers need to be
459   ;; empty; pop them all.
460   (dotimes (i 8)
461     (inst fstp fr0-tn))
462
463   ;; I'm unlikely to ever forget this again.
464   (inst cld)
465
466   ;; Restore our saved registers
467   (inst popa)
468
469   ;; And we're done.
470   (inst mov eax-tn 1) ;; exception-continue-search
471   (inst ret))