1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: function.lisp
6 ;;;; Purpose: UFFI source to C function definitions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
12 ;;;; This file, part of UFFI, is Copyright (c) 2005 by Kevin M. Rosenberg
14 ;;;; *************************************************************************
18 (defun process-function-args (args)
20 #+(or lispworks cmu sbcl scl cormanlisp digitool) nil
22 #+openmcl (values nil nil)
25 #+(or lispworks allegro cmu sbcl scl digitool cormanlisp)
28 (push (process-one-function-arg arg) processed))
34 (let ((name (car arg))
35 (type (convert-from-uffi-type (cadr arg) :routine)))
36 ;;(when (and (listp type) (eq (car type) :address))
37 ;;(setf type :address))
40 (push name processed)))
41 (values (nreverse params) (nreverse processed)))
44 (defun process-one-function-arg (arg)
45 (let ((name (car arg))
46 (type (convert-from-uffi-type (cadr arg) :routine)))
49 `(,name ,type ,@(if (= (length arg) 3) (list (third arg)) (values)))
50 #+(or allegro lispworks digitool)
51 (if (and (listp type) (listp (car type)))
52 (append (list name) type)
55 (declare (ignore name type))
59 (defun allegro-convert-return-type (type)
60 (if (and (listp type) (not (listp (car type))))
64 (defun funcallable-lambda-list (args)
71 (defmacro def-funcallable (name args &key returning)
72 (let ((result-type (convert-from-uffi-type returning :return))
73 (function-args (process-function-args args)))
75 `(fli:define-foreign-funcallable ,name ,function-args
76 :result-type ,result-type
78 :calling-convention :cdecl)
80 ;; requires the type of the function pointer be declared correctly!
81 (let* ((ptrsym (gensym))
82 (ll (funcallable-lambda-list args)))
83 `(defun ,name ,(cons ptrsym ll)
84 (alien::alien-funcall ,ptrsym ,@ll)))
86 (multiple-value-bind (params args) (process-function-args args)
87 (let ((ptrsym (gensym)))
88 `(defun ,name ,(cons ptrsym params)
89 (ccl::ff-call ,ptrsym ,@args ,result-type))))
91 ;; this is most definitely wrong
92 (let* ((ptrsym (gensym))
93 (ll (funcallable-lambda-list args)))
94 `(defun ,name ,(cons ptrsym ll)
95 (system::ff-funcall ,ptrsym ,@ll)))
99 (defun convert-lispworks-args (args)
100 (loop for arg in args
103 (if (and (= (length arg) 3) (eq (third arg) :out))
104 (push (list (first arg)
105 (list :reference-return (second arg))) processed)
106 (push (subseq arg 0 2) processed))
107 finally (return processed)))
109 (defun preprocess-names (names)
110 (let ((fname (gensym)))
112 (values (list names fname) fname (uffi::make-lisp-name names))
113 (values (list (first names) fname) fname (second names)))))
115 (defun preprocess-args (args)
116 (loop for arg in args
117 with lisp-args = nil and out = nil and processed = nil
119 (if (= (length arg) 3)
123 (push (first arg) lisp-args)
124 (push (list (first arg) (second arg)) processed)))
127 (push (list (first arg) (second arg)) out)
128 (push (list (first arg) (list '* (second arg))) processed))))
130 (push (first arg) lisp-args)
131 (push arg processed)))
132 finally (return (values (nreverse lisp-args)
134 (nreverse processed)))))
137 (defmacro def-function (names args &key module returning)
138 (multiple-value-bind (lisp-args out processed)
139 (preprocess-args args)
140 (declare (ignorable lisp-args processed))
141 (if (= (length out) 0)
142 `(%def-function ,names ,args
143 ,@(if module (list :module module) (values))
144 ,@(if returning (list :returning returning) (values)))
147 `(%def-function ,names ,args
148 ,@(if returning (list :returning returning) (values)))
150 `(%def-function ,names ,(convert-lispworks-args args)
151 ,@(if module (list :module module) (values))
152 ,@(if returning (list :returning returning) (values)))
153 #-(or cmu scl sbcl lispworks)
154 (multiple-value-bind (name-pair fname lisp-name)
155 (preprocess-names names)
157 (%def-function ,name-pair ,processed
158 :module ,module :returning ,returning)
159 ;(declaim (inline ,fname))
160 (defun ,lisp-name ,lisp-args
161 (with-foreign-objects ,out
162 (values (,fname ,@(mapcar #'first args))
163 ,@(mapcar #'(lambda (arg)
166 (second arg))) out))))))
170 ;; name is either a string representing foreign name, or a list
171 ;; of foreign-name as a string and lisp name as a symbol
172 (defmacro %def-function (names args &key module returning)
173 #+(or cmu sbcl scl allegro openmcl digitool cormanlisp) (declare (ignore module))
175 (let* ((result-type (convert-from-uffi-type returning :return))
176 (function-args (process-function-args args))
177 (foreign-name (if (atom names) names (car names)))
178 (lisp-name (if (atom names) (make-lisp-name names) (cadr names))))
180 ;; todo: calling-convention :stdcall for cormanlisp
182 `(ff:def-foreign-call (,lisp-name ,foreign-name)
184 :returning ,(allegro-convert-return-type result-type)
186 :strings-convert nil)
188 `(alien:def-alien-routine (,foreign-name ,lisp-name)
192 `(sb-alien:define-alien-routine (,foreign-name ,lisp-name)
196 `(fli:define-foreign-function (,lisp-name ,foreign-name :source)
198 ,@(if module (list :module module) (values))
199 :result-type ,result-type
201 #+:win32 :calling-convention #+:win32 :cdecl)
203 `(eval-when (:compile-toplevel :load-toplevel :execute)
204 (ccl:define-entry-point (,lisp-name ,foreign-name)
208 (declare (ignore function-args))
209 #+(and openmcl darwinppc-target)
210 (setf foreign-name (concatenate 'string "_" foreign-name))
212 (multiple-value-bind (params args) (process-function-args args)
213 `(defun ,lisp-name ,params
214 (ccl::external-call ,foreign-name ,@args ,result-type)))
216 `(ct:defun-dll ,lisp-name (,function-args)
217 :return-type ,result-type
218 ,@(if module (list :library-name module) (values))
219 :entry-name ,foreign-name
220 :linkage-type ,calling-convention) ; we need :pascal