Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If the first C compiler was written in C... how could it be first C compiler? How could you compile the first C compiler?


The first (or proto) C compiler was written in B†[1] (called NB, or New B). This is the first C compiler written in C.

† Or maybe some variant of BCPL -- I'm not exactly sure how functionally different the two were.

[1] https://www.bell-labs.com/usr/dmr/www/chist.html


The very first B compiler was written in BCPL by Ken Thompson. B later became self-hosting, i.e. the BCPL compiler compiled the B compiler, but this had another set of challenges due to the extreme memory constraints. It was an iterative process where a new feature was added such that it pushed the memory limit and then the compiler was rewritten to use the new feature to bring the memory usage down.

C was heavily inspired by B and I suspect written in B aswell. Alternatively, BCPL was extremely portable as it compiled to OCode (what we'd recognise today as bytecode) so that might have been another option. The assignment operators of =+ are straight from B and later changed to += due to Dennis Ritchie's personal taste.


The first B compiler was actually written in TMG, and once it was bootstrapped that way in B itself. BCPL was only the inspiration for the language.


Wow, TMG was a new one for me. From the Wiki article on it:

"Douglas McIlroy ported TMG to an early version of Unix. According to Ken Thompson, McIlroy wrote TMG in TMG on a piece of paper and "decided to give his piece of paper his piece of paper," hand-compiling assembly language that he entered and assembled on Thompson's Unix system running on PDP-7."

We are not worthy, friends. We are not worthy.


I did pretty much the same thing with https://github.com/kragen/peg-bootstrap/blob/master/peg.md, although admittedly hand-compiling to JS was noticeably less work than hand-compiling to assembly language would have taken. My friend Dave did something similar with Val Schorre's META-II: https://queue.acm.org/detail.cfm?id=2724586 (missing the figures for some reason, so see:) doi:10.1145/2697401


Ehh...I taught myself machine code programming when I was 11. Hand translating programs I wrote in Assembler on paper to byte code and typing it in byte by byte. And I am no programming God. So it might be less hard than you think :)


We are merely observing the footsteps of compiler gods.


How was gcc bootstrapped?




If it could bootstrap itself, then there would be no need to port it to GCC.

From how I read it, it is not capable of bootstrapping itself, and an earlier C compiler in BCPL existed, this is the first C compiler written in C itself.


this port is [optionally] a cross compiler - it will run on x86/arm/whatever and produce pdp11 assembly

on an actual pdp11 it CAN bootstrap


It was bootstrapped. For compilers, it is sort of "a thing" to finally bootstrap your new language compiler in your new language.


The question of if the first C compiler was written in C, how could it be the first C Compiler?

Because to be the first, it has to be bootstrapped in an intermediate host language… You have to get a parser running, then the syntax, then the etc… etc…

( immense plug of the Ahl book here…)

To be the first complier in a language, as was pointed out, long before I was born, the compiler has to compile itself, so before it could compile itself, it had to have other language processing programs creating the parsing, the syntax, the etc…

Porting it to GCC just means that they could compile it with GCC, the big test is to get it to compile itself, on what ever platform that is the target platform, because finally, if it cannot generate object code/machine language in the target machine’s binary, then its not really ported.

Later on, UNIX came with tools to build compilers with, YACC and LEX.

If they got it to produce PDP-7 Code, its not really much of a port, really.


It was a rhetorical question.

It wasn't the first C compiler, it was the first self-hosted C compiler, which is different.



Probably out of topic, but are there real examples of compiler attacks due to bootstrapping ? I did not hear about them before reading the scifi classic Accelerando by C. Stross


Oh, the thing with bootstrapping attacks is you never know are there any real examples.

I recommend this[0] paper by Ken Thompson dated 1984 and still relevant.

[0]: https://www.cs.cmu.edu/~rdriley/487/papers/Thompson_1984_Ref...


Brilliant. I was going to post this but you posted it first, I read this while learning to program in C, and then looked at my compiler with extreme suspicion.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: