LLVM IR 中的一切都是指针吗?
Is everything a pointer in 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";
}
}
把所有东西都设置成指针是什么意思?
有什么办法可以取'real'类型吗?例如在我的示例中:获取 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()
来获取指针的指针类型,因为指针类型是派生类型。
我遍历程序的全局变量并对它们的类型感兴趣。
对于测试,例如:
#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";
}
}
把所有东西都设置成指针是什么意思? 有什么办法可以取'real'类型吗?例如在我的示例中:获取 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()
来获取指针的指针类型,因为指针类型是派生类型。