1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; cffi-ecl.lisp --- ECL backend for CFFI.
5 ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
7 ;;; Permission is hereby granted, free of charge, to any person
8 ;;; obtaining a copy of this software and associated documentation
9 ;;; files (the "Software"), to deal in the Software without
10 ;;; restriction, including without limitation the rights to use, copy,
11 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
12 ;;; of the Software, and to permit persons to whom the Software is
13 ;;; furnished to do so, subject to the following conditions:
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
18 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;;; DEALINGS IN THE SOFTWARE.
28 (in-package #:cffi-sys
)
31 ;;; ECL allows many ways of calling a foreign function, and also many
32 ;;; ways of finding the pointer associated to a function name. They
33 ;;; depend on whether the FFI relies on libffi or on the C/C++ compiler,
34 ;;; and whether they use the shared library loader to locate symbols
35 ;;; or they are linked by the linker.
39 ;;; ECL uses libffi to call foreign functions. The only way to find out
40 ;;; foreign symbols is by loading shared libraries and using dlopen()
45 ;;; ECL compiles FFI code as C/C++ statements. The names are resolved
46 ;;; at run time by the shared library loader every time the function
51 ;;; ECL compiles FFI code as C/C++ statements, but the name resolution
52 ;;; happens at link time. In this case you have to tell the ECL
53 ;;; compiler which are the right ld-flags (c:*ld-flags*) to link in
56 (defvar *cffi-ecl-method
*
58 #+(and dlopen
(not dffi
)) :dlopen
59 #-
(or dffi dlopen
) :c
/c
++
60 "The type of code that CFFI generates for ECL: :DFFI when using the
61 dynamical foreign function interface; :DLOPEN when using C code and
62 dynamical references to symbols; :C/C++ for C/C++ code with static
63 references to symbols.")
68 (pushnew 'no-long-long
*features
*)
69 (pushnew 'flat-namespace
*features
*)
73 (defun canonicalize-symbol-name-case (name)
74 (declare (string name
))
79 (defun %foreign-alloc
(size)
80 "Allocate SIZE bytes of foreign-addressable memory."
81 (si:allocate-foreign-data
:void size
))
83 (defun foreign-free (ptr)
84 "Free a pointer PTR allocated by FOREIGN-ALLOC."
85 (si:free-foreign-data ptr
))
87 (defmacro with-foreign-pointer
((var size
&optional size-var
) &body body
)
88 "Bind VAR to SIZE bytes of foreign memory during BODY. The
89 pointer in VAR is invalid beyond the dynamic extent of BODY, and
90 may be stack-allocated if supported by the implementation. If
91 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
93 (setf size-var
(gensym "SIZE")))
94 `(let* ((,size-var
,size
)
95 (,var
(%foreign-alloc
,size-var
)))
98 (foreign-free ,var
))))
100 ;;;# Misc. Pointer Operations
102 (deftype foreign-pointer
()
105 (defun null-pointer ()
106 "Construct and return a null pointer."
107 (si:allocate-foreign-data
:void
0))
109 (defun inc-pointer (ptr offset
)
110 "Return a pointer OFFSET bytes past PTR."
111 (ffi:make-pointer
(+ (ffi:pointer-address ptr
) offset
) :void
))
113 (defun pointerp (ptr)
114 "Return true if PTR is a foreign pointer."
115 (typep ptr
'si
:foreign-data
))
117 (defun pointer-eq (ptr1 ptr2
)
118 "Return true if PTR1 and PTR2 point to the same address."
119 (= (ffi:pointer-address ptr1
) (ffi:pointer-address ptr2
)))
121 (defun make-pointer (address)
122 "Return a pointer pointing to ADDRESS."
123 (ffi:make-pointer address
:void
))
125 (defun pointer-address (ptr)
126 "Return the address pointed to by PTR."
127 (ffi:pointer-address ptr
))
129 ;;;# Shareable Vectors
131 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
132 ;;; should be defined to perform a copy-in/copy-out if the Lisp
133 ;;; implementation can't do this.
135 (defun make-shareable-byte-vector (size)
136 "Create a Lisp vector of SIZE bytes that can passed to
137 WITH-POINTER-TO-VECTOR-DATA."
138 (make-array size
:element-type
'(unsigned-byte 8)))
140 (defmacro with-pointer-to-vector-data
((ptr-var vector
) &body body
)
141 "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
142 `(let ((,ptr-var
(si:make-foreign-data-from-array
,vector
)))
147 (defconstant +translation-table
+
148 '((:char
:byte
"char")
149 (:unsigned-char
:unsigned-byte
"unsigned char")
150 (:short
:short
"short")
151 (:unsigned-short
:unsigned-short
"unsigned short")
153 (:unsigned-int
:unsigned-int
"unsigned int")
155 (:unsigned-long
:unsigned-long
"unsigned long")
157 (:long-long
:long-long
"long long")
159 (:unsigned-long-long
:unsigned-long-long
"unsigned long long")
160 (:float
:float
"float")
161 (:double
:double
"double")
162 (:pointer
:pointer-void
"void*")
163 (:void
:void
"void")))
165 (defun cffi-type->ecl-type
(type-keyword)
166 "Convert a CFFI type keyword to an ECL type keyword."
167 (or (second (find type-keyword
+translation-table
+ :key
#'first
))
168 (error "~S is not a valid CFFI type" type-keyword
)))
170 (defun ecl-type->c-type
(type-keyword)
171 "Convert a CFFI type keyword to an valid C type keyword."
172 (or (third (find type-keyword
+translation-table
+ :key
#'second
))
173 (error "~S is not a valid CFFI type" type-keyword
)))
175 (defun %foreign-type-size
(type-keyword)
176 "Return the size in bytes of a foreign type."
177 (nth-value 0 (ffi:size-of-foreign-type
178 (cffi-type->ecl-type type-keyword
))))
180 (defun %foreign-type-alignment
(type-keyword)
181 "Return the alignment in bytes of a foreign type."
182 (nth-value 1 (ffi:size-of-foreign-type
183 (cffi-type->ecl-type type-keyword
))))
187 (defun %mem-ref
(ptr type
&optional
(offset 0))
188 "Dereference an object of TYPE at OFFSET bytes from PTR."
189 (let* ((type (cffi-type->ecl-type type
))
190 (type-size (ffi:size-of-foreign-type type
)))
191 (si:foreign-data-ref-elt
192 (si:foreign-data-recast ptr
(+ offset type-size
) :void
) offset type
)))
194 (defun %mem-set
(value ptr type
&optional
(offset 0))
195 "Set an object of TYPE at OFFSET bytes from PTR."
196 (let* ((type (cffi-type->ecl-type type
))
197 (type-size (ffi:size-of-foreign-type type
)))
198 (si:foreign-data-set-elt
199 (si:foreign-data-recast ptr
(+ offset type-size
) :void
)
202 ;;; Inline versions that use C expressions instead of function calls.
204 (defparameter +mem-ref-strings
+
205 (loop for
(cffi-type ecl-type c-string
) in
+translation-table
+
206 for string
= (format nil
"*((~A *)(((char*)#0)+#1))" c-string
)
207 collect
(list cffi-type ecl-type string
)))
209 (defparameter +mem-set-strings
+
210 (loop for
(cffi-type ecl-type c-string
) in
+translation-table
+
211 for string
= (format nil
"*((~A *)(((char*)#0)+#1))=#2" c-string
)
212 collect
(list cffi-type ecl-type string
)))
214 (define-compiler-macro %mem-ref
(&whole whole ptr type
&optional
(offset 0))
215 (if (and (constantp type
) (constantp offset
))
216 (let ((record (assoc (eval type
) +mem-ref-strings
+)))
217 `(ffi:c-inline
(,ptr
,offset
)
218 (:pointer-void
:cl-index
) ; argument types
219 ,(second record
) ; return type
220 ,(third record
) ; the precomputed expansion
224 (define-compiler-macro %mem-set
(&whole whole value ptr type
&optional
(offset 0))
225 (if (and (constantp type
) (constantp offset
))
226 (let ((record (assoc (eval type
) +mem-set-strings
+)))
227 `(ffi:c-inline
(,ptr
,offset
,value
) ; arguments with type translated
228 (:pointer-void
:cl-index
,(second record
))
229 :void
; does not return anything
230 ,(third record
) ; precomputed expansion
234 ;;;# Calling Foreign Functions
236 (defconstant +ecl-inline-codes
+ "#0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#a,#b,#c,#d,#e,#f,#g,#h,#i,#j,#k,#l,#m,#n,#o,#p,#q,#r,#s,#t,#u,#v,#w,#x,#y,#z")
238 (defun c-inline-function-call (thing fixed-types types values return-type dynamic-call variadic
)
240 (when (stringp thing
)
241 (setf thing
`(%foreign-symbol-pointer
,thing nil
)))
243 (push :pointer-void types
))
245 (format nil
"~{~A~^, ~}~A"
246 (mapcar #'ecl-type-
>c-type fixed-types
) (if (null variadic
) "" ", ...")))
249 ;; #0 is already used in a cast (it is a function pointer)
250 (subseq +ecl-inline-codes
+ 3 (max 3 (1- (* (length values
) 3))))
251 ;; #0 is not used, so we start from the beginning
252 (subseq +ecl-inline-codes
+ 0 (max 0 (1- (* (length values
) 3))))))
256 (format nil
"extern ~A ~A(~A);"
257 (ecl-type->c-type return-type
) thing decl-args
)))
260 (format nil
"((~A (*)(~A))(#0))(~A)"
261 (ecl-type->c-type return-type
) decl-args call-args
)
262 (format nil
"~A(~A)" thing call-args
))))
264 (ffi:clines
,@(ensure-list clines
))
265 (ffi:c-inline
,values
,types
,return-type
,call-code
:one-liner t
:side-effects t
))))
267 (defun dffi-function-pointer-call (pointer types values return-type
)
268 (when (stringp pointer
)
269 (setf pointer
`(%foreign-symbol-pointer
,pointer nil
)))
271 `(error "In interpreted code, attempted to call a foreign function~% ~A~%~
272 but ECL was built without support for that." ,pointer
)
274 `(si::call-cfun
,pointer
,return-type
(list ,@types
) (list ,@values
)))
276 (defun foreign-funcall-parse-args (args)
277 "Return three values, lists of arg types, values, and result type."
278 (let ((return-type :void
))
279 (loop for
(type arg
) on args by
#'cddr
280 if arg collect
(cffi-type->ecl-type type
) into types
281 and collect arg into values
282 else do
(setf return-type
(cffi-type->ecl-type type
))
283 finally
(return (values types values return-type
)))))
285 (defmacro %foreign-funcall
(name args
&key library convention
)
286 "Call a foreign function."
287 (declare (ignore library convention
))
288 (multiple-value-bind (types values return-type
)
289 (foreign-funcall-parse-args args
)
292 ,(dffi-function-pointer-call name types values return-type
)
294 ,(ecase *cffi-ecl-method
*
295 (:dffi
(dffi-function-pointer-call name types values return-type
))
296 (:dlopen
(c-inline-function-call name types types values return-type t nil
))
297 (:c
/c
++ (c-inline-function-call name types types values return-type nil nil
))))))
299 (defmacro %foreign-funcall-pointer
(pointer args
&key convention
)
300 "Funcall a pointer to a foreign function."
301 (declare (ignore convention
))
302 (multiple-value-bind (types values return-type
)
303 (foreign-funcall-parse-args args
)
306 ,(dffi-function-pointer-call pointer types values return-type
)
308 ,(if (eq *cffi-ecl-method
* :dffi
)
309 (dffi-function-pointer-call pointer types values return-type
)
310 (c-inline-function-call pointer types types values return-type t nil
)))))
312 (defmacro %foreign-funcall-varargs
(name args varargs
&key library convention
)
313 (declare (ignore library convention
))
314 (multiple-value-bind (fixed-types fixed-values
)
315 (foreign-funcall-parse-args args
)
316 (multiple-value-bind (varargs-types varargs-values return-type
)
317 (foreign-funcall-parse-args varargs
)
318 (let ((all-types (append fixed-types varargs-types
))
319 (values (append fixed-values varargs-values
)))
322 ,(dffi-function-pointer-call name all-types values return-type
)
324 ,(ecase *cffi-ecl-method
*
325 (:dffi
(dffi-function-pointer-call name all-types values return-type
))
326 (:dlopen
(c-inline-function-call name fixed-types all-types values return-type t t
))
327 (:c
/c
++ (c-inline-function-call name fixed-types all-types values return-type nil t
))))))))
329 (defmacro %foreign-funcall-pointer-varargs
(pointer args varargs
&key convention
)
330 (declare (ignore convention
))
331 (multiple-value-bind (fixed-types fixed-values
)
332 (foreign-funcall-parse-args args
)
333 (multiple-value-bind (varargs-types varargs-values return-type
)
334 (foreign-funcall-parse-args varargs
)
335 (let ((all-types (append fixed-types varargs-types
))
336 (values (append fixed-values varargs-values
)))
339 ,(dffi-function-pointer-call pointer all-types values return-type
)
341 ,(if (eq *cffi-ecl-method
* :dffi
)
342 (dffi-function-pointer-call pointer all-types values return-type
)
343 (c-inline-function-call pointer fixed-types all-types values return-type t t
)))))))
345 ;;;# Foreign Libraries
347 (defun %load-foreign-library
(name path
)
348 "Load a foreign library."
349 (declare (ignore name
))
350 #-dffi
(error "LOAD-FOREIGN-LIBRARY requires ECL's DFFI support. Use ~
351 FFI:LOAD-FOREIGN-LIBRARY with a constant argument instead.")
353 (handler-case (si:load-foreign-module path
)
355 (error "file error while trying to load `~A'" path
))))
357 (defun %close-foreign-library
(handle)
358 "Close a foreign library."
359 (handler-case (si::unload-foreign-module handle
)
360 (undefined-function ()
361 (restart-case (error "Detected ECL prior to version 15.2.21. ~
362 Function CFFI:CLOSE-FOREIGN-LIBRARY isn't implemented yet.")
363 (ignore () :report
"Continue anyway (foreign library will remain opened).")))))
365 (defun native-namestring (pathname)
366 (namestring pathname
))
370 (defvar *callbacks
* (make-hash-table))
372 ;;; Intern a symbol in the CFFI-CALLBACKS package used to name the
373 ;;; internal callback for NAME.
374 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
375 (defun intern-callback (name)
376 (intern (format nil
"~A::~A"
377 (if-let (package (symbol-package name
))
378 (package-name package
)
383 (defmacro %defcallback
(name rettype arg-names arg-types body
385 (declare (ignore convention
))
386 (let ((cb-name (intern-callback name
))
387 (cb-type #.
(if (> ext
:+ecl-version-number
+ 160102)
390 (ffi:defcallback
(,cb-name
,cb-type
)
391 ,(cffi-type->ecl-type rettype
)
392 ,(mapcar #'list arg-names
393 (mapcar #'cffi-type-
>ecl-type arg-types
))
395 (setf (gethash ',name
*callbacks
*) ',cb-name
))))
397 (defun %callback
(name)
398 (multiple-value-bind (symbol winp
)
399 (gethash name
*callbacks
*)
401 (error "Undefined callback: ~S" name
))
402 (ffi:callback symbol
)))
406 (defun %foreign-symbol-pointer
(name library
)
407 "Returns a pointer to a foreign symbol NAME."
408 (declare (ignore library
))
410 (si:find-foreign-symbol
(coerce name
'base-string
)
411 :default
:pointer-void
0)