llvm 调用一个以 char * 作为参数的外部函数
llvm calling an external function with char * as argument
我想在创建的调用中调用一个带有 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);
相关文章:
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名
- 用作默认参数的静态成员会导致无法解析的外部
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- antlr cpp target 的标头部分不允许使用具有默认参数的外部函数
- 类的成员函数,具有类外部的模板参数和默认参数
- llvm 调用一个以 char * 作为参数的外部函数
- 如何使结构内部的可变参数模板可以在外部访问?
- 当用作模板参数时,功能指针是否需要指向具有外部链接的函数
- 用相同的参数将内部和外部构造函数分开
- 参数内部和外部的指针
- CUDA9.2及以上版本中模板默认参数存在无法解决的外部函数错误
- 在 gcc 中使用数组参数编译外部"c"代码
- 从内部类型参数推断外部类型
- 使用从外部参数包中获取的参数类型声明函数
- 为什么对非类型模板参数的引用需要外部链接
- 用CPP中的参数动态调用外部DLL
- 将参数传递给类外部的基类构造函数
- 如何将 CMake 参数转发到外部项目
- 是否可以在模板外部使用 c++11 参数包
- 指向具有非类型模板参数外部链接的对象的指针