c++ - llvm 以 char * 作为参数调用外部函数
问题描述
我想在创建的调用中使用 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);
解决方案
考虑你的代码
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);
推荐阅读
- python - 课堂练习题中的Python语法错误
- html - 我无法让我的 flexbox 在 css 中使用“justify-content : center”将内容居中
- javascript - 无论如何在我调试调用堆栈时检查未调用的异步任务队列
- javascript - 为什么我在 express 中收到 404 错误?我已在视图和路由文件夹中正确创建了文件
- javascript - 如何使用 Node.js 和 MongoDB 实现“滚动加载更多”?
- reactjs - Gatsby 在没有服务器的情况下作为本地文件 (index.html) 运行
- java - 如何在 POJO/Model 中使用 FieldValue.serverTimestamp() 获取状态时间戳?
- javascript - 如何在javascript中删除第二类html元素?
- javascript - 检测图像中的特定文本
- python - 如何将 pytorch 模型保存在 redis 缓存中以更快地访问模型以进行视频流传输?