Subject: [clisp-list] Clisp FFI

From: Kick Damien-DKICK1 <[email-masked]>
Date: Mon Nov 4 13:34:01 2002

I'm having problems with attempting build my own FFI example based on
the example in the FFI implementation notes
<http://clisp.cons.org/impnotes/dffi.html> and would appreciate any
help.

    [kick@login0 ~/tmp]% $DIR/clisp-link create-module-set f00f f00f-code.c
    [kick@login0 ~/tmp]% gcc -c f00f-code.c 
    [kick@login0 ~/tmp]% lf
    call-f00f.lisp	f00f/  f00f-code.c  f00f-code.o
    [kick@login0 ~/tmp]% cd f00f
    [kick@login0 tmp/f00f]% ln -s ../f00f-code.o
    [kick@login0 tmp/f00f]% cd ..
    [kick@login0 ~/tmp]% $DIR/base/lisp.run -M $DIR/base/lispinit.mem -c call-f00f.lisp 
      i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
      I I I I I I I      8     8   8           8     8     o  8    8
      I  \ `+' /  I      8         8           8     8        8    8
       \  `-+-'  /       8         8           8      ooooo   8oooo
        `-__|__-'        8         8           8           8  8
            |            8     o   8           8     o     8  8
      ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

    Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
    Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
    Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
    Copyright (c) Bruno Haible, Sam Steingold 1999-2002

    Compiling file /home/kick/tmp/call-f00f.lisp ...
    WARNING:
    SETQ(FFI::*FOREIGN-LANGUAGE*): #<PACKAGE FFI> is locked
    Ignore the lock and proceed
    WARNING in (DEFAULT-FOREIGN-LANGUAGE :STDC)-4 in line 4 :
    SETQ: assignment to the internal special symbol FFI::*FOREIGN-LANGUAGE*

    Wrote file /home/kick/tmp/call-f00f.fas
    Wrote file /home/kick/tmp/call-f00f.c
    0 errors, 1 warning
    Bye.
    [kick@login0 ~/tmp]% $DIR/clisp-link add-module-set f00f $DIR/base base+f00f
    make: Warning: File `Makefile' has modification time 44 s in the future
    make: Nothing to be done for `clisp-module'.
    make: warning:  Clock skew detected.  Your build may be incomplete.
    /usr/test/npaero/local/sparc-sun-solaris2.6/bin/gcc -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type -fomit-frame-pointer -Wno-sign-compare -O2 -fno-schedule-insns -fno-gcse -DUNIX_BINARY_DISTRIB -DUNICODE -DDYNAMIC_FFI -DNO_GETTEXT -DNO_SIGSEGV -I. -I/usr/test/awo/user_work/kick/sparc-sun-solaris2.6/lib/clisp/linkkit -c modules.c
    In file included from modules.d:11:
    /usr/test/awo/user_work/kick/sparc-sun-solaris2.6/lib/clisp/linkkit/clisp.h:2855: warning: call-clobbered register used for global register variable
    /usr/test/npaero/local/sparc-sun-solaris2.6/bin/gcc -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type -fomit-frame-pointer -Wno-sign-compare -O2 -fno-schedule-insns -fno-gcse -DUNIX_BINARY_DISTRIB -DUNICODE -DDYNAMIC_FFI -DNO_GETTEXT -DNO_SIGSEGV -I. -x none modules.o f00f-code.o lisp.a libcharset.a libavcall.a libcallback.a -ltermcap -lnsl -lsocket -o lisp.run
    modules.o(.data+0x30): undefined reference to `module__f00f_code__subr_tab'
    modules.o(.data+0x34): undefined reference to `module__f00f_code__subr_tab_size'
    modules.o(.data+0x38): undefined reference to `module__f00f_code__object_tab'
    modules.o(.data+0x3c): undefined reference to `module__f00f_code__object_tab_size'
    modules.o(.data+0x44): undefined reference to `module__f00f_code__subr_tab_initdata'
    modules.o(.data+0x48): undefined reference to `module__f00f_code__object_tab_initdata'
    modules.o(.data+0x4c): undefined reference to `module__f00f_code__init_function_1'
    modules.o(.data+0x50): undefined reference to `module__f00f_code__init_function_2'
    collect2: ld returned 1 exit status
    [kick@login0 ~/tmp]% lf
    base+f00f/   call-f00f.fas  call-f00f.lisp  f00f-code.c
    call-f00f.c  call-f00f.lib  f00f/	    f00f-code.o
    [kick@login0 ~/tmp]% cat f00f-code.c
    #include <stdio.h>

    struct X { int i; };

    void f(struct X* p)
    {
        printf("p->i => %d\n", p->i);
    }
    [kick@login0 ~/tmp]% cat call-f00f.lisp 
    (defpackage "CALL-F00F" (:use "LISP" "FFI"))
    (in-package "CALL-F00F")
    (def-c-struct X (i int))
    (default-foreign-language :stdc)
    (def-call-out f
        (:arguments (p (c-ptr X) :in :alloca))
      (:return-type nil))
    (defun call-f ()
      (f (make-X :i 5)))
    [kick@login0 ~/tmp]% cat f00f/link.sh 
    file_list=''
    mod_list=''
    if test -r f00f-code.c; then
      file_list="$file_list"' f00f-code.o'
      mod_list="$mod_list"' f00f_code'
    fi
    make clisp-module CC="${CC}" CFLAGS="${CFLAGS}" INCLUDES="$absolute_linkkitdir"
    NEW_FILES="$file_list"
    NEW_LIBS="$file_list"
    NEW_MODULES="$mod_list"
    TO_LOAD=''
    [kick@login0 ~/tmp]% cat call-f00f.c
    #include "clisp.h"

    extern object module__call_f00f__object_tab[];

    subr_t module__call_f00f__subr_tab[1];
    uintC module__call_f00f__subr_tab_size = 0;
    subr_initdata_t module__call_f00f__subr_tab_initdata[1];

    object module__call_f00f__object_tab[1];
    object_initdata_t module__call_f00f__object_tab_initdata[1];
    uintC module__call_f00f__object_tab_size = 0;

    extern void (f)();

    void module__call_f00f__init_function_1(module)
      var module_t* module;
    { }

    void module__call_f00f__init_function_2(module)
      var module_t* module;
    {
      register_foreign_function(&f,"f",1024);
    }
    [kick@login0 ~/tmp]% 

Why is "call-f00f.c" not being included in the build?

--
Damien Kick

---

From: Kaz Kylheku <[email-masked]>
Date: Mon Nov 4 14:28:07 2002

>     SETQ: assignment to the internal special symbol FFI::*FOREIGN-LANGUAGE*

That is annoying. def-c-call-out is supposed to be obsolete, but the
workaround of setting the default FFI language triggers an error. What
I do is define my own private versions of def-c-call-out and
def-c-call-in in my own package, which add the (:language :stdc)
property. I set these up as shadowing symbols so they hide the ones
accessible in FFI.

>     make: Warning: File `Makefile' has modification time 44 s in the future

NFS?

>     modules.o(.data+0x30): undefined reference to `module__f00f_code__subr_tab'

This problem is caused by a disagreement within the link.sh script
about what your module is called. 

When CLISP's FFI compiler processes your Lisp module to spit out the .c
file, it creates a module name based on the name of that Lisp module,
translated to a C identifier (dashes to underscores).  That module name
is encoded into various C definitions. These definitions must have
names that agree with the modules.o. 

In other words, the module name that you configure in your link.sh must
agree with the name of your .lisp file.

>     call-f00f.c  call-f00f.lib  f00f/	    f00f-code.o

Based on this listing, your module name is ``call-f00f'', which
translates to the C language identifier ``call_f00f''. Edit link.sh to
set this as the module's name. The end result should be that modules.o
makes references to module__call_f00f__subr_tab and so forth, and these
references are satisfied by definitions inside call-f00f.o.

Your module's name is not derived from ``f00f-code''. That is just the
name of a source file containing some functions to be compiled and
linked into the Lisp executable.  CLISP doesn't care about the name of
this at all, nor does it even require it to exist.