50023b354919a1932ae1e06d07efcd29f4cbe89a
[uffi.git] / src / primitives.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          primitives.lisp
6 ;;;; Purpose:       UFFI source to handle immediate types
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi)
17
18 #+(or openmcl digitool)
19 (defvar *keyword-package* (find-package "KEYWORD"))
20
21 #+(or openmcl digitool)
22 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
23 ; So this provides a function to convert any quoted symbols to keywords.
24 (defun keyword (obj)
25   (cond ((keywordp obj)
26          obj)
27         ((null obj)
28          nil)
29         ((symbolp obj)
30          (intern (symbol-name obj) *keyword-package*))
31         ((and (listp obj) (eq (car obj) 'cl:quote))
32          (keyword (cadr obj)))
33         ((stringp obj)
34          (intern obj *keyword-package*))
35         (t
36          obj)))
37
38 ; Wrapper for unexported function we have to use
39 #+digitool
40 (defmacro def-mcl-type (name type)
41   `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
42
43 (defmacro def-constant (name value &key (export nil))
44   "Macro to define a constant and to export it"
45   `(eval-when (:compile-toplevel :load-toplevel :execute)
46      (defconstant ,name ,value)
47      ,(when export (list 'export `(quote ,name)))
48     ',name))
49
50 (defmacro def-type (name type)
51   "Generates a (deftype) statement for CL. Currently, only CMUCL
52 supports takes advantage of this optimization."
53   #+(or lispworks allegro openmcl digitool cormanlisp)  (declare (ignore type))
54   #+(or lispworks allegro openmcl digitool cormanlisp) `(deftype ,name () t)
55   #+(or cmu scl)
56   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
57   #+sbcl
58   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
59   )
60
61 (defmacro null-char-p (val)
62   "Returns T if character is NULL"
63   `(zerop ,val))
64
65 (defmacro def-foreign-type (name type)
66   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
67   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
68   #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
69   #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
70   #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
71   #+(or openmcl digitool)
72   (let ((mcl-type (convert-from-uffi-type type :type)))
73     (unless (or (keywordp mcl-type) (consp mcl-type))
74       (setf mcl-type `(quote ,mcl-type)))
75     #+digitool
76     `(def-mcl-type ,(keyword name) ,mcl-type)
77     #+openmcl
78     `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
79   )
80
81 (eval-when (:compile-toplevel :load-toplevel :execute)
82   (defvar +type-conversion-hash+ (make-hash-table :size 20 :test #'eq))
83   #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
84                         (make-hash-table :size 20 :test #'eq))
85   )
86
87 #+(or cmu scl)
88 (defvar *cmu-sbcl-def-type-list*
89     '((:char . (alien:signed 8))
90       (:unsigned-char . (alien:unsigned 8))
91       (:byte . (alien:signed 8))
92       (:unsigned-byte . (alien:unsigned 8))
93       (:short . (alien:signed 16))
94       (:unsigned-short . (alien:unsigned 16))
95       (:int . (alien:signed 32))
96       (:unsigned-int . (alien:unsigned 32))
97       #-x86-64 (:long . (alien:signed 32))
98       #-x86-64 (:unsigned-long . (alien:unsigned 32))
99       #+x86-64 (:long . (alien:signed 64))
100       #+x86-64 (:unsigned-long . (alien:unsigned 64))
101       (:long-long (alien:signed 64))
102       (:unsigned-long-long (alien:unsigned 64))
103       (:float . alien:single-float)
104       (:double . alien:double-float)
105       (:void . t)
106       )
107   "Conversions in CMUCL for def-foreign-type are different than in def-function")
108
109 #+sbcl
110 (defvar *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       #-x86-64 (:long . (sb-alien:signed 32))
120       #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
121       #+x86-64 (:long . (sb-alien:signed 64))
122       #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
123       (:long-long (sb-alien:signed 64))
124       (:unsigned-long-long (sb-alien:unsigned 64))
125       (:float . sb-alien:single-float)
126       (:double . sb-alien:double-float)
127       (:void . t)
128       )
129   "Conversions in SBCL for def-foreign-type are different than in def-function")
130
131 (defvar *type-conversion-list* nil)
132
133 #+(or cmu scl)
134 (setq *type-conversion-list*
135     '((* . *) (:void . c-call:void)
136       (:pointer-void . (* t))
137       (:cstring . c-call:c-string)
138       (:char . c-call:char)
139       (:unsigned-char . (alien:unsigned 8))
140       (:byte . (alien:signed 8))
141       (:unsigned-byte . (alien:unsigned 8))
142       (:short . c-call:short)
143       (:unsigned-short . c-call:unsigned-short)
144       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
145       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
146       (:long-long . c-call:long-long)
147       (:unsigned-long-long . c-call:unsigned-long-long)
148       (:float . c-call:float) (:double . c-call:double)
149       (:array . alien:array)))
150
151 #+sbcl
152 (setq *type-conversion-list*
153     '((* . *) (:void . sb-alien:void)
154       (:pointer-void . (* t))
155       #-sb-unicode(:cstring . sb-alien:c-string)
156       #+sb-unicode(:cstring . sb-alien:utf8-string)
157       (:char . sb-alien:char)
158       (:unsigned-char . (sb-alien:unsigned 8))
159       (:byte . (sb-alien:signed 8))
160       (:unsigned-byte . (sb-alien:unsigned 8))
161       (:short . sb-alien:short)
162       (:unsigned-short . sb-alien:unsigned-short)
163       (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int)
164       (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
165       (:long-long . sb-alien:long-long)
166       (:unsigned-long-long . sb-alien:unsigned-long-long)
167       (:float . sb-alien:float) (:double . sb-alien:double)
168       (:array . sb-alien:array)))
169
170 #+(or allegro cormanlisp)
171 (setq *type-conversion-list*
172     '((* . *) (:void . :void)
173       (:short . :short)
174       (:pointer-void . (* :void))
175       (:cstring . (* :unsigned-char))
176       (:byte . :char)
177       (:unsigned-byte . :unsigned-char)
178       (:char . :char)
179       (:unsigned-char . :unsigned-char)
180       (:int . :int) (:unsigned-int . :unsigned-int)
181       (:long . :long) (:unsigned-long . :unsigned-long)
182       (:float . :float) (:double . :double)
183       (:array . :array)))
184
185 #+(or allegro cormanlisp)
186 (push 'uffi:no-long-long cl:*features)
187
188 #+lispworks
189 (setq *type-conversion-list*
190     '((* . :pointer) (:void . :void)
191       (:short . :short)
192       (:pointer-void . (:pointer :void))
193       (:cstring . (:reference-pass (:ef-mb-string :external-format
194                                     (:latin-1 :eol-style :lf))
195                    :allow-null t))
196       (:cstring-returning . (:reference (:ef-mb-string :external-format
197                                          (:latin-1 :eol-style :lf))
198                              :allow-null t))
199       (:byte . :byte)
200       (:unsigned-byte . (:unsigned :byte))
201       (:char . :char)
202       (:unsigned-char . (:unsigned :char))
203       (:int . :int) (:unsigned-int . (:unsigned :int))
204       (:long . :long) (:unsigned-long . (:unsigned :long))
205       #+lispworks6 (:long-long . :int64)
206       #+lispworks6 (:unsigned-long-long . :uint64)
207       (:float . :float) (:double . :double)
208       (:array . :c-array)))
209
210 #+(and lispworks (not lispworks6))
211 (push 'uffi:no-long-long cl:*features)
212
213 #+digitool
214 (setq *type-conversion-list*
215      '((* . :pointer) (:void . :void)
216        (:short . :short) (:unsigned-short . :unsigned-short)
217        (:pointer-void . :pointer)
218        (:cstring . :string)
219        (:char . :character)
220        (:unsigned-char . :unsigned-byte)
221        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
222        (:int . :long) (:unsigned-int . :unsigned-long)
223        (:long . :long) (:unsigned-long . :unsigned-long)
224        (:float . :single-float) (:double . :double-float)
225        (:array . :array)))
226 #+digitool
227 (push 'uffi:no-long-long cl:*features)
228
229 #+openmcl
230 (setq *type-conversion-list*
231      '((* . :address) (:void . :void)
232        (:short . :short) (:unsigned-short . :unsigned-short)
233        (:pointer-void . :address)
234        (:cstring . :address)
235        (:char . :signed-char)
236        (:unsigned-char . :unsigned-char)
237        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
238        (:int . :int) (:unsigned-int . :unsigned-int)
239        (:long . :long) (:unsigned-long . :unsigned-long)
240        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
241        (:float . :single-float) (:double . :double-float)
242        (:array . :array)))
243
244 (dolist (type *type-conversion-list*)
245   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
246
247 #+(or cmu sbcl scl)
248 (dolist (type *cmu-sbcl-def-type-list*)
249   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
250
251 (defun basic-convert-from-uffi-type (type)
252   (let ((found-type (gethash type +type-conversion-hash+)))
253     (if found-type
254         found-type
255       #-(or openmcl digitool) type
256       #+(or openmcl digitool) (keyword type))))
257
258 (defun %convert-from-uffi-type (type context)
259   "Converts from a uffi type to an implementation specific type"
260   (if (atom type)
261       (cond
262        #+(or allegro cormanlisp)
263        ((and (or (eq context :routine) (eq context :return))
264              (eq type :cstring))
265         (setq type '((* :char) integer)))
266        #+(or cmu sbcl scl)
267        ((eq context :type)
268         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
269           (if cmu-type
270               cmu-type
271               (basic-convert-from-uffi-type type))))
272        #+lispworks
273        ((and (eq context :return)
274              (eq type :cstring))
275         (basic-convert-from-uffi-type :cstring-returning))
276        #+digitool
277        ((and (eq type :void) (eq context :return)) nil)
278        (t
279         (basic-convert-from-uffi-type type)))
280     (let ((sub-type (car type)))
281       (case sub-type
282         (cl:quote
283          (convert-from-uffi-type (cadr type) context))
284         (:struct-pointer
285          #+(or openmcl digitool) `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
286          #-(or openmcl digitool) (%convert-from-uffi-type (list '* (cadr type)) :struct)
287          )
288         (:struct
289          #+(or openmcl digitool) `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
290          #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :struct)
291          )
292        (:union
293         #+(or openmcl digitool) `(:union ,(%convert-from-uffi-type (cadr type) :union))
294         #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :union)
295         )
296        (t
297         (cons (%convert-from-uffi-type (first type) context)
298               (%convert-from-uffi-type (rest type) context)))))))
299
300 (defun convert-from-uffi-type (type context)
301   (let ((result (%convert-from-uffi-type type context)))
302     (cond
303      ((atom result) result)
304      #+openmcl
305      ((eq (car result) :address)
306       (if (eq context :struct)
307           (append '(:*) (cdr result))
308         :address))
309      #+digitool
310      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
311      (t result))))
312
313 (eval-when (:compile-toplevel :load-toplevel :execute)
314   (when (char= #\a (schar (symbol-name '#:a) 0))
315     (pushnew :uffi-lowercase-reader *features*))
316   (when (not (string= (symbol-name '#:a)
317                       (symbol-name '#:A)))
318     (pushnew :uffi-case-sensitive *features*)))
319
320 (defun make-lisp-name (name)
321   (let ((converted (substitute #\- #\_ name)))
322      (intern
323       #+uffi-case-sensitive converted
324       #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
325       #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
326
327 (eval-when (:compile-toplevel :load-toplevel :execute)
328   (setq cl:*features* (delete :uffi-lowercase-reader *features*))
329   (setq cl:*features* (delete :uffi-case-sensitive *features*)))