SectorC: A C Compiler in 512 bytes
5 hours ago
- #minimalism
- #C-compiler
- #x86-asm
- SectorC is a C compiler written in x86-16 assembly that fits within a 512-byte boot sector.
- It supports a subset of C including global variables, functions, if/while statements, operators, and inline machine-code.
- The compiler uses space-delimited 'mega-tokens' to minimize the tokenizer size.
- A key insight was using `atoi()` as a hash function to simplify token recognition.
- The initial implementation was 468 bytes, optimized down to 303 bytes with techniques like call-fusion and tail-calls.
- Features added include nested if/while blocks, various operators, function calls, and comments.
- Inline machine-code via `asm` allows hardware access, enabling I/O and other low-level operations.
- Error handling is minimal, trusting the programmer to write correct code.
- Includes a runtime (`rt/lib.c`, `rt/_start.c`) and examples like a sine-wave animation and PC speaker music.
- The project demonstrates extreme minimalism, fitting a usable C compiler in 512 bytes.