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