Skip to content

Commit

Permalink
Fix stack type of comparison operations
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Jul 1, 2024
1 parent 6f5816e commit 94942e1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Underanalyzer/Decompiler/AST/Nodes/BinaryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ public BinaryNode(IExpressionNode left, IExpressionNode right, IGMInstruction in
Right = right;
Instruction = instruction;

// Type1 and Type2 on the instruction represent the data types of Left and Right on the stack.
// Choose whichever type has a higher bias, or if equal, the smaller numerical data type value.
int bias1 = StackTypeBias(instruction.Type1);
int bias2 = StackTypeBias(instruction.Type2);
if (bias1 == bias2)
if (Instruction.Kind == Opcode.Compare)
{
StackType = (DataType)Math.Min((byte)instruction.Type1, (byte)instruction.Type2);
// For comparison operations, the result is always a boolean.
StackType = DataType.Boolean;
}
else
{
StackType = (bias1 > bias2) ? instruction.Type1 : instruction.Type2;
// Type1 and Type2 on the instruction represent the data types of Left and Right on the stack.
// Choose whichever type has a higher bias, or if equal, the smaller numerical data type value.
int bias1 = StackTypeBias(instruction.Type1);
int bias2 = StackTypeBias(instruction.Type2);
if (bias1 == bias2)
{
StackType = (DataType)Math.Min((byte)instruction.Type1, (byte)instruction.Type2);
}
else
{
StackType = (bias1 > bias2) ? instruction.Type1 : instruction.Type2;
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions UnderanalyzerTest/DecompileContext.DecompileToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2070,4 +2070,37 @@ bf [end]
"""
);
}

[Fact]
public void TestBooleanSwitch()
{
TestUtil.VerifyDecompileResult(
"""
:[0]
push.v builtin.a
push.v builtin.b
cmp.v.v LT
dup.b 0
pushi.e 0
cmp.i.b EQ
bt [2]
:[1]
b [3]
:[2]
b [3]
:[3]
popz.b
""",
"""
switch (a < b)
{
case 0:
break;
}
"""
);
}
}

0 comments on commit 94942e1

Please sign in to comment.