Self-hosting (compilers)

Short description: Software that can produce new versions of itself

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. Self-hosting software is commonplace on personal computers and larger systems. Other programs that are typically self-hosting include kernels, assemblers, command-line interpreters and revision control software.

## Contents

* 1 Operating systems
* 2 Compilers
* 3 History
* 4 Examples
* 5 List of languages having self-hosting compilers
* 6 See also
* 7 References

## Operating systems

An operating system is self-hosted when the toolchain to build the operating system runs on that same operating system. For example, Windows can be built on a computer running Windows.

Before a system can become self-hosted, another system is needed to develop it until it reaches a stage where self-hosting is possible. When developing for a new computer or operating system, a system to run the development software is needed, but development software used to write and build the operating system is also necessary. This is called a bootstrapping problem or, more generically, a chicken or the egg dilemma.

A solution to this problem is the cross compiler (or cross assembler when working with assembly language). A cross compiler allows source code on one platform to be compiled for a different machine or operating system, making it possible to create an operating system for a machine for which a self-hosting compiler does not yet exist. Once written, software can be deployed to the target system using means such as an EPROM, floppy diskette, flash memory (such as a USB thumb drive), or JTAG device. This is similar to the method used to write software for gaming consoles or for handheld devices like cellular phones or tablets, which do not host their own development tools.

Once the system is mature enough to compile its own code, the cross-development dependency ends. At this point, an operating system is said to be self-hosted.

## Compilers

Software development using compiler or interpreters can also be self hosted when the compiler is capable of compiling itself.[1]

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.

## History

Main article: History of compiler construction

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.[2]

> 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.

— AI Memo 39[2]

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.[3] 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.[4]

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.[3]

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.

## List of languages having self-hosting compilers

The following programming languages have self-hosting compilers:[citation needed]

* Ada
* ALGOL (Burroughs B5000)
* BASIC[5]
* BCPL
* C
* C++ (Visual C++, clang, gcc 4.8)
* C# (Microsoft Roslyn, Mono)
* ClojureScript[6]
* CoffeeScript
* Crystal
* Curry
* D
* Dart
* Delphi
* Dylan
* Eiffel
* Elixir
* F#
* FASM[7]
* Factor
* Forth
* Gambas
* Go
* Haskell[8]
* Idris
* Java
* Kotlin
* Lisp (Common Lisp)
* LiveScript
* Mercury
* Nemerle
* Nim
* Oberon
* Object Pascal (Free Pascal)
* OCaml
* Pascal (Free Pascal)
* Pyret[9]
* Python (PyPy)
* Raku (Rakudo)
* Rust
* Scala
* Scheme
* Smalltalk
* Standard ML (MLton)
* Tcl[10]
* TMG
* TypeScript
* Vala
* Virgil[11]
* Visual Basic .NET (Microsoft Roslyn, Mono)
* Zig

## See also

* Bootstrapping (compilers)
* Compiler-compiler
* Cross-compiler
* Dogfooding
* Futamura projection
* Self-interpreter
* Self-reference
* Indirect self-modification

## References

1. ↑ Heaton, Robert. "What is a self-hosting compiler?". https://robertheaton.com/2017/10/24/what-is-a-self-hosting-compiler/.
2. ↑ 2.0 2.1 Tim Hart and Mike Levin. "AI Memo 39-The new compiler". ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-039.pdf.
3. ↑ 3.0 3.1 Ken Thompson. "VCF East 2019 -- Brian Kernighan interviews Ken Thompson". https://www.youtube.com/watch?v=EY6q5dv_B-o&t=2330.
4. ↑ Dennis M. Ritchie. "The Development of the C Language". 1993.
5. ↑ BASICO compiler bootstrapping example
6. ↑ ClojureScript Next
7. ↑ "flat assembler". https://flatassembler.net/download.php. "The flat assembler is self-hosting and the complete source code is included."
8. ↑ "Haskell Communities and Activities Report". https://www.haskell.org/communities/05-2016/html/report.html#sect3.4.
9. ↑ https://www.pyret.org
10. ↑ "Implement TCL in TCL". Archived from the original on 2017-06-04. https://web.archive.org/web/20170604205050/http://wiki.tcl.tk/12646. Retrieved 2017-09-19.
11. ↑ "Virgil". https://github.com/titzer/virgil.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Self-hosting (compilers). Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Self-hosting_(compilers)&oldid=260883"