From d8f398d42cdcec8a250e7f36b3e5009e666e77a6 Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Mon, 6 Oct 2025 12:09:22 +1000 Subject: [PATCH 1/2] Move some helper methods to processors where they are used --- src/DelegateDecompiler/Processor.cs | 29 +------------------ .../Processors/BoxProcessor.cs | 20 ++++++++++++- .../Processors/UnaryExpressionProcessor.cs | 9 +++++- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/DelegateDecompiler/Processor.cs b/src/DelegateDecompiler/Processor.cs index 21d2fbc..2f1ec13 100644 --- a/src/DelegateDecompiler/Processor.cs +++ b/src/DelegateDecompiler/Processor.cs @@ -225,9 +225,7 @@ static int ProcessInstruction(ProcessorState state, Instruction instruction) static LambdaExpression DecompileLambdaExpression(MethodInfo method, Func @this) { if (method.IsStatic) - { return AnonymousDelegatesCache.GetOrAdd(method, m => m.Decompile()); - } //Should always call. var expression = @this(); @@ -284,32 +282,7 @@ internal static BinaryExpression MakeBinaryExpression(Address left, Address righ return Expression.MakeBinary(expressionType, left, right); } - internal static UnaryExpression MakeUnaryExpression(Expression operand, ExpressionType expressionType) - { - operand = ConvertEnumExpressionToUnderlyingType(operand); - - return Expression.MakeUnary(expressionType, operand, operand.Type); - } - - internal static Expression Box(Expression expression, Type type) - { - if (expression.Type == type) - return expression; - - var constantExpression = expression as ConstantExpression; - if (constantExpression != null) - { - if (type.IsEnum) - return Expression.Constant(Enum.ToObject(type, constantExpression.Value)); - } - - if (expression.Type.IsEnum) - return Expression.Convert(expression, type); - - return expression; - } - - static Expression ConvertEnumExpressionToUnderlyingType(Expression expression) + internal static Expression ConvertEnumExpressionToUnderlyingType(Expression expression) { if (expression.Type.IsEnum) return Expression.Convert(expression, expression.Type.GetEnumUnderlyingType()); diff --git a/src/DelegateDecompiler/Processors/BoxProcessor.cs b/src/DelegateDecompiler/Processors/BoxProcessor.cs index db06503..fcc1db1 100644 --- a/src/DelegateDecompiler/Processors/BoxProcessor.cs +++ b/src/DelegateDecompiler/Processors/BoxProcessor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq.Expressions; using System.Reflection.Emit; using Mono.Reflection; @@ -14,6 +15,23 @@ public static void Register(Dictionary processors) public void Process(ProcessorState state, Instruction instruction) { - state.Stack.Push(Processor.Box(state.Stack.Pop(), (Type)instruction.Operand)); + state.Stack.Push(Box(state.Stack.Pop(), (Type)instruction.Operand)); + } + + static Expression Box(Expression expression, Type type) + { + if (expression.Type == type) + return expression; + + if (expression is ConstantExpression constantExpression) + { + if (type.IsEnum) + return Expression.Constant(Enum.ToObject(type, constantExpression.Value)); + } + + if (expression.Type.IsEnum) + return Expression.Convert(expression, type); + + return expression; } } \ No newline at end of file diff --git a/src/DelegateDecompiler/Processors/UnaryExpressionProcessor.cs b/src/DelegateDecompiler/Processors/UnaryExpressionProcessor.cs index 3ed7c1e..4d6d274 100644 --- a/src/DelegateDecompiler/Processors/UnaryExpressionProcessor.cs +++ b/src/DelegateDecompiler/Processors/UnaryExpressionProcessor.cs @@ -16,6 +16,13 @@ public static void Register(Dictionary processors) public void Process(ProcessorState state, Instruction instruction) { var val = state.Stack.Pop(); - state.Stack.Push(Processor.MakeUnaryExpression(val, expressionType)); + state.Stack.Push(MakeUnaryExpression(val, expressionType)); + } + + static UnaryExpression MakeUnaryExpression(Expression operand, ExpressionType expressionType) + { + operand = Processor.ConvertEnumExpressionToUnderlyingType(operand); + + return Expression.MakeUnary(expressionType, operand, operand.Type); } } \ No newline at end of file From 1ba0b23e59976cccca449f2af3f84e64ed4b8a8c Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Mon, 6 Oct 2025 12:11:55 +1000 Subject: [PATCH 2/2] Split file --- .../Processors/LdlocConstantProcessor.cs | 23 +++++++++++++++++++ ...Processor.cs => LdlocVariableProcessor.cs} | 20 ---------------- 2 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 src/DelegateDecompiler/Processors/LdlocConstantProcessor.cs rename src/DelegateDecompiler/Processors/{LdlocProcessor.cs => LdlocVariableProcessor.cs} (50%) diff --git a/src/DelegateDecompiler/Processors/LdlocConstantProcessor.cs b/src/DelegateDecompiler/Processors/LdlocConstantProcessor.cs new file mode 100644 index 0000000..66a42d8 --- /dev/null +++ b/src/DelegateDecompiler/Processors/LdlocConstantProcessor.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Reflection.Emit; +using Mono.Reflection; + +namespace DelegateDecompiler.Processors +{ + internal class LdlocConstantProcessor(int index) : IProcessor + { + public static void Register(Dictionary processors) + { + processors.Register(new LdlocConstantProcessor(0), OpCodes.Ldloc_0); + processors.Register(new LdlocConstantProcessor(1), OpCodes.Ldloc_1); + processors.Register(new LdlocConstantProcessor(2), OpCodes.Ldloc_2); + processors.Register(new LdlocConstantProcessor(3), OpCodes.Ldloc_3); + } + + public void Process(ProcessorState state, Instruction instruction) + { + var local = state.Locals[index]; + state.Stack.Push(local.Address); + } + } +} diff --git a/src/DelegateDecompiler/Processors/LdlocProcessor.cs b/src/DelegateDecompiler/Processors/LdlocVariableProcessor.cs similarity index 50% rename from src/DelegateDecompiler/Processors/LdlocProcessor.cs rename to src/DelegateDecompiler/Processors/LdlocVariableProcessor.cs index 41b1306..8564814 100644 --- a/src/DelegateDecompiler/Processors/LdlocProcessor.cs +++ b/src/DelegateDecompiler/Processors/LdlocVariableProcessor.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; @@ -6,25 +5,6 @@ namespace DelegateDecompiler.Processors; -// Processor for constant local variable indices (ldloc.0, ldloc.1, etc.) -internal class LdlocConstantProcessor(int index) : IProcessor -{ - public static void Register(Dictionary processors) - { - processors.Register(new LdlocConstantProcessor(0), OpCodes.Ldloc_0); - processors.Register(new LdlocConstantProcessor(1), OpCodes.Ldloc_1); - processors.Register(new LdlocConstantProcessor(2), OpCodes.Ldloc_2); - processors.Register(new LdlocConstantProcessor(3), OpCodes.Ldloc_3); - } - - public void Process(ProcessorState state, Instruction instruction) - { - var local = state.Locals[index]; - state.Stack.Push(local.Address); - } -} - -// Processor for dynamic local variable index resolution (ldloc, ldloc.s, ldloca, ldloca.s) internal class LdlocVariableProcessor : IProcessor { public static void Register(Dictionary processors)