r/Compilers • u/Opening-Primary-9576 • 10d ago
A C compiler in C whose IR is printed text, which the x86-64 backend parses back
I have been building a C compiler in C, about 14,000 lines with no dependencies. Two decisions in it are worth other people's opinions, and both are the kind you only get to make once.
The repo: https://github.com/IzonIcy/LumiComp
The IR is printed text.
codegen prints a textual IR to stdout. The assembly backend then parses that same text back to build its own representation.
Why: the IR stays diffable, golden tests are just text files, and codegen output can be read without a debugger. Every stage of the pipeline is a subcommand, and this is the decision that made that possible. You can run codegen and look at the result.
What it costs: the backend re-derives types from text instead of being handed a typed structure. It guesses from the operation and the operands, and three of those guesses were wrong before I found them. The IR carries less information than it should, and that is a real defect I have chosen to live with.
The backend does no register allocation.
Every value lives in a stack slot. Load, operate, store, for everything.
Why: the output is correct before it is fast, and the backend stays small enough to read. It is about 3,400 lines.
What it costs: the generated code is slow, and the backend cannot use the register count as a signal about anything. I think this is the right trade for a compiler meant to be read, but I would like to be argued out of it.
What it covers
Integers, doubles, pointers, control flow, function calls. The assembly backend rejects structs, local arrays, single-precision floats, sizeof, and more than six integer arguments, each with a located diagnostic. The front end is wider than the backend.
What I am asking
- If you have built a compiler with a text IR, did you keep it or move to an in-memory structure, and what made you move?
- Is there a version of "no register allocation" that stays readable but produces code someone would actually ship? A linear scan is maybe 200 lines, and I keep deciding not to.
- The preprocessor rewrites the program as text rather than as tokens, so source locations are approximate inside macros. Is that fixable without rewriting the stage?
Build log, one post per stage, each about one trade and one bug: https://github.com/IzonIcy/LumiComp/tree/main/docs/build-log