7 ms·
CC500: a tiny self-hosting C compiler
- jaytaylor 12y agoDoes 767 LoC / 17KB really get to be considered tiny? I mean, sure, it's small and efficient.. but there are other contenders to consider, such as Fabrice Bellard's [0] tiny obfuscated self-hosting c-compiler in under 500 lines. How can we unambiguously distinguish between what qualifies for the classification of a "tiny" c-compiler versus just a "small" one? [0] http://www.bellard.org/otcc/otcc.c http://www.bellard.org/otcc/otcc.c
- huegh 12y agoI propose the tinyness of a compiler is directly proportional to the number of comments spontaneously arguing it isn't.
- EGreg 12y agoThat means it can be gamed
- wlievens 12y agoIf you compare it to the gargantuan that is an actual production compiler, it sure is tiny.
- boomlinde 12y ago> How can we unambiguously distinguish between what qualifies for the classification of a "tiny" c-compiler versus just a "small" one? We don't care about an unambiguous distinction.
- vidarh 12y ago767 LoC of code including comments describing the grammar. Strip out the comments and you're already do to 600. Move opening brackets and a other very few steps, and you get it down to 560. Getting this below 500 lines should be fairly trivial. So if your comparison is the completely unreadable otcc, then I'd say yes, this qualifies.
- userbinator 12y agoThe distinctions are quite arbitrary since there currently isn't anything like a size-competition for compilers, but if forced to make categories, I'd consider <1kLoC to be "tiny" and <10kLoC "small". In contrast, GCC is nearing 10MLoC - 4 orders of magnitude difference - and its C parser alone is >10kLoC: https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser.c https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser...
- progman 12y agoNice hack! The next step would probably be a CC which compiles to a minimal virtual CPU instruction set so that we just need some Assembler macros to port it to a native platform (x86, ARM, etc.), plus a library of formats (ELF etc.) to create applications for those platforms.
- vog 12y agoI believe this is how modern compilers work anyway. GCC as well as clang (via llvm) have multiple frontends (one for each programming language) and multiple backends (one for each target platform.)
- beagle3 12y agoThis was here just 3 days ago: https://news.ycombinator.com/item?id=8558822 https://news.ycombinator.com/item?id=8558822 ; Now, YOU just need to write those assumbler macros. But why? Are you in some shortage of working compilers?
- progman 12y ago> Are you in some shortage of working compilers? I am just interested in a small maintainable C compiler which can easily be adopted to bootstrap a new platform or cpu (on FPGA). I consider such a tiny C compiler just a convenient assembler to implement basic things on new platforms. I know GCC and Clang/LLVM but those tool chains are really big, and I guess it requires a lot of work to support a new platform and to polish all things so that everything works properly. A portable tiny GCC/LLVM replacement for small embedded systems would be really nice. GCC and Clang are great for powerful systems but I consider them overkill for small systems.
- Zardoz84 12y agoAnd now, to check how port it to TR3200 cpu...
- jokoon 12y agowhat does he mean by "self-hosting" ? I mean it generates a binary.
- mungoman2 12y agoIt means it can compile itself.
- codingcoding 12y agoI am playing with your code. But all my inputs end with error in the program function. Could you provide one meaningful example, which cc500 compiles completely?
- logicallee 12y agoNice project, but is it just me or is calling this a "C" compiler a bit of a stretch? Couldn't the title read CC500: a tiny self-hosting subset-of-C compiler. I realize it looks like C, but then so does anything with the words if, while, and braces. (It doesn't seem to have the word "for".) Since it says the compiler "does not even parse the types", that part seems like window-dressing and not part of the language. I bet you could write the complete reference for the language this compiles in a paragraph.