"Ravi Mohan" <magicindian@gmail.com> writes:>I now wish to learn how to interface c code to the languages I create.>In other words, I would like to call existing C libraries from the>languages I create.>>I am utterly at a loss how to do this. Can someone please point me at>any book or website that explains how to interface existing C code to>an interpreter/bytecode based vm?

- libffi, where the latest versions are distributed with gcc and are
pretty much undocumented; it is also slower and more complex. On
the plus side, when you debug programs that call through libffi, the
backtrace works (it does not with ffcall).

It is probably a good idea to look at examples of their use. One such
example is in the Cacao JVM interpreter:

Look at the function nativecall() (towards the end of the file). This
example contains support for both ffcall and libffi (call only, no
callbacks or other advanced features).

In addition, you will probably want to use dlopen() and dlsym() to get
the addresses of the functions you want to call.

>From what I understand from a survey of various languages with ffi,>the first step would seem to be to write a vm/interpreter *in C*

That is not necessary, but the libraries above have C interfaces, so
you need to be able to at least call C from your implementation
language. Many people write VM interpreters in C (or in languages
that compile to C, like Vmgen), though, because C (especially with the
GNU C extensions) is quite good for the task.

>and>then have some kind of "mylanguage.h" header file that exposes various>data structures of the VM/AST.

Such a header file is useful if the C code (whether it is the callee
of the interpreter or the caller) should be able to access these
structures. It is not necessary if you just want to call C-library
functions.