如何使用位转换初始化LLVM全局数组?

huangapple go评论57阅读模式
英文:

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 &quot;ab&quot;
@b = internal global i64 zeroinitializer, section &quot;ab&quot;

@llvm.used = appending global [2 x i8*] [
		i8* bitcast (i32* @a to i8*),
		i8* bitcast (i64* @b to i8*)
	], section &quot;llvm.metadata&quot;

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&lt;Instruction*&gt; elements;
elements.push_back(B1);
elements.push_back(B2);
LU-&gt;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.

huangapple
  • 本文由 发表于 2023年5月24日 21:04:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定