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

Jan Rehders cmdkeen at gmx.de
Thu Jun 7 08:38:36 PDT 2007


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: codegen1.cpp
Type: application/octet-stream
Size: 5240 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20070607/653399de/attachment.obj>


More information about the llvm-dev mailing list