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