1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: primitives.lisp
6 ;;;; Purpose: UFFI source to handle immediate types
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
12 ;;;; *************************************************************************
16 #+(or openmcl digitool)
17 (defvar *keyword-package* (find-package "KEYWORD"))
19 #+(or openmcl digitool)
20 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
21 ; So this provides a function to convert any quoted symbols to keywords.
28 (intern (symbol-name obj) *keyword-package*))
29 ((and (listp obj) (eq (car obj) 'cl:quote))
32 (intern obj *keyword-package*))
36 ; Wrapper for unexported function we have to use
38 (defmacro def-mcl-type (name type)
39 `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
41 (defmacro def-constant (name value &key (export nil))
42 "Macro to define a constant and to export it"
43 `(eval-when (:compile-toplevel :load-toplevel :execute)
44 (defconstant ,name ,value)
45 ,(when export (list 'export `(quote ,name)))
48 (defmacro def-type (name type)
49 "Generates a (deftype) statement for CL. Currently, only CMUCL
50 supports takes advantage of this optimization."
51 #+(or lispworks allegro openmcl digitool cormanlisp) (declare (ignore type))
52 #+(or lispworks allegro openmcl digitool cormanlisp) `(deftype ,name () t)
54 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
56 `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
59 (defmacro null-char-p (val)
60 "Returns T if character is NULL"
63 (defmacro def-foreign-type (name type)
64 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
65 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
66 #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
67 #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
68 #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
69 #+(or openmcl digitool)
70 (let ((mcl-type (convert-from-uffi-type type :type)))
71 (unless (or (keywordp mcl-type) (consp mcl-type))
72 (setf mcl-type `(quote ,mcl-type)))
74 `(def-mcl-type ,(keyword name) ,mcl-type)
76 `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
79 (eval-when (:compile-toplevel :load-toplevel :execute)
80 (defvar +type-conversion-hash+ (make-hash-table :size 20 :test #'eq))
81 #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
82 (make-hash-table :size 20 :test #'eq))
86 (defvar *cmu-sbcl-def-type-list*
87 '((:char . (alien:signed 8))
88 (:unsigned-char . (alien:unsigned 8))
89 (:byte . (alien:signed 8))
90 (:unsigned-byte . (alien:unsigned 8))
91 (:short . (alien:signed 16))
92 (:unsigned-short . (alien:unsigned 16))
93 (:int . (alien:signed 32))
94 (:unsigned-int . (alien:unsigned 32))
95 #-x86-64 (:long . (alien:signed 32))
96 #-x86-64 (:unsigned-long . (alien:unsigned 32))
97 #+x86-64 (:long . (alien:signed 64))
98 #+x86-64 (:unsigned-long . (alien:unsigned 64))
99 (:long-long (alien:signed 64))
100 (:unsigned-long-long (alien:unsigned 64))
101 (:float . alien:single-float)
102 (:double . alien:double-float)
105 "Conversions in CMUCL for def-foreign-type are different than in def-function")
108 (defvar *cmu-sbcl-def-type-list*
109 '((:char . (sb-alien:signed 8))
110 (:unsigned-char . (sb-alien:unsigned 8))
111 (:byte . (sb-alien:signed 8))
112 (:unsigned-byte . (sb-alien:unsigned 8))
113 (:short . (sb-alien:signed 16))
114 (:unsigned-short . (sb-alien:unsigned 16))
115 (:int . (sb-alien:signed 32))
116 (:unsigned-int . (sb-alien:unsigned 32))
117 #-x86-64 (:long . (sb-alien:signed 32))
118 #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
119 #+x86-64 (:long . (sb-alien:signed 64))
120 #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
121 (:long-long (sb-alien:signed 64))
122 (:unsigned-long-long (sb-alien:unsigned 64))
123 (:float . sb-alien:single-float)
124 (:double . sb-alien:double-float)
127 "Conversions in SBCL for def-foreign-type are different than in def-function")
129 (defvar *type-conversion-list* nil)
132 (setq *type-conversion-list*
133 '((* . *) (:void . c-call:void)
134 (:pointer-void . (* t))
135 (:cstring . c-call:c-string)
136 (:char . c-call:char)
137 (:unsigned-char . (alien:unsigned 8))
138 (:byte . (alien:signed 8))
139 (:unsigned-byte . (alien:unsigned 8))
140 (:short . c-call:short)
141 (:unsigned-short . c-call:unsigned-short)
142 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
143 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
144 #+#.(cl:if (cl:and (cl:find-package (cl:string '#:c-call))
145 (cl:find-symbol (cl:string '#:long-long)
146 (cl:string '#:c-call)))
148 (:long-long . c-call:long-long)
149 #+#.(cl:if (cl:and (cl:find-package (cl:string '#:c-call))
150 (cl:find-symbol (cl:string '#:unsigned-long-long)
151 (cl:string '#:c-call)))
153 (:unsigned-long-long . c-call:unsigned-long-long)
154 (:float . c-call:float) (:double . c-call:double)
155 (:array . alien:array)))
158 (setq *type-conversion-list*
159 '((* . *) (:void . sb-alien:void)
160 (:pointer-void . (* t))
161 #-sb-unicode(:cstring . sb-alien:c-string)
162 #+sb-unicode(:cstring . sb-alien:utf8-string)
163 (:char . sb-alien:char)
164 (:unsigned-char . (sb-alien:unsigned 8))
165 (:byte . (sb-alien:signed 8))
166 (:unsigned-byte . (sb-alien:unsigned 8))
167 (:short . sb-alien:short)
168 (:unsigned-short . sb-alien:unsigned-short)
169 (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int)
170 (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
171 (:long-long . sb-alien:long-long)
172 (:unsigned-long-long . sb-alien:unsigned-long-long)
173 (:float . sb-alien:float) (:double . sb-alien:double)
174 (:array . sb-alien:array)))
176 #+(or allegro cormanlisp)
177 (setq *type-conversion-list*
178 '((* . *) (:void . :void)
180 (:pointer-void . (* :void))
181 (:cstring . (* :unsigned-char))
183 (:unsigned-byte . :unsigned-char)
185 (:unsigned-char . :unsigned-char)
186 (:int . :int) (:unsigned-int . :unsigned-int)
187 (:long . :long) (:unsigned-long . :unsigned-long)
188 #+64bit (:unsigned-long-long . :unsigned-long)
189 #+64bit (:long-long . :unsigned-long)
190 (:float . :float) (:double . :double)
193 #+(and allegro (not 64bit))
194 (push 'uffi:no-long-long cl:*features*)
197 (push 'uffi:no-long-long cl:*features*)
200 (setq *type-conversion-list*
201 '((* . :pointer) (:void . :void)
203 (:pointer-void . (:pointer :void))
204 (:cstring . (:reference-pass (:ef-mb-string :external-format
205 (:latin-1 :eol-style :lf))
207 (:cstring-returning . (:reference (:ef-mb-string :external-format
208 (:latin-1 :eol-style :lf))
211 (:unsigned-byte . (:unsigned :byte))
213 (:unsigned-char . (:unsigned :char))
214 (:int . :int) (:unsigned-int . (:unsigned :int))
215 (:long . :long) (:unsigned-long . (:unsigned :long))
216 #+lispworks6 (:long-long . :int64)
217 #+lispworks6 (:unsigned-long-long . :uint64)
218 (:float . :float) (:double . :double)
219 (:array . :c-array)))
221 #+(and lispworks (not lispworks6))
222 (push 'uffi:no-long-long cl:*features*)
225 (setq *type-conversion-list*
226 '((* . :pointer) (:void . :void)
227 (:short . :short) (:unsigned-short . :unsigned-short)
228 (:pointer-void . :pointer)
231 (:unsigned-char . :unsigned-byte)
232 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
233 (:int . :long) (:unsigned-int . :unsigned-long)
234 (:long . :long) (:unsigned-long . :unsigned-long)
235 (:float . :single-float) (:double . :double-float)
238 (push 'uffi:no-long-long cl:*features*)
241 (setq *type-conversion-list*
242 '((* . :address) (:void . :void)
243 (:short . :short) (:unsigned-short . :unsigned-short)
244 (:pointer-void . :address)
245 (:cstring . :address)
246 (:char . :signed-char)
247 (:unsigned-char . :unsigned-char)
248 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
249 (:int . :int) (:unsigned-int . :unsigned-int)
250 (:long . :long) (:unsigned-long . :unsigned-long)
251 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
252 (:float . :single-float) (:double . :double-float)
255 (dolist (type *type-conversion-list*)
256 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
259 (dolist (type *cmu-sbcl-def-type-list*)
260 (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
262 (defun basic-convert-from-uffi-type (type)
263 (let ((found-type (gethash type +type-conversion-hash+)))
266 #-(or openmcl digitool) type
267 #+(or openmcl digitool) (keyword type))))
269 (defun %convert-from-uffi-type (type context)
270 "Converts from a uffi type to an implementation specific type"
273 #+(or allegro cormanlisp)
274 ((and (or (eq context :routine) (eq context :return))
276 (setq type '((* :char) integer)))
279 (let ((cmu-type (gethash type *cmu-def-type-hash*)))
282 (basic-convert-from-uffi-type type))))
284 ((and (eq context :return)
286 (basic-convert-from-uffi-type :cstring-returning))
288 ((and (eq type :void) (eq context :return)) nil)
290 (basic-convert-from-uffi-type type)))
291 (let ((sub-type (car type)))
294 (convert-from-uffi-type (cadr type) context))
296 #+(or openmcl digitool) `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
297 #-(or openmcl digitool) (%convert-from-uffi-type (list '* (cadr type)) :struct)
300 #+(or openmcl digitool) `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
301 #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :struct)
304 #+(or openmcl digitool) `(:union ,(%convert-from-uffi-type (cadr type) :union))
305 #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :union)
308 (cons (%convert-from-uffi-type (first type) context)
309 (%convert-from-uffi-type (rest type) context)))))))
311 (defun convert-from-uffi-type (type context)
312 (let ((result (%convert-from-uffi-type type context)))
314 ((atom result) result)
316 ((eq (car result) :address)
317 (if (eq context :struct)
318 (append '(:*) (cdr result))
321 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
324 (eval-when (:compile-toplevel :load-toplevel :execute)
325 (when (char= #\a (schar (symbol-name '#:a) 0))
326 (pushnew :uffi-lowercase-reader *features*))
327 (when (not (string= (symbol-name '#:a)
329 (pushnew :uffi-case-sensitive *features*)))
331 (defun make-lisp-name (name)
332 (let ((converted (substitute #\- #\_ name)))
334 #+uffi-case-sensitive converted
335 #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
336 #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
338 (eval-when (:compile-toplevel :load-toplevel :execute)
339 (setq cl:*features* (delete :uffi-lowercase-reader *features*))
340 (setq cl:*features* (delete :uffi-case-sensitive *features*)))