lua-users home
lua-l archive

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


On Mon, May 19, 2008 at 10:56 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> Also I was surprised to find that sometimes lua seems to leave holes
>> into the local variable allocation.
>
> Can you give more information about this?
>

It only SEEMS to leave holes (and only if you are too tired when
looking at it, and if you don't understand CALL and TFORLOOP codes)

Also to add my confusion there is a problem in TFORLOOP in locals.lua,
looking how the listing file is formed, the second loop should use b instead c.


Anyways my little function:

local function check_globals(name,old)
   for n,v in pairs(_G) do
      if not old[n] then
	 print("new global in ",name,n)
      end
   end
end

--------------------
which compiles to:
--------------------


main <tt.lua:0,0> (2 instructions, 8 bytes at 00346CE8)
0+ params, 2 slots, 0 upvalues, 1 local, 0 constants, 1 function
	1	[7]	CLOSURE  	0 0	; 00346F20
	2	[7]	RETURN   	0 1

function <tt.lua:1,7> (15 instructions, 60 bytes at 00346F20)
2 params, 11 slots, 0 upvalues, 7 locals, 4 constants, 0 functions
	1	[2]	GETGLOBAL	2 -1	; pairs
	2	[2]	GETGLOBAL	3 -2	; _G
	3	[2]	CALL     	2 2 4
	4	[2]	JMP      	8	; to 13
	5	[3]	GETTABLE 	7 1 5
	6	[3]	TEST     	7 0 1
	7	[3]	JMP      	5	; to 13
	8	[4]	GETGLOBAL	7 -3	; print
	9	[4]	LOADK    	8 -4	; "new global in "
	10	[4]	MOVE     	9 0
	11	[4]	MOVE     	10 5
	12	[4]	CALL     	7 4 1
	13	[2]	TFORLOOP 	2 2
	14	[5]	JMP      	-10	; to 5
	15	[7]	RETURN   	0 1


demonstrates the parameter problem with locals.lua
(I guess that can be fixed by parsing the function header
for parameter count, and marking those).

More seriously it shows how it is not possible to analyze the listing
line by line. In the compiled code the loop variables are created
at the bottom of the loop (TFORLOOP)

           Eero