r9418: rework cmucl/sbcl arrays in deref-array, allocate-foreign-object, and with...
[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$
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 :test #'eq))
86   #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
87                         (make-hash-table :size 20 :test #'eq))
88   )
89
90 #+(or cmu scl)
91 (defvar *cmu-sbcl-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       (:void . t)
105       )
106   "Conversions in CMUCL for def-foreign-type are different than in def-function")
107
108 #+sbcl
109 (defvar *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       (:void . t)
123       )
124   "Conversions in SBCL for def-foreign-type are different than in def-function")
125
126 (defvar *type-conversion-list* nil)
127
128 #+(or cmu scl)
129 (setq *type-conversion-list*
130     '((* . *) (:void . c-call:void) 
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: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       (:pointer-void . (* t))
148       (:cstring . sb-alien:c-string)
149       (:char . sb-alien:char) 
150       (:unsigned-char . (sb-alien:unsigned 8))
151       (:byte . (sb-alien:signed 8))
152       (:unsigned-byte . (sb-alien:unsigned 8))
153       (:short . sb-alien:short)
154       (:unsigned-short . sb-alien:unsigned-short)
155       (:int . sb-alien:integer) (:unsigned-int . sb-alien:unsigned-int) 
156       (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
157       (:float . sb-alien:float) (:double . sb-alien:double)
158       (:array . sb-alien:array)))
159
160 #+(or allegro cormanlisp)
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-char)
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
181                                     (:latin-1 :eol-style :lf))
182                    :allow-null t))
183       (:cstring-returning . (:reference (:ef-mb-string :external-format
184                                          (:latin-1 :eol-style :lf))
185                              :allow-null t))
186       (:byte . :byte)
187       (:unsigned-byte . (:unsigned :byte))
188       (:char . :char)
189       (:unsigned-char . (:unsigned :char))
190       (:int . :int) (:unsigned-int . (:unsigned :int))
191       (:long . :long) (:unsigned-long . (:unsigned :long))
192       (:float . :float) (:double . :double)
193       (:array . :c-array)))
194
195 #+(and mcl (not openmcl))
196 (setq *type-conversion-list*
197      '((* . :pointer) (:void . :void)
198        (:short . :short) (:unsigned-short . :unsigned-short)
199        (:pointer-void . :pointer)
200        (:cstring . :string)
201        (:char . :character)
202        (:unsigned-char . :unsigned-byte)
203        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
204        (:int . :long) (:unsigned-int . :unsigned-long)
205        (:long . :long) (:unsigned-long . :unsigned-long)
206        (:float . :single-float) (:double . :double-float)
207        (:array . :array)))
208
209 #+openmcl
210 (setq *type-conversion-list*
211      '((* . :address) (:void . :void)
212        (:short . :short) (:unsigned-short . :unsigned-short)
213        (:pointer-void . :address)
214        (:cstring . :address)
215        (:char . :signed-char)
216        (:unsigned-char . :unsigned-char)
217        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
218        (:int . :int) (:unsigned-int . :unsigned-int)
219        (:long . :long) (:unsigned-long . :unsigned-long)
220        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
221        (:float . :single-float) (:double . :double-float)
222        (:array . :array)))
223
224 (dolist (type *type-conversion-list*)
225   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
226
227 #+(or cmu sbcl scl)
228 (dolist (type *cmu-sbcl-def-type-list*)
229   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
230
231 (defun basic-convert-from-uffi-type (type)
232   (let ((found-type (gethash type +type-conversion-hash+)))
233     (if found-type
234         found-type
235       #-mcl type
236       #+mcl (keyword type))))
237
238 (defun %convert-from-uffi-type (type context)
239   "Converts from a uffi type to an implementation specific type"
240   (if (atom type)
241       (cond
242        #+(or allegro cormanlisp)
243        ((and (or (eq context :routine) (eq context :return))
244              (eq type :cstring))
245         (setq type '((* :char) integer)))
246        #+(or cmu sbcl scl)
247        ((eq context :type)
248         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
249           (if cmu-type
250               cmu-type
251               (basic-convert-from-uffi-type type))))
252        #+lispworks
253        ((and (eq context :return)
254              (eq type :cstring))
255         (basic-convert-from-uffi-type :cstring-returning))
256        #+(and mcl (not openmcl))
257        ((and (eq type :void) (eq context :return)) nil)
258        (t
259         (basic-convert-from-uffi-type type)))
260     (let ((sub-type (car type)))
261       (cond
262        ((eq sub-type 'cl:quote)
263         (convert-from-uffi-type (cadr type) context))
264        #+sbcl
265        ((and (eq sub-type :array)
266              (or (eq context :declare) (eq context :routine))
267              (= 2 (length type)))
268         `(sb-alien:array ,(%convert-from-uffi-type (second type) context) nil))
269        #+cmu
270        ((and (eq sub-type :array)
271              (or (eq context :declare) (eq context :routine))
272              (= 2 (length type)))
273         `(alien:array ,(%convert-from-uffi-type (second type) context) nil))
274        ((eq sub-type :struct-pointer)
275         #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
276         #-mcl (%convert-from-uffi-type (list '* (second type)) :struct)
277         )
278         ((eq sub-type :struct)
279          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
280          #-mcl (%convert-from-uffi-type (second type) :struct)
281          )
282         (t
283          (cons (%convert-from-uffi-type (first type) context) 
284                (%convert-from-uffi-type (rest type) context)))))))
285
286 (defun convert-from-uffi-type (type context)
287   (let ((result (%convert-from-uffi-type type context)))
288     (cond
289      ((atom result) result)
290      #+openmcl
291      ((eq (car result) :address)
292       (if (eq context :struct)
293           (append '(:*) (cdr result))
294         :address))
295      #+(and mcl (not openmcl))
296      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
297      (t result))))
298
299 (eval-when (:compile-toplevel :load-toplevel :execute)
300   (when (char= #\a (schar (symbol-name '#:a) 0))
301     (pushnew :uffi-lowercase-reader *features*))
302   (when (not (string= (symbol-name '#:a)
303                       (symbol-name '#:A)))
304     (pushnew :uffi-case-sensitive *features*)))
305
306 (defun make-lisp-name (name)
307   (let ((converted (substitute #\- #\_ name)))
308      (intern 
309       #+uffi-case-sensitive converted
310       #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
311       #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
312
313 (eval-when (:compile-toplevel :load-toplevel :execute)
314   (setq cl:*features* (delete :uffi-lowercase-reader *features*))
315   (setq cl:*features* (delete :uffi-case-sensitive *features*)))