4

我正在做一个项目,我必须定义一个新的处理器硬件架构。我需要一个编译器来为这个目标生成汇编代码(它有自己的指令集)。

该处理器的程序将用 C 语言编写。

我的想法是解析 C 代码并生成抽象语法树 (AST),然后从 AST 生成程序集。

当然我想重用现有的组件(我希望不需要重写 C 解析器),但是我可以使用哪些工具或框架来完成这项任务?

谢谢。

4

5 回答 5

6

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.

于 2011-11-10T08:16:30.303 回答
2

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

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/

于 2011-11-10T08:22:36.487 回答
1

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.

于 2011-11-10T08:16:31.137 回答
0

LLVM is one option. You can also consider writing a gcc backend but it will be much harder given how complex GCC is.

于 2011-11-10T08:22:42.400 回答
0

Clang + LLVM 是其中一种选择。或者,您可以尝试重新定位lccOpen64

lcc 适用于简单的非标准架构,对适当的低级优化有一点希望。LLVM 是寄存器机器的最佳选择(但如果您需要分段 16 位内存,则会引起麻烦)。Open64 提供几乎相同的级别。

重新定位 gcc 也是一种选择,但与其他方法相比,它需要更多的普通体力劳动。

于 2011-11-10T08:27:49.220 回答