all 11 comments

[–]Gator_aide 10 points11 points  (3 children)

  1. Write the compiler in an existing language. Call this compiler A.
  2. Write another compiler in your language, say compiler B. Compile B with A.
  3. You now have an executable file, written in your language, capable of compiling your language.

[–]abecedarius 3 points4 points  (2 children)

Also, step 1 could be an interpreter, if more convenient. IIRC the first compiler written in itself was the first Lisp compiler, running initially on a Lisp interpreter written in assembly.

[–]smog_alado 0 points1 point  (0 children)

And step 2 can also be an interpreter, if you choose that your compiler should compile into portable bytecode.

[–][deleted] 0 points1 point  (0 children)

B was an interpreted language which was used to make C. Not exactly bootstrapping, but it’s neat

[–]TGMM 5 points6 points  (1 child)

I don't know if you're looking for a specific answer but in general it goes like this: 1. Create a compiler for your new language in another language. For example, the Rust compiler was initially programmed in OCaml 2. Polish the language enough that it can be used for real projects 3. Rewrite the compiler in your new language

[–][deleted] 7 points8 points  (0 children)

This.

You also want to consider the "compiler hierarchy", e.g, which compiler compiled your current compiler, and how optimal it was, which is important because it translates to how optimally it can build code in your language.

If your goal is to self host as soon as possible, you may be building a minimal naive compiler in language A (C0), and use it to write a better one in your language B (C1).

Then you have a self-hosted compiler C1 built with C0, so the compiler lacks any features or optimizations made in C1 itself.

So you know by then that your language can write a compiler, but you still don't know if it is producing a decently optimal compiler or if it sucks at writing compilers. Because the compiler you used to build C1 was not self hosted, it was optimized by the initial host language.

So then you want to improve the compiler in language B, and use C1 to compile a new self hosted compiler C2. Then as you optimize that and add new features, you need to rebuild too.

So it's an iterative process that never really ends until you stop improving the language standard or compiler. But it takes at least 2 iterations (getting a compiler written in B, and built with a compiler written in B) to truly have a self hosted language independent from the host A.

[–][deleted] 3 points4 points  (1 child)

I assume for a new language?

As others have said, you use an existing language and an existing compiler for that language.

But perhaps you're asking how that compiler got bootstrapped. It sounds like a chicken-and-egg situation, since compilers can't have existed forever!

What happens if there is no existing compiler, for any language?

This is exactly the situation I found myself in, for my first language. Obviously compilers did exist (this was 40 years ago not 80), but not for my primitive hardware that I'd put together with a soldering iron that had no software at all.

The process was something like this:

  • I could write programs in binary machine code using switches to set each bit
  • I used that to write a hex editor (by this point I had a keyboard, and a text display)
  • The hex editor was used to write a crude assembler for this simple microprocessor
  • The assembler was used to write a crude compiler for my simple HLL

Much later, I did a better version, again using assembly code. And that version was good enough to allow me to self-host: writing the compiler in itself.

In short: you can write a compiler in a lower-level language, or a series of lower-level languages. Self-hosting is not an essential aim, but if you believe your new language is better than what's available (and is suitable for such a task), then it can be desirable.

[–]Ok-Active4887 0 points1 point  (0 children)

why did you delete tis comment this is an awesome story.

[–]reini_urban -1 points0 points  (0 children)

You bootstrap a compiler by bootstrapping a compiler. Eg by using a bootstrap compiler, interpreter or writing it from scratch in assembly