78

Is it true that the first C compiler was written in C itself? Then, how was it executed and compiled? Or, was this compiler written in assembly language?

Jens
  • 69,818
  • 15
  • 125
  • 179
Jaskaran S.P
  • 1,055
  • 2
  • 14
  • 15
  • 10
    Which c-compiler? – nic Aug 08 '13 at 12:01
  • 21
    [Bootstrapping](http://en.wikipedia.org/wiki/Bootstrapping_%28compilers%29) – P.P Aug 08 '13 at 12:01
  • Do you mean bootstrapping of the very first C compiler? Or of C compilers in general? – Some programmer dude Aug 08 '13 at 12:02
  • 1
    Punchcards. That's why `int` is implicit, so it saves on holes. – Kerrek SB Aug 08 '13 at 12:03
  • 1
    If you are talking about the gcc compiler, see http://stackoverflow.com/questions/5657454/is-gcc-c-compiler-written-in-c-itself – Lake Aug 08 '13 at 12:05
  • 7
    @H2CO3 the egg was first. But which one was first, the _chicken_ egg or the chicken? ;-) – chux - Reinstate Monica Aug 08 '13 at 12:06
  • 6
    Don't you know it's written in emacs lisp! M-x build-c-compiler – Skizz Aug 08 '13 at 12:28
  • 1
    I am baffled at the downvotes and the votes to close. The question isn't – user207421 Aug 08 '13 at 12:29
  • 1
    A much simpler subset of a language X can be implemented in another language (such as assembly, etc). Then one can write a compiler for the full language X set using the language X subset. – lurker Aug 08 '13 at 13:08
  • @chux, it follows logically that the chicken came first. ;) – Devolus Aug 08 '13 at 17:30
  • related: [How create a C compiler without a C native compiler](http://stackoverflow.com/questions/14698786/how-create-a-c-compiler-without-a-c-native-compiler) – mctylr Aug 08 '13 at 19:41
  • @mah: A tiny bit more critical thinking should convince you that it would have been perfectly possible to write the first C compiler in C, and hand-translate it into something else. – Keith Thompson Feb 08 '15 at 18:56
  • @KeithThompson it doesn't require critical thinking to realize that something is perfectly possible, just the normal kind of thinking will generally do. One must apply critical thinking to reach the point of recognizing how unlikely it would have been. – mah Feb 09 '15 at 19:28
  • @mah: [The first Lisp compiler was written in Lisp](https://en.wikipedia.org/wiki/History_of_compiler_construction#Lisp). It was initially tested using a Lisp interpreter. I don't know if there are examples of a compiler for language X being written in X and hand-translated, but it seems entirely plausible. – Keith Thompson Feb 09 '15 at 19:32
  • possible duplicate of [Writing a compiler in its own language](http://stackoverflow.com/questions/193560/writing-a-compiler-in-its-own-language). The specific question about C may be off topic because it is a historical question. – Ciro Santilli OurBigBook.com Sep 06 '15 at 08:30

3 Answers3

106

It is described pretty well in Dennis Ritchie's writeup of the C language history.

Giving just a summary of what he wrote there, use his article for the finer details. C started with the BCPL language, Ken Thomson had access to a compiler for it that ran on their General Electrics 635 main frame. Unhappy with the language, Thomson used BCPL to write a compiler for the B language, an evolutionary step beyond BCPL that removed some of the technical problems in BCPL.

They used B to create programs on their PDP-7 minicomputer, although most of them were little more than toy programs, the machine had very restricted hardware capabilities. One important step taken by Thomson was to rewrite the B compiler in B itself. A common bootstrapping step.

That compiler was then gradually tweaked, through a short-lived intermediary step called NB (New B) to start resembling C. The PDP-11 minicomputer was instrumental in that step, giving them enough room to improve the language and compiler.

Garry Shutler
  • 32,260
  • 12
  • 84
  • 119
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 32
    Someone put one of Dennis Ritchie's early PDP-11 versions on github for posterity: https://github.com/mortdeus/legacy-cc [source](http://thechangelog.com/explore-a-piece-of-unix-history-dennis-ritchies-earliest-c-compilers/) – sehe Aug 08 '13 at 15:39
  • Wow. That looks like an interesting hack :) _(For a moment I thought that it was a sign of garbage collection in ephemeral C compiler :<)_ – sehe Aug 08 '13 at 23:32
  • 5
    Link above is broken - for posterity, I believe this is the same write-up: https://www.bell-labs.com/usr/dmr/www/chist.html – tonysdg Jan 21 '16 at 19:36
33

The first C compiler wasn't written in C, usually when writing a compiler we use either assembly language, or another programming language, and it's common that after first compilation, the compiler is rewritten in its native language.

There are a lot of programming languages that were first written in C then rewritten in their native language: Java for example, Ada ...

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
  • 5
    Actually I don't know the last time a compiler was written in assembly. I believe that quickly went out of style by the mid-1960s. While at least several of the compilers of the 1950s were written in assembly (Hopper's famous first compiler for the A-0 language in 1952, and IBM (Backus et team) FORTRAN in 1957. By 1960 at least one compiler, namely the COBOL compiler for UNIVAC-II was written in FLOW-MATE. Src: [History of compiler construction](https://en.wikipedia.org/wiki/History_of_compiler_construction) Since then most were bootstrapped from existing high-level languages. – mctylr Aug 08 '13 at 19:26
  • 1
    Hi, in the legacy c compiler: https://github.com/mortdeus/legacy-cc we can see that the entry point is an assembly written macro, and the code base is 17% assembly. – Belkacem REBBOUH Aug 09 '13 at 14:15
  • 2
    I've skimed the legacy-cc as well, the assembly part is necessary I believe because it has to output assembly at some point. I think the assembly in legacy-cc are mostly the templates that the compiler will output. But I'd also like to point that assembly is not necessarily to be avoided like pest, Chris Sawyer is said to have developped Roller Coaster Tycoon in assembly ! If you master the language it can be productive as well. – v.oddou Mar 28 '14 at 00:53
  • 4
    The first Ada compilers were not written in C. The one I worked on (at TeleSoft) was written in Pascal, and then re-implemented in Ada. I believe the very first validated Ada compiler was written in SETL. – Keith Thompson Feb 08 '15 at 18:55
  • 4
    just a minor nitpick: the java language has a compiler written in java and a virtual machine written in C++. – Alexander Oh Feb 08 '15 at 20:35
  • Writing C compiler in assembly is not that hard. This has been done many times and even recently. E.g. cc_* family of compilers for a few different architectures (x86, amd64, aarch64, riscv64): https://github.com/oriansj/stage0-posix/blob/master/riscv64/cc_riscv64.M1 – Andrius Štikonas Oct 15 '21 at 14:54
10

Reading Dennis Ritchie's note on primevalC indicates that, rather like the chicken and the egg, C evolved from a precursor language along with its own compiler via bootstrapping. The speciation timescales were quicker than for Gallus gallus domesticus.

One can even see the compiler source code caught in the midst of the evolutionary turmoil. The note links to two snapshots of it restored from tape backups. This code has also been placed on github. These snapshots constitute intermediate fossils, years before K&R syntax. Dennis' description of one of the tapes shows it is a freeze-frame of structs being bootstrapped:

"prestruct-c" is a copy of the compiler just before I started changing it to use structures itself.

In other words the compiler had been augmented to support structs but didn't yet use them. A good time to make a tape backup...

egyik
  • 363
  • 2
  • 12