Compilers

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • How can I interface an interpreter to C code?

    2 answers - 1629 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hello,
    I just finished working through Essentials Programming Languages
    (first edition) and now know enough to write recursive descent
    interpreters for simple languages. The book also explains how to create
    bytecode virtual machines. So far so good.
    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?
    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* and
    then have some kind of "mylanguage.h" header file that exposes various
    data structures of the VM/AST. This is the key step that seems to
    enable the creation of an FFI mechanism (the details of which are a
    mystery to me :-( )
    I am sorry if that is totally wrong. (please correct me). Am i
    totally on the wrong track? Should i now learn how linkers work?
    I am totally confused as to what to study and where to look to
    understand how to write a language with an ffi. Any pointers will be
    very much appreciated.
    Thanks in advance,
    Ravi
    [If you want to make a fully general mechanism, you're on the right
    track and it's a lot of work. the other hand, if you merely have a
    few existing libraries that you want to call, just write some operators
    for the VM that call them. -John]
  • No.1 | | 2198 bytes | |

    "Ravi Mohan" <magicindian@gmail.comwrites:
    >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?


    There are two libraries for doing the actual calling and stuff:
    - ffcall <>, which is
    documented, simpler and a little faster.
    - libffi, where the latest versions are distributed with gcc and are
    pretty much undocumented; it is also slower and more complex.
    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. 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.
    - anton
  • No.2 | | 1321 bytes | |

    When you're running in a VM and you want to call compiled C code, this
    is called a native interface. In the Java world it's called JNI, and in
    the .NET world it's called Platform Invoke (p-invoke).

    As John said, you basically need to create a virtual instruction that
    can call a routine at a specified address. The need to know the address
    is a fundamental requirement, of course.

    Windows we typically call into native DLLs. The virtual machine
    could be given the job of loading the DLL, and then it can query the
    addresses of the public functions.

    In the embedded world, we typically compile code to run in a fixed
    memory range, and we use a vector table to create standard entry points
    the VM can call into.

    of the key requirements is to pass parameters both ways. This
    requires the VM to know the particular calling convention used by the C
    code.

    All this is a little harder if you have a managed memory environment in
    your VM (like the JVM or .NET CLR). It requires a lot of thought on how
    to handle pointers in this kind of situation since it's considered
    "unsafe" to access managed memory areas with a pointer. It's not
    impossible to implement, but you need to be aware of the security
    issues.

Re: How can I interface an interpreter to C code?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 9 =
QUESTION ON "Compilers"

EMSDN.COM