1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: primitives.cl
6 ;;;; Purpose: UFFI source to handle immediate types
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: primitives.cl,v 1.6 2002/03/15 11:38:13 kevin Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
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))))
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)
36 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
39 (defmacro null-char-p (val)
45 (defmacro ensure-char (val)
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))
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))
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)
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)
85 (:unsigned-char . (alien:unsigned 8))
86 (:byte . (alien:unsigned 8))
87 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
88 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
89 (:float . c-call:float) (:double . c-call:double)
90 (:array . alien:array)))
92 (defconstant +type-conversion-list+
93 '((* . *) (:void . :void)
95 (:pointer-void . (* :void))
96 (:cstring . (* :char))
98 (:unsigned-char . :unsigned-char)
100 (:int . :int) (:unsigned-int . :unsigned-int)
101 (:long . :long) (:unsigned-long . :unsigned-long)
102 (:float . :float) (:double . :double)
105 (defconstant +type-conversion-list+
106 '((* . :pointer) (:void . :void)
108 (:pointer-void . (:pointer :void))
109 (:cstring . (:pointer :char))
111 (:unsigned-char . (:unsigned :char))
112 (:int . :int) (:unsigned-int . (:unsigned :int))
113 (:long . :long) (:unsigned-long . (:unsigned :long))
114 (:float . :float) (:double . :double)
115 (:array . :c-array)))
117 (dolist (type +type-conversion-list+)
118 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
121 (dolist (type +cmu-def-type-list+)
122 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
124 (defmethod ph (&optional (os *standard-output*))
125 (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
127 (defun convert-from-uffi-type (type context)
128 "Converts from a uffi type to an implementation specific type"
132 ((and (or (eq context :routine) (eq context :return))
134 (setq type '((* :char) integer)))
137 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
140 (let ((found-type (gethash type +type-conversion-hash+)))
145 (let ((found-type (gethash type +type-conversion-hash+)))
149 (cons (convert-from-uffi-type (first type) context)
150 (convert-from-uffi-type (rest type) context))))