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 (: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 (:float . sb-alien:single-float)
122 (:double . sb-alien:double-float)
125 "Conversions in SBCL for def-foreign-type are different than in def-function")
127 (defvar *type-conversion-list* nil)
130 (setq *type-conversion-list*
131 '((* . *) (:void . c-call:void)
132 (:pointer-void . (* t))
133 (:cstring . c-call:c-string)
134 (:char . c-call:char)
135 (:unsigned-char . (alien:unsigned 8))
136 (:byte . (alien:signed 8))
137 (:unsigned-byte . (alien:unsigned 8))
138 (:short . c-call:short)
139 (:unsigned-short . c-call:unsigned-short)
140 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
141 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
142 (:float . c-call:float) (:double . c-call:double)
143 (:array . alien:array)))
146 (setq *type-conversion-list*
147 '((* . *) (:void . sb-alien:void)
148 (:pointer-void . (* t))
149 #-sb-unicode(:cstring . sb-alien:c-string)
150 #+sb-unicode(:cstring . sb-alien:utf8-string)
151 (:char . sb-alien:char)
152 (:unsigned-char . (sb-alien:unsigned 8))
153 (:byte . (sb-alien:signed 8))
154 (:unsigned-byte . (sb-alien:unsigned 8))
155 (:short . sb-alien:short)
156 (:unsigned-short . sb-alien:unsigned-short)
157 (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int)
158 (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
159 (:float . sb-alien:float) (:double . sb-alien:double)
160 (:array . sb-alien:array)))
162 #+(or allegro cormanlisp)
163 (setq *type-conversion-list*
164 '((* . *) (:void . :void)
166 (:pointer-void . (* :void))
167 (:cstring . (* :unsigned-char))
169 (:unsigned-byte . :unsigned-char)
171 (:unsigned-char . :unsigned-char)
172 (:int . :int) (:unsigned-int . :unsigned-int)
173 (:long . :long) (:unsigned-long . :unsigned-long)
174 (:float . :float) (:double . :double)
178 (setq *type-conversion-list*
179 '((* . :pointer) (:void . :void)
181 (:pointer-void . (:pointer :void))
182 (:cstring . (:reference-pass (:ef-mb-string :external-format
183 (:latin-1 :eol-style :lf))
185 (:cstring-returning . (:reference (:ef-mb-string :external-format
186 (:latin-1 :eol-style :lf))
189 (:unsigned-byte . (:unsigned :byte))
191 (:unsigned-char . (:unsigned :char))
192 (:int . :int) (:unsigned-int . (:unsigned :int))
193 (:long . :long) (:unsigned-long . (:unsigned :long))
194 (:float . :float) (:double . :double)
195 (:array . :c-array)))
198 (setq *type-conversion-list*
199 '((* . :pointer) (:void . :void)
200 (:short . :short) (:unsigned-short . :unsigned-short)
201 (:pointer-void . :pointer)
204 (:unsigned-char . :unsigned-byte)
205 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
206 (:int . :long) (:unsigned-int . :unsigned-long)
207 (:long . :long) (:unsigned-long . :unsigned-long)
208 (:float . :single-float) (:double . :double-float)
212 (setq *type-conversion-list*
213 '((* . :address) (:void . :void)
214 (:short . :short) (:unsigned-short . :unsigned-short)
215 (:pointer-void . :address)
216 (:cstring . :address)
217 (:char . :signed-char)
218 (:unsigned-char . :unsigned-char)
219 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
220 (:int . :int) (:unsigned-int . :unsigned-int)
221 (:long . :long) (:unsigned-long . :unsigned-long)
222 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
223 (:float . :single-float) (:double . :double-float)
226 (dolist (type *type-conversion-list*)
227 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
230 (dolist (type *cmu-sbcl-def-type-list*)
231 (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
233 (defun basic-convert-from-uffi-type (type)
234 (let ((found-type (gethash type +type-conversion-hash+)))
237 #-(or openmcl digitool) type
238 #+(or openmcl digitool) (keyword type))))
240 (defun %convert-from-uffi-type (type context)
241 "Converts from a uffi type to an implementation specific type"
244 #+(or allegro cormanlisp)
245 ((and (or (eq context :routine) (eq context :return))
247 (setq type '((* :char) integer)))
250 (let ((cmu-type (gethash type *cmu-def-type-hash*)))
253 (basic-convert-from-uffi-type type))))
255 ((and (eq context :return)
257 (basic-convert-from-uffi-type :cstring-returning))
259 ((and (eq type :void) (eq context :return)) nil)
261 (basic-convert-from-uffi-type type)))
262 (let ((sub-type (car type)))
265 (convert-from-uffi-type (cadr type) context))
267 #+(or openmcl digitool) `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
268 #-(or openmcl digitool) (%convert-from-uffi-type (list '* (cadr type)) :struct)
271 #+(or openmcl digitool) `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
272 #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :struct)
275 #+(or openmcl digitool) `(:union ,(%convert-from-uffi-type (cadr type) :union))
276 #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :union)
279 (cons (%convert-from-uffi-type (first type) context)
280 (%convert-from-uffi-type (rest type) context)))))))
282 (defun convert-from-uffi-type (type context)
283 (let ((result (%convert-from-uffi-type type context)))
285 ((atom result) result)
287 ((eq (car result) :address)
288 (if (eq context :struct)
289 (append '(:*) (cdr result))
292 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
295 (eval-when (:compile-toplevel :load-toplevel :execute)
296 (when (char= #\a (schar (symbol-name '#:a) 0))
297 (pushnew :uffi-lowercase-reader *features*))
298 (when (not (string= (symbol-name '#:a)
300 (pushnew :uffi-case-sensitive *features*)))
302 (defun make-lisp-name (name)
303 (let ((converted (substitute #\- #\_ name)))
305 #+uffi-case-sensitive converted
306 #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
307 #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
309 (eval-when (:compile-toplevel :load-toplevel :execute)
310 (setq cl:*features* (delete :uffi-lowercase-reader *features*))
311 (setq cl:*features* (delete :uffi-case-sensitive *features*)))