Removed leftover IL generation code
このコミットが含まれているのは:
1 親
0133009c12
コミット
0c553d5b09
5ファイル変更
-215
検索条件に一致するファイルはありません
@@ -1,107 +0,0 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
namespace ComputeSharp.Shaders.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that provides extension methods for the <see langword="ILGenerator"/> type.
|
||||
/// </summary>
|
||||
internal static class ILGeneratorExtensions
|
||||
{
|
||||
/// <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>
|
||||
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions.</param>
|
||||
/// <param name="member">The member to read.</param>
|
||||
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: ThrowHelper.ThrowArgumentException("The input MemberInfo instance can't be read"); return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts the appropriate <see langword="ldc.i4"/>, <see langword="conv.i"/> and <see langword="add"/> instructions to advance a reference onto the stream of instructions.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of reference at the top of the stack.</typeparam>
|
||||
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions.</param>
|
||||
/// <param name="offset">The offset to use to advance the current reference on top of the execution stack.</param>
|
||||
public static void EmitAddOffset<T>(this ILGenerator il, int offset) => il.EmitAddOffset(Unsafe.SizeOf<T>() * offset);
|
||||
|
||||
/// <summary>
|
||||
/// Puts the appropriate <see langword="ldc.i4"/>, <see langword="conv.i"/> and <see langword="add"/> instructions to advance a reference onto the stream of instructions.
|
||||
/// </summary>
|
||||
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions.</param>
|
||||
/// <param name="offset">The offset in bytes to use to advance the current reference on top of the execution stack.</param>
|
||||
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,
|
||||
_ => ThrowHelper.ThrowArgumentException<OpCode>("Invalid offset value")
|
||||
});
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts the appropriate <see langword="stind"/> or <see langword="stobj"/> instruction to write to a reference onto the stream of instructions.
|
||||
/// </summary>
|
||||
/// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions.</param>
|
||||
/// <param name="type">The type of value being written to the current reference on top of the execution stack.</param>
|
||||
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(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace ComputeSharp.Shaders.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that provides extension methods for the <see cref="Type"/> type.
|
||||
/// </summary>
|
||||
internal static class TypeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether or not the input <see cref="Type"/> represents a <see cref="Delegate"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The input type to analyze.</param>
|
||||
[Pure]
|
||||
public static bool IsDelegate(this Type type)
|
||||
{
|
||||
return type.IsSubclassOf(typeof(Delegate));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
|
||||
namespace System.Reflection.Emit
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that can be used to easily create dynamic methods.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="Delegate"/> that will be used to wrap the new methods.</typeparam>
|
||||
internal static class DynamicMethod<T>
|
||||
where T : Delegate
|
||||
{
|
||||
/// <summary>
|
||||
/// The return type of the <typeparamref name="T"/> <see langword="delegate"/>.
|
||||
/// </summary>
|
||||
private static readonly Type ReturnType;
|
||||
|
||||
/// <summary>
|
||||
/// The types of the arguments of the <typeparamref name="T"/> <see langword="delegate"/>.
|
||||
/// </summary>
|
||||
private static readonly Type[] ParameterTypes;
|
||||
|
||||
/// <summary>
|
||||
/// Loads all the necessary <see cref="Type"/> info for the current <typeparamref name="T"/> parameter.
|
||||
/// </summary>
|
||||
static DynamicMethod()
|
||||
{
|
||||
MethodInfo method = typeof(T).GetMethod("Invoke")!;
|
||||
|
||||
ReturnType = method.ReturnType;
|
||||
ParameterTypes = new[] { typeof(object) }.Concat(method.GetParameters().Select(static p => p.ParameterType)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <typeparamref name="T"/> <see langword="delegate"/> for the target owner. For performance reasons,
|
||||
/// the underlying dynamic method will be created with an extra <see cref="object"/> parameter, and then bound to
|
||||
/// a <see langword="null"/> instance. This is to emulate the behavior of instance methods, which unlike static ones
|
||||
/// don't need to go through the shuffle thunk when the delegate is invoked. This would be needed to redirect the
|
||||
/// invocation to a stub that performs parameters swapping, as the Invoke method is an instance one with the parameters
|
||||
/// starting from offset 1, while the target static method would expect them to start from offset 0. This solution
|
||||
/// allows the delegate created for the dynamic method to avoid having a shuffle thunk at all, and with it only having
|
||||
/// to replace the hidden instance argument when being invoked, resulting in much better performance. Because of this,
|
||||
/// it is expected that <paramref name="builder"/> will emit the bytecode assuming that there is an extra unused argument
|
||||
/// at offset 0. As a result of this, all argument load and store opcodes will need to be shifted by one position forwards.
|
||||
/// </summary>
|
||||
/// <param name="builder">An <see cref="Action"/> that builds the IL bytecode for the new method.</param>
|
||||
/// <returns>A new dynamic method wrapped as a <typeparamref name="T"/> <see langword="delegate"/>.</returns>
|
||||
[Pure]
|
||||
public static T New(Action<ILGenerator> builder)
|
||||
{
|
||||
DynamicMethod method = new(string.Empty, ReturnType, ParameterTypes, true);
|
||||
|
||||
builder(method.GetILGenerator());
|
||||
|
||||
return method.CreateDelegate<T>(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
差分なしでファイル名を変更。
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using System.Reflection.Emit;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests.Internals
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("DynamicMethod")]
|
||||
public class DynamicMethodTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestFunc()
|
||||
{
|
||||
Func<int, int> square = DynamicMethod<Func<int, int>>.New(il =>
|
||||
{
|
||||
// Due to the instance method trick, the first parameter is at offset 1.
|
||||
// See the docs on DynamicMethod<T>.New for more info on how this works.
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.Emit(OpCodes.Mul);
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
|
||||
int result = square(2); // result = 2 * 2
|
||||
|
||||
Assert.IsTrue(result == 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする