Skip to content

Commit

Permalink
Small edge case local declaration fix
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Dec 2, 2024
1 parent 87a29f4 commit 3106422
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Underanalyzer/Decompiler/AST/LocalScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void GenerateDeclarations(HashSet<string> declaredAnywhere)
}
}

// For remaining locals declared in this scope, declare them on their first assignments
// For remaining locals to be declared in this scope, declare them on their first assignments
foreach (string local in DeclaredLocals)
{
if (hoistedLocals.Contains(local))
Expand Down
1 change: 1 addition & 0 deletions Underanalyzer/Decompiler/AST/Nodes/AssignNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ binary.Instruction.Kind is Opcode.Add or Opcode.Subtract or Opcode.Multiply or O
public IStatementNode PostClean(ASTCleaner cleaner)
{
if (cleaner.Context.Settings.CleanupLocalVarDeclarations &&
AssignKind == AssignType.Normal &&
Variable is VariableNode
{
Left: Int16Node { Value: (int)InstanceType.Local } or InstanceTypeNode { InstanceType: InstanceType.Local },
Expand Down
44 changes: 44 additions & 0 deletions UnderanalyzerTest/DecompileContext.DecompileToString.Locals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ pop.v.i local.a
);
}

[Fact]
public void TestOnlyIncrementLocal()
{
TestUtil.VerifyDecompileResult(
"""
:[0]
push.v local.a
pushi.e 1
add.i.v
pop.v.v local.a
""",
"""
var a;
a += 1;
"""
);
}

[Fact]
public void TestOnlyIncrementLocal2()
{
TestUtil.VerifyDecompileResult(
"""
:[0]
push.v local.a
pushi.e 1
add.i.v
pop.v.v local.a
pushi.e -7
pushi.e 0
dup.i 1
push.v [array]local.b
pushi.e 1
add.i.v
pop.i.v [array]local.b
""",
"""
var a, b;
a += 1;
b[0] += 1;
"""
);
}

[Fact]
public void TestFunctionLocal1()
{
Expand Down

0 comments on commit 3106422

Please sign in to comment.