有什么方法可以获取llvm顺从指针值的原始类型(即指针类型)
Is any way to get llvm deference pointer value's raw type(i.e. pointer type)
也许标题有点混乱。但让我给你举个例子。
void foo(int val)
{
// do something
}
int i = 27;
int* pi = &i;
foo(*pi);
这里,如果我们使用clang编译它,*pi的类型将是i32,但我们知道pi是指针类型。
我的问题是我们使用函数::getgetFunctionParamType方法,结果将是i32。但是我如何使用一些方法来获得"pi"类型,而不是"*pi"类型?这个问题让我困惑了好几天。
更新:
我看到有些人混淆了这个问题。好吧,我已经把这个源代码编译成了LLVM中间格式的flie(即.ll文件),所以我已经达到了生成中间代码的步骤,我可以处理的是与LLVM IR相关的,我只能看到i32、i32*等等(现在没有int、int*)。我不想构造一个指针类型,我只是想以某种方式将*pi"反转"为pi,这样我就可以检查"pi"是指针类型。情况是这样的:我在.ll文件中有*pi,可能pi是
%pi = alloca i32*, align 32
%1 = load i32** %pi, align 32
%2 = load volatile i32* %1, align 1
%3 = call foo(i32 %2)
所以如果我检查函数的参数类型,我只能得到i32,因为它现在是pi。但如果我能得到pi,即%pi=alloca i32align 32,我就能知道pi是指针类型。
我想您正在寻找PointerType* PointerType::get(Type*)
如果我正确理解你的问题,你需要的是调用函数的CallInst
的操作数,而不是函数声明。
假设您的Function* F
指向foo(i32)
:
(我在脑海中写道,如果它不能编译,很抱歉)
for(auto U : F->users())
{
if (CallInst* CI = dyn_cast<CallInst>(U))
{
Value* O0 = CI->getOperand(0) // we simply know foo needs one operand
if (Constant* C = dyn_cast<Constant>(O0))
{
// parameter is a constant you can get the type as usual from every value
}
else if (LoadInst* LI = dyn_cast<LoadInst>(O0))
{
// since the argument is not a constant it must be a value loaded by
// a LoadInst and LoadInst has the function getPointerOperand()
Value* PO = LI->getPointerOperand();
// since we know it's a pointer Operand we can cast safely here
PointerType* PT = cast<PointerType>(PO->getType());
PT->dump(); // will print i32*
}
}
}
相关文章:
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- (C )找到基本类型指针的儿童类型
- 表达式必须具有指向对象的指针类型(指针向量)
- 将子项复制构造到父类型指针中
- C 返回类型指针声明
- 调用虚拟函数而不通过类类型指针创建任何对象
- C++ 如何使用类类型指针制作向量
- 为什么基类型指针不能获取派生类对象的地址值?
- 如何在 C++ 中获取映射类型指针,映射
- 如何检查该类型的类型指针是正确对齐的
- 正在转换为短类型指针
- 指向任意类方法的模板非类型指针
- 如何通过强制转换类型指针将字符数组转换为uint16_t
- 指向shared_ptr的不透明类型 C 指针
- C++:从值类型指针强制转换为包含迭代器
- C/C++:访问给定类型指针的位置与访问另一类型指针的相同位置不同
- 比较类型指针
- 数据类型指针使用*(Datatype *)
- 如何打印c++中char类型指针的所有值