gpt4 book ai didi

c++ - LLVM,用值 0 初始化一个整数全局变量

转载 作者:可可西里 更新时间:2023-11-01 18:36:54 25 4
gpt4 key购买 nike

我一直在 LLVM 文档/Stack Overflow 中兜圈子,无法弄清楚整数全局变量应该如何初始化为 0(第一次使用 LLVM)。这是我目前的一些代码:

TheModule = (argc > 1) ? new Module(argv[1], Context) : new Module("Filename", Context);

// Unrelated code

// currentGlobal->id is just a string
TheModule->getOrInsertGlobal(currentGlobal->id, Builder.getInt32Ty());
llvm::GlobalVariable* gVar = TheModule->getNamedGlobal(currentGlobal->id);
gVar->setLinkage(llvm::GlobalValue::CommonLinkage);
gVar->setAlignment(4);

// What replaces "???" below?
//gVar->setInitializer(???);

这几乎可以满足我的要求,它可以产生一个输出示例:

@a = common global i32, align 4
@b = common global i32, align 4
@c = common global i32, align 4

但是,clang foo.c -S -emit-llvm 也产生了我想要的:

@a = common global i32 0, align 4
@b = common global i32 0, align 4
@c = common global i32 0, align 4

据我所知,我需要一个Constant*,其中有"???",但我不确定如何去做:http://llvm.org/docs/doxygen/html/classllvm_1_1GlobalVariable.html#a095f8f031d99ce3c0b25478713293dea

最佳答案

使用one of the APInt constructors获得 0 值 ConstantInt(AP 代表任意精度)

ConstantInt* const_int_val = ConstantInt::get(module->getContext(), APInt(32,0));

然后设置你的初始化值(一个常量子类)

global_var->setInitializer(const_int_val);

关于c++ - LLVM,用值 0 初始化一个整数全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23328832/

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