I wrote a self-hosting C compiler in 40 days (2015)
sigbus.info
sigbus.info
The original assumption of the pre-ANSI <vararags.h> was that way, since it was just a hack exploiting actual object code behavior in the absence of language spec support. But ANSI C standardized the ... ellipsis, and the old hack became undefined behavior. A function declaration with the ellipsis is not type compatible with one that lacks it. This difference in type means that the compiler can map variadic functions to a different calling convention, such as one that always uses the stack for all the trailing arguments, even if they occur in positions for which the regular convention uses registers.
I wouldn't call variadic functions well designed on the whole, but aspect isn't badly designed.
Since I'll be using the new one as a reference implementation for a book, and the book is intended to be for beginners, I put as much effort as I can into improving the readability of the code. In particular, not only the head of the repository but every commit in the repo should be readable, so that readers can easily understand how each feature is implemented. I believe I'm doing a good job keeping it clean so far. (Actually in order to keep the commit history clean, I continue rewriting commit history and doing `git push -f`, but that should be fine because the purpose of publishing the repo is not for co-development but for sharing a reference implementation.)
chibicc does not have a C preprocessor, but except that it can compile itself already, so if you are interested, you can take a look.
I would love to see a similar book/project for a linker and assembler.
(Actually in order to keep the commit history clean, I continue rewriting commit history and doing `git push -f`,
actually I wonder if you might consider to maintain another clean repository. So there is a raw repository to record every real history, and a clean repositiory to for better reading? Because sometimes the raw history could be interesting too.That said you could always turn off gc if some people want it, but I wouldn't put any more than a token effort into keeping that history.
The preprocessor is fiendishly tricky to write. I wondered how he did it in such a short time :-) I had to scrap mine and reimplement it 3 times.
I wish I had had https://www.spinellis.gr/blog/20060626/cpp.algo.pdf to work from.
Not that I have had any doubts about how hard it is...
https://github.com/facebookarchive/warp
and the one for the C compiler I wrote long ago:
https://github.com/DigitalMars/Compiler/blob/master/dm/src/d...
which is integrated with the C lexer for speed reasons.
I’m also interested in how one would write a C preprocessor in a day. It’s got so many tricky edge cases and whatnot.
Full disclosure: I looked into the future with my Chronoscope, and copied the most popular language!
open(2) is also called relatively frequently and AFAIK requires varargs if it’s implemented in C.
More folks should write compilers. Can't wait to see your next compiler, Rui.
It's on my TODO list. After, of course, I finish that x86_32 preemptive multi-tasking kernel I started writing 20 years ago and never finished. Plus the 16-bit CP/M clone for the 68000 that's gathering dust. Or maybe after my half finished Z80 emulator or my half finished Z80-systems-in-Unreal-Engine project, or my mostly-but-not-completely-working VT100 emulator. Sigh. How do people ever finish this stuff?
EDIT, oh, he says as much at the bottom: "Since then, I have moved to the LLVM team in Google, and I'm now working on lld, the LLVM linker."
* Use C as a fast scripting language. This can be useful for game programming, where you don't want to recompile your engine, but you can't tolerate the sluggishness of interpreted languages.
* Use C as a compile target. I would love to build a "pythonic lean C++", without the hard stuff, without template or backward compatibility of C. Just python indenting, strongly typed, maps, set, geometric types, python-like standard functions... I guess that using C as a compile target allows one to avoid the hassle of building a low-level compiler... Not sure though.
I just wish C had some nicer things, like a string type, maps, etc. python indentation would also make it readable.
D has a lot of difference with C/C++, even if it does things right.
C is nice because it's simple and readable.
D looks like it's using a lot of new syntax and it looks hard to adapt/learn.
The semantics of the language are quite simple if we discard the weird implicit integer promotion and conversion rules. Every operation yields a new value of a certain type, and some expressions are considered lvalues by virtue of designating a modifiable location in memory.