ferib / LuaToolkit

Lua Encode/Decoder/Disassembler/Decompiler in C#
https://ferib.dev/blog.php?l=post/Lua_Devirtualization_Part_2_Decompiling_Lua
MIT License
86 stars 35 forks source link

Improve decompilation process #26

Closed Vertridge closed 5 months ago

Vertridge commented 1 year ago

This PR does a lot of things, it completely changes everything, I mean everything.

Improve disassembler:

Has an instruction implementation for every instruction type. This way the disassembler has more instruction specific information.

AST based decompilation

Instead of trying to build a string based on the instructions, I generate a Lua AST. The AST can than be dumped to a string to get the decompiled Lua. The AST could be used for other things, e.g. execute decompiled Lua, but this is currently not implemented.

Instruction Grouping

Getting the correct control flow for Lua instructions is tricky. I use a pattern matcher to already create instruction groups, for statements that have control-flow, e.g. IfStatements, For Loop, While Loops, ...

Code cleanup

This PR almost removes all code from Ferib himself to make it more readable and easier to maintain. There are a lot of parts that I am not proud of and should be implemented better, this might happen some day.