A rather simple method for moving values to the stack if possible. All it does is check if the stack height doesn't change between a set and get, and if it doesn't then these can be safely removed. Unless the local is used again later, then the set can be removed and the get replaced with a tee (set+get). This handily cleans up the slightly annoying/common set_local get_local pairs.
Due to the way blocks work with the stack, essentially creating a new stack on entry, this can only be done with instructions at the same height. Blocks can return a single value which would allow applying this once on the way out of a block, but I've ignored that for the time being, and it might be best to do it separately. The restriction of 1 return from a block might be lifted in the future, which would make it easier to make use of.
Here is an example:
function addTwo(x, y)
res = 10+x
t = 10+x
r = 431*y
p = t*r
q = p+2
return res
end
For the sake of cleaning up the registers I've passed it to allocate_registers, but all the structural changes are due to stack_locals. In this example all calls to set_local are removed (apart from the call to drop which is hiding an unused set_local).
A rather simple method for moving values to the stack if possible. All it does is check if the stack height doesn't change between a set and get, and if it doesn't then these can be safely removed. Unless the local is used again later, then the set can be removed and the get replaced with a tee (set+get). This handily cleans up the slightly annoying/common set_local get_local pairs.
Due to the way blocks work with the stack, essentially creating a new stack on entry, this can only be done with instructions at the same height. Blocks can return a single value which would allow applying this once on the way out of a block, but I've ignored that for the time being, and it might be best to do it separately. The restriction of 1 return from a block might be lifted in the future, which would make it easier to make use of.
Here is an example:
For the sake of cleaning up the registers I've passed it to
allocate_registers
, but all the structural changes are due tostack_locals
. In this example all calls toset_local
are removed (apart from the call to drop which is hiding an unusedset_local
).I plan to implement drop removal next which would reduce this particular example to the first 3 instructions.