Code refactoring using the new DynamicMethod<T> type
このコミットが含まれているのは:
1 親
2b310aba25
コミット
52ba3183e6
3ファイル変更
+86
-75
検索条件に一致するファイルはありません
@@ -4,6 +4,7 @@ using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
@@ -79,39 +80,35 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
[Pure]
|
||||
private Getter BuildDynamicGetter()
|
||||
{
|
||||
// Create a new dynamic method for the current member
|
||||
DynamicMethod method = new DynamicMethod($"Get{Name}", typeof(object), new[] { typeof(object) }, DeclaringType);
|
||||
ILGenerator il = method.GetILGenerator();
|
||||
|
||||
// Info for hierarchical access
|
||||
ReadableMember[] hierarchy = (Parents ?? Array.Empty<ReadableMember>()).Concat(new[] { this }).ToArray();
|
||||
|
||||
// Load the argument (the object instance) and cast it to the right type, if needed
|
||||
if (!IsStatic)
|
||||
return DynamicMethod<Getter>.New(il =>
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
// Info for hierarchical access
|
||||
ReadableMember[] hierarchy = (Parents ?? Array.Empty<ReadableMember>()).Concat(new[] { this }).ToArray();
|
||||
|
||||
Type unboxType = hierarchy[0].DeclaringType;
|
||||
il.Emit(unboxType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, unboxType);
|
||||
}
|
||||
// Load the argument (the object instance) and cast it to the right type, if needed
|
||||
if (!IsStatic)
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
|
||||
// Unroll the member access
|
||||
foreach (ReadableMember member in hierarchy)
|
||||
{
|
||||
// Get the member value with the appropriate method
|
||||
if (member.Property != null) il.EmitCall(member.IsStatic ? OpCodes.Call : OpCodes.Callvirt, member.Property.GetMethod, null);
|
||||
else if (member.Field != null) il.Emit(member.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, member.Field);
|
||||
else throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
}
|
||||
Type unboxType = hierarchy[0].DeclaringType;
|
||||
il.Emit(unboxType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, unboxType);
|
||||
}
|
||||
|
||||
// Box the value, if needed
|
||||
if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType);
|
||||
// Unroll the member access
|
||||
foreach (ReadableMember member in hierarchy)
|
||||
{
|
||||
// Get the member value with the appropriate method
|
||||
if (member.Property != null) il.EmitCall(member.IsStatic ? OpCodes.Call : OpCodes.Callvirt, member.Property.GetMethod, null);
|
||||
else if (member.Field != null) il.Emit(member.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, member.Field);
|
||||
else throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
}
|
||||
|
||||
// Return the member value from the top of the evaluation stack
|
||||
il.Emit(OpCodes.Ret);
|
||||
// Box the value, if needed
|
||||
if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType);
|
||||
|
||||
// Build and return the delegate
|
||||
return (Getter)method.CreateDelegate(typeof(Getter));
|
||||
// Return the member value from the top of the evaluation stack
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,58 +67,54 @@ namespace ComputeSharp.Shaders.Translation
|
||||
[Pure]
|
||||
private static Hasher BuildDynamicHasher(Type declaringType, IReadOnlyCollection<FieldInfo> fieldInfos)
|
||||
{
|
||||
// Create a new dynamic method for the current member
|
||||
DynamicMethod method = new DynamicMethod("GetHashCodeForHlslDelegates", typeof(int), new[] { typeof(int), typeof(object) }, declaringType);
|
||||
ILGenerator il = method.GetILGenerator();
|
||||
|
||||
// Declare the local variable to store the target instance
|
||||
il.DeclareLocal(declaringType);
|
||||
|
||||
// Load the argument (the object instance)
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Castclass, declaringType);
|
||||
il.Emit(OpCodes.Stloc_0);
|
||||
|
||||
// Unroll the member access
|
||||
MethodInfo
|
||||
validateDelegateInfo = typeof(ShaderHashCodeProvider).GetMethod(nameof(ValidateDelegate), BindingFlags.NonPublic | BindingFlags.Static),
|
||||
getMethodInfo = typeof(Delegate).GetProperty(nameof(Delegate.Method)).GetMethod,
|
||||
getHashCodeInfo = typeof(object).GetMethod(nameof(GetHashCode));
|
||||
foreach (FieldInfo fieldInfo in fieldInfos)
|
||||
return DynamicMethod<Hasher>.New(il =>
|
||||
{
|
||||
Label label = il.DefineLabel();
|
||||
// Declare the local variable to store the target instance
|
||||
il.DeclareLocal(declaringType);
|
||||
|
||||
// if (fieldInfo.GetValue(obj) != null) {
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
il.Emit(OpCodes.Brfalse_S, label);
|
||||
// Load the argument (the object instance)
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Castclass, declaringType);
|
||||
il.Emit(OpCodes.Stloc_0);
|
||||
|
||||
// if (ValidateDelegate(field)) {
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
il.EmitCall(OpCodes.Call, validateDelegateInfo, null);
|
||||
il.Emit(OpCodes.Brfalse_S, label);
|
||||
// Unroll the member access
|
||||
MethodInfo
|
||||
validateDelegateInfo = typeof(ShaderHashCodeProvider).GetMethod(nameof(ValidateDelegate), BindingFlags.NonPublic | BindingFlags.Static),
|
||||
getMethodInfo = typeof(Delegate).GetProperty(nameof(Delegate.Method)).GetMethod,
|
||||
getHashCodeInfo = typeof(object).GetMethod(nameof(GetHashCode));
|
||||
foreach (FieldInfo fieldInfo in fieldInfos)
|
||||
{
|
||||
Label label = il.DefineLabel();
|
||||
|
||||
// hash = hash * 17 + field.Method.GetHashCode();
|
||||
// if (fieldInfo.GetValue(obj) != null) {
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
il.Emit(OpCodes.Brfalse_S, label);
|
||||
|
||||
// if (ValidateDelegate(field)) {
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
il.EmitCall(OpCodes.Call, validateDelegateInfo, null);
|
||||
il.Emit(OpCodes.Brfalse_S, label);
|
||||
|
||||
// hash = hash * 17 + field.Method.GetHashCode();
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Ldc_I4, 17);
|
||||
il.Emit(OpCodes.Mul);
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
il.EmitCall(OpCodes.Callvirt, getMethodInfo, null);
|
||||
il.EmitCall(OpCodes.Callvirt, getHashCodeInfo, null);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.Emit(OpCodes.Starg_S, 0);
|
||||
|
||||
il.MarkLabel(label);
|
||||
}
|
||||
|
||||
// Return the computed hash
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Ldc_I4, 17);
|
||||
il.Emit(OpCodes.Mul);
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
il.EmitCall(OpCodes.Callvirt, getMethodInfo, null);
|
||||
il.EmitCall(OpCodes.Callvirt, getHashCodeInfo, null);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.Emit(OpCodes.Starg_S, 0);
|
||||
|
||||
il.MarkLabel(label);
|
||||
}
|
||||
|
||||
// Return the computed hash
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
// Build and return the delegate
|
||||
return (Hasher)method.CreateDelegate(typeof(Hasher));
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection.Emit;
|
||||
using System;
|
||||
using System.Reflection.Emit;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
@@ -8,8 +9,25 @@ namespace ComputeSharp.Tests.Internals
|
||||
[TestCategory("DynamicMethod")]
|
||||
public class DynamicMethodTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestFunc()
|
||||
{
|
||||
Func<int, int> square = DynamicMethod<Func<int, int>>.New(il =>
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.Emit(OpCodes.Mul);
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
|
||||
int result = square(2); // result = 2 * 2
|
||||
|
||||
Assert.IsTrue(result == 4);
|
||||
}
|
||||
|
||||
public delegate int Square(int x);
|
||||
|
||||
[TestMethod]
|
||||
public void TestSquare()
|
||||
{
|
||||
Square square = DynamicMethod<Square>.New(il =>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする