lua-users home
lua-l archive

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


>    function test() return end
>
>function <t.lua:1> (2 instructions, 8 bytes at 008701D0)
>0 params, 2 stacks, 0 upvalues, 0 locals, 0 constants, 0 functions
> 1 [2] RETURN    0 1 0
> 2 [3] RETURN    0 1 0

>Why the two returns above? This is normal, a problem of luac, or a problem
>of lua?

It's normal (and harmless). The code generator always generate a RETURN
instruction at the end of functions. See close_func in lparser.c.

It's not simple to avoid such duplication. It's not a simple matter of just
checking whether the last instruction is already a RETURN, because of the
example below, where we need both RETURNs. In other words, it's not a matter
of peep-hole optimization. Since the extra RETURN is harmless and the cost is
a single instruction, the flow analysis required to remove it is not worth it.

--lhf

function f()
	if a then
		return 2
	end
end

function </tmp/i:1> (6 instructions, 24 bytes at 0x805b6d8)
0 params, 2 stacks, 0 upvalues, 0 locals, 2 constants, 0 functions
	1	[2]	GETGLOBAL	0 0	; a
	2	[2]	TEST     	0 0 0
	3	[2]	JMP      	0 2	; to 6
	4	[3]	LOADK    	0 1	; 2
	5	[3]	RETURN   	0 2 0
	6	[5]	RETURN   	0 1 0