1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
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.2 2002/09/30 01:57:32 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)))
39 `(deftype ,name () '(sb-alien:alien ,(convert-from-uffi-type type :declare)))
42 (defmacro null-char-p (val)
43 "Returns T if character is NULL"
46 (defmacro def-foreign-type (name type)
47 #+lispworks `(fli:define-c-typedef ,name ,(convert-from-uffi-type type :type))
48 #+allegro `(ff:def-foreign-type ,name ,(convert-from-uffi-type type :type))
49 #+cmu `(alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
50 #+sbcl `(sb-alien:def-alien-type ,name ,(convert-from-uffi-type type :type))
53 (eval-when (:compile-toplevel :load-toplevel :execute)
54 (defvar +type-conversion-hash+ (make-hash-table :size 20))
55 #+(or cmu sbcl) (defvar +cmu-def-type-hash+ (make-hash-table :size 20))
59 (defconstant +cmu-def-type-list+
60 '((:char . (alien:signed 8))
61 (:unsigned-char . (alien:unsigned 8))
62 (:byte . (alien:signed 8))
63 (:unsigned-byte . (alien:unsigned 8))
64 (:short . (alien:signed 16))
65 (:unsigned-short . (alien:unsigned 16))
66 (:int . (alien:signed 32))
67 (:unsigned-int . (alien:unsigned 32))
68 (:long . (alien:signed 32))
69 (:unsigned-long . (alien:unsigned 32))
70 (:float . alien:single-float)
71 (:double . alien:double-float)
73 "Conversions in CMUCL for def-foreign-type are different than in def-function")
75 (defconstant +cmu-def-type-list+
76 '((:char . (sb-alien:signed 8))
77 (:unsigned-char . (sb-alien:unsigned 8))
78 (:byte . (sb-alien:signed 8))
79 (:unsigned-byte . (sb-alien:unsigned 8))
80 (:short . (sb-alien:signed 16))
81 (:unsigned-short . (sb-alien:unsigned 16))
82 (:int . (sb-alien:signed 32))
83 (:unsigned-int . (sb-alien:unsigned 32))
84 (:long . (sb-alien:signed 32))
85 (:unsigned-long . (sb-alien:unsigned 32))
86 (:float . sb-alien:single-float)
87 (:double . sb-alien:double-float)
89 "Conversions in SBCL for def-foreign-type are different than in def-function")
91 (defparameter +type-conversion-list+ nil)
94 (setq +type-conversion-list+
95 '((* . *) (:void . c-call:void)
96 (:short . c-call:short)
97 (:pointer-void . (* t))
98 (:cstring . c-call:c-string)
100 (:unsigned-char . (alien:unsigned 8))
101 (:byte . (alien:signed 8))
102 (:unsigned-byte . (alien:unsigned 8))
103 (:short . c-call:unsigned-short)
104 (:unsigned-short . c-call:unsigned-short)
105 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
106 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
107 (:float . c-call:float) (:double . c-call:double)
108 (:array . alien:array)))
111 (setq +type-conversion-list+
112 '((* . *) (:void . void)
114 (:pointer-void . (* t))
115 (:cstring . c-string)
117 (:unsigned-char . (sb-alien:unsigned 8))
118 (:byte . (sb-alien:signed 8))
119 (:unsigned-byte . (sb-alien:unsigned 8))
120 (:short . unsigned-short)
121 (:unsigned-short . unsigned-short)
122 (:int . integer) (:unsigned-int . unsigned-int)
123 (:long . long) (:unsigned-long . unsigned-long)
124 (:float . float) (:double . double)
128 (setq +type-conversion-list+
129 '((* . *) (:void . :void)
131 (:pointer-void . (* :void))
132 (:cstring . (* :unsigned-char))
134 (:unsigned-byte . :unsigned-byte)
136 (:unsigned-char . :unsigned-char)
137 (:int . :int) (:unsigned-int . :unsigned-int)
138 (:long . :long) (:unsigned-long . :unsigned-long)
139 (:float . :float) (:double . :double)
142 (setq +type-conversion-list+
143 '((* . :pointer) (:void . :void)
145 (:pointer-void . (:pointer :void))
146 (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
148 (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
150 (:unsigned-byte . (:unsigned :byte))
152 (:unsigned-char . (:unsigned :char))
153 (:int . :int) (:unsigned-int . (:unsigned :int))
154 (:long . :long) (:unsigned-long . (:unsigned :long))
155 (:float . :float) (:double . :double)
156 (:array . :c-array)))
158 (dolist (type +type-conversion-list+)
159 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
162 (dolist (type +cmu-def-type-list+)
163 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
165 (defun basic-convert-from-uffi-type (type)
166 (let ((found-type (gethash type +type-conversion-hash+)))
171 (defun convert-from-uffi-type (type context)
172 "Converts from a uffi type to an implementation specific type"
176 ((and (or (eq context :routine) (eq context :return))
178 (setq type '((* :char) integer)))
181 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
184 (basic-convert-from-uffi-type type))))
186 ((and (eq context :return)
188 (basic-convert-from-uffi-type :cstring-returning))
190 (basic-convert-from-uffi-type type)))
191 (let ((sub-type (car type)))
194 (convert-from-uffi-type (cadr type) context))
196 #+openmcl `(:* (:struct ,(convert-from-uffi-type (cadr type) :struct)))
197 #-openmcl `(* ,(convert-from-uffi-type (cadr type) :struct))
200 #+openmcl `(:struct ,(convert-from-uffi-type (cadr type) :struct))
201 #-openmcl (convert-from-uffi-type (cadr type) :struct)
204 (cons (convert-from-uffi-type (first type) context)
205 (convert-from-uffi-type (rest type) context)))))))