1 ;;;; the VOPs and other necessary machine specific support
2 ;;;; routines for call-out to C
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;; The MOVE-ARG vop is going to store args on the stack for
16 ;; call-out. These tn's will be used for that. move-arg is normally
17 ;; used for things going down the stack but C wants to have args
18 ;; indexed in the positive direction.
20 (defun my-make-wired-tn (prim-type-name sc-name offset)
21 (make-wired-tn (primitive-type-or-lose prim-type-name)
22 (sc-number-or-lose sc-name)
25 (defstruct (arg-state (:copier nil))
28 (define-alien-type-method (integer :arg-tn) (type state)
29 (let ((stack-frame-size (arg-state-stack-frame-size state)))
30 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
31 (multiple-value-bind (ptype stack-sc)
32 (if (alien-integer-type-signed type)
33 (values 'signed-byte-32 'signed-stack)
34 (values 'unsigned-byte-32 'unsigned-stack))
35 (my-make-wired-tn ptype stack-sc stack-frame-size))))
37 (define-alien-type-method (system-area-pointer :arg-tn) (type state)
38 (declare (ignore type))
39 (let ((stack-frame-size (arg-state-stack-frame-size state)))
40 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
41 (my-make-wired-tn 'system-area-pointer
46 (define-alien-type-method (long-float :arg-tn) (type state)
47 (declare (ignore type))
48 (let ((stack-frame-size (arg-state-stack-frame-size state)))
49 (setf (arg-state-stack-frame-size state) (+ stack-frame-size 3))
50 (my-make-wired-tn 'long-float 'long-stack stack-frame-size)))
52 (define-alien-type-method (double-float :arg-tn) (type state)
53 (declare (ignore type))
54 (let ((stack-frame-size (arg-state-stack-frame-size state)))
55 (setf (arg-state-stack-frame-size state) (+ stack-frame-size 2))
56 (my-make-wired-tn 'double-float 'double-stack stack-frame-size)))
58 (define-alien-type-method (single-float :arg-tn) (type state)
59 (declare (ignore type))
60 (let ((stack-frame-size (arg-state-stack-frame-size state)))
61 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
62 (my-make-wired-tn 'single-float 'single-stack stack-frame-size)))
64 (defstruct (result-state (:copier nil))
67 (defun result-reg-offset (slot)
72 (define-alien-type-method (integer :result-tn) (type state)
73 (let ((num-results (result-state-num-results state)))
74 (setf (result-state-num-results state) (1+ num-results))
75 (multiple-value-bind (ptype reg-sc)
76 (if (alien-integer-type-signed type)
77 (values 'signed-byte-32 'signed-reg)
78 (values 'unsigned-byte-32 'unsigned-reg))
79 (my-make-wired-tn ptype reg-sc (result-reg-offset num-results)))))
81 (define-alien-type-method (integer :naturalize-gen) (type alien)
82 (if (<= (alien-type-bits type) 16)
83 (if (alien-integer-type-signed type)
84 `(sign-extend ,alien ,(alien-type-bits type))
85 `(logand ,alien ,(1- (ash 1 (alien-type-bits type)))))
88 (define-alien-type-method (system-area-pointer :result-tn) (type state)
89 (declare (ignore type))
90 (let ((num-results (result-state-num-results state)))
91 (setf (result-state-num-results state) (1+ num-results))
92 (my-make-wired-tn 'system-area-pointer 'sap-reg
93 (result-reg-offset num-results))))
96 (define-alien-type-method (long-float :result-tn) (type state)
97 (declare (ignore type))
98 (let ((num-results (result-state-num-results state)))
99 (setf (result-state-num-results state) (1+ num-results))
100 (my-make-wired-tn 'long-float 'long-reg (* num-results 2))))
102 (define-alien-type-method (double-float :result-tn) (type state)
103 (declare (ignore type))
104 (let ((num-results (result-state-num-results state)))
105 (setf (result-state-num-results state) (1+ num-results))
106 (my-make-wired-tn 'double-float 'double-reg (* num-results 2))))
108 (define-alien-type-method (single-float :result-tn) (type state)
109 (declare (ignore type))
110 (let ((num-results (result-state-num-results state)))
111 (setf (result-state-num-results state) (1+ num-results))
112 (my-make-wired-tn 'single-float 'single-reg (* num-results 2))))
114 (define-alien-type-method (values :result-tn) (type state)
115 (let ((values (alien-values-type-values type)))
116 (when (> (length values) 2)
117 (error "Too many result values from c-call."))
118 (mapcar (lambda (type)
119 (invoke-alien-type-method :result-tn type state))
122 (!def-vm-support-routine make-call-out-tns (type)
123 (let ((arg-state (make-arg-state)))
125 (dolist (arg-type (alien-fun-type-arg-types type))
126 (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
127 (values (my-make-wired-tn 'positive-fixnum 'any-reg esp-offset)
128 (* (arg-state-stack-frame-size arg-state) n-word-bytes)
130 (invoke-alien-type-method :result-tn
131 (alien-fun-type-result-type type)
132 (make-result-state))))))
135 (deftransform %alien-funcall ((function type &rest args) * * :node node)
136 (aver (sb!c::constant-lvar-p type))
137 (let* ((type (sb!c::lvar-value type))
138 (env (sb!c::node-lexenv node))
139 (arg-types (alien-fun-type-arg-types type))
140 (result-type (alien-fun-type-result-type type)))
141 (aver (= (length arg-types) (length args)))
142 (if (or (some #'(lambda (type)
143 (and (alien-integer-type-p type)
144 (> (sb!alien::alien-integer-type-bits type) 32)))
146 (and (alien-integer-type-p result-type)
147 (> (sb!alien::alien-integer-type-bits result-type) 32)))
148 (collect ((new-args) (lambda-vars) (new-arg-types))
149 (dolist (type arg-types)
150 (let ((arg (gensym)))
152 (cond ((and (alien-integer-type-p type)
153 (> (sb!alien::alien-integer-type-bits type) 32))
154 (new-args `(logand ,arg #xffffffff))
155 (new-args `(ash ,arg -32))
156 (new-arg-types (parse-alien-type '(unsigned 32) env))
157 (if (alien-integer-type-signed type)
158 (new-arg-types (parse-alien-type '(signed 32) env))
159 (new-arg-types (parse-alien-type '(unsigned 32) env))))
162 (new-arg-types type)))))
163 (cond ((and (alien-integer-type-p result-type)
164 (> (sb!alien::alien-integer-type-bits result-type) 32))
165 (let ((new-result-type
166 (let ((sb!alien::*values-type-okay* t))
168 (if (alien-integer-type-signed result-type)
169 '(values (unsigned 32) (signed 32))
170 '(values (unsigned 32) (unsigned 32)))
172 `(lambda (function type ,@(lambda-vars))
173 (declare (ignore type))
174 (multiple-value-bind (low high)
175 (%alien-funcall function
176 ',(make-alien-fun-type
177 :arg-types (new-arg-types)
178 :result-type new-result-type)
180 (logior low (ash high 32))))))
182 `(lambda (function type ,@(lambda-vars))
183 (declare (ignore type))
184 (%alien-funcall function
185 ',(make-alien-fun-type
186 :arg-types (new-arg-types)
187 :result-type result-type)
189 (sb!c::give-up-ir1-transform))))
191 ;;; The ABI is vague about how signed sub-word integer return values
192 ;;; are handled, but since gcc versions >=4.3 no longer do sign
193 ;;; extension in the callee, we need to do it in the caller. FIXME:
194 ;;; If the value to be extended is known to already be of the target
195 ;;; type at compile time, we can (and should) elide the extension.
196 (defknown sign-extend ((signed-byte 32) t) fixnum
197 (foldable flushable movable))
199 (define-vop (sign-extend)
200 (:translate sign-extend)
202 ;; Need to wire this to EAX since in x86 some dword registers don't
203 ;; have a matching word or byte register.
204 (:args (val :scs (signed-reg) :target eax))
205 (:temporary (:sc signed-reg :offset eax-offset :from :eval :to :result) eax)
206 (:arg-types signed-num (:constant fixnum))
208 (:results (res :scs (signed-reg)))
209 (:result-types fixnum)
213 (make-random-tn :kind :normal
214 :sc (sc-or-lose (ecase size
217 :offset (tn-offset val)))))
220 (defun sign-extend (x size)
221 (declare (type (signed-byte 32) x))
223 (8 (sign-extend x size))
224 (16 (sign-extend x size))))
227 (defun sign-extend (x size)
228 (if (logbitp (1- size) x)
229 (dpb x (byte size 0) -1)
232 (define-vop (foreign-symbol-sap)
233 (:translate foreign-symbol-sap)
236 (:arg-types (:constant simple-string))
237 (:info foreign-symbol)
238 (:results (res :scs (sap-reg)))
239 (:result-types system-area-pointer)
241 (inst lea res (make-fixup foreign-symbol :foreign))))
244 (define-vop (foreign-symbol-dataref-sap)
245 (:translate foreign-symbol-dataref-sap)
248 (:arg-types (:constant simple-string))
249 (:info foreign-symbol)
250 (:results (res :scs (sap-reg)))
251 (:result-types system-area-pointer)
253 (inst mov res (make-fixup foreign-symbol :foreign-dataref))))
255 (define-vop (call-out)
256 (:args (function :scs (sap-reg))
258 (:results (results :more t))
259 (:temporary (:sc unsigned-reg :offset eax-offset
260 :from :eval :to :result) eax)
261 (:temporary (:sc unsigned-reg :offset ecx-offset
262 :from :eval :to :result) ecx)
263 (:temporary (:sc unsigned-reg :offset edx-offset
264 :from :eval :to :result) edx)
265 #!+sb-safepoint (:temporary (:sc unsigned-reg :offset esi-offset) esi)
266 #!+sb-safepoint (:temporary (:sc unsigned-reg :offset edi-offset) edi)
267 #!-sb-safepoint (:node-var node)
270 (:ignore args ecx edx
274 ;; FIXME & OAOOM: This is brittle and error-prone to maintain two
275 ;; instances of the same logic, on in arch-assem.S, and one in
276 ;; c-call.lisp. If you modify this, modify that one too...
278 ;; On safepoints builds, we currently use the out-of-line
279 ;; calling routine irrespectively of SPACE and SPEED policy.
280 ;; An inline version of said changes is left to the
281 ;; sufficiently motivated maintainer.
282 #!-sb-safepoint (policy node (> space speed)))
284 (inst call (make-fixup "call_into_c" :foreign)))
286 ;; Setup the NPX for C; all the FP registers need to be
287 ;; empty; pop them all.
291 ;; Clear out DF: Darwin, Windows, and Solaris at least require
292 ;; this, and it should not hurt others either.
296 ;; To give the debugger a clue. FIXME: not really internal-error?
297 (note-this-location vop :internal-error)
299 ;; Restore the NPX for lisp; ensure no regs are empty
304 (location= (tn-ref-tn results) fr0-tn))
305 ;; The return result is in fr0.
306 (inst fxch fr7-tn) ; move the result back to fr0
307 (inst fldz)) ; insure no regs are empty
310 ;;; While SBCL uses the FPU in 53-bit mode, most C libraries assume that
311 ;;; the FPU is in 64-bit mode. So we change the FPU mode to 64-bit with
312 ;;; the SET-FPU-WORD-FOR-C VOP before calling out to C and set it back
313 ;;; to 53-bit mode after coming back using the SET-FPU-WORD-FOR-LISP VOP.
314 (define-vop (set-fpu-word-for-c)
317 (when (policy node (= sb!c::float-accuracy 3))
319 (inst fnstcw (make-ea :word :base esp-tn))
321 (inst or (make-ea :word :base esp-tn) #x300)
322 (inst fldcw (make-ea :word :base esp-tn))
325 (define-vop (set-fpu-word-for-lisp)
328 (when (policy node (= sb!c::float-accuracy 3))
329 (inst fnstcw (make-ea :word :base esp-tn))
331 (inst and (make-ea :word :base esp-tn) #xfeff)
332 (inst fldcw (make-ea :word :base esp-tn))
334 (inst add esp-tn 4))))
336 (define-vop (alloc-number-stack-space)
338 (:results (result :scs (sap-reg any-reg)))
339 (:result-types system-area-pointer)
341 (aver (location= result esp-tn))
342 (unless (zerop amount)
343 (let ((delta (logandc2 (+ amount 3) 3)))
344 (inst sub esp-tn delta)))
345 (align-stack-pointer esp-tn)
346 (move result esp-tn)))
348 (define-vop (alloc-alien-stack-space)
350 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
351 (:results (result :scs (sap-reg any-reg)))
352 (:result-types system-area-pointer)
355 (aver (not (location= result esp-tn)))
356 (unless (zerop amount)
357 (let ((delta (logandc2 (+ amount 3) 3)))
358 (with-tls-ea (EA :base temp
360 :disp (make-ea-for-symbol-tls-index *alien-stack*))
361 (inst sub EA delta :maybe-fs))))
362 (load-tl-symbol-value result *alien-stack*))
365 (aver (not (location= result esp-tn)))
366 (unless (zerop amount)
367 (let ((delta (logandc2 (+ amount 3) 3)))
368 (inst sub (make-ea-for-symbol-value *alien-stack*)
370 (load-symbol-value result *alien-stack*)))
372 (define-vop (dealloc-alien-stack-space)
374 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
377 (unless (zerop amount)
378 (let ((delta (logandc2 (+ amount 3) 3)))
379 (with-tls-ea (EA :base temp
381 :disp (make-ea-for-symbol-tls-index *alien-stack*))
382 (inst add EA delta :maybe-fs)))))
385 (unless (zerop amount)
386 (let ((delta (logandc2 (+ amount 3) 3)))
387 (inst add (make-ea-for-symbol-value *alien-stack*)
390 ;;; not strictly part of the c-call convention, but needed for the
391 ;;; WITH-PINNED-OBJECTS macro used for "locking down" lisp objects so
392 ;;; that GC won't move them while foreign functions go to work.
393 (define-vop (touch-object)
394 (:translate touch-object)
402 (defun alien-callback-accessor-form (type sp offset)
403 `(deref (sap-alien (sap+ ,sp ,offset) (* ,type))))
406 (defun alien-callback-assembler-wrapper
407 (index return-type arg-types &optional (stack-offset 0))
408 "Cons up a piece of code which calls call-callback with INDEX and a
409 pointer to the arguments."
410 (declare (ignore arg-types))
411 (let* ((segment (make-segment))
416 ([ebp-8] (make-ea :dword :base ebp :disp -8))
417 ([ebp-4] (make-ea :dword :base ebp :disp -4)))
419 (inst push ebp) ; save old frame pointer
420 (inst mov ebp esp) ; establish new frame
422 (inst sub eax 8) ; place for result
423 (inst push eax) ; arg2
424 (inst add eax 16) ; arguments
425 (inst push eax) ; arg1
426 (inst push (ash index 2)) ; arg0
430 (inst mov eax (foreign-symbol-address "callback_wrapper_trampoline"))
435 ;; Indirect the access to ENTER-ALIEN-CALLBACK through
436 ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
437 ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
438 ;; Skip any SB-THREAD TLS magic, since we don't expecte anyone
439 ;; to rebind the variable. -- JES, 2006-01-01
440 (load-symbol-value eax sb!alien::*enter-alien-callback*)
441 (inst push eax) ; function
442 (inst mov eax (foreign-symbol-address "funcall3"))
445 ;; now put the result into the right register
447 ((and (alien-integer-type-p return-type)
448 (eql (alien-type-bits return-type) 64))
449 (inst mov eax [ebp-8])
450 (inst mov edx [ebp-4]))
451 ((or (alien-integer-type-p return-type)
452 (alien-pointer-type-p return-type)
453 (alien-type-= #.(parse-alien-type 'system-area-pointer nil)
455 (inst mov eax [ebp-8]))
456 ((alien-single-float-type-p return-type)
458 ((alien-double-float-type-p return-type)
460 ((alien-void-type-p return-type))
462 (error "unrecognized alien type: ~A" return-type)))
463 (inst mov esp ebp) ; discard frame
464 (inst pop ebp) ; restore frame pointer
465 (inst ret stack-offset))
466 (finalize-segment segment)
467 ;; Now that the segment is done, convert it to a static
468 ;; vector we can point foreign code to.
469 (let ((buffer (sb!assem::segment-buffer segment)))
470 (make-static-vector (length buffer)
471 :element-type '(unsigned-byte 8)
472 :initial-contents buffer))))