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.1 2002/09/30 10:02:36 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)
58 (declare (ignore type))
59 #+(or lispworks allegro mcl)
62 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
64 `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
67 (defmacro null-char-p (val)
68 "Returns T if character is NULL"
71 (defmacro def-foreign-type (name type)
72 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
73 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
74 #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
75 #+sbcl `(sb-alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
77 (let ((mcl-type (convert-from-uffi-type type :type)))
78 (unless (or (keywordp mcl-type) (consp mcl-type))
79 (setf mcl-type `(quote ,mcl-type)))
80 #+(and mcl (not openmcl))
81 `(def-mcl-type ,(keyword name) ,mcl-type)
83 `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
86 (eval-when (:compile-toplevel :load-toplevel :execute)
87 (defvar +type-conversion-hash+ (make-hash-table :size 20))
88 #+(or cmu sbcl) (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
92 (defconstant +cmu-def-type-list+
93 '((:char . (alien:signed 8))
94 (:unsigned-char . (alien:unsigned 8))
95 (:byte . (alien:signed 8))
96 (:unsigned-byte . (alien:unsigned 8))
97 (:short . (alien:signed 16))
98 (:unsigned-short . (alien:unsigned 16))
99 (:int . (alien:signed 32))
100 (:unsigned-int . (alien:unsigned 32))
101 (:long . (alien:signed 32))
102 (:unsigned-long . (alien:unsigned 32))
103 (:float . alien:single-float)
104 (:double . alien:double-float)
106 "Conversions in CMUCL for def-foreign-type are different than in def-function")
108 (defconstant +cmu-def-type-list+
109 '((:char . (sb-alien:signed 8))
110 (:unsigned-char . (sb-alien:unsigned 8))
111 (:byte . (sb-alien:signed 8))
112 (:unsigned-byte . (sb-alien:unsigned 8))
113 (:short . (sb-alien:signed 16))
114 (:unsigned-short . (sb-alien:unsigned 16))
115 (:int . (sb-alien:signed 32))
116 (:unsigned-int . (sb-alien:unsigned 32))
117 (:long . (sb-alien:signed 32))
118 (:unsigned-long . (sb-alien:unsigned 32))
119 (:float . sb-alien:single-float)
120 (:double . sb-alien:double-float)
122 "Conversions in SBCL for def-foreign-type are different than in def-function")
124 (defparameter +type-conversion-list+ nil)
127 (setq +type-conversion-list+
128 '((* . *) (:void . c-call:void)
129 (:short . c-call:short)
130 (:pointer-void . (* t))
131 (:cstring . c-call:c-string)
132 (:char . c-call:char)
133 (:unsigned-char . (alien:unsigned 8))
134 (:byte . (alien:signed 8))
135 (:unsigned-byte . (alien:unsigned 8))
136 (:short . c-call:unsigned-short)
137 (:unsigned-short . c-call:unsigned-short)
138 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
139 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
140 (:float . c-call:float) (:double . c-call:double)
141 (:array . alien:array)))
144 (setq +type-conversion-list+
145 '((* . *) (:void . void)
147 (:pointer-void . (* t))
148 (:cstring . c-string)
150 (:unsigned-char . (sb-alien:unsigned 8))
151 (:byte . (sb-alien:signed 8))
152 (:unsigned-byte . (sb-alien:unsigned 8))
153 (:short . unsigned-short)
154 (:unsigned-short . unsigned-short)
155 (:int . integer) (:unsigned-int . unsigned-int)
156 (:long . long) (:unsigned-long . unsigned-long)
157 (:float . float) (:double . double)
161 (setq +type-conversion-list+
162 '((* . *) (:void . :void)
164 (:pointer-void . (* :void))
165 (:cstring . (* :unsigned-char))
167 (:unsigned-byte . :unsigned-byte)
169 (:unsigned-char . :unsigned-char)
170 (:int . :int) (:unsigned-int . :unsigned-int)
171 (:long . :long) (:unsigned-long . :unsigned-long)
172 (:float . :float) (:double . :double)
176 (setq +type-conversion-list+
177 '((* . :pointer) (:void . :void)
179 (:pointer-void . (:pointer :void))
180 (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
182 (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
184 (:unsigned-byte . (:unsigned :byte))
186 (:unsigned-char . (:unsigned :char))
187 (:int . :int) (:unsigned-int . (:unsigned :int))
188 (:long . :long) (:unsigned-long . (:unsigned :long))
189 (:float . :float) (:double . :double)
190 (:array . :c-array)))
192 #+(and mcl (not openmcl))
193 (setq +type-conversion-list+
194 '((* . :pointer) (:void . :void)
195 (:short . :short) (:unsigned-short . :unsigned-short)
196 (:pointer-void . :pointer)
199 (:unsigned-char . :unsigned-byte)
200 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
201 (:int . :long) (:unsigned-int . :unsigned-long)
202 (:long . :long) (:unsigned-long . :unsigned-long)
203 (:float . :single-float) (:double . :double-float)
207 (setq +type-conversion-list+
208 '((* . :address) (:void . :void)
209 (:short . :short) (:unsigned-short . :unsigned-short)
210 (:pointer-void . :address)
211 (:cstring . :address)
212 (:char . :signed-char)
213 (:unsigned-char . :unsigned-char)
214 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
215 (:int . :int) (:unsigned-int . :unsigned-int)
216 (:long . :long) (:unsigned-long . :unsigned-long)
217 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
218 (:float . :single-float) (:double . :double-float)
221 (dolist (type +type-conversion-list+)
222 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
225 (dolist (type +cmu-def-type-list+)
226 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
228 (defun basic-convert-from-uffi-type (type)
229 (let ((found-type (gethash type +type-conversion-hash+)))
233 #+mcl (keyword type))))
235 (defun %convert-from-uffi-type (type context)
236 "Converts from a uffi type to an implementation specific type"
240 ((and (or (eq context :routine) (eq context :return))
242 (setq type '((* :char) integer)))
245 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
248 (basic-convert-from-uffi-type type))))
250 ((and (eq context :return)
252 (basic-convert-from-uffi-type :cstring-returning))
253 #+(and mcl (not openmcl))
254 ((and (eq type :void) (eq context :return)) nil)
256 (basic-convert-from-uffi-type type)))
257 (let ((sub-type (car type)))
260 (convert-from-uffi-type (cadr type) context))
262 #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
263 #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
266 #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
267 #-mcl (%convert-from-uffi-type (cadr type) :struct)
270 (cons (%convert-from-uffi-type (first type) context)
271 (%convert-from-uffi-type (rest type) context)))))))
273 (defun convert-from-uffi-type (type context)
274 (let ((result (%convert-from-uffi-type type context)))
276 ((atom result) result)
278 ((eq (car result) :address)
279 (if (eq context :struct)
280 (append '(:*) (cdr result))
282 #+(and mcl (not openmcl))
283 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))