From 055af56e27fc0e8b322d0a84ca7ec04cd95619e9 Mon Sep 17 00:00:00 2001 From: CPunch Date: Thu, 11 Aug 2022 16:45:05 -0500 Subject: [PATCH] lparser.py: don't read RK values here, use lundump.py to read them --- README.md | 24 ++++++++++++------------ lparser.py | 14 +++++++------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 0a282f1..a219d2e 100644 --- a/README.md +++ b/README.md @@ -26,18 +26,18 @@ example.luac ==== [[example.lua's dissassembly]] ==== -[ 0] LOADK : R[0] K[1] -[ 1] LOADK : R[1] K[2] -[ 2] CONCAT : R[0] R[0] R[1] -[ 3] SETGLOBAL : R[0] R[0] -[ 4] EQ : R[0] K[3] K[3] -[ 5] JMP : R[0] R[5] -[ 6] GETGLOBAL : R[0] K[4] -[ 7] GETGLOBAL : R[1] K[0] -[ 8] GETTABLE : R[0] R[0] R[1] -[ 9] LOADK : R[1] K[5] -[ 10] CALL : R[0] R[2] R[1] -[ 11] RETURN : R[0] R[1] R[0] +[ 0] LOADK : R[0] K[1] ; load "pri" into R[0] +[ 1] LOADK : R[1] K[2] ; load "nt" into R[1] +[ 2] CONCAT : R[0] R[0] R[1] ; concat 2 values from R[0] to R[1] +[ 3] SETGLOBAL : R[0] R[0] ; +[ 4] EQ : R[0] K[3] K[3] ; +[ 5] JMP : R[0] R[5] ; +[ 6] GETGLOBAL : R[0] K[4] ; +[ 7] GETGLOBAL : R[1] K[0] ; +[ 8] GETTABLE : R[0] R[0] R[1] ; +[ 9] LOADK : R[1] K[5] ; load "Hello world" into R[1] +[ 10] CALL : R[0] R[2] R[1] ; +[ 11] RETURN : R[0] R[1] R[0] ; ==== [[example.lua's decompiled source]] ==== diff --git a/lparser.py b/lparser.py index a016b7f..e0f8c39 100644 --- a/lparser.py +++ b/lparser.py @@ -1,14 +1,14 @@ ''' lparser.py - Depends on ldump.py for lua dump deserialization. + Depends on lundump.py for lua dump deserialization. An experimental bytecode decompiler. ''' from operator import concat from subprocess import call -from lundump import Chunk, LuaUndump, Constant, Instruction, InstructionType, Opcodes +from lundump import Chunk, Constant, Instruction, Opcodes, whichRK, readRKasK class _Scope: def __init__(self, startPC: int, endPC: int): @@ -104,8 +104,8 @@ class LuaDecomp: # 'RK's are special in because can be a register or a konstant. a bitflag is read to determine which def __readRK(self, rk: int) -> str: - if (rk & (1 << 8)) > 0: - return self.chunk.constants[(rk & ~(1 << 8))].toCode() + if (whichRK(rk)) > 0: + return self.chunk.getConstant(readRKasK(rk)).toCode() else: return self.__getReg(rk) @@ -117,19 +117,19 @@ class LuaDecomp: # move registers self.__setReg(instr.A, self.__getReg(instr.B)) elif instr.opcode == Opcodes.LOADK: - self.__setReg(instr.A, self.chunk.constants[instr.B].toCode()) + self.__setReg(instr.A, self.chunk.getConstant(instr.B).toCode()) elif instr.opcode == Opcodes.LOADBOOL: if instr.B == 0: self.__setReg(instr.A, "false") else: self.__setReg(instr.A, "true") elif instr.opcode == Opcodes.GETGLOBAL: - self.__setReg(instr.A, self.chunk.constants[instr.B].data) + self.__setReg(instr.A, self.chunk.getConstant(instr.B).data) elif instr.opcode == Opcodes.GETTABLE: self.__setReg(instr.A, self.__getReg(instr.B) + "[" + self.__readRK(instr.C) + "]") elif instr.opcode == Opcodes.SETGLOBAL: self.__startStatement() - self.__addExpr(self.chunk.constants[instr.B].data + " = " + self.__getReg(instr.A)) + self.__addExpr(self.chunk.getConstant(instr.B).data + " = " + self.__getReg(instr.A)) elif instr.opcode == Opcodes.SETTABLE: self.__startStatement() self.__addExpr(self.__getReg(instr.A) + "[" + self.__readRK(instr.B) + "] = " + self.__readRK(instr.C))