r5062: return from san diego
[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: primitives.lisp,v 1.9 2003/06/06 21:59:18 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 (in-package #:uffi)
20
21 #+mcl
22 (defvar *keyword-package* (find-package "KEYWORD"))
23
24 #+mcl
25 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
26 ; So this provides a function to convert any quoted symbols to keywords.
27 (defun keyword (obj)
28   (cond ((keywordp obj) 
29          obj)
30         ((null obj)
31          nil)
32         ((symbolp obj)
33          (intern (symbol-name obj) *keyword-package*))
34         ((and (listp obj) (eq (car obj) 'cl:quote))
35          (keyword (cadr obj)))
36         ((stringp obj)
37          (intern obj *keyword-package*))
38         (t 
39          obj)))
40
41 ; Wrapper for unexported function we have to use
42 #+(and mcl (not openmcl))
43 (defmacro def-mcl-type (name type)
44   `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
45
46 (defmacro def-constant (name value &key (export nil))
47   "Macro to define a constant and to export it"
48   `(eval-when (:compile-toplevel :load-toplevel :execute)
49      (defconstant ,name ,value)
50      ,(when export (list 'export `(quote ,name)))
51     ',name))
52
53 (defmacro def-type (name type)
54   "Generates a (deftype) statement for CL. Currently, only CMUCL
55 supports takes advantage of this optimization."
56   #+(or lispworks allegro mcl cormanlisp)  (declare (ignore type))
57   #+(or lispworks allegro mcl cormanlisp) `(deftype ,name () t)
58   #+(or cmu scl)
59   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
60   #+sbcl
61   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
62   )
63
64 (defmacro null-char-p (val)
65   "Returns T if character is NULL"
66   `(zerop ,val))
67       
68 (defmacro def-foreign-type (name type)
69   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
70   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
71   #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
72   #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
73   #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
74   #+mcl
75   (let ((mcl-type (convert-from-uffi-type type :type)))
76     (unless (or (keywordp mcl-type) (consp mcl-type))
77       (setf mcl-type `(quote ,mcl-type)))
78     #+(and mcl (not openmcl))
79     `(def-mcl-type ,(keyword name) ,mcl-type)
80     #+openmcl
81     `(ccl::def-foreign-type ,(keyword name) ,mcl-type))  
82   )
83
84 (eval-when (:compile-toplevel :load-toplevel :execute)
85   (defvar +type-conversion-hash+ (make-hash-table :size 20))
86   #+(or cmu sbcl scl) (defvar *cmu-def-type-hash* (make-hash-table :size 20))
87   )
88
89 #+(or cmu sbcl scl)
90 (defparameter *cmu-sbcl-def-type-list* nil)
91
92 #+(or cmu scl)
93 (defparameter *cmu-sbcl-def-type-list*
94     '((:char . (alien:signed 8))
95       (:unsigned-char . (alien:unsigned 8))
96       (:byte . (alien:signed 8))
97       (:unsigned-byte . (alien:unsigned 8))
98       (:short . (alien:signed 16))
99       (:unsigned-short . (alien:unsigned 16))
100       (:int . (alien:signed 32))
101       (:unsigned-int . (alien:unsigned 32))
102       (:long . (alien:signed 32))
103       (:unsigned-long . (alien:unsigned 32))
104       (:float . alien:single-float)
105       (:double . alien:double-float)
106       )
107   "Conversions in CMUCL for def-foreign-type are different than in def-function")
108 #+sbcl
109 (defparameter *cmu-sbcl-def-type-list*
110     '((:char . (sb-alien:signed 8))
111       (:unsigned-char . (sb-alien:unsigned 8))
112       (:byte . (sb-alien:signed 8))
113       (:unsigned-byte . (sb-alien:unsigned 8))
114       (:short . (sb-alien:signed 16))
115       (:unsigned-short . (sb-alien:unsigned 16))
116       (:int . (sb-alien:signed 32))
117       (:unsigned-int . (sb-alien:unsigned 32))
118       (:long . (sb-alien:signed 32))
119       (:unsigned-long . (sb-alien:unsigned 32))
120       (:float . sb-alien:single-float)
121       (:double . sb-alien:double-float)
122       )
123   "Conversions in SBCL for def-foreign-type are different than in def-function")
124
125 (defparameter *type-conversion-list* nil)
126
127 #+(or cmu scl)
128 (setq *type-conversion-list*
129     '((* . *) (:void . c-call:void) 
130       (:short . c-call:short)
131       (:pointer-void . (* t))
132       (:cstring . c-call:c-string)
133       (:char . c-call:char) 
134       (:unsigned-char . (alien:unsigned 8))
135       (:byte . (alien:signed 8))
136       (:unsigned-byte . (alien:unsigned 8))
137       (:short . c-call:unsigned-short) 
138       (:unsigned-short . c-call:unsigned-short)
139       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
140       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
141       (:float . c-call:float) (:double . c-call:double)
142       (:array . alien:array)))
143
144 #+sbcl
145 (setq *type-conversion-list*
146     '((* . *) (:void . sb-alien:void) 
147       (:short . sb-alien:short)
148       (:pointer-void . (* t))
149       (:cstring . sb-alien:c-string)
150       (:char . sb-alien:char) 
151       (:unsigned-char . (sb-alien:unsigned 8))
152       (:byte . (sb-alien:signed 8))
153       (:unsigned-byte . (sb-alien:unsigned 8))
154       (:short . sb-alien:unsigned-short) 
155       (:unsigned-short . sb-alien:unsigned-short)
156       (:int . sb-alien:integer) (:unsigned-int . sb-alien:unsigned-int) 
157       (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
158       (:float . sb-alien:float) (:double . sb-alien:double)
159       (:array . sb-alien:array)))
160
161 #+(or allegro cormanlisp)
162 (setq *type-conversion-list*
163     '((* . *) (:void . :void)
164       (:short . :short)
165       (:pointer-void . (* :void))
166       (:cstring . (* :unsigned-char))
167       (:byte . :char)
168       (:unsigned-byte . :unsigned-char)
169       (:char . :char)
170       (:unsigned-char . :unsigned-char)
171       (:int . :int) (:unsigned-int . :unsigned-int) 
172       (:long . :long) (:unsigned-long . :unsigned-long)
173       (:float . :float) (:double . :double)
174       (:array . :array)))
175
176 #+lispworks
177 (setq *type-conversion-list*
178     '((* . :pointer) (:void . :void) 
179       (:short . :short)
180       (:pointer-void . (:pointer :void))
181       (:cstring . (:reference-pass (:ef-mb-string :external-format
182                                     (:latin-1 :eol-style :lf))
183                    :allow-null t))
184       (:cstring-returning . (:reference (:ef-mb-string :external-format
185                                          (:latin-1 :eol-style :lf))
186                              :allow-null t))
187       (:byte . :byte)
188       (:unsigned-byte . (:unsigned :byte))
189       (:char . :char)
190       (:unsigned-char . (:unsigned :char))
191       (:int . :int) (:unsigned-int . (:unsigned :int))
192       (:long . :long) (:unsigned-long . (:unsigned :long))
193       (:float . :float) (:double . :double)
194       (:array . :c-array)))
195
196 #+(and mcl (not openmcl))
197 (setq *type-conversion-list*
198      '((* . :pointer) (:void . :void)
199        (:short . :short) (:unsigned-short . :unsigned-short)
200        (:pointer-void . :pointer)
201        (:cstring . :string)
202        (:char . :character)
203        (:unsigned-char . :unsigned-byte)
204        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
205        (:int . :long) (:unsigned-int . :unsigned-long)
206        (:long . :long) (:unsigned-long . :unsigned-long)
207        (:float . :single-float) (:double . :double-float)
208        (:array . :array)))
209
210 #+openmcl
211 (setq *type-conversion-list*
212      '((* . :address) (:void . :void)
213        (:short . :short) (:unsigned-short . :unsigned-short)
214        (:pointer-void . :address)
215        (:cstring . :address)
216        (:char . :signed-char)
217        (:unsigned-char . :unsigned-char)
218        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
219        (:int . :int) (:unsigned-int . :unsigned-int)
220        (:long . :long) (:unsigned-long . :unsigned-long)
221        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
222        (:float . :single-float) (:double . :double-float)
223        (:array . :array)))
224
225 (dolist (type *type-conversion-list*)
226   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
227
228 #+(or cmu sbcl scl)
229 (dolist (type *cmu-sbcl-def-type-list*)
230   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
231
232 (defun basic-convert-from-uffi-type (type)
233   (let ((found-type (gethash type +type-conversion-hash+)))
234     (if found-type
235         found-type
236       #-mcl type
237       #+mcl (keyword type))))
238
239 (defun %convert-from-uffi-type (type context)
240   "Converts from a uffi type to an implementation specific type"
241   (if (atom type)
242       (cond
243        #+(or allegro cormanlisp)
244        ((and (or (eq context :routine) (eq context :return))
245              (eq type :cstring))
246         (setq type '((* :char) integer)))
247        #+(or cmu sbcl scl)
248        ((eq context :type)
249         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
250           (if cmu-type
251               cmu-type
252               (basic-convert-from-uffi-type type))))
253        #+lispworks
254        ((and (eq context :return)
255              (eq type :cstring))
256         (basic-convert-from-uffi-type :cstring-returning))
257        #+(and mcl (not openmcl))
258        ((and (eq type :void) (eq context :return)) nil)
259        (t
260         (basic-convert-from-uffi-type type)))
261     (let ((sub-type (car type)))
262       (case sub-type
263         (cl:quote
264          (convert-from-uffi-type (cadr type) context))
265         (:struct-pointer
266          #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
267          #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
268          )
269         (:struct
270          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
271          #-mcl (%convert-from-uffi-type (cadr type) :struct)
272          )
273         (t
274          (cons (%convert-from-uffi-type (first type) context) 
275                (%convert-from-uffi-type (rest type) context)))))))
276
277 (defun convert-from-uffi-type (type context)
278   (let ((result (%convert-from-uffi-type type context)))
279     (cond
280      ((atom result) result)
281      #+openmcl
282      ((eq (car result) :address)
283       (if (eq context :struct)
284           (append '(:*) (cdr result))
285         :address))
286      #+(and mcl (not openmcl))
287      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
288      (t result))))
289