1.0.27.13: more RET on x86oids
[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 are really arguments.
24      (:temp ecx unsigned-reg rcx-offset)
25      (:temp esi unsigned-reg rsi-offset)
26
27      ;; These we need as temporaries.
28      (:temp eax unsigned-reg rax-offset)
29      (:temp ebx unsigned-reg rbx-offset)
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   (inst mov ebx rbp-tn)
43   ;; Save the count, because the loop is going to destroy it.
44   (inst mov edx ecx)
45   (inst mov eax (make-ea :qword :base rbp-tn
46                          :disp (frame-byte-offset return-pc-save-offset)))
47   (inst mov rbp-tn (make-ea :qword :base rbp-tn
48                             :disp (frame-byte-offset ocfp-save-offset)))
49   ;; Blit the values down the stack. Note: there might be overlap, so
50   ;; we have to be careful not to clobber values before we've read
51   ;; them. Because the stack builds down, we are copying to a larger
52   ;; address. Therefore, we need to iterate from larger addresses to
53   ;; smaller addresses. pfw-this says copy ecx words from esi to edi
54   ;; counting down.
55   (inst shr ecx (1- n-lowtag-bits))
56   (inst std)                            ; count down
57   (inst sub esi n-word-bytes)
58   (inst lea edi (make-ea :qword :base ebx :disp (- n-word-bytes)))
59   (inst rep)
60   (inst movs :qword)
61   (inst cld)
62
63   ;; Restore the count.
64   (inst mov ecx edx)
65
66   ;; Set the stack top to the last result.
67   (inst lea rsp-tn (make-ea :qword :base edi :disp n-word-bytes))
68
69   ;; Load the register args.
70   (loadw edx ebx -1)
71   (loadw edi ebx -2)
72   (loadw esi ebx -3)
73
74   ;; And back we go.
75   (inst stc)
76   (inst push eax)
77   (inst ret)
78
79   ;; Handle the register arg cases.
80   ZERO-VALUES
81   (inst mov ebx rbp-tn)
82   (inst mov edx nil-value)
83   (inst mov edi edx)
84   (inst mov esi edx)
85   (inst lea rsp-tn
86         (make-ea :qword :base ebx
87                  :disp (frame-byte-offset ocfp-save-offset)))
88   (inst stc)
89   (inst pop rbp-tn)
90   (inst ret)
91
92   ;; Note: we can get this, because the return-multiple vop doesn't
93   ;; check for this case when size > speed.
94   ONE-VALUE
95   (loadw edx esi -1)
96   (inst lea rsp-tn
97         (make-ea :qword :base rbp-tn
98                  :disp (frame-byte-offset ocfp-save-offset)))
99   (inst clc)
100   (inst pop rbp-tn)
101   (inst ret)
102
103   TWO-VALUES
104   (inst mov ebx rbp-tn)
105   (loadw edx esi -1)
106   (loadw edi esi -2)
107   (inst mov esi nil-value)
108   (inst lea rsp-tn
109         (make-ea :qword :base ebx
110                  :disp (frame-byte-offset ocfp-save-offset)))
111   (inst stc)
112   (inst pop rbp-tn)
113   (inst ret)
114
115   THREE-VALUES
116   (inst mov ebx rbp-tn)
117   (loadw edx esi -1)
118   (loadw edi esi -2)
119   (loadw esi esi -3)
120   (inst lea rsp-tn
121         (make-ea :qword :base ebx
122                  :disp (frame-byte-offset ocfp-save-offset)))
123   (inst stc)
124   (inst pop rbp-tn)
125   (inst ret))
126 \f
127 ;;;; TAIL-CALL-VARIABLE
128
129 ;;; For tail-call-variable, we have to copy the arguments from the end
130 ;;; of our stack frame (were args are produced) to the start of our
131 ;;; stack frame (were args are expected).
132 ;;;
133 ;;; We take the function to call in EAX and a pointer to the arguments in
134 ;;; ESI. EBP says the same over the jump, and the old frame pointer is
135 ;;; still saved in the first stack slot. The return-pc is saved in
136 ;;; the second stack slot, so we have to push it to make it look like
137 ;;; we actually called. We also have to compute ECX from the difference
138 ;;; between ESI and the stack top.
139 #+sb-assembling ;; No vop for this one either.
140 (define-assembly-routine
141     (tail-call-variable
142      (:return-style :none))
143
144     ((:temp eax unsigned-reg rax-offset)
145      (:temp ebx unsigned-reg rbx-offset)
146      (:temp ecx unsigned-reg rcx-offset)
147      (:temp edx unsigned-reg rdx-offset)
148      (:temp edi unsigned-reg rdi-offset)
149      (:temp esi unsigned-reg rsi-offset))
150
151   ;; Calculate NARGS (as a fixnum)
152   (move ecx esi)
153   (inst sub ecx rsp-tn)
154
155   ;; Check for all the args fitting the registers.
156   (inst cmp ecx (fixnumize 3))
157   (inst jmp :le REGISTER-ARGS)
158
159   ;; Save the OLD-FP and RETURN-PC because the blit is going to trash
160   ;; those stack locations. Save the ECX, because the loop is going to
161   ;; trash it.
162   (pushw rbp-tn (frame-word-offset ocfp-save-offset))
163   (loadw ebx rbp-tn (frame-word-offset return-pc-save-offset))
164   (inst push ecx)
165
166   ;; Do the blit. Because we are coping from smaller addresses to
167   ;; larger addresses, we have to start at the largest pair and work
168   ;; our way down.
169   (inst shr ecx (1- n-lowtag-bits))
170   (inst std)                            ; count down
171   (inst lea edi (make-ea :qword :base rbp-tn :disp (frame-byte-offset 0)))
172   (inst sub esi (fixnumize 1))
173   (inst rep)
174   (inst movs :qword)
175   (inst cld)
176
177   ;; Load the register arguments carefully.
178   (loadw edx rbp-tn (frame-word-offset ocfp-save-offset))
179
180   ;; Restore OLD-FP and ECX.
181   (inst pop ecx)
182   ;; Overwrites a1
183   (popw rbp-tn (frame-word-offset ocfp-save-offset))
184
185   ;; Blow off the stack above the arguments.
186   (inst lea rsp-tn (make-ea :qword :base edi :disp n-word-bytes))
187
188   ;; remaining register args
189   (inst mov edi edx)
190   (loadw edx rbp-tn (frame-word-offset 0))
191   (loadw esi rbp-tn (frame-word-offset 2))
192
193   ;; Push the (saved) return-pc so it looks like we just called.
194   (inst push ebx)
195
196   ;; And jump into the function.
197   (inst jmp
198         (make-ea :byte :base eax
199                  :disp (- (* closure-fun-slot n-word-bytes)
200                           fun-pointer-lowtag)))
201
202   ;; All the arguments fit in registers, so load them.
203   REGISTER-ARGS
204   (loadw edx esi -1)
205   (loadw edi esi -2)
206   (loadw esi esi -3)
207
208   ;; Clear most of the stack.
209   (inst lea rsp-tn
210         (make-ea :qword :base rbp-tn :disp (* -3 n-word-bytes)))
211
212   ;; Push the return-pc so it looks like we just called.
213   (pushw rbp-tn (frame-word-offset return-pc-save-offset))
214
215   ;; And away we go.
216   (inst jmp (make-ea :byte :base eax
217                      :disp (- (* closure-fun-slot n-word-bytes)
218                               fun-pointer-lowtag))))
219 \f
220 (define-assembly-routine (throw
221                           (:return-style :none))
222                          ((:arg target (descriptor-reg any-reg) rdx-offset)
223                           (:arg start any-reg rbx-offset)
224                           (:arg count any-reg rcx-offset)
225                           (:temp catch any-reg rax-offset))
226
227   (declare (ignore start count))
228
229   (load-tl-symbol-value catch *current-catch-block*)
230
231   LOOP
232
233   (let ((error (generate-error-code nil 'unseen-throw-tag-error target)))
234     (inst or catch catch)               ; check for NULL pointer
235     (inst jmp :z error))
236
237   (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
238   (inst jmp :e EXIT)
239
240   (loadw catch catch catch-block-previous-catch-slot)
241   (inst jmp LOOP)
242
243   EXIT
244
245   ;; Here EAX points to catch block containing symbol pointed to by EDX.
246   (inst jmp (make-fixup 'unwind :assembly-routine)))
247
248 ;;;; non-local exit noise
249
250 (define-assembly-routine (unwind
251                           (:return-style :none)
252                           (:translate %continue-unwind)
253                           (:policy :fast-safe))
254                          ((:arg block (any-reg descriptor-reg) rax-offset)
255                           (:arg start (any-reg descriptor-reg) rbx-offset)
256                           (:arg count (any-reg descriptor-reg) rcx-offset)
257                           (:temp uwp unsigned-reg rsi-offset))
258   (declare (ignore start count))
259
260   (let ((error (generate-error-code nil 'invalid-unwind-error)))
261     (inst or block block)               ; check for NULL pointer
262     (inst jmp :z error))
263
264   (load-tl-symbol-value uwp *current-unwind-protect-block*)
265
266   ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
267   ;; argument's CURRENT-UWP-SLOT?
268   (inst cmp uwp
269         (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
270   ;; If a match, return to context in arg block.
271   (inst jmp :e DO-EXIT)
272
273   ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
274   ;; Important! Must save (and return) the arg 'block' for later use!!
275   (move rdx-tn block)
276   (move block uwp)
277   ;; Set next unwind protect context.
278   (loadw uwp uwp unwind-block-current-uwp-slot)
279   ;; we're about to reload ebp anyway, so let's borrow it here as a
280   ;; temporary.  Hope this works
281   (store-tl-symbol-value uwp *current-unwind-protect-block* rbp-tn)
282
283   DO-EXIT
284
285   (loadw rbp-tn block unwind-block-current-cont-slot)
286
287   ;; Uwp-entry expects some things in known locations so that they can
288   ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
289   ;; count in ecx-tn.
290
291   (inst jmp (make-ea :byte :base block
292                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))