Related articles |
---|
[Newbie Alert] How can I interface an interpreter to C code? magicindian@gmail.com (Ravi Mohan) (2005-12-23) |
Re: How can I interface an interpreter to C code? englere.geo@yahoo.com (Eric) (2005-12-24) |
From: | "Eric" <englere.geo@yahoo.com> |
Newsgroups: | comp.compilers |
Date: | 24 Dec 2005 15:36:32 -0500 |
Organization: | http://groups.google.com |
References: | 05-12-063 |
Keywords: | interpreter, design |
Posted-Date: | 24 Dec 2005 15:36:32 EST |
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.
On 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.
One 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.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.