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.14 2002/03/23 16:32:39 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 ,(when export (list 'export `(quote ,name)))
29 (defmacro def-type (name type)
30 "Generates a (deftype) statement for CL. Currently, only CMUCL
31 supports takes advantage of this optimization."
32 #+(or lispworks allegro)
33 (declare (ignore type))
34 #+(or lispworks allegro)
37 `(deftype ,name () '(alien:alien ,(convert-from-uffi-type type :declare)))
40 (defmacro null-char-p (val)
41 "Returns T if character is NULL"
44 (defmacro def-foreign-type (name type)
45 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
46 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
47 #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
50 (eval-when (:compile-toplevel :load-toplevel :execute)
51 (defvar +type-conversion-hash+ (make-hash-table :size 20))
52 #+cmu (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
56 (defconstant +cmu-def-type-list+
57 '((:char . (alien:signed 8))
58 (:unsigned-char . (alien:unsigned 8))
59 (:byte . (alien:unsigned 8))
60 (:short . (alien:signed 16))
61 (:unsigned-short . (alien:unsigned 16))
62 (:int . (alien:signed 32))
63 (:unsigned-int . (alien:unsigned 32))
64 (:long . (alien:signed 32))
65 (:unsigned-long . (alien:unsigned 32))
66 (:float . alien:single-float)
67 (:double . alien:double-float)
69 "Conversions in CMUCL or def-foreign-type are different thatn in def-function")
73 (defconstant +type-conversion-list+
74 '((* . *) (:void . c-call:void)
75 (:short . c-call:short)
76 (:pointer-void . (* t))
77 (:cstring . c-call:c-string)
79 (:unsigned-char . (alien:unsigned 8))
80 (:byte . (alien:unsigned 8))
81 (:short . c-call:unsigned-short)
82 (:unsigned-short . c-call:unsigned-short)
83 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
84 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
85 (:float . c-call:float) (:double . c-call:double)
86 (:array . alien:array)))
88 (defconstant +type-conversion-list+
89 '((* . *) (:void . :void)
91 (:pointer-void . (* :void))
92 (:cstring . (* :unsigned-char))
94 (:unsigned-char . :unsigned-char)
96 (:int . :int) (:unsigned-int . :unsigned-int)
97 (:long . :long) (:unsigned-long . :unsigned-long)
98 (:float . :float) (:double . :double)
101 (defconstant +type-conversion-list+
102 '((* . :pointer) (:void . :void)
104 (:pointer-void . (:pointer :void))
105 (:cstring . (:reference-pass :ef-mb-string :allow-null t))
106 (:cstring-returning . (:reference :ef-mb-string :allow-null t))
109 (:unsigned-char . (:unsigned :char))
110 (:int . :int) (:unsigned-int . (:unsigned :int))
111 (:long . :long) (:unsigned-long . (:unsigned :long))
112 (:float . :float) (:double . :double)
113 (:array . :c-array)))
115 (dolist (type +type-conversion-list+)
116 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
119 (dolist (type +cmu-def-type-list+)
120 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
122 (defun basic-convert-from-uffi-type (type)
123 (let ((found-type (gethash type +type-conversion-hash+)))
128 (defun convert-from-uffi-type (type context)
129 "Converts from a uffi type to an implementation specific type"
133 ((and (or (eq context :routine) (eq context :return))
135 (setq type '((* :char) integer)))
138 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
141 (basic-convert-from-uffi-type type))))
143 ((and (eq context :return)
145 (basic-convert-from-uffi-type :cstring-returning))
147 (basic-convert-from-uffi-type type)))
148 (cons (convert-from-uffi-type (first type) context)
149 (convert-from-uffi-type (rest type) context))))