Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions src/DelegateDecompiler/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ static int ProcessInstruction(ProcessorState state, Instruction instruction)
static LambdaExpression DecompileLambdaExpression(MethodInfo method, Func<Expression> @this)
{
if (method.IsStatic)
{
return AnonymousDelegatesCache.GetOrAdd(method, m => m.Decompile());
}

//Should always call.
var expression = @this();
Expand Down Expand Up @@ -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());
Expand Down
20 changes: 19 additions & 1 deletion src/DelegateDecompiler/Processors/BoxProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection.Emit;
using Mono.Reflection;

Expand All @@ -14,6 +15,23 @@ public static void Register(Dictionary<OpCode, IProcessor> 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;
}
}
23 changes: 23 additions & 0 deletions src/DelegateDecompiler/Processors/LdlocConstantProcessor.cs
Original file line number Diff line number Diff line change
@@ -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<OpCode, IProcessor> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using Mono.Reflection;

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<OpCode, IProcessor> 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<OpCode, IProcessor> processors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public static void Register(Dictionary<OpCode, IProcessor> 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);
}
}