r10123: fix for openmcl
[uffi.git] / src / aggregates.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          aggregates.lisp
6 ;;;; Purpose:       UFFI source to handle aggregate 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 (defmacro def-enum (enum-name args &key (separator-string "#"))
22   "Creates a constants for a C type enum list, symbols are created
23 in the created in the current package. The symbol is the concatenation
24 of the enum-name name, separator-string, and field-name"
25   (let ((counter 0)
26         (cmds nil)
27         (constants nil))
28     (declare (fixnum counter))
29     (dolist (arg args)
30       (let ((name (if (listp arg) (car arg) arg))
31             (value (if (listp arg) 
32                        (prog1
33                            (setq counter (cadr arg))
34                          (incf counter))
35                      (prog1 
36                          counter
37                        (incf counter)))))
38         (setq name (intern (concatenate 'string
39                              (symbol-name enum-name)
40                              separator-string
41                              (symbol-name name))))
42         (push `(uffi:def-constant ,name ,value) constants)))
43     (setf cmds (append '(progn)
44                        #+allegro `((ff:def-foreign-type ,enum-name :int))
45                        #+lispworks `((fli:define-c-typedef ,enum-name :int))
46                        #+(or cmu scl) `((alien:def-alien-type ,enum-name alien:signed))
47                        #+sbcl `((sb-alien:define-alien-type ,enum-name sb-alien:signed))
48                        #+(and mcl (not openmcl)) `((def-mcl-type ,enum-name :integer))
49                        #+openmcl `((ccl::def-foreign-type ,enum-name :int))
50                        (nreverse constants)))
51     cmds))
52
53
54 (defmacro def-array-pointer (name-array type)
55   #+allegro
56   `(ff:def-foreign-type ,name-array 
57     (:array ,(convert-from-uffi-type type :array)))
58   #+lispworks
59   `(fli:define-c-typedef ,name-array
60     (:c-array ,(convert-from-uffi-type type :array)))
61   #+(or cmu scl)
62   `(alien:def-alien-type ,name-array 
63     (* ,(convert-from-uffi-type type :array)))
64   #+sbcl
65   `(sb-alien:define-alien-type ,name-array 
66     (* ,(convert-from-uffi-type type :array)))
67   #+(and mcl (not openmcl))
68   `(def-mcl-type ,name-array '(:array ,type))
69   #+openmcl
70   `(ccl::def-foreign-type ,name-array (:array ,(convert-from-uffi-type type :array)))
71   )
72
73 (defun process-struct-fields (name fields &optional (variant nil))
74   (let (processed)
75     (dolist (field fields)
76       (let* ((field-name (car field))
77              (type (cadr field))
78              (def (append (list field-name)
79                           (if (eq type :pointer-self)
80                               #+(or cmu scl) `((* (alien:struct ,name)))
81                               #+sbcl `((* (sb-alien:struct ,name)))
82                               #+mcl `((:* (:struct ,name)))
83                               #+lispworks `((:pointer ,name))
84                               #-(or cmu sbcl scl mcl lispworks) `((* ,name))
85                               `(,(convert-from-uffi-type type :struct))))))
86         (if variant
87             (push (list def) processed)
88           (push def processed))))
89     (nreverse processed)))
90         
91             
92 (defmacro def-struct (name &rest fields)
93   #+(or cmu scl)
94   `(alien:def-alien-type ,name (alien:struct ,name ,@(process-struct-fields name fields)))
95   #+sbcl
96   `(sb-alien:define-alien-type ,name (sb-alien:struct ,name ,@(process-struct-fields name fields)))
97   #+allegro
98   `(ff:def-foreign-type ,name (:struct ,@(process-struct-fields name fields)))
99   #+lispworks
100   `(fli:define-c-struct ,name ,@(process-struct-fields name fields))
101   #+(and mcl (not openmcl))
102   `(ccl:defrecord ,name ,@(process-struct-fields name fields))
103   #+openmcl
104   `(ccl::def-foreign-type
105     nil 
106     (:struct ,name ,@(process-struct-fields name fields)))
107   )
108
109
110 (defmacro get-slot-value (obj type slot)
111   #+(or lispworks cmu sbcl scl) (declare (ignore type))
112   #+allegro
113   `(ff:fslot-value-typed ,type :c ,obj ,slot)
114   #+lispworks
115   `(fli:foreign-slot-value ,obj ,slot)
116   #+(or cmu scl)
117   `(alien:slot ,obj ,slot)
118   #+sbcl
119   `(sb-alien:slot ,obj ,slot)
120   #+mcl
121   `(ccl:pref ,obj ,(intern (concatenate 'string (symbol-name type) "." (symbol-name slot))
122                           :keyword))
123   )
124
125 #+mcl
126 (defmacro set-slot-value (obj type slot value) ;use setf to set values
127   `(setf (ccl:pref ,obj ,(read-from-string (format nil ":~a.~a" (keyword type) (keyword slot)))) ,value))
128
129 #+mcl
130 (defsetf get-slot-value set-slot-value)
131
132
133 (defmacro get-slot-pointer (obj type slot)
134   #+(or lispworks cmu sbcl scl) (declare (ignore type))
135   #+allegro
136   `(ff:fslot-value-typed ,type :c ,obj ,slot)
137   #+lispworks
138   `(fli:foreign-slot-pointer ,obj ,slot)
139   #+(or cmu scl)
140   `(alien:slot ,obj ,slot)
141   #+sbcl
142   `(sb-alien:slot ,obj ,slot)
143   #+(and mcl (not openmcl))
144   `(ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl:field-info ,type ,slot))))
145   #+openmcl
146   `(let ((field (ccl::%find-foreign-record-type-field ,type ,slot)))
147      (ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl::foreign-record-field-offset field)))))  
148 )
149
150 ;; necessary to eval at compile time for openmcl to compile convert-from-foreign-usb8
151 ;; below
152 (eval-when (:compile-toplevel :load-toplevel :execute)
153   ;; so we could allow '(:array :long) or deref with other type like :long only
154   #+mcl
155   (defun array-type (type)
156     (let ((result type))
157       (when (listp type)
158         (let ((type-list (if (eq (car type) 'quote) (nth 1 type) type)))
159           (when (and (listp type-list) (eq (car type-list) :array))
160             (setf result (cadr type-list)))))
161       result))
162   
163   
164   (defmacro deref-array (obj type i)
165     "Returns a field from a row"
166     #+(or lispworks cmu sbcl scl) (declare (ignore type))
167     #+(or cmu scl)  `(alien:deref ,obj ,i)
168     #+sbcl `(sb-alien:deref ,obj ,i)
169     #+lispworks `(fli:dereference ,obj :index ,i :copy-foreign-object nil)
170     #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :type)) :c ,obj ,i)
171     #+openmcl
172     (let* ((array-type (array-type type))
173            (local-type (convert-from-uffi-type array-type :allocation))
174            (element-size-in-bits (ccl::%foreign-type-or-record-size local-type :bits)))
175       (ccl::%foreign-access-form
176        obj
177        (ccl::%foreign-type-or-record local-type)
178        `(* ,i ,element-size-in-bits)
179        nil))
180     #+(and mcl (not openmcl))
181     (let* ((array-type (array-type type))
182            (local-type (convert-from-uffi-type array-type :allocation))
183            (accessor (first (macroexpand `(ccl:pref obj ,local-type)))))
184       `(,accessor
185         ,obj
186         (* (the fixnum ,i) ,(size-of-foreign-type local-type))))
187     ))
188   
189 ; this expands to the %set-xx functions which has different params than %put-xx
190 #+(and mcl (not openmcl))
191 (defmacro deref-array-set (obj type i value)
192   (let* ((array-type (array-type type))
193          (local-type (convert-from-uffi-type array-type :allocation))
194          (accessor (first (macroexpand `(ccl:pref obj ,local-type))))
195          (settor (first (macroexpand `(setf (,accessor obj ,local-type) value)))))
196     `(,settor 
197       ,obj
198       (* (the fixnum ,i) ,(size-of-foreign-type local-type)) 
199       ,value)))
200
201 #+(and mcl (not openmcl))
202 (defsetf deref-array deref-array-set)
203
204 (defmacro def-union (name &rest fields)
205   #+allegro
206   `(ff:def-foreign-type ,name (:union ,@(process-struct-fields name fields)))
207   #+lispworks
208   `(fli:define-c-union ,name ,@(process-struct-fields name fields))
209   #+(or cmu scl)
210   `(alien:def-alien-type ,name (alien:union ,name ,@(process-struct-fields name fields)))
211   #+sbcl
212   `(sb-alien:define-alien-type ,name (sb-alien:union ,name ,@(process-struct-fields name fields)))
213   #+(and mcl (not openmcl))
214   `(ccl:defrecord ,name (:variant ,@(process-struct-fields name fields t)))
215   #+openmcl
216   `(ccl::def-foreign-type nil 
217                           (:union ,name ,@(process-struct-fields name fields)))
218 )
219
220
221 #-(or sbcl cmu)
222 (defun convert-from-foreign-usb8 (s len)
223   (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
224            (fixnum len))
225   (let ((a (make-array len :element-type '(unsigned-byte 8))))
226     (dotimes (i len a)
227       (declare (fixnum i))
228       (setf (aref a i) (uffi:deref-array s '(:array :unsigned-byte) i)))))
229
230 #+sbcl
231 (defun convert-from-foreign-usb8 (sap len)
232   (declare (type sb-sys:system-area-pointer sap))
233   (locally
234       (declare (optimize (speed 3) (safety 0)))
235     (let ((result (make-array len :element-type '(unsigned-byte 8))))
236       (sb-kernel:copy-from-system-area sap 0
237                                        result (* sb-vm:vector-data-offset
238                                                  sb-vm:n-word-bits)
239                                        (* len sb-vm:n-byte-bits))
240       result)))
241
242 #+cmu
243 (defun convert-from-foreign-usb8 (sap len)
244   (declare (type system:system-area-pointer sap))
245   (locally
246       (declare (optimize (speed 3) (safety 0)))
247     (let ((result (make-array len :element-type '(unsigned-byte 8))))
248       (kernel:copy-from-system-area sap 0
249                                     result (* vm:vector-data-offset
250                                               vm:word-bits)
251                                     (* len vm:byte-bits))
252       result)))