Allow constant memory recursion by reusing stack frame when doing tail-call.
Context
A crucial part of making real software in Teika is to do recursive functions, but every function call adds an entry to the stack, while the Teika runtime supports a shadow stack to avoid stack overflow, it will still lead to a lot memory being used.
But it is well-known that function calls at a tail call position can reuse the previous stack frame, so this is what this PR does. By having a wrapper $jmp where the runtime can do instanceof $jmp to know when to reuse the frame.
NOTE this works for any tail-call not only recursive ones.
Goals
Allow constant memory recursion by reusing stack frame when doing tail-call.
Context
A crucial part of making real software in Teika is to do recursive functions, but every function call adds an entry to the stack, while the Teika runtime supports a shadow stack to avoid stack overflow, it will still lead to a lot memory being used.
But it is well-known that function calls at a tail call position can reuse the previous stack frame, so this is what this PR does. By having a wrapper
$jmp
where the runtime can doinstanceof $jmp
to know when to reuse the frame.NOTE this works for any tail-call not only recursive ones.