Skip to content

Commit

Permalink
Merge pull request #25 from ZehMatt/refactor/registermask
Browse files Browse the repository at this point in the history
Refactor RegisterMaskGp
  • Loading branch information
ZehMatt authored Oct 23, 2021
2 parents 930c229 + 7702e13 commit 2e9f33e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
14 changes: 14 additions & 0 deletions src/Dotx64DbgManagedTests/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,24 @@ private static void LogException(TestingState state, Exception ex)
}
}

private static void DryRunTest(TestingState state, object instance, TestGroup group, TestEntry test)
{
try
{
test.Function.Invoke(instance, Array.Empty<object>());
}
catch (Exception ex)
{
}
}

private static bool RunTest(TestingState state, object instance, TestGroup group, TestEntry test)
{
bool passed = true;

// Do not account for assembly loading, make a quiet run first.
DryRunTest(state, instance, group, test);

var sw = new Stopwatch();
state.Ran++;

Expand Down
8 changes: 4 additions & 4 deletions src/Dotx64DbgManagedTests/Tests/Tests.RegisterMaskGp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void TestMask()

AssertEq(mask.Count, 2);

Register[] regs;
mask.GetRegisters(out regs);
var regs = mask.GetRegisters();
AssertEq(regs.Length, 2);

#if _X64_
AssertEq(regs[0], Register.Rax);
Expand Down Expand Up @@ -60,8 +60,8 @@ public void TestMask2()

AssertEq(mask.Count, 2);

Register[] regs;
mask.GetRegisters(out regs);
var regs = mask.GetRegisters();
AssertEq(regs.Length, 2);

#if _X64_
AssertEq(regs[0], Register.Rax);
Expand Down
60 changes: 24 additions & 36 deletions src/Dotx64Managed/API/Analysis/RegisterMask.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotx64Dbg.Analysis
{
using RegisterMaskType = UInt32;

public struct RegisterMaskGp
{
private UInt16 Mask;
private RegisterMaskType Mask;

public const uint MaxRegisters = sizeof(RegisterMaskType) * 8u;

public static readonly RegisterMaskGp None = new RegisterMaskGp();
public static readonly RegisterMaskGp All = new RegisterMaskGp { Mask = 0b1111111111111111 };
public static readonly RegisterMaskGp All = new RegisterMaskGp { Mask = ~0u };

public RegisterMaskGp Add(Dotx64Dbg.Operand.OpReg reg)
{
Expand All @@ -30,22 +30,22 @@ public RegisterMaskGp Add(Register reg)
{
index -= 4;
}
Mask |= (UInt16)(1u << index);
Mask |= (RegisterMaskType)(1u << index);
}
else if (reg >= Register.Ax && reg <= Register.R15w)
{
var index = (int)reg - (int)Register.Ax;
Mask |= (UInt16)(1u << index);
Mask |= (RegisterMaskType)(1u << index);
}
else if (reg >= Register.Eax && reg <= Register.R15d)
{
var index = (int)reg - (int)Register.Eax;
Mask |= (UInt16)(1u << index);
Mask |= (RegisterMaskType)(1u << index);
}
else if (reg >= Register.Rax && reg <= Register.R15)
{
var index = (int)reg - (int)Register.Rax;
Mask |= (UInt16)(1u << index);
Mask |= (RegisterMaskType)(1u << index);
}
else
{
Expand All @@ -58,24 +58,7 @@ public int Count
{
get
{
var res = 0;
var val = Mask;
var maxCount = 0;
if ((Mask & 0x000000FFu) != 0)
maxCount = 8;
if ((Mask & 0x0000FF00u) != 0)
maxCount = 16;
if ((Mask & 0x00FF0000u) != 0)
maxCount = 24;
if ((Mask & 0xFF000000u) != 0)
maxCount = 32;
for (int i = 0; i < maxCount; i++)
{
if ((val & 1u) != 0)
res++;
val >>= 1;
}
return res;
return (int)System.Runtime.Intrinsics.X86.Popcnt.PopCount(Mask);
}
}

Expand All @@ -84,7 +67,7 @@ public void GetRegisters(out Operand.OpReg[] res)
res = new Operand.OpReg[Count];

var idx = 0;
for (int i = 0; i < 32; i++)
for (int i = 0; i < MaxRegisters; i++)
{
if ((Mask & (1u << i)) != 0)
{
Expand All @@ -98,10 +81,9 @@ public void GetRegisters(out Operand.OpReg[] res)
}
}
}
public void GetRegisters(out Register[] res)
public Register[] GetRegisters()
{
res = new Register[Count];

var res = new Register[Count];
var idx = 0;
for (int i = 0; i < 32; i++)
{
Expand All @@ -116,26 +98,32 @@ public void GetRegisters(out Register[] res)
idx++;
}
}
return res;
}

public RegisterMaskType Value()
{
return Mask;
}

public static RegisterMaskGp operator &(RegisterMaskGp left, RegisterMaskGp right)
{
return new() { Mask = (UInt16)(left.Mask & right.Mask) };
return new() { Mask = (RegisterMaskType)(left.Mask & right.Mask) };
}

public static RegisterMaskGp operator |(RegisterMaskGp left, RegisterMaskGp right)
{
return new() { Mask = (UInt16)(left.Mask | right.Mask) };
return new() { Mask = (RegisterMaskType)(left.Mask | right.Mask) };
}

public static RegisterMaskGp operator ^(RegisterMaskGp left, RegisterMaskGp right)
{
return new() { Mask = (UInt16)(left.Mask ^ right.Mask) };
return new() { Mask = (RegisterMaskType)(left.Mask ^ right.Mask) };
}

public static RegisterMaskGp operator ~(RegisterMaskGp left)
{
return new() { Mask = (UInt16)~left.Mask };
return new() { Mask = (RegisterMaskType)~left.Mask };
}
}
}

0 comments on commit 2e9f33e

Please sign in to comment.