r2997: *** empty log message ***
[uffi.git] / src / functions.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          function.cl
6 ;;;; Purpose:       UFFI source to C function definitions
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: functions.lisp,v 1.3 2002/10/14 01:51:15 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22 (defun process-function-args (args)
23   (if (null args)
24       #+(or lispworks cmu sbcl cormanlisp (and mcl (not openmcl))) nil
25       #+allegro '(:void)
26       #+mcl (values nil nil)
27
28       ;; args not null
29       #+(or lispworks allegro cmu sbcl (and mcl (not openmcl)) cormanlisp)
30       (let (processed)
31         (dolist (arg args)
32           (push (process-one-function-arg arg) processed))
33         (nreverse processed))
34       #+openmcl
35       (let ((processed nil)
36             (params nil)
37             name type)
38         (dolist (arg args)
39           (setf name (car arg))
40           (setf type (convert-from-uffi-type (cadr arg) :routine))
41           ;;(when (and (listp type) (eq (car type) :address))
42           ;;(setf type :address))
43           (push name params)
44           (push type processed)
45           (push name processed))
46         (values (nreverse params) (nreverse processed)))
47     ))
48
49 (defun process-one-function-arg (arg)
50   (let ((name (car arg))
51         (type (convert-from-uffi-type (cadr arg) :routine)))
52     #+(or cmu sbcl)
53     (list name type :in)
54     #+(or allegro lispworks (and mcl (not openmcl)))
55     (if (and (listp type) (listp (car type)))
56         (append (list name) type)
57       (list name type))
58     ))    
59
60
61 (defun allegro-convert-return-type (type)
62   (if (and (listp type) (not (listp (car type))))
63       (list type)
64     type))
65
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))
70   
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))))
75
76     ;; todo: calling-convention :stdcall for cormanlisp
77     #+allegro
78     `(ff:def-foreign-call (,lisp-name ,foreign-name)
79          ,function-args
80        :returning ,(allegro-convert-return-type result-type)
81        :call-direct t
82        :strings-convert nil)
83     #+cmu
84     `(alien:def-alien-routine (,foreign-name ,lisp-name)
85          ,result-type
86        ,@function-args)
87     #+sbcl
88     `(sb-alien:def-alien-routine (,foreign-name ,lisp-name)
89          ,result-type
90        ,@function-args)
91     #+lispworks
92     `(fli:define-foreign-function (,lisp-name ,foreign-name :source)
93          ,function-args
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)
100          ,function-args
101          ,result-type))
102     #+(and openmcl darwinppc-target)
103     (setf foreign-name (concatenate 'string "_" foreign-name))
104     #+openmcl
105     (multiple-value-bind (params args) (process-function-args args)
106       `(defun ,lisp-name ,params
107          (ccl::external-call ,foreign-name ,@args ,result-type)))
108     #+cormanlisp
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
114     ))
115
116
117 (defun make-lisp-name (name)
118   (let ((converted (substitute #\- #\_ name)))
119      (intern 
120       #+case-sensitive converted
121       #-case-sensitive (string-upcase converted))))
122
123