teikalang / teika

MIT License
318 stars 7 forks source link

jsend: implement tail call optimization #177

Closed EduardoRFS closed 11 months ago

EduardoRFS commented 11 months ago

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 do instanceof $jmp to know when to reuse the frame.

NOTE this works for any tail-call not only recursive ones.