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.10 2003/08/14 21:40:13 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 :test #'eq))
86 #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
87 (make-hash-table :size 20 :test #'eq))
88 #+allegro (defvar *allegro-foreign-type-hash*
89 (make-hash-table :size 20 :test #'eq))
93 (defvar *cmu-sbcl-def-type-list* nil)
96 (defvar *cmu-sbcl-def-type-list*
97 '((:char . (alien:signed 8))
98 (:unsigned-char . (alien:unsigned 8))
99 (:byte . (alien:signed 8))
100 (:unsigned-byte . (alien:unsigned 8))
101 (:short . (alien:signed 16))
102 (:unsigned-short . (alien:unsigned 16))
103 (:int . (alien:signed 32))
104 (:unsigned-int . (alien:unsigned 32))
105 (:long . (alien:signed 32))
106 (:unsigned-long . (alien:unsigned 32))
107 (:float . alien:single-float)
108 (:double . alien:double-float)
110 "Conversions in CMUCL for def-foreign-type are different than in def-function")
112 (defvar *cmu-sbcl-def-type-list*
113 '((:char . (sb-alien:signed 8))
114 (:unsigned-char . (sb-alien:unsigned 8))
115 (:byte . (sb-alien:signed 8))
116 (:unsigned-byte . (sb-alien:unsigned 8))
117 (:short . (sb-alien:signed 16))
118 (:unsigned-short . (sb-alien:unsigned 16))
119 (:int . (sb-alien:signed 32))
120 (:unsigned-int . (sb-alien:unsigned 32))
121 (:long . (sb-alien:signed 32))
122 (:unsigned-long . (sb-alien:unsigned 32))
123 (:float . sb-alien:single-float)
124 (: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 (:short . c-call:short)
134 (:pointer-void . (* t))
135 (:cstring . c-call:c-string)
136 (:char . c-call:char)
137 (:unsigned-char . (alien:unsigned 8))
138 (:byte . (alien:signed 8))
139 (:unsigned-byte . (alien:unsigned 8))
140 (:short . c-call:unsigned-short)
141 (:unsigned-short . c-call:unsigned-short)
142 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
143 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
144 (:float . c-call:float) (:double . c-call:double)
145 (:array . alien:array)))
148 (setq *type-conversion-list*
149 '((* . *) (:void . sb-alien:void)
150 (:short . sb-alien:short)
151 (:pointer-void . (* t))
152 (:cstring . sb-alien:c-string)
153 (:char . sb-alien:char)
154 (:unsigned-char . (sb-alien:unsigned 8))
155 (:byte . (sb-alien:signed 8))
156 (:unsigned-byte . (sb-alien:unsigned 8))
157 (:short . sb-alien:unsigned-short)
158 (:unsigned-short . sb-alien:unsigned-short)
159 (:int . sb-alien:integer) (:unsigned-int . sb-alien:unsigned-int)
160 (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
161 (:float . sb-alien:float) (:double . sb-alien:double)
162 (:array . sb-alien:array)))
164 #+(or allegro cormanlisp)
165 (setq *type-conversion-list*
166 '((* . *) (:void . :void)
168 (:pointer-void . (* :void))
169 (:cstring . (* :unsigned-char))
171 (:unsigned-byte . :unsigned-char)
173 (:unsigned-char . :unsigned-char)
174 (:int . :int) (:unsigned-int . :unsigned-int)
175 (:long . :long) (:unsigned-long . :unsigned-long)
176 (:float . :float) (:double . :double)
180 (setq *type-conversion-list*
181 '((* . :pointer) (:void . :void)
183 (:pointer-void . (:pointer :void))
184 (:cstring . (:reference-pass (:ef-mb-string :external-format
185 (:latin-1 :eol-style :lf))
187 (:cstring-returning . (:reference (:ef-mb-string :external-format
188 (:latin-1 :eol-style :lf))
191 (:unsigned-byte . (:unsigned :byte))
193 (:unsigned-char . (:unsigned :char))
194 (:int . :int) (:unsigned-int . (:unsigned :int))
195 (:long . :long) (:unsigned-long . (:unsigned :long))
196 (:float . :float) (:double . :double)
197 (:array . :c-array)))
199 #+(and mcl (not openmcl))
200 (setq *type-conversion-list*
201 '((* . :pointer) (:void . :void)
202 (:short . :short) (:unsigned-short . :unsigned-short)
203 (:pointer-void . :pointer)
206 (:unsigned-char . :unsigned-byte)
207 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
208 (:int . :long) (:unsigned-int . :unsigned-long)
209 (:long . :long) (:unsigned-long . :unsigned-long)
210 (:float . :single-float) (:double . :double-float)
214 (setq *type-conversion-list*
215 '((* . :address) (:void . :void)
216 (:short . :short) (:unsigned-short . :unsigned-short)
217 (:pointer-void . :address)
218 (:cstring . :address)
219 (:char . :signed-char)
220 (:unsigned-char . :unsigned-char)
221 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
222 (:int . :int) (:unsigned-int . :unsigned-int)
223 (:long . :long) (:unsigned-long . :unsigned-long)
224 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
225 (:float . :single-float) (:double . :double-float)
229 (defvar *allegro-foreign-type-list*
230 '((:char . :signed-byte)
231 (:unsigned-char . :unsigned-byte)
232 (:byte . :signed-byte)
233 (:unsigned-byte . :unsigned-byte)
234 (:short . :signed-word)
235 (:unsigned-short . :unsigned-word)
236 (:int . :signed-long)
237 (:unsigned-int . :unsigned-long32)
238 (:long . :signed-long)
239 (:unsigned-long . :unsigned-long)
240 (:float . :single-float)
241 (:double . :double-float)
243 "Conversion for Allegro's system:memref function")
245 (dolist (type *type-conversion-list*)
246 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
249 (dolist (type *cmu-sbcl-def-type-list*)
250 (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
253 (dolist (type *allegro-foreign-type-list*)
254 (setf (gethash (car type) *allegro-foreign-type-hash*) (cdr type)))
256 (defun foreign-var-type-convert (type)
257 #+allegro (gethash type *allegro-foreign-type-hash*))
260 (defun basic-convert-from-uffi-type (type)
261 (let ((found-type (gethash type +type-conversion-hash+)))
265 #+mcl (keyword type))))
267 (defun %convert-from-uffi-type (type context)
268 "Converts from a uffi type to an implementation specific type"
271 #+(or allegro cormanlisp)
272 ((and (or (eq context :routine) (eq context :return))
274 (setq type '((* :char) integer)))
277 (let ((cmu-type (gethash type *cmu-def-type-hash*)))
280 (basic-convert-from-uffi-type type))))
282 ((and (eq context :return)
284 (basic-convert-from-uffi-type :cstring-returning))
285 #+(and mcl (not openmcl))
286 ((and (eq type :void) (eq context :return)) nil)
288 ((eq context :foreign-var)
289 (foreign-var-type-convert type))
291 (basic-convert-from-uffi-type type)))
292 (let ((sub-type (car type)))
295 (convert-from-uffi-type (cadr type) context))
297 #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
298 #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
301 #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
302 #-mcl (%convert-from-uffi-type (cadr type) :struct)
305 (cons (%convert-from-uffi-type (first type) context)
306 (%convert-from-uffi-type (rest type) context)))))))
308 (defun convert-from-uffi-type (type context)
309 (let ((result (%convert-from-uffi-type type context)))
311 ((atom result) result)
313 ((eq (car result) :address)
314 (if (eq context :struct)
315 (append '(:*) (cdr result))
317 #+(and mcl (not openmcl))
318 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))