From 69251fabd8ccaf608bc8953d615ec3abed6b4f72 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Wed, 24 Jul 2024 17:14:17 +0300 Subject: [PATCH] Use BinaryPrimitives in ILDisassembler --- .../Common/TypeSystem/IL/ILDisassembler.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/coreclr/tools/Common/TypeSystem/IL/ILDisassembler.cs b/src/coreclr/tools/Common/TypeSystem/IL/ILDisassembler.cs index 1dfe404b63f265..511393c080a470 100644 --- a/src/coreclr/tools/Common/TypeSystem/IL/ILDisassembler.cs +++ b/src/coreclr/tools/Common/TypeSystem/IL/ILDisassembler.cs @@ -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; @@ -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; } @@ -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)