Browse Source

Better check if the call can be replaced. Don't clone arguments.

pull/903/head
mohe2015 8 years ago
parent
commit
5b3adbd4fd
  1. 79
      ICSharpCode.Decompiler/IL/Transforms/ProxyCallReplacer.cs

79
ICSharpCode.Decompiler/IL/Transforms/ProxyCallReplacer.cs

@ -52,13 +52,33 @@ namespace ICSharpCode.Decompiler.IL.Transforms
transform.Run(function, context); transform.Run(function, context);
} }
} }
Call currentCall = new ProxyMethodVisitor().GetCalledMethod(function, context);
if (currentCall != null) {
Call newInst = (Call)currentCall.Clone();
if (function.Children.Count != 1)
return;
var blockContainer = function.Children[0];
if (blockContainer.OpCode != OpCode.BlockContainer)
return;
if (blockContainer.Children.Count != 1)
return;
var block = blockContainer.Children[0];
if (block.OpCode != OpCode.Block)
return;
if (block.Children.Count > 2)
return;
if (block.Children.Count == 2 && block.Children[1].OpCode != OpCode.Nop)
return;
var leave = block.Children[0];
if (leave.OpCode != OpCode.Leave)
return;
if (leave.Children.Count != 1)
return;
Call call = leave.Children[0] as Call;
if (call == null)
return;
// check if original arguments are only correct ldloc calls // check if original arguments are only correct ldloc calls
for (int i = 0; i < currentCall.Arguments.Count; i++) { for (int i = 0; i < call.Arguments.Count; i++) {
var originalArg = currentCall.Arguments.ElementAtOrDefault(i); var originalArg = call.Arguments[i];
if (originalArg.OpCode != OpCode.LdLoc || if (originalArg.OpCode != OpCode.LdLoc ||
originalArg.Children.Count != 0 || originalArg.Children.Count != 0 ||
((LdLoc)originalArg).Variable.Kind != VariableKind.Parameter || ((LdLoc)originalArg).Variable.Kind != VariableKind.Parameter ||
@ -66,9 +86,12 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return; return;
} }
} }
Call newInst = (Call)call.Clone();
newInst.Arguments.Clear(); newInst.Arguments.Clear();
ILInstruction thisArg = inst.Arguments.ElementAtOrDefault(0).Clone(); ILInstruction thisArg = inst.Arguments[0];
// special handling for first argument (this) - the underlying issue may be somewhere else // special handling for first argument (this) - the underlying issue may be somewhere else
// normally // normally
@ -85,53 +108,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// add everything except first argument // add everything except first argument
for (int i = 1; i < inst.Arguments.Count; i++) { for (int i = 1; i < inst.Arguments.Count; i++) {
newInst.Arguments.Add(inst.Arguments.ElementAtOrDefault(i).Clone()); newInst.Arguments.Add(inst.Arguments[i]);
} }
inst.ReplaceWith(newInst); inst.ReplaceWith(newInst);
} }
} }
} }
} }
// Checks if the method is a proxy method by checking if it only contains a call instruction and if it has special compiler attributes.
private class ProxyMethodVisitor : ILVisitor
{
ILTransformContext context;
int invalidInstructions = 0;
Call currentCall;
public Call GetCalledMethod(ILFunction function, ILTransformContext context)
{
this.context = context;
function.AcceptVisitor(this);
if (invalidInstructions == 0) {
return currentCall;
}
return null;
}
protected override void Default(ILInstruction inst)
{
if (inst.OpCode != OpCode.ILFunction &&
inst.OpCode != OpCode.BlockContainer &&
inst.OpCode != OpCode.Block &&
inst.OpCode != OpCode.Leave &&
inst.OpCode != OpCode.Nop) {
invalidInstructions++;
}
foreach (var child in inst.Children) {
child.AcceptVisitor(this);
}
}
protected internal override void VisitCall(Call inst)
{
if (currentCall == null) {
currentCall = inst;
} else {
invalidInstructions++; // more than one call in the function
}
}
}
}
} }

Loading…
Cancel
Save