[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Odd code generation in Lua
- From: Sean Conner <sean@...>
- Date: Sat, 30 Jul 2011 00:44:03 -0400
I've been working on some projects and I've noticed that Lua (5.1.4, with
all the current patches) seems to produce some extraneous instructions. For
instance, the following Lua code [1]:
local function Y(f)
local function g(...) return f(g, ...) end
return g
end
generates the following Lua opcodes:
main <y.lua:0,0> (2 instructions, 8 bytes at 0x9333d10)
0+ params, 2 slots, 0 upvalues, 1 local, 0 constants, 1 function
1 [4] CLOSURE 0 0 ; 0x9333eb0
2 [4] RETURN 0 1
function <y.lua:1,4> (5 instructions, 20 bytes at 0x9333eb0)
1 param, 2 slots, 0 upvalues, 2 locals, 0 constants, 1 function
1 [2] CLOSURE 1 0 ; 0x9334008
2 [2] MOVE 0 0
3 [2] MOVE 0 1
4 [3] RETURN 1 2
5 [4] RETURN 0 1 *** WHAT?
function <y.lua:2,2> (6 instructions, 24 bytes at 0x9334008)
0+ params, 4 slots, 2 upvalues, 1 local, 0 constants, 0 functions
1 [2] GETUPVAL 1 0 ; f
2 [2] GETUPVAL 2 1 ; g
3 [2] VARARG 3 0
4 [2] TAILCALL 1 0 0
5 [2] RETURN 1 0 *** WHAT?
6 [2] RETURN 0 1 *** WHAT?
I've marked the three instructions that appear to be extraneous, in that,
how can the code ever reach this spot, and why are these instructions even
generated?
-spc (It just seems odd to me to see redundant instructions being
generated)
[1] This is the famous Y combinator [2] in Lua.
[2] Basically, this allows you to create recursive lambda functions.
http://lua-users.org/lists/lua-l/2010-12/msg01219.html