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.
19 ;;; For RETURN-MULTIPLE, we have to move the results from the end of
20 ;;; the frame for the function that is returning to the end of the
21 ;;; frame for the function being returned to.
23 #+sb-assembling ;; We don't want a vop for this one.
24 (define-assembly-routine
25 (return-multiple (:return-style :none))
26 (;; These four are really arguments.
27 (:temp eax unsigned-reg eax-offset)
28 (:temp ebx unsigned-reg ebx-offset)
29 (:temp ecx unsigned-reg ecx-offset)
30 (:temp esi unsigned-reg esi-offset)
32 ;; These we need as temporaries.
33 (:temp edx unsigned-reg edx-offset)
34 (:temp edi unsigned-reg edi-offset))
36 ;; Pick off the cases where everything fits in register args.
37 (inst jecxz zero-values)
38 (inst cmp ecx (fixnumize 1))
39 (inst jmp :e one-value)
40 (inst cmp ecx (fixnumize 2))
41 (inst jmp :e two-values)
42 (inst cmp ecx (fixnumize 3))
43 (inst jmp :e three-values)
45 ;; Save the count, because the loop is going to destroy it.
48 ;; Blit the values down the stack. Note: there might be overlap, so we have
49 ;; to be careful not to clobber values before we've read them. Because the
50 ;; stack builds down, we are coping to a larger address. Therefore, we need
51 ;; to iterate from larger addresses to smaller addresses.
52 ;; pfw-this says copy ecx words from esi to edi counting down.
53 (inst shr ecx 2) ; fixnum to raw word count
54 (inst std) ; count down
56 (inst lea edi (make-ea :dword :base ebx :disp (- word-bytes)))
63 ;; Set the stack top to the last result.
64 (inst lea esp-tn (make-ea :dword :base edi :disp word-bytes))
66 ;; Load the register args.
74 ;; Handle the register arg cases.
77 (inst mov edx nil-value)
82 ONE-VALUE ; Note: we can get this, because the return-multiple vop
83 ; doesn't check for this case when size > speed.
92 (inst mov esi nil-value)
93 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -2 word-bytes)))
100 (inst lea esp-tn (make-ea :dword :base ebx :disp (* -3 word-bytes)))
103 ;;;; TAIL-CALL-VARIABLE
105 ;;; For tail-call-variable, we have to copy the arguments from the end of our
106 ;;; stack frame (were args are produced) to the start of our stack frame
107 ;;; (were args are expected).
109 ;;; We take the function to call in EAX and a pointer to the arguments in
110 ;;; ESI. EBP says the same over the jump, and the old frame pointer is
111 ;;; still saved in the first stack slot. The return-pc is saved in
112 ;;; the second stack slot, so we have to push it to make it look like
113 ;;; we actually called. We also have to compute ECX from the difference
114 ;;; between ESI and the stack top.
115 #+sb-assembling ;; No vop for this one either.
116 (define-assembly-routine
118 (:return-style :none))
120 ((:temp eax unsigned-reg eax-offset)
121 (:temp ebx unsigned-reg ebx-offset)
122 (:temp ecx unsigned-reg ecx-offset)
123 (:temp edx unsigned-reg edx-offset)
124 (:temp edi unsigned-reg edi-offset)
125 (:temp esi unsigned-reg esi-offset))
127 ;; Calculate NARGS (as a fixnum)
129 (inst sub ecx esp-tn)
131 ;; Check for all the args fitting the the registers.
132 (inst cmp ecx (fixnumize 3))
133 (inst jmp :le REGISTER-ARGS)
135 ;; Save the OLD-FP and RETURN-PC because the blit it going to trash
136 ;; those stack locations. Save the ECX, because the loop is going
139 (loadw ebx ebp-tn -2)
142 ;; Do the blit. Because we are coping from smaller addresses to larger
143 ;; addresses, we have to start at the largest pair and work our way down.
144 (inst shr ecx 2) ; fixnum to raw words
145 (inst std) ; count down
146 (inst lea edi (make-ea :dword :base ebp-tn :disp (- word-bytes)))
147 (inst sub esi (fixnumize 1))
151 ;; Load the register arguments carefully.
152 (loadw edx ebp-tn -1)
154 ;; Restore OLD-FP and ECX.
156 (popw ebp-tn -1) ; overwrites a0
158 ;; Blow off the stack above the arguments.
159 (inst lea esp-tn (make-ea :dword :base edi :disp word-bytes))
161 ;; remaining register args
162 (loadw edi ebp-tn -2)
163 (loadw esi ebp-tn -3)
165 ;; Push the (saved) return-pc so it looks like we just called.
168 ;; And jump into the function.
170 (make-ea :byte :base eax
171 :disp (- (* closure-function-slot word-bytes)
172 function-pointer-type)))
174 ;; All the arguments fit in registers, so load them.
180 ;; Clear most of the stack.
182 (make-ea :dword :base ebp-tn :disp (* -3 word-bytes)))
184 ;; Push the return-pc so it looks like we just called.
188 (inst jmp (make-ea :byte :base eax
189 :disp (- (* closure-function-slot word-bytes)
190 function-pointer-type))))
192 (define-assembly-routine (throw
193 (:return-style :none))
194 ((:arg target (descriptor-reg any-reg) edx-offset)
195 (:arg start any-reg ebx-offset)
196 (:arg count any-reg ecx-offset)
197 (:temp catch any-reg eax-offset))
199 (declare (ignore start count))
201 (load-symbol-value catch sb!impl::*current-catch-block*)
205 (let ((error (generate-error-code nil unseen-throw-tag-error target)))
206 (inst or catch catch) ; check for NULL pointer
209 (inst cmp target (make-ea-for-object-slot catch catch-block-tag-slot 0))
212 (loadw catch catch catch-block-previous-catch-slot)
217 ;; Hear EAX points to catch block containing symbol pointed to by EDX.
218 (inst jmp (make-fixup 'unwind :assembly-routine)))
220 ;;;; non-local exit noise
222 (define-assembly-routine (unwind
223 (:return-style :none)
224 (:translate %continue-unwind)
225 (:policy :fast-safe))
226 ((:arg block (any-reg descriptor-reg) eax-offset)
227 (:arg start (any-reg descriptor-reg) ebx-offset)
228 (:arg count (any-reg descriptor-reg) ecx-offset)
229 (:temp uwp unsigned-reg esi-offset))
230 (declare (ignore start count))
232 (let ((error (generate-error-code nil invalid-unwind-error)))
233 (inst or block block) ; check for NULL pointer
236 (load-symbol-value uwp sb!impl::*current-unwind-protect-block*)
238 ;; Does *cuwpb* match value stored in argument cuwp slot?
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)
244 ;; Not a match - return to *current-unwind-protect-block* context.
245 ;; Important! Must save (and return) the arg 'block' for later use!!
248 ;; Set next unwind protect context.
249 (loadw uwp uwp unwind-block-current-uwp-slot)
250 (store-symbol-value uwp sb!impl::*current-unwind-protect-block*)
254 (loadw ebp-tn block unwind-block-current-cont-slot)
256 ;; Uwp-entry expects some things in known locations so that they can
257 ;; be saved on the stack: the block in edx-tn; start in ebx-tn; and
260 (inst jmp (make-ea :byte :base block
261 :disp (* unwind-block-entry-pc-slot word-bytes))))