gpt4 book ai didi

LLVM - 如何从 std::string 构造 llvm::Value

转载 作者:行者123 更新时间:2023-12-05 07:37:12 27 4
gpt4 key购买 nike

std::string myIR = "%3 = alloca i32, align 4";

如何将 C++ std::string(如上面的)转换为 llvm::Value?我可以将 llvm::Instruction 放入 std:string 中,例如 this所以答案显示,但我不确定如何从 std::stringllvm::Instructionllvm::Value.

最佳答案

我不确定您要完成什么(似乎有点可疑),因为打印或转换为字符串的典型用法是用于调试目的(您链接的 SO Q&A)。

话虽如此,也许你可以看看 Parser.hllvm::parseAssemblyString() 函数头文件(另请注意,在其他变体中也有一个 llvm::parseAssemblyString() 函数)。这需要至少将您的 IR 包装到一个函数中(您可以创建一个虚拟的 void foo(void) 一个或类似的东西)。

一个最小(不完整)的例子是(至少 LLVM 3.9.0):

#include <memory>
// using std::unique_ptr

#include "llvm/IR/LLVMContext.h"
// using llvm::LLVMContext

#include "llvm/IR/Module.h"
// using llvm::Module

#include "llvm/IR/Verifier.h"
// using llvm::verifyModule

#include "llvm/AsmParser/Parser.h"
// using llvm::parseAssemblyString

#include "llvm/Support/SourceMgr.h"
// using llvm::SMDiagnostic

#include "llvm/Support/raw_ostream.h"
// using llvm::raw_string_ostream

#include "llvm/Support/ErrorHandling.h"
// using llvm::report_fatal_error

std::string myAsmIR = "...";
llvm::LLVMContext theContext;
llvm::SMDiagnostic theDiagnostic;

std::unique_ptr<llvm::Module> myModule = llvm::parseAssemblyFile(myAsmIR, theDiagnostic, theContext);

// for verifying and printing diagnostics from the parsed file/string
std::string msg;
llvm::raw_string_ostream os(msg);
theDiagnostic.print("", os);

if(llvm::verifyModule(*myModule, &(llvm::errs()))
llvm::report_fatal_error(os.str().c_str());

这将为您提供一个 llvm::Module,您可以用通常的方式处理它(例如,*myModule->begin() 用于获取第一个函数)。

最后,您已将您的问题标记为“llvm-3.0”,但这是一个很旧的版本,因此不确定该 API 有多少,但您可以四处挖掘。

如果您找到其他方法,请告诉我们。

关于LLVM - 如何从 std::string 构造 llvm::Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48775661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com