如何为llvm IR调用指令创建结构参数

How can I create a struct parameter for a llvm IR call instruction?

本文关键字:指令 创建 结构 参数 调用 IR llvm      更新时间:2023-10-16

我想在IR文件中插入一条函数调用指令。但是,当我尝试创建一个结构参数时,会出现一些问题。

功能:

 "__myfun(int addr,struct buginfor struct_var)" 

结构为:

 typedef struct buginfor{
    int line;
    char *str1;
    char *str2;
 };

我的代码:

Value* args[] = { addr };
// struct parameter create
Value *struct_line = ConstantInt::get(IntegerType::get(M->getContext(), 64), 4);
Value *struct_filename= llvm::PointerType::get(IntegerType::get(M->getContext(), 8),20);//20 bytes
Value *struct_dir= llvm::PointerType::get(IntegerType::get(M->getContext(), 8),100);//100 tytes
Type* struct_Ty[] = { struct_line->getType(),struct_filename->getType(),struct_dir->getType()};
llvm::StructType * struct_var= llvm::StructType::create(M->getContext(),"buginfor");
struct_var->setBody(struct_Ty);
Value* arg2 = struct_var;
Type* argsTy[] = { addr->getType(),arg2->getType()};
FunctionType *funcTy = FunctionType::get(Type::getVoidTy(M->getContext()), ArrayRef<Type*>(argsTy, 2), false);
Function *hook;
hook = cast<Function>( M->getOrInsertFunction("__myfun", funcTy, attribute(M)));
CallInst *newInst = CallInst::Create(hook, ArrayRef<Value *>(args, 1), "");

有人能告诉我创建buginfo这样的结构参数的正确方法吗?

ps:我想得到三个参数,并使其成为llvm IR constantstruct

          DILocation Loc(N);    // DILocation is in DebugInfo.h
          unsigned Line = Loc.getLineNumber();
          StringRef File = Loc.getFilename();
          StringRef Dir = Loc.getDirectory();

您似乎想要创建一个ConstantStruct或一个undef结构,然后使用insertvalue指令将值插入其中。取决于是否有常量或运行时值。