c++ - 从 std::istream 读取 LLVM IR

标签 c++ llvm llvm-ir

之前我曾询问过如何将 IR 写入文件或从文件中读回 IR。读取的代码如下所示:

LLVMContext ctx;
SMDiagnostic diag;
Module *m = ParseIRFile( "my_file", diag, ctx );

但是,我尝试改造 LLVM IR 的代码只传递给我一个 std::istream&。如何从 std::istream 读取 IR?

我想出了如何使用raw_os_ostreamstd::ostream改编为raw_ostream来编写模块,但没有明显的适应读取代码的方法,例如,没有适应 std::istreamMemoryBuffer (除非我错过了)。

最佳答案

您应该使用 ParseIR() 而不是 ParseIRFile()。它获取一个 MemoryBuffer 作为参数,而不是文件名。您可以通过 getMemBuffer() 工厂方法从 StringRef 创建一个 MemoryBuffer:

/// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
/// that InputData must be null terminated if RequiresNullTerminator is true.
static MemoryBuffer *getMemBuffer(StringRef InputData,
                                  StringRef BufferName = "",
                                  bool RequiresNullTerminator = true);

由于 StringRef 可以(甚至隐式)从 std::string 构造,因此您需要做的就是 convert your std::istream to std::string .

关于c++ - 从 std::istream 读取 LLVM IR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13013270/

相关文章:

C++模板类编译报错信息: error: expected a qualified name after 'typename'

opencl - 如何将 opencl-kernel-file(.cl) 编译为 LLVM IR

c++ - 确定数组索引

c++ - 列表框对齐问题

c++ - OpenGL 的缓冲区是如何工作的?

haskell - 将 G-Machine 源转换为 LLVM IR

c++ - 如何使函数的 LLVM IR 可用于我的程序?

c++ - 二维数组的初始化如何工作?

C++ 与 Qt4 插件开发

c - 使用 LLVM : Unknown token in expression 的 intel 语法内联汇编