better readable code

This commit is contained in:
CPunch 2019-08-15 12:02:55 -05:00 committed by GitHub
parent 97c558956e
commit a5cbcfd1f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 23 deletions

79
luac.py
View File

@ -47,7 +47,7 @@ def get_bits(num, p, k):
kBitSubStr = binary[start : end] kBitSubStr = binary[start : end]
# convert extracted sub-string into decimal again # convert extracted sub-string into decimal again
return (int(kBitSubStr,2) % 256) return (int(kBitSubStr,2) % 256)
class LuaCompiler: class LuaCompiler:
def __init__(self): def __init__(self):
@ -55,8 +55,34 @@ class LuaCompiler:
self.o_flag = "-o" self.o_flag = "-o"
self.temp_out = "out.luac" self.temp_out = "out.luac"
self.chunks = [] self.chunks = []
self.chunk = {}
self.index = 0 self.index = 0
@staticmethod
def dis_chunk(chunk):
print("==== [[" + str(chunk['NAME']) + "]] ====\n")
for z in chunk['PROTOTYPES']:
print("** decoding proto\n")
LuaCompiler.dis_chunk(chunk['PROTOTYPES'][z])
print("\n==== [[" + str(chunk['NAME']) + "'s constants]] ====\n")
for z in chunk['CONSTANTS']:
i = chunk['CONSTANTS'][z]
print(str(z) + ": " + str(i['DATA']))
print("\n==== [[" + str(chunk['NAME']) + "'s dissassembly]] ====\n")
for z in chunk['INSTRUCTIONS']:
i = chunk['INSTRUCTIONS'][z]
if (i['TYPE'] == "ABC"):
print(lua_opcode_names[i['OPCODE']], i['A'], i['B'], i['C'])
elif (i['TYPE'] == "ABx"):
if (i['OPCODE'] == 1 or i['OPCODE'] == 5):
print(lua_opcode_names[i['OPCODE']], i['A'], -i['Bx']-1, chunk['CONSTANTS'][i['Bx']]['DATA'])
else:
print(lua_opcode_names[i['OPCODE']], i['A'], -i['Bx']-1)
elif (i['TYPE'] == "AsBx"):
print("AsBx", lua_opcode_names[i['OPCODE']], i['A'], i['sBx'])
def get_byte(self): def get_byte(self):
b = self.bytecode[self.index] b = self.bytecode[self.index]
@ -136,23 +162,23 @@ class LuaCompiler:
# opcode = opcode number; # opcode = opcode number;
# type = [ABC, ABx, AsBx] # type = [ABC, ABx, AsBx]
# A, B, C, Bx, or sBx depending on type # A, B, C, Bx, or sBx depending on type
}; }
data = self.get_int32(); data = self.get_int32()
opcode = get_bits(data, 1, 6); opcode = get_bits(data, 1, 6)
tp = lua_opcode_types[opcode]; tp = lua_opcode_types[opcode]
instruction['OPCODE'] = opcode instruction['OPCODE'] = opcode
instruction['TYPE'] = tp instruction['TYPE'] = tp
instruction['A'] = get_bits(data, 7, 14) instruction['A'] = get_bits(data, 7, 14)
if instruction['TYPE'] == "ABC": if instruction['TYPE'] == "ABC":
instruction['B'] = get_bits(data, 24, 32); instruction['B'] = get_bits(data, 24, 32)
instruction['C'] = get_bits(data, 15, 23); instruction['C'] = get_bits(data, 15, 23)
elif instruction['TYPE'] == "ABx": elif instruction['TYPE'] == "ABx":
instruction['Bx'] = get_bits(data, 15, 32); instruction['Bx'] = get_bits(data, 15, 32)
elif instruction['TYPE'] == "AsBx": elif instruction['TYPE'] == "AsBx":
instruction['sBx'] = get_bits(data, 15, 32) - 131071; instruction['sBx'] = get_bits(data, 15, 32) #- 131071
chunk['INSTRUCTIONS'][i] = instruction chunk['INSTRUCTIONS'][i] = instruction
@ -161,20 +187,20 @@ class LuaCompiler:
# get constants # get constants
print("** DECODING CONSTANTS") print("** DECODING CONSTANTS")
num = self.get_int(); num = self.get_int()
for i in range(num): for i in range(num):
constant = { constant = {
# type = constant type; # type = constant type;
# data = constant data; # data = constant data;
}; }
constant['TYPE'] = self.get_byte() constant['TYPE'] = self.get_byte()
if constant['TYPE'] == 1: if constant['TYPE'] == 1:
constant['DATA'] = (self.get_byte() != 0); constant['DATA'] = (self.get_byte() != 0)
elif constant['TYPE'] == 3: elif constant['TYPE'] == 3:
constant['DATA'] = self.get_double(); constant['DATA'] = self.get_double()
elif constant['TYPE'] == 4: elif constant['TYPE'] == 4:
constant['DATA'] = self.get_string(None)[:-1]; constant['DATA'] = self.get_string(None)[:-1]
print(constant) print(constant)
@ -184,7 +210,7 @@ class LuaCompiler:
print("** DECODING PROTOS") print("** DECODING PROTOS")
num = self.get_int(); num = self.get_int()
for i in range(num): for i in range(num):
chunk['PROTOTYPES'][i] = self.decode_chunk() chunk['PROTOTYPES'][i] = self.decode_chunk()
@ -192,22 +218,24 @@ class LuaCompiler:
print("** DECODING DEBUG SYMBOLS") print("** DECODING DEBUG SYMBOLS")
# line numbers # line numbers
num = self.get_int(); num = self.get_int()
for i in range(num): for i in range(num):
self.get_int32() self.get_int32()
# locals # locals
num = self.get_int(); num = self.get_int()
for i in range(num): for i in range(num):
self.get_string(None)[:-1] # local name print(self.get_string(None)[:-1]) # local name
self.get_int32() # local start PC self.get_int32() # local start PC
self.get_int32() # local end PC self.get_int32() # local end PC
# upvalues # upvalues
num = self.get_int(); num = self.get_int()
for i in range(num): for i in range(num):
self.get_string(None) # upvalue name self.get_string(None) # upvalue name
self.chunks.append(chunk)
return chunk return chunk
def decode_rawbytecode(self, rawbytecode): def decode_rawbytecode(self, rawbytecode):
@ -226,21 +254,23 @@ class LuaCompiler:
self.index = 4 self.index = 4
self.vm_version = self.get_byte() self.vm_version = self.get_byte()
self.offical_bytecode = self.get_byte() self.bytecode_format = self.get_byte()
self.big_endian = (self.get_byte() == 0) self.big_endian = (self.get_byte() == 0)
self.int_size = self.get_byte() self.int_size = self.get_byte()
self.size_t = self.get_byte() self.size_t = self.get_byte()
self.thing = self.get_string(3) self.instr_size = self.get_byte() # gets size of instructions
self.l_number_size = self.get_byte() # size of lua_Number
self.integral_flag = self.get_byte()
print("Lua VM version: ", hex(self.vm_version)) print("Lua VM version: ", hex(self.vm_version))
print("Big Endian: ", self.big_endian) print("Big Endian: ", self.big_endian)
print("int_size: ", self.int_size) print("int_size: ", self.int_size)
print("size_t: ", self.size_t) print("size_t: ", self.size_t)
print(self.thing)
#print(self.bytecode) #print(self.bytecode)
return self.decode_chunk() self.chunk = self.decode_chunk()
return self.chunk
def compileC(self, luafile): def compileC(self, luafile):
os.system(self.luac + " " + self.o_flag + " " + self.temp_out + " " + luafile) os.system(self.luac + " " + self.o_flag + " " + self.temp_out + " " + luafile)
@ -249,3 +279,6 @@ class LuaCompiler:
bytecode = luac_file.read() bytecode = luac_file.read()
return self.decode_rawbytecode(bytecode) return self.decode_rawbytecode(bytecode)
def print_dissassembly(self):
LuaCompiler.dis_chunk(self.chunk)