How to write a compiler to create an assembly for a user-defined hw architecture, from C code

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 purpose (it has its own set of commands).

Programs for this processor will be written to C.

My idea to do this is to parse the C code and create an abstract syntax tree (AST), then create an assembly from AST.

Of course, I would like to reuse existing components (no need to rewrite the C parser, I hope), but what tools or frameworks can I use to accomplish this task?

Thanks.

+4
source share
5 answers

Take a look at LLVM .

It consists of separate modules that can be created individually and interact through an intermediate language. In this case, you will need to write a back-end assembly and reuse other people's C. compiler.

+6
source

You should look at LLVM ( http://llvm.org ).

Writing a compiler is far from trivial. I would not suggest doing it from scratch.

LLVM is modular and you only need to create a server assembly.

+2
source

I think the GNU GCC 4.5.x toolchain is excellent, as it now has plugins. Create foo.c and look at the source tree dumps from gcc:

gcc -fdump-tree-original-raw ./foo.c 

Preconceived notion

I prefer its LLVM for porting because it is widely accepted and portable. LLVM adds an extra layer of abstraction that you might not need for your project. However, study both, there are pros and cons.

More fun stuff

http://dragonegg.llvm.org/

+2
source

LLVM is one option. You may also consider creating a gcc backend , but it will be much more complicated if you have complex GCC.

0
source

Clang + LLVM is one of the options. Alternatively, you can try redirecting lcc or Open64 .

lcc is suitable for simple, custom architectures with little hope of proper low-level optimization. LLVM is the best choice for registration machines (but will cause problems if you need, say, segmented 16-bit memory). Open64 offers almost the same level.

Retargeting gcc is also an option, but it will require much more conventional manual labor than others.

0
source

Source: https://habr.com/ru/post/1380528/


All Articles