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