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.4 2002/09/30 01:57:32 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 (or (keywordp type) (consp 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)
82 (:short . :short) (:unsigned-short . :unsigned-short)
83 (:pointer-void . :pointer)
86 (:unsigned-char . :unsigned-byte)
87 (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte)
88 (:int . :long) (:unsigned-int . :unsigned-long)
89 (:long . :long) (:unsigned-long . :unsigned-long)
90 (:float . :single-float) (:double . :double-float)
94 (defconstant +type-conversion-list+
95 '((* . :address) (:void . :void)
96 (:short . :short) (:unsigned-short . :unsigned-short)
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)
109 (dolist (type +type-conversion-list+)
110 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
113 (defmethod ph (&optional (os *standard-output*))
114 (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+))
118 (defun basic-convert-from-uffi-type (type)
119 (let ((found-type (gethash type +type-conversion-hash+)))
124 (defun %convert-from-uffi-type (type context)
125 "Converts from a uffi type to an implementation specific type"
128 #-openmcl ((and (eq type :void) (eq context :return)) nil)
129 (t (basic-convert-from-uffi-type type)))
130 (let ((sub-type (car type)))
133 (%convert-from-uffi-type (cadr type) context))
135 #+openmcl `(:* (:struct ,(convert-from-uffi-type (cadr type) :struct)))
136 #-openmcl `(,(convert-from-uffi-type (list '* (cadr type)) :struct))
139 #+openmcl `(:struct ,(convert-from-uffi-type (cadr type) :struct))
140 #-openmcl `(,(convert-from-uffi-type (cadr type) :struct))
143 (cons (%convert-from-uffi-type (first type) context)
144 (%convert-from-uffi-type (rest type) context)))))))
146 (defun convert-from-uffi-type (type context)
147 (let ((result (%convert-from-uffi-type type context)))
149 ((atom result) result)
151 ((eq (car result) :address)
152 (if (eq context :struct)
153 (append '(:*) (cdr result))
156 ((and (eq (car result) :pointer) (eq context :allocation) :pointer))