diff --git a/ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs b/ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs index 2b737ac94..288738040 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/ReplaceMethodCallsWithOperators.cs @@ -235,9 +235,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms static bool IsWithoutSideEffects(Expression left) { - if (left is ThisReferenceExpression) - return true; - if (left is IdentifierExpression) + if (left is ThisReferenceExpression || left is IdentifierExpression || left is TypeReferenceExpression) return true; MemberReferenceExpression mre = left as MemberReferenceExpression; if (mre != null) diff --git a/ICSharpCode.Decompiler/ILAst/PatternMatching.cs b/ICSharpCode.Decompiler/ILAst/PatternMatching.cs index 3921121e3..d2a6fd48e 100644 --- a/ICSharpCode.Decompiler/ILAst/PatternMatching.cs +++ b/ICSharpCode.Decompiler/ILAst/PatternMatching.cs @@ -28,16 +28,6 @@ namespace ICSharpCode.Decompiler.ILAst return false; } - public static bool Match(this ILNode node, ILCode code, T operand) - { - ILExpression expr = node as ILExpression; - if (expr != null && expr.Prefixes == null && expr.Code == code) { - Debug.Assert(expr.Arguments.Count == 0); - return operand.Equals(expr.Operand); - } - return false; - } - public static bool Match(this ILNode node, ILCode code, out List args) { ILExpression expr = node as ILExpression; diff --git a/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs b/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs index 4f1f898b4..f8b918c43 100644 --- a/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs +++ b/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs @@ -190,16 +190,14 @@ namespace ICSharpCode.Decompiler.ILAst // stloc(v, exprVar) // -> // exprVar = stloc(v, ...)) - ILExpression nextExpr = body.ElementAtOrDefault(pos + 1) as ILExpression; ILVariable exprVar; ILExpression initializer; + if (!(expr.Match(ILCode.Stloc, out exprVar, out initializer) && exprVar.IsGenerated)) + return false; + ILExpression nextExpr = body.ElementAtOrDefault(pos + 1) as ILExpression; ILVariable v; ILExpression stLocArg; - if (expr.Match(ILCode.Stloc, out exprVar, out initializer) && - exprVar.IsGenerated && - nextExpr.Match(ILCode.Stloc, out v, out stLocArg) && - stLocArg.Match(ILCode.Ldloc, exprVar)) - { + if (nextExpr.Match(ILCode.Stloc, out v, out stLocArg) && stLocArg.MatchLdloc(exprVar)) { ILExpression store2 = body.ElementAtOrDefault(pos + 2) as ILExpression; if (StoreCanBeConvertedToAssignment(store2, exprVar)) { // expr_44 = ... @@ -224,6 +222,18 @@ namespace ICSharpCode.Decompiler.ILAst nextExpr.Arguments[0] = initializer; ((ILExpression)body[pos]).Arguments[0] = nextExpr; return true; + } else { + // exprVar = ... + // stsfld(fld, exprVar) + // -> + // exprVar = stsfld(fld, ...)) + FieldReference field; + if (nextExpr.Match(ILCode.Stsfld, out field, out stLocArg) && stLocArg.MatchLdloc(exprVar)) { + body.RemoveAt(pos + 1); // remove stfld + nextExpr.Arguments[0] = initializer; + ((ILExpression)body[pos]).Arguments[0] = nextExpr; + return true; + } } return false; } diff --git a/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs b/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs index d1cbe720c..de85c7acc 100644 --- a/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs +++ b/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs @@ -152,7 +152,7 @@ namespace ICSharpCode.Decompiler.ILAst head.Body[head.Body.Count - 3].Match(ILCode.Stloc, out v, out leftExpr) && leftExpr.Match(ILCode.Ldloc, out leftVar) && head.MatchLastAndBr(ILCode.Brtrue, out endBBLabel, out leftExpr2, out rightBBLabel) && - leftExpr2.Match(ILCode.Ldloc, leftVar) && + leftExpr2.MatchLdloc(leftVar) && labelToBasicBlock.TryGetValue(rightBBLabel, out rightBB) && rightBB.MatchSingleAndBr(ILCode.Stloc, out v2, out rightExpr, out endBBLabel2) && v == v2 && diff --git a/ICSharpCode.Decompiler/Tests/IncrementDecrement.cs b/ICSharpCode.Decompiler/Tests/IncrementDecrement.cs index 6836d1bdc..95a175510 100644 --- a/ICSharpCode.Decompiler/Tests/IncrementDecrement.cs +++ b/ICSharpCode.Decompiler/Tests/IncrementDecrement.cs @@ -10,6 +10,8 @@ public class IncrementDecrement public int Field; } + public static int StaticField; + private IncrementDecrement.MutableClass M() { return new IncrementDecrement.MutableClass(); @@ -35,11 +37,21 @@ public class IncrementDecrement return ++m.Field; } + public int PreIncrementStaticField() + { + return ++IncrementDecrement.StaticField; + } + public int CompoundMultiplyInstanceField() { return this.M().Field *= 10; } + public int CompoundXorStaticField() + { + return IncrementDecrement.StaticField ^= 100; + } + public int CompoundMultiplyArrayElement1(int[] array, int pos) { return array[pos] *= 10;