← Back to Projects
SystemsIteratingFeatured

C Compiler

OCaml compiler pipeline from C source to x86-64 assembly.

As I gained a deeper interest in understanding lower level systems, I realized that I never understood the basis behind compilers, the actual foundation of programming. Thus, I researched what I could about compilers, and I stumbled upon Nora Sandler's book on writing compilers. Here, I learnt the principles behind developing a compiler starting with a real lexer -> parser -> IR -> codegen pipeline where I could inspect every stage. One cool thing is that the book didn't spoonfeed me the code; rather, it gave the foundations, which I had to apply to the best of my ability.

Date
Spring 2026
Signal
Systems
Build stage
Actively expanding language features
Stack
OCaml, Recursive Descent
compilersystemsirdebuggingocaml
Pipeline visual

Project notes

Highlights

What I built

  • Recursive-descent parser supporting nested unary expressions.
  • Custom IR lowered into stack-backed assembly with fixup passes.
  • Incremental tests from return literals to structured control flow.

Architecture

How the system works

  • Lexer tokenizes source into typed tokens.
  • Parser builds AST, then semantic analysis resolves symbols and scopes.
  • TACKY IR flattens expressions before architecture-specific codegen.
  • Emitter outputs AT&T syntax x86-64 assembly for clang linking.

Challenges

What made it hard

  • Memory-to-memory instructions in x86 required explicit scratch-register fixups.
  • Nested unary operations exposed edge cases in AST lowering.
  • Apple Silicon workflow needed careful x86_64 target handling.

Lessons

What I learned

  • As expression increases, IR becomes everso more important.
  • To optimize debugging, testing each compiler stage independently is a necessity.
  • Reading generated assembly is still the fastest debug loop.

Stack / materials

OCamlRecursive DescentTACKY IRx86-64Make
  • Roadmap: binary operators, local variables, richer control flow, function calls.
  • Current priority is correctness and clean lowering, not optimization passes.

Media timeline

Build photos, clips, and process visuals. The goal is to show how the project evolved, not just the final screenshot.

AST output
Compiler AST output
Assembly output
Generated assembly output
Build snapshot
C Compiler media 1
Gallery 3
C Compiler media 3