Hi, I still have some primary problems about using string
in llvm pass.
I wanna use string as argument, like that:
extern "C" void logvar(int i, std::string name) {
std::cout << "Num: " << i << "; Name: " << name << std::endl;
}
And pass.cpp
// Get the function to call from our runtime library.
LLVMContext &Ctx = F.getContext();
std::vector<Type*> paramTypes = { // Param Types
Type::getInt32Ty(Ctx),
Type::getInt8PtrTy(Ctx) // HERE: use *i8 to represent string, is it right?
};
Type *retType = Type::getVoidTy(Ctx);
FunctionType *logFuncType = FunctionType::get(retType, paramTypes, false);
FunctionCallee logFunc = F.getParent()->getOrInsertFunction("logvar", logFuncType);
Insert logvar in pass.cpp
for store instruction such as store i32 0, i32* %retval, align 4
// for store instruction
void insertLogvar(StoreInst *inst, BasicBlock &B, FunctionCallee logFunc, LLVMContext &Ctx) {
IRBuilder<> builder(inst);
builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
Value *argi = inst->getOperand(0); // integer
if (auto constant_int = dyn_cast<ConstantInt>(argi)) {
Value* argstr = ? // HERE
Value* args[] = {argi, argstr};
builder.CreateCall(logFunc, args);
} else {
errs() << "store inst has no instance number" << "\n";
}
}
My question is how to accomplish above code? To match string type.
Best wishes!