Version 1.8.4: Impoved detection of long-long type for CMUCL
[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 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package #:uffi)
15
16 #+(or openmcl digitool)
17 (defvar *keyword-package* (find-package "KEYWORD"))
18
19 #+(or openmcl digitool)
20 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
21 ; So this provides a function to convert any quoted symbols to keywords.
22 (defun keyword (obj)
23   (cond ((keywordp obj)
24          obj)
25         ((null obj)
26          nil)
27         ((symbolp obj)
28          (intern (symbol-name obj) *keyword-package*))
29         ((and (listp obj) (eq (car obj) 'cl:quote))
30          (keyword (cadr obj)))
31         ((stringp obj)
32          (intern obj *keyword-package*))
33         (t
34          obj)))
35
36 ; Wrapper for unexported function we have to use
37 #+digitool
38 (defmacro def-mcl-type (name type)
39   `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
40
41 (defmacro def-constant (name value &key (export nil))
42   "Macro to define a constant and to export it"
43   `(eval-when (:compile-toplevel :load-toplevel :execute)
44      (defconstant ,name ,value)
45      ,(when export (list 'export `(quote ,name)))
46     ',name))
47
48 (defmacro def-type (name type)
49   "Generates a (deftype) statement for CL. Currently, only CMUCL
50 supports takes advantage of this optimization."
51   #+(or lispworks allegro openmcl digitool cormanlisp)  (declare (ignore type))
52   #+(or lispworks allegro openmcl digitool cormanlisp) `(deftype ,name () t)
53   #+(or cmu scl)
54   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
55   #+sbcl
56   `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
57   )
58
59 (defmacro null-char-p (val)
60   "Returns T if character is NULL"
61   `(zerop ,val))
62
63 (defmacro def-foreign-type (name type)
64   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
65   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
66   #+(or cmu scl) `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
67   #+sbcl `(sb-alien:define-alien-type ,name ,(convert-from-uffi-type type :type))
68   #+cormanlisp `(ct:defctype ,name ,(convert-from-uffi-type type :type))
69   #+(or openmcl digitool)
70   (let ((mcl-type (convert-from-uffi-type type :type)))
71     (unless (or (keywordp mcl-type) (consp mcl-type))
72       (setf mcl-type `(quote ,mcl-type)))
73     #+digitool
74     `(def-mcl-type ,(keyword name) ,mcl-type)
75     #+openmcl
76     `(ccl::def-foreign-type ,(keyword name) ,mcl-type))
77   )
78
79 (eval-when (:compile-toplevel :load-toplevel :execute)
80   (defvar +type-conversion-hash+ (make-hash-table :size 20 :test #'eq))
81   #+(or cmu sbcl scl) (defvar *cmu-def-type-hash*
82                         (make-hash-table :size 20 :test #'eq))
83   )
84
85 #+(or cmu scl)
86 (defvar *cmu-sbcl-def-type-list*
87     '((:char . (alien:signed 8))
88       (:unsigned-char . (alien:unsigned 8))
89       (:byte . (alien:signed 8))
90       (:unsigned-byte . (alien:unsigned 8))
91       (:short . (alien:signed 16))
92       (:unsigned-short . (alien:unsigned 16))
93       (:int . (alien:signed 32))
94       (:unsigned-int . (alien:unsigned 32))
95       #-x86-64 (:long . (alien:signed 32))
96       #-x86-64 (:unsigned-long . (alien:unsigned 32))
97       #+x86-64 (:long . (alien:signed 64))
98       #+x86-64 (:unsigned-long . (alien:unsigned 64))
99       (:long-long (alien:signed 64))
100       (:unsigned-long-long (alien:unsigned 64))
101       (:float . alien:single-float)
102       (:double . alien:double-float)
103       (:void . t)
104       )
105   "Conversions in CMUCL for def-foreign-type are different than in def-function")
106
107 #+sbcl
108 (defvar *cmu-sbcl-def-type-list*
109     '((:char . (sb-alien:signed 8))
110       (:unsigned-char . (sb-alien:unsigned 8))
111       (:byte . (sb-alien:signed 8))
112       (:unsigned-byte . (sb-alien:unsigned 8))
113       (:short . (sb-alien:signed 16))
114       (:unsigned-short . (sb-alien:unsigned 16))
115       (:int . (sb-alien:signed 32))
116       (:unsigned-int . (sb-alien:unsigned 32))
117       #-x86-64 (:long . (sb-alien:signed 32))
118       #-x86-64 (:unsigned-long . (sb-alien:unsigned 32))
119       #+x86-64 (:long . (sb-alien:signed 64))
120       #+x86-64 (:unsigned-long . (sb-alien:unsigned 64))
121       (:long-long (sb-alien:signed 64))
122       (:unsigned-long-long (sb-alien:unsigned 64))
123       (:float . sb-alien:single-float)
124       (:double . sb-alien:double-float)
125       (:void . t)
126       )
127   "Conversions in SBCL for def-foreign-type are different than in def-function")
128
129 (defvar *type-conversion-list* nil)
130
131 #+(or cmu scl)
132 (setq *type-conversion-list*
133     '((* . *) (:void . c-call:void)
134       (:pointer-void . (* t))
135       (:cstring . c-call:c-string)
136       (:char . c-call:char)
137       (:unsigned-char . (alien:unsigned 8))
138       (:byte . (alien:signed 8))
139       (:unsigned-byte . (alien:unsigned 8))
140       (:short . c-call:short)
141       (:unsigned-short . c-call:unsigned-short)
142       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
143       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
144       #+#.(cl:if (cl:find-symbol (cl:string '#:long-long)
145                                  (cl:string '#:c-call))
146                  '(and) '(or))
147       (:long-long . c-call:long-long)
148       #+#.(cl:if (cl:find-symbol (cl:string '#:unsigned-long-long)
149                                  (cl:string '#:c-call))
150                  '(and) '(or))
151       (:unsigned-long-long . c-call:unsigned-long-long)
152       (:float . c-call:float) (:double . c-call:double)
153       (:array . alien:array)))
154
155 #+sbcl
156 (setq *type-conversion-list*
157     '((* . *) (:void . sb-alien:void)
158       (:pointer-void . (* t))
159       #-sb-unicode(:cstring . sb-alien:c-string)
160       #+sb-unicode(:cstring . sb-alien:utf8-string)
161       (:char . sb-alien:char)
162       (:unsigned-char . (sb-alien:unsigned 8))
163       (:byte . (sb-alien:signed 8))
164       (:unsigned-byte . (sb-alien:unsigned 8))
165       (:short . sb-alien:short)
166       (:unsigned-short . sb-alien:unsigned-short)
167       (:int . sb-alien:int) (:unsigned-int . sb-alien:unsigned-int)
168       (:long . sb-alien:long) (:unsigned-long . sb-alien:unsigned-long)
169       (:long-long . sb-alien:long-long)
170       (:unsigned-long-long . sb-alien:unsigned-long-long)
171       (:float . sb-alien:float) (:double . sb-alien:double)
172       (:array . sb-alien:array)))
173
174 #+(or allegro cormanlisp)
175 (setq *type-conversion-list*
176     '((* . *) (:void . :void)
177       (:short . :short)
178       (:pointer-void . (* :void))
179       (:cstring . (* :unsigned-char))
180       (:byte . :char)
181       (:unsigned-byte . :unsigned-char)
182       (:char . :char)
183       (:unsigned-char . :unsigned-char)
184       (:int . :int) (:unsigned-int . :unsigned-int)
185       (:long . :long) (:unsigned-long . :unsigned-long)
186       (:float . :float) (:double . :double)
187       (:array . :array)))
188
189 #+(or allegro cormanlisp)
190 (push 'uffi:no-long-long cl:*features*)
191
192 #+lispworks
193 (setq *type-conversion-list*
194     '((* . :pointer) (:void . :void)
195       (:short . :short)
196       (:pointer-void . (:pointer :void))
197       (:cstring . (:reference-pass (:ef-mb-string :external-format
198                                     (:latin-1 :eol-style :lf))
199                    :allow-null t))
200       (:cstring-returning . (:reference (:ef-mb-string :external-format
201                                          (:latin-1 :eol-style :lf))
202                              :allow-null t))
203       (:byte . :byte)
204       (:unsigned-byte . (:unsigned :byte))
205       (:char . :char)
206       (:unsigned-char . (:unsigned :char))
207       (:int . :int) (:unsigned-int . (:unsigned :int))
208       (:long . :long) (:unsigned-long . (:unsigned :long))
209       #+lispworks6 (:long-long . :int64)
210       #+lispworks6 (:unsigned-long-long . :uint64)
211       (:float . :float) (:double . :double)
212       (:array . :c-array)))
213
214 #+(and lispworks (not lispworks6))
215 (push 'uffi:no-long-long cl:*features*)
216
217 #+digitool
218 (setq *type-conversion-list*
219      '((* . :pointer) (:void . :void)
220        (:short . :short) (:unsigned-short . :unsigned-short)
221        (:pointer-void . :pointer)
222        (:cstring . :string)
223        (:char . :character)
224        (:unsigned-char . :unsigned-byte)
225        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
226        (:int . :long) (:unsigned-int . :unsigned-long)
227        (:long . :long) (:unsigned-long . :unsigned-long)
228        (:float . :single-float) (:double . :double-float)
229        (:array . :array)))
230 #+digitool
231 (push 'uffi:no-long-long cl:*features*)
232
233 #+openmcl
234 (setq *type-conversion-list*
235      '((* . :address) (:void . :void)
236        (:short . :short) (:unsigned-short . :unsigned-short)
237        (:pointer-void . :address)
238        (:cstring . :address)
239        (:char . :signed-char)
240        (:unsigned-char . :unsigned-char)
241        (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
242        (:int . :int) (:unsigned-int . :unsigned-int)
243        (:long . :long) (:unsigned-long . :unsigned-long)
244        (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
245        (:float . :single-float) (:double . :double-float)
246        (:array . :array)))
247
248 (dolist (type *type-conversion-list*)
249   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
250
251 #+(or cmu sbcl scl)
252 (dolist (type *cmu-sbcl-def-type-list*)
253   (setf (gethash (car type) *cmu-def-type-hash*) (cdr type)))
254
255 (defun basic-convert-from-uffi-type (type)
256   (let ((found-type (gethash type +type-conversion-hash+)))
257     (if found-type
258         found-type
259       #-(or openmcl digitool) type
260       #+(or openmcl digitool) (keyword type))))
261
262 (defun %convert-from-uffi-type (type context)
263   "Converts from a uffi type to an implementation specific type"
264   (if (atom type)
265       (cond
266        #+(or allegro cormanlisp)
267        ((and (or (eq context :routine) (eq context :return))
268              (eq type :cstring))
269         (setq type '((* :char) integer)))
270        #+(or cmu sbcl scl)
271        ((eq context :type)
272         (let ((cmu-type (gethash type *cmu-def-type-hash*)))
273           (if cmu-type
274               cmu-type
275               (basic-convert-from-uffi-type type))))
276        #+lispworks
277        ((and (eq context :return)
278              (eq type :cstring))
279         (basic-convert-from-uffi-type :cstring-returning))
280        #+digitool
281        ((and (eq type :void) (eq context :return)) nil)
282        (t
283         (basic-convert-from-uffi-type type)))
284     (let ((sub-type (car type)))
285       (case sub-type
286         (cl:quote
287          (convert-from-uffi-type (cadr type) context))
288         (:struct-pointer
289          #+(or openmcl digitool) `(:* (:struct ,(%convert-from-uffi-type (cadr type) :struct)))
290          #-(or openmcl digitool) (%convert-from-uffi-type (list '* (cadr type)) :struct)
291          )
292         (:struct
293          #+(or openmcl digitool) `(:struct ,(%convert-from-uffi-type (cadr type) :struct))
294          #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :struct)
295          )
296        (:union
297         #+(or openmcl digitool) `(:union ,(%convert-from-uffi-type (cadr type) :union))
298         #-(or openmcl digitool) (%convert-from-uffi-type (cadr type) :union)
299         )
300        (t
301         (cons (%convert-from-uffi-type (first type) context)
302               (%convert-from-uffi-type (rest type) context)))))))
303
304 (defun convert-from-uffi-type (type context)
305   (let ((result (%convert-from-uffi-type type context)))
306     (cond
307      ((atom result) result)
308      #+openmcl
309      ((eq (car result) :address)
310       (if (eq context :struct)
311           (append '(:*) (cdr result))
312         :address))
313      #+digitool
314      ((and (eq (car result) :pointer) (eq context :allocation) :pointer))
315      (t result))))
316
317 (eval-when (:compile-toplevel :load-toplevel :execute)
318   (when (char= #\a (schar (symbol-name '#:a) 0))
319     (pushnew :uffi-lowercase-reader *features*))
320   (when (not (string= (symbol-name '#:a)
321                       (symbol-name '#:A)))
322     (pushnew :uffi-case-sensitive *features*)))
323
324 (defun make-lisp-name (name)
325   (let ((converted (substitute #\- #\_ name)))
326      (intern
327       #+uffi-case-sensitive converted
328       #+(and (not uffi-lowercase-reader) (not uffi-case-sensitive)) (string-upcase converted)
329       #+(and uffi-lowercase-reader (not uffi-case-sensitive)) (string-downcase converted))))
330
331 (eval-when (:compile-toplevel :load-toplevel :execute)
332   (setq cl:*features* (delete :uffi-lowercase-reader *features*))
333   (setq cl:*features* (delete :uffi-case-sensitive *features*)))