英文:
How to initialize LLVM global array with bitcasts?
问题
I apologize for any misunderstanding, but it seems you want assistance with code translation. Here's the translation of your provided code:
我正在尝试创建llvm.used数组,以防止全局变量被优化掉。但它们具有不同的类型,所以我需要使用位转换。期望的结果:
@a = internal global i32 zeroinitializer, section "ab"
@b = internal global i64 zeroinitializer, section "ab"
@llvm.used = appending global [2 x i8*] [
i8* bitcast (i32* @a to i8*),
i8* bitcast (i64* @b to i8*)
], section "llvm.metadata"
我能够创建该数组,但如何将位转换放入初始化程序?
BitCastInst* B1 = new BitCastInst(variableA, Type::getInt8PtrTy(context));
BitCastInst* B2 = new BitCastInst(variableB, Type::getInt8PtrTy(context));
std::vector<Instruction*> elements;
elements.push_back(B1);
elements.push_back(B2);
LU->setInitializer(ConstantArray::get(T, elements));
这不起作用,因为ConstantArray期望一些常量数组,但我有一组指令。
英文:
I'm trying to create llvm.used array to keep globals from being optimized out. But they have different types, so I need to use bitcasts. Expected result
@a = internal global i32 zeroinitializer, section "ab"
@b = internal global i64 zeroinitializer, section "ab"
@llvm.used = appending global [2 x i8*] [
i8* bitcast (i32* @a to i8*),
i8* bitcast (i64* @b to i8*)
], section "llvm.metadata"
I'm able to create that array, but how put bitcasts to initializer?
BitCastInst* B1 = new BitCastInst(variableA, Type::getInt8PtrTy(context));
BitCastInst* B2 = new BitCastInst(variableB, Type::getInt8PtrTy(context));
std::vector<Instruction*> elements;
elements.push_back(B1);
elements.push_back(B2);
LU->setInitializer(ConstantArray::get(T, elements));
This doesn't work because ConstantArray expect some array of Constants, but I have array of Instructions
答案1
得分: 1
你可以使用常量数组和使用 getBitCast() 进行常量转换;ConstantExpr 还包含一些其他可用的转换。
英文:
You can use a constant array of constants and make constant casts using getBitCast(); ConstantExpr also contains some other casts you can use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论