r2175: *** empty log message ***
[uffi.git] / src / primitives.cl
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
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 ;;;; $Id: primitives.cl,v 1.19 2002/06/27 05:46:39 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22 (defmacro def-constant (name value &key (export nil))
23   "Macro to define a constant and to export it"
24   `(eval-when (:compile-toplevel :load-toplevel :execute)
25      (defconstant ,name ,value)
26      ,(when export (list 'export `(quote ,name)))
27     ',name))
28
29 (defmacro def-type (name type)
30   "Generates a (deftype) statement for CL. Currently, only CMUCL
31 supports takes advantage of this optimization."
32   #+(or lispworks allegro)
33   (declare (ignore type))
34   #+(or lispworks allegro)
35   `(deftype ,name () t)
36   #+cmu
37   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
38   )
39
40 (defmacro null-char-p (val)
41   "Returns T if character is NULL"
42   `(zerop ,val))
43       
44 (defmacro def-foreign-type (name type)
45   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
46   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
47   #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
48   )
49
50 (eval-when (:compile-toplevel :load-toplevel :execute)
51   (defvar +type-conversion-hash+ (make-hash-table :size 20))
52   #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
53   )
54
55 #+cmu
56 (defconstant +cmu-def-type-list+
57     '((:char . (alien:signed 8))
58       (:unsigned-char . (alien:unsigned 8))
59       (:byte . (alien:signed 8))
60       (:unsigned-byte . (alien:unsigned 8))
61       (:short . (alien:signed 16))
62       (:unsigned-short . (alien:unsigned 16))
63       (:int . (alien:signed 32))
64       (:unsigned-int . (alien:unsigned 32))
65       (:long . (alien:signed 32))
66       (:unsigned-long . (alien:unsigned 32))
67       (:float . alien:single-float)
68       (:double . alien:double-float)
69       )
70   "Conversions in CMUCL or def-foreign-type are different thatn in def-function")
71
72 (defparameter +type-conversion-list+ nil)
73
74 #+cmu
75 (setq +type-conversion-list+
76     '((* . *) (:void . c-call:void) 
77       (:short . c-call:short)
78       (:pointer-void . (* t))
79       (:cstring . c-call:c-string)
80       (:char . c-call:char) 
81       (:unsigned-char . (alien:unsigned 8))
82       (:byte . (alien:signed 8))
83       (:unsigned-byte . (alien:unsigned 8))
84       (:short . c-call:unsigned-short) 
85       (:unsigned-short . c-call:unsigned-short)
86       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
87       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
88       (:float . c-call:float) (:double . c-call:double)
89       (:array . alien:array)))
90 #+allegro
91 (setq +type-conversion-list+
92     '((* . *) (:void . :void)
93       (:short . :short)
94       (:pointer-void . (* :void))
95       (:cstring . (* :unsigned-char))
96       (:byte . :char)
97       (:unsigned-byte . :unsigned-byte)
98       (:char . :char)
99       (:unsigned-char . :unsigned-char)
100       (:int . :int) (:unsigned-int . :unsigned-int) 
101       (:long . :long) (:unsigned-long . :unsigned-long)
102       (:float . :float) (:double . :double)
103       (:array . :array)))
104 #+lispworks
105 (setq +type-conversion-list+
106     '((* . :pointer) (:void . :void) 
107       (:short . :short)
108       (:pointer-void . (:pointer :void))
109       (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
110                                    :allow-null t))
111       (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
112       (:byte . :byte))
113       (:unsigned-byte . (:unsigned :byte))
114       (:char . :char)
115       (:unsigned-char . (:unsigned :char))
116       (:int . :int) (:unsigned-int . (:unsigned :int))
117       (:long . :long) (:unsigned-long . (:unsigned :long))
118       (:float . :float) (:double . :double)
119       (:array . :c-array)))
120
121 (dolist (type +type-conversion-list+)
122   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
123
124 #+cmu
125 (dolist (type +cmu-def-type-list+)
126   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
127
128 (defun basic-convert-from-uffi-type (type)
129   (let ((found-type (gethash type +type-conversion-hash+)))
130     (if found-type
131         found-type
132         type)))
133
134 (defun convert-from-uffi-type (type context)
135   "Converts from a uffi type to an implementation specific type"
136   (if (atom type)
137       (cond
138        #+allegro 
139        ((and (or (eq context :routine) (eq context :return))
140              (eq type :cstring))
141         (setq type '((* :char) integer)))
142        #+cmu 
143        ((eq context :type)
144         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
145           (if cmu-type
146               cmu-type
147               (basic-convert-from-uffi-type type))))
148        #+lispworks
149        ((and (eq context :return)
150              (eq type :cstring))
151         (basic-convert-from-uffi-type :cstring-returning))
152        (t
153         (basic-convert-from-uffi-type type)))
154       (cons (convert-from-uffi-type (first type) context) 
155             (convert-from-uffi-type (rest type) context))))
156
157
158
159
160
161