Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

llvm ir back to human-readable source language?

Tags:

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.

like image 357
Dan Avatar asked Mar 03 '11 12:03

Dan


Video Answer


1 Answers

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.

like image 124
Evan Phoenix Avatar answered Sep 19 '22 14:09

Evan Phoenix