r2905: *** empty log message ***
[uffi.git] / src / primitives.cl
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          primitives.cl
6 ;;;; Purpose:       UFFI source to handle immediate types
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: primitives.cl,v 1.24 2002/09/30 07:51:01 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22 #+mcl
23 (defvar *keyword-package* (find-package "KEYWORD"))
24
25 #+mcl
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.
28 (defun keyword (obj)
29   (cond ((keywordp obj) 
30          obj)
31         ((null obj)
32          nil)
33         ((symbolp obj)
34          (intern (symbol-name obj) *keyword-package*))
35         ((and (listp obj) (eq (car obj) 'cl:quote))
36          (keyword (cadr obj)))
37         ((stringp obj)
38          (intern obj *keyword-package*))
39         (t 
40          obj)))
41
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)))
46
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)))
52     ',name))
53
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)
60   `(deftype ,name () t)
61   #+cmu
62   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
63   #+sbcl
64   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
65   )
66
67 (defmacro null-char-p (val)
68   "Returns T if character is NULL"
69   `(zerop ,val))
70       
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))
76   #+mcl
77   (let ((type (convert-from-uffi-type uffi-type :type)))
78     (unless (or (keywordp type) (consp type))
79       (setf type `(quote ,type)))
80     #+(and mcl (not openmcl))
81     `(def-mcl-type ,(keyword name) ,type)
82     #+openmcl
83     `(ccl::def-foreign-type ,(keyword name) ,type))  
84   )
85
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))
89   )
90
91 #+cmu
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)
105       )
106   "Conversions in CMUCL for def-foreign-type are different than in def-function")
107 #+sbcl
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)
121       )
122   "Conversions in SBCL for def-foreign-type are different than in def-function")
123
124 (defparameter +type-conversion-list+ nil)
125
126 #+cmu
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)))
142
143 #+sbcl
144 (setq +type-conversion-list+
145     '((* . *) (:void . void) 
146       (:short . short)
147       (:pointer-void . (* t))
148       (:cstring . c-string)
149       (:char . char) 
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)
158       (:array . array)))
159
160 #+allegro
161 (setq +type-conversion-list+
162     '((* . *) (:void . :void)
163       (:short . :short)
164       (:pointer-void . (* :void))
165       (:cstring . (* :unsigned-char))
166       (:byte . :char)
167       (:unsigned-byte . :unsigned-byte)
168       (:char . :char)
169       (:unsigned-char . :unsigned-char)
170       (:int . :int) (:unsigned-int . :unsigned-int) 
171       (:long . :long) (:unsigned-long . :unsigned-long)
172       (:float . :float) (:double . :double)
173       (:array . :array)))
174
175 #+lispworks
176 (setq +type-conversion-list+
177     '((* . :pointer) (:void . :void) 
178       (:short . :short)
179       (:pointer-void . (:pointer :void))
180       (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
181                                    :allow-null t))
182       (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
183       (:byte . :byte)
184       (:unsigned-byte . (:unsigned :byte))
185       (:char . :char)
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)))
191
192 #+(and mcl (not openmcl))
193 (defconstant +type-conversion-list+
194      '((* . :pointer) (:void . :void)
195        (:short . :short) (:unsigned-short . :unsigned-short)
196        (:pointer-void . :pointer)
197        (:cstring . :string)
198        (:char . :character)
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)
204        (:array . :array)))
205
206 #+openmcl
207 (defconstant +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)
219        (:array . :array)))
220
221 (dolist (type +type-conversion-list+)
222   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
223
224 #+(or cmu sbcl)
225 (dolist (type +cmu-def-type-list+)
226   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
227
228 (defun basic-convert-from-uffi-type (type)
229   (let ((found-type (gethash type +type-conversion-hash+)))
230     (if found-type
231         found-type
232       #-mcl type
233       #+mcl (keyword type))))
234
235 (defun %convert-from-uffi-type (type context)
236   "Converts from a uffi type to an implementation specific type"
237   (if (atom type)
238       (cond
239        #+allegro 
240        ((and (or (eq context :routine) (eq context :return))
241              (eq type :cstring))
242         (setq type '((* :char) integer)))
243        #+(or cmu sbcl)
244        ((eq context :type)
245         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
246           (if cmu-type
247               cmu-type
248               (basic-convert-from-uffi-type type))))
249        #+lispworks
250        ((and (eq context :return)
251              (eq type :cstring))
252         (basic-convert-from-uffi-type :cstring-returning))
253        #+(and mcl (not openmcl))
254        ((and (eq type :void) (eq context :return)) nil)
255        (t
256         (basic-convert-from-uffi-type type)))
257     (let ((sub-type (car type)))
258       (case sub-type
259         (cl:quote
260          (convert-from-uffi-type (cadr type) context))
261         (:struct-pointer
262          #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
263          #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
264          )
265         (:struct
266          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
267          #-mcl (%convert-from-uffi-type (cadr type) :struct)
268          )
269         (t
270          (cons (%convert-from-uffi-type (first type) context) 
271                (%convert-from-uffi-type (rest type) context)))))))
272
273 (defun convert-from-uffi-type (type context)
274   (let ((result (%convert-from-uffi-type type context)))
275     (cond
276      ((atom result) result)
277      #+openmcl
278      ((eq (car result) :address)
279       (if (eq context :struct)
280           (append '(:*) (cdr result))
281         :address))
282      #+(and mcl (not openmcl))
283      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
284      (t result))))
285