gpt4 book ai didi

compiler-construction - 如何在 LLVM 中声明全局变量?

转载 作者:行者123 更新时间:2023-12-04 00:11:57 26 4
gpt4 key购买 nike

我想将一些动态行为记录到一些全局变量中。所以我写了一个 pass 来检测代码并插入一些指令来更新全局变量。我尝试使用 GlobalVariable 构造函数来定义一个全局变量,但是有两个问题。首先,如何在包含主函数的模块中定义全局变量?其次,如何在其他模块中声明这些全局变量?这就像“extern double someThing;”。

目标程序是用 C 编写的。

最佳答案

有一个工具可以回答这个问题以及关于 LLVM API 的许多其他问题:llc -march=cpp .您可以使用 Clang 或 llvm-gcc 生成一个位码文件,然后构建一个 C++ 代码,该代码应该使用 cpp 重建相同的模块后端。

示例输出,显示如何定义全局 int *多变的:

// Global Variable Declarations

GlobalVariable* gvar_ptr_abc = new GlobalVariable(/*Module=*/*mod,
/*Type=*/PointerTy_0,
/*isConstant=*/false,
/*Linkage=*/GlobalValue::CommonLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"abc");
gvar_ptr_abc->setAlignment(4);

// Constant Definitions
ConstantPointerNull* const_ptr_2 = ConstantPointerNull::get(PointerTy_0);

// Global Variable Definitions
gvar_ptr_abc->setInitializer(const_ptr_2);

关于compiler-construction - 如何在 LLVM 中声明全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787308/

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