Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create a C compiler without a C native compiler

It's a simple question. If to compile the C compiler is needed a C compiler... Maybe directly with assembly code? Perhaps the kernel provides a basic tool for converting C to assembler and create an escalating infrastructure? It's a stupid question also but I'm really interested in how to design an operating system (not me) from 0 to interact with the CPU and memory.

like image 314
user1629569 Avatar asked Feb 05 '13 01:02

user1629569


2 Answers

Bootstrapping

This comes from the phrase, "pulling yourself up by your bootstraps", if you want to look it up. Bootstrapping is a straightforward process, but it is a lot of work.

  1. Write a C compiler in another language.

  2. Write a C compiler in C.

  3. Use the compiler from step #1 to compile the compiler from step #2.

Some other compilers have to be bootstrapped, not just those for C. For example, GHC must be used to compile itself.

Note that bootstrapping is only necessary if both of the following are true:

  1. You are inventing a new language, so there are no existing compilers.

  2. You want to write the compiler in the language you are compiling.

This has nothing to do with operating systems. If you are designing an operating system, you have enough work ahead of you already. If your new operating system does not have a C compiler yet, you can cross-compile the compiler from a different operating system. This is much less work — maybe a few hours or days, instead of months or years.

like image 122
Dietrich Epp Avatar answered Nov 15 '22 03:11

Dietrich Epp


It sounds like this is a task for tcc (http://bellard.org/tcc/), the Tiny C Compiler.

It's the smallest C compiler I know of, it can compile a bigger one once you've ported it, and there's numerous guides on how to port it to custom systems.

like image 25
slezica Avatar answered Nov 15 '22 04:11

slezica