我正在玩LLVM.我想过在中间代码中改变常量的值.但是,对于类llvm :: ConstantInt,我没有看到setvalue函数.任何想法如何修改IR代码中常量的值?
我试图创建一个函数调用,该函数foo接收函数指针(带签名void bar(void))作为参数.从这篇文章我得到了完成这个的基本想法.首先我注册Foo功能.Bar实际上是一个由LLVM编译的函数,因此没有必要的注册.
FunctionType* BarType = FunctionType::get(Type::getVoidTy(getGlobalContext()), false);
Type* FooType[1];
FooType[0] = static_cast<Type*>(BarType)->getPointerTo();
ArrayRef<Type*> FooTypeARef(FooType, 1);
FunctionType* signature = FunctionType::get(Type::getInt32Ty(getGlobalContext()), FooTypeARef, false);
Function* func = Function::Create(signature, Function::ExternalLinkage, "Foo", TheModule);
LLVM_ExecutionEngine()->addGlobalMapping(func, const_cast<void*>(&Foo));
Run Code Online (Sandbox Code Playgroud)
这是插入实际调用的方式(案例I)
std::vector<Value*> ArgsV_Foo;
Function *FooFun= LLVM_Module()->getFunction("Foo");
Function* BarFun = LLVM_Module()->getFunction("Bar");
ArgsV_Foo.push_back( BarFun );
LLVM_Builder()->CreateCall(FooFun, ArgsV, "calltmp")
Run Code Online (Sandbox Code Playgroud)
但是,这会在CreateCall内部中止,原因是"调用带有错误签名的函数".我不确定这条线ArgsV_Foo.push_back( BarFun )是否正确.
我觉得可能的另一种方法是使用ExecutionEngine来获取指向Bar的指针,但我不明白如何将生成的函数指针转换为llvm::Value*(Case II)
std::vector<Value*> ArgsV_Foo;
Function *FooFun= LLVM_Module()->getFunction("Foo");
Function* BarFun = LLVM_Module()->getFunction("Bar");
void* BarFunPtr = LLVM_ExecutionEngine()->getPointerToFunction(BarFun);
Value* val …Run Code Online (Sandbox Code Playgroud)