r1555: *** 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 ;;;; $Id: primitives.cl,v 1.4 2002/03/14 21:03:12 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)
23   "Macro to define a constant and to export it"
24   `(eval-when (:compile-toplevel :load-toplevel :execute)
25      (defconstant ,name ,value)
26      (export ',name)))
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   `(deftype ,name () t)
33   #+cmu
34   `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
35   )
36
37 (defmacro null-char-p (val)
38   `(if (or (eql ,val 0)
39            (eq ,val #\Null))
40       t
41     nil))
42
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:unsigned 8))
60       (:short . (alien:signed 16))
61       (:unsigned-short . (alien:unsigned 16))
62       (:int . (alien:signed 32))
63       (:unsigned-int . (alien:unsigned 32))
64       (:long . (alien:signed 32))
65       (:unsigned-long . (alien:unsigned 32))
66       (:float . alien:single-float)
67       (:double . alien:double-float)
68       ))
69
70 #+cmu
71 (defconstant +type-conversion-list+
72     '((* . *) (:void . c-call:void) 
73       (:short . c-call:short)
74       (:pointer-void . (* t))
75       (:cstring . c-call:c-string)
76       (:char . c-call:char) 
77       (:unsigned-char . (alien:unsigned 8))
78       (:byte . (alien:unsigned 8))
79       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
80       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
81       (:float . c-call:float) (:double . c-call:double)
82       (:array . alien:array)))
83 #+allegro
84 (defconstant +type-conversion-list+
85     '((* . *) (:void . :void)
86       (:short . :short)
87       (:pointer-void . (* :void))
88       (:cstring . (* :char))
89       (:char . :char)
90       (:unsigned-char . :unsigned-char)
91       (:byte . :byte)
92       (:int . :int) (:unsigned-int . :unsigned-int) 
93       (:long . :long) (:unsigned-long . :unsigned-long)
94       (:float . :float) (:double . :double)
95       (:array . :array)))
96 #+lispworks
97 (defconstant +type-conversion-list+
98     '((* . :pointer) (:void . :void) 
99       (:short . :short)
100       (:pointer-void . (:pointer :void))
101       (:cstring . (:pointer :char))
102       (:char . :char) 
103       (:unsigned-char . (:unsigned :char))
104       (:int . :int) (:unsigned-int . (:unsigned :int))
105       (:long . :long) (:unsigned-long . (:unsigned :long))
106       (:float . :float) (:double . :double)
107       (:array . :c-array)))
108
109 (dolist (type +type-conversion-list+)
110   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
111
112 #+cmu
113 (dolist (type +cmu-def-type-list+)
114   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
115
116 (defmethod ph (&optional (os *standard-output*))
117   (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
118
119 (defun convert-from-uffi-type (type context)
120   "Converts from a uffi type to an implementation specific type"
121   (if (atom type)
122       (cond
123        #+allegro 
124        ((and (or (eq context :routine) (eq context :return))
125              (eq type :cstring))
126         (setq type '((* :char) integer)))
127        #+cmu 
128        ((eq context :type)
129         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
130           (if cmu-type
131               cmu-type
132             (let ((found-type (gethash type +type-conversion-hash+)))
133               (if found-type
134                   found-type
135                 type)))))
136        (t
137         (let ((found-type (gethash type +type-conversion-hash+)))
138           (if found-type
139               found-type
140             type))))
141     (cons (convert-from-uffi-type (first type) context) 
142           (convert-from-uffi-type (rest type) context))))
143
144
145
146
147
148