lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sun, Nov 15, 2020 at 12:30 PM David W wrote:
Locals a, b, and c are shown as starting their lifetime at instruction 2 and having a 0-length scope, which is basically correct. (You can quibble about whether their lifetime should start with instruction 2 or 1.)

A local variable has a "definition" and a "lifetime".
"Definition" is the instruction where an initial value has been assigned to the variable.
"Lifetime" is the range of instructions inside the scope of a local variable, that is, where it can be used in RHS expressions.
Luac output shows only "lifetime".
For variable "a" the "definition" is the instruction #1, and "lifetime" is the empty range of instructions starting from instruction #2.

But d, e and f are all shown as starting at 11 and ending at 11, which is wrong for everything except f.

The Lua manual says:
"The scope of a local variable begins at the first statement after its declaration"
This means that "lifetime" of a local variable starts from the first instruction of the next Lua statement after the local variable definition.
The instructions #2-10 belong to "definition" (Lua statement "local d, e, f = ...."), and "lifetime" starts from instruction #11.

I don't think that with just this set of ranges and these opcodes, you can algorithmically deduce that (say) local e is assigned to register 2.

The algorithm:
at the start of "lifetime" of new local variables their VM registers are numbered sequentially starting from the first unused VM register.
At the instruction #11 (the start of "lifetime" of variables d,e,f) the VM registers 0-2 are already unused because the previous variables holding these registers (a,b,c) are out of scope (their "lifetime" has ended at instruction #2).