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.7 2003/02/06 06:54:22 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 scl cormanlisp (and mcl (not openmcl))) nil
26 #+openmcl (values nil nil)
29 #+(or lispworks allegro cmu sbcl scl (and mcl (not openmcl)) cormanlisp)
32 (push (process-one-function-arg arg) processed))
38 (let ((name (car arg))
39 (type (convert-from-uffi-type (cadr arg) :routine)))
40 ;;(when (and (listp type) (eq (car type) :address))
41 ;;(setf type :address))
44 (push name processed)))
45 (values (nreverse params) (nreverse processed)))
48 (defun process-one-function-arg (arg)
49 (let ((name (car arg))
50 (type (convert-from-uffi-type (cadr arg) :routine)))
53 #+(or allegro lispworks (and mcl (not openmcl)))
54 (if (and (listp type) (listp (car type)))
55 (append (list name) type)
58 (declare (ignore name type))
62 (defun allegro-convert-return-type (type)
63 (if (and (listp type) (not (listp (car type))))
67 ;; name is either a string representing foreign name, or a list
68 ;; of foreign-name as a string and lisp name as a symbol
69 (defmacro def-function (names args &key module returning)
70 #+(or cmu sbcl scl allegro mcl cormanlisp) (declare (ignore module))
72 (let* ((result-type (convert-from-uffi-type returning :return))
73 (function-args (process-function-args args))
74 (foreign-name (if (atom names) names (car names)))
75 (lisp-name (if (atom names) (make-lisp-name names) (cadr names))))
77 ;; todo: calling-convention :stdcall for cormanlisp
79 `(ff:def-foreign-call (,lisp-name ,foreign-name)
81 :returning ,(allegro-convert-return-type result-type)
85 `(alien:def-alien-routine (,foreign-name ,lisp-name)
89 `(sb-alien:define-alien-routine (,foreign-name ,lisp-name)
93 `(fli:define-foreign-function (,lisp-name ,foreign-name :source)
95 ,@(if module (list :module module) (values))
96 :result-type ,result-type
98 :calling-convention :cdecl)
99 #+(and mcl (not openmcl))
100 `(eval-when (:compile-toplevel :load-toplevel :execute)
101 (ccl:define-entry-point (,lisp-name ,foreign-name)
105 (declare (ignore function-args))
106 #+(and openmcl darwinppc-target)
107 (setf foreign-name (concatenate 'string "_" foreign-name))
109 (multiple-value-bind (params args) (process-function-args args)
110 `(defun ,lisp-name ,params
111 (ccl::external-call ,foreign-name ,@args ,result-type)))
113 `(ct:defun-dll ,lisp-name (,function-args)
114 :return-type ,result-type
115 ,@(if module (list :library-name module) (values))
116 :entry-name ,foreign-name
117 :linkage-type ,calling-convention) ; we need :pascal
121 (defun make-lisp-name (name)
122 (let ((converted (substitute #\- #\_ name)))
124 #+case-sensitive converted
125 #-case-sensitive (string-upcase converted))))