1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: UFFI source to C function definitions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: functions.lisp,v 1.3 2002/10/14 01:51:15 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 (defun process-function-args (args)
24 #+(or lispworks cmu sbcl cormanlisp (and mcl (not openmcl))) nil
26 #+mcl (values nil nil)
29 #+(or lispworks allegro cmu sbcl (and mcl (not openmcl)) cormanlisp)
32 (push (process-one-function-arg arg) processed))
40 (setf type (convert-from-uffi-type (cadr arg) :routine))
41 ;;(when (and (listp type) (eq (car type) :address))
42 ;;(setf type :address))
45 (push name processed))
46 (values (nreverse params) (nreverse processed)))
49 (defun process-one-function-arg (arg)
50 (let ((name (car arg))
51 (type (convert-from-uffi-type (cadr arg) :routine)))
54 #+(or allegro lispworks (and mcl (not openmcl)))
55 (if (and (listp type) (listp (car type)))
56 (append (list name) type)
61 (defun allegro-convert-return-type (type)
62 (if (and (listp type) (not (listp (car type))))
66 ;; name is either a string representing foreign name, or a list
67 ;; of foreign-name as a string and lisp name as a symbol
68 (defmacro def-function (names args &key module returning)
69 #+(or cmu sbcl allegro mcl cormanlisp) (declare (ignore module))
71 (let* ((result-type (convert-from-uffi-type returning :return))
72 (function-args (process-function-args args))
73 (foreign-name (if (atom names) names (car names)))
74 (lisp-name (if (atom names) (make-lisp-name names) (cadr names))))
76 ;; todo: calling-convention :stdcall for cormanlisp
78 `(ff:def-foreign-call (,lisp-name ,foreign-name)
80 :returning ,(allegro-convert-return-type result-type)
84 `(alien:def-alien-routine (,foreign-name ,lisp-name)
88 `(sb-alien:def-alien-routine (,foreign-name ,lisp-name)
92 `(fli:define-foreign-function (,lisp-name ,foreign-name :source)
94 ,@(if module (list :module module) (values))
95 :result-type ,result-type
96 :calling-convention :cdecl)
97 #+(and mcl (not openmcl))
98 `(eval-when (:compile-toplevel :load-toplevel :execute)
99 (ccl:define-entry-point (,lisp-name ,foreign-name)
102 #+(and openmcl darwinppc-target)
103 (setf foreign-name (concatenate 'string "_" foreign-name))
105 (multiple-value-bind (params args) (process-function-args args)
106 `(defun ,lisp-name ,params
107 (ccl::external-call ,foreign-name ,@args ,result-type)))
109 `(ct:defun-dll ,lisp-name (,function-args)
110 :return-type ,result-type
111 ,@(if module (list :library-name module) (values))
112 :entry-name ,foreign-name
113 :linkage-type ,calling-convention) ; we need :pascal
117 (defun make-lisp-name (name)
118 (let ((converted (substitute #\- #\_ name)))
120 #+case-sensitive converted
121 #-case-sensitive (string-upcase converted))))