1.0.27.11: swap ocfp and return-pc slots in x86oid call frames
[sbcl.git] / src / assembly / x86-64 / 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 rax-offset)
25      (:temp ebx unsigned-reg rbx-offset)
26      (:temp ecx unsigned-reg rcx-offset)
27      (:temp esi unsigned-reg rsi-offset)
28
29      ;; These we need as temporaries.
30      (:temp edx unsigned-reg rdx-offset)
31      (:temp edi unsigned-reg rdi-offset))
32
33   ;; Pick off the cases where everything fits in register args.
34   (inst jrcxz 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 3)                      ; fixnum to raw word count
52   (inst std)                            ; count down
53   (inst sub esi 8)                      ; ?
54   (inst lea edi (make-ea :qword :base ebx :disp (- n-word-bytes)))
55   (inst rep)
56   (inst movs :qword)
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 rsp-tn (make-ea :qword :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 rsp-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 rsp-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 rsp-tn (make-ea :qword :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 rsp-tn (make-ea :qword :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 rax-offset)
124      (:temp ebx unsigned-reg rbx-offset)
125      (:temp ecx unsigned-reg rcx-offset)
126      (:temp edx unsigned-reg rdx-offset)
127      (:temp edi unsigned-reg rdi-offset)
128      (:temp esi unsigned-reg rsi-offset))
129
130   ;; Calculate NARGS (as a fixnum)
131   (move ecx esi)
132   (inst sub ecx rsp-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 rbp-tn (frame-word-offset ocfp-save-offset))
142   (loadw ebx rbp-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 3)                      ; fixnum to raw words
149   (inst std)                            ; count down
150   (inst lea edi (make-ea :qword :base rbp-tn :disp (frame-byte-offset 0)))
151   (inst sub esi (fixnumize 1))
152   (inst rep)
153   (inst movs :qword)
154   (inst cld)
155
156   ;; Load the register arguments carefully.
157   (loadw edx rbp-tn (frame-word-offset ocfp-save-offset))
158
159   ;; Restore OLD-FP and ECX.
160   (inst pop ecx)
161   ;; Overwrites a1
162   (popw rbp-tn (frame-word-offset ocfp-save-offset))
163
164   ;; Blow off the stack above the arguments.
165   (inst lea rsp-tn (make-ea :qword :base edi :disp n-word-bytes))
166
167   ;; remaining register args
168   (inst mov edi edx)
169   (loadw edx rbp-tn (frame-word-offset 0))
170   (loadw esi rbp-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
177         (make-ea :byte :base eax
178                  :disp (- (* closure-fun-slot n-word-bytes)
179                           fun-pointer-lowtag)))
180
181   ;; All the arguments fit in registers, so load them.
182   REGISTER-ARGS
183   (loadw edx esi -1)
184   (loadw edi esi -2)
185   (loadw esi esi -3)
186
187   ;; Clear most of the stack.
188   (inst lea rsp-tn
189         (make-ea :qword :base rbp-tn :disp (* -3 n-word-bytes)))
190
191   ;; Push the return-pc so it looks like we just called.
192   (pushw rbp-tn (frame-word-offset return-pc-save-offset))
193
194   ;; And away we go.
195   (inst jmp (make-ea :byte :base eax
196                      :disp (- (* closure-fun-slot n-word-bytes)
197                               fun-pointer-lowtag))))
198 \f
199 (define-assembly-routine (throw
200                           (:return-style :none))
201                          ((:arg target (descriptor-reg any-reg) rdx-offset)
202                           (:arg start any-reg rbx-offset)
203                           (:arg count any-reg rcx-offset)
204                           (:temp catch any-reg rax-offset))
205
206   (declare (ignore start count))
207
208   (load-tl-symbol-value catch *current-catch-block*)
209
210   LOOP
211
212   (let ((error (generate-error-code nil 'unseen-throw-tag-error target)))
213     (inst or catch catch)               ; check for NULL pointer
214     (inst jmp :z error))
215
216   (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
217   (inst jmp :e EXIT)
218
219   (loadw catch catch catch-block-previous-catch-slot)
220   (inst jmp LOOP)
221
222   EXIT
223
224   ;; Here EAX points to catch block containing symbol pointed to by EDX.
225   (inst jmp (make-fixup 'unwind :assembly-routine)))
226
227 ;;;; non-local exit noise
228
229 (define-assembly-routine (unwind
230                           (:return-style :none)
231                           (:translate %continue-unwind)
232                           (:policy :fast-safe))
233                          ((:arg block (any-reg descriptor-reg) rax-offset)
234                           (:arg start (any-reg descriptor-reg) rbx-offset)
235                           (:arg count (any-reg descriptor-reg) rcx-offset)
236                           (:temp uwp unsigned-reg rsi-offset))
237   (declare (ignore start count))
238
239   (let ((error (generate-error-code nil 'invalid-unwind-error)))
240     (inst or block block)               ; check for NULL pointer
241     (inst jmp :z error))
242
243   (load-tl-symbol-value uwp *current-unwind-protect-block*)
244
245   ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
246   ;; argument's CURRENT-UWP-SLOT?
247   (inst cmp uwp
248         (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
249   ;; If a match, return to context in arg block.
250   (inst jmp :e DO-EXIT)
251
252   ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
253   ;; Important! Must save (and return) the arg 'block' for later use!!
254   (move rdx-tn block)
255   (move block uwp)
256   ;; Set next unwind protect context.
257   (loadw uwp uwp unwind-block-current-uwp-slot)
258   ;; we're about to reload ebp anyway, so let's borrow it here as a
259   ;; temporary.  Hope this works
260   (store-tl-symbol-value uwp *current-unwind-protect-block* rbp-tn)
261
262   DO-EXIT
263
264   (loadw rbp-tn block unwind-block-current-cont-slot)
265
266   ;; Uwp-entry expects some things in known locations so that they can
267   ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
268   ;; count in ecx-tn.
269
270   (inst jmp (make-ea :byte :base block
271                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))