1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: UFFI source to C function defintions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: functions.lisp,v 1.1 2002/09/30 10:02:36 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)
27 #+(and mcl (not openmcl)) nil
28 #+mcl (values nil nil)
31 #+(or lispworks allegro cmu (and mcl (not openmcl)))
34 (push (process-one-function-arg arg) processed))
42 (setf type (convert-from-uffi-type (cadr arg) :routine))
43 ;;(when (and (listp type) (eq (car type) :address))
44 ;;(setf type :address))
47 (push name processed))
48 (values (nreverse params) (nreverse processed)))
51 (defun process-one-function-arg (arg)
52 (let ((name (car arg))
53 (type (convert-from-uffi-type (cadr arg) :routine)))
56 #+(or allegro lispworks (and mcl (not openmcl)))
57 (if (and (listp type) (listp (car type)))
58 (append (list name) type)
63 (defun allegro-convert-return-type (type)
64 (if (and (listp type) (not (listp (car type))))
68 ;; name is either a string representing foreign name, or a list
69 ;; of foreign-name as a string and lisp name as a symbol
70 (defmacro def-function (names args &key module returning)
71 #+(or cmu allegro mcl) (declare (ignore module))
73 (let* ((result-type (convert-from-uffi-type returning :return))
74 (function-args (process-function-args args))
75 (foreign-name (if (atom names) names (car names)))
76 (lisp-name (if (atom names) (make-lisp-name names) (cadr names))))
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 `(fli:define-foreign-function (,lisp-name ,foreign-name :source)
91 ,@(if module (list :module module) (values))
92 :result-type ,result-type
93 :calling-convention :cdecl)
94 #+(and mcl (not openmcl))
95 `(eval-when (:compile-toplevel :load-toplevel :execute)
96 (ccl:define-entry-point (,lisp-name ,foreign-name)
99 #+(and openmcl darwinppc-target)
100 (setf foreign-name (concatenate 'string "_" foreign-name))
102 (multiple-value-bind (params args) (process-function-args args)
103 `(defun ,lisp-name ,params
104 (ccl::external-call ,foreign-name ,@args ,result-type)))
108 (defun make-lisp-name (name)
109 (let ((converted (substitute #\- #\_ name)))
111 #+case-sensitive converted
112 #-case-sensitive (string-upcase converted))))