1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; cffi-corman.lisp --- CFFI-SYS implementation for Corman Lisp.
5 ;;; Copyright (C) 2005-2008, Luis Oliveira <loliveira(@)common-lisp.net>
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 ;;; This port is suffering from bitrot as of 2007-03-29. Corman Lisp
29 ;;; is too funky with ASDF, crashes easily, makes it very painful to
30 ;;; do any testing. -- luis
34 (defpackage #:cffi-sys
35 (:use
#:common-lisp
#:c-types
)
36 (:import-from
#:alexandria
#:with-unique-names
)
38 #:canonicalize-symbol-name-case
49 #:with-foreign-pointer
51 #:%foreign-type-alignment
53 #:%load-foreign-library
57 ;#:make-shareable-byte-vector
58 ;#:with-pointer-to-vector-data
59 #:foreign-symbol-pointer
60 #:defcfun-helper-forms
64 (in-package #:cffi-sys
)
68 (pushnew 'no-long-long
*features
*)
69 (pushnew 'no-foreign-funcall
*features
*)
73 (defun canonicalize-symbol-name-case (name)
74 (declare (string name
))
77 ;;;# Basic Pointer Operations
79 (deftype foreign-pointer
()
83 "Return true if PTR is a foreign pointer."
86 (defun pointer-eq (ptr1 ptr2
)
87 "Return true if PTR1 and PTR2 point to the same address."
88 (cpointer= ptr1 ptr2
))
90 (defun null-pointer ()
91 "Return a null pointer."
94 (defun null-pointer-p (ptr)
95 "Return true if PTR is a null pointer."
98 (defun inc-pointer (ptr offset
)
99 "Return a pointer pointing OFFSET bytes past PTR."
100 (let ((new-ptr (create-foreign-ptr)))
101 (setf (cpointer-value new-ptr
)
102 (+ (cpointer-value ptr
) offset
))
105 (defun make-pointer (address)
106 "Return a pointer pointing to ADDRESS."
107 (int-to-foreign-ptr address
))
109 (defun pointer-address (ptr)
110 "Return the address pointed to by PTR."
111 (foreign-ptr-to-int ptr
))
115 ;;; Functions and macros for allocating foreign memory on the stack
116 ;;; and on the heap. The main CFFI package defines macros that wrap
117 ;;; FOREIGN-ALLOC and FOREIGN-FREE in UNWIND-PROTECT for the common usage
118 ;;; when the memory has dynamic extent.
120 (defun %foreign-alloc
(size)
121 "Allocate SIZE bytes on the heap and return a pointer."
124 (defun foreign-free (ptr)
125 "Free a PTR allocated by FOREIGN-ALLOC."
128 (defmacro with-foreign-pointer
((var size
&optional size-var
) &body body
)
129 "Bind VAR to SIZE bytes of foreign memory during BODY. The
130 pointer in VAR is invalid beyond the dynamic extent of BODY, and
131 may be stack-allocated if supported by the implementation. If
132 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
134 (setf size-var
(gensym "SIZE")))
135 `(let* ((,size-var
,size
)
136 (,var
(malloc ,size-var
)))
141 ;;;# Shareable Vectors
143 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
144 ;;; should be defined to perform a copy-in/copy-out if the Lisp
145 ;;; implementation can't do this.
147 ;(defun make-shareable-byte-vector (size)
148 ; "Create a Lisp vector of SIZE bytes can passed to
149 ;WITH-POINTER-TO-VECTOR-DATA."
150 ; (make-array size :element-type '(unsigned-byte 8)))
152 ;(defmacro with-pointer-to-vector-data ((ptr-var vector) &body body)
153 ; "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
154 ; `(sb-sys:without-gcing
155 ; (let ((,ptr-var (sb-sys:vector-sap ,vector)))
160 ;;; According to the docs, Corman's C Function Definition Parser
161 ;;; converts int to long, so we'll assume that.
162 (defun convert-foreign-type (type-keyword)
163 "Convert a CFFI type keyword to a CormanCL type."
166 (:unsigned-char
:unsigned-char
)
168 (:unsigned-short
:unsigned-short
)
170 (:unsigned-int
:unsigned-long
)
172 (:unsigned-long
:unsigned-long
)
173 (:float
:single-float
)
174 (:double
:double-float
)
178 (defun %mem-ref
(ptr type
&optional
(offset 0))
179 "Dereference an object of TYPE at OFFSET bytes from PTR."
180 (unless (eql offset
0)
181 (setq ptr
(inc-pointer ptr offset
)))
183 (:char
(cref (:char
*) ptr
0))
184 (:unsigned-char
(cref (:unsigned-char
*) ptr
0))
185 (:short
(cref (:short
*) ptr
0))
186 (:unsigned-short
(cref (:unsigned-short
*) ptr
0))
187 (:int
(cref (:long
*) ptr
0))
188 (:unsigned-int
(cref (:unsigned-long
*) ptr
0))
189 (:long
(cref (:long
*) ptr
0))
190 (:unsigned-long
(cref (:unsigned-long
*) ptr
0))
191 (:float
(cref (:single-float
*) ptr
0))
192 (:double
(cref (:double-float
*) ptr
0))
193 (:pointer
(cref (:handle
*) ptr
0))))
195 ;(define-compiler-macro %mem-ref (&whole form ptr type &optional (offset 0))
196 ; (if (constantp type)
197 ; `(cref (,(convert-foreign-type type) *) ,ptr ,offset)
200 (defun %mem-set
(value ptr type
&optional
(offset 0))
201 "Set the object of TYPE at OFFSET bytes from PTR."
202 (unless (eql offset
0)
203 (setq ptr
(inc-pointer ptr offset
)))
205 (:char
(setf (cref (:char
*) ptr
0) value
))
206 (:unsigned-char
(setf (cref (:unsigned-char
*) ptr
0) value
))
207 (:short
(setf (cref (:short
*) ptr
0) value
))
208 (:unsigned-short
(setf (cref (:unsigned-short
*) ptr
0) value
))
209 (:int
(setf (cref (:long
*) ptr
0) value
))
210 (:unsigned-int
(setf (cref (:unsigned-long
*) ptr
0) value
))
211 (:long
(setf (cref (:long
*) ptr
0) value
))
212 (:unsigned-long
(setf (cref (:unsigned-long
*) ptr
0) value
))
213 (:float
(setf (cref (:single-float
*) ptr
0) value
))
214 (:double
(setf (cref (:double-float
*) ptr
0) value
))
215 (:pointer
(setf (cref (:handle
*) ptr
0) value
))))
217 ;;;# Calling Foreign Functions
219 (defun %foreign-type-size
(type-keyword)
220 "Return the size in bytes of a foreign type."
221 (sizeof (convert-foreign-type type-keyword
)))
223 ;;; Couldn't find anything in sys/ffi.lisp and the C declaration parser
224 ;;; doesn't seem to care about alignment so we'll assume that it's the
225 ;;; same as its size.
226 (defun %foreign-type-alignment
(type-keyword)
227 (sizeof (convert-foreign-type type-keyword
)))
229 (defun find-dll-containing-function (name)
230 "Searches for NAME in the loaded DLLs. If found, returns
231 the DLL's name (a string), else returns NIL."
232 (dolist (dll ct
::*dlls-loaded
*)
234 (ct::get-dll-proc-address name
(ct::dll-record-handle dll
)))
235 (return (ct::dll-record-name dll
)))))
237 ;;; This won't work at all...
239 (defmacro %foreign-funcall
(name &rest args
)
240 (let ((sym (gensym)))
242 (ct::install-dll-function
,(find-dll-containing-function name
)
244 (funcall ,sym
,@(loop for
(type arg
) on args by
#'cddr
245 if arg collect arg
)))))
248 ;;; It *might* be possible to implement by copying most of the code
249 ;;; from Corman's DEFUN-DLL. Alternatively, it could implemented the
250 ;;; same way as Lispworks' foreign-funcall. In practice, nobody uses
251 ;;; Corman with CFFI, apparently. :)
252 (defmacro %foreign-funcall
(name &rest args
)
253 "Call a foreign function NAME passing arguments ARGS."
254 `(format t
"~&;; Calling ~A with args ~S.~%" ,name
',args
))
256 (defun defcfun-helper-forms (name lisp-name rettype args types
)
257 "Return 2 values for DEFCFUN. A prelude form and a caller form."
258 (let ((ff-name (intern (format nil
"%cffi-foreign-function/~A" lisp-name
)))
259 ;; XXX This will only work if the dll is already loaded, fix this.
260 (dll (find-dll-containing-function name
)))
263 ,(mapcar (lambda (type)
264 (list (gensym) (convert-foreign-type type
)))
266 :return-type
,(convert-foreign-type rettype
)
269 ;; we want also :pascal linkage type to access
270 ;; the win32 api for instance..
272 `(,ff-name
,@args
))))
276 ;;; defun-c-callback vs. defun-direct-c-callback?
277 ;;; same issue as Allegro, no return type declaration, should we coerce?
278 (defmacro %defcallback
(name rettype arg-names arg-types body-form
)
279 (declare (ignore rettype
))
280 (with-unique-names (cb-sym)
282 (defun-c-callback ,cb-sym
283 ,(mapcar (lambda (sym type
) (list sym
(convert-foreign-type type
)))
286 (setf (get ',name
'callback-ptr
)
287 (get-callback-procinst ',cb-sym
)))))
289 ;;; Just continue to use the plist for now even though this really
290 ;;; should use a *CALLBACKS* hash table and not define the callbacks
291 ;;; as gensyms. Someone with access to Corman should update this.
292 (defun %callback
(name)
293 (get name
'callback-ptr
))
295 ;;;# Loading Foreign Libraries
297 (defun %load-foreign-library
(name)
298 "Load the foreign library NAME."
299 (ct::get-dll-record name
))
301 (defun %close-foreign-library
(name)
302 "Close the foreign library NAME."
303 (error "Not implemented."))
305 (defun native-namestring (pathname)
306 (namestring pathname
)) ; TODO: confirm
310 ;;; FFI to GetProcAddress from the Win32 API.
311 ;;; "The GetProcAddress function retrieves the address of an exported
312 ;;; function or variable from the specified dynamic-link library (DLL)."
313 (defun-dll get-proc-address
317 :library-name
"Kernel32.dll"
318 :entry-name
"GetProcAddress"
319 :linkage-type
:pascal
)
321 (defun foreign-symbol-pointer (name)
322 "Returns a pointer to a foreign symbol NAME."
323 (let ((str (lisp-string-to-c-string name
)))
325 (dolist (dll ct
::*dlls-loaded
*)
326 (let ((ptr (get-proc-address
327 (int-to-foreign-ptr (ct::dll-record-handle dll
))
329 (when (not (cpointer-null ptr
))