Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a C compiler for custom CPU?

What would be the easiest way to create a C compiler for a custom CPU, assuming of course I already have an assembler for it?

Since a C compiler generates assembly, is there some way to just define standard bits and pieces of assembly code for the various C idioms, rebuild the compiler, and thereby obtain a cross compiler for the target hardware?

Preferably the compiler itself would be written in C, and build as a native executable for either Linux or Windows.

Please note: I am not asking how to write the compiler itself. I did take that course in college, I know about general compiler-compilers, etc. In this situation, I'd just like to configure some existing framework if at all possible. I don't want to modify the language, I just want to be able to target an arbitrary architecture. If the answer turns out to be "it doesn't work that way", that information will be useful to myself and anyone else who might make similar assumptions.

like image 295
JustJeff Avatar asked Jan 01 '12 23:01

JustJeff


People also ask

How is C compiler written?

Usually, a first compiler is written in another language (directly in PDP11 assembler in this case, or in C for most of the "modern" languages). Then, this first compiler is used to program a compiler written in the language itself. You can read this page about the history of the C language.

Does Windows 10 have a built in C compiler?

Unfortunately, there is no in-built compiler.

Does C have its own compiler?

Yes, C programs are first compiled to generate the object code and then that object code is Run.

What compiler should I use for C?

The compiler that we recommend is the GNU Compiler collection or GCC. This is a widely used cross-platform compiler toolsuite that has libraries and compilers for C, C++, Fortran, Java, and more. Additionally the compiler that we will use later on in the course for compiling C code to run on the PIC32 is based on GCC.


2 Answers

Quick overview/tutorial on writing a LLVM backend.

This document describes techniques for writing backends for LLVM which convert the LLVM representation to machine assembly code or other languages.

[ . . . ]

To create a static compiler (one that emits text assembly), you need to implement the following:

  • Describe the register set.
  • Describe the instruction set.
  • Describe the target machine.
  • Implement the assembly printer for the architecture.
  • Implement an instruction selector for the architecture.
like image 188
Pubby Avatar answered Sep 16 '22 15:09

Pubby


There's the concept of a cross-compiler, ie., one that runs on one architecture, but targets a different one. You can see how GCC does it (for example) and add a new architecture to the set, if that's the compiler you want to extend.

Edit: I just spotted a question a few years ago on a GCC mailing list on how to add a new target and someone pointed to this

like image 27
Ricardo Cárdenes Avatar answered Sep 17 '22 15:09

Ricardo Cárdenes