Is there an easy way of going from llvm ir to working source code?

Specifically, I'd like to start with some simple C++ code that merely modifies PODs (mainly arrays of ints, floats, etc), convert it to llvm ir, perform some simple analysis and translation on it and then convert it back into C++ code?

It don't really mind about any of the names getting mangled, I'd just like to be able to hack about with the source before doing the machine-dependent optimisations.

share|improve this question
5  
LLVM assembly is relatively readable, provided you're familiar with low level details. You can instruct llc to output C code (-march=c) - however, the result won't be pretty. – delnan Mar 3 '11 at 13:02
up vote 4 down vote accepted

There is an issue here... it might not be possible to easily represent the IR back into the language.

I mean, you'll probably be able to get some representation, but it might be less readable.

The issue is that the IR is not concerned with high-level semantic, and without it...

I'd rather advise you to learn to read the IR. I can read a bit of it without that much effort, and I am far from being a llvm expert.

Otherwise, you can C code from the IR. It won't be much more similar to your C++ code, but you'll perhaps feel better without ssa and phi nodes.

share|improve this answer
    
good advice. thanks – Dan Mar 10 '11 at 9:41
    
what is a phi node? phi = ? – paulm Dec 25 '14 at 20:28
    
@paulm: It is intrinsic to SSA. – Matthieu M. Dec 26 '14 at 13:39

There are number of options actually. The 2 that you'll probably be interested in are -march=c and -march=cpp, which are options to llc.

Run:

llc -march=c -o code.c code.ll

This will convert the LLVM bitcode in code.ll back to C and put it in code.c.

Also:

llc -march=cpp -o code.cpp code.ll

This is different than the C output engine. It actually will write out C++ code that can be run to reconstruct the IR. I use this personal to embed LLVM IR in a program without having to deal with parsing bitcode files or anything.

-march=cpp has more options you can see with llc --help, such as -cppgen= which controls how much of the IR the output C++ reconstructs.

share|improve this answer
2  
With my own (recent - i.e. 4.0) builds of LLVM, llc --version lists about 30 targets, but C and C++ are not included. I tried using Ubuntu's apt-get to install the older LLVM (3.8), and see that it does include the C++ backend, but not the C backend. The "Building LLVM with CMake" page (llvm.org/docs/CMake.html) indicates that all targets are built by default. Have the C and C++ targets recently been removed? – user2023370 Nov 21 '16 at 10:00
    
Same here, 4.0 SVN build is missing the c and cpp backends – aerkenemesis Jan 7 at 22:15

CppBackend was removed. We have no -march=cpp and -march=c option since 2016-05-05, r268631.

share|improve this answer
1  
Any more info, such as reasons why or the actual notice of deprecation? – Coder256 Dec 11 '16 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.