Skip to content

Commit

Permalink
Use BinaryPrimitives in ILDisassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Jul 24, 2024
1 parent 7e06936 commit 69251fa
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/coreclr/tools/Common/TypeSystem/IL/ILDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers.Binary;
using System.Text;

using Internal.TypeSystem;
Expand Down Expand Up @@ -191,15 +192,15 @@ private byte ReadILByte()

private ushort ReadILUInt16()
{
ushort val = (ushort)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
_currentOffset += 2;
ushort val = BinaryPrimitives.ReadUInt16LittleEndian(_ilBytes.AsSpan(_currentOffset, sizeof(ushort)));
_currentOffset += sizeof(ushort);
return val;
}

private uint ReadILUInt32()
{
uint val = (uint)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8) + (_ilBytes[_currentOffset + 2] << 16) + (_ilBytes[_currentOffset + 3] << 24));
_currentOffset += 4;
uint val = BinaryPrimitives.ReadUInt32LittleEndian(_ilBytes.AsSpan(_currentOffset, sizeof(uint)));
_currentOffset += sizeof(uint);
return val;
}

Expand All @@ -211,21 +212,23 @@ private int ReadILToken()

private ulong ReadILUInt64()
{
ulong value = ReadILUInt32();
value |= (((ulong)ReadILUInt32()) << 32);
ulong value = BinaryPrimitives.ReadUInt64LittleEndian(_ilBytes.AsSpan(_currentOffset, sizeof(ulong)));
_currentOffset += sizeof(ulong);
return value;
}

private unsafe float ReadILFloat()
private float ReadILFloat()
{
uint value = ReadILUInt32();
return *(float*)(&value);
float value = BinaryPrimitives.ReadSingleLittleEndian(_ilBytes.AsSpan(_currentOffset, sizeof(float)));
_currentOffset += sizeof(float);
return value;
}

private unsafe double ReadILDouble()
private double ReadILDouble()
{
ulong value = ReadILUInt64();
return *(double*)(&value);
double value = BinaryPrimitives.ReadDoubleLittleEndian(_ilBytes.AsSpan(_currentOffset, sizeof(double)));
_currentOffset += sizeof(double);
return value;
}

public static void AppendOffset(StringBuilder sb, int offset)
Expand Down

0 comments on commit 69251fa

Please sign in to comment.