c10766416a0ee348599e604e66f4ea2b56abc0c2
[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.22 2002/08/23 15:28:52 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 (defmacro def-constant (name value &key (export nil))
23   "Macro to define a constant and to export it"
24   `(eval-when (:compile-toplevel :load-toplevel :execute)
25      (defconstant ,name ,value)
26      ,(when export (list 'export `(quote ,name)))
27     ',name))
28
29 (defmacro def-type (name type)
30   "Generates a (deftype) statement for CL. Currently, only CMUCL
31 supports takes advantage of this optimization."
32   #+(or lispworks allegro)
33   (declare (ignore type))
34   #+(or lispworks allegro)
35   `(deftype ,name () t)
36   #+cmu
37   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
38   #+sbcl
39   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
40   )
41
42 (defmacro null-char-p (val)
43   "Returns T if character is NULL"
44   `(zerop ,val))
45       
46 (defmacro def-foreign-type (name type)
47   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
48   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
49   #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
50   #+sbcl `(sb-alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
51   )
52
53 (eval-when (:compile-toplevel :load-toplevel :execute)
54   (defvar +type-conversion-hash+ (make-hash-table :size 20))
55   #+(or cmu sbcl) (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
56   )
57
58 #+cmu
59 (defconstant +cmu-def-type-list+
60     '((:char . (alien:signed 8))
61       (:unsigned-char . (alien:unsigned 8))
62       (:byte . (alien:signed 8))
63       (:unsigned-byte . (alien:unsigned 8))
64       (:short . (alien:signed 16))
65       (:unsigned-short . (alien:unsigned 16))
66       (:int . (alien:signed 32))
67       (:unsigned-int . (alien:unsigned 32))
68       (:long . (alien:signed 32))
69       (:unsigned-long . (alien:unsigned 32))
70       (:float . alien:single-float)
71       (:double . alien:double-float)
72       )
73   "Conversions in CMUCL for def-foreign-type are different than in def-function")
74 #+sbcl
75 (defconstant +cmu-def-type-list+
76     '((:char . (sb-alien:signed 8))
77       (:unsigned-char . (sb-alien:unsigned 8))
78       (:byte . (sb-alien:signed 8))
79       (:unsigned-byte . (sb-alien:unsigned 8))
80       (:short . (sb-alien:signed 16))
81       (:unsigned-short . (sb-alien:unsigned 16))
82       (:int . (sb-alien:signed 32))
83       (:unsigned-int . (sb-alien:unsigned 32))
84       (:long . (sb-alien:signed 32))
85       (:unsigned-long . (sb-alien:unsigned 32))
86       (:float . sb-alien:single-float)
87       (:double . sb-alien:double-float)
88       )
89   "Conversions in SBCL for def-foreign-type are different than in def-function")
90
91 (defparameter +type-conversion-list+ nil)
92
93 #+cmu
94 (setq +type-conversion-list+
95     '((* . *) (:void . c-call:void) 
96       (:short . c-call:short)
97       (:pointer-void . (* t))
98       (:cstring . c-call:c-string)
99       (:char . c-call:char) 
100       (:unsigned-char . (alien:unsigned 8))
101       (:byte . (alien:signed 8))
102       (:unsigned-byte . (alien:unsigned 8))
103       (:short . c-call:unsigned-short) 
104       (:unsigned-short . c-call:unsigned-short)
105       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
106       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
107       (:float . c-call:float) (:double . c-call:double)
108       (:array . alien:array)))
109
110 #+sbcl
111 (setq +type-conversion-list+
112     '((* . *) (:void . void) 
113       (:short . short)
114       (:pointer-void . (* t))
115       (:cstring . c-string)
116       (:char . char) 
117       (:unsigned-char . (sb-alien:unsigned 8))
118       (:byte . (sb-alien:signed 8))
119       (:unsigned-byte . (sb-alien:unsigned 8))
120       (:short . unsigned-short) 
121       (:unsigned-short . unsigned-short)
122       (:int . integer) (:unsigned-int . unsigned-int) 
123       (:long . long) (:unsigned-long . unsigned-long)
124       (:float . float) (:double . double)
125       (:array . array)))
126
127 #+allegro
128 (setq +type-conversion-list+
129     '((* . *) (:void . :void)
130       (:short . :short)
131       (:pointer-void . (* :void))
132       (:cstring . (* :unsigned-char))
133       (:byte . :char)
134       (:unsigned-byte . :unsigned-byte)
135       (:char . :char)
136       (:unsigned-char . :unsigned-char)
137       (:int . :int) (:unsigned-int . :unsigned-int) 
138       (:long . :long) (:unsigned-long . :unsigned-long)
139       (:float . :float) (:double . :double)
140       (:array . :array)))
141 #+lispworks
142 (setq +type-conversion-list+
143     '((* . :pointer) (:void . :void) 
144       (:short . :short)
145       (:pointer-void . (:pointer :void))
146       (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
147                                    :allow-null t))
148       (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
149       (:byte . :byte)
150       (:unsigned-byte . (:unsigned :byte))
151       (:char . :char)
152       (:unsigned-char . (:unsigned :char))
153       (:int . :int) (:unsigned-int . (:unsigned :int))
154       (:long . :long) (:unsigned-long . (:unsigned :long))
155       (:float . :float) (:double . :double)
156       (:array . :c-array)))
157
158 (dolist (type +type-conversion-list+)
159   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
160
161 #+(or cmu sbcl)
162 (dolist (type +cmu-def-type-list+)
163   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
164
165 (defun basic-convert-from-uffi-type (type)
166   (let ((found-type (gethash type +type-conversion-hash+)))
167     (if found-type
168         found-type
169         type)))
170
171 (defun convert-from-uffi-type (type context)
172   "Converts from a uffi type to an implementation specific type"
173   (if (atom type)
174       (cond
175        #+allegro 
176        ((and (or (eq context :routine) (eq context :return))
177              (eq type :cstring))
178         (setq type '((* :char) integer)))
179        #+(or cmu sbcl)
180        ((eq context :type)
181         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
182           (if cmu-type
183               cmu-type
184               (basic-convert-from-uffi-type type))))
185        #+lispworks
186        ((and (eq context :return)
187              (eq type :cstring))
188         (basic-convert-from-uffi-type :cstring-returning))
189        (t
190         (basic-convert-from-uffi-type type)))
191       (cons (convert-from-uffi-type (first type) context) 
192             (convert-from-uffi-type (rest type) context))))
193
194
195
196
197
198