llvm以char *作为参数调用外部函数

时间:2019-06-21 02:49:21

标签: c++ llvm llvm-clang llvm-c++-api

我想在创建的调用中使用char *作为参数来调用外部函数,但是我一直在努力。

我尝试查看文档,但只显示了如何传递常量整数。

这是代码的相关部分:

// not sure if the line below is correct. I am trying to call an external function: FPRCLAP_path(char * string)
std::vector<llvm::Type*> arg_types = {llvm::Type::getInt8PtrTy(context)};

llvm::FunctionType *function_type = llvm::FunctionType::get(llvm::Type::getVoidTy(context), arg_types, false);
llvm::FunctionCallee instrumentation_function = function.getParent()->getOrInsertFunction("FPRCLAP_path", function_type);

llvm::IRBuilder<> builder(&instruction);
builder.SetInsertPoint(&basic_block, ++builder.GetInsertPoint());
// I want to pass a string for instrumentation but I am not sure how to do it using llvm. I think I am supposed to allocate a string or reference an existing string.
llvm::ArrayRef<llvm::Value*> arguments = {???};
builder.CreateCall(instrumentation_function, arguments);

1 个答案:

答案 0 :(得分:1)

考虑您的代码

llvm::IRBuilder<> builder(&instruction);
builder.SetInsertPoint(&basic_block, ++builder.GetInsertPoint());
// I want to pass a string for instrumentation but I am not sure how to do it using llvm. I think I am supposed to allocate a string or reference an existing string.
llvm::ArrayRef<llvm::Value*> arguments = {???};
builder.CreateCall(instrumentation_function, arguments);

以下代码片段应做您想做的事情

LLVM优化将使该字符串成为本地字符串(如果已运行)。如果您在没有优化的情况下生成IR,则可能会有点草率。

//str is a char*
 llvm::Value *strPointer = program->builder.CreateGlobalStringPtr(str);
//Using a Vector instead of ArrayRef
 const std::vector<llvm::Value *> args{strPointer};
 builder.CreateCall(instrumentation_function, args);