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