c++ - LLVM IR 中的一切都是指针吗?

标签 c++ llvm llvm-ir

我遍历程序的全局变量并对它们的类​​型感兴趣。

对于测试,例如:

#include <stdio.h>

int i=0;
int main(){
    printf("lala %d \n",i);
    return 0;
}

我得到的输出是:

Globals: 
i Type: 14  //14 ==> POINTER TYPE ID !
StackLock: Stack1 
Function Argument: i32* @i

我的代码:

for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
            std::cout << I->getName().str() << " Type: "<< I->getType()->getTypeID() << std::endl;

            if (I->getType()->isPointerTy() ) {
                std::string o1;
                {
                    raw_string_ostream os1(o1);
                    I->printAsOperand(os1, true);
                }
                char* stackLoc = new char[50];
                sprintf(stackLoc, "Stack%d", StackCounter++);
                errs() << "StackLock: " << stackLoc << "\n";
                errs() << "Function Argument: " << o1 << "\n";
            }

        }

将一切都设置为指针是什么意思? 有什么办法可以采用“真实”类型吗?例如在我的示例中:获取 i 变量的整数类型。

最佳答案

根据 LLVM IR Reference , 全局变量定义了在编译时而不是运行时分配的内存区域,并且它们必须被初始化。

As SSA values, global variables define pointer values that are in scope (i.e. they dominate) all basic blocks in the program. Global variables always define a pointer to their “content” type because they describe a region of memory, and all memory objects in LLVM are accessed through pointers.

要获取全局变量的实际类型,因为全局变量的 LLVM IR 内部类型是指针,Type::getContainedType(int)Type::getPointerElementType() 可用于获取指针的指针类型,因为指针类型是派生类型。

关于c++ - LLVM IR 中的一切都是指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30671585/

相关文章:

llvm-ir - 执行从Python代码生成的LLVM IR代码

llvm - 简化 LLVM SSA 中的表达式

c++ - 从 void 指针到结构的深度复制到另一个变量

c++ - 回溯时如何避免输出参数?

linux - libcxxabi 在 linux 下有意义吗?有什么好处?

c# - 您能否使用 Mono/LLVM 生成比使用 Microsoft 的 C# 编译器更快的 .NET 应用程序?

c++ - 如何在 C++ 中存储此数据集(我使用哪些容器)?

python - Qt 调试器在 mac 上使用错误的 python 版本

基于堆栈的机器的 LLVM 后端

command-line - llvm llc 命令无法识别