llvm ir 返回人类可读的源语言?
有没有一种简单的方法可以从 llvm ir 转到工作源代码?
具体来说,我想从一些简单的 C++ 代码开始,这些代码仅修改 POD(主要是整数、浮点数等数组),将其转换为 llvm ir,对其执行一些简单的分析和翻译,然后将其转换回 C++ 代码?
它并不真正介意任何名称被破坏,我只是希望能够在进行与机器相关的优化之前对源代码进行修改。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上有很多选择。您可能感兴趣的两个是
-march=c
和-march=cpp
,它们是 llc 的选项。运行:
这会将
code.ll
中的 LLVM 位码转换回 C 并将其放入code.c
中。另外:
这与 C 输出引擎不同。它实际上会写出可以运行来重建 IR 的 C++ 代码。我使用此工具将 LLVM IR 嵌入到程序中,而无需处理解析位码文件或任何其他内容。
-march=cpp
有更多选项,您可以通过llc --help
查看,例如-cppgen=
,它控制 IR 的多少输出 C++ 重建。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:
This will convert the LLVM bitcode in
code.ll
back to C and put it incode.c
.Also:
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 withllc --help
, such as-cppgen=
which controls how much of the IR the output C++ reconstructs.CppBackend 已被删除。自 2016-05-05,r268631 以来,我们没有 -march=cpp 和 -march=c 选项。
CppBackend was removed. We have no -march=cpp and -march=c option since 2016-05-05, r268631.
这里有一个问题......可能无法轻松地将 IR 表示回语言。
我的意思是,您可能能够获得一些表示,但其可读性可能较差。
问题是 IR 不关心高级语义,如果没有它......
我宁愿建议你学习阅读 IR。我不用花那么多功夫就能读懂其中的一部分,而且我还远远不是 llvm 专家。
否则,您可以从 IR 中进行 C 代码。它不会与您的 C++ 代码更加相似,但如果没有 ssa 和 phi 节点,您可能会感觉更好。
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.