Skip to content

Commit

Permalink
Fix: bugs in TrashedRegisterFinder and DistributedConversionRule.
Browse files Browse the repository at this point in the history
Reko was generating invalid CONVERT() expression.
  • Loading branch information
uxmal committed Jun 5, 2024
1 parent 14e8046 commit ccee2ab
Show file tree
Hide file tree
Showing 26 changed files with 238,646 additions and 238,448 deletions.
14 changes: 13 additions & 1 deletion src/Decompiler/Analysis/TrashedRegisterFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,20 @@ public Expression GetValue(MemoryAccess access, IMemory memory)
return StackState!.Get(stack.StackOffset);
if (!IdState.TryGetValue(id, out (Expression, BitRange) value))
return null;
else if (value.Item1 is { })
return MaybeSlice(value.Item1, id.DataType);
else
return value.Item1;
return null;
}

private static Expression MaybeSlice(Expression exp, DataType dt)
{
var wantedBitsize = dt.BitSize;
if (exp.DataType.BitSize > wantedBitsize)
{
exp = new Slice(dt, exp, 0);
}
return exp;
}

public BitRange GetBitRange(Identifier id)
Expand Down
9 changes: 1 addition & 8 deletions src/Decompiler/Evaluation/DistributedConversionRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Reko.Core;
using Reko.Core.Expressions;
using Reko.Core.Operators;
using Reko.Core.Types;

namespace Reko.Evaluation
{
Expand Down Expand Up @@ -54,7 +47,7 @@ public DistributedConversionRule()
return new Conversion(
new BinaryExpression(op, dt, eLeft, eRight),
dtSrc,
dt);
cLeft.DataType);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/UnitTests/Decompiler/Analysis/TrashedRegisterFinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,29 @@ public void TrfLoop()
RunTest();
}

[Test]
public void TrfSliceNeeded()
{
var arch = new Reko.Arch.X86.X86ArchitectureFlat32(new ServiceContainer(), "x86-protected-32", new Dictionary<string, object>());
Given_Architecture(arch);

Expect(
"TrfSliceNeeded",
"Preserved: ",
"Trashed: rax",
"");

builder.Add(nameof(TrfSliceNeeded), m =>
{
var rax = m.Frame.EnsureRegister(arch.GetRegister("rax"));
var eax = m.Frame.EnsureRegister(arch.GetRegister("eax"));
var fp = m.Frame.FramePointer;
m.MStore(m.IAddS(fp, 30), rax);
m.Assign(eax, m.Mem32(m.IAddS(fp, 30)));
m.Assign(rax, m.Convert(eax, PrimitiveType.Word32, PrimitiveType.UInt64));
m.Return();
});
RunTest();
}
}
}
20 changes: 20 additions & 0 deletions src/UnitTests/Decompiler/Evaluation/ExpressionSimplifierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,5 +1328,25 @@ public void Exs_SliceMemPtrIdPlusIntConst()

Assert.AreEqual("Mem0[id_3 + 2<32>:word32]", exp.ToString());
}

[Test]
public void Exs_DistributedConversion()
{
Given_ExpressionSimplifier();
var rax_6 = Given_Tmp("id", PrimitiveType.Ptr64);
Expression exp =
m.IAdd(
m.Convert(
m.Mem16(m.IAddS(rax_6, 14)),
PrimitiveType.Word16,
PrimitiveType.Word32),
m.Convert(
m.Mem16(m.IAddS(rax_6, 12)),
PrimitiveType.Word16,
PrimitiveType.Word32));
exp = RunExpressionSimplifier(exp);

Assert.AreEqual("CONVERT(Mem0[id_3 + 14<i64>:word16] + Mem0[id_3 + 12<i64>:word16], word16, word32)", exp.ToString());
}
}
}
Loading

0 comments on commit ccee2ab

Please sign in to comment.