0.9.14.3:
[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 (define-assembly-routine (unwind
232                           (:return-style :none)
233                           (:translate %continue-unwind)
234                           (:policy :fast-safe))
235                          ((:arg block (any-reg descriptor-reg) eax-offset)
236                           (:arg start (any-reg descriptor-reg) ebx-offset)
237                           (:arg count (any-reg descriptor-reg) ecx-offset)
238                           (:temp uwp unsigned-reg esi-offset))
239   (declare (ignore start count))
240
241   (let ((error (generate-error-code nil invalid-unwind-error)))
242     (inst or block block)               ; check for NULL pointer
243     (inst jmp :z error))
244
245   (load-tl-symbol-value uwp *current-unwind-protect-block*)
246
247   ;; Does *CURRENT-UNWIND-PROTECT-BLOCK* match the value stored in
248   ;; argument's CURRENT-UWP-SLOT?
249   (inst cmp uwp
250         (make-ea-for-object-slot block unwind-block-current-uwp-slot 0))
251   ;; If a match, return to context in arg block.
252   (inst jmp :e do-exit)
253
254   ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
255   ;; Important! Must save (and return) the arg 'block' for later use!!
256   (move edx-tn block)
257   (move block uwp)
258   ;; Set next unwind protect context.
259   (loadw uwp uwp unwind-block-current-uwp-slot)
260   ;; we're about to reload ebp anyway, so let's borrow it here as a
261   ;; temporary.  Hope this works
262   (store-tl-symbol-value uwp *current-unwind-protect-block* ebp-tn)
263
264   DO-EXIT
265
266   (loadw ebp-tn block unwind-block-current-cont-slot)
267
268   ;; Uwp-entry expects some things in known locations so that they can
269   ;; be saved on the stack: the block in edx-tn, start in ebx-tn, and
270   ;; count in ecx-tn.
271
272   (inst jmp (make-ea :byte :base block
273                      :disp (* unwind-block-entry-pc-slot n-word-bytes))))