[LLVMdev] How to call native functions from bytecode run in JIT?

Evan Cheng evan.cheng at apple.com
Sat Jun 9 21:08:01 PDT 2007


Are you able make calls to well known external functions such as  
printf? As far as I known, this capability is well tested on x86 /  
Linux.

I am wondering if there is some name mangling issue?

Evan

On Jun 7, 2007, at 8:38 AM, Jan Rehders wrote:

> Hello,
>
> can anyone help me calling native functions from LLVM-Bytecode  
> functions run in the JIT?
>
> I have a program which creates an LLVM execution engine and adds  
> modules and functions
> to it on the fly. I need to call some native functions of my  
> program from bytecode functions which causes some troubles as it  
> appears not to be documented. My test scenario works like the  
> following:
>
> I have a simple function "int get5()" which I want to call from a  
> bytecode function "int callget5()" which is generated at runtime. I  
> register the native function "get5" using addGlobalMapping in the  
> execution engine. I then try to call it using runFunction. This  
> works under OS X, but fails in Linux giving something like "tried  
> to call unknown function int* () get5". Calling module->getFunction 
> ("foo5") does not return NULL, though. Some relevant code snippets:
>
> // the native function
>
> int get5() { return 5; }
>
> // registering it
>
> std::vector<const Type*> emptyArgList;
> FunctionType* get5Type = FunctionType::get(Type::Int32Ty,  
> emptyArgList, false);
> Function* get5Function = new Function(get5Type,  
> GlobalValue::ExternalLinkage, "get5", m);
> EE->addGlobalMapping( get5Function, (void*)get5 );
>
> // verifying it's existance
>
> Function* func = m->getFunction("get5");
> if( func == NULL ) {
>   std::cout << "registered native function not found\n";
>   exit(-1);
> }
>
> // calling it
>
> EE->runFunction(func, noargs).IntVal.getLimitedValue()
>
> As said, it will fail calling the function in Linux (but works in  
> OS X). Version is LLVM 2.0, OS X 10.4.9, several linux distros
>
> How can I fix this?
>
> When I try to construct a function which will call "get5" it will  
> fail on OS X, too. I have attached the whole .cpp file containing  
> the complete code. It's adapted from the JIT example in the LLVM  
> source distribution. I compile it using
>
> g++ -c codegen1.cpp -o codegen1.o -D__STDC_LIMIT_MACROS
> g++ -o codegen1 -L${LLVM_LIB_DIR} codegen1.o ${LLVM_LIB_DIR}/ 
> LLVMPowerPC.o -lLLVMSelectionDAG ${LLVM_LIB_DIR}/LLVMInterpreter.o $ 
> {LLVM_LIB_DIR}/LLVMJIT.o -lLLVMCodeGen -lLLVMScalarOpts - 
> lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis ${LLVM_LIB_DIR}/ 
> LLVMExecutionEngine.o -lLLVMTarget -lLLVMCore -lLLVMSupport - 
> lLLVMbzip2 -lLLVMSystem -lpthread -ldl
>
> greetings,
> Jan Rehders
>
> <codegen1.cpp>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list