8 ms·
8cc: A Small C Compiler
- userbinator 12y agoAlways good to see more relatively small C compilers, even if not in the extremely small size class. That said, I get a feeling this one has many more files than necessary, as a lot of them only have a few functions in them that could be consolidated into one "utilities" file.
- andrewchambers 12y agoThis was written for readability.
- ptaipale 12y agoI think having many small files is not good for readability. It may be good for reading that one file, but not for understanding the whole.
- vog 12y agoI believe this is mostly a matter of taste. I find this style very consistent. Each file deals with exactly one abstract data type, and all functions around it. This is common in many languages. Java enforces one (public) class per file, which is also a common coding style in C++, Python, Ruby and PHP, even though those languages don't enforce that. You also find this in more modern languages like OCaml, where it is recommended to have one "module" per file, and one "module" concentrates on one data type. (and maybe one or two helper types if needed, much like in-class type definitions in C++) This may be unusual in C projects, but it does have its merits. I read the code and wonder "Oh, what exactly is that 'dict' stuff?". Then, I can blindly guess the file names "dict.h" and "dict.h". I see a full explaination of "dict" and nothing else. I then notice that it is a combination of "map" and "vector", and maybe that's all I need to know. If not, I dig into "map.c" and "vector.c". Otherwise, I'd have to search, and finally find it in some unrelated file name like "utils", "utililities" or "mapvectorutils". Then I have to search for all dict-relevant code in that file, skipping through a bunch of unrelated code. To get the "whole picture", I'd prefer to see a simple diagram (autogenerated by IDE, or as part of the docs) that shows which module depends on which other module, maybe somehow arranged into layers. The other strategy "group multiple modules into a file" may convey the highest level quite well, but utterly fails to explain the dependencies of the modules inside a single file.
- barrkel 12y agoYour text search skills or tools are lacking, then. In an IDE, "go to definition" solves this problem. In an editor, incremental text search over the entire project solves this problem (I normally use helm-git-grep in emacs). I'm a fan of bigger and fewer files. Atomized bits of functionality are tedious to read, when you have to read lots of code to understand the gestalt.
- vog 12y ago> I'm a fan of bigger and fewer files Indeed, this is mostly a matter of taste. Both styles have their merits and downsides. For some projects, I also preferred bigger files up to single-file solutions, which also worked great. > In an IDE, "go to definition" solves this problem. In an editor, incremental text search over the entire project solves this problem When using the right tools, it doesn't matter how the code distributed across files. But I believe this is slightly out of scope for the question at hand.
- vog 12y ago> Your text search skills or tools are lacking Ad Hominem should have to place at HN. Please don't do that. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- barrkel 12y agoIf you seriously need lots of tiny files in order to find code, it is a fact that you don't know how to use the tools you have available. That's my professional opinion.
- vog 12y ago> If you seriously need lots of tiny files in order to find code Nobody said that. Please read more carefully, especially before goofing off others. Or, simply don't goof off anyone in the first place.
- rui314 12y ago
- dmytrish 12y agoYes, it's terrible to have a myriad of files each of fifty sparse lines. "Modern" languages like Java/C# tend to scatter code over many small files. I think it's a cultural thing: since IDEs do not have "split" mode that provides several views to different parts of the same file side-by-side (like Vim does), programmers tend to split code into files small enough to scroll fast and IDE handles switching between files. Plus the dogma "one class, one file" that limits file size even if it's not helpful. I'm glad that functional programming is on the rise: it divides code units more naturally with modules of functionality, not a one-size-fits-all programming language structure like classes. P.S. Also, composition of a big source code file conveys some "background" information you'd have to recover from a needlessly fragmented pile of small files.
- vog 12y ago> I'm glad that functional programming is on the rise: it divides code units more naturally with modules of functionality I'm not sure this will change anything. For instance, OCaml's modules are usually a bunch of functions surrounding a single abstract type. This is very similar to classes in other languages.
- JasonFruit 12y agoOn a complete side note, I was going to tell you that myriad is an adjective and should not be used in the construction, "a myriad of". But then I looked it up; apparently I was wrong, and so were the many other people I had heard that from. Thanks for teaching me.
- xjia 12y agoIf more people knew how to write compilers, is it likely that programming languages will be better?
- sanxiyn 12y agoIn my opinion, no. Programming language progress is bottlenecked on adoption, and then language design. Implementation is not a bottleneck.
- yoklov 12y agoThat's true, but more people thinking about a problem makes breakthroughs more likely.
- frozenport 12y ago>>Implementation is not a bottleneck. Lets not forget what comes first. C++11 adoption was held back a few years because MSVC didn't have support for those features.
- pjmlp 12y agoYeah right, as if the embedded compiler vendors, mainframe compilers and commercial UNIXes (AIX, HP-UX, ...) are all 100% C++11 now. Or worse, as if most IT departments are allowing the use of C++11 compilers in corporate environments.
- robzyb 12y agoCompilier design and programming language design are two very diffierent things. Perhaps someone could even make the argument that a programming language is BEST designed by someone who doesn't know how to make a compiler? The choices for a programming language should purely be based on the language, and on the user experience. In the design phase no thought will be given to compiler design (initially) and all of that will eventually be hiddden as implementaiton details.
- yoklov 12y ago
- sanxiyn 12y agoIf you like a hobby C compiler, check nwcc(Nils Weller's C Compiler) too. http://nwcc.sourceforge.net/ http://nwcc.sourceforge.net/ nwcc is a bit more mature. It supports multiple architectures, multiple OSes, and it can bootstrap GCC.
- fit2rule 12y agoDoesn't build out of the box for me on Darwin 14.1.0, gcc-4.9. Does it need clang? configure script doesn't let you use anything but gcc ..
- andrewchambers 12y agoIt says specificially in the readme is only supports linux currently. Also, CC=clang works fine on ubuntu. I have tested on both Ubuntu and arch linux.
- justincormack 12y agopcc[1] is in a pretty good state, it can compile most of NetBSD, and is under active development. [1] http://pcc.ludd.ltu.se/ http://pcc.ludd.ltu.se/
- swah 12y agoThere is also http://bellard.org/tcc/ http://bellard.org/tcc/
- ackalker 12y ago...which can compile a(n older, preprocessed, but none the less) Linux kernel "on the fly", while booting: http://bellard.org/tcc/tccboot.html http://bellard.org/tcc/tccboot.html
- swah 12y agoAnd Rob Landley wants to plug his fork of tcc into qemu code generator http://www.landley.net/qcc/ http://www.landley.net/qcc/ to do something that I do not completely understand but sounds awesome!
- SixSigma 12y agoI wish it had a different name. 8c is the name of the plan c compiler on x86 http://man.cat-v.org/plan_9/1/2c http://man.cat-v.org/plan_9/1/2c
- logicallee 12y agowhere does the 8 come from? (that it occurred to both project creators.)
- SixSigma 12y agoMaybe 8086. But the Plan9 ones have a number for each arch (until it was more than 9!) It was packaged as kencc for *nix, in honour of Ken http://gsoc.cat-v.org/projects/kencc/ http://gsoc.cat-v.org/projects/kencc/
- 4ad 12y agoThat kencc never worked right, this is the ken-cc to use now: https://code.google.com/p/ken-cc/ https://code.google.com/p/ken-cc/
- 4ad 12y agoThe first character represents the architecture: 0c spim little-endian MIPS 3000 family 1c 68000 Motorola MC68000 2c 68020 Motorola MC68020 4c mips64 little-endian MIPS64 5c arm little-endian ARM 6c 960 Intel i960 (historical) 6c amd64 AMD64 and compatibles (e.g., Intel EM64T) 7c arm64 little-endian ARM64 7c alpha Digital Alpha APX (historical) 8c 386 Intel i386, i486, Pentium, etc. 9c 2900 AMD 29000 (historical) 9c power64 64-bit POWER kc sparc Sun SPARC qc power Power PC uc sparc64 SPARC64 vc mips big-endian MIPS 3000 family xc 3210 AT&T DSP 3210 (historical) zc avr Atmel AVR Some are more mnemonic than others.
- andrewchambers 12y agoI asked the creator Rui about the name previously, he said there is no reason, just that other letters and numbers were taken.
- deleted 12y ago[deleted]
- orangea 12y agoThis is an interesting design decision: > No memory management is a memory management scheme in 8cc. Memory regions allocated using malloc are never freed until the process terminates. That has greatly simplified the code and the APIs because 1) you can write code as if garbage collector were present, and 2) that design decision has eliminated use-after-free bugs entirely.
- kgabis 12y agoWalter Bright did something similar in D's compiler (http://www.drdobbs.com/cpp/increasing-compiler-speed-by-over-75/240158941 http://www.drdobbs.com/cpp/increasing-compiler-speed-by-over...) >DMD does memory allocation in a bit of a sneaky way. Since compilers are short-lived programs, and speed is of the essence, DMD just mallocs away, and never frees. This eliminates the scaffolding and complexity of figuring out who owns the memory and when it should be released. (It has the downside of consuming all the resources of your machine if the module being compiled is big enough.)
- shultays 12y agoNice 'feature'
- 101914 12y ago"x86_64 Linux only"
- oscargrouch 12y agoI' ve compiled this in FreeBSD also.. no problem Now it just need to parse the FreeBSD stdio.h correctly..