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
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
14 ;;;; *************************************************************************
18 #+(or openmcl digitool)
19 (defvar *keyword-package* (find-package "KEYWORD"))
21 #+(or openmcl digitool)
22 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
23 ; So this provides a function to convert any quoted symbols to keywords.
30 (intern (symbol-name obj) *keyword-package*))
31 ((and (listp obj) (eq (car obj) 'cl:quote))
34 (intern obj *keyword-package*))
38 ; Wrapper for unexported function we have to use
40 (defmacro def-mcl-type (name type)
41 `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
43 (defmacro def-constant (name value &key (export nil))
44 "Macro to define a constant and to export it"
45 `(eval-when (:compile-toplevel :load-toplevel :execute)
46 (defconstant ,name ,value)
47 ,(when export (list 'export `(quote ,name)))
50 (defmacro def-type (name type)
51 "Generates a (deftype) statement for CL. Currently, only CMUCL
52 supports takes advantage of this optimization."
53 #+(or lispworks allegro openmcl digitool cormanlisp) (declare (ignore type))
54 #+(or lispworks allegro openmcl digitool cormanlisp) `(deftype ,name () t)
56 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
58 `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
61 (defmacro null-char-p (val)
62 "Returns T if character is NULL"
65 (defmacro def-foreign-type (name type)
66 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
67 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
68 #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
69 #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
70 #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
71 #+(or openmcl digitool)
72 (let ((mcl-type (convert-from-uffi-type type :type)))
73 (unless (or (keywordp mcl-type) (consp mcl-type))
74 (setf mcl-type `(quote ,mcl-type)))
76 `(def-mcl-type ,(keyword name) ,mcl-type)
78 `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
81 (eval-when (:compile-toplevel :load-toplevel :execute)
82 (defvar +type-conversion-hash+ (make-hash-table :size 20 :test #'eq))
83 #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
84 (make-hash-table :size 20 :test #'eq))
88 (defvar *cmu-sbcl-def-type-list*
89 '((:char . (alien:signed 8))
90 (:unsigned-char . (alien:unsigned 8))
91 (:byte . (alien:signed 8))
92 (:unsigned-byte . (alien:unsigned 8))
93 (:short . (alien:signed 16))
94 (:unsigned-short . (alien:unsigned 16))
95 (:int . (alien:signed 32))
96 (:unsigned-int . (alien:unsigned 32))
97 #-x86-64 (:long . (alien:signed 32))
98 #-x86-64 (:unsigned-long . (alien:unsigned 32))
99 #+x86-64 (:long . (alien:signed 64))
100 #+x86-64 (:unsigned-long . (alien:unsigned 64))
101 (:long-long (alien:signed 64))
102 (:unsigned-long-long (alien:unsigned 64))
103 (:float . alien:single-float)
104 (:double . alien:double-float)
107 "Conversions in CMUCL for def-foreign-type are different than in def-function")
110 (defvar *cmu-sbcl-def-type-list*
111 '((:char . (sb-alien:signed 8))
112 (:unsigned-char . (sb-alien:unsigned 8))
113 (:byte . (sb-alien:signed 8))
114 (:unsigned-byte . (sb-alien:unsigned 8))
115 (:short . (sb-alien:signed 16))
116 (:unsigned-short . (sb-alien:unsigned 16))
117 (:int . (sb-alien:signed 32))
118 (:unsigned-int . (sb-alien:unsigned 32))
119 #-x86-64 (:long . (sb-alien:signed 32))
120 #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
121 #+x86-64 (:long . (sb-alien:signed 64))
122 #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
123 (:long-long (sb-alien:signed 64))
124 (:unsigned-long-long (sb-alien:unsigned 64))
125 (:float . sb-alien:single-float)
126 (:double . sb-alien:double-float)
129 "Conversions in SBCL for def-foreign-type are different than in def-function")
131 (defvar *type-conversion-list* nil)
134 (setq *type-conversion-list*
135 '((* . *) (:void . c-call:void)
136 (:pointer-void . (* t))
137 (:cstring . c-call:c-string)
138 (:char . c-call:char)
139 (:unsigned-char . (alien:unsigned 8))
140 (:byte . (alien:signed 8))
141 (:unsigned-byte . (alien:unsigned 8))
142 (:short . c-call:short)
143 (:unsigned-short . c-call:unsigned-short)
144 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
145 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
146 (:long-long . c-call:long-long)
147 (:unsigned-long-long . c-call:unsigned-long-long)
148 (:float . c-call:float) (:double . c-call:double)
149 (:array . alien:array)))
152 (setq *type-conversion-list*
153 '((* . *) (:void . sb-alien:void)
154 (:pointer-void . (* t))
155 #-sb-unicode(:cstring . sb-alien:c-string)
156 #+sb-unicode(:cstring . sb-alien:utf8-string)
157 (:char . sb-alien:char)
158 (:unsigned-char . (sb-alien:unsigned 8))
159 (:byte . (sb-alien:signed 8))
160 (:unsigned-byte . (sb-alien:unsigned 8))
161 (:short . sb-alien:short)
162 (:unsigned-short . sb-alien:unsigned-short)
163 (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int)
164 (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
165 (:long-long . sb-alien:long-long)
166 (:unsigned-long-long . sb-alien:unsigned-long-long)
167 (:float . sb-alien:float) (:double . sb-alien:double)
168 (:array . sb-alien:array)))
170 #+(or allegro cormanlisp)
171 (setq *type-conversion-list*
172 '((* . *) (:void . :void)
174 (:pointer-void . (* :void))
175 (:cstring . (* :unsigned-char))
177 (:unsigned-byte . :unsigned-char)
179 (:unsigned-char . :unsigned-char)
180 (:int . :int) (:unsigned-int . :unsigned-int)
181 (:long . :long) (:unsigned-long . :unsigned-long)
182 (:float . :float) (:double . :double)
185 #+(or allegro cormanlisp)
186 (push 'uffi:no-long-long cl:*features)
189 (setq *type-conversion-list*
190 '((* . :pointer) (:void . :void)
192 (:pointer-void . (:pointer :void))
193 (:cstring . (:reference-pass (:ef-mb-string :external-format
194 (:latin-1 :eol-style :lf))
196 (:cstring-returning . (:reference (:ef-mb-string :external-format
197 (:latin-1 :eol-style :lf))
200 (:unsigned-byte . (:unsigned :byte))
202 (:unsigned-char . (:unsigned :char))
203 (:int . :int) (:unsigned-int . (:unsigned :int))
204 (:long . :long) (:unsigned-long . (:unsigned :long))
205 #+lispworks6 (:long-long . :int64)
206 #+lispworks6 (:unsigned-long-long . :uint64)
207 (:float . :float) (:double . :double)
208 (:array . :c-array)))
210 #+(and lispworks (not lispworks6))
211 (push 'uffi:no-long-long cl:*features)
214 (setq *type-conversion-list*
215 '((* . :pointer) (:void . :void)
216 (:short . :short) (:unsigned-short . :unsigned-short)
217 (:pointer-void . :pointer)
220 (:unsigned-char . :unsigned-byte)
221 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
222 (:int . :long) (:unsigned-int . :unsigned-long)
223 (:long . :long) (:unsigned-long . :unsigned-long)
224 (:float . :single-float) (:double . :double-float)
227 (push 'uffi:no-long-long cl:*features)
230 (setq *type-conversion-list*
231 '((* . :address) (:void . :void)
232 (:short . :short) (:unsigned-short . :unsigned-short)
233 (:pointer-void . :address)
234 (:cstring . :address)
235 (:char . :signed-char)
236 (:unsigned-char . :unsigned-char)
237 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
238 (:int . :int) (:unsigned-int . :unsigned-int)
239 (:long . :long) (:unsigned-long . :unsigned-long)
240 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
241 (:float . :single-float) (:double . :double-float)
244 (dolist (type *type-conversion-list*)
245 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
248 (dolist (type *cmu-sbcl-def-type-list*)
249 (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
251 (defun basic-convert-from-uffi-type (type)
252 (let ((found-type (gethash type +type-conversion-hash+)))
255 #-(or openmcl digitool) type
256 #+(or openmcl digitool) (keyword type))))
258 (defun %convert-from-uffi-type (type context)
259 "Converts from a uffi type to an implementation specific type"
262 #+(or allegro cormanlisp)
263 ((and (or (eq context :routine) (eq context :return))
265 (setq type '((* :char) integer)))
268 (let ((cmu-type (gethash type *cmu-def-type-hash*)))
271 (basic-convert-from-uffi-type type))))
273 ((and (eq context :return)
275 (basic-convert-from-uffi-type :cstring-returning))
277 ((and (eq type :void) (eq context :return)) nil)
279 (basic-convert-from-uffi-type type)))
280 (let ((sub-type (car type)))
283 (convert-from-uffi-type (cadr type) context))
285 #+(or openmcl digitool) `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
286 #-(or openmcl digitool) (%convert-from-uffi-type (list '* (cadr type)) :struct)
289 #+(or openmcl digitool) `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
290 #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :struct)
293 #+(or openmcl digitool) `(:union ,(%convert-from-uffi-type (cadr type) :union))
294 #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :union)
297 (cons (%convert-from-uffi-type (first type) context)
298 (%convert-from-uffi-type (rest type) context)))))))
300 (defun convert-from-uffi-type (type context)
301 (let ((result (%convert-from-uffi-type type context)))
303 ((atom result) result)
305 ((eq (car result) :address)
306 (if (eq context :struct)
307 (append '(:*) (cdr result))
310 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
313 (eval-when (:compile-toplevel :load-toplevel :execute)
314 (when (char= #\a (schar (symbol-name '#:a) 0))
315 (pushnew :uffi-lowercase-reader *features*))
316 (when (not (string= (symbol-name '#:a)
318 (pushnew :uffi-case-sensitive *features*)))
320 (defun make-lisp-name (name)
321 (let ((converted (substitute #\- #\_ name)))
323 #+uffi-case-sensitive converted
324 #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
325 #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
327 (eval-when (:compile-toplevel :load-toplevel :execute)
328 (setq cl:*features* (delete :uffi-lowercase-reader *features*))
329 (setq cl:*features* (delete :uffi-case-sensitive *features*)))