我似乎无法弄清楚如何为全局整数数组设置 zeroinitializer
。目前我的代码输出:
@a = common global [1 x i32], align 4
但是,clang foo.c -S -emit-llvm
产生:
@a = common global [1 x i32] zeroinitializer, align 4
我的代码目前是这样的,我的setInitializer()
代码不起作用,被注释掉了:
TheModule = (argc > 1) ? new Module(argv[1], Context) : new Module("Filename", Context);
// Unrelated code
// currentGlobal->id is a string, currentGlobal->stype->dimension is the array length
TheModule->getOrInsertGlobal(currentGlobal->id, ArrayType::get(Builder.getInt32Ty(), currentGlobal->stype->dimension));
GlobalVariable* gVar = TheModule->getNamedGlobal(currentGlobal->id);
gVar->setLinkage(GlobalValue::CommonLinkage);
// Not working, not sure if this is how it should be done roughly either
/*gVar->setInitializer(
ConstantArray::get(
ArrayType::get(Builder.getInt32Ty(), currentGlobal->stype->dimension),
ArrayRef(0, currentGlobal->stype->dimension)
)
);*/
gVar->setAlignment(4);
最佳答案
您需要使用 ConstantAggregateZero
的一个实例而不是 ConstantArray
的实例。
=== 注意 - 以下信息已过时 ===
一般来说,如果您想知道如何模拟 Clang 为特定文件生成的内容,您可以 use the C++ backend在 Clang 生成的文件上发出生成相同输出的 C++ 代码。
例如,如果您转到 LLVM demo page ,提供代码 int a[5] = {0};
并选择“LLVM C++ API 代码”目标,您将获得:
// Type Definitions
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(mod->getContext(), 32), 5);
PointerType* PointerTy_1 = PointerType::get(ArrayTy_0, 0);
// Function Declarations
// Global Variable Declarations
GlobalVariable* gvar_array_a = new GlobalVariable(/*Module=*/*mod,
/*Type=*/ArrayTy_0,
/*isConstant=*/false,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"a");
gvar_array_a->setAlignment(16);
// Constant Definitions
ConstantAggregateZero* const_array_2 = ConstantAggregateZero::get(ArrayTy_0);
// Global Variable Definitions
gvar_array_a->setInitializer(const_array_2);
并且您可以在前一代码行中看到 ConstantAggregateZero
的使用。
关于c++ - LLVM,全局整数数组零初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330018/