10 ms·
Dennis Ritchie's first C compiler on Github
- habosa 13y agoIs this written in C? If so, what compiled this? Sorry for the noob question, I'm just a little lost.
- jshou 13y agoIt compiles itself! http://en.wikipedia.org/wiki/Bootstrapping_(compilers) http://en.wikipedia.org/wiki/Bootstrapping_(compilers)
- kibibu 13y agoPresumably the first step on the road to a self-compiling C compiler was written in B.
- habosa 13y agoI understand bootstrapping, but at some point there has to exist some outside compiler in another language or a hand-compiled version of this otherwise the chicken and egg chain never ends.
- coliveira 13y agoYou can use any language available in the machine to create the first, most basic compiler. You can also use a cross-compiler if a compiler for the language already exists for another machine. If all else fails, you can write the proto-compiler in assembly.
- sytelus 13y agoThe very first compilers were tediously written in assembly. Bill Gates wrote his first version of Basic in assembly. I believe all early Fortran compilers were written in direct machine code through punch cards! Coding in assembly is not all that bad :). Once you got some compiler going, you can write more complex compiler with it and keep adding more syntactic sugar :).
- GnarfGnarf 13y agoCoding in Assembly was a gas! I wrote applications in IBM BAL in the 70's. G/L, Payroll, Inventory. We did it in part because we had so little memory (typically 64K to 131K), we had to squeeze every drop we could out of available memory. I worked on Univac 9400s. We received the O/S in source code form (Assembly) on tape. We ran it through a parametizer (PROC), compiled the resulting source, and that's what the customer ran with. You haven't lived until you've stepped through your code one instruction at a time, displaying op codes and raw binary data on the maintenance panel lights.
- guelo 13y agoThe mysterious true origins of bootstrapping compilers.
- tcas 13y agoLooks like a very early dialect. C assumes everything is an int unless specified otherwise. You can declare parameter types after the function name. So: init(s, t) char s[]; { would be equivalent to: int init(char s[], int t) { This still works with modern compilers. I'd be interested if anyone has any more info about this: waste() /* waste space */ { waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); waste(waste(waste),waste(waste),waste(waste)); } Found in last1120c/c10.c
- cwzwarich 13y agoFrom the linked description (http://www.cs.bell-labs.com/who/dmr/primevalC.html http://www.cs.bell-labs.com/who/dmr/primevalC.html): A second, less noticeable, but astonishing peculiarity is the space allocation: temporary storage is allocated that deliberately overwrites the beginning of the program, smashing its initialization code to save space. The two compilers differ in the details in how they cope with this. In the earlier one, the start is found by naming a function; in the later, the start is simply taken to be 0. This indicates that the first compiler was written before we had a machine with memory mapping, so the origin of the program was not at location 0, whereas by the time of the second, we had a PDP-11 that did provide mapping. (See the Unix History paper). In one of the files (prestruct-c/c10.c) the kludgery is especially evident.
- tcas 13y agoDoh, I completely glossed over the readme and went straight to the code. That makes sense -- Thanks! Cool to think that that waste function can still compile with todays compilers. A quick disassembly it seems to take up 751 bytes compiled on x64 using clang on O0.
- enf 13y agoThat was the standard way of declaring parameter types until ANSI C in 1989. C actually copied the current style back from C++.
- ghshephard 13y agoFrom the link on GitHub: http://cm.bell-labs.com/cm/cs/who/dmr/primevalC.html http://cm.bell-labs.com/cm/cs/who/dmr/primevalC.html Which led me to here: http://cm.bell-labs.com/cm/cs/who/dmr/chist.html http://cm.bell-labs.com/cm/cs/who/dmr/chist.html Where, if you take the time, you will find a wonderful story, upon completing, you will probably know more about the early embryonic history of C then 95% of your peers. (Spoiler - We start with BCPL, then Move to B - it's left as an exercise to determine how we originally compiled BCPL)
- hkmurakami 13y agocoupled with user:rmrfrmrf's pseudo-religious post praising the code, I read the second url as "christ" :P Oh how our minds play tricks on us!
- mseepgood 13y ago> (Spoiler - We start with BCPL, then Move to B - it's left as an exercise to determine how we originally compiled BCPL) The first version of Go started with B: http://code.google.com/p/go/source/detail?r=f6182e5abf5e http://code.google.com/p/go/source/detail?r=f6182e5abf5e The second revision was converted to C: http://code.google.com/p/go/source/detail?name=f6182e5abf5e&r=b66d0bf8da3e1829323172e37fd7616ad8df03a5 http://code.google.com/p/go/source/detail?name=f6182e5abf5e&... The third to Draft-Proposed ANSI C: http://code.google.com/p/go/source/detail?name=f6182e5abf5e&r=ac3363d7e788ba8ef6f692d6ef75db36674d07c7 http://code.google.com/p/go/source/detail?name=f6182e5abf5e&... And the fourth to ANSI C: http://code.google.com/p/go/source/detail?name=f6182e5abf5e&r=172d32922e729198442edca66c5c3fa8a992b891 http://code.google.com/p/go/source/detail?name=f6182e5abf5e&...
- lifthrasiir 13y agoThey definitely messed up with a conversion from Subversion to Mercurial. ;)
- chubot 13y agoUh these commits seem interesting, but I don't quite understand the context. What's macho and what is its relation to go? Are the dates way off or is it an import of older history?
- btilly 13y agoYour question is answered at length in http://plan9.bell-labs.com/who/dmr/chist.html http://plan9.bell-labs.com/who/dmr/chist.html. But it is common to write compilers some subset of the language that you want to compile, see http://en.wikipedia.org/wiki/Bootstrapping_(compilers) http://en.wikipedia.org/wiki/Bootstrapping_(compilers).
- Jach 13y agoOne might write a compiler from language X to language Y first in assembly, then when that works, write a new compiler from language X to language Y in language X itself and use the previous compiler to compile it. Presumably they already had a C compiler that they used to get this one compiled, then it can compile itself afterwards.
- lanna 13y agoA compiler has to do a lot more than parse a source file and translate it to target code: error checking and reporting, optimization, etc. To compile a C compiler, you don't need a full-blown C compiler. For instance, I bet floats and doubles are not used. Therefore, you can write a barebones proto-C compiler in whatever language you have available and use it to bootstrap your compiler. Rinse and repeat.
- deleted 13y ago[deleted]
- a_bonobo 13y agoCoincidences... I thought "How come Warren Toomey (one of the guys of the Unix Heritage Society [1]), has never posted this?" Turns out, this Github-repo is just a mirror/copy of his work, but with attribution [2]. Still worth reading through there, tuhs also stores some extremely old UNIX versions. [1] www.tuhs.org [2] http://cm.bell-labs.com/cm/cs/who/dmr/primevalC.html http://cm.bell-labs.com/cm/cs/who/dmr/primevalC.html Edit: Warren has written a paper on restoring ancient UNIX versions and C-compilers, you might like it [3] [3] http://epublications.bond.edu.au/infotech_pubs/146/ http://epublications.bond.edu.au/infotech_pubs/146/ Edit2: Now that I've thought a little bit about it, I'm not happy that the sources are on GitHub in this form. This is Warren's work - he did a lot of work in getting these tapes to work again, and "mortdeus" just copied the work and didn't even change the folder-names - "last1120c" is the first tape, "prestruct" the second. And you still need Warren's Apout emulator to get these files to work.
- shabda 13y ago> Edit2: Now that I've thought a little bit about it, I'm not happy that the sources are on GitHub in this form. If one releases code under an open license and people use/copy/fork it, why should you or the original author be unhappy. (As long as the license terms are not being broken.) If we had to worry about this each time we forked/cloned someone's work, it would make code reuse very hard. [Edit] I am working on some Python charting. I found Pychart, which I found interesting: http://home.gna.org/pychart/ http://home.gna.org/pychart/ . Because it is in bzr which I don't work with, I put it on github. Should I be worried that someone will feel offended with it?
- yoklov 13y agoIf you're within the terms of the license, you're fine. The developer should say so in the license (or choose another license) if they don't want it reproduced anywhere. A link to the original is common courtesy, however. In the case of the C compiler, I think the modifications are under the same license as the original, but I'm not totally certain. If they aren't it would be cause for concern.
- rmrfrmrf 13y agoReading Dennis Ritchie's code is as close to reading a religious text as I'll ever come. The straightforward elegance of it is so inspiring!
- mseepgood 13y agoHe already used the right brace style (hanging braces, cuddled else) and the right indentation (tabs, not spaces).
- comex 13y agoSomehow over the years learning C on my own, perhaps because it seemed more consistent to me, I got used to writing if(foo) { omitting the space after control statements' names, which is almost never done - except in very early C code. (It's not just this: V7 Unix often omits the space as well.). I should probably settle on a less idiosyncratic personal style, but I can't help but take a little heart from seeing "my" style in such a famous codebase. :)
- PySlice 13y agoNever using a space before the paren is more consistent. I don't know why people put a space there...
- brianberns 13y agoif(foo) I avoid doing this because it looks like a function call.
- orangethirty 13y agoIt felt weird being able to read it and understand (on a high level) what was happening.
- Kiro 13y agoI didn't understand anything but I want to. Where do you start reading to follow the flow?
- aap_ 13y agoAlso see my B compiler that was inspired by these early C compilers: https://github.com/aap/abc https://github.com/aap/abc
- willvarfar 13y agoGorgeous! Have good fun, thx for the link
- ch 13y agoSomeone should check that thing for back doors! http://cm.bell-labs.com/who/ken/trust.html http://cm.bell-labs.com/who/ken/trust.html
- Mr_T_ 13y agoToo late. Ken Thompson already put the back door in his B compiler.
- androidb 13y agoI'm ashamed for having to google his name but for others like me here's a glimpse: "Dennis MacAlistair Ritchie (born September 9, 1941; found dead October 12, 2011) was an American computer scientist who "helped shape the digital era." He created the C programming language and, with long-time colleague Ken Thompson, the Unix operating system" http://en.wikipedia.org/wiki/Dennis_Ritchie http://en.wikipedia.org/wiki/Dennis_Ritchie
- jychang 13y agoYou should have seen HN when he died. It was that hell week of a bunch of early computer pioneers all passed away...
- chimeracoder 13y agoTo be honest, I don't remember much of a hullaballo when either Dennis Ritchie or John McCarthy died. There was a post or two on each event, but nothing like when Steve Jobs died (when literally the entire front page was posts about his death). (All three events were in October 2011 - Jobs, then Ritchie, then McCarthy)
- areddy 13y agoCan anyone explain the naming of files? c00.c, c01.c etc
- atesti 13y agoIf you like c compilers, also check out http://bellard.org/tcc/ http://bellard.org/tcc/ and http://bellard.org/otcc/ http://bellard.org/otcc/ The later is a c compiler in about 1kb of source code! It's quite functional and can compile itself. The first link is what came out of it: A compiler so fast, that it can boot Linux from source code in a few seconds: http://bellard.org/tcc/tccboot.html http://bellard.org/tcc/tccboot.html
- Tyr42 13y agoI don't understand this main(argc, argv) int argv[]; { Is that still valid today?
- Macha 13y agoI don't know if it's still valid by the spec, but it's still supported by some compilers. There was a post on vim reaching 7.3.0.1000 the other day, and that's still using that style of typing.
- lholden 13y agoFunction declaration for main. The type declarations for the function parameters default to int, but can be specified outside of the parens before the curly brace. The int argv[]; is where the declaration for argv is happening and is being declared as an pointer for ints. You can also see elsewhere in the code where they are passing pointer addresses (as int params) into functions and then using the address to build pointers referencing that data.
- 30thElement 13y agoIt's a holdover from Fortran (and probably from before that). Whereas modern day we would define functions as int foo(int i, int j) {...} in Fortran you would do (! is comment) function foo(i, j) integer :: foo !return type integer :: i integer :: j !body goes here Early C stuck to that style, so you would just put the names of the variables in the declaration, and then before the body give them types. The reason only argv is mentioned in that example is that C assumes a variable is an int if not declared otherwise, so there's no reason to put "int argc" like "int argv[]".
- a-priori 13y agoYes it is. Originally that's how you specified parameters: just the name in the parentheses, then (optionally) the types in a format similar to variable declarations before the function body. If you didn't specify a type it would default to int, so argc above would be an int. All modern C compilers still accept this style for backwards compatibility. I'm not sure about C++ compilers.
- 13y ago
- lholden 13y agoThe declaration of printf is both scary and pretty cool. What happens when you have more then 9 substitutions specified in the string? :D edit: decided the code was a bit long to have pasted into my post. Can find it at the bottom of http://cm.bell-labs.com/cm/cs/who/dmr/last1120c/c03.c http://cm.bell-labs.com/cm/cs/who/dmr/last1120c/c03.c
- grandpa 13y agoThis is the compiler's printf(), not the one that gets linked in with compiled code. As long as the compiler itself doesn't have more than 9 substitutions, it's OK. Your code will link with a different one.
- csense 13y agoCan anyone get this to compile and run? Does anyone know what hardware the assembly language files are for? Maybe you could produce a modified version with the archaic features removed, compile it with a modern compiler, then use the binary produced to compile an unmodified version. Or maybe there are still binaries of really old compilers that can understand this code floating around out there. Any ideas?