Browse Source

Convert simple IfInstructions to ConditionalExpression

pull/728/head
Siegfried Pammer 9 years ago
parent
commit
134d4610d6
  1. 11
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  2. 17
      ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

11
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1082,6 +1082,17 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1082,6 +1082,17 @@ namespace ICSharpCode.Decompiler.CSharp
return true;
}
protected internal override TranslatedExpression VisitIfInstruction(IfInstruction inst)
{
var condition = TranslateCondition(inst.Condition);
var targetType = compilation.FindType(inst.ResultType.ToKnownTypeCode());
var trueBranch = Translate(inst.TrueInst).ConvertTo(targetType, this);
var falseBranch = Translate(inst.FalseInst).ConvertTo(targetType, this);
return new ConditionalExpression(condition.Expression, trueBranch.Expression, falseBranch.Expression)
.WithILInstruction(inst)
.WithRR(new ResolveResult(trueBranch.Type));
}
protected internal override TranslatedExpression VisitInvalidInstruction(InvalidInstruction inst)
{
string message = "Invalid IL";

17
ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

@ -186,5 +186,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -186,5 +186,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms
inst.ReplaceWith(new StLoc(v, inst.Value));
}
}
protected internal override void VisitIfInstruction(IfInstruction inst)
{
base.VisitIfInstruction(inst);
// if (cond) stloc (A, V1) else stloc (A, V2) --> stloc (A, if (cond) V1 else V2)
Block trueInst = inst.TrueInst as Block;
if (trueInst == null || trueInst.Instructions.Count != 1)
return;
Block falseInst = inst.FalseInst as Block;
if (falseInst == null || falseInst.Instructions.Count != 1)
return;
ILVariable v1, v2;
ILInstruction value1, value2;
if (trueInst.Instructions[0].MatchStLoc(out v1, out value1) && falseInst.Instructions[0].MatchStLoc(out v2, out value2) && v1 == v2) {
inst.ReplaceWith(new StLoc(v1, new IfInstruction(inst.Condition, value1, value2)));
}
}
}
}

Loading…
Cancel
Save