Skip to content

Stack Allocation

Dibyendu Majumdar edited this page Nov 28, 2020 · 5 revisions

When can we allocate variables on the stack.

  1. No GC needed. GC needs to scan the Lua stack. We could in theory do GC-able objects too but then we will have to write stuff to Lua stack whenever there is a chance that control might flow away from us.

  2. Variable did not escape - i.e. there is no up-value reference.

  3. If a table is created and used in the function without escaping then it could also be stack allocated ... even if it is passed to a function, if we know that the function only reads the data from the table then we could still allocate on stack. So we need to know that the table was not modified by the called function. (Maybe we should support read-only tables as arguments).

  4. Coroutines are bad for stack allocation. Fortunately, Ravi only allows running JIT code in the main thread.

Current State

We convert integer/number variables to temporaries and all integer/number temporaries can just be on C stack. Since we only convert variables that have not escaped, and are not GC-able, this means that the implementation is quite simple. However resuming a coroutine is not going to work as it may need the C stack values to be restored.

Clone this wiki locally