简体   繁体   English

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

[英]Is everything a pointer in LLVM IR?

I iterate through the global vars of the program and am interested for their types. 我遍历程序的全局变量并且对它们的类​​型感兴趣。

For a test, like: 对于测试,例如:

#include <stdio.h>

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

What I get on the output is: 我得到的输出是:

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

My code: 我的代码:

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";
            }

        }

What's the meaning of setting everything as pointers? 将所有内容设置为指针的含义是什么? Is there any way to take the 'real' type? 有没有办法采取'真实'类型? eg on my example: to get the Integer Type for i variable. 例如,在我的例子中:获取i变量的Integer Type。

According to LLVM IR Reference , Global variables define regions of memory allocated at compilation time instead of run-time, and they must be initialized. 根据LLVM IR Reference ,全局变量定义在编译时而不是运行时分配的内存区域,并且必须对它们进行初始化。

As SSA values, global variables define pointer values that are in scope (ie they dominate) all basic blocks in the program. 作为SSA值,全局变量定义程序中所有基本块的范围内(即它们占主导地位)的指针值。 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中的所有内存对象都是通过指针访问的。

To get the actual type of your global variable, since the global variable's LLVM IR internal type is pointer, the Type::getContainedType(int) or Type::getPointerElementType() could be used to get the pointee type of the pointer, since pointer type is a derived type. 要获取全局变量的实际类型,由于全局变量的LLVM IR内部类型是指针,因此可以使用Type::getContainedType(int)Type::getPointerElementType()来获取指针的指针类型,因为指针type是派生类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM