asteria
asteria copied to clipboard
RFC: Deblockification
The program
var i = 1;
{
i += 2;
i *= 3;
}
std.io.putf("i = $1\n", i); // 9
can be transformed to
var i = 1;
// {
i += 2;
i *= 3;
// }
std.io.putf("i = $1\n", i); // 9
However this requires rewriting the IR, as folows:
var i = 1;
{
i += 2; // depth of local reference `i` is 1
i *= 3;
}
std.io.putf("i = $1\n", i); // 9
var i = 1;
// {
i += 2; // depth of local reference `i` is now 0
i *= 3;
// }
std.io.putf("i = $1\n", i); // 9
This can be performed during either code generation or IR solidification. We also have to apply the same transformation to not only plain blocks, but if
branches, switch
clauses, while
bodies, etc. (note due to type completeness limitation, these cannot be Statement
s).
This is lambda lifting and closure eliminiation is the classical sense, or if you're using ANF it can be directly encoded in the "basic block" during transformation of high-level IR. It is error-prone to reinvent this particular kind of optimization technique.
I have an idea now. This should be doable in AST.