top | item 46925741

SectorC: A C Compiler in 512 bytes (2023)

381 points| valyala | 22 days ago |xorvoid.com

79 comments

order

layer8|22 days ago

If this implementation had existed in the 1980s, the C standard would have a rule that different tokens hashing to the same 16-bit value invoke undefined behavior, and optimizing compilers in the 2000s would simply optimize such tokens away to a no-op. ;)

RodgerTheGreat|22 days ago

"you don't have -wTokenHashCollision enabled! it's your own foolish ignorance that triggered UB; the spec is perfectly clear!"

xorvoid|22 days ago

Too real! LMAO

mati365|22 days ago

Oh, it looks like my X86-16 boot sector C compiler that I made recently [1]. Writing boot sector games has a nostalgic magic to it, when programming was actually fun and showed off your skills. It's a shame that the AI era has terribly devalued these projects.

[1] https://github.com/Mati365/ts-c-compiler

guenthert|21 days ago

Er, what? The article describes a compiler for a not-quite-C programming language which fits entirely in 512B. Your project, if I see this correctly, can optionally produce code meant to execute as boot sector.

Both interesting projects, but other than the words 'boot sector', 'C' and 'compiler', I don't see a similarity.

w4yai|22 days ago

> when programming was actually fun and showed off your skills

Oh no. Now more people are able to do what I do. I'm not special anymore.

xorvoid|22 days ago

I may be the author.. enjoy! It was an absolute blast making this!

einpoklum|22 days ago

An interesting use case - for the compiler as-is or for the essentiall idea of barely-C - might be in bootstrapping chains, i.e. starting from tiny platform-specific binaries one could verify the disassembly of, and gradually building more complex tools, interpreters, and compiler, so that eventually you get to something like a version of GCC and can then build an entire OS distribution.

Examples:

https://github.com/cosinusoidally/mishmashvm/

and https://github.com/cosinusoidally/tcc_bootstrap_alt/

veltas|22 days ago

This is very nice. I'm currently writing a minimalist C compiler although my goal isn't fitting in a boot sector, it's more targeted at 8-bit systems with a lot more room than that.

This is a great demonstration of how simple the bare bones of C are, which I think is one reason I and many others find it so appealing despite how Spartan it is. C really evolved from B which was a demake of Fortran, if Ken Thompson is to be trusted.

JamesTRexx|22 days ago

Would and how much would it shrink when if, while, and for were replaced by the simple goto routine? (after all, in assembly there is only jmp and no other fancy jump instruction (I assume) ).

And PS, it's "chose your own adventure". :-) I love minimalism.

wzbtoolbox|21 days ago

This is the kind of project that reminds you how far removed modern development is from the actual machine. We pile abstractions on abstractions until "Hello World" needs 200MB of node_modules, and then someone fits a C compiler in 512 bytes.

Not saying we should all write boot sector code, but reading through projects like this is genuinely humbling. Great educational resource too.

lock1|21 days ago

This kind of comment reminds me of how broad "software development" is.

On other HN posts, they're stating something like "software development is dead", "LLM as a compiler", "Do you read compiled assembly?", and so on.

While some other posts like this contain huge mechanical sympathy and literally r/w the assembly directly.

mojuba|22 days ago

Compare that to the C compiler in 100,000 lines written by Claude in two weeks for $20,000 (I think was posted on HN just yesterday)

vidarh|22 days ago

It's a fun comparison, but with the notable difference that that one can compile the Linux kernel and generate code for multiple different architectures, while this one can only compile a small proportion of valid C. It's a great project, but it's not so much a C compiler, as a compiler for a subset of C that allows all programs this compiler can compile to also be compiled by an actual C compiler, but not vice versa.

sanufar|22 days ago

The way hashing is used for tokens and for making a pseudo symbol table is such an elegant idea.

fix4fun|22 days ago

I think the same. Really nice project and good trick with hashing tokens.

PS. There left 21 bytes (21 * 0x00 - from 0x01e0 to 0x01fd). Maybe something can be packed there ;)

avadodin|21 days ago

I actually "shipped" a parser using the symbols' hash(as the only identifier) for a test tool once. Hopefully, the users never used enough symbols to collide 32-bits.

benj111|21 days ago

I've had the idea before. Was never quite brave enough to do it. It's elegant until it isn't....

fooker|22 days ago

This is so cool!

Fun fact, Tiny C Compiler was derived from such a C compiler submitted to the the International Obfuscated C Code Contest.

https://www.ioccc.org/2001/bellard/index.html

pseudohadamard|22 days ago

Meh, I did an entire awk interpreter in two lines:

  #!/bin/sh
  echo "awk: bailing out" >&2

DeathArrow|22 days ago

For me is not interesting because it fits in 512 bytes, it's interesting because it's very simple. I think it would be a great introduction to learning about compilers.

kreelman|22 days ago

There seems to be a good amount of interest for a boot sector compiler!!

If you're running on Linux, adjust the qemu call to use alsa rather than coreaudio.

I generated a pull request for this on Github. If the author is happy enough with my verbose shell scripting style :-) it might get included.

userbinator|22 days ago

C-subset, to be precise; but microcomputer C compilers were in the tens of KB range, for one that can actually compile real C.

drob518|21 days ago

Brilliant! I love the stealing of Forth ideas to power this. Forth’s minimalism is highly underrated.

zahlman|21 days ago

> Big Insight #2 is that atoi() behaves as a (bad) hash function on ordinary text. It consumes characters and updates a 16-bit integer.

I could have sworn I remembered atoi() being defined to return 0 for invalid input (i.e. text not representing an integer in base ten).

MobiusHorizons|21 days ago

That would be true of one using a libc, but in a boot sector, you only have the bios, so the atoi being referenced is the one defined in c near the beginning of the article

alittlebee|21 days ago

This is really beautiful (I feel like this sort of project is outsider art), thank you for sharing.

hgs3|21 days ago

Great read. It would be neat to see a mini operating system under 1 kb of code.

SeanSullivan86|22 days ago

Why is it called a C Compiler if it's a subset of C?

NooneAtAll3|22 days ago

> I wrote a fairly straight-forward and minimalist lexer and it took >150 lines of C code

was it supposed to be "<150"?

owalt|22 days ago

They're saying the naive implementation was more than 150 lines of C code (300-450 bytes), i.e. too big.

wbsun|22 days ago

Nice, now you can dd it to your boot sector and ... Wait, it is 2026, there are 1000 ways of booting and memory mapping on so-called unified ARM architecture @,@

gonzus|22 days ago

Lacking support for structs, I think this is too minimalistic to be called "a C compiler".

benj111|21 days ago

Weren't structs a fairly late addition to C?

And anyway, isn't that kind of missing the point. 512 bytes isn't much. Your comment is nearly a 5th of that budget.

pilord314|22 days ago

you bootstrap it into a library you can include optionally, duh