From 3cd3814df7a85b1e1f7b8967f39d7a4174d1e0ed Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Sep 2019 22:06:28 +0200 Subject: [PATCH] Code refactoring to the ILGenerator extensions --- .../Extensions/ILGeneratorExtensions.cs | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs index afedebb6..31f9094a 100644 --- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace System.Reflection.Emit { @@ -85,9 +86,17 @@ namespace System.Reflection.Emit /// /// Puts the appropriate , and instructions to advance a reference onto the stream of instructions /// + /// The type of reference at the top of the stack /// The input instance to use to emit instructions /// The offset to use to advance the current reference on top of the execution stack - public static void EmitAddByteOffset(this ILGenerator il, int offset) + public static void EmitAddOffset(this ILGenerator il, int offset) => il.EmitAddOffset(Unsafe.SizeOf() * offset); + + /// + /// Puts the appropriate , and instructions to advance a reference onto the stream of instructions + /// + /// The input instance to use to emit instructions + /// The offset in bytes to use to advance the current reference on top of the execution stack + public static void EmitAddOffset(this ILGenerator il, int offset) { // Push the offset to the stack if (offset > 8) il.Emit(OpCodes.Ldc_I4, offset); @@ -115,19 +124,23 @@ namespace System.Reflection.Emit /// The type of value being written to the current reference on top of the execution stack public static void EmitStoreToAddress(this ILGenerator il, Type type) { - il.Emit(Marshal.SizeOf(type) switch + if (type.IsValueType) { - // Use the faster op codes for sizes <= 8 - 1 => OpCodes.Stind_I1, - 2 => OpCodes.Stind_I2, - 4 when type == typeof(float) => OpCodes.Stind_R4, - 4 => OpCodes.Stind_I4, - 8 when type == typeof(double) => OpCodes.Stind_R8, - 8 when type == typeof(long) || type == typeof(ulong) => OpCodes.Stind_I8, + il.Emit(Marshal.SizeOf(type) switch + { + // Use the faster op codes for sizes <= 8 + 1 => OpCodes.Stind_I1, + 2 => OpCodes.Stind_I2, + 4 when type == typeof(float) => OpCodes.Stind_R4, + 4 => OpCodes.Stind_I4, + 8 when type == typeof(double) => OpCodes.Stind_R8, + 8 when type == typeof(long) || type == typeof(ulong) => OpCodes.Stind_I8, - // Default to stobj for all other value types - _ => OpCodes.Stobj - }); + // Default to stobj for all other value types + _ => OpCodes.Stobj + }); + } + else il.Emit(OpCodes.Stind_Ref); } } }