r2927: Integrate Reini Urban's cormanlisp patches into main UFFI source
[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.2 2002/10/01 17:05:29 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 cormanlisp (and mcl (not openmcl))) nil
25       #+allegro '(:void)
26       #+mcl (values nil nil)
27
28       ;; args not null
29       #+(or lispworks allegro cmu (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     #+cmu
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 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     #+lispworks
88     `(fli:define-foreign-function (,lisp-name ,foreign-name :source)
89          ,function-args
90        ,@(if module (list :module module) (values))
91        :result-type ,result-type
92        :calling-convention :cdecl)
93     #+(and mcl (not openmcl))
94     `(eval-when (:compile-toplevel :load-toplevel :execute)
95        (ccl:define-entry-point (,lisp-name ,foreign-name)
96          ,function-args
97          ,result-type))
98     #+(and openmcl darwinppc-target)
99     (setf foreign-name (concatenate 'string "_" foreign-name))
100     #+openmcl
101     (multiple-value-bind (params args) (process-function-args args)
102       `(defun ,lisp-name ,params
103          (ccl::external-call ,foreign-name ,@args ,result-type)))
104     #+cormanlisp
105     `(ct:defun-dll ,lisp-name (,function-args)
106        :return-type ,result-type
107        ,@(if module (list :library-name module) (values))
108        :entry-name ,foreign-name
109        :linkage-type ,calling-convention) ; we need :pascal
110     ))
111
112
113 (defun make-lisp-name (name)
114   (let ((converted (substitute #\- #\_ name)))
115      (intern 
116       #+case-sensitive converted
117       #-case-sensitive (string-upcase converted))))
118
119