【问题标题】:llvm calling an external function with char * as argumentllvm 以 char * 作为参数调用外部函数
【发布时间】:2019-06-21 02:49:21
【问题描述】:

我想在创建的调用中以 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);

【问题讨论】:

  • 分配一个常量字符串(我指的是 C 的东西,而不是一个合理的字符串类的实例)isn't at all difficult。 getString() 的返回值是一个值(通过大约六个中间类),因此您可以将其用作调用指令的操作数。分配可修改的东西是更多的工作,但别担心,继续努力,它会来找你的。
  • @arnt 谢谢。当我回到我的研究计算机时,我会尝试。如果我有任何其他问题,将更新。

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


【解决方案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);

下面的sn-p应该做你想做的事情

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);

【讨论】:

  • 对不起。在看到通知之前,我忘了回到这里。在你的帮助下,我设法做到了。再次感谢您。
  • 没问题的伙伴:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多