r2927: Integrate Reini Urban's cormanlisp patches into main UFFI source
[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.2 2002/10/01 17:05:29 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:def-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 #+cmu
91 (defconstant +cmu-def-type-list+
92     '((:char . (alien:signed 8))
93       (:unsigned-char . (alien:unsigned 8))
94       (:byte . (alien:signed 8))
95       (:unsigned-byte . (alien:unsigned 8))
96       (:short . (alien:signed 16))
97       (:unsigned-short . (alien:unsigned 16))
98       (:int . (alien:signed 32))
99       (:unsigned-int . (alien:unsigned 32))
100       (:long . (alien:signed 32))
101       (:unsigned-long . (alien:unsigned 32))
102       (:float . alien:single-float)
103       (:double . alien:double-float)
104       )
105   "Conversions in CMUCL for def-foreign-type are different than in def-function")
106 #+sbcl
107 (defconstant +cmu-def-type-list+
108     '((:char . (sb-alien:signed 8))
109       (:unsigned-char . (sb-alien:unsigned 8))
110       (:byte . (sb-alien:signed 8))
111       (:unsigned-byte . (sb-alien:unsigned 8))
112       (:short . (sb-alien:signed 16))
113       (:unsigned-short . (sb-alien:unsigned 16))
114       (:int . (sb-alien:signed 32))
115       (:unsigned-int . (sb-alien:unsigned 32))
116       (:long . (sb-alien:signed 32))
117       (:unsigned-long . (sb-alien:unsigned 32))
118       (:float . sb-alien:single-float)
119       (:double . sb-alien:double-float)
120       )
121   "Conversions in SBCL for def-foreign-type are different than in def-function")
122
123 (defparameter +type-conversion-list+ nil)
124
125 #+cmu
126 (setq +type-conversion-list+
127     '((* . *) (:void . c-call:void) 
128       (:short . c-call:short)
129       (:pointer-void . (* t))
130       (:cstring . c-call:c-string)
131       (:char . c-call:char) 
132       (:unsigned-char . (alien:unsigned 8))
133       (:byte . (alien:signed 8))
134       (:unsigned-byte . (alien:unsigned 8))
135       (:short . c-call:unsigned-short) 
136       (:unsigned-short . c-call:unsigned-short)
137       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
138       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
139       (:float . c-call:float) (:double . c-call:double)
140       (:array . alien:array)))
141
142 #+sbcl
143 (setq +type-conversion-list+
144     '((* . *) (:void . void) 
145       (:short . short)
146       (:pointer-void . (* t))
147       (:cstring . c-string)
148       (:char . char) 
149       (:unsigned-char . (sb-alien:unsigned 8))
150       (:byte . (sb-alien:signed 8))
151       (:unsigned-byte . (sb-alien:unsigned 8))
152       (:short . unsigned-short) 
153       (:unsigned-short . unsigned-short)
154       (:int . integer) (:unsigned-int . unsigned-int) 
155       (:long . long) (:unsigned-long . unsigned-long)
156       (:float . float) (:double . double)
157       (:array . array)))
158
159 #+(or allegro cormanlisp)
160 (setq +type-conversion-list+
161     '((* . *) (:void . :void)
162       (:short . :short)
163       (:pointer-void . (* :void))
164       (:cstring . (* :unsigned-char))
165       (:byte . :char)
166       (:unsigned-byte . :unsigned-byte)
167       (:char . :char)
168       (:unsigned-char . :unsigned-char)
169       (:int . :int) (:unsigned-int . :unsigned-int) 
170       (:long . :long) (:unsigned-long . :unsigned-long)
171       (:float . :float) (:double . :double)
172       (:array . :array)))
173
174 #+lispworks
175 (setq +type-conversion-list+
176     '((* . :pointer) (:void . :void) 
177       (:short . :short)
178       (:pointer-void . (:pointer :void))
179       (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
180                                    :allow-null t))
181       (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
182       (:byte . :byte)
183       (:unsigned-byte . (:unsigned :byte))
184       (:char . :char)
185       (:unsigned-char . (:unsigned :char))
186       (:int . :int) (:unsigned-int . (:unsigned :int))
187       (:long . :long) (:unsigned-long . (:unsigned :long))
188       (:float . :float) (:double . :double)
189       (:array . :c-array)))
190
191 #+(and mcl (not openmcl))
192 (setq +type-conversion-list+
193      '((* . :pointer) (:void . :void)
194        (:short . :short) (:unsigned-short . :unsigned-short)
195        (:pointer-void . :pointer)
196        (:cstring . :string)
197        (:char . :character)
198        (:unsigned-char . :unsigned-byte)
199        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
200        (:int . :long) (:unsigned-int . :unsigned-long)
201        (:long . :long) (:unsigned-long . :unsigned-long)
202        (:float . :single-float) (:double . :double-float)
203        (:array . :array)))
204
205 #+openmcl
206 (setq +type-conversion-list+
207      '((* . :address) (:void . :void)
208        (:short . :short) (:unsigned-short . :unsigned-short)
209        (:pointer-void . :address)
210        (:cstring . :address)
211        (:char . :signed-char)
212        (:unsigned-char . :unsigned-char)
213        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
214        (:int . :int) (:unsigned-int . :unsigned-int)
215        (:long . :long) (:unsigned-long . :unsigned-long)
216        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
217        (:float . :single-float) (:double . :double-float)
218        (:array . :array)))
219
220 (dolist (type +type-conversion-list+)
221   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
222
223 #+(or cmu sbcl)
224 (dolist (type +cmu-def-type-list+)
225   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
226
227 (defun basic-convert-from-uffi-type (type)
228   (let ((found-type (gethash type +type-conversion-hash+)))
229     (if found-type
230         found-type
231       #-mcl type
232       #+mcl (keyword type))))
233
234 (defun %convert-from-uffi-type (type context)
235   "Converts from a uffi type to an implementation specific type"
236   (if (atom type)
237       (cond
238        #+(or allegro cormanlisp)
239        ((and (or (eq context :routine) (eq context :return))
240              (eq type :cstring))
241         (setq type '((* :char) integer)))
242        #+(or cmu sbcl)
243        ((eq context :type)
244         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
245           (if cmu-type
246               cmu-type
247               (basic-convert-from-uffi-type type))))
248        #+lispworks
249        ((and (eq context :return)
250              (eq type :cstring))
251         (basic-convert-from-uffi-type :cstring-returning))
252        #+(and mcl (not openmcl))
253        ((and (eq type :void) (eq context :return)) nil)
254        (t
255         (basic-convert-from-uffi-type type)))
256     (let ((sub-type (car type)))
257       (case sub-type
258         (cl:quote
259          (convert-from-uffi-type (cadr type) context))
260         (:struct-pointer
261          #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
262          #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
263          )
264         (:struct
265          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
266          #-mcl (%convert-from-uffi-type (cadr type) :struct)
267          )
268         (t
269          (cons (%convert-from-uffi-type (first type) context) 
270                (%convert-from-uffi-type (rest type) context)))))))
271
272 (defun convert-from-uffi-type (type context)
273   (let ((result (%convert-from-uffi-type type context)))
274     (cond
275      ((atom result) result)
276      #+openmcl
277      ((eq (car result) :address)
278       (if (eq context :struct)
279           (append '(:*) (cdr result))
280         :address))
281      #+(and mcl (not openmcl))
282      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
283      (t result))))
284