如何在LLVM中获取函数指针

6
我需要在我的 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函数呢?

2
反引号 ``` 用于内联代码。缩进四个空格用于代码块。 - Jonathon Reinhart
1个回答

5

您需要将func_pthread_create进行位转换(bitcast)成函数所期望的类型,并将该位转换的结果作为第三个参数传递。您可以使用静态的ConstantExpr::getBitCast方法来实现,将函数作为第一个参数传递,将func_pthread_create第三个参数的类型作为第二个参数传递。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接