如何在LLVM中声明一个全局变量? [英] How can I declare a global variable in LLVM?

查看:2829
本文介绍了如何在LLVM中声明一个全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to record some dynamic behaviors into some global variables. So I wrote a pass to instrument the code and insert some instructions to update the global variable. I tried to use the GlobalVariable constructor to define a global variable, but there are two problems. First, how can I DEFINE the global variables in the module containing main function? Second, how can I DECLARE those global variables in other modules? It's like "extern double someThing;".

目标程序用C编写。

推荐答案

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

There is a tool which can answer this and many other questions about LLVM API: llc -march=cpp. You can generate a bitcode file using Clang or llvm-gcc, and then build a C++ code which should reconstruct the same module using the cpp backend.

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

A sample output, showing how to define a global int * variable:

// 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);

这篇关于如何在LLVM中声明一个全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆