std::string myIR = "%3 = alloca i32, align 4";如何将C++ std::string (如上面的)转换为llvm::Value?我可以把llvm::Instruction放到像this这样的std:string中,答案是这样的,但我不确定如何从std::string转到llvm::Instruction或llvm::Value。
发布于 2018-02-14 17:25:44
我不确定您想要完成什么(看起来有点可疑),因为打印或转换为字符串的典型用法是为了调试目的(链接的SO Q&A )。
话虽如此,也许您可以看看Parser.h头文件的llvm::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有多少,但您可以深入研究。
如果你找到另一种方法,请告诉我们。
发布于 2018-09-26 22:53:49
您可以使用IRBuilder<>.CreateGlobalStringPtr或IRBuilder<>.CreateGlobalString函数从stl字符串创建全局字符串:
LLVMContext context;
IRBuilder<> builder(context);
std::string str = "hello world"
Value * v = builder.CreateGlobalString(StringRef(str),"varName");
...https://stackoverflow.com/questions/48775661
复制相似问题