From 7fa1530c9d86ca3d4fc54a3c98de1ca667e1022b Mon Sep 17 00:00:00 2001 From: Phil Garcia Date: Mon, 25 Jun 2018 22:27:59 -0700 Subject: Changed internal value to readonly to all the primitive types --- src/System.Private.CoreLib/shared/System/Boolean.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Byte.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Char.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Double.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Int16.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Int32.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Int64.cs | 4 ++-- src/System.Private.CoreLib/shared/System/IntPtr.cs | 4 ++-- src/System.Private.CoreLib/shared/System/SByte.cs | 4 ++-- src/System.Private.CoreLib/shared/System/Single.cs | 4 ++-- src/System.Private.CoreLib/shared/System/UInt16.cs | 4 ++-- src/System.Private.CoreLib/shared/System/UInt32.cs | 4 ++-- src/System.Private.CoreLib/shared/System/UInt64.cs | 4 ++-- src/System.Private.CoreLib/shared/System/UIntPtr.cs | 4 ++-- 14 files changed, 28 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/System.Private.CoreLib/shared/System/Boolean.cs b/src/System.Private.CoreLib/shared/System/Boolean.cs index dbc7bd75ee..7ec9227f1b 100644 --- a/src/System.Private.CoreLib/shared/System/Boolean.cs +++ b/src/System.Private.CoreLib/shared/System/Boolean.cs @@ -19,12 +19,12 @@ namespace System { [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Boolean : IComparable, IConvertible, IComparable, IEquatable + public readonly struct Boolean : IComparable, IConvertible, IComparable, IEquatable { // // Member Variables // - private bool m_value; // Do not rename (binary serialization) + private readonly bool m_value; // Do not rename (binary serialization) // The true value. // diff --git a/src/System.Private.CoreLib/shared/System/Byte.cs b/src/System.Private.CoreLib/shared/System/Byte.cs index 64512b0fee..5e8580642b 100644 --- a/src/System.Private.CoreLib/shared/System/Byte.cs +++ b/src/System.Private.CoreLib/shared/System/Byte.cs @@ -12,9 +12,9 @@ namespace System [Serializable] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Byte : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct Byte : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private byte m_value; // Do not rename (binary serialization) + private readonly byte m_value; // Do not rename (binary serialization) // The maximum value that a Byte may represent: 255. public const byte MaxValue = (byte)0xFF; diff --git a/src/System.Private.CoreLib/shared/System/Char.cs b/src/System.Private.CoreLib/shared/System/Char.cs index a3d2963402..8c743369b1 100644 --- a/src/System.Private.CoreLib/shared/System/Char.cs +++ b/src/System.Private.CoreLib/shared/System/Char.cs @@ -21,12 +21,12 @@ namespace System [Serializable] [StructLayout(LayoutKind.Sequential)] [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Char : IComparable, IComparable, IEquatable, IConvertible + public readonly struct Char : IComparable, IComparable, IEquatable, IConvertible { // // Member Variables // - private char m_value; // Do not rename (binary serialization) + private readonly char m_value; // Do not rename (binary serialization) // // Public Constants diff --git a/src/System.Private.CoreLib/shared/System/Double.cs b/src/System.Private.CoreLib/shared/System/Double.cs index 79021f22b8..308dda5fad 100644 --- a/src/System.Private.CoreLib/shared/System/Double.cs +++ b/src/System.Private.CoreLib/shared/System/Double.cs @@ -26,7 +26,7 @@ namespace System [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public struct Double : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private double m_value; // Do not rename (binary serialization) + private readonly double m_value; // Do not rename (binary serialization) // // Public Constants @@ -226,7 +226,7 @@ namespace System [MethodImpl(MethodImplOptions.AggressiveInlining)] // 64-bit constants make the IL unusually large that makes the inliner to reject the method public override int GetHashCode() { - var bits = Unsafe.As(ref m_value); + var bits = BitConverter.DoubleToInt64Bits(m_value); // Optimized check for IsNan() || IsZero() if (((bits - 1) & 0x7FFFFFFFFFFFFFFF) >= 0x7FF0000000000000) diff --git a/src/System.Private.CoreLib/shared/System/Int16.cs b/src/System.Private.CoreLib/shared/System/Int16.cs index 2993337a74..4973299763 100644 --- a/src/System.Private.CoreLib/shared/System/Int16.cs +++ b/src/System.Private.CoreLib/shared/System/Int16.cs @@ -12,9 +12,9 @@ namespace System [Serializable] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Int16 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct Int16 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private short m_value; // Do not rename (binary serialization) + private readonly short m_value; // Do not rename (binary serialization) public const short MaxValue = (short)0x7FFF; public const short MinValue = unchecked((short)0x8000); diff --git a/src/System.Private.CoreLib/shared/System/Int32.cs b/src/System.Private.CoreLib/shared/System/Int32.cs index 5c40812c0a..1d0aefe73c 100644 --- a/src/System.Private.CoreLib/shared/System/Int32.cs +++ b/src/System.Private.CoreLib/shared/System/Int32.cs @@ -12,9 +12,9 @@ namespace System [Serializable] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Int32 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct Int32 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private int m_value; // Do not rename (binary serialization) + private readonly int m_value; // Do not rename (binary serialization) public const int MaxValue = 0x7fffffff; public const int MinValue = unchecked((int)0x80000000); diff --git a/src/System.Private.CoreLib/shared/System/Int64.cs b/src/System.Private.CoreLib/shared/System/Int64.cs index 29198781d7..62c9ffd4fe 100644 --- a/src/System.Private.CoreLib/shared/System/Int64.cs +++ b/src/System.Private.CoreLib/shared/System/Int64.cs @@ -12,9 +12,9 @@ namespace System [Serializable] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Int64 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct Int64 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private long m_value; // Do not rename (binary serialization) + private readonly long m_value; // Do not rename (binary serialization) public const long MaxValue = 0x7fffffffffffffffL; public const long MinValue = unchecked((long)0x8000000000000000L); diff --git a/src/System.Private.CoreLib/shared/System/IntPtr.cs b/src/System.Private.CoreLib/shared/System/IntPtr.cs index c5419a96e0..f79334a96b 100644 --- a/src/System.Private.CoreLib/shared/System/IntPtr.cs +++ b/src/System.Private.CoreLib/shared/System/IntPtr.cs @@ -17,13 +17,13 @@ namespace System { [Serializable] [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct IntPtr : IEquatable, ISerializable + public readonly struct IntPtr : IEquatable, ISerializable { // WARNING: We allow diagnostic tools to directly inspect this member (_value). // See https://github.com/dotnet/corert/blob/master/Documentation/design-docs/diagnostics/diagnostics-tools-contract.md for more details. // Please do not change the type, the name, or the semantic usage of this member without understanding the implication for tools. // Get in touch with the diagnostics team if you have questions. - private unsafe void* _value; // Do not rename (binary serialization) + private readonly unsafe void* _value; // Do not rename (binary serialization) [Intrinsic] public static readonly IntPtr Zero; diff --git a/src/System.Private.CoreLib/shared/System/SByte.cs b/src/System.Private.CoreLib/shared/System/SByte.cs index e3c6d170a1..e347e3b323 100644 --- a/src/System.Private.CoreLib/shared/System/SByte.cs +++ b/src/System.Private.CoreLib/shared/System/SByte.cs @@ -12,9 +12,9 @@ namespace System [Serializable] [CLSCompliant(false)] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct SByte : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct SByte : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private sbyte m_value; // Do not rename (binary serialization) + private readonly sbyte m_value; // Do not rename (binary serialization) // The maximum value that a Byte may represent: 127. public const sbyte MaxValue = (sbyte)0x7F; diff --git a/src/System.Private.CoreLib/shared/System/Single.cs b/src/System.Private.CoreLib/shared/System/Single.cs index 1a778c957c..d62ff9ceea 100644 --- a/src/System.Private.CoreLib/shared/System/Single.cs +++ b/src/System.Private.CoreLib/shared/System/Single.cs @@ -25,7 +25,7 @@ namespace System [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public struct Single : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private float m_value; // Do not rename (binary serialization) + private readonly float m_value; // Do not rename (binary serialization) // // Public constants @@ -217,7 +217,7 @@ namespace System public override int GetHashCode() { - var bits = Unsafe.As(ref m_value); + var bits = BitConverter.SingleToInt32Bits(m_value); // Optimized check for IsNan() || IsZero() if (((bits - 1) & 0x7FFFFFFF) >= 0x7F800000) diff --git a/src/System.Private.CoreLib/shared/System/UInt16.cs b/src/System.Private.CoreLib/shared/System/UInt16.cs index cd09894c62..f9ef1f6a62 100644 --- a/src/System.Private.CoreLib/shared/System/UInt16.cs +++ b/src/System.Private.CoreLib/shared/System/UInt16.cs @@ -13,9 +13,9 @@ namespace System [CLSCompliant(false)] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct UInt16 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct UInt16 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private ushort m_value; // Do not rename (binary serialization) + private readonly ushort m_value; // Do not rename (binary serialization) public const ushort MaxValue = (ushort)0xFFFF; public const ushort MinValue = 0; diff --git a/src/System.Private.CoreLib/shared/System/UInt32.cs b/src/System.Private.CoreLib/shared/System/UInt32.cs index d72a0a1956..5ed193e956 100644 --- a/src/System.Private.CoreLib/shared/System/UInt32.cs +++ b/src/System.Private.CoreLib/shared/System/UInt32.cs @@ -13,9 +13,9 @@ namespace System [CLSCompliant(false)] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct UInt32 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct UInt32 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private uint m_value; // Do not rename (binary serialization) + private readonly uint m_value; // Do not rename (binary serialization) public const uint MaxValue = (uint)0xffffffff; public const uint MinValue = 0U; diff --git a/src/System.Private.CoreLib/shared/System/UInt64.cs b/src/System.Private.CoreLib/shared/System/UInt64.cs index 3b1010a51f..6abd76da21 100644 --- a/src/System.Private.CoreLib/shared/System/UInt64.cs +++ b/src/System.Private.CoreLib/shared/System/UInt64.cs @@ -13,9 +13,9 @@ namespace System [CLSCompliant(false)] [StructLayout(LayoutKind.Sequential)] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct UInt64 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable + public readonly struct UInt64 : IComparable, IConvertible, IFormattable, IComparable, IEquatable, ISpanFormattable { - private ulong m_value; // Do not rename (binary serialization) + private readonly ulong m_value; // Do not rename (binary serialization) public const ulong MaxValue = (ulong)0xffffffffffffffffL; public const ulong MinValue = 0x0; diff --git a/src/System.Private.CoreLib/shared/System/UIntPtr.cs b/src/System.Private.CoreLib/shared/System/UIntPtr.cs index 484eef4b61..9534f4f872 100644 --- a/src/System.Private.CoreLib/shared/System/UIntPtr.cs +++ b/src/System.Private.CoreLib/shared/System/UIntPtr.cs @@ -18,9 +18,9 @@ namespace System [Serializable] [CLSCompliant(false)] [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct UIntPtr : IEquatable, ISerializable + public readonly struct UIntPtr : IEquatable, ISerializable { - private unsafe void* _value; // Do not rename (binary serialization) + private readonly unsafe void* _value; // Do not rename (binary serialization) [Intrinsic] public static readonly UIntPtr Zero; -- cgit v1.2.3 From d1d26ac8fd6aabbfe655de03bf0ee3ecb2d096cd Mon Sep 17 00:00:00 2001 From: Phil Garcia Date: Tue, 26 Jun 2018 22:03:49 -0700 Subject: Applying PR feedback --- .../shared/Internal/Runtime/CompilerServices/Unsafe.cs | 11 +++++++++++ src/System.Private.CoreLib/shared/System/Double.cs | 2 +- src/System.Private.CoreLib/shared/System/Single.cs | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/System.Private.CoreLib/shared/Internal/Runtime/CompilerServices/Unsafe.cs b/src/System.Private.CoreLib/shared/Internal/Runtime/CompilerServices/Unsafe.cs index aeff3ce2ca..03d3f85211 100644 --- a/src/System.Private.CoreLib/shared/Internal/Runtime/CompilerServices/Unsafe.cs +++ b/src/System.Private.CoreLib/shared/Internal/Runtime/CompilerServices/Unsafe.cs @@ -357,6 +357,17 @@ namespace Internal.Runtime.CompilerServices return ref Unsafe.As(ref *(byte*)source); } + /// + /// Reinterprets the given location as a reference to a value of type . + /// + [Intrinsic] + [NonVersionable] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref T AsRef(in T source) + { + throw new PlatformNotSupportedException(); + } + /// /// Determines the byte offset from origin to target from the given references. /// diff --git a/src/System.Private.CoreLib/shared/System/Double.cs b/src/System.Private.CoreLib/shared/System/Double.cs index 308dda5fad..69da2e34b7 100644 --- a/src/System.Private.CoreLib/shared/System/Double.cs +++ b/src/System.Private.CoreLib/shared/System/Double.cs @@ -226,7 +226,7 @@ namespace System [MethodImpl(MethodImplOptions.AggressiveInlining)] // 64-bit constants make the IL unusually large that makes the inliner to reject the method public override int GetHashCode() { - var bits = BitConverter.DoubleToInt64Bits(m_value); + var bits = Unsafe.As(ref Unsafe.AsRef(in m_value)); // Optimized check for IsNan() || IsZero() if (((bits - 1) & 0x7FFFFFFFFFFFFFFF) >= 0x7FF0000000000000) diff --git a/src/System.Private.CoreLib/shared/System/Single.cs b/src/System.Private.CoreLib/shared/System/Single.cs index d62ff9ceea..7dddb029a8 100644 --- a/src/System.Private.CoreLib/shared/System/Single.cs +++ b/src/System.Private.CoreLib/shared/System/Single.cs @@ -217,7 +217,7 @@ namespace System public override int GetHashCode() { - var bits = BitConverter.SingleToInt32Bits(m_value); + var bits = Unsafe.As(ref Unsafe.AsRef(in m_value)); // Optimized check for IsNan() || IsZero() if (((bits - 1) & 0x7FFFFFFF) >= 0x7F800000) -- cgit v1.2.3 From 51cdf2c39d6064f6fbc7805f10f09149b0b85970 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 26 Jun 2018 22:38:42 -0700 Subject: Add VM support for Unsafe.AsRef(in T) --- src/vm/jitinterface.cpp | 3 ++- src/vm/metasig.h | 2 ++ src/vm/mscorlib.h | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/vm/jitinterface.cpp b/src/vm/jitinterface.cpp index ccd99df9ac..c532f201d4 100644 --- a/src/vm/jitinterface.cpp +++ b/src/vm/jitinterface.cpp @@ -7047,7 +7047,8 @@ bool getILIntrinsicImplementationForUnsafe(MethodDesc * ftn, } else if (tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__BYREF_AS)->GetMemberDef() || tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__OBJECT_AS)->GetMemberDef() || - tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__AS_REF)->GetMemberDef()) + tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__AS_REF_POINTER)->GetMemberDef() || + tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__AS_REF_IN)->GetMemberDef()) { // Return the argument that was passed in. static const BYTE ilcode[] = { CEE_LDARG_0, CEE_RET }; diff --git a/src/vm/metasig.h b/src/vm/metasig.h index 49e26f17e9..b3a4139a7c 100644 --- a/src/vm/metasig.h +++ b/src/vm/metasig.h @@ -290,6 +290,8 @@ DEFINE_METASIG(GM(RefByte_T_RetVoid, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(b) M(0) DEFINE_METASIG(GM(PtrVoid_RetT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, P(v), M(0))) DEFINE_METASIG(GM(PtrVoid_T_RetVoid, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, P(v) M(0), v)) +DEFINE_METASIG(GM(RefT_RetRefT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(M(0)), r(M(0)))) +DEFINE_METASIG(GM(VoidPtr_RetRefT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, P(v), r(M(0)))) DEFINE_METASIG(GM(RefTFrom_RetRefTTo, IMAGE_CEE_CS_CALLCONV_DEFAULT, 2, r(M(0)), r(M(1)))) DEFINE_METASIG(GM(Obj_RetT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, j, M(0))) DEFINE_METASIG(GM(RefT_Int_RetRefT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(M(0)) i, r(M(0)))) diff --git a/src/vm/mscorlib.h b/src/vm/mscorlib.h index c9715e1396..c57290ff12 100644 --- a/src/vm/mscorlib.h +++ b/src/vm/mscorlib.h @@ -775,7 +775,8 @@ DEFINE_METHOD(JIT_HELPERS, GET_RAW_SZ_ARRAY_DATA, GetRawSzArrayData, N DEFINE_CLASS(UNSAFE, InternalCompilerServices, Unsafe) DEFINE_METHOD(UNSAFE, AS_POINTER, AsPointer, NoSig) -DEFINE_METHOD(UNSAFE, AS_REF, AsRef, NoSig) +DEFINE_METHOD(UNSAFE, AS_REF_IN, AsRef, GM_RefT_RetRefT) +DEFINE_METHOD(UNSAFE, AS_REF_POINTER, AsRef, GM_VoidPtr_RetRefT) DEFINE_METHOD(UNSAFE, SIZEOF, SizeOf, NoSig) DEFINE_METHOD(UNSAFE, BYREF_AS, As, GM_RefTFrom_RetRefTTo) DEFINE_METHOD(UNSAFE, OBJECT_AS, As, GM_Obj_RetT) -- cgit v1.2.3