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
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.
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.
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
53 (inst lea edi (make-ea :dword :base ebx :disp (- word-bytes)))
60 ;; Set the stack top to the last result.
61 (inst lea esp-tn (make-ea :dword :base edi :disp word-bytes))
63 ;; Load the register args.
71 ;; Handle the register arg cases.
74 (inst mov edx nil-value)
79 ONE-VALUE ; Note: we can get this, because the return-multiple vop
80 ; doesn't check for this case when size > speed.
89 (inst mov esi nil-value)
90 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -2 word-bytes)))
97 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -3 word-bytes)))
100 ;;;; TAIL-CALL-VARIABLE
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).
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
115 (:return-style :none))
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))
124 ;; Calculate NARGS (as a fixnum)
126 (inst sub ecx esp-tn)
128 ;; Check for all the args fitting the the registers.
129 (inst cmp ecx (fixnumize 3))
130 (inst jmp :le REGISTER-ARGS)
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
136 (loadw ebx ebp-tn -2)
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))
148 ;; Load the register arguments carefully.
149 (loadw edx ebp-tn -1)
151 ;; Restore OLD-FP and ECX.
153 (popw ebp-tn -1) ; overwrites a0
155 ;; Blow off the stack above the arguments.
156 (inst lea esp-tn (make-ea :dword :base edi :disp word-bytes))
158 ;; remaining register args
159 (loadw edi ebp-tn -2)
160 (loadw esi ebp-tn -3)
162 ;; Push the (saved) return-pc so it looks like we just called.
165 ;; And jump into the function.
167 (make-ea :byte :base eax
168 :disp (- (* closure-function-slot word-bytes)
169 function-pointer-type)))
171 ;; All the arguments fit in registers, so load them.
177 ;; Clear most of the stack.
179 (make-ea :dword :base ebp-tn :disp (* -3 word-bytes)))
181 ;; Push the return-pc so it looks like we just called.
185 (inst jmp (make-ea :byte :base eax
186 :disp (- (* closure-function-slot word-bytes)
187 function-pointer-type))))
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))
196 (declare (ignore start count))
198 (load-symbol-value catch *current-catch-block*)
202 (let ((error (generate-error-code nil unseen-throw-tag-error target)))
203 (inst or catch catch) ; check for NULL pointer
206 (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
209 (loadw catch catch catch-block-previous-catch-slot)
214 ;; Hear EAX points to catch block containing symbol pointed to by EDX.
215 (inst jmp (make-fixup 'unwind :assembly-routine)))
217 ;;;; non-local exit noise
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))
229 (let ((error (generate-error-code nil invalid-unwind-error)))
230 (inst or block block) ; check for NULL pointer
233 (load-symbol-value uwp *current-unwind-protect-block*)
235 ;; Does *cuwpb* match value stored in argument cuwp slot?
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)
241 ;; Not a match - return to *CURRENT-UNWIND-PROTECT-BLOCK* context.
242 ;; Important! Must save (and return) the arg 'block' for later use!!
245 ;; Set next unwind protect context.
246 (loadw uwp uwp unwind-block-current-uwp-slot)
247 (store-symbol-value uwp *current-unwind-protect-block*)
251 (loadw ebp-tn block unwind-block-current-cont-slot)
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
257 (inst jmp (make-ea :byte :base block
258 :disp (* unwind-block-entry-pc-slot word-bytes))))