1 ;;;; VOPs and other machine-specific support routines for call-out to C
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.
14 (defun my-make-wired-tn (prim-type-name sc-name offset)
15 (make-wired-tn (primitive-type-or-lose prim-type-name)
16 (sc-number-or-lose sc-name)
24 (define-alien-type-method (integer :arg-tn) (type state)
25 (let ((stack-frame-size (arg-state-stack-frame-size state)))
26 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
27 (setf (arg-state-did-int-arg state) t)
29 (ptype reg-sc stack-sc)
30 (if (alien-integer-type-signed type)
31 (values 'signed-byte-32 'signed-reg 'signed-stack)
32 (values 'unsigned-byte-32 'unsigned-reg 'unsigned-stack))
33 (if (< stack-frame-size 4)
34 (my-make-wired-tn ptype reg-sc (+ stack-frame-size 4))
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 (setf (arg-state-did-int-arg state) t)
42 (if (< stack-frame-size 4)
43 (my-make-wired-tn 'system-area-pointer
45 (+ stack-frame-size 4))
46 (my-make-wired-tn 'system-area-pointer
50 (define-alien-type-method (double-float :arg-tn) (type state)
51 (declare (ignore type))
52 (let ((stack-frame-size (logandc2 (1+ (arg-state-stack-frame-size state)) 1))
53 (float-args (arg-state-float-args state)))
54 (setf (arg-state-stack-frame-size state) (+ stack-frame-size 2))
55 (setf (arg-state-float-args state) (1+ float-args))
56 (cond ((>= stack-frame-size 4)
57 (my-make-wired-tn 'double-float
60 ((and (not (arg-state-did-int-arg state))
62 (my-make-wired-tn 'double-float
64 (+ (* float-args 2) 12)))
66 (my-make-wired-tn 'double-float
68 (+ stack-frame-size 4))))))
70 (define-alien-type-method (single-float :arg-tn) (type state)
71 (declare (ignore type))
72 (let ((stack-frame-size (arg-state-stack-frame-size state))
73 (float-args (arg-state-float-args state)))
74 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
75 (setf (arg-state-float-args state) (1+ float-args))
76 (cond ((>= stack-frame-size 4)
77 (my-make-wired-tn 'single-float
80 ((and (not (arg-state-did-int-arg state))
82 (my-make-wired-tn 'single-float
84 (+ (* float-args 2) 12)))
86 (my-make-wired-tn 'single-float
88 (+ stack-frame-size 4))))))
90 (defstruct result-state
93 (defun result-reg-offset (slot)
98 (define-alien-type-method (integer :result-tn) (type state)
99 (let ((num-results (result-state-num-results state)))
100 (setf (result-state-num-results state) (1+ num-results))
101 (multiple-value-bind (ptype reg-sc)
102 (if (alien-integer-type-signed type)
103 (values 'signed-byte-32 'signed-reg)
104 (values 'unsigned-byte-32 'unsigned-reg))
105 (my-make-wired-tn ptype reg-sc (result-reg-offset num-results)))))
107 (define-alien-type-method (system-area-pointer :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 'system-area-pointer 'sap-reg (result-reg-offset num-results))))
113 ;;; FIXME: do these still work? -- CSR, 2002-08-28
114 (define-alien-type-method (double-float :result-tn) (type state)
115 (declare (ignore type))
116 (let ((num-results (result-state-num-results state)))
117 (setf (result-state-num-results state) (1+ num-results))
118 (my-make-wired-tn 'double-float 'double-reg (* num-results 2))))
120 (define-alien-type-method (single-float :result-tn) (type state)
121 (declare (ignore type))
122 (let ((num-results (result-state-num-results state)))
123 (setf (result-state-num-results state) (1+ num-results))
124 (my-make-wired-tn 'single-float 'single-reg (* num-results 2))))
126 (define-alien-type-method (values :result-tn) (type state)
127 (let ((values (alien-values-type-values type)))
128 (when (> (length values) 2)
129 (error "Too many result values from c-call."))
130 (mapcar #'(lambda (type)
131 (invoke-alien-type-method :result-tn type state))
134 (!def-vm-support-routine make-call-out-tns (type)
135 (let ((arg-state (make-arg-state)))
137 (dolist (arg-type (alien-fun-type-arg-types type))
138 (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
139 (values (my-make-wired-tn 'positive-fixnum 'any-reg nsp-offset)
140 (* (max (arg-state-stack-frame-size arg-state) 4) n-word-bytes)
142 (invoke-alien-type-method :result-tn
143 (alien-fun-type-result-type type)
144 (make-result-state))))))
146 (deftransform %alien-funcall ((function type &rest args))
147 (aver (sb!c::constant-lvar-p type))
148 (let* ((type (sb!c::lvar-value type))
149 (env (sb!kernel:make-null-lexenv))
150 (arg-types (alien-fun-type-arg-types type))
151 (result-type (alien-fun-type-result-type type)))
152 (aver (= (length arg-types) (length args)))
153 ;; We need to do something special for 64-bit integer arguments
155 (if (or (some #'(lambda (type)
156 (and (alien-integer-type-p type)
157 (> (sb!alien::alien-integer-type-bits type) 32)))
159 (and (alien-integer-type-p result-type)
160 (> (sb!alien::alien-integer-type-bits result-type) 32)))
161 (collect ((new-args) (lambda-vars) (new-arg-types))
162 (dolist (type arg-types)
163 (let ((arg (gensym)))
165 (cond ((and (alien-integer-type-p type)
166 (> (sb!alien::alien-integer-type-bits type) 32))
167 ;; 64-bit long long types are stored in
168 ;; consecutive locations, endian word order,
169 ;; aligned to 8 bytes.
170 (when (oddp (length (new-args)))
173 (progn (new-args `(ash ,arg -32))
174 (new-args `(logand ,arg #xffffffff))
175 (if (oddp (length (new-arg-types)))
176 (new-arg-types (parse-alien-type '(unsigned 32) env)))
177 (if (alien-integer-type-signed type)
178 (new-arg-types (parse-alien-type '(signed 32) env))
179 (new-arg-types (parse-alien-type '(unsigned 32) env)))
180 (new-arg-types (parse-alien-type '(unsigned 32) env)))
182 (progn (new-args `(logand ,arg #xffffffff))
183 (new-args `(ash ,arg -32))
184 (if (oddp (length (new-arg-types)))
185 (new-arg-types (parse-alien-type '(unsigned 32) env)))
186 (new-arg-types (parse-alien-type '(unsigned 32) env))
187 (if (alien-integer-type-signed type)
188 (new-arg-types (parse-alien-type '(signed 32) env))
189 (new-arg-types (parse-alien-type '(unsigned 32) env)))))
192 (new-arg-types type)))))
193 (cond ((and (alien-integer-type-p result-type)
194 (> (sb!alien::alien-integer-type-bits result-type) 32))
195 (let ((new-result-type
196 (let ((sb!alien::*values-type-okay* t))
198 (if (alien-integer-type-signed result-type)
200 '(values (signed 32) (unsigned 32))
202 '(values (unsigned 32) (signed 32))
203 '(values (unsigned 32) (unsigned 32)))
205 `(lambda (function type ,@(lambda-vars))
206 (declare (ignore type))
212 (%alien-funcall function
213 ',(make-alien-fun-type
214 :arg-types (new-arg-types)
215 :result-type new-result-type)
217 (logior low (ash high 32))))))
219 `(lambda (function type ,@(lambda-vars))
220 (declare (ignore type))
221 (%alien-funcall function
222 ',(make-alien-fun-type
223 :arg-types (new-arg-types)
224 :result-type result-type)
226 (sb!c::give-up-ir1-transform))))
228 (define-vop (foreign-symbol-sap)
229 (:translate foreign-symbol-sap)
232 (:arg-types (:constant simple-string))
233 (:info foreign-symbol)
234 (:results (res :scs (sap-reg)))
235 (:result-types system-area-pointer)
237 (inst li res (make-fixup foreign-symbol :foreign))))
240 (define-vop (foreign-symbol-dataref-sap)
241 (:translate foreign-symbol-dataref-sap)
244 (:arg-types (:constant simple-string))
245 (:info foreign-symbol)
246 (:results (res :scs (sap-reg)))
247 (:result-types system-area-pointer)
248 (:temporary (:scs (non-descriptor-reg)) addr)
250 (inst li addr (make-fixup foreign-symbol :foreign-dataref))
253 (define-vop (call-out)
254 (:args (function :scs (sap-reg) :target cfunc)
256 (:results (results :more t))
257 (:ignore args results)
259 (:temporary (:sc any-reg :offset cfunc-offset
260 :from (:argument 0) :to (:result 0)) cfunc)
261 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
264 (let ((cur-nfp (current-nfp-tn vop)))
266 (store-stack-tn nfp-save cur-nfp))
267 (inst jal (make-fixup "call_into_c" :foreign))
268 (move cfunc function t)
270 (load-stack-tn cur-nfp nfp-save)))))
272 (define-vop (alloc-number-stack-space)
274 (:results (result :scs (sap-reg any-reg)))
275 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
277 (unless (zerop amount)
278 (let ((delta (logandc2 (+ amount 7) 7)))
279 (cond ((< delta (ash 1 15))
280 (inst subu nsp-tn delta))
283 (inst subu nsp-tn temp)))))
284 (move result nsp-tn)))
286 (define-vop (dealloc-number-stack-space)
289 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
291 (unless (zerop amount)
292 (let ((delta (logandc2 (+ amount 7) 7)))
293 (cond ((< delta (ash 1 15))
294 (inst addu nsp-tn delta))
297 (inst addu nsp-tn temp)))))))
300 (defun alien-callback-accessor-form (type sap offset)
301 (let ((parsed-type type))
302 (if (alien-integer-type-p parsed-type)
303 (let ((bits (sb!alien::alien-integer-type-bits parsed-type)))
305 (cond ((< bits n-word-bits)
307 (ceiling bits n-byte-bits)))
309 `(deref (sap-alien (sap+ ,sap
310 ,(+ byte-offset offset))
312 `(deref (sap-alien (sap+ ,sap ,offset) (* ,type))))))
314 ;;; Returns a vector in static space containing machine code for the
317 (defun alien-callback-assembler-wrapper (index result-type argument-types)
318 "Cons up a piece of code which calls enter-alien-callback with INDEX
319 and a pointer to the arguments."
321 (make-random-tn :kind :normal :sc (sc-or-lose 'any-reg) :offset n))
323 (make-random-tn :kind :normal :sc (sc-or-lose 'double-reg) :offset n)))
324 (let* ((segment (make-segment))
326 (mapcar (lambda (arg) (ceiling (alien-type-bits arg) n-word-bits))
328 (n-linkage-area-bytes 8)
330 (ceiling (or (alien-type-bits result-type) 0) n-word-bits))
331 (n-return-area-bytes (* n-return-area-words n-word-bytes))
332 (n-callee-register-args-bytes 16)
333 (n-frame-bytes (logandc2 (+ n-linkage-area-bytes
335 n-callee-register-args-bytes
340 (gprs (mapcar #'make-gpr '(4 5 6 7)))
341 (fprs (mapcar #'make-fpr '(12 14))))
342 (flet ((save-arg (type words)
343 (let ((offset (* words-processed n-word-bytes)))
344 (cond ((not (alien-float-type-p type))
345 (when (and (alien-integer-type-p type)
346 (> (sb!alien::alien-integer-type-bits type)
348 (oddp words-processed))
350 (incf words-processed)
351 (incf offset n-word-bytes))
356 (inst sw gpr nsp-tn offset)
359 (incf words-processed)
360 (incf offset n-word-bytes))))
361 ((alien-single-float-type-p type)
363 (let ((gpr (pop gprs))
366 (when gpr (inst sw gpr nsp-tn offset))
367 (when fpr (inst swc1 fpr nsp-tn offset))))
368 (incf words-processed)))
369 ((alien-double-float-type-p type)
370 (when (oddp words-processed)
372 (incf words-processed)
373 (incf offset n-word-bytes))
375 (let* ((gpr1 (pop gprs))
380 (ecase *backend-byte-order*
382 (inst sw gpr1 nsp-tn offset)
383 (inst sw gpr2 nsp-tn (+ offset n-word-bytes)))
385 (inst sw gpr2 nsp-tn offset)
386 (inst sw gpr1 nsp-tn (+ offset n-word-bytes)))))
388 (ecase *backend-byte-order*
390 (inst swc1 fpr nsp-tn offset)
391 (inst swc1-odd fpr nsp-tn (+ offset n-word-bytes)))
393 (inst swc1-odd fpr nsp-tn offset)
394 (inst swc1 fpr nsp-tn (+ offset n-word-bytes)))))))
395 (incf words-processed 2)))
397 (bug "Unknown alien floating point type: ~S" type))))))
399 (mapc #'save-arg argument-types n-argument-words)
400 ;; funcall3 (enter-alien-callback, index, args, return-area)
402 ;; INDEX is fixnumized, ARGS and RETURN-AREA don't need to be
403 ;; because they're word-aligned. Kinda gross, but hey ...
404 (destructuring-bind (v0 v1 a0 a1 a2 a3 t9 gp sp ra)
405 (mapcar #'make-gpr '(2 3 4 5 6 7 25 28 29 31))
406 ;; Allocate stack frame.
407 (inst subu sp n-frame-bytes)
410 (inst sw gp sp (- n-frame-bytes (* 2 n-word-bytes)))
411 (inst sw ra sp (- n-frame-bytes n-word-bytes))
413 ;; Setup the args and make the call.
414 (inst li a0 (get-lisp-obj-address #'enter-alien-callback))
415 (inst li t9 (foreign-symbol-address "funcall3"))
416 (inst li a1 (fixnumize index))
417 (inst addu a2 sp n-frame-bytes)
419 (inst addu a3 sp n-callee-register-args-bytes)
421 ;; We're back! Restore GP.
422 (inst lw gp sp (- n-frame-bytes (* 2 n-word-bytes)))
424 ;; Load the return value.
426 ((alien-single-float-type-p result-type)
427 (inst lwc1 (make-fpr 0) sp n-callee-register-args-bytes))
428 ((alien-double-float-type-p result-type)
429 (inst lwc1 (make-fpr 0) sp n-callee-register-args-bytes)
430 (inst lwc1 (make-fpr 1) sp (+ n-callee-register-args-bytes
432 ((and (alien-integer-type-p result-type)
433 (> (sb!alien::alien-integer-type-bits result-type)
435 (inst lw v0 sp n-callee-register-args-bytes)
436 (inst lw v1 sp (+ n-callee-register-args-bytes n-word-bytes)))
437 ((or (alien-integer-type-p result-type)
438 (alien-pointer-type-p result-type)
439 (alien-type-= #.(parse-alien-type 'system-area-pointer nil)
441 (inst lw v0 sp n-callee-register-args-bytes))
442 ((alien-void-type-p result-type))
444 (error "unrecognized alien type: ~A" result-type)))
446 ;; Restore RA, free stack frame, and return.
447 (inst lw ra sp (- n-frame-bytes n-word-bytes))
449 (inst addu sp n-frame-bytes))))
450 (finalize-segment segment)
451 ;; Now that the segment is done, convert it to a static
452 ;; vector we can point foreign code to.
453 (let ((buffer (sb!assem::segment-buffer segment)))
454 (make-static-vector (length buffer)
455 :element-type '(unsigned-byte 8)
456 :initial-contents buffer)))))