LLVM:创建一个带有空指针操作数的CallInst

LLVM: Creating a CallInst with a null pointer operand

本文关键字:空指针 操作数 CallInst 一个 创建 LLVM      更新时间:2023-10-16

我试图使用LLVM c++绑定来编写一个生成以下IR的传递

%1 = call i64 @time(i64* null) #3

@time这里是C标准库time()函数。

这是我写的代码

void Pass::Insert(BasicBlock *bb, Type *timety, Module *m) {
  Type *timetype[1];
  timetype[0] = timety;
  ArrayRef<Type *> timeTypeAref(timetype, 1);
  Value *args[1];
  args[0] = ConstantInt::get(timety, 0, false);
  ArrayRef<Value *> argsRef(args, 1);
  FunctionType *signature = FunctionType::get(timety, false);
  Function *timeFunc =
      Function::Create(signature, Function::ExternalLinkage, "time", m);
  IRBuilder<> Builder(&*(bb->getFirstInsertionPt()));
  AllocaInst *a1 = Builder.CreateAlloca(timety, nullptr, Twine("a1"));
  CallInst *c1 = Builder.CreateCall(timeFunc, args, Twine("time"));
}

可以编译,但是在运行

时会导致以下错误
Incorrect number of arguments passed to called function!
  %time = call i64 @time(i64 0)

正如我所理解的,我需要传递一个int64指针,它与nullptr有关,但我无法弄清楚如何做到这一点。

LLVM提供了一个ConstantPointerNull类,这正是我想要的-它返回所需类型的空指针。

所有需要改变的是以args[0] = ...开始的行到args[0] = ConstantPointerNull::get(PointerType::get(timety, 0)); .