assemblyscript
assemblyscript copied to clipboard
Experiment: What if locals are never free'd
In https://github.com/AssemblyScript/assemblyscript/pull/1240 it came up that the way we free temporary locals for reuse during compilation is both unexpected for developers new to the codebase as well as hard to get right when passing compiled expressions that might or might not (re)use a temporary local down to other code paths.
As such, this is an experiment what'd happen if every top-level, scoped and temporary local would be unique, making this process more convenient and less error-prone at the cost of emitting larger untouched functions. Note that the std/set and std/map tests don't compile this way currently due to unification of local states in loops, needing a new mechanism if we decide to go this route.
Regarding the failing tests, the most straight-forward option would be to unroll the first loop iteration if incompatible local states are detected. In most cases, this will not be necessary, while in the other cases it guarantees that local states are exactly what subsequent code expects, eliminating edge cases of recompilation by actually compiling twice. The cost we'd pay there is that the body of the loop is essentially emitted twice in uncommon cases, doubling code size of the loop, so a follow-up might be to look into optimizing such patterns in Binaryen where possible.
Keeping this PR open as part of 2020 vacuum since I'm planning to look into this further. In general it appears good to do as it simplifies some parts of the compiler a lot, making it easier to contribute.