使用c++api将c++回调函数转换为llvm
translating c++ callback function to llvm using c++ api
我想使用c++api将c回调函数转换为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函数(一个c回调函数)的参数转换为相应的llvm函数。使用API有可能这样做吗?
我真的很感激任何想法。
感谢
compare
是指向函数编译代码所在位置的指针。那里没有源代码——事实上,它甚至可能不是从C编译的——所以LLVM不能用它做太多事情,也不能将它转换为LLVM Function
对象。
可以插入对该函数的调用:从该compare
指针创建一个LLVM Value
,将其强制转换为适当的类型,然后使用该强制转换的指针创建一条调用指令作为函数。使用API插入这样的调用看起来像:
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);
相关文章:
- 防止主数据类型C++的隐式转换
- 将尾部调用void(i32,..)位转换为llvm::函数以获取FnAttribute
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- LLVM 无法将数组类型强制转换为常量数组
- 目标代码可以转换回LLVM IR吗?
- LLVM:如何将一些指令/内在的指令转换为几个链式说明
- 使用 LLVM 从 const void * 转换为模板类型时出现问题
- LLVM-如何将ConstantExpr转换为ConstantDataArray,以便打印全局变量char*的值
- 将 GCC IR 转换为 LLVM IR
- 奇怪的 LLVM 在 For 循环中无符号到有符号的转换
- LLVM IR生成的代码转换为本机代码
- 从OSX平台转换并使用gcc和llvm时生成失败
- LLVM强制转换说明
- 将 LLVM 位码加载到模块中:无法将"std::unique_ptr<llvm::Module>"转换为"llvm::Module*"
- 对于具有不同位数的类型,LLVM 类型转换失败
- C++11将传统指针转换为智能指针.使用来自llvm的最新SVN-Clang和libc++
- 使用c++api将c++回调函数转换为llvm
- llvm- c++ -4.2和c++新型类型转换
- 如何检查llvm callInst是否包含位转换
- 通过LLVM/Clang实现c++与MyLanguage的转换