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