Vem*_*ulo 5 c++ llvm llvm-c++-api
我想在堆栈上创建 LLVM ArrayType 所以我想使用AllocaInst (Type *Ty, Value *ArraySize=nullptr, const Twine &Name="", Instruction *InsertBefore=nullptr). 问题是我不明白这个界面。我猜那Ty会是类似的东西ArrayType::get(I.getType(), 4),但我应该付出什么ArraySize。此外,它需要Value*,所以它让我很困惑。
要么我误解了 llvm alloc,要么我需要提供一个 llvm 常量作为数组大小的值。如果我必须给出常量,是不是有点多余,因为ArrayType包含 numElement 作为信息。
作为示例代码行,我尝试的方式是:
AllocaInst* arr_alloc = new AllocaInst(ArrayType::get(I.getType(), num)
/*, What is this parameter for?*/,
"",
funcEntry.getFirstInsertionPt());
Run Code Online (Sandbox Code Playgroud)
I 是数组元素的类型,例如:
Type* I = IntegerType::getInt32Ty(module->getContext());
Run Code Online (Sandbox Code Playgroud)
然后你可以创建一个num元素的ArrayType:
ArrayType* arrayType = ArrayType::get(I, num);
Run Code Online (Sandbox Code Playgroud)
该类型可以在 AllocInstr 中使用,如下所示:
AllocaInst* arr_alloc = new AllocaInst(
arrayType, "myarray" , funcEntry
// ~~~~~~~~~
// -> custom variable name in the LLVM IR which can be omitted,
// LLVM will create a random name then such as %2.
);
Run Code Online (Sandbox Code Playgroud)
此示例将产生以下 LLVM IR 指令:
%myarray = alloca [10 x i32]
Run Code Online (Sandbox Code Playgroud)
编辑 此外,似乎您可以将可变数组大小传递给 AllocInstr,如下所示:
Type* I = IntegerType::getInt32Ty(module->getContext());
auto num = 10;
auto funcEntry = label_entry;
ArrayType* arrayType = ArrayType::get(I, num);
AllocaInst* variable = new AllocaInst(
I, "array_size", funcEntry
);
new StoreInst(ConstantInt::get(I, APInt(32, 10)), variable, funcEntry);
auto load = new LoadInst(variable, "loader", funcEntry);
AllocaInst* arr_alloc = new AllocaInst(
I, load, "my_array", funcEntry
);
Run Code Online (Sandbox Code Playgroud)