我正在做一个项目,我必须定义一个新的处理器硬件架构。我需要一个编译器来为这个目标生成汇编代码(它有自己的指令集)。
该处理器的程序将用 C 语言编写。
我的想法是解析 C 代码并生成抽象语法树 (AST),然后从 AST 生成程序集。
当然我想重用现有的组件(我希望不需要重写 C 解析器),但是我可以使用哪些工具或框架来完成这项任务?
谢谢。
我正在做一个项目,我必须定义一个新的处理器硬件架构。我需要一个编译器来为这个目标生成汇编代码(它有自己的指令集)。
该处理器的程序将用 C 语言编写。
我的想法是解析 C 代码并生成抽象语法树 (AST),然后从 AST 生成程序集。
当然我想重用现有的组件(我希望不需要重写 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.
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:
gcc -fdump-tree-original-raw ./foo.c
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.
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 is one option. You can also consider writing a gcc backend but it will be much harder given how complex GCC is.