LLVM,全局整数数组零初始值设定项
LLVM, Global integer array zeroinitializer
我似乎不知道如何为全局整数数组设置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为特定文件生成的内容,可以使用Clang生成的文件上的C++后端来发出生成相同输出的C++代码。
例如,如果您转到LLVM演示页面,提供代码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
的使用。
相关文章:
- 如何反转整数参数包
- 为什么在全局范围内使用"extern int a"似乎不行?
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 当vector是tje全局变量时,c++中vector的内存管理
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何创建一个空的全局类并在启动时实例化它
- 根据全局整数定义整数
- C++全局整数和作用域整数初始值
- 将一系列整数写入全局内存的快速(est)方法
- LLVM,全局整数数组零初始值设定项
- c++从类头文件中访问全局整数
- 为什么c++编译器不将每个整数声明初始化为0,无论是局部的、全局的还是成员的