Skip to content

Commit

Permalink
iced asm
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Mar 27, 2022
1 parent eaf778d commit eb7ec04
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void Long_Loop()
{
Stopwatch sw = Stopwatch.StartNew();

const int loopCount = 1000_000_000;
const int loopCount = 100_000_000;
byte[] repeat = new byte[4];
BinaryPrimitives.TryWriteInt32BigEndian(repeat, loopCount);

Expand Down
61 changes: 58 additions & 3 deletions src/Nethermind/Nethermind.Evm/IlVirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using Iced.Intel;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Implementation;
using Nethermind.Core.Extensions;
using Nethermind.Evm.Tracing;
using Nethermind.Int256;
using Nethermind.State;
using Label = System.Reflection.Emit.Label;

namespace Nethermind.Evm;

Expand Down Expand Up @@ -128,7 +134,9 @@ public TransactionSubstate Run(EvmState state, IWorldState worldState, ITxTracer

// TODO: stack invariants, gasCost application

DynamicMethod method = new("JIT_" + state.Env.CodeSource, typeof(EvmExceptionType), new []{typeof(long)}, typeof(IlVirtualMachine).Assembly.Modules.First(), true)
string name = "ILVM_" + Guid.NewGuid().ToString();

DynamicMethod method = new(name, typeof(EvmExceptionType), new[] { typeof(long) }, typeof(IlVirtualMachine).Assembly.Modules.First(), true)
{
InitLocals = false
};
Expand Down Expand Up @@ -422,9 +430,56 @@ public TransactionSubstate Run(EvmState state, IWorldState worldState, ITxTracer
il.MarkLabel(ret);
il.Emit(OpCodes.Ret);

Func<long, EvmExceptionType> del = method.CreateDelegate<Func<long,EvmExceptionType>>();
Func<long, EvmExceptionType> del = method.CreateDelegate<Func<long, EvmExceptionType>>();

EvmExceptionType result = del(state.GasAvailable);

using DataTarget dt = DataTarget.CreateSnapshotAndAttach(Process.GetCurrentProcess().Id);
using ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime();
ClrHeap heap = runtime.Heap;

ClrmdMethod clrMethod = heap.GetProxies("System.Reflection.Emit.DynamicMethod")
.Select(proxy =>
{
ulong mdToken = (ulong)proxy.m_methodHandle.m_value.m_handle;
return runtime.GetMethodByHandle(mdToken);
}).Single(m => m.Name == name) as ClrmdMethod;



TextWriter writer = Console.Out;

ulong address = clrMethod.NativeCode;
const int length = 2048;

writer.WriteLine($"ASM-------------");

unsafe
{
byte[] asm = new Span<byte>(new UIntPtr(address).ToPointer(), length).ToArray();

Decoder decoder = Decoder.Create(IntPtr.Size * 8, asm);

IntelFormatter formatter = new();
StringOutput? output = new();

decoder.IP = address;
while (decoder.IP < (address + length))
{
decoder.Decode(out Iced.Intel.Instruction instruction);

formatter.Format(instruction, output);

writer.Write((instruction.IP - address).ToString("x4"));
writer.Write(": ");
writer.WriteLine(output.ToStringAndReset());
}
}

writer.WriteLine("ASM END-------------");
writer.WriteLine();

return new TransactionSubstate(del(state.GasAvailable), true);
return new TransactionSubstate(result, true);
}

public void DisableSimdInstructions()
Expand Down
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Evm/Nethermind.Evm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DynaMD" Version="1.0.9.1" />
<PackageReference Include="Iced" Version="1.17.0" />
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.0.226801" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Math.Gmp.Native\MathGmp.Native\MathGmp.Native.csproj" />
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
Expand Down

0 comments on commit eb7ec04

Please sign in to comment.