首页 > 解决方案 > 提取 PointerType 中包含的值

问题描述

LLVM 红外

  call void @llvm.dbg.declare(metadata i32* %z, metadata !24, metadata !DIExpression()), !dbg !25
  %arraydecay1 = getelementptr inbounds [55 x i8], [55 x i8]* %input, i32 0, i32 0, !dbg !26
  %call2 = call i64 @strlen(i8* %arraydecay1) #4, !dbg !28

strlen 文档https://www.cplusplus.com/reference/cstring/strlen/

%call2 是 strlen 函数的返回值,它是 size_t 类型。我以为它是结构类型,但结果是指针类型,它是 i64 (i8*)* 类型

我如何取消引用并获取指针值中包含的整数值。

编辑:我使用了错误的操作数,但问题还没有解决。它不是关于指针类型,我有问题的 int 64 类型转换。见下文

 CallInst I; //passed by reference
  CallSite cs(&I);
  if(!cs.getInstruction()){
    return;
  } else {
    for (User* user : cs.getInstruction()->users()) {
      if (Instruction* i = dyn_cast<Instruction>(user)) {
        Value *v1 = dyn_cast<Value>(i->getOperand(0));
        errs() << "Type:==" << *(v1) << "\n";
        errs() << "is integer type=" << (v1->getType()->isIntegerTy()) << "\n";
        ConstantInt *cint = dyn_cast<ConstantInt>(v1);
        if (cint) {
          errs() << "constant int" << *cint << "\n";
        } else {
          errs() << "not a constant int??" << "\n";
        }
      }
    }
  }

Type:==  %call2 = call i64 @strlen(i8* %arraydecay1) #4, !dbg !28
is integer type=1
not a constant int??

标签: llvmllvm-irllvm-c++-api

解决方案


如果你说%call2的是 call 的返回值strlen,那么它的类型就是i64它所写的call i64 @strlen(...)

i64 (i8*)*类型是指向接受i8*和返回的函数的指针i64。它对应&strlen于 C 中的表达式类型。

因此,如果您想使用 的返回值strlen,那么只需使用%call2value.


推荐阅读