2

我想使用 c++ api 将 ac 回调函数转换为 llvm 函数。我的示例 C++ 函数如下所示。

extern "C" void bindMe(int(*compare)(const int a))
{

    llvm::LLVMContext& context = llvm::getGlobalContext();
    llvm::Module *module = new llvm::Module("top", context);
    llvm::IRBuilder<> builder(context);

    //I want to create the corresponding llvm function here which is called compareLLVM

    llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entrypoint", compareLLVM);
    builder.SetInsertPoint(entry);

    module->dump();

}

基本上我想将bindMe函数的参数(ac回调函数)转换为相应的llvm函数。使用 API 可以实现这样的事情吗?

我真的很感激任何想法。

谢谢

4

1 回答 1

2

compare is a pointer to where the compiled code of a function resides. There's no source code there - in fact, it might even not have been compiled from C - so there's not much LLVM can do with it, and you cannot convert it to an LLVM Function object.

What you can do is insert a call to that function: create an LLVM Value from that compare pointer, cast it to the appropriate type, then create a call instruction with that casted pointer as the function. Inserting such a call with the API will look something like:

Type* longType = Type::getInt64Ty(context);
Type* intType = Type::getInt32Ty(context);
Type* funcType = FunctionType::get(intType, intType, false);

// Insert the 'compare' value:
Constant* ptrInt = ConstantInt::get(longType, (long long)compare); 

// Convert it to the correct type:
Value* ptr = ConstantExpr::getIntToPtr(ptrInt, funcType->getPointerTo());

// Insert function call, assuming 'num' is an int32-typed
// value you have already created:
CallInst* call = builder.CreateCall(ptr, num);
于 2013-07-09T09:24:35.863 回答