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 ;;;; Programmers: Kevin M. Rosenberg and John DeSoi
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: primitives.cl,v 1.2 2002/09/20 04:51:14 kevin Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; UFFI users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
24 (defvar *keyword-package* (find-package "KEYWORD"))
26 ; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL)
27 ; So this provides a function to convert any quoted symbols to keywords.
34 (intern (symbol-name obj) *keyword-package*))
35 ((and (listp obj) (eq (car obj) 'cl:quote))
38 (intern obj *keyword-package*))
43 ; Wrapper for unexported function we have to use
45 (defmacro def-mcl-type (name type)
46 `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type)))
49 (defmacro def-constant (name value &key (export nil))
50 "Macro to define a constant and to export it"
51 `(eval-when (:compile-toplevel :load-toplevel :execute)
52 (defconstant ,name ,value)
53 ,(if export (list 'export `(quote ,name)) (values))))
55 (defmacro def-type (name type)
56 "Generates a (deftype) statement for CL. Currently, only CMUCL
57 supports takes advantage of this optimization."
58 (declare (ignore type))
59 `(deftype ,name () t))
61 (defmacro null-char-p (val)
62 "Returns T if character is NULL"
66 (defmacro def-foreign-type (name uffi-type)
67 (let ((type (convert-from-uffi-type uffi-type :type)))
68 (unless (keywordp type)
69 (setf type `(quote ,type)))
71 `(def-mcl-type ,(keyword name) ,type)
73 `(ccl::def-foreign-type ,(keyword name) ,type)))
76 (eval-when (:compile-toplevel :load-toplevel :execute)
77 (defvar +type-conversion-hash+ (make-hash-table :size 20)))
80 (defconstant +type-conversion-list+
81 '((* . :pointer) (:void . :void)
83 (:pointer-void . :pointer)
86 (:unsigned-char . :unsigned-byte)
87 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
88 (:int . :integer) (:unsigned-int . :unsigned-integer)
89 (:long . :long) (:unsigned-long . :unsigned-long)
90 (:float . :single-float) (:double . :double-float)
94 (defconstant +type-conversion-list+
95 '((* . :address) (:void . :void)
97 (:pointer-void . :address)
99 (:char . :signed-char)
100 (:unsigned-char . :unsigned-char)
101 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
102 (:int . :int) (:unsigned-int . :unsigned-int)
103 (:long . :long) (:unsigned-long . :unsigned-long)
104 (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword)
105 (:float . :single-float) (:double . :double-float)
108 (dolist (type +type-conversion-list+)
109 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
112 (defmethod ph (&optional (os *standard-output*))
113 (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
117 (defun basic-convert-from-uffi-type (type)
118 (let ((found-type (gethash type +type-conversion-hash+)))
123 (defun %convert-from-uffi-type (type context)
124 "Converts from a uffi type to an implementation specific type"
128 ((and (eq type :void) (eq context :return)) nil)
129 (t (basic-convert-from-uffi-type type)))
130 (if (eq (car type) 'cl:quote)
131 (%convert-from-uffi-type (cadr type) context)
132 (cons (%convert-from-uffi-type (first type) context)
133 (%convert-from-uffi-type (rest type) context)))))
135 (defun convert-from-uffi-type (type context)
136 (let ((result (%convert-from-uffi-type type context)))
138 ((atom result) result)
140 ((eq (car result) :address) :address)
142 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))