lua-users home
lua-l archive

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


> Sorry, what I want to know is about the code at lparser.c line
> 520. The comment is " fix it at stack top (for GC) ". What dose it
> mean?If we skip the call to "luaK_exp2nextreg" and keep the expdesc as
> VRELOCABLE as other expdesc, I think that the "MOVE" instruction can
> be eliminated.

The reason is very subtle. (It took me some time to remember ;)

Lua uses a register-based virtual machine. One problem with these
machines is how the GC knows which registers are valid and which
registers are garbage. Lua solves this problem thanks to its naive
register-allocation policy. Registers are allocated in a stack-based
way; so, if Lua knows the last allocated register, it knows that all
registers up to that point are valid, all others after that are garbage.

But how does Lua know the last allocated register? All instructions
that can invoke the GC (OP_CLOSURE, OP_NEWTABLE, OP_CONCAT) code that
information in some way. Both OP_CLOSURE and OP_NEWTABLE put their
result in a new, fresh register, which therefore is the last one in use
when the instruction runs.

(OP_CONCAT is a little different. Because its arguments must be
in sequential registers, they are already using the top of the
stack. So the last register is either the result register or
the argument register, whichever is larger.)

-- Roberto