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 ;;;; $Id: primitives.lisp,v 1.9 2003/06/06 21:59:18 kevin Exp $
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))
86 #+(or cmu sbcl scl) (defvar *cmu-def-type-hash* (make-hash-table :size 20))
90 (defparameter *cmu-sbcl-def-type-list* nil)
93 (defparameter *cmu-sbcl-def-type-list*
94 '((:char . (alien:signed 8))
95 (:unsigned-char . (alien:unsigned 8))
96 (:byte . (alien:signed 8))
97 (:unsigned-byte . (alien:unsigned 8))
98 (:short . (alien:signed 16))
99 (:unsigned-short . (alien:unsigned 16))
100 (:int . (alien:signed 32))
101 (:unsigned-int . (alien:unsigned 32))
102 (:long . (alien:signed 32))
103 (:unsigned-long . (alien:unsigned 32))
104 (:float . alien:single-float)
105 (:double . alien:double-float)
107 "Conversions in CMUCL for def-foreign-type are different than in def-function")
109 (defparameter *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 (:long . (sb-alien:signed 32))
119 (:unsigned-long . (sb-alien:unsigned 32))
120 (:float . sb-alien:single-float)
121 (:double . sb-alien:double-float)
123 "Conversions in SBCL for def-foreign-type are different than in def-function")
125 (defparameter *type-conversion-list* nil)
128 (setq *type-conversion-list*
129 '((* . *) (:void . c-call:void)
130 (:short . c-call:short)
131 (:pointer-void . (* t))
132 (:cstring . c-call:c-string)
133 (:char . c-call:char)
134 (:unsigned-char . (alien:unsigned 8))
135 (:byte . (alien:signed 8))
136 (:unsigned-byte . (alien:unsigned 8))
137 (:short . c-call:unsigned-short)
138 (:unsigned-short . c-call:unsigned-short)
139 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
140 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
141 (:float . c-call:float) (:double . c-call:double)
142 (:array . alien:array)))
145 (setq *type-conversion-list*
146 '((* . *) (:void . sb-alien:void)
147 (:short . sb-alien:short)
148 (:pointer-void . (* t))
149 (:cstring . sb-alien:c-string)
150 (:char . sb-alien:char)
151 (:unsigned-char . (sb-alien:unsigned 8))
152 (:byte . (sb-alien:signed 8))
153 (:unsigned-byte . (sb-alien:unsigned 8))
154 (:short . sb-alien:unsigned-short)
155 (:unsigned-short . sb-alien:unsigned-short)
156 (:int . sb-alien:integer) (:unsigned-int . sb-alien:unsigned-int)
157 (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
158 (:float . sb-alien:float) (:double . sb-alien:double)
159 (:array . sb-alien:array)))
161 #+(or allegro cormanlisp)
162 (setq *type-conversion-list*
163 '((* . *) (:void . :void)
165 (:pointer-void . (* :void))
166 (:cstring . (* :unsigned-char))
168 (:unsigned-byte . :unsigned-char)
170 (:unsigned-char . :unsigned-char)
171 (:int . :int) (:unsigned-int . :unsigned-int)
172 (:long . :long) (:unsigned-long . :unsigned-long)
173 (:float . :float) (:double . :double)
177 (setq *type-conversion-list*
178 '((* . :pointer) (:void . :void)
180 (:pointer-void . (:pointer :void))
181 (:cstring . (:reference-pass (:ef-mb-string :external-format
182 (:latin-1 :eol-style :lf))
184 (:cstring-returning . (:reference (:ef-mb-string :external-format
185 (:latin-1 :eol-style :lf))
188 (:unsigned-byte . (:unsigned :byte))
190 (:unsigned-char . (:unsigned :char))
191 (:int . :int) (:unsigned-int . (:unsigned :int))
192 (:long . :long) (:unsigned-long . (:unsigned :long))
193 (:float . :float) (:double . :double)
194 (:array . :c-array)))
196 #+(and mcl (not openmcl))
197 (setq *type-conversion-list*
198 '((* . :pointer) (:void . :void)
199 (:short . :short) (:unsigned-short . :unsigned-short)
200 (:pointer-void . :pointer)
203 (:unsigned-char . :unsigned-byte)
204 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
205 (:int . :long) (:unsigned-int . :unsigned-long)
206 (:long . :long) (:unsigned-long . :unsigned-long)
207 (:float . :single-float) (:double . :double-float)
211 (setq *type-conversion-list*
212 '((* . :address) (:void . :void)
213 (:short . :short) (:unsigned-short . :unsigned-short)
214 (:pointer-void . :address)
215 (:cstring . :address)
216 (:char . :signed-char)
217 (:unsigned-char . :unsigned-char)
218 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
219 (:int . :int) (:unsigned-int . :unsigned-int)
220 (:long . :long) (:unsigned-long . :unsigned-long)
221 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
222 (:float . :single-float) (:double . :double-float)
225 (dolist (type *type-conversion-list*)
226 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
229 (dolist (type *cmu-sbcl-def-type-list*)
230 (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
232 (defun basic-convert-from-uffi-type (type)
233 (let ((found-type (gethash type +type-conversion-hash+)))
237 #+mcl (keyword type))))
239 (defun %convert-from-uffi-type (type context)
240 "Converts from a uffi type to an implementation specific type"
243 #+(or allegro cormanlisp)
244 ((and (or (eq context :routine) (eq context :return))
246 (setq type '((* :char) integer)))
249 (let ((cmu-type (gethash type *cmu-def-type-hash*)))
252 (basic-convert-from-uffi-type type))))
254 ((and (eq context :return)
256 (basic-convert-from-uffi-type :cstring-returning))
257 #+(and mcl (not openmcl))
258 ((and (eq type :void) (eq context :return)) nil)
260 (basic-convert-from-uffi-type type)))
261 (let ((sub-type (car type)))
264 (convert-from-uffi-type (cadr type) context))
266 #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
267 #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
270 #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
271 #-mcl (%convert-from-uffi-type (cadr type) :struct)
274 (cons (%convert-from-uffi-type (first type) context)
275 (%convert-from-uffi-type (rest type) context)))))))
277 (defun convert-from-uffi-type (type context)
278 (let ((result (%convert-from-uffi-type type context)))
280 ((atom result) result)
282 ((eq (car result) :address)
283 (if (eq context :struct)
284 (append '(:*) (cdr result))
286 #+(and mcl (not openmcl))
287 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))