26

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.

Dan
  • 33,953
  • 24
  • 61
  • 87
  • 7
    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. –  Mar 03 '11 at 13:02

3 Answers3

37

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.

eush77
  • 3,870
  • 1
  • 23
  • 30
Evan Phoenix
  • 371
  • 2
  • 2
  • 6
    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 07 '17 at 22:15
  • 4
    @aerkenemesis: Official support for the C and C++ backends were removed as they were lagging behind and there was not enough interest in maintaining them, so this option is no longer possible: see http://releases.llvm.org/3.1/docs/ReleaseNotes.html#changes . In the mean time, the Julia team maintains their own unofficial C backend at https://github.com/JuliaComputing/llvm-cbe . – Matthieu M. Sep 23 '19 at 10:53
25

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

Boris Ulasevich
  • 249
  • 3
  • 2
10

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.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722