X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=src-mcl%2Fprimitives.cl;h=f78fd54b1104de4088fbd7836d2f6ce535ba3927;hb=7523905053e1fd44e5cbef67332a3fb00d35e848;hp=6e49d87bfee409268c8df6c8720bf68627f4743e;hpb=0eaed82d93e9d2afbdcbdb8b49b0fc2386f86963;p=uffi.git diff --git a/src-mcl/primitives.cl b/src-mcl/primitives.cl index 6e49d87..f78fd54 100644 --- a/src-mcl/primitives.cl +++ b/src-mcl/primitives.cl @@ -7,7 +7,7 @@ ;;;; Programmers: Kevin M. Rosenberg and John DeSoi ;;;; Date Started: Feb 2002 ;;;; -;;;; $Id: primitives.cl,v 1.1 2002/09/16 17:57:43 kevin Exp $ +;;;; $Id: primitives.cl,v 1.3 2002/09/20 13:05:59 kevin Exp $ ;;;; ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; and John DeSoi @@ -20,9 +20,30 @@ (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0))) (in-package :uffi) + +(defvar *keyword-package* (find-package "KEYWORD")) + +; MCL and OpenMCL expect a lot of FFI elements to be keywords (e.g. struct field names in OpenMCL) +; So this provides a function to convert any quoted symbols to keywords. +(defun keyword (obj) + (cond ((keywordp obj) + obj) + ((null obj) + nil) + ((symbolp obj) + (intern (symbol-name obj) *keyword-package*)) + ((and (listp obj) (eq (car obj) 'cl:quote)) + (keyword (cadr obj))) + ((stringp obj) + (intern obj *keyword-package*)) + (t + obj))) + + ; Wrapper for unexported function we have to use +#-openmcl (defmacro def-mcl-type (name type) - `(ccl::def-mactype (quote ,name) (ccl:find-mactype ,type))) + `(ccl::def-mactype ,(keyword name) (ccl:find-mactype ,type))) (defmacro def-constant (name value &key (export nil)) @@ -42,26 +63,48 @@ supports takes advantage of this optimization." `(zerop ,val)) -(defmacro def-foreign-type (name type) - `(def-mcl-type ,name (convert-from-uffi-type ,type :type))) +(defmacro def-foreign-type (name uffi-type) + (let ((type (convert-from-uffi-type uffi-type :type))) + (unless (keywordp type) + (setf type `(quote ,type))) + #-openmcl + `(def-mcl-type ,(keyword name) ,type) + #+openmcl + `(ccl::def-foreign-type ,(keyword name) ,type))) (eval-when (:compile-toplevel :load-toplevel :execute) (defvar +type-conversion-hash+ (make-hash-table :size 20))) +#-openmcl +(defconstant +type-conversion-list+ + '((* . :pointer) (:void . :void) + (:short . :short) (:unsigned-short . :unsigned-short) + (:pointer-void . :pointer) + (:cstring . :string) + (:char . :character) + (:unsigned-char . :unsigned-byte) + (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte) + (:int . :long) (:unsigned-int . :unsigned-long) + (:long . :long) (:unsigned-long . :unsigned-long) + (:float . :single-float) (:double . :double-float) + (:array . :array))) + +#+openmcl +(defconstant +type-conversion-list+ + '((* . :address) (:void . :void) + (:short . :short) (:unsigned-short . :unsigned-short) + (:pointer-void . :address) + (:cstring . :address) + (:char . :signed-char) + (:unsigned-char . :unsigned-char) + (:byte . :signed-byte) (:unsigned-byte . :unsigned-byte) + (:int . :int) (:unsigned-int . :unsigned-int) + (:long . :long) (:unsigned-long . :unsigned-long) + (:long-long . :signed-doubleword) (:unsigned-long-long . :unsigned-doubleword) + (:float . :single-float) (:double . :double-float) + (:array . :array))) -(defconstant +type-conversion-list+ - '((* . :pointer) (:void . :void) - (:short . :short) - (:pointer-void . :pointer) - (:cstring . :string) - (:char . :character) - (:unsigned-char . :unsigned-byte) - (:byte . :byte) - (:int . :integer) (:unsigned-int . :unsigned-integer) - (:long . :long) (:unsigned-long . :unsigned-long) - (:float . :single-float) (:double . :double-float) - (:array . :array))) (dolist (type +type-conversion-list+) (setf (gethash (car type) +type-conversion-hash+) (cdr type))) @@ -69,23 +112,33 @@ supports takes advantage of this optimization." (defmethod ph (&optional (os *standard-output*)) (maphash #'(lambda (k v) (format os "~&~S => ~S" k v)) +type-conversion-hash+)) - -(defun convert-from-uffi-type (type context) - "Converts from a uffi type to an implementation specific type" - (if (atom type) - (cond - #+mcl - ((and (eq type :void) (eq context :return)) nil) - (t - (let ((found-type (gethash type +type-conversion-hash+))) - (if found-type - found-type - type)))) - (cons (convert-from-uffi-type (first type) context) - (convert-from-uffi-type (rest type) context)))) - - + +(defun basic-convert-from-uffi-type (type) + (let ((found-type (gethash type +type-conversion-hash+))) + (if found-type + found-type + (keyword type)))) +(defun %convert-from-uffi-type (type context) + "Converts from a uffi type to an implementation specific type" + (if (atom type) + (cond + #-openmcl + ((and (eq type :void) (eq context :return)) nil) + (t (basic-convert-from-uffi-type type))) + (if (eq (car type) 'cl:quote) + (%convert-from-uffi-type (cadr type) context) + (cons (%convert-from-uffi-type (first type) context) + (%convert-from-uffi-type (rest type) context))))) +(defun convert-from-uffi-type (type context) + (let ((result (%convert-from-uffi-type type context))) + (cond + ((atom result) result) + #+openmcl + ((eq (car result) :address) :address) + #-openmcl + ((and (eq (car result) :pointer) (eq context :allocation) :pointer)) + (t result))))