10 ms·
First C compiler ported to GCC
- burstmode 5y agoDid this compiler really support "auto" as a variable type, as seen in example fizzbuzz ?
- bawolff 5y agoAuto is not a type, it means local variable. I think its still a thing, just its the default so nobody uses it.
- captainmuon 5y ago"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".
- dvt 5y ago> The type is not given at all, I think by default it would be "int". Yep, this is called the "implicit int" rule, and it was specifically outlawed[1] by C99 and onward. [1] https://herbsutter.com/2015/04/16/reader-qa-why-was-implicit-int-removed/ https://herbsutter.com/2015/04/16/reader-qa-why-was-implicit...
- ufo 5y agoOne of the unusual things in this early version of C is that "int" can be used for any word-sized value, including pointers. The type system was very loose.
- Blikkentrekker 5y ago> types of the function parameters are not checked, anything can be passed to any function Still a better type system than Twilight.
- Turing_Machine 5y agoEven back then this was considered poor practice, however. The first edition of K&R had a subsection entitled "Pointers are Not Integers" (I don't know if that's still in modern editions).
- rbanffy 5y agoNow I want a float to access individual bits.
- ufo 5y agoThe interesting thing for me is that a variable without a type annotation could potentially store anything. It kind of explains why the language used "int" as the default type of variables declared without a type annotation.
- LukeShu 5y agoIt looks to me like that section was removed in the 2nd edition. Some sections moved around, so maybe I'm just looking in the wrong place, but it's not nestled between "5.5 Character Pointers and Functions" and "5.7 Multi-Dimensional Arrays" like it is in the 1st edition.
- deleted 5y ago[deleted]
- layoutIfNeeded 5y agoHehe, “auto” used to mean “automatic storage” aka the stack. Then much later C++ repurposed the keyword for type deduction.
- 1ris 5y ago"auto" in c is a storage class specifier, like "register", "extern" or "static". https://en.cppreference.com/w/c/language/storage_duration https://en.cppreference.com/w/c/language/storage_duration It was considered pretty useless by most, so c++11 recycled the keyword to mean something different.
- jwilk 5y agoSee also: https://www.seebs.net/faqs/c-iaq.html#question-1.8 https://www.seebs.net/faqs/c-iaq.html#question-1.8
- mywittyname 5y agoAuto is the implicit default right? As in function scoped, stack allocated, and lives until the function is returned? K&R (Second Ed). Makes no mention of the auto keyword in Section 1.10, but it does say, > Each local variable in a function comes into existence only when the function is called, and disappears when the function is exited. This is why such variables are usually known as automatic [sic] variables[...]
- 1ris 5y agoyes, exactly. That's why there is no need for it in modern c. This compiler however is different: The type is optional (and assumed to be int). Say you have a variable declaration "auto int i;". Back then you could omit int, now you can omit auto.
- IncRnd 5y agoNo. "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.
- 29athrowaway 5y agoIf the first C compiler was written in C... how could it be first C compiler? How could you compile the first C compiler?
- dvt 5y agoThe 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 https://www.bell-labs.com/usr/dmr/www/chist.html
- deleted 5y ago[deleted]
- Nicksil 5y agoBootstrapping https://en.wikipedia.org/wiki/Bootstrapping_(compilers) https://en.wikipedia.org/wiki/Bootstrapping_(compilers)
- Blikkentrekker 5y agoIf 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.
- dmitrygr 5y agothis port is [optionally] a cross compiler - it will run on x86/arm/whatever and produce pdp11 assembly on an actual pdp11 it CAN bootstrap
- pabs3 5y agohttps://bootstrappable.org/ https://bootstrappable.org/
- 5y ago
- Taniwha 5y ago"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
- nils-m-holm 5y agoYes, 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.
- vesinisa 5y agoI 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.
- jart 5y agoWhy 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.
- dboreham 5y agoIt can be, but people have arranged for it to not be, presumably because they don't feel the storage space to have all integers be 8-bytes is not justified.
- Someone 5y agoIt can be, and is, on platforms where supporting large arrays (if integers are 32 bits, arrays can ‘only’ have 2³¹ entries (#)) is deemed more important than memory usage. (#) https://software.intel.com/content/www/us/en/develop/documentation/onemkl-macos-developer-guide/top/linking-your-application-with-the-intel-math-kernel-library/linking-in-detail/linking-with-interface-libraries/using-the-ilp64-interface-vs-lp64-interface.html https://software.intel.com/content/www/us/en/develop/documen... seems to imply that limit is 2³¹-1. I don’t understand why that would be true.
- ggm 5y agoThe a[b] implemented as *(a+b) Thing, is how we were taught to think about array indexing in the CS lectures of the 70s
- sgtnoodle 5y agoAre 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.
- tinus_hn 5y agoI’m pretty sure you have to take the size of the objects in mind.
- throwaway525142 5y agoNo, 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.
- amelius 5y ago"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.
- dvko 5y agoIt’s also in the K&R book IIRC, stating that the two are equivalent.
- st_goliath 5y agoAnd 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"]);
- polska 5y agoI do not understand why we should create a C compiler ported to GCC.