解析和修改LLVM IR code [英] Parsing and Modifying LLVM IR code

查看:474
本文介绍了解析和修改LLVM IR code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读(解析)LLVM IR code(保存在一个文本文件)和我自己的一些code添加到它。我需要这样的一些实施例,也就是说,这是如何通过使用由LLVM为此目的提供的库进行。所以基本上我想要的是从一个文本文件中的IR code读入内存(也许是LLVM库重新presents它AST的形式,我不知道),进行修改,比如增加更多的节点在AST,最后写回的IR文本文件中的AST。

I want to read (parse) LLVM IR code (which is saved in a text file) and add some of my own code to it. I need some example of doing this, that is, how this is done by using the libraries provided by LLVM for this purpose. So basically what I want is to read in the IR code from a text file into the memory (perhaps the LLVM library represents it in AST form, I dont know), make modifications, like adding some more nodes in the AST and then finally write back the AST in the IR text file.

虽然我需要读取和修改IR code,我将不胜AP preciate如果有人可以提供或参考我刚刚读(解析)一些例子吧。

Although I need to both read and modify the IR code, I would greatly appreciate if someone could provide or refer me to some example which just read (parses) it.

推荐答案

首先,要解决一个明显的误区:LLVM是在IR格式操纵code的框架。有看不到的AST(*) - 你读IR,变换/处理/分析它,你写回IR

First, to fix an obvious misunderstanding: LLVM is a framework for manipulating code in IR format. There are no ASTs in sight (*) - you read IR, transform/manipulate/analyze it, and you write IR back.

阅读IR是非常简单的:

Reading IR is really simple:

int main(int argc, char** argv)
{
    if (argc < 2) {
        errs() << "Expected an argument - IR file name\n";
        exit(1);
    }

    LLVMContext &Context = getGlobalContext();
    SMDiagnostic Err;
    Module *Mod = ParseIRFile(argv[1], Err, Context);

    if (!Mod) {
        Err.print(argv[0], errs());
        return 1;
    }

    [...]
  }

这code接受一个文件名。这应该是一个LLVM IR文件(文本)。它接着将它解析为模块,从而重新presents IR的模块中的LLVM内部的内存格式。然后可将其与各种通行证LLVM有操纵或添加你自己的。看看在LLVM code碱基一些例子(如的lib /变换/你好/ HELLO.CPP ),并阅读本 - <一个href=\"http://llvm.org/docs/WritingAnLLVMPass.html\">http://llvm.org/docs/WritingAnLLVMPass.html.

This code accepts a file name. This should be an LLVM IR file (textual). It then goes on to parse it into a Module, which represents a module of IR in LLVM's internal in-memory format. This can then be manipulated with the various passes LLVM has or you add on your own. Take a look at some examples in the LLVM code base (such as lib/Transforms/Hello/Hello.cpp) and read this - http://llvm.org/docs/WritingAnLLVMPass.html.

吐痰IR回一个文件更容易。在模块类只是自身写入流:

Spitting IR back into a file is even easier. The Module class just writes itself to a stream:

 some_stream << *Mod;

这就是它。

现在,如果您有任何的具体的有关问题的具体的你想要做的IR code的修改,你应该问的东西更有针对性。我希望这个答案告诉您如何解析IR和写回。

Now, if you have any specific questions about specific modifications you want to do to IR code, you should really ask something more focused. I hope this answer shows you how to parse IR and write it back.

(*)IR不具有内LLVM的AST重新presentation,因为它是一个简单的汇编语言一样。如果你走一步了,到C或C ++,您可以使用铛来解析成的AST,然后在AST水平做操作。锵则知道如何从它的AST产生LLVM IR。但是,你必须开始与C / C ++在这里,而不是LLVM IR。如果LLVM IR是所有你关心的,忘了AST的。

(*) IR doesn't have an AST representation inside LLVM, because it's a simple assembly-like language. If you go one step up, to C or C++, you can use Clang to parse that into ASTs, and then do manipulations at the AST level. Clang then knows how to produce LLVM IR from its AST. However, you do have to start with C/C++ here, and not LLVM IR. If LLVM IR is all you care about, forget about ASTs.

这篇关于解析和修改LLVM IR code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆