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