-
Notifications
You must be signed in to change notification settings - Fork 5
Stack Allocation
When can we allocate variables on the stack.
-
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.
-
Variable did not escape - i.e. there is no up-value reference.
-
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).
-
Coroutines are bad for stack allocation. Fortunately, Ravi only allows running JIT code in the main thread.
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.