gpt4 book ai didi

c++ - 如何使用整数列表在 LLVM 中初始化整数数组?

转载 作者:行者123 更新时间:2023-12-03 18:00:52 27 4
gpt4 key购买 nike

我有以下要为其生成 C++ 的 IR 代码:

@gArray = global [10 x i32] [i32 3, i32 4, i32 5, i32 6, i32 0, i32 0, i32 0, i32 0, i32 12, i32 0], align 16

我知道我可以使用这些代码行将数组初始化为全零:
    ConstantAggregateZero* const_array_2 = ConstantAggregateZero::get(ArrayTy_0);
GArray->setInitializer(const_array_2);

如何将 LLVM 中的数组初始化为值列表?

最佳答案

您可以创建一个常量初始化列表:

std::vector<llvm::Constant*> values;
...
/* Make the value 42 appear in the array - ty is "i32" */
llvm::Constant* c = llvm::Constant::getIntegerValue(ty, 42);
values.push_back(c);
... // Add more values here ...
llvm::Constant* init = llvm::ConstantArray::get(arrayTy_0, values);
GArray->setInitializer(init);

这段代码(以及它之前的 20 行左右)创建了一个初始化的全局结构:
https://github.com/Leporacanthicus/lacsap/blob/master/expr.cpp#L2585

这是另一个使用 setInitializer 的示例 - 同样,它不是数组而是结构体,但从概念上讲,数组和结构体之间没有太大区别:
https://github.com/Leporacanthicus/lacsap/blob/master/expr.cpp#L3376

另见(例如):
http://llvm.org/docs/doxygen/html/classllvm_1_1ConstantArray.html

关于c++ - 如何使用整数列表在 LLVM 中初始化整数数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42255453/

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