Basic bytecode inspection for test1.lua

This commit is contained in:
Enrico Lumetti 2022-05-08 00:10:26 +02:00
parent d800d334dc
commit a65b94e483
4 changed files with 110 additions and 73 deletions

14
inspectbc.lua Normal file
View File

@ -0,0 +1,14 @@
local readbc = require('readbc')
local opcodes = require('opcodes')
fp = io.open('tests/test1.luo')
header = readbc.read_header(fp)
proto = readbc.read_proto(fp)
for i = 1, #proto.bcins do
local decoded = opcodes.decode(proto.bcins[i])
local def = opcodes.defs[decoded.id]
print(def.name)
print(decoded.a)
print(decoded.d)
end

95
opcodes.lua Normal file
View File

@ -0,0 +1,95 @@
local bit = require('bit')
local function opcode(id, name, a, b, c, metamethod)
return {
id=id,
name=name,
a=a,
b=b,
c=c,
methametod=metamethod,
}
end
local function gen_opcodes(tbl)
res = {}
for i = 1, #tbl do
res[tbl[i].id] = {
name = tbl[i].name,
a = tbl[i].a,
b = tbl[i].b,
c = tbl[i].c,
metamethod = tbl[i].metamethod,
}
end
return res
end
local Mode = {
none = 0,
dst = 1,
base = 2,
var = 3,
rbase = 4,
uv = 5,
lit = 6,
lits = 7,
pri = 8,
num = 9,
str = 10,
tab = 11,
func = 12,
jump = 13,
cdata = 14,
max = 15,
none = 15, -- same as max
}
local Metamethod = {
index = 0,
newindex = 1,
gc = 2,
mode = 3,
eq = 4,
len = 5,
lt = 6,
le = 7,
concat = 8,
call = 9,
add = 10,
sub = 11,
mul = 12,
div = 13,
mod = 14,
pow = 15,
unm = 16,
metatable = 17,
tostring = 18,
}
local Opcodes_defs = gen_opcodes {
opcode(37, 'POW', Mode.dst, Mode.none, Mode.lits, Metamethod.none),
opcode(41, 'KSHORT', Mode.dst, Mode.var, Mode.var, Metamethod.pow),
opcode(75, 'RET0', Mode.rbase, Mode.none, Mode.lit, Mode.none),
}
local function decode(ins)
-- TODO: endianess
local id = bit.band(ins, 0xff)
local a = bit.band(ins, 0xff00) / 0x100
local b = bit.band(ins, 0xff0000) / 0x10000
local c = bit.band(ins, 0xff000000) / 0x1000000
local d = bit.band(ins, 0xffff0000) / 0x10000
return {
id = id,
a = a,
b = b,
c = c,
d = d,
}
end
return {
defs = Opcodes_defs,
decode = decode,
}

View File

@ -125,7 +125,7 @@ local function read_proto(fp)
return { return {
flags = flags, flags = flags,
numparams = numparams, numparams = numparams,
framsize = framesize, framesize = framesize,
numuv = numuv, numuv = numuv,
numkgc = numkgc, numkgc = numkgc,
numkn = numkn, numkn = numkn,

72
vm.lua
View File

@ -1,73 +1 @@
local funcbc = require('jit.util').funcbc
function opcode(id, name, a, b, c, metamethod)
return {
id=id,
name=name,
a=a,
b=b,
c=c,
methametod=metamethod,
}
end
function gen_opcodes(tbl)
length = #tbl
res = {}
for i=1,length do
res[i] = i+1
end
return res
end
Mode = {
none = 0,
dst = 1,
base = 2,
var = 3,,
rbase = 4,
uv = 5,
lit = 6,
lits = 7,
pri = 8,
num = 9,
str = 10,
tab = 11,
func = 12,
jump = 13,
cdata = 14,
max = 15,
none = 15, -- same as max
}
Metamethod = {
index = 0,
newindex = 1,
gc = 2,
mode = 3,
eq = 4,
len = 5,
lt = 6,
le = 7,
concat = 8,
call = 9,
add = 10,
sub = 11,
mul = 12,
div = 13,
mod = 14,
pow = 15,
unm = 16,
metatable = 17,
tostring = 18,
}
Opcodes = gen_opcodes {
opcode(37, 'POW', Mode.dst, Mode.none, Mode.lits, Metamethod.none),
opcode(41, 'KSHORT', Mode.dst, Mode.var, Mode.var, Metamethod.pow),
}
print(Opcodes[1])
ins, m = funcbc(test1, 3) -- TODO: what is m?
print(ins)
print(m)