r1543: *** empty log message ***
[uffi.git] / src / primitives.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          primitives.cl
6 ;;;; Purpose:       UFFI source to handle immediate types
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; Copyright (c) 2002 Kevin M. Rosenberg
11 ;;;;
12 ;;;; $Id: primitives.cl,v 1.1 2002/03/10 21:48:50 kevin Exp $
13 ;;;;
14 ;;;; This file is part of the UFFI. 
15 ;;;;
16 ;;;; UFFI is free software; you can redistribute it and/or modify
17 ;;;; it under the terms of the GNU General Public License (version 2) as
18 ;;;; published by the Free Software Foundation.
19 ;;;;
20 ;;;; UFFI is distributed in the hope that it will be useful,
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;;;; GNU General Public License for more details.
24 ;;;;
25 ;;;; You should have received a copy of the GNU General Public License
26 ;;;; along with UFFI; if not, write to the Free Software
27 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 ;;;; *************************************************************************
29
30 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
31 (in-package :uffi)
32
33 (defmacro def-constant (name value)
34   "Macro to define a constant and to export it"
35   `(eval-when (:compile-toplevel :load-toplevel :execute)
36      (defconstant ,name ,value)
37      (export ',name)))
38
39 (defmacro uffi-declare (type name)
40   "Generates a declare statement for CL. Currently, only CMUCL
41 supports this."
42   #+(or lispworks allegro)
43   (declare (ignore type name))
44   #+cmu
45   `(declare (type (alien ,type) ,name))
46   )
47
48 (defmacro slot-type (type)
49   #+(or lispworks allegro)
50   (declare (ignore type))
51   #+(or lispworks allegro)
52   t
53   #+cmu `'(alien:alien ,type))
54
55 (defmacro null-char-p (val)
56   `(if (or (eql ,val 0)
57            (eq ,val #\Null))
58       t
59     nil))
60
61       
62 (defmacro def-type (name type)
63   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
64   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
65   #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
66   )
67
68 (eval-when (:compile-toplevel :load-toplevel :execute)
69   (defvar +type-conversion-hash+ (make-hash-table :size 20))
70   #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
71   )
72
73 #+cmu
74 (defconstant +cmu-def-type-list+
75     '((:char . (alien:signed 8))
76       (:unsigned-char . (alien:unsigned 8))
77       (:byte . (alien:unsigned 8))
78       (:short . (alien:signed 16))
79       (:unsigned-short . (alien:unsigned 16))
80       (:int . (alien:signed 32))
81       (:unsigned-int . (alien:unsigned 32))
82       (:long . (alien:signed 32))
83       (:unsigned-long . (alien:unsigned 32))
84       (:float . alien:single-float)
85       (:double . alien:double-float)
86       ))
87
88 #+cmu
89 (defconstant +type-conversion-list+
90     '((* . *) (:void . c-call:void) 
91       (:short . c-call:short)
92       (:pointer-void . (* t))
93       (:cstring . c-call:cstring)
94       (:char . c-call:char) 
95       (:unsigned-char . (alien:unsigned 8))
96       (:byte . (alien:unsigned 8))
97       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
98       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
99       (:float . c-call:float) (:double . c-call:double)
100       (:array . alien:array)))
101 #+allegro
102 (defconstant +type-conversion-list+
103     '((* . *) (:void . :void)
104       (:short . :short)
105       (:pointer-void . (* :void))
106       (:cstring . (* :char))
107       (:char . :char)
108       (:unsigned-char . :unsigned-char)
109       (:byte . :byte)
110       (:int . :int) (:unsigned-int . :unsigned-int) 
111       (:long . :long) (:unsigned-long . :unsigned-long)
112       (:float . :float) (:double . :double)
113       (:array . :array)))
114 #+lispworks
115 (defconstant +type-conversion-list+
116     '((* . :pointer) (:void . :void) 
117       (:short . :short)
118       (:pointer-void . (:pointer :void))
119       (:cstring . (:pointer :char))
120       (:char . :char) 
121       (:unsigned-char . (:unsigned :char))
122       (:int . :int) (:unsigned-int . (:unsigned :int))
123       (:long . :long) (:unsigned-long . (:unsigned :long))
124       (:float . :float) (:double . :double)
125       (:array . :c-array)))
126
127 (dolist (type +type-conversion-list+)
128   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
129
130 #+cmu
131 (dolist (type +cmu-def-type-list+)
132   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
133
134 (defun ph (&optional (os *standard-output*))
135   (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
136
137 (defun convert-from-uffi-type (type context)
138   "Converts from a uffi type to an implementation specific type"
139   (if (atom type)
140       (cond
141        #+allegro 
142        ((and (or (eq context :routine) (eq context :return))
143              (eq type :cstring))
144         (setq type '((* :char) integer)))
145        #+cmu 
146        ((eq context :type)
147         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
148           (if cmu-type
149               cmu-type
150             (let ((found-type (gethash type +type-conversion-hash+)))
151               (if found-type
152                   found-type
153                 type)))))
154        (t
155         (let ((found-type (gethash type +type-conversion-hash+)))
156           (if found-type
157               found-type
158             type))))
159     (cons (convert-from-uffi-type (first type) context) 
160           (convert-from-uffi-type (rest type) context))))
161
162
163
164
165
166