bf013e65669c2e9ca658bad075ae254978007429
[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.11 2002/03/21 10:58:57 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   "Returns T if character is NULL"
41   `(zerop ,val))
42       
43 (defmacro def-foreign-type (name type)
44   #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
45   #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
46   #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
47   )
48
49 (eval-when (:compile-toplevel :load-toplevel :execute)
50   (defvar +type-conversion-hash+ (make-hash-table :size 20))
51   #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
52   )
53
54 #+cmu
55 (defconstant +cmu-def-type-list+
56     '((:char . (alien:signed 8))
57       (:unsigned-char . (alien:unsigned 8))
58       (:byte . (alien:unsigned 8))
59       (:short . (alien:signed 16))
60       (:unsigned-short . (alien:unsigned 16))
61       (:int . (alien:signed 32))
62       (:unsigned-int . (alien:unsigned 32))
63       (:long . (alien:signed 32))
64       (:unsigned-long . (alien:unsigned 32))
65       (:float . alien:single-float)
66       (:double . alien:double-float)
67       ))
68
69 #+cmu
70 (defconstant +type-conversion-list+
71     '((* . *) (:void . c-call:void) 
72       (:short . c-call:short)
73       (:pointer-void . (* t))
74       (:cstring . c-call:c-string)
75       (:char . c-call:char) 
76       (:unsigned-char . (alien:unsigned 8))
77       (:byte . (alien:unsigned 8))
78       (:short . c-call:unsigned-short) 
79       (:unsigned-short . c-call:unsigned-short)
80       (:int . alien:integer) (:unsigned-int . c-call:unsigned-int) 
81       (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
82       (:float . c-call:float) (:double . c-call:double)
83       (:array . alien:array)))
84 #+allegro
85 (defconstant +type-conversion-list+
86     '((* . *) (:void . :void)
87       (:short . :short)
88       (:pointer-void . (* :void))
89       (:cstring . (* :unsigned-char))
90       (:char . :char)
91       (:unsigned-char . :unsigned-char)
92       (:byte . :byte)
93       (:int . :int) (:unsigned-int . :unsigned-int) 
94       (:long . :long) (:unsigned-long . :unsigned-long)
95       (:float . :float) (:double . :double)
96       (:array . :array)))
97 #+lispworks
98 (defconstant +type-conversion-list+
99     '((* . :pointer) (:void . :void) 
100       (:short . :short)
101       (:pointer-void . (:pointer :unsigned :void))
102       (:cstring . (:pointer (:unsigned :char)))
103       (:char . :char)
104       (:byte :byte)
105       (:unsigned-char . (:unsigned :char))
106       (:int . :int) (:unsigned-int . (:unsigned :int))
107       (:long . :long) (:unsigned-long . (:unsigned :long))
108       (:float . :float) (:double . :double)
109       (:array . :c-array)))
110
111 (dolist (type +type-conversion-list+)
112   (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
113
114 #+cmu
115 (dolist (type +cmu-def-type-list+)
116   (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
117
118 (defun convert-from-uffi-type (type context)
119   "Converts from a uffi type to an implementation specific type"
120   (if (atom type)
121       (cond
122        #+allegro 
123        ((and (or (eq context :routine) (eq context :return))
124              (eq type :cstring))
125         (setq type '((* :char) integer)))
126        #+cmu 
127        ((eq context :type)
128         (let ((cmu-type (gethash type +cmu-def-type-hash+)))
129           (if cmu-type
130               cmu-type
131             (let ((found-type (gethash type +type-conversion-hash+)))
132               (if found-type
133                   found-type
134                 type)))))
135        (t
136         (let ((found-type (gethash type +type-conversion-hash+)))
137           (if found-type
138               found-type
139             type))))
140     (cons (convert-from-uffi-type (first type) context) 
141           (convert-from-uffi-type (rest type) context))))
142
143
144
145
146
147