forked from katalash/DSLuaDecompiler
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added debug support for T7, fixed self calls, changed function id gen…
…eration, fix for multiple returns order, fixed varargs
- Loading branch information
1 parent
2bf83d3
commit cfc5c51
Showing
32 changed files
with
569 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
CoDLuaDecompiler.Decompiler/Analyzers/Havok/DebugLineInfoAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Linq; | ||
using CoDLuaDecompiler.Decompiler.IR.Functions; | ||
using CoDLuaDecompiler.Decompiler.IR.Instruction; | ||
|
||
namespace CoDLuaDecompiler.Decompiler.Analyzers.Havok | ||
{ | ||
public class DebugLineInfoAnalyzer : IAnalyzer | ||
{ | ||
public void Analyze(Function f) | ||
{ | ||
if (f.FunctionDebugInfo == null) | ||
return; | ||
|
||
AddLines(f); | ||
} | ||
|
||
public void AddLines(Function f) | ||
{ | ||
f.Closures.ForEach(AddLines); | ||
for (int bi = 0; bi < f.Blocks.Count; bi++) | ||
{ | ||
var b = f.Blocks[bi]; | ||
if (b.Instructions.Count <= 1) | ||
continue; | ||
var prevLine = b.Instructions[0].LineLocation == 0 && bi > 0 ? b.Instructions[1].LineLocation - 1 : b.Instructions[0].LineLocation; | ||
var prevLength = b.Instructions[0].ToString()!.Count(s => s == '\n') + 1; | ||
for (int i = 1; i < b.Instructions.Count; i++) | ||
{ | ||
var instr = b.Instructions[i]; | ||
for (int j = 0; j < instr.LineLocation - prevLine - prevLength; j++) | ||
{ | ||
b.Instructions.Insert(i, new NewLine()); | ||
i++; | ||
} | ||
|
||
prevLine = instr.LineLocation == 0 ? prevLine + 1 : instr.LineLocation; | ||
prevLength = instr.ToString()!.Count(s => s == '\n') + 1; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
CoDLuaDecompiler.Decompiler/Analyzers/Havok/LoopIdentifierReplacementAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using CoDLuaDecompiler.Decompiler.IR.Expression; | ||
using CoDLuaDecompiler.Decompiler.IR.Functions; | ||
using CoDLuaDecompiler.Decompiler.IR.Identifiers; | ||
using CoDLuaDecompiler.Decompiler.IR.Instruction; | ||
|
||
namespace CoDLuaDecompiler.Decompiler.Analyzers.Havok | ||
{ | ||
public class LoopIdentifierReplacementAnalyzer : IAnalyzer | ||
{ | ||
// We need to replace the identifiers used by loops because the ssa doesn't notice it's used in different scope | ||
public void Analyze(Function f) | ||
{ | ||
for (int i = 1; i < f.Blocks.Count; i++) | ||
{ | ||
var b = f.Blocks[i]; | ||
|
||
if (!b.IsLoopHead) | ||
continue; | ||
|
||
|
||
|
||
if (b.Instructions.Any() && b.Instructions[0] is Assignment a && a.Left.Count == 2 && a.Right is FunctionCall fc && fc.Arguments.Count == 2) | ||
{ | ||
if (fc.Function is IdentifierReference ir2 && ir2.Identifier.DefiningInstruction is Assignment a2 && a2.Left.Count == 3 && b.LoopLatches.Any() && | ||
b.LoopLatches[0].Instructions.Any() && b.LoopLatches[0].Instructions[0] is Assignment a3 && a3.Left.Count == 1 && a3.Right is IdentifierReference ir && ir.Identifier == a.Left[0].Identifier) | ||
{ | ||
var newFuncId = f.SymbolTable.GetNewRegister(); | ||
var newIterId = f.SymbolTable.GetNewRegister(); | ||
var newIndexId = f.SymbolTable.GetNewRegister(); | ||
var oldFuncId = ((IdentifierReference)fc.Function).Identifier; | ||
var oldIterId = ((IdentifierReference)fc.Arguments[0]).Identifier; | ||
var oldIndexId = ((IdentifierReference)fc.Arguments[1]).Identifier; | ||
((IdentifierReference) fc.Function).Identifier = newFuncId; | ||
((IdentifierReference) fc.Arguments[0]).Identifier = newIterId; | ||
((IdentifierReference) fc.Arguments[1]).Identifier = newIndexId; | ||
a2.Left[0].Identifier = newFuncId; | ||
a2.Left[1].Identifier = newIterId; | ||
a2.Left[2].Identifier = newIndexId; | ||
a3.Left[0].Identifier = newIndexId; | ||
} | ||
|
||
var newKeyId = f.SymbolTable.GetNewRegister(); | ||
var newValueId = f.SymbolTable.GetNewRegister(); | ||
var oldKeyId = a.Left[0].Identifier; | ||
var oldValueId = a.Left[1].Identifier; | ||
|
||
for (int j = i + 1; j < f.Blocks.Count; j++) | ||
{ | ||
var b2 = f.Blocks[j]; | ||
if (b2 == b.LoopFollow) | ||
break; | ||
|
||
for (int k = 0; k < b2.Instructions.Count; k++) | ||
{ | ||
var instr = b2.Instructions[k]; | ||
ReplaceRegisterUses(oldKeyId, newKeyId, instr.GetUses(true), instr); | ||
ReplaceRegisterUses(oldKeyId, newKeyId, instr.GetDefines(true), instr); | ||
ReplaceRegisterUses(oldValueId, newValueId, instr.GetUses(true), instr); | ||
ReplaceRegisterUses(oldValueId, newValueId, instr.GetDefines(true), instr); | ||
} | ||
} | ||
|
||
a.Left[0].Identifier = newKeyId; | ||
a.Left[1].Identifier = newValueId; | ||
} | ||
} | ||
} | ||
|
||
private void ReplaceRegisterUses(Identifier original, Identifier newId, HashSet<Identifier> ids, IInstruction instr) | ||
{ | ||
foreach (var uses in ids) | ||
{ | ||
if (uses == original) | ||
{ | ||
instr.ReplaceUses(uses, new IdentifierReference(newId)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.