Read all the GCproto in a bytecode serialization by skipping

This commit is contained in:
Enrico Lumetti 2022-05-11 23:18:30 +02:00
parent e76d2cb187
commit 0d88291fde
4 changed files with 29 additions and 7 deletions

View File

@ -3,20 +3,24 @@ local opcodes = require('opcodes')
local function inspect_proto(proto)
print('\n-- proto --')
-- print('Debug: ' .. proto.debug)
print('Params: ' .. proto.numparams)
print('Frame Size: ' .. proto.framesize)
print('#Upvalues: ' .. proto.numuv)
print('#kgc: ' .. proto.numkgc)
print('#bcins: ' .. proto.numbc)
print('#instructions: ' .. proto.numbc)
print('#upvalues: ' .. proto.numuv)
print('#collectable constants: ' .. proto.numkgc)
print('#numeric constants: ' .. proto.numkn)
if proto.debugInfo ~= nil then
print('-- bytecode: lines ' .. proto.debugInfo.firstLine .. ' ' .. proto.debugInfo.numLine)
else
print('-- bytecode --')
end
for i = 1, #proto.bcins do
local decoded = opcodes.decode(proto.bcins[i])
if opcodes.defs[decoded.id] ~= nil then
local def = opcodes.defs[decoded.id]
-- check for ABC or AD format
if def.b == opcodes.Mode.none then
if def.b ~= opcodes.Mode.none then
print(string.format('%s\t A: %s\tB: %s\t C: %s', def.name, decoded.a, decoded.b, decoded.c))
else
print(string.format('%s\t A: %s\tD: %s', def.name, decoded.a, decoded.d))

View File

@ -94,6 +94,8 @@ local function read_proto(fp)
return
end
local curpos = fp:seek()
-- read proto header
local flags = read_u8(fp)
local numparams = read_u8(fp)
@ -109,7 +111,6 @@ local function read_proto(fp)
if bit.band(flags, FLAG_STRIP) ~= 0 then
debugLen = read_uleb128(fp)
-- TODO: is this correct?
if debugLen ~= 0 then
local firstLine = read_uleb128(fp)
local numLine = read_uleb128(fp)
@ -124,6 +125,10 @@ local function read_proto(fp)
local uvdata = read_u16n(fp, numuv)
-- local kgc
-- local numkn
-- local debug = fp:read(debugLen)
-- TODO: remove when reading of kgc, numkn and debug is implemented
fp:seek('set', curpos + length)
return {
flags = flags,

3
tests/test3.lua Normal file
View File

@ -0,0 +1,3 @@
local function b(x)
return x+1
end

10
tests/test4.lua Normal file
View File

@ -0,0 +1,10 @@
function b(x)
return x
end
function c(y)
return y
end
return b(1) * c(2)