我试图创建一个函数调用,该函数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) 我试图弄清楚如何调整此示例,以便将控制台窗口的输出重定向到另一个进程内的文本框。
不幸的是,读者似乎永远不会收到任何输入。
进一步的调试表明,对的调用SetHandleInformation始终会中止Error 6: Invalid Handle。的价值hPipeOutRd看起来不错,类似于 0x00000244。
这再现了这个问题:
int main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
int result = 0;
HANDLE hPipeOutRd = INVALID_HANDLE_VALUE; // This end is passed to the pipe reader
HANDLE hPipeOutWr = INVALID_HANDLE_VALUE; // This end is passed to the child process
if ( result == 0 && !::CreatePipe( &hPipeOutRd, &hPipeOutWr, &sa, 4096 ) )
{
result = -1; …Run Code Online (Sandbox Code Playgroud)