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.1 2002/08/02 14:39:12 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:signed 8))
60 (:unsigned-byte . (alien:unsigned 8))
61 (:short . (alien:signed 16))
62 (:unsigned-short . (alien:unsigned 16))
63 (:int . (alien:signed 32))
64 (:unsigned-int . (alien:unsigned 32))
65 (:long . (alien:signed 32))
66 (:unsigned-long . (alien:unsigned 32))
67 (:float . alien:single-float)
68 (:double . alien:double-float)
70 "Conversions in CMUCL or def-foreign-type are different thatn in def-function")
72 (defparameter +type-conversion-list+ nil)
75 (setq +type-conversion-list+
76 '((* . *) (:void . c-call:void)
77 (:short . c-call:short)
78 (:pointer-void . (* t))
79 (:cstring . c-call:c-string)
81 (:unsigned-char . (alien:unsigned 8))
82 (:byte . (alien:signed 8))
83 (:unsigned-byte . (alien:unsigned 8))
84 (:short . c-call:unsigned-short)
85 (:unsigned-short . c-call:unsigned-short)
86 (:int . alien:integer) (:unsigned-int . c-call:unsigned-int)
87 (:long . c-call:long) (:unsigned-long . c-call:unsigned-long)
88 (:float . c-call:float) (:double . c-call:double)
89 (:array . alien:array)))
91 (setq +type-conversion-list+
92 '((* . *) (:void . :void)
94 (:pointer-void . (* :void))
95 (:cstring . (* :unsigned-char))
97 (:unsigned-byte . :unsigned-byte)
99 (:unsigned-char . :unsigned-char)
100 (:int . :int) (:unsigned-int . :unsigned-int)
101 (:long . :long) (:unsigned-long . :unsigned-long)
102 (:float . :float) (:double . :double)
105 (setq +type-conversion-list+
106 '((* . :pointer) (:void . :void)
108 (:pointer-void . (:pointer :void))
109 (:cstring . (:reference-pass (:ef-mb-string :external-format :latin-1)
111 (:cstring-returning . (:reference (:ef-mb-string :external-format :latin-1) :allow-null t))
113 (:unsigned-byte . (:unsigned :byte))
115 (:unsigned-char . (:unsigned :char))
116 (:int . :int) (:unsigned-int . (:unsigned :int))
117 (:long . :long) (:unsigned-long . (:unsigned :long))
118 (:float . :float) (:double . :double)
119 (:array . :c-array)))
121 (dolist (type +type-conversion-list+)
122 (setf (gethash (car type) +type-conversion-hash+) (cdr type)))
125 (dolist (type +cmu-def-type-list+)
126 (setf (gethash (car type) +cmu-def-type-hash+) (cdr type)))
128 (defun basic-convert-from-uffi-type (type)
129 (let ((found-type (gethash type +type-conversion-hash+)))
134 (defun convert-from-uffi-type (type context)
135 "Converts from a uffi type to an implementation specific type"
139 ((and (or (eq context :routine) (eq context :return))
141 (setq type '((* :char) integer)))
144 (let ((cmu-type (gethash type +cmu-def-type-hash+)))
147 (basic-convert-from-uffi-type type))))
149 ((and (eq context :return)
151 (basic-convert-from-uffi-type :cstring-returning))
153 (basic-convert-from-uffi-type type)))
154 (cons (convert-from-uffi-type (first type) context)
155 (convert-from-uffi-type (rest type) context))))