1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: primitives.cl
6 ;;;; Purpose: UFFI source to handle immediate types
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: primitives.lisp,v 1.2 2002/10/01 17:05:29 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 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
23 (defvar *keyword-package* (find-package "KEYWORD"))
26 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
27 ; So this provides a function to convert any quoted symbols to keywords.
34 (intern (symbol-name obj) *keyword-package*))
35 ((and (listp obj) (eq (car obj) 'cl:quote))
38 (intern obj *keyword-package*))
42 ; Wrapper for unexported function we have to use
43 #+(and mcl (not openmcl))
44 (defmacro def-mcl-type (name type)
45 `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
47 (defmacro def-constant (name value &key (export nil))
48 "Macro to define a constant and to export it"
49 `(eval-when (:compile-toplevel :load-toplevel :execute)
50 (defconstant ,name ,value)
51 ,(when export (list 'export `(quote ,name)))
54 (defmacro def-type (name type)
55 "Generates a (deftype) statement for CL. Currently, only CMUCL
56 supports takes advantage of this optimization."
57 #+(or lispworks allegro mcl cormanlisp) (declare (ignore type))
58 #+(or lispworks allegro mcl cormanlisp) `(deftype ,name () t)
60 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
62 `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
65 (defmacro null-char-p (val)
66 "Returns T if character is NULL"
69 (defmacro def-foreign-type (name type)
70 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
71 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
72 #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
73 #+sbcl `(sb-alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
74 #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
76 (let ((mcl-type (convert-from-uffi-type type :type)))
77 (unless (or (keywordp mcl-type) (consp mcl-type))
78 (setf mcl-type `(quote ,mcl-type)))
79 #+(and mcl (not openmcl))
80 `(def-mcl-type ,(keyword name) ,mcl-type)
82 `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
85 (eval-when (:compile-toplevel :load-toplevel :execute)
86 (defvar +type-conversion-hash+ (make-hash-table :size 20))
87 #+(or cmu sbcl) (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
91 (defconstant +cmu-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)
105 "Conversions in CMUCL for def-foreign-type are different than in def-function")
107 (defconstant +cmu-def-type-list+
108 '((:char . (sb-alien:signed 8))
109 (:unsigned-char . (sb-alien:unsigned 8))
110 (:byte . (sb-alien:signed 8))
111 (:unsigned-byte . (sb-alien:unsigned 8))
112 (:short . (sb-alien:signed 16))
113 (:unsigned-short . (sb-alien:unsigned 16))
114 (:int . (sb-alien:signed 32))
115 (:unsigned-int . (sb-alien:unsigned 32))
116 (:long . (sb-alien:signed 32))
117 (:unsigned-long . (sb-alien:unsigned 32))
118 (:float . sb-alien:single-float)
119 (:double . sb-alien:double-float)
121 "Conversions in SBCL for def-foreign-type are different than in def-function")
123 (defparameter +type-conversion-list+ nil)
126 (setq +type-conversion-list+
127 '((* . *) (:void . c-call:void)
128 (:short . c-call:short)
129 (:pointer-void . (* t))
130 (:cstring . c-call:c-string)
131 (:char . c-call:char)
132 (:unsigned-char . (alien:unsigned 8))
133 (:byte . (alien:signed 8))
134 (:unsigned-byte . (alien:unsigned 8))
135 (:short . c-call:unsigned-short)
136 (:unsigned-short . c-call:unsigned-short)
137 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
138 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
139 (:float . c-call:float) (:double . c-call:double)
140 (:array . alien:array)))
143 (setq +type-conversion-list+
144 '((* . *) (:void . void)
146 (:pointer-void . (* t))
147 (:cstring . c-string)
149 (:unsigned-char . (sb-alien:unsigned 8))
150 (:byte . (sb-alien:signed 8))
151 (:unsigned-byte . (sb-alien:unsigned 8))
152 (:short . unsigned-short)
153 (:unsigned-short . unsigned-short)
154 (:int . integer) (:unsigned-int . unsigned-int)
155 (:long . long) (:unsigned-long . unsigned-long)
156 (:float . float) (:double . double)
159 #+(or allegro cormanlisp)
160 (setq +type-conversion-list+
161 '((* . *) (:void . :void)
163 (:pointer-void . (* :void))
164 (:cstring . (* :unsigned-char))
166 (:unsigned-byte . :unsigned-byte)
168 (:unsigned-char . :unsigned-char)
169 (:int . :int) (:unsigned-int . :unsigned-int)
170 (:long . :long) (:unsigned-long . :unsigned-long)
171 (:float . :float) (:double . :double)
175 (setq +type-conversion-list+
176 '((* . :pointer) (:void . :void)
178 (:pointer-void . (:pointer :void))
179 (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
181 (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
183 (:unsigned-byte . (:unsigned :byte))
185 (:unsigned-char . (:unsigned :char))
186 (:int . :int) (:unsigned-int . (:unsigned :int))
187 (:long . :long) (:unsigned-long . (:unsigned :long))
188 (:float . :float) (:double . :double)
189 (:array . :c-array)))
191 #+(and mcl (not openmcl))
192 (setq +type-conversion-list+
193 '((* . :pointer) (:void . :void)
194 (:short . :short) (:unsigned-short . :unsigned-short)
195 (:pointer-void . :pointer)
198 (:unsigned-char . :unsigned-byte)
199 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
200 (:int . :long) (:unsigned-int . :unsigned-long)
201 (:long . :long) (:unsigned-long . :unsigned-long)
202 (:float . :single-float) (:double . :double-float)
206 (setq +type-conversion-list+
207 '((* . :address) (:void . :void)
208 (:short . :short) (:unsigned-short . :unsigned-short)
209 (:pointer-void . :address)
210 (:cstring . :address)
211 (:char . :signed-char)
212 (:unsigned-char . :unsigned-char)
213 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
214 (:int . :int) (:unsigned-int . :unsigned-int)
215 (:long . :long) (:unsigned-long . :unsigned-long)
216 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
217 (:float . :single-float) (:double . :double-float)
220 (dolist (type +type-conversion-list+)
221 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
224 (dolist (type +cmu-def-type-list+)
225 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
227 (defun basic-convert-from-uffi-type (type)
228 (let ((found-type (gethash type +type-conversion-hash+)))
232 #+mcl (keyword type))))
234 (defun %convert-from-uffi-type (type context)
235 "Converts from a uffi type to an implementation specific type"
238 #+(or allegro cormanlisp)
239 ((and (or (eq context :routine) (eq context :return))
241 (setq type '((* :char) integer)))
244 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
247 (basic-convert-from-uffi-type type))))
249 ((and (eq context :return)
251 (basic-convert-from-uffi-type :cstring-returning))
252 #+(and mcl (not openmcl))
253 ((and (eq type :void) (eq context :return)) nil)
255 (basic-convert-from-uffi-type type)))
256 (let ((sub-type (car type)))
259 (convert-from-uffi-type (cadr type) context))
261 #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
262 #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
265 #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
266 #-mcl (%convert-from-uffi-type (cadr type) :struct)
269 (cons (%convert-from-uffi-type (first type) context)
270 (%convert-from-uffi-type (rest type) context)))))))
272 (defun convert-from-uffi-type (type context)
273 (let ((result (%convert-from-uffi-type type context)))
275 ((atom result) result)
277 ((eq (car result) :address)
278 (if (eq context :struct)
279 (append '(:*) (cdr result))
281 #+(and mcl (not openmcl))
282 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))