ArticleslgStudy

computer science

Self-hosting (compilers)

Self-hosting (compilers) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Self-hosting (compilers) rather than just read about it. In short: In computer programming, self-hosting is the use of a program as part of the toolchain or operating system that produces new versions of that same program—for example, a compiler that can compile its own source code. Compilers Software development using compiler or interpreters can also be self hosted when the compiler is capable of compiling itself.

Key takeaways

  • Self-hosting (compilers) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Self-hosting (compilers) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Self-hosting (compilers) from memory before moving on to harder problems.

Reference excerpt

In computer programming, self-hosting is the use of a program as part of the toolchain or operating system that produces new versions of that same program—for example, a compiler that can compile its own source code.

Compilers Software development using compiler or interpreters can also be self hosted when the compiler is capable of compiling itself. Since self-hosted compilers suffer from the same bootstrap problems as operating systems, a compiler for a new programming language needs to be written in an existing language. So the developer may use something like assembly language, C/C++, or even a scripting language like Python or Lua to build the first version of the compiler. Once the language is mature enough, development of the compiler can shift to the compiler's native language, allowing the compiler to build itself.

Advantages Self-hosting a compiler has the following advantages:

It is a non-trivial test of the language being compiled, and as such is a form of dogfooding. Compiler developers and bug reporters only need to know the language being compiled. Compiler development can be performed in the higher-level language being compiled. Improvements to the compiler's back-end improve not only general-purpose programs but also the compiler itself. It is a comprehensive consistency check as it should be able to reproduce its own object code. Note that some of these points assume that the language runtime is also written in the same language.

History

The first self-hosting compiler (excluding assemblers) was written for Lisp by Hart and Levin at MIT in 1962. They wrote a Lisp compiler in Lisp, testing it inside an existing Lisp Interpreter. Once they had improved the compiler to the point where it could compile its own source code, it was self-hosting.

The compiler as it exists on the standard compiler tape is a machine language program that was obtained by having the S-expression definition of the compiler work on itself through the interpreter. This technique is usually only practicable when an interpreter already exists for the very same language that is to be compiled; though possible, it is extremely uncommon to humanly compile a compiler with itself. The concept borrows directly from and is an example of the broader notion of running a program on itself as input, used also in various proofs in theoretical computer science, such as the proof that the halting problem is undecidable.

Examples Ken Thompson started development on Unix in 1968 by writing and compiling programs on the GE-635 and carrying them over to the PDP-7 for testing. After the initial Unix kernel, a command interpreter, an editor, an assembler, and a few utilities were completed, the Unix operating system was self-hosting – programs could be written and tested on the PDP-7 itself. Douglas McIlroy wrote TMG (a compiler-compiler) in TMG on a piece of paper and "decided to give his piece of paper to his piece of paper", doing the computation himself, thus compiling a TMG compiler into assembly, which he typed up and assembled on Ken Thompson's PDP-7. Development of the GNU system relies largely on GCC (the GNU Compiler Collection) and GNU Emacs (a popular editor), making possible the self contained, maintained and sustained development of free software for the GNU Project. Many programming languages have self-hosted implementations: compilers that are both in and for the same language. An approach is bootstrapping, where a core version of the language is initially implemented using another high-level language, assembler, or even machine language; the resulting compiler is then used to start building successive expanded versions of itself.

See also Bootstrapping Cross-compiler Dogfooding Futamura projection Self-interpreter Self-reference Indirect self-modification

References

Worked examples

Example 1 — a first encounter with Self-hosting (compilers)

Start with the simplest possible case. Write down what Self-hosting (compilers) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Self-hosting (compilers) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Self-hosting (compilers) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Self-hosting (compilers)

In research
Self-hosting (compilers) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Self-hosting (compilers) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Self-hosting (compilers) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming, Self-hosting software, so understanding it makes those chapters shorter.
In everyday life
Look for Self-hosting (compilers) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Self-hosting (compilers)” →

How to study Self-hosting (compilers) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Self-hosting (compilers) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Self-hosting (compilers) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Self-hosting (compilers) in simple terms?

In computer programming, self-hosting is the use of a program as part of the toolchain or operating system that produces new versions of that same program—for example, a compiler that can compile its own source code. Compilers Software development using compiler or interpreters can also be self hos…

Why does Self-hosting (compilers) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Self-hosting (compilers)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Self-hosting (compilers).

Tags

  • Computer programming
  • Self-hosting software

Keep exploring