1.0.9.53: trivial typo fixes
[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 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 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
58   ;; Restore the count.
59   (inst mov ecx edx)
60
61   ;; Set the stack top to the last result.
62   (inst lea rsp-tn (make-ea :qword :base edi :disp n-word-bytes))
63
64   ;; Load the register args.
65   (loadw edx ebx -1)
66   (loadw edi ebx -2)
67   (loadw esi ebx -3)
68
69   ;; And back we go.
70   (inst stc)
71   (inst jmp eax)
72
73   ;; Handle the register arg cases.
74   ZERO-VALUES
75   (move rsp-tn ebx)
76   (inst mov edx nil-value)
77   (inst mov edi edx)
78   (inst mov esi edx)
79   (inst stc)
80   (inst jmp eax)
81
82   ONE-VALUE ; Note: we can get this, because the return-multiple vop
83             ; doesn't check for this case when size > speed.
84   (loadw edx esi -1)
85   (inst mov rsp-tn ebx)
86   (inst clc)
87   (inst jmp eax)
88
89   TWO-VALUES
90   (loadw edx esi -1)
91   (loadw edi esi -2)
92   (inst mov esi nil-value)
93   (inst lea rsp-tn (make-ea :qword :base ebx :disp (* -2 n-word-bytes)))
94   (inst stc)
95   (inst jmp eax)
96
97   THREE-VALUES
98   (loadw edx esi -1)
99   (loadw edi esi -2)
100   (loadw esi esi -3)
101   (inst lea rsp-tn (make-ea :qword :base ebx :disp (* -3 n-word-bytes)))
102   (inst stc)
103   (inst jmp eax))
104 \f
105 ;;;; TAIL-CALL-VARIABLE
106
107 ;;; For tail-call-variable, we have to copy the arguments from the end
108 ;;; of our stack frame (were args are produced) to the start of our
109 ;;; stack frame (were args are expected).
110 ;;;
111 ;;; We take the function to call in EAX and a pointer to the arguments in
112 ;;; ESI. EBP says the same over the jump, and the old frame pointer is
113 ;;; still saved in the first stack slot. The return-pc is saved in
114 ;;; the second stack slot, so we have to push it to make it look like
115 ;;; we actually called. We also have to compute ECX from the difference
116 ;;; between ESI and the stack top.
117 #+sb-assembling ;; No vop for this one either.
118 (define-assembly-routine
119     (tail-call-variable
120      (:return-style :none))
121
122     ((:temp eax unsigned-reg rax-offset)
123      (:temp ebx unsigned-reg rbx-offset)
124      (:temp ecx unsigned-reg rcx-offset)
125      (:temp edx unsigned-reg rdx-offset)
126      (:temp edi unsigned-reg rdi-offset)
127      (:temp esi unsigned-reg rsi-offset))
128
129   ;; Calculate NARGS (as a fixnum)
130   (move ecx esi)
131   (inst sub ecx rsp-tn)
132
133   ;; Check for all the args fitting the registers.
134   (inst cmp ecx (fixnumize 3))
135   (inst jmp :le REGISTER-ARGS)
136
137   ;; Save the OLD-FP and RETURN-PC because the blit it going to trash
138   ;; those stack locations. Save the ECX, because the loop is going
139   ;; to trash it.
140   (pushw rbp-tn -1)
141   (loadw ebx rbp-tn -2)
142   (inst push ecx)
143
144   ;; Do the blit. Because we are coping from smaller addresses to
145   ;; larger addresses, we have to start at the largest pair and work
146   ;; our way down.
147   (inst shr ecx 3)                      ; fixnum to raw words
148   (inst std)                            ; count down
149   (inst lea edi (make-ea :qword :base rbp-tn :disp (- n-word-bytes)))
150   (inst sub esi (fixnumize 1))
151   (inst rep)
152   (inst movs :qword)
153
154   ;; Load the register arguments carefully.
155   (loadw edx rbp-tn -1)
156
157   ;; Restore OLD-FP and ECX.
158   (inst pop ecx)
159   (popw rbp-tn -1)                      ; overwrites a0
160
161   ;; Blow off the stack above the arguments.
162   (inst lea rsp-tn (make-ea :qword :base edi :disp n-word-bytes))
163
164   ;; remaining register args
165   (loadw edi rbp-tn -2)
166   (loadw esi rbp-tn -3)
167
168   ;; Push the (saved) return-pc so it looks like we just called.
169   (inst push ebx)
170
171   ;; And jump into the function.
172     (inst jmp
173           (make-ea :byte :base eax
174                    :disp (- (* closure-fun-slot n-word-bytes)
175                             fun-pointer-lowtag)))
176
177   ;; All the arguments fit in registers, so load them.
178   REGISTER-ARGS
179   (loadw edx esi -1)
180   (loadw edi esi -2)
181   (loadw esi esi -3)
182
183   ;; Clear most of the stack.
184   (inst lea rsp-tn
185         (make-ea :qword :base rbp-tn :disp (* -3 n-word-bytes)))
186
187   ;; Push the return-pc so it looks like we just called.
188   (pushw rbp-tn -2)    ; XXX dan ?
189
190   ;; And away we go.
191   (inst jmp (make-ea :byte :base eax
192                      :disp (- (* closure-fun-slot n-word-bytes)
193                               fun-pointer-lowtag))))
194 \f
195 (define-assembly-routine (throw
196                           (:return-style :none))
197                          ((:arg target (descriptor-reg any-reg) rdx-offset)
198                           (:arg start any-reg rbx-offset)
199                           (:arg count any-reg rcx-offset)
200                           (:temp catch any-reg rax-offset))
201
202   (declare (ignore start count))
203
204   (load-tl-symbol-value catch *current-catch-block*)
205
206   LOOP
207
208   (let ((error (generate-error-code nil unseen-throw-tag-error target)))
209     (inst or catch catch)               ; check for NULL pointer
210     (inst jmp :z error))
211
212   (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
213   (inst jmp :e EXIT)
214
215   (loadw catch catch catch-block-previous-catch-slot)
216   (inst jmp LOOP)
217
218   EXIT
219
220   ;; Here EAX points to catch block containing symbol pointed to by EDX.
221   (inst jmp (make-fixup 'unwind :assembly-routine)))
222
223 ;;;; non-local exit noise
224
225 (define-assembly-routine (unwind
226                           (:return-style :none)
227                           (:translate %continue-unwind)
228                           (:policy :fast-safe))
229                          ((:arg block (any-reg descriptor-reg) rax-offset)
230                           (:arg start (any-reg descriptor-reg) rbx-offset)
231                           (:arg count (any-reg descriptor-reg) rcx-offset)
232                           (:temp uwp unsigned-reg rsi-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 rdx-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* rbp-tn)
257
258   DO-EXIT
259
260   (loadw rbp-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 :byte :base block
267                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))