[LLVMdev] Please help with LLVM C++ integration

Gordon Henriksen gordonhenriksen at me.com
Tue Aug 19 14:06:36 PDT 2008


On Aug 19, 2008, at 16:39, John Criswell wrote:

> kirill havok wrote:
>
>> I wrote a small example, but while running I get an error("Tied to  
>> execute an unknown external function"), where I am wrong?
>>
>
> I think the problem is that some_test_func() is a C++ function, so  
> its name is being mangled during compilation.
>
> To fix it, I think you want to add a declaration telling the  
> compiler to treat some_test_func() as a C function.  Something like  
> the below will work:
>
> extern "C" {
>    int some_test_func(int);
> }
>
> int some_test_func (int) {
>    <code for function here>
>    ...
> }

John, that wouldn't much matter since he's registering the function by  
hand using addGlobalMapping. It could influence the calling  
convention, but the linkage is rendered irrelevant.

Kirill, your test program looks nearly correct to me. Try stepping  
through runFunction to see what's gone awry.

>> int some_test_func( int ){
>>        std::cout << "!!!!" << std::endl;
>>        return 8848;
>> }
>>
>> int _tmain(int argc, _TCHAR* argv[]){
>>
>>        Module *M = new Module("test");
>>        ExistingModuleProvider* MP = new ExistingModuleProvider(M);
>>        ExecutionEngine* EE = ExecutionEngine::create(MP, false);
>>
>>        std::vector<const Type *> func_args;
>>        func_args.push_back(Type::Int32Ty);
>>        FunctionType * func_type = FunctionType::get(Type::Int32Ty,  
>> func_args, false);
>>        Function * my_function = Function::Create( func_type,
>> Function::ExternalLinkage, "some_test_func", M );
>>        EE->addGlobalMapping( my_function, (void*)&some_test_func );
>>
>>        Function *FooF = cast<Function>(M->getOrInsertFunction("foo",
>> Type::Int32Ty, (Type *)0));
>>        BasicBlock * BB = BasicBlock::Create("EntryBlock", FooF);
>>        Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
>>        CallInst *Add1CallRes = CallInst::Create(my_function, Ten,
>> "some_test_func", BB);
>>        Add1CallRes->setTailCall(true);
>>
>>        ReturnInst::Create(Add1CallRes, BB);
>>
>>        std::cout << "We just constructed this LLVM module:\n\n" <<  
>> *M;
>>        std::cout << "\n\nRunning foo: " << std::flush;
>>
>>        std::vector<GenericValue> noargs;
>>        GenericValue gv = EE->runFunction(FooF, noargs);
>>
>>        std::cout << "Result: " << gv.IntVal.toStringUnsigned(10) <<  
>> "\n";
>>        return 0;
>> }



More information about the llvm-dev mailing list