1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: immediates.cl
6 ;;;; Purpose: UFFI source to handle immediate types
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; Copyright (c) 2002 Kevin M. Rosenberg
12 ;;;; $Id: immediates.cl,v 1.1 2002/03/09 19:55:33 kevin Exp $
14 ;;;; This file is part of the UFFI.
16 ;;;; UFFI is free software; you can redistribute it and/or modify
17 ;;;; it under the terms of the GNU General Public License (version 2) as
18 ;;;; published by the Free Software Foundation.
20 ;;;; UFFI is distributed in the hope that it will be useful,
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;;;; GNU General Public License for more details.
25 ;;;; You should have received a copy of the GNU General Public License
26 ;;;; along with UFFI; if not, write to the Free Software
27 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 ;;;; *************************************************************************
30 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
33 (defmacro def-constant (name value)
34 "Macro to define a constant and to export it"
35 `(eval-when (:compile-toplevel :load-toplevel :execute)
36 (defconstant ,name ,value)
39 (defmacro uffi-declare (type name)
40 "Generates a declare statement for CL. Currently, only CMUCL
42 #+(or lispworks allegro)
43 (declare (ignore type name))
45 `(declare (type (alien ,type) ,name))
48 (defmacro slot-type (type)
49 #+(or lispworks allegro)
50 (declare (ignore type))
51 #+(or lispworks allegro)
53 #+cmu `'(alien:alien ,type))
55 (defmacro null-char-p (val)
62 (defmacro def-type (name type)
63 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
64 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
65 #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
68 (eval-when (:compile-toplevel :load-toplevel :execute)
69 (defvar +type-conversion-hash+ (make-hash-table :size 20))
70 #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
74 (defconstant +cmu-def-type-list+
75 '((:char . (alien:signed 8))
76 (:unsigned-char . (alien:unsigned 8))
77 (:short . (alien:signed 16))
78 (:unsigned-short . (alien:unsigned 16))
79 (:int . (alien:signed 32))
80 (:unsigned-int . (alien:unsigned 32))
81 (:long . (alien:signed 32))
82 (:unsigned-long . (alien:unsigned 32))
83 (:float . alien:single-float)
84 (:double . alien:double-float)
88 (defconstant +type-conversion-list+
89 '((* . *) (:void . c-call:void)
90 (:short . c-call:short)
91 (:pointer-void . (* t))
92 (:c-string . c-call:c-string)
93 (:char . c-call:char) (:unsigned-char . (alien:unsigned 8))
94 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
95 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
96 (:float . c-call:float) (:double . c-call:double)
97 (:array . alien:array)))
99 (defconstant +type-conversion-list+
100 '((* . *) (:void . :void)
102 (:pointer-void . (* :void))
103 (:c-string . (* :char))
104 (:char . :char) (:unsigned-char . :unsigned-char)
105 (:int . :int) (:unsigned-int . :unsigned-int)
106 (:long . :long) (:unsigned-long . :unsigned-long)
107 (:float . :float) (:double . :double)
110 (defconstant +type-conversion-list+
111 '((* . :pointer) (:void . :void)
113 (:pointer-void . (:pointer :void))
114 (:c-string . (:pointer (:unsigned :char)))
115 (:char . :char) (: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)))
121 (dolist (type +type-conversion-list+)
122 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
125 (dolist (type +cmu-def-type-list+)
126 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
128 (defun ph (&optional (os *standard-output*))
129 (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
131 (defun convert-from-uffi-type (type context)
132 "Converts from a uffi type to an implementation specific type"
136 ((and (or (eq context :routine) (eq context :return))
138 (setq type '((* :char) integer)))
141 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
144 (let ((found-type (gethash type +type-conversion-hash+)))
149 (let ((found-type (gethash type +type-conversion-hash+)))
153 (cons (convert-from-uffi-type (first type) context)
154 (convert-from-uffi-type (rest type) context))))