How to parse PNaCl as human readable LLVM IR? - llvm

I have a .pexe and want to get the LLVM IR code. How can this be done? This is the file I want to convert to LLVM IR: NaClAMBullet.pexe
I don't understand why it is not already readable LLVM IR code:
A PNaCl portable executable (pexe in short) is a single LLVM IR module.
Source: Google Native Client Reference

The PNaCl toolchain contains many tools to manipulate PNaCl and LLVM files, including pnacl-dis and pnacl-bcdis. A .pexe is indeed create from a single LLVM IR module, but it ins't the same as an LLVM IR module: the PNaCl bitcode is stable and different from LLVM IR.

Related

Is it possible to find the LLVM version from textual LLVM IR .ll file?

I have several .ll files containing LLVM IR code in textual form. I want to filter the files depending on their LLVM version for example I would like to find all the files that use LLVM version 3-7.
Currently, I have tried to convert .ll file to .bc file using llvm-as tool and tried using llvm-bcanalyzer to get some useful information such as the required version number but it seems that I was mistaken and llvm-bcanalyzer does not provide such information.
So is there any way to find out which version of LLVM was used to write a given .ll file?
Actually, the version of the entire LLVM toolchain will be same as version of the clang used to generate the .ll file, as clang is a part of LLVM toolchain only.
Just open the .ll file and search clang version and you will be able to find something like clang version 12.0.0

How can I generate LLVM code coverage in IR representation?

The tutorial (http://logan.tw/posts/2015/04/28/check-code-coverage-with-clang-and-lcov/) has an example to C code. I need evaluate the coverage on LLVM IR code.
How can I do this using clang or other tool?
You can do it the same way -- the coverage instrumentation pass is implemented at the IR level, and clang can accept IR files (.ll) as input. Just make sure those files have debug info.

LLVM IR bitcode file versioning

I have LLVM IR files in textual form that were compiled targeting different LLVM versions. In my LLVM IR interpreter I want to identify which parser I should select to parse such a file, since the textual format is not backward compatible. However, I cannot see a version ID in these files.
If I instead change my parsers to parse the bit code files, I have longer compatibility guarantees. There is also a version field but the documentation mentions that it is currently always 0.
So is there any way to identify the LLVM version of a bit code file, or do I have no other choice than letting the user specify the version?

How do I parse LLVM IR

I have LLVM IR code in text format. What I wanna do is to be able to parse it and modify that code. Is there an API which can help in parsing the LLVM IR code? What libraries should I have in my system? At this moment I have clang compiler installed as well LLVM, as I can use commands such as llc, opt and llvm-link.
LLVM is primarily a C++ library. It has all the tools you can imagine to parse, manipulate and produce IR in both textual and bitcode (binary) formats.
To get started, take a look at the llvm::ParseIRFile function, defined in header include/llvm/Support/IRReader.h.
The best way to proceed would be to download the LLVM source code and build it, following these instructions. It's then easy to write your own code that uses the LLVM libraries.

How to embed LLVM?

The LLVM Core project consists of:
Compiler - converts source code to LLVM IR
VM - executes compiled IR code
How can I embed the VM to a C++ application?
The LLVM is really a collection of libraries that you can link to, so it's pretty easy to embed. More often the LLVM takes IR that you generate and compiles it directly to machine code. There is also a library available to interpret and execute IR for platforms that do not support JIT compilation.
There's a pretty good tutorial available on the LLVM website here: http://llvm.org/docs/tutorial/. I suggest that you go through that and then ask more specific questions if you have them.
Take a look at the HowToUseJIT example in LLVM.