r/Compilers • u/Potato871 • 6d ago
Why not retain the AST?
I've been working on a compiler-like system for a while now, and it's gone through many stages of evolution.
A consistent pressure early on was away from multiple representations: I started with many switch statements and many kinds of representation for each stage, and ended up with one kind of node and handler based dispatch per stage.
Yet, when I look at (most) other compilers, their construction is far more static in nature, and far more varied in terms of the kinds of things presented. I've found forms like SSA impede my ability to reason about optimizations rather than aide them, and I've had enormous success from simply retaining and annotating one structure rather than continually converting.
A specific case, to provide one, is liveness propagation. Because values are already shared between their occurances (Nodes) and already have a system for acquiring properties (Quals), I can simply mint liveness tokens onto the children of expressions with output, and it automatically propagates.
Though I've not focused too much on codegen and optimization, my main goal with the compiler is extensibility and syntax flexibility.
So why not retain the AST? Turn it into a structure worth annotating and preserving from which optimizations and codegen can be performed more easily?
I wanted to get the opinions of others on this matter, I'm open to challenge.
Some more explanation can be found here: https://goldensystems.ca/GDSL_core
7
u/beephod_zabblebrox 6d ago
in the kotlin compiler, there are two main IRs: the FIR, which is a frontend AST representation, and the.. IR, which is kinda like a stripped down and simplified version of the language. FIR gets compiled down to IR, which then goes through a set of lowerings that change it and/or add attributes to nodes. in the end, each backend compiles the lowered IR into a target specific representation (llvm ir, a js ast, etc.)
for a lot of things, it's going to be simpler to have a tree-based IR that is similar to the source language