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))
30 (defun int-arg (state prim-type reg-sc stack-sc)
31 (let ((reg-args (arg-state-register-args state)))
33 (setf (arg-state-register-args state) (1+ reg-args))
34 (my-make-wired-tn prim-type reg-sc
35 (nth reg-args *c-call-register-arg-offsets*)))
37 (let ((frame-size (arg-state-stack-frame-size state)))
38 (setf (arg-state-stack-frame-size state) (1+ frame-size))
39 (my-make-wired-tn prim-type stack-sc frame-size))))))
41 (define-alien-type-method (integer :arg-tn) (type state)
42 (if (alien-integer-type-signed type)
43 (int-arg state 'signed-byte-64 'signed-reg 'signed-stack)
44 (int-arg state 'unsigned-byte-64 'unsigned-reg 'unsigned-stack)))
46 (define-alien-type-method (system-area-pointer :arg-tn) (type state)
47 (declare (ignore type))
48 (int-arg state 'system-area-pointer 'sap-reg 'sap-stack))
50 (defun float-arg (state prim-type reg-sc stack-sc)
51 (let ((xmm-args (arg-state-xmm-args state)))
53 (setf (arg-state-xmm-args state) (1+ xmm-args))
54 (my-make-wired-tn prim-type reg-sc
55 (nth xmm-args *float-regs*)))
57 (let ((frame-size (arg-state-stack-frame-size state)))
58 (setf (arg-state-stack-frame-size state) (1+ frame-size))
59 (my-make-wired-tn prim-type stack-sc frame-size))))))
61 (define-alien-type-method (double-float :arg-tn) (type state)
62 (declare (ignore type))
63 (float-arg state 'double-float 'double-reg 'double-stack))
65 (define-alien-type-method (single-float :arg-tn) (type state)
66 (declare (ignore type))
67 (float-arg state 'single-float 'single-reg 'single-stack))
69 (defstruct (result-state (:copier nil))
72 (defun result-reg-offset (slot)
77 ;; XXX The return handling probably doesn't conform to the ABI
79 (define-alien-type-method (integer :result-tn) (type state)
80 (let ((num-results (result-state-num-results state)))
81 (setf (result-state-num-results state) (1+ num-results))
82 (multiple-value-bind (ptype reg-sc)
83 (if (alien-integer-type-signed type)
84 (values 'signed-byte-64 'signed-reg)
85 (values 'unsigned-byte-64 'unsigned-reg))
86 (my-make-wired-tn ptype reg-sc (result-reg-offset num-results)))))
88 (define-alien-type-method (integer :naturalize-gen) (type alien)
89 (if (and (alien-integer-type-signed type)
90 (<= (alien-type-bits type) 32))
94 (define-alien-type-method (system-area-pointer :result-tn) (type state)
95 (declare (ignore type))
96 (let ((num-results (result-state-num-results state)))
97 (setf (result-state-num-results state) (1+ num-results))
98 (my-make-wired-tn 'system-area-pointer 'sap-reg
99 (result-reg-offset num-results))))
101 (define-alien-type-method (double-float :result-tn) (type state)
102 (declare (ignore type))
103 (let ((num-results (result-state-num-results state)))
104 (setf (result-state-num-results state) (1+ num-results))
105 (my-make-wired-tn 'double-float 'double-reg num-results)))
107 (define-alien-type-method (single-float :result-tn) (type state)
108 (declare (ignore type))
109 (let ((num-results (result-state-num-results state)))
110 (setf (result-state-num-results state) (1+ num-results))
111 (my-make-wired-tn 'single-float 'single-reg num-results)))
113 (define-alien-type-method (values :result-tn) (type state)
114 (let ((values (alien-values-type-values type)))
115 (when (> (length values) 2)
116 (error "Too many result values from c-call."))
117 (mapcar (lambda (type)
118 (invoke-alien-type-method :result-tn type state))
121 (!def-vm-support-routine make-call-out-tns (type)
122 (let ((arg-state (make-arg-state)))
124 (dolist (arg-type (alien-fun-type-arg-types type))
125 (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
126 (values (my-make-wired-tn 'positive-fixnum 'any-reg esp-offset)
127 (* (arg-state-stack-frame-size arg-state) n-word-bytes)
129 (invoke-alien-type-method :result-tn
130 (alien-fun-type-result-type type)
131 (make-result-state))))))
134 (deftransform %alien-funcall ((function type &rest args) * * :node node)
135 (aver (sb!c::constant-lvar-p type))
136 (let* ((type (sb!c::lvar-value type))
137 (env (sb!c::node-lexenv node))
138 (arg-types (alien-fun-type-arg-types type))
139 (result-type (alien-fun-type-result-type type)))
140 (aver (= (length arg-types) (length args)))
141 (if (or (some #'(lambda (type)
142 (and (alien-integer-type-p type)
143 (> (sb!alien::alien-integer-type-bits type) 64)))
145 (and (alien-integer-type-p result-type)
146 (> (sb!alien::alien-integer-type-bits result-type) 64)))
147 (collect ((new-args) (lambda-vars) (new-arg-types))
148 (dolist (type arg-types)
149 (let ((arg (gensym)))
151 (cond ((and (alien-integer-type-p type)
152 (> (sb!alien::alien-integer-type-bits type) 64))
153 (new-args `(logand ,arg #xffffffff))
154 (new-args `(ash ,arg -64))
155 (new-arg-types (parse-alien-type '(unsigned 64) env))
156 (if (alien-integer-type-signed type)
157 (new-arg-types (parse-alien-type '(signed 64) env))
158 (new-arg-types (parse-alien-type '(unsigned 64) env))))
161 (new-arg-types type)))))
162 (cond ((and (alien-integer-type-p result-type)
163 (> (sb!alien::alien-integer-type-bits result-type) 64))
164 (let ((new-result-type
165 (let ((sb!alien::*values-type-okay* t))
167 (if (alien-integer-type-signed result-type)
168 '(values (unsigned 64) (signed 64))
169 '(values (unsigned 64) (unsigned 64)))
171 `(lambda (function type ,@(lambda-vars))
172 (declare (ignore type))
173 (multiple-value-bind (low high)
174 (%alien-funcall function
175 ',(make-alien-fun-type
176 :arg-types (new-arg-types)
177 :result-type new-result-type)
179 (logior low (ash high 64))))))
181 `(lambda (function type ,@(lambda-vars))
182 (declare (ignore type))
183 (%alien-funcall function
184 ',(make-alien-fun-type
185 :arg-types (new-arg-types)
186 :result-type result-type)
188 (sb!c::give-up-ir1-transform))))
190 ;;; The ABI specifies that signed short/int's are returned as 32-bit
191 ;;; values. Negative values need to be sign-extended to 64-bits (done
192 ;;; in a :NATURALIZE-GEN alien-type-method).
193 (defknown sign-extend ((signed-byte 32)) fixnum
194 (foldable flushable movable))
196 (define-vop (sign-extend)
197 (:translate sign-extend)
199 (:args (val :scs (signed-reg)))
201 (:results (res :scs (signed-reg)))
202 (:result-types fixnum)
205 (make-random-tn :kind :normal
206 :sc (sc-or-lose 'dword-reg)
207 :offset (tn-offset val)))))
209 (defun sign-extend (x)
210 (declare (type (signed-byte 32) x))
213 (define-vop (foreign-symbol-sap)
214 (:translate foreign-symbol-sap)
217 (:arg-types (:constant simple-string))
218 (:info foreign-symbol)
219 (:results (res :scs (sap-reg)))
220 (:result-types system-area-pointer)
222 (inst lea res (make-fixup foreign-symbol :foreign))))
225 (define-vop (foreign-symbol-dataref-sap)
226 (:translate foreign-symbol-dataref-sap)
229 (:arg-types (:constant simple-string))
230 (:info foreign-symbol)
231 (:results (res :scs (sap-reg)))
232 (:result-types system-area-pointer)
234 (inst mov res (make-fixup foreign-symbol :foreign-dataref))))
236 (define-vop (call-out)
237 (:args (function :scs (sap-reg))
239 (:results (results :more t))
240 (:temporary (:sc unsigned-reg :offset rax-offset :to :result) rax)
245 ;; ABI: Direction flag must be clear on function entry. -- JES, 2006-01-20
247 ;; ABI: AL contains amount of arguments passed in XMM registers
250 (loop for tn-ref = args then (tn-ref-across tn-ref)
252 count (eq (sb-name (sc-sb (tn-sc (tn-ref-tn tn-ref))))
255 ;; To give the debugger a clue. XX not really internal-error?
256 (note-this-location vop :internal-error)))
258 (define-vop (alloc-number-stack-space)
260 (:results (result :scs (sap-reg any-reg)))
262 (aver (location= result rsp-tn))
263 (unless (zerop amount)
264 (let ((delta (logandc2 (+ amount 7) 7)))
265 (inst sub rsp-tn delta)))
266 ;; C stack must be 16 byte aligned
267 (inst and rsp-tn -16)
268 (move result rsp-tn)))
270 (define-vop (dealloc-number-stack-space)
273 (unless (zerop amount)
274 (let ((delta (logandc2 (+ amount 7) 7)))
275 (inst add rsp-tn delta)))))
277 (define-vop (alloc-alien-stack-space)
279 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
280 (:results (result :scs (sap-reg any-reg)))
283 (aver (not (location= result rsp-tn)))
284 (unless (zerop amount)
285 (let ((delta (logandc2 (+ amount 7) 7)))
289 (static-symbol-offset '*alien-stack*)
290 (ash symbol-tls-index-slot word-shift)
291 (- other-pointer-lowtag))))
292 (inst sub (make-ea :qword :base thread-base-tn
293 :scale 1 :index temp) delta)))
294 (load-tl-symbol-value result *alien-stack*))
297 (aver (not (location= result rsp-tn)))
298 (unless (zerop amount)
299 (let ((delta (logandc2 (+ amount 7) 7)))
300 (inst sub (make-ea :qword
302 (static-symbol-offset '*alien-stack*)
303 (ash symbol-value-slot word-shift)
304 (- other-pointer-lowtag)))
306 (load-symbol-value result *alien-stack*)))
308 (define-vop (dealloc-alien-stack-space)
310 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
313 (unless (zerop amount)
314 (let ((delta (logandc2 (+ amount 7) 7)))
318 (static-symbol-offset '*alien-stack*)
319 (ash symbol-tls-index-slot word-shift)
320 (- other-pointer-lowtag))))
321 (inst add (make-ea :qword :base thread-base-tn :scale 1 :index temp)
325 (unless (zerop amount)
326 (let ((delta (logandc2 (+ amount 7) 7)))
327 (inst add (make-ea :qword
329 (static-symbol-offset '*alien-stack*)
330 (ash symbol-value-slot word-shift)
331 (- other-pointer-lowtag)))
334 ;;; these are not strictly part of the c-call convention, but are
335 ;;; needed for the WITH-PRESERVED-POINTERS macro used for "locking
336 ;;; down" lisp objects so that GC won't move them while foreign
337 ;;; functions go to work.
339 (define-vop (push-word-on-c-stack)
340 (:translate push-word-on-c-stack)
341 (:args (val :scs (sap-reg)))
343 (:arg-types system-area-pointer)
347 (define-vop (pop-words-from-c-stack)
348 (:translate pop-words-from-c-stack)
350 (:arg-types (:constant (unsigned-byte 60)))
354 (inst add rsp-tn (fixnumize number))))
359 (defun alien-callback-accessor-form (type sp offset)
360 `(deref (sap-alien (sap+ ,sp ,offset) (* ,type))))
363 (defun alien-callback-assembler-wrapper (index result-type argument-types)
364 (labels ((make-tn-maker (sc-name)
366 (make-random-tn :kind :normal
367 :sc (sc-or-lose sc-name)
369 (out-of-registers-error ()
370 (error "Too many arguments in callback")))
371 (let* ((segment (make-segment))
380 ([rsp] (make-ea :qword :base rsp :disp 0))
381 ;; How many arguments have been copied
383 ;; How many arguments have been copied from the stack
384 (stack-argument-count 0)
385 (gprs (mapcar (make-tn-maker 'any-reg) *c-call-register-arg-offsets*))
386 (fprs (mapcar (make-tn-maker 'double-reg)
387 ;; Only 8 first XMM registers are used for
389 (subseq *float-regs* 0 8))))
391 ;; Make room on the stack for arguments.
392 (inst sub rsp (* n-word-bytes (length argument-types)))
393 ;; Copy arguments from registers to stack
394 (dolist (type argument-types)
395 (let ((integerp (not (alien-float-type-p type)))
396 ;; A TN pointing to the stack location where the
397 ;; current argument should be stored for the purposes
398 ;; of ENTER-ALIEN-CALLBACK.
399 (target-tn (make-ea :qword :base rsp
402 ;; A TN pointing to the stack location that contains
403 ;; the next argument passed on the stack.
404 (stack-arg-tn (make-ea :qword :base rsp
406 (length argument-types)
407 stack-argument-count)
411 (let ((gpr (pop gprs)))
412 ;; Argument not in register, copy it from the old
413 ;; stack location to a temporary register.
415 (incf stack-argument-count)
416 (setf gpr temp-reg-tn)
417 (inst mov gpr stack-arg-tn))
418 ;; Copy from either argument register or temporary
419 ;; register to target.
420 (inst mov target-tn gpr)))
421 ((or (alien-single-float-type-p type)
422 (alien-double-float-type-p type))
423 (let ((fpr (pop fprs)))
425 ;; Copy from float register to target location.
426 (inst movq target-tn fpr))
428 ;; Not in float register. Copy from stack to
429 ;; temporary (general purpose) register, and
430 ;; from there to the target location.
431 (incf stack-argument-count)
432 (inst mov temp-reg-tn stack-arg-tn)
433 (inst mov target-tn temp-reg-tn)))))
435 (bug "Unknown alien floating point type: ~S" type)))))
437 ;; arg0 to FUNCALL3 (function)
439 ;; Indirect the access to ENTER-ALIEN-CALLBACK through
440 ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
441 ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
442 ;; Skip any SB-THREAD TLS magic, since we don't expect anyone
443 ;; to rebind the variable. -- JES, 2006-01-01
444 (inst mov rdi (+ nil-value (static-symbol-offset
445 'sb!alien::*enter-alien-callback*)))
446 (loadw rdi rdi symbol-value-slot other-pointer-lowtag)
447 ;; arg0 to ENTER-ALIEN-CALLBACK (trampoline index)
448 (inst mov rsi (fixnumize index))
449 ;; arg1 to ENTER-ALIEN-CALLBACK (pointer to argument vector)
451 ;; add room on stack for return value
453 ;; arg2 to ENTER-ALIEN-CALLBACK (pointer to return value)
461 (inst mov rax (foreign-symbol-address "funcall3"))
464 ;; Back! Restore frame
468 ;; Result now on top of stack, put it in the right register
470 ((or (alien-integer-type-p result-type)
471 (alien-pointer-type-p result-type)
472 (alien-type-= #.(parse-alien-type 'system-area-pointer nil)
474 (inst mov rax [rsp]))
475 ((or (alien-single-float-type-p result-type)
476 (alien-double-float-type-p result-type))
477 (inst movq xmm0 [rsp]))
478 ((alien-void-type-p result-type))
480 (error "unrecognized alien type: ~A" result-type)))
482 ;; Pop the arguments and the return value from the stack to get
483 ;; the return address at top of stack.
484 (inst add rsp (* (1+ (length argument-types)) n-word-bytes))
487 (finalize-segment segment)
488 ;; Now that the segment is done, convert it to a static
489 ;; vector we can point foreign code to.
490 (let ((buffer (sb!assem::segment-buffer segment)))
491 (make-static-vector (length buffer)
492 :element-type '(unsigned-byte 8)
493 :initial-contents buffer)))))