How do I parse LLVM IR - llvm

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.

Related

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 to parse PNaCl as human readable LLVM IR?

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.

LLVM for parsing math expressions

I have some troubles wrapping my head around what LLVM actually does...
Am I right to assume that it could be used to parse mathematical expressions at runtime in a C++ program?
Right now at runtime, I'm getting the math expressions and build a C program out of it, compile it on the fly by doing system call to gcc. Then I dynamically load the .so produced by gcc and extract my eval function...
I'd like to replace this workflow by something simpler, maybe even faster...
Can LLVM help me out? Any resources out there to get me started?
You're describing using LLVM as a JIT compiler, which is absolutely possible. If you generate LLVM IR code (in memory) and hand it off to the library, it will generate machine code for you (still in memory). You can then run that code however you like.
If you want to generate LLVM IR from C code, you can also link clang as a library.
Here is a PDF I found at this answer, which has some examples of how to use LLVM as a JIT.

binary generation from LLVM

How does one generate executable binaries from the c++ side of LLVM?
I'm currently writing a toy compiler, and I'm not quite sure how to do the final step of creating an executable from the IR.
The only solution I currently see is to write out the bitcode and then call llc using system or the like. Is there a way to do this from the c++ interface instead?
This seems like it would be a common question, but I can't find anything on it.
LLVM does not ship the linker necessary to perform this task. It can only write out as assembler and then invoke the system linker to deal with it. You can see the source code of llvm-ld to see how it's done.

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.