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.8 2002/12/15 17:11:08 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 #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
73 #+sbcl `(sb-alien:define-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 scl) (defvar *cmu-def-type-hash* (make-hash-table :size 20))
91 (defparameter *cmu-sbcl-def-type-list* nil)
94 (defparameter *cmu-sbcl-def-type-list*
95 '((:char . (alien:signed 8))
96 (:unsigned-char . (alien:unsigned 8))
97 (:byte . (alien:signed 8))
98 (:unsigned-byte . (alien:unsigned 8))
99 (:short . (alien:signed 16))
100 (:unsigned-short . (alien:unsigned 16))
101 (:int . (alien:signed 32))
102 (:unsigned-int . (alien:unsigned 32))
103 (:long . (alien:signed 32))
104 (:unsigned-long . (alien:unsigned 32))
105 (:float . alien:single-float)
106 (:double . alien:double-float)
108 "Conversions in CMUCL for def-foreign-type are different than in def-function")
110 (defparameter *cmu-sbcl-def-type-list*
111 '((:char . (sb-alien:signed 8))
112 (:unsigned-char . (sb-alien:unsigned 8))
113 (:byte . (sb-alien:signed 8))
114 (:unsigned-byte . (sb-alien:unsigned 8))
115 (:short . (sb-alien:signed 16))
116 (:unsigned-short . (sb-alien:unsigned 16))
117 (:int . (sb-alien:signed 32))
118 (:unsigned-int . (sb-alien:unsigned 32))
119 (:long . (sb-alien:signed 32))
120 (:unsigned-long . (sb-alien:unsigned 32))
121 (:float . sb-alien:single-float)
122 (:double . sb-alien:double-float)
124 "Conversions in SBCL for def-foreign-type are different than in def-function")
126 (defparameter *type-conversion-list* nil)
129 (setq *type-conversion-list*
130 '((* . *) (:void . c-call:void)
131 (:short . c-call:short)
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:unsigned-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 (:short . sb-alien:short)
149 (:pointer-void . (* t))
150 (:cstring . sb-alien:c-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:unsigned-short)
156 (:unsigned-short . sb-alien:unsigned-short)
157 (:int . sb-alien:integer) (: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)))
197 #+(and mcl (not openmcl))
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+)))
238 #+mcl (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))
258 #+(and mcl (not openmcl))
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 #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
268 #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
271 #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
272 #-mcl (%convert-from-uffi-type (cadr type) :struct)
275 (cons (%convert-from-uffi-type (first type) context)
276 (%convert-from-uffi-type (rest type) context)))))))
278 (defun convert-from-uffi-type (type context)
279 (let ((result (%convert-from-uffi-type type context)))
281 ((atom result) result)
283 ((eq (car result) :address)
284 (if (eq context :struct)
285 (append '(:*) (cdr result))
287 #+(and mcl (not openmcl))
288 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))