using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System.Reflection.Emit { /// /// A that provides extension methods for the type /// internal static class ILGeneratorExtensions { /// /// Puts the appropriate or instruction to unbox/cast a value onto the stream of instructions /// /// The input instance to use to emit instructions /// The type of value to convert public static void EmitCastOrUnbox(this ILGenerator il, Type type) { il.Emit(type.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, type); } /// /// Puts the appropriate instruction to read a local variable onto the stream of instructions /// /// The input instance to use to emit instructions /// The index of the local variable to load public static void EmitLoadLocal(this ILGenerator il, int index) { if (index <= 3) { il.Emit(index switch { 0 => OpCodes.Ldloc_0, 1 => OpCodes.Ldloc_1, 2 => OpCodes.Ldloc_2, 3 => OpCodes.Ldloc_3, _ => throw new InvalidOperationException($"Invalid local variable index [{index}]") }); } else if (index <= 255) il.Emit(OpCodes.Ldloc_S, (byte)index); else if (index <= 65534) il.Emit(OpCodes.Ldloc, (short)index); else throw new ArgumentOutOfRangeException($"Invalid local index {index}"); } /// /// Puts the appropriate instruction to write a local variable onto the stream of instructions /// /// The input instance to use to emit instructions /// The index of the local variable to store public static void EmitStoreLocal(this ILGenerator il, int index) { if (index <= 3) { il.Emit(index switch { 0 => OpCodes.Stloc_0, 1 => OpCodes.Stloc_1, 2 => OpCodes.Stloc_2, 3 => OpCodes.Stloc_3, _ => throw new InvalidOperationException($"Invalid local variable index [{index}]") }); } else if (index <= 255) il.Emit(OpCodes.Stloc_S, (byte)index); else if (index <= 65534) il.Emit(OpCodes.Stloc, (short)index); else throw new ArgumentOutOfRangeException($"Invalid local index {index}"); } /// /// Puts the appropriate , , or instruction to read a member onte the stream of instructions /// /// The input instance to use to emit instructions /// The member to read public static void EmitReadMember(this ILGenerator il, MemberInfo member) { switch (member) { case FieldInfo field: il.Emit(field.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, field); break; case PropertyInfo property when property.CanRead: il.EmitCall(property.GetMethod.IsStatic ? OpCodes.Call : OpCodes.Callvirt, property.GetMethod, null); break; default: throw new ArgumentException($"The input {member.GetType()} instance can't be read"); } } /// /// 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 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 < -128) il.Emit(OpCodes.Ldc_I4, offset); else if (offset < 0) il.Emit(OpCodes.Ldc_I4_S, (sbyte)offset); else if (offset <= 8) { il.Emit(offset switch { 0 => OpCodes.Ldc_I4_0, 1 => OpCodes.Ldc_I4_1, 2 => OpCodes.Ldc_I4_2, 3 => OpCodes.Ldc_I4_3, 4 => OpCodes.Ldc_I4_4, 5 => OpCodes.Ldc_I4_5, 6 => OpCodes.Ldc_I4_6, 7 => OpCodes.Ldc_I4_7, 8 => OpCodes.Ldc_I4_8, _ => throw new InvalidOperationException($"Invalid offset value [{offset}]") }); } else if (offset <= 127) il.Emit(OpCodes.Ldc_I4_S, (byte)offset); else il.Emit(OpCodes.Ldc_I4, offset); // Converts the offset to native int and adds to the address il.Emit(OpCodes.Conv_I); il.Emit(OpCodes.Add); } /// /// Puts the appropriate or instruction to write to a reference onto the stream of instructions /// /// The input instance to use to emit instructions /// 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) { if (type.IsValueType) { // Pick the optimal opcode to set a value type OpCode opcode = Marshal.SizeOf(type) switch { // Use the faster op codes for sizes <= 8 1 when type == typeof(bool) || type == typeof(byte) || type == typeof(sbyte) => OpCodes.Stind_I1, 2 when type == typeof(short) || type == typeof(ushort) => OpCodes.Stind_I2, 4 when type == typeof(float) => OpCodes.Stind_R4, 4 when type == typeof(int) || type == typeof(uint) => 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 }; // Also pass the type token if stobj is used if (opcode == OpCodes.Stobj) il.Emit(opcode, type); else il.Emit(opcode); } else il.Emit(OpCodes.Stind_Ref); } } }