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