lua-users home
lua-l archive

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


On Mon, Mar 21, 2016 at 7:32 AM, Wangbo <wangbo.red@gmail.com> wrote:
> I have simple Lua code, why have two RETURN opcode for foo function after i
> check compile code.
>
> function foo (a)
>     print("foo", a)
>     return 2
> end
> foo(123)
>
> ./luac -l a.lua
>
> main <a.lua:0,0> (6 instructions at 0xe55b40)
> 0+ params, 2 slots, 1 upvalue, 0 locals, 3 constants, 1 function
> 1 [5] CLOSURE   0 0 ; 0xe55d40
> 2 [2] SETTABUP 0 -1 0 ; _ENV "foo"
> 3 [6] GETTABUP 0 0 -2 ; _ENV "foo"
> 4 [6] LOADK     1 -3 ; 123
> 5 [6] CALL     0 2 1
> 6 [6] RETURN   0 1
>
> function <a.lua:2,5> (7 instructions at 0xe55d40)
> 1 param, 4 slots, 1 upvalue, 1 local, 3 constants, 0 functions
> 1 [3] GETTABUP 1 0 -1 ; _ENV "print"
> 2 [3] LOADK     2 -2 ; "foo"
> 3 [3] MOVE     3 0
> 4 [3] CALL     1 3 1
> 5 [4] LOADK     1 -3 ; 2
> 6 [4] RETURN   1 2
> 7 [5] RETURN   0 1

If I recall correctly, it's simply because all functions get an
automatically-generated no-parameter RETURN. You then also included a
return statement in your code, with one parameter. That's all it is,
and as far as I know there's no harm except a couple wasted bytes to
have the extra one there.

/s/ Adam