[cfe-dev] Call external (non-JIT) function from JIT

Frank Fuchs fk.fuchs at googlemail.com
Tue Jul 13 14:46:48 CDT 2010


Hi I'm rather new to llvm+clang, 
but i'm intrigued by the idea to create some jit "scripting" extension for arbitrary c++ applications 
using llvm and clang (the scripts shall use c or even c++ syntax).
I started looking into the clang-interpreter example which in my eyes is already half way there. 
Now I tried to extend the functionality of the example by adding a mapping for an already defined 
C-function to the JIT. ... Strangely enough the code did compile afterwards, but of course it didn't work.

Here is what I did:

examples/clang-interpreter/main.cpp
int Execute(llvm::Module *Mod, char * const *envp) 
{
.....
  llvm::Function *EntryFn = Mod->getFunction("main");
  // external function
  llvm::Function *KnownFunction = llvm::Function::Create(llvm::TypeBuilder<int(int), false>::get(Mod->getContext()),llvm::GlobalValue::ExternalLinkage, "known", Mod);
  EE->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);

  if (!EntryFn) 
....
}

my_test.cpp:
#include <stdio.h>
int main() 
{
	int i=1;
	i=known(i);
	printf("Ext. CALL %d \n\n",i);
	return 0;
}

tests> clang-interpreter -v my_test.cpp

Here's the error message:
main.cpp:5:4: error: use of undeclared identifier 'known'
        i=known(i);
          ^
1 error generated.

also defining "known" as: extern "C" int known(int);    does not solve the issue, the error is then:
LLVM ERROR: Program used external function 'known' which could not be resolved!


Obviously something else needs to be done in order to resolve the function., but I don't know what. 
So any help is appreciated! 

Thank you in advance
Frank
 






More information about the cfe-dev mailing list