gpt4 book ai didi

c - 如何在 LLVM 中获取函数指针

转载 作者:太空狗 更新时间:2023-10-29 17:23:43 25 4
gpt4 key购买 nike

我需要在我的 LoopPass 中插入 IR 指令来调用 pthread_create,所以我需要将实际函数作为参数传递给 pthread_create应该调用新线程。

目前我已经将在新线程上运行的函数定义为

Function *worker_func = Function::Create(funcType,
GlobalValue::ExternalLinkage,
"worker_func",
CurrentModule);

然后我得到了指向 pthread_create 的指针:

Function *func_pthread_create = dyn_cast<Function>(
TheModule->getOrInsertFunction("pthread_create", pthreadCreateTy));

我需要将一个 Type* 数组作为参数传递给 pthread_create,如下所示。

Value* pthread_create_call = builder.CreateCall(
func_pthread_create, args, "pthread_create");

参数为:

Value* args[4];
args[0] = pthread_t_ld
args[1] = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(getGlobalContext())->getPointerTo());

args[2] = ??? // supposed to be the pointer to worker_func`

args[3] = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(getGlobalContext())->getPointerTo());

那么我怎样才能得到指向这个 worker_func 函数的指针以传递给 pthread_create

最佳答案

您需要将 func_pthread_create 位转换为函数期望的类型,并将该位转换的结果作为第三个参数传递。您可以为此使用静态 ConstantExpr::getBitCast 方法,将函数作为第一个参数传递给它,将 func_pthread_create 的第三个参数的类型作为第二个参数传递给它。

关于c - 如何在 LLVM 中获取函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com