top | item 26697061

First C compiler ported to GCC

137 points| vegesm | 5 years ago |github.com | reply

87 comments

order
[+] ggm|5 years ago|reply
The

  a[b] implemented as *(a+b)
Thing, is how we were taught to think about array indexing in the CS lectures of the 70s
[+] st_goliath|5 years ago|reply
And that's how it's still taught nowadays.

Both the C89 and the C99 standard draft contain the following:

> The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2)))

In fact the expressions a[b] *(a + b) and b[a] are equivalent.

Here is a perfectly valid snippet of C code that will print out 't':

    putchar(3["test"]);
[+] sgtnoodle|5 years ago|reply
Are those not equivalent expressions in modern C?

I imagine there are more optimal and less optimal ways of actually doing the indexing in machine code and the former may be better semantics, but I would think a compiler would generate identical machine code for both.

[+] raverbashing|5 years ago|reply
Hence why it "can be written" as b[a] as well

Edit: it doesn't blow up, not even with -Wall and -std=c99

[+] nspain|5 years ago|reply
That's how we were taught a few years ago too! It really helped it "click" that array elements are stored contiguously.
[+] dvko|5 years ago|reply
It’s also in the K&R book IIRC, stating that the two are equivalent.
[+] burstmode|5 years ago|reply
Did this compiler really support "auto" as a variable type, as seen in example fizzbuzz ?
[+] bawolff|5 years ago|reply
Auto is not a type, it means local variable. I think its still a thing, just its the default so nobody uses it.
[+] layoutIfNeeded|5 years ago|reply
Hehe, “auto” used to mean “automatic storage” aka the stack. Then much later C++ repurposed the keyword for type deduction.
[+] captainmuon|5 years ago|reply
"auto" probably is the storage class, it tells what kind of variable this is. Automatic as opposed to "register" which would force the variable to be a register, or "static" or "extern".

The type is not given at all, I think by default it would be "int".

[+] IncRnd|5 years ago|reply
No. "auto" is not a type but a storage class that means automatically allocated instead of being allocated to a register, extern-al to the file, or in the static code segment.
[+] Taniwha|5 years ago|reply
"The compiler runs only in 32 bit mode as the original code assumes that the pointer size and word size are the same." ... which was um, 16-bits
[+] vesinisa|5 years ago|reply
I think he means that int and pointer address must be interchangeable. As long as that holds, the size can be either 16 bits or 32 bits.

On a PDP-11 int would have been 16-bit. On x86 32 bits. But on x86_64 int is 32 bits but pointers are 64-bit. The easiest way to retain the original assumption with minimal changes to the historical source code while targeting a modern CPU is to compile in 32-bit mode.

[+] nils-m-holm|5 years ago|reply
Yes, and on 16-bit and 32-bit systems, sizeof(int) == sizeof(int*). On 64-bit systems, this is most probably not the case. This is a common roadblock when porting old C programs.
[+] 29athrowaway|5 years ago|reply
If the first C compiler was written in C... how could it be first C compiler? How could you compile the first C compiler?
[+] dvt|5 years ago|reply
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

[+] onei|5 years ago|reply
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.

[+] IncRnd|5 years ago|reply
It was bootstrapped. For compilers, it is sort of "a thing" to finally bootstrap your new language compiler in your new language.
[+] ForOldHack|5 years ago|reply
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.

[+] polska|5 years ago|reply
I do not understand why we should create a C compiler ported to GCC.
[+] vkoskiv|5 years ago|reply
Not a C compiler. This is the original C compiler from ~1972. It's just an experiment to bring a bit of computing history to life.