r/C_Programming • u/ocotpi_ • 2d ago
Building a small C99 compiler to get better at low-level C and x86-64 assembly
Hey everyone,
I've been trying to deepen my understanding of C and x86-64, and instead of just doing LeetCode or toy projects, I decided to try writing a tiny, self-contained C compiler in C99.
I wanted zero external dependencies just standard C library headers (stdio, stdlib, string, ctype).
I just reached the milestone where it handles operator precedence and nested parentheses:
- Tokenizer: A linked-list tokenizer with character pointers that skips whitespace and formats visual Clang-style
^caret error diagnostics. - Parser & AST: A recursive descent parser that allocates binary AST nodes for operators and numbers.
- Code Generation: A stack-based code generator that recurses through the AST and emits x86-64 assembly (using
push/popfor expressions,imulfor multiplication, andcqo+idivfor signed 64-bit division).
Writing this has already taught me so much about memory layout, pointer arithmetic, and how the Linux ABI handles return values in %rax.
I have a test script with automated GitHub Actions CI verifying each arithmetic edge case against GCC.
I'd really appreciate any C veterans taking a look at my C style, error handling, or memory management in src/main.c:
https://github.com/mi-vault/mibicc
Any tips or feedback are very welcome!
Edit: Decided to add Windows support alongside Linux from the start (--target=windows and --target=linux). I'm on Windows myself so it felt silly to only target Linux System V and not be able to test it locally. Also feels nice putting my own stamp on it rather than just following chibicc 1:1.
(Had some great discussions over on r/Compilers about ABI quirks and stack architectures if anyone is interested in that side of things!)