lua-users home
lua-l archive

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


> The modified version has the ability to undump compiled files (a 
> feature that supposedly existed in v3 but not v4).

What do you mean by undump? In both v3 and v4, luac -l generates listings.

> I am trying to learn *how* to reconstruct the source code--well enough
> to automate the process.

The virtual machine in Lua 4 is a stack machine. To reconstruct the source
code you have to simulate it symbolically. The way I do it is to maintain
a stack in which each slot contains a string representation of whatever is
supposed to be there after each instruction is executed. This is pretty easy
for most instructions, but harder for some. Here is an example:

function SETTABLE(p1,p2,c)
	local k=SP-p1
	local t=S[k]
	local i=S[k+1]
	local v=S[SP-1]
	write(t,"[",i,"]=",v,"\n")
	SP=SP-p2
end

S is the stack and SP is the stack pointer. lopcodes.h says this:

OP_SETTABLE,/*	A B	v a_a-a_1 i t	(pops b values)	t[i]=v		*/

which means the value v is at the top, there are A values between v and the
index i and the table t. In the code above, A is p1 and B is p2. The code
directly implements the description in the comment.

Here is another example:

function GETGLOBAL(p1,p2,c)
	push(c)
end

This simply stores in the stack the name of the global, In this case, it's
easier to use the comment field in the listing (c in the code above):
     1	[11]	GETGLOBAL  	0	; aitrace

I hope you get the idea. Have fun. (It *is* fun! Just implement one
instruction at a time, as they occur in the listings.)

> I don't understand what all the numbers mean or the best way to 
> approach automation.

Like I said, you have to read lopcodes.h and lvm.c. That's what I did...
Perhaps it'd be easier to start with LuaDC and fix that, if you have
access to its source code (I could not find it with google).
--lhf