如何编写编译器后端以从 C 代码生成用户定义的硬件架构的程序集
我正在开展一个项目,必须定义新的处理器硬件架构。 我需要一个编译器来生成该目标的汇编代码(它有自己的指令集)。
该处理器的程序将用 C 语言编写。
我的想法是解析 C 代码并生成抽象语法树 (AST),然后从 AST 生成程序集。
当然,我想重用现有组件(我希望不需要重写 C 解析器),但是我可以使用哪些工具或框架来完成此任务?
谢谢。
I am working on a project where I have to define a new processor hardware architecture.
I need a compiler to generate assembly code for this target (it has its own instruction set).
Programs for this processor will be written in C.
My idea to do this is to parse the C code and generate an Abstract Syntax Tree (AST), then from the AST generate the assembly.
Certainly I'd like to reuse existing components (no need to rewrite a C parser I hope), but what tools or frameworks may I use to accomplish this task?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 LLVM。
它由单独的模块组成,这些模块可以单独创建并通过中间语言进行通信。在这种情况下,您必须编写程序集后端并重用其他人的 C 编译器。
Take a look at LLVM.
It consists of seperate modules which can be created individually and communicate through an intermediate language. In you're case you'll have to write the assembly back-end and reuse other people's C compiler.
我认为 GNU GCC 4.5.x 工具链非常出色,因为它现在也可以有插件。创建一个 foo.c 并查看来自 gcc 的原始树转储:
有偏见的观点
我更喜欢使用它而不是 LLVM 进行移植,因为它被广泛采用和移植。 LLVM 提供了您的项目可能不需要的额外抽象级别。不过,两者都要学习,各有利弊。
更多有趣的东西
http://dragonegg.llvm.org/
I think the GNU GCC 4.5.x toolchain is excellent, as it can now have plugins as well. Create a foo.c and have a look at raw tree dumps from gcc:
Biased opinion
I prefer it over LLVM for porting because it's widely adopted and porting. LLVM puts in an extra level of abstraction that you may not need for your project. However, do study both, there are pros and cons.
More fun stuff
http://dragonegg.llvm.org/
您应该查看 LLVM ( http://llvm.org )。
编写编译器绝非易事。我不建议从头开始做。
LLVM 是模块化的,您只需要创建程序集后端。
You should look at LLVM ( http://llvm.org ).
Writing a compiler is far from beeing trivial. I would not suggest doing it from scratch.
LLVM is modular and you will only need to create the assembly backend.
LLVM 是一种选择。您还可以考虑编写一个 gcc 后端,但考虑到 GCC 的复杂性,这会困难得多。
LLVM is one option. You can also consider writing a gcc backend but it will be much harder given how complex GCC is.
Clang + LLV 是选项之一。或者,您可以尝试重新定位 lcc 或 Open64。
lcc 适用于简单的非标准架构,对适当的低级优化希望不大。 LLVM 是寄存器机的最佳选择(但如果您需要分段 16 位内存,则会带来麻烦)。 Open64 提供几乎相同的级别。
重新定位 gcc 也是一种选择,但它需要比其他方法更多的普通体力劳动。
Clang + LLVMis one of the options. Alternatively, you can try retargetting lcc or Open64.
lcc is suitable for simple, non-standard architectures with a little hope for proper low-level optimisation. LLVM is the best choice for register machines (but will cause troubles if you need, say, segmented 16-bit memory). Open64 offers pretty much the same level.
Retargeting gcc is also an option, but it will require much more mundane manual labour than the others.