首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LLVM -如何从std::string构造llvm::Value

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

Stack Overflow用户
提问于 2018-02-14 05:07:50
回答 2查看 1.4K关注 0票数 2
代码语言:javascript
复制
std::string myIR = "%3 = alloca i32, align 4";

如何将C++ std::string (如上面的)转换为llvm::Value?我可以把llvm::Instruction放到像this这样的std:string中,答案是这样的,但我不确定如何从std::string转到llvm::Instructionllvm::Value

EN

回答 2

Stack Overflow用户

发布于 2018-02-14 17:25:44

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

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

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

代码语言:javascript
复制
#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有多少,但您可以深入研究。

如果你找到另一种方法,请告诉我们。

票数 0
EN

Stack Overflow用户

发布于 2018-09-26 22:53:49

您可以使用IRBuilder<>.CreateGlobalStringPtrIRBuilder<>.CreateGlobalString函数从stl字符串创建全局字符串:

代码语言:javascript
复制
LLVMContext context;
IRBuilder<> builder(context);

std::string str = "hello world"
Value * v = builder.CreateGlobalString(StringRef(str),"varName");
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48775661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档