ac4116ae8fd0ad9f0556d3ed4beb6db2f408cea4
[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       #-x86-64 (:long . (alien:signed 32))
101       #-x86-64 (:unsigned-long . (alien:unsigned 32))
102       #+x86-64 (:long . (alien:signed 64))
103       #+x86-64 (:unsigned-long . (alien:unsigned 64))
104       (:float . alien:single-float)
105       (:double . alien:double-float)
106       (:void . t)
107       )
108   "Conversions in CMUCL for def-foreign-type are different than in def-function")
109
110 #+sbcl
111 (defvar *cmu-sbcl-def-type-list*
112     '((:char . (sb-alien:signed 8))
113       (:unsigned-char . (sb-alien:unsigned 8))
114       (:byte . (sb-alien:signed 8))
115       (:unsigned-byte . (sb-alien:unsigned 8))
116       (:short . (sb-alien:signed 16))
117       (:unsigned-short . (sb-alien:unsigned 16))
118       (:int . (sb-alien:signed 32))
119       (:unsigned-int . (sb-alien:unsigned 32))
120       #-x86-64 (:long . (sb-alien:signed 32))
121       #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
122       #+x86-64 (:long . (sb-alien:signed 64))
123       #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
124       (:float . sb-alien:single-float)
125       (:double . sb-alien:double-float)
126       (:void . t)
127       )
128   "Conversions in SBCL for def-foreign-type are different than in def-function")
129
130 (defvar *type-conversion-list* nil)
131
132 #+(or cmu scl)
133 (setq *type-conversion-list*
134     '((* . *) (:void . c-call:void) 
135       (:pointer-void . (* t))
136       (:cstring . c-call:c-string)
137       (:char . c-call:char) 
138       (:unsigned-char . (alien:unsigned 8))
139       (:byte . (alien:signed 8))
140       (:unsigned-byte . (alien:unsigned 8))
141       (:short . c-call:short)
142       (:unsigned-short . c-call:unsigned-short)
143       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
144       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
145       (:float . c-call:float) (:double . c-call:double)
146       (:array . alien:array)))
147
148 #+sbcl
149 (setq *type-conversion-list*
150     '((* . *) (:void . sb-alien:void) 
151       (:pointer-void . (* t))
152       #-sb-unicode(:cstring . sb-alien:c-string)
153       #+sb-unicode(:cstring . sb-alien:utf8-string)
154       (:char . sb-alien:char) 
155       (:unsigned-char . (sb-alien:unsigned 8))
156       (:byte . (sb-alien:signed 8))
157       (:unsigned-byte . (sb-alien:unsigned 8))
158       (:short . sb-alien:short)
159       (:unsigned-short . sb-alien:unsigned-short)
160       (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int) 
161       (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
162       (:float . sb-alien:float) (:double . sb-alien:double)
163       (:array . sb-alien:array)))
164
165 #+(or allegro cormanlisp)
166 (setq *type-conversion-list*
167     '((* . *) (:void . :void)
168       (:short . :short)
169       (:pointer-void . (* :void))
170       (:cstring . (* :unsigned-char))
171       (:byte . :char)
172       (:unsigned-byte . :unsigned-char)
173       (:char . :char)
174       (:unsigned-char . :unsigned-char)
175       (:int . :int) (:unsigned-int . :unsigned-int) 
176       (:long . :long) (:unsigned-long . :unsigned-long)
177       (:float . :float) (:double . :double)
178       (:array . :array)))
179
180 #+lispworks
181 (setq *type-conversion-list*
182     '((* . :pointer) (:void . :void) 
183       (:short . :short)
184       (:pointer-void . (:pointer :void))
185       (:cstring . (:reference-pass (:ef-mb-string :external-format
186                                     (:latin-1 :eol-style :lf))
187                    :allow-null t))
188       (:cstring-returning . (:reference (:ef-mb-string :external-format
189                                          (:latin-1 :eol-style :lf))
190                              :allow-null t))
191       (:byte . :byte)
192       (:unsigned-byte . (:unsigned :byte))
193       (:char . :char)
194       (:unsigned-char . (:unsigned :char))
195       (:int . :int) (:unsigned-int . (:unsigned :int))
196       (:long . :long) (:unsigned-long . (:unsigned :long))
197       (:float . :float) (:double . :double)
198       (:array . :c-array)))
199
200 #+(and mcl (not openmcl))
201 (setq *type-conversion-list*
202      '((* . :pointer) (:void . :void)
203        (:short . :short) (:unsigned-short . :unsigned-short)
204        (:pointer-void . :pointer)
205        (:cstring . :string)
206        (:char . :character)
207        (:unsigned-char . :unsigned-byte)
208        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
209        (:int . :long) (:unsigned-int . :unsigned-long)
210        (:long . :long) (:unsigned-long . :unsigned-long)
211        (:float . :single-float) (:double . :double-float)
212        (:array . :array)))
213
214 #+openmcl
215 (setq *type-conversion-list*
216      '((* . :address) (:void . :void)
217        (:short . :short) (:unsigned-short . :unsigned-short)
218        (:pointer-void . :address)
219        (:cstring . :address)
220        (:char . :signed-char)
221        (:unsigned-char . :unsigned-char)
222        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
223        (:int . :int) (:unsigned-int . :unsigned-int)
224        (:long . :long) (:unsigned-long . :unsigned-long)
225        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
226        (:float . :single-float) (:double . :double-float)
227        (:array . :array)))
228
229 (dolist (type *type-conversion-list*)
230   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
231
232 #+(or cmu sbcl scl)
233 (dolist (type *cmu-sbcl-def-type-list*)
234   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
235
236 (defun basic-convert-from-uffi-type (type)
237   (let ((found-type (gethash type +type-conversion-hash+)))
238     (if found-type
239         found-type
240       #-mcl type
241       #+mcl (keyword type))))
242
243 (defun %convert-from-uffi-type (type context)
244   "Converts from a uffi type to an implementation specific type"
245   (if (atom type)
246       (cond
247        #+(or allegro cormanlisp)
248        ((and (or (eq context :routine) (eq context :return))
249              (eq type :cstring))
250         (setq type '((* :char) integer)))
251        #+(or cmu sbcl scl)
252        ((eq context :type)
253         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
254           (if cmu-type
255               cmu-type
256               (basic-convert-from-uffi-type type))))
257        #+lispworks
258        ((and (eq context :return)
259              (eq type :cstring))
260         (basic-convert-from-uffi-type :cstring-returning))
261        #+(and mcl (not openmcl))
262        ((and (eq type :void) (eq context :return)) nil)
263        (t
264         (basic-convert-from-uffi-type type)))
265     (let ((sub-type (car type)))
266       (case sub-type
267         (cl:quote
268          (convert-from-uffi-type (cadr type) context))
269         (:struct-pointer
270          #+mcl `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
271          #-mcl (%convert-from-uffi-type (list '* (cadr type)) :struct)
272          )
273         (:struct
274          #+mcl `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
275          #-mcl (%convert-from-uffi-type (cadr type) :struct)
276          )
277        (:union
278         #+mcl `(:union ,(%convert-from-uffi-type (cadr type) :union))
279         #-mcl (%convert-from-uffi-type (cadr type) :union)
280         )
281        (t
282         (cons (%convert-from-uffi-type (first type) context) 
283               (%convert-from-uffi-type (rest type) context)))))))
284
285 (defun convert-from-uffi-type (type context)
286   (let ((result (%convert-from-uffi-type type context)))
287     (cond
288      ((atom result) result)
289      #+openmcl
290      ((eq (car result) :address)
291       (if (eq context :struct)
292           (append '(:*) (cdr result))
293         :address))
294      #+(and mcl (not openmcl))
295      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
296      (t result))))
297
298 (eval-when (:compile-toplevel :load-toplevel :execute)
299   (when (char= #\a (schar (symbol-name '#:a) 0))
300     (pushnew :uffi-lowercase-reader *features*))
301   (when (not (string= (symbol-name '#:a)
302                       (symbol-name '#:A)))
303     (pushnew :uffi-case-sensitive *features*)))
304
305 (defun make-lisp-name (name)
306   (let ((converted (substitute #\- #\_ name)))
307      (intern 
308       #+uffi-case-sensitive converted
309       #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
310       #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
311
312 (eval-when (:compile-toplevel :load-toplevel :execute)
313   (setq cl:*features* (delete :uffi-lowercase-reader *features*))
314   (setq cl:*features* (delete :uffi-case-sensitive *features*)))