1 ;;;; -*- Mode: ANSI-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.13 2002/03/23 09:32:43 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)
40 "Returns T if character is NULL"
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))
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))
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)
68 "Conversions in CMUCL or def-foreign-type are different thatn in def-function")
72 (defconstant +type-conversion-list+
73 '((* . *) (:void . c-call:void)
74 (:short . c-call:short)
75 (:pointer-void . (* t))
76 (:cstring . c-call:c-string)
78 (:unsigned-char . (alien:unsigned 8))
79 (:byte . (alien:unsigned 8))
80 (:short . c-call:unsigned-short)
81 (:unsigned-short . c-call:unsigned-short)
82 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
83 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
84 (:float . c-call:float) (:double . c-call:double)
85 (:array . alien:array)))
87 (defconstant +type-conversion-list+
88 '((* . *) (:void . :void)
90 (:pointer-void . (* :void))
91 (:cstring . (* :unsigned-char))
93 (:unsigned-char . :unsigned-char)
95 (:int . :int) (:unsigned-int . :unsigned-int)
96 (:long . :long) (:unsigned-long . :unsigned-long)
97 (:float . :float) (:double . :double)
100 (defconstant +type-conversion-list+
101 '((* . :pointer) (:void . :void)
103 (:pointer-void . (:pointer :void))
104 (:cstring . (:reference-pass :ef-mb-string :allow-null t))
105 (:cstring-returning . (:reference :ef-mb-string :allow-null t))
108 (:unsigned-char . (:unsigned :char))
109 (:int . :int) (:unsigned-int . (:unsigned :int))
110 (:long . :long) (:unsigned-long . (:unsigned :long))
111 (:float . :float) (:double . :double)
112 (:array . :c-array)))
114 (dolist (type +type-conversion-list+)
115 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
118 (dolist (type +cmu-def-type-list+)
119 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
121 (defun basic-convert-from-uffi-type (type)
122 (let ((found-type (gethash type +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 (basic-convert-from-uffi-type type))))
142 ((and (eq context :return)
144 (basic-convert-from-uffi-type :cstring-returning))
146 (basic-convert-from-uffi-type type)))
147 (cons (convert-from-uffi-type (first type) context)
148 (convert-from-uffi-type (rest type) context))))