news.volyx.in

SectorC: A C Compiler in 512 bytes (2023) (xorvoid.com)

381 points by valyala · 164 days ago · 79 comments on HN

Article summary

The article presents SectorC, a C compiler written in x86-16 assembly that fits within a 512-byte boot sector. It supports a subset of C and is likely the smallest C compiler ever written. The compiler uses a minimalist lexer and parser, and its implementation is based on several key insights, including the use of hashing for tokens and a simplified grammar. The compiler can compile and run small C programs, including examples that leverage x86-16 hardware features.

Main themes

  • Minimalist C compiler
  • Boot sector programming
  • x86-16 assembly
  • Compiler design
  • Language subsets
  • Tokenization and parsing

What commenters say

  • The use of hashing for tokens is an elegant solution for minimizing the compiler's size.
  • Replacing if, while, and for statements with goto routines could potentially shrink the compiler further, but would require additional handling for conditional jumps.
  • The compiler's limitations, such as only supporting a subset of C, make it less useful for real-world applications, but it is still a notable achievement in terms of minimalism.
  • The comparison to other compilers, such as Claude's C compiler, highlights the trade-offs between size, functionality, and usability.
  • Some commenters argue that the compiler's inability to find standard headers is a significant limitation, while others point out that this is a common issue with many compilers.
  • The idea of using content-addressed headers could potentially solve the header location problem, but may have its own set of drawbacks.
  • The project's value lies in its demonstration of the simplicity of the C language and its potential for use in bootstrapping chains or other specialized applications.
  • The compiler's implementation is impressive, but its usefulness is limited by its lack of support for error handling and other essential features.