llvm ir back to human-readable source language?

隐身守侯 提交于 2019-11-27 10:51:40

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/5180914/llvm-ir-back-to-human-readable-source-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!