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 by Kevin M. Rosenberg
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
22 (defvar *keyword-package* (find-package "KEYWORD"))
25 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
26 ; So this provides a function to convert any quoted symbols to keywords.
33 (intern (symbol-name obj) *keyword-package*))
34 ((and (listp obj) (eq (car obj) 'cl:quote))
37 (intern obj *keyword-package*))
41 ; Wrapper for unexported function we have to use
42 #+(and mcl (not openmcl))
43 (defmacro def-mcl-type (name type)
44 `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
46 (defmacro def-constant (name value &key (export nil))
47 "Macro to define a constant and to export it"
48 `(eval-when (:compile-toplevel :load-toplevel :execute)
49 (defconstant ,name ,value)
50 ,(when export (list 'export `(quote ,name)))
53 (defmacro def-type (name type)
54 "Generates a (deftype) statement for CL. Currently, only CMUCL
55 supports takes advantage of this optimization."
56 #+(or lispworks allegro mcl cormanlisp) (declare (ignore type))
57 #+(or lispworks allegro mcl cormanlisp) `(deftype ,name () t)
59 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
61 `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
64 (defmacro null-char-p (val)
65 "Returns T if character is NULL"
68 (defmacro def-foreign-type (name type)
69 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
70 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
71 #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
72 #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
73 #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
75 (let ((mcl-type (convert-from-uffi-type type :type)))
76 (unless (or (keywordp mcl-type) (consp mcl-type))
77 (setf mcl-type `(quote ,mcl-type)))
78 #+(and mcl (not openmcl))
79 `(def-mcl-type ,(keyword name) ,mcl-type)
81 `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
84 (eval-when (:compile-toplevel :load-toplevel :execute)
85 (defvar +type-conversion-hash+ (make-hash-table :size 20 :test #'eq))
86 #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
87 (make-hash-table :size 20 :test #'eq))
91 (defvar *cmu-sbcl-def-type-list*
92 '((:char . (alien:signed 8))
93 (:unsigned-char . (alien:unsigned 8))
94 (:byte . (alien:signed 8))
95 (:unsigned-byte . (alien:unsigned 8))
96 (:short . (alien:signed 16))
97 (:unsigned-short . (alien:unsigned 16))
98 (:int . (alien:signed 32))
99 (:unsigned-int . (alien:unsigned 32))
100 (:long . (alien:signed 32))
101 (:unsigned-long . (alien:unsigned 32))
102 (:float . alien:single-float)
103 (:double . alien:double-float)
106 "Conversions in CMUCL for def-foreign-type are different than in def-function")
109 (defvar *cmu-sbcl-def-type-list*
110 '((:char . (sb-alien:signed 8))
111 (:unsigned-char . (sb-alien:unsigned 8))
112 (:byte . (sb-alien:signed 8))
113 (:unsigned-byte . (sb-alien:unsigned 8))
114 (:short . (sb-alien:signed 16))
115 (:unsigned-short . (sb-alien:unsigned 16))
116 (:int . (sb-alien:signed 32))
117 (:unsigned-int . (sb-alien:unsigned 32))
118 #-x86-64 (:long . (sb-alien:signed 32))
119 #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
120 #+x86-64 (:long . (sb-alien:signed 64))
121 #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
122 (:float . sb-alien:single-float)
123 (:double . sb-alien:double-float)
126 "Conversions in SBCL for def-foreign-type are different than in def-function")
128 (defvar *type-conversion-list* nil)
131 (setq *type-conversion-list*
132 '((* . *) (:void . c-call:void)
133 (:pointer-void . (* t))
134 (:cstring . c-call:c-string)
135 (:char . c-call:char)
136 (:unsigned-char . (alien:unsigned 8))
137 (:byte . (alien:signed 8))
138 (:unsigned-byte . (alien:unsigned 8))
139 (:short . c-call:short)
140 (:unsigned-short . c-call:unsigned-short)
141 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
142 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
143 (:float . c-call:float) (:double . c-call:double)
144 (:array . alien:array)))
147 (setq *type-conversion-list*
148 '((* . *) (:void . sb-alien:void)
149 (:pointer-void . (* t))
150 #-sb-unicode(:cstring . sb-alien:c-string)
151 #+sb-unicode(:cstring . sb-alien:utf8-string)
152 (:char . sb-alien:char)
153 (:unsigned-char . (sb-alien:unsigned 8))
154 (:byte . (sb-alien:signed 8))
155 (:unsigned-byte . (sb-alien:unsigned 8))
156 (:short . sb-alien:short)
157 (:unsigned-short . sb-alien:unsigned-short)
158 (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int)
159 (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
160 (:float . sb-alien:float) (:double . sb-alien:double)
161 (:array . sb-alien:array)))
163 #+(or allegro cormanlisp)
164 (setq *type-conversion-list*
165 '((* . *) (:void . :void)
167 (:pointer-void . (* :void))
168 (:cstring . (* :unsigned-char))
170 (:unsigned-byte . :unsigned-char)
172 (:unsigned-char . :unsigned-char)
173 (:int . :int) (:unsigned-int . :unsigned-int)
174 (:long . :long) (:unsigned-long . :unsigned-long)
175 (:float . :float) (:double . :double)
179 (setq *type-conversion-list*
180 '((* . :pointer) (:void . :void)
182 (:pointer-void . (:pointer :void))
183 (:cstring . (:reference-pass (:ef-mb-string :external-format
184 (:latin-1 :eol-style :lf))
186 (:cstring-returning . (:reference (:ef-mb-string :external-format
187 (:latin-1 :eol-style :lf))
190 (:unsigned-byte . (:unsigned :byte))
192 (:unsigned-char . (:unsigned :char))
193 (:int . :int) (:unsigned-int . (:unsigned :int))
194 (:long . :long) (:unsigned-long . (:unsigned :long))
195 (:float . :float) (:double . :double)
196 (:array . :c-array)))
198 #+(and mcl (not openmcl))
199 (setq *type-conversion-list*
200 '((* . :pointer) (:void . :void)
201 (:short . :short) (:unsigned-short . :unsigned-short)
202 (:pointer-void . :pointer)
205 (:unsigned-char . :unsigned-byte)
206 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
207 (:int . :long) (:unsigned-int . :unsigned-long)
208 (:long . :long) (:unsigned-long . :unsigned-long)
209 (:float . :single-float) (:double . :double-float)
213 (setq *type-conversion-list*
214 '((* . :address) (:void . :void)
215 (:short . :short) (:unsigned-short . :unsigned-short)
216 (:pointer-void . :address)
217 (:cstring . :address)
218 (:char . :signed-char)
219 (:unsigned-char . :unsigned-char)
220 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
221 (:int . :int) (:unsigned-int . :unsigned-int)
222 (:long . :long) (:unsigned-long . :unsigned-long)
223 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
224 (:float . :single-float) (:double . :double-float)
227 (dolist (type *type-conversion-list*)
228 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
231 (dolist (type *cmu-sbcl-def-type-list*)
232 (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
234 (defun basic-convert-from-uffi-type (type)
235 (let ((found-type (gethash type +type-conversion-hash+)))
239 #+mcl (keyword type))))
241 (defun %convert-from-uffi-type (type context)
242 "Converts from a uffi type to an implementation specific type"
245 #+(or allegro cormanlisp)
246 ((and (or (eq context :routine) (eq context :return))
248 (setq type '((* :char) integer)))
251 (let ((cmu-type (gethash type *cmu-def-type-hash*)))
254 (basic-convert-from-uffi-type type))))
256 ((and (eq context :return)
258 (basic-convert-from-uffi-type :cstring-returning))
259 #+(and mcl (not openmcl))
260 ((and (eq type :void) (eq context :return)) nil)
262 (basic-convert-from-uffi-type type)))
263 (let ((sub-type (car type)))
266 (convert-from-uffi-type (cadr type) context))
268 #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
269 #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
272 #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
273 #-mcl (%convert-from-uffi-type (cadr type) :struct)
276 (cons (%convert-from-uffi-type (first type) context)
277 (%convert-from-uffi-type (rest type) context)))))))
279 (defun convert-from-uffi-type (type context)
280 (let ((result (%convert-from-uffi-type type context)))
282 ((atom result) result)
284 ((eq (car result) :address)
285 (if (eq context :struct)
286 (append '(:*) (cdr result))
288 #+(and mcl (not openmcl))
289 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
292 (eval-when (:compile-toplevel :load-toplevel :execute)
293 (when (char= #\a (schar (symbol-name '#:a) 0))
294 (pushnew :uffi-lowercase-reader *features*))
295 (when (not (string= (symbol-name '#:a)
297 (pushnew :uffi-case-sensitive *features*)))
299 (defun make-lisp-name (name)
300 (let ((converted (substitute #\- #\_ name)))
302 #+uffi-case-sensitive converted
303 #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
304 #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
306 (eval-when (:compile-toplevel :load-toplevel :execute)
307 (setq cl:*features* (delete :uffi-lowercase-reader *features*))
308 (setq cl:*features* (delete :uffi-case-sensitive *features*)))