Added IL extensions to load and store local variables

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-18 20:28:49 +02:00
1 親 a18baed6fe
コミット c58b63a254
1ファイル変更
+46
+46
ファイルの表示
@@ -17,6 +17,52 @@ namespace System.Reflection.Emit
il.Emit(type.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, type);
}
/// <summary>
/// Puts the appropriate <see langword="ldloc"/> instruction to read a local variable onto the stream of instructions
/// </summary>
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions</param>
/// <param name="index">The index of the local variable to load</param>
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("Huh?")
});
}
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}");
}
/// <summary>
/// Puts the appropriate <see langword="stloc"/> instruction to write a local variable onto the stream of instructions
/// </summary>
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions</param>
/// <param name="index">The index of the local variable to store</param>
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("Huh?")
});
}
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}");
}
/// <summary>
/// Puts the appropriate <see langword="ldsfld"/>, <see langword="ldfld"/>, <see langword="call"/> or <see langword="callvirt"/> instruction to read a member onte the stream of instructions
/// </summary>