Live data from Hacker News

First C compiler ported to GCC

github.com

21–30 of 90 posts

Re: First C compiler ported to GCC

#22
post #21

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

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.

Re: First C compiler ported to GCC

#23

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

Re: First C compiler ported to GCC

#24
post #11

"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

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.

Why can't it be 64-bit? I don't see any reason why we can't have an ILP64 data model. If int and int* were both 64-bit then it would restore so much of the original beauty of C.

Re: First C compiler ported to GCC

#25
post #21

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

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.

I’m pretty sure you have to take the size of the objects in mind.

Re: First C compiler ported to GCC

#26

Earlier quoted context omitted.

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.

I’m pretty sure you have to take the size of the objects in mind.

No, these are equivalent.

The size of the objects is implicitly taken into account by the compiler, it knows the size of the objects by the type of the pointer.

Re: First C compiler ported to GCC

#28

Earlier quoted context omitted.

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.

I’m pretty sure you have to take the size of the objects in mind.

"Pointer arithmetic" takes care of that. Adding an integer to a pointer will multiply the size of the type pointed to by the integer and adds that to the pointer.

Re: First C compiler ported to GCC

#29
post #23

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 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 in…

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.

Re: First C compiler ported to GCC

#30
post #11

"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

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.

My original comment was rather tongue in cheek - but I have actually ported this compiler (well a later version of it, from the v6 release) to a 32-bit target - it was a different time, and C was a different, definitely more forgiving and simpler language - with other systems languages like BCPL/Bliss/etc around at the time the whole 'int is the same as a pointer' was definitely a way of thinking about stuff at the time
Post reply on HN