7 ms·
I wrote a self-hosting C compiler in 40 days (2015)
- sly010 10y ago> At first the compiler was about 20 lines long, and the only thing that was able to do is to read an integer from the standard input and then emit a program that immediately exits with the integer as exit code. I always find it hard to find the absolute minimum functionality i can implement so I usually just attack the problem hard, fail, and keep repeating until I have enough broken functionality, then I start adding tests and refactoring. Can I ask what is your day job? :)
- niix 10y agoHis blog says that he is an engineer at Google.
- tmccrmck 10y agoHe's also the creator of LLD.
- JustSomeNobody 10y agoI've heard it called a walking skeleton. This is what I try to do early because I think it aides greatly in data structure design.
- rjeli 10y agoI've heard it as a "skewer", because you're penetrating every necessary layer.
- rjeli 10y agoJust kidding, it's actually a Spike: http://wiki.c2.com/?SpikeSolution http://wiki.c2.com/?SpikeSolution
- marssaxman 10y agoI've always used the phrase "driving a wire all the way through", but I like "spike" better, especially if other people will know what it means.
- e19293001 10y agoFor me, one of the most rewarding task in learning a programming language is writing a compiler that can compile itself though more complex programming languages like Haskell and perl6 are very challenging but doable.
- dang 10y agoPreviously discussed at https://news.ycombinator.com/item?id=10731002 https://news.ycombinator.com/item?id=10731002.
- anonymousiam 10y ago"In x86 calling convention, structs are copied to the stack and their pointers are passed to functions. But in x86-64, you have to destructure a struct into multiple pieces of data and pass them via registers. It's complicated, so I'll leave it alone for now. It's rare that you want to pass a struct as a value instead of passing a pointer to a struct." Would anyone care to explain this? It makes no sense at all to me. Why would the 64-bit architecture restrict passing a large object on the stack? Thanks.
- mbel 10y agoAnswer is of course performance. Passing through registers is faster. You have more of them on x86-64 and they are also larger.
- hrydgard 10y agoIt's faster to pass small structs in registers, and on x86-64 there's more of them so doing that is more practical than on x86.
- wyc 10y agoPerformance. Storing to and retrieving from registers are typically much faster than writing to and then accessing memory. It's like keeping things in your 16 hands vs. keeping them in your backpack en transit. I believe this to be Linux-specific, as the BSDs have a different ABI that relies solely on the stack. Some notes: http://www.int80h.org/bsdasm/#default-calling-convention http://www.int80h.org/bsdasm/#default-calling-convention
- anonymousiam 10y agoThanks for the explanation everyone. I suppose it was my fault for interpreting "have to" as an absolute requirement rather than a preferred method.
- gpderetta 10y agoIt is a requirement in the sense that if the C code passes the aggregate parameter by value, the compiler is required to generate the assembler code to pass via the registers [1]. [1] inlining, calls to static functions and whole program optimization do give the compilers freedom to pick a different calling convention of course.
- anonymousiam 10y agoWhat isn't stated is whether or not this new compiler requires libc from the one used to compile it. Assuming a new CPU, the bootstrap code must be written in assembly (assuming an assembler has been written to translate the .asm source into machine code). Writing a C compiler in C with no way to bootstrap is "cheating".
- lifthrasiir 10y ago> Assuming a new CPU, the bootstrap code must be written in assembly [...]. Not really, thanks to cross-compilation. Only a small number of CPUs has really required bootstrapping with direct machine code.
- kardos 10y agoSo does this mean that we can put to rest the 'trusting trust' paradigm? As in, use 8cc to bootstrap our way into compiling gcc without using gcc?
- akkartik 10y agoIndeed, a small trusted compiler is part of the solution. There's still some subtlety in how you use it, though: http://www.dwheeler.com/trusting-trust/dissertation/html/wheeler-trusting-trust-ddc.html http://www.dwheeler.com/trusting-trust/dissertation/html/whe...
- rocqua 10y agoOnly if your version of the 8cc binary doesn't have GCC in it's compiler heritage, which it probably does. The blog actually goes into this a bit, stating that the byte-value of '\n' never occurs in the source-code of 8cc. Instead, that value comes from gcc. It is entirely possible that gcc has the same property, eventually the source could be a hand-written compiler. Really, to get around trusting trust, you need a compiler who's entire lineage you know (up to the hand-written compiler) and trust.
- deleted 10y ago[deleted]
- pklausler 10y agoThat's about how long it took me to write a self-compiling C compiler and basic runtime library for a simulation environment for a new instruction set architecture back in '98 (it eventually became the Cray X-1). Although I did start with a working C preprocessor left over from an earlier project, and in many ways the preprocessor can be the hardest part to get right. Bringing up a compiler in a simulated environment has its advantages over real metal. Seeing instruction traces with computed values is awesome for debugging.
- deleted 10y ago[deleted]