lua-users home
lua-l archive

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


Thanks for the answer, I have another question regarding the order of assignments.

If we have a script like this
`a,b,c = d,e,f`, logically we could assume that this is the equivalent of 

a = d; 
b = e; 
c = f;

but if we look at the bytecode listing, we see that it assigns the globals on the reverse order (c, b, a)

x6QrT3XGBR.png

As far as I know, most languages do the expected output, such as c++, Java, Python, and even Luau now.
Is there a specific reason for choosing this behavior?


On Tue, Apr 25, 2023 at 9:52 AM Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> Recently I've been working on a C# lua compiler from scratch, and I'm
> interested in understanding better how register allocation is done in Lua.
> I'm a bit confused in a few scenarios and how the compiler knows where to
> allocate, mainly as I couldn't find any documentation on lua's compiler.

Register allocation in Lua is done basically in a stack approach.
(Actually, several instructions use the registers as a stack.)
There is no optimizations at all, as there are plenty of registers.

During compilation there is a "top" variable (field FuncState.freereg in
the current version); when an _expression_ needs a register it increments
that counter, which is decremented after that result has been used.

-- Roberto