lua-users home
lua-l archive

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


> > ===============
> > local a;
> > a = function() end;
> > ===============
> > The code generated was:
> > ===============
> > 1 [1] LOADNIL 0 0
> > 2 [2] CLOSURE 1 0 ; 0x80049078
> > 3 [2] MOVE 0 1
> > 4 [2] RETURN 0 1
> > ===============
> > It seemed that we can not store the closure directly to local var "a".
> > Why not keep the expdesc as "VRELOCABLE" and patch the target register
> > later?
>
> The bytecode does store the function in "a", only the comment does
> not help you to know that.
>
> Local variables are mere aliases for VM registers.
>
> You should use "luac -l -l". That shows you the constants, locals,
> upvalues and function prototypes too, not only the disassembly.
>
> You'll see that "a" is register 0. The closure is generated in
> register 1, so the MOVE instruction does store the closure in "a".
>
Perhaps I didn't describe my question clearly. I knew the relationship between register and local var. What I want to know is the code generation policy at lparser.c line 520. I think that we can eliminate the "MOVE" instruction by removing this "luaK_exp2nextreg" call, and let "luaK_storevar" function to fill register 0 to "CLOSURE" instruction directly.