【问题标题】:How to get function pointer in LLVM如何在 LLVM 中获取函数指针
【发布时间】:2013-11-12 00:29:11
【问题描述】:

我需要在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

【问题讨论】:

  • 反引号 ` 用于内联代码。代码块缩进四个空格。

标签: c compiler-construction pthreads llvm llvm-ir


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2022-12-01
    • 2013-09-06
    相关资源
    最近更新 更多