1df69e1cbdf5ecb3c4ba3288d2ee073518df62ca
[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.3 2002/03/11 18:00:57 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 def-type (name type)
40   "Generates a (deftype) statement for CL. Currently, only CMUCL
41 supports takes advantage of this optimization."
42   #+(or lispworks allegro)
43   `(deftype ,name () t)
44   #+cmu
45   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
46   )
47
48 (defmacro null-char-p (val)
49   `(if (or (eql ,val 0)
50            (eq ,val #\Null))
51       t
52     nil))
53
54       
55 (defmacro def-foreign-type (name type)
56   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
57   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
58   #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
59   )
60
61 (eval-when (:compile-toplevel :load-toplevel :execute)
62   (defvar +type-conversion-hash+ (make-hash-table :size 20))
63   #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
64   )
65
66 #+cmu
67 (defconstant +cmu-def-type-list+
68     '((:char . (alien:signed 8))
69       (:unsigned-char . (alien:unsigned 8))
70       (:byte . (alien:unsigned 8))
71       (:short . (alien:signed 16))
72       (:unsigned-short . (alien:unsigned 16))
73       (:int . (alien:signed 32))
74       (:unsigned-int . (alien:unsigned 32))
75       (:long . (alien:signed 32))
76       (:unsigned-long . (alien:unsigned 32))
77       (:float . alien:single-float)
78       (:double . alien:double-float)
79       ))
80
81 #+cmu
82 (defconstant +type-conversion-list+
83     '((* . *) (:void . c-call:void) 
84       (:short . c-call:short)
85       (:pointer-void . (* t))
86       (:cstring . c-call:c-string)
87       (:char . c-call:char) 
88       (:unsigned-char . (alien:unsigned 8))
89       (:byte . (alien:unsigned 8))
90       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
91       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
92       (:float . c-call:float) (:double . c-call:double)
93       (:array . alien:array)))
94 #+allegro
95 (defconstant +type-conversion-list+
96     '((* . *) (:void . :void)
97       (:short . :short)
98       (:pointer-void . (* :void))
99       (:cstring . (* :char))
100       (:char . :char)
101       (:unsigned-char . :unsigned-char)
102       (:byte . :byte)
103       (:int . :int) (:unsigned-int . :unsigned-int) 
104       (:long . :long) (:unsigned-long . :unsigned-long)
105       (:float . :float) (:double . :double)
106       (:array . :array)))
107 #+lispworks
108 (defconstant +type-conversion-list+
109     '((* . :pointer) (:void . :void) 
110       (:short . :short)
111       (:pointer-void . (:pointer :void))
112       (:cstring . (:pointer :char))
113       (:char . :char) 
114       (:unsigned-char . (:unsigned :char))
115       (:int . :int) (:unsigned-int . (:unsigned :int))
116       (:long . :long) (:unsigned-long . (:unsigned :long))
117       (:float . :float) (:double . :double)
118       (:array . :c-array)))
119
120 (dolist (type +type-conversion-list+)
121   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
122
123 #+cmu
124 (dolist (type +cmu-def-type-list+)
125   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
126
127 (defmethod ph (&optional (os *standard-output*))
128   (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
129
130 (defun convert-from-uffi-type (type context)
131   "Converts from a uffi type to an implementation specific type"
132   (if (atom type)
133       (cond
134        #+allegro 
135        ((and (or (eq context :routine) (eq context :return))
136              (eq type :cstring))
137         (setq type '((* :char) integer)))
138        #+cmu 
139        ((eq context :type)
140         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
141           (if cmu-type
142               cmu-type
143             (let ((found-type (gethash type +type-conversion-hash+)))
144               (if found-type
145                   found-type
146                 type)))))
147        (t
148         (let ((found-type (gethash type +type-conversion-hash+)))
149           (if found-type
150               found-type
151             type))))
152     (cons (convert-from-uffi-type (first type) context) 
153           (convert-from-uffi-type (rest type) context))))
154
155
156
157
158
159