Parsing (and compiling) on demand.

Hi!

I'm trying to develop JIT compiler using LLVM as its backend. I know
LLVM itself supports JIT-compiling, but I need to generate IR first.
I don't want to generate IR before function is actually needed. How
can I achieve this?
If it matters, I have prototypes for all functions I'm going to use.

Best regards,
Milovanov Victor.

lost wrote:

Hi!

I'm trying to develop JIT compiler using LLVM as its backend. I know
LLVM itself supports JIT-compiling, but I need to generate IR first.
I don't want to generate IR before function is actually needed. How
can I achieve this?
If it matters, I have prototypes for all functions I'm going to use.

See ExecutionEngine::InstallLazyFunctionCreator(). http://llvm.org/doxygen/classllvm_1_1ExecutionEngine.html#6a126d6cd1fa07a4331179597de0c46a

Nick

Ok than, but how to insert a call to an undefined function?

lost wrote:

Ok than, but how to insert a call to an undefined function?

You need to have the function declaration and insert a call to that. Once your LazyFunctionCreator is called, you fill in the body and call JIT->getPointerToFunction() on it and return that result.

I haven't actually tried this, but it seems to be the only way to use this API, so I presume someone else has. The only potential problem I can forsee is if the JIT isn't actually re-entrant.

Nick

I actually tried that several months ago and, it turned out, the JIT
wasn't actually reentrant and didn't like that at all.

So you mean that is not the way. But what is InstallLazyFunctionCreator for?

lost wrote:

So you mean that is not the way. But what is InstallLazyFunctionCreator for?

You can use it to return a function pointer when asked for a function by name. It can replace dlsym() style lookups.

I would've expected that given its name it could be used to lazily create functions too, but apparently not.

Nick

Just to do my own try. How can I use it to replace dlsym style
lookups? Any example?

Yeah look in ClamAV's bytecode2llvm.cpp (GPLv2 code), there is a noUnknownFunctions() callback:
http://git.clamav.net/gitweb?p=clamav-devel.git;a=blob;f=libclamav/c%2B%2B/bytecode2llvm.cpp;

Best regards,
--Edwin