From 0d88291fdec30c44417490daf92e72592b802964 Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Wed, 11 May 2022 23:18:30 +0200 Subject: [PATCH] Read all the GCproto in a bytecode serialization by skipping --- inspectbc.lua | 16 ++++++++++------ readbc.lua | 7 ++++++- tests/test3.lua | 3 +++ tests/test4.lua | 10 ++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 tests/test3.lua create mode 100644 tests/test4.lua diff --git a/inspectbc.lua b/inspectbc.lua index aea6a4d..5b153b7 100644 --- a/inspectbc.lua +++ b/inspectbc.lua @@ -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) - print('-- bytecode --') + 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)) diff --git a/readbc.lua b/readbc.lua index 386dde2..dc775b0 100644 --- a/readbc.lua +++ b/readbc.lua @@ -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, diff --git a/tests/test3.lua b/tests/test3.lua new file mode 100644 index 0000000..fc1b917 --- /dev/null +++ b/tests/test3.lua @@ -0,0 +1,3 @@ +local function b(x) + return x+1 +end diff --git a/tests/test4.lua b/tests/test4.lua new file mode 100644 index 0000000..3aa9681 --- /dev/null +++ b/tests/test4.lua @@ -0,0 +1,10 @@ +function b(x) + return x +end + + +function c(y) + return y +end + +return b(1) * c(2)