r3004: more sbcl changes
[uffi.git] / src / primitives.lisp
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.lisp,v 1.4 2002/10/14 04:15:02 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 cormanlisp)  (declare (ignore type))
58   #+(or lispworks allegro mcl cormanlisp) `(deftype ,name () t)
59   #+cmu
60   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
61   #+sbcl
62   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
63   )
64
65 (defmacro null-char-p (val)
66   "Returns T if character is NULL"
67   `(zerop ,val))
68       
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   #+cmu `(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))
75   #+mcl
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)
81     #+openmcl
82     `(ccl::def-foreign-type ,(keyword name) ,mcl-type))  
83   )
84
85 (eval-when (:compile-toplevel :load-toplevel :execute)
86   (defvar +type-conversion-hash+ (make-hash-table :size 20))
87   #+(or cmu sbcl) (defvar *cmu-def-type-hash* (make-hash-table :size 20))
88   )
89
90 #+(or cmu sbcl)
91 (defparameter *cmu-sbcl-def-type-list* nil)
92
93 #+cmu
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)
107       )
108   "Conversions in CMUCL for def-foreign-type are different than in def-function")
109 #+sbcl
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)
123       )
124   "Conversions in SBCL for def-foreign-type are different than in def-function")
125
126 (defparameter *type-conversion-list* nil)
127
128 #+cmu
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)))
144
145 #+sbcl
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)))
161
162 #+(or allegro cormanlisp)
163 (setq *type-conversion-list*
164     '((* . *) (:void . :void)
165       (:short . :short)
166       (:pointer-void . (* :void))
167       (:cstring . (* :unsigned-char))
168       (:byte . :char)
169       (:unsigned-byte . :unsigned-byte)
170       (:char . :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)
175       (:array . :array)))
176
177 #+lispworks
178 (setq *type-conversion-list*
179     '((* . :pointer) (:void . :void) 
180       (:short . :short)
181       (:pointer-void . (:pointer :void))
182       (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
183                                    :allow-null t))
184       (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
185       (:byte . :byte)
186       (:unsigned-byte . (:unsigned :byte))
187       (:char . :char)
188       (:unsigned-char . (:unsigned :char))
189       (:int . :int) (:unsigned-int . (:unsigned :int))
190       (:long . :long) (:unsigned-long . (:unsigned :long))
191       (:float . :float) (:double . :double)
192       (:array . :c-array)))
193
194 #+(and mcl (not openmcl))
195 (setq *type-conversion-list*
196      '((* . :pointer) (:void . :void)
197        (:short . :short) (:unsigned-short . :unsigned-short)
198        (:pointer-void . :pointer)
199        (:cstring . :string)
200        (:char . :character)
201        (:unsigned-char . :unsigned-byte)
202        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
203        (:int . :long) (:unsigned-int . :unsigned-long)
204        (:long . :long) (:unsigned-long . :unsigned-long)
205        (:float . :single-float) (:double . :double-float)
206        (:array . :array)))
207
208 #+openmcl
209 (setq *type-conversion-list*
210      '((* . :address) (:void . :void)
211        (:short . :short) (:unsigned-short . :unsigned-short)
212        (:pointer-void . :address)
213        (:cstring . :address)
214        (:char . :signed-char)
215        (:unsigned-char . :unsigned-char)
216        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
217        (:int . :int) (:unsigned-int . :unsigned-int)
218        (:long . :long) (:unsigned-long . :unsigned-long)
219        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
220        (:float . :single-float) (:double . :double-float)
221        (:array . :array)))
222
223 (dolist (type *type-conversion-list*)
224   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
225
226 #+(or cmu sbcl)
227 (dolist (type *cmu-sbcl-def-type-list*)
228   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
229
230 (defun basic-convert-from-uffi-type (type)
231   (let ((found-type (gethash type +type-conversion-hash+)))
232     (if found-type
233         found-type
234       #-mcl type
235       #+mcl (keyword type))))
236
237 (defun %convert-from-uffi-type (type context)
238   "Converts from a uffi type to an implementation specific type"
239   (if (atom type)
240       (cond
241        #+(or allegro cormanlisp)
242        ((and (or (eq context :routine) (eq context :return))
243              (eq type :cstring))
244         (setq type '((* :char) integer)))
245        #+(or cmu sbcl)
246        ((eq context :type)
247         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
248           (if cmu-type
249               cmu-type
250               (basic-convert-from-uffi-type type))))
251        #+lispworks
252        ((and (eq context :return)
253              (eq type :cstring))
254         (basic-convert-from-uffi-type :cstring-returning))
255        #+(and mcl (not openmcl))
256        ((and (eq type :void) (eq context :return)) nil)
257        (t
258         (basic-convert-from-uffi-type type)))
259     (let ((sub-type (car type)))
260       (case sub-type
261         (cl:quote
262          (convert-from-uffi-type (cadr type) context))
263         (:struct-pointer
264          #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
265          #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
266          )
267         (:struct
268          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
269          #-mcl (%convert-from-uffi-type (cadr type) :struct)
270          )
271         (t
272          (cons (%convert-from-uffi-type (first type) context) 
273                (%convert-from-uffi-type (rest type) context)))))))
274
275 (defun convert-from-uffi-type (type context)
276   (let ((result (%convert-from-uffi-type type context)))
277     (cond
278      ((atom result) result)
279      #+openmcl
280      ((eq (car result) :address)
281       (if (eq context :struct)
282           (append '(:*) (cdr result))
283         :address))
284      #+(and mcl (not openmcl))
285      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
286      (t result))))
287