From 5bc2692ab79bf787ba4a97a204df7b077277ff20 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Wed, 25 May 2011 04:54:46 +0300 Subject: [PATCH] Boolean logic decompilation improvement refactored to SimplifyTernaryOperator --- .../ILAst/PeepholeTransform.cs | 20 ------------------- .../ILAst/SimpleControlFlow.cs | 10 +++++----- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs b/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs index d735e1e89..3825012d0 100644 --- a/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs +++ b/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs @@ -911,26 +911,6 @@ namespace ICSharpCode.Decompiler.ILAst } } - if (expr.Code == ILCode.TernaryOp && TypeAnalysis.IsBoolean(expr.Arguments[2].InferredType) && (a = expr.Arguments[1]).Code == ILCode.Ldc_I4) - switch ((int)a.Operand) { - // "ternaryop(a, ldc.i4.0, b)" becomes "logicand(logicnot(a), b)" if the inferred type for expression "b" is boolean - case 0: - expr.Code = ILCode.LogicAnd; - a = new ILExpression(ILCode.LogicNot, null, expr.Arguments[0]) { ILRanges = a.ILRanges }; - if (!SimplifyLogicNotArgument(a)) expr.Arguments[0] = a; - goto common; - // "ternaryop(a, ldc.i4.1, b)" becomes "logicor(a, b)" if the inferred type for expression "b" is boolean - case 1: - expr.Code = ILCode.LogicOr; - expr.ILRanges.AddRange(a.ILRanges); - common: - expr.Arguments.RemoveAt(1); - expr.InferredType = expr.Arguments[1].InferredType; - res = expr; - modified = true; - break; - } - return res; } diff --git a/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs b/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs index f7f2cf8a3..e92e6abf8 100644 --- a/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs +++ b/ICSharpCode.Decompiler/ILAst/SimpleControlFlow.cs @@ -98,16 +98,16 @@ namespace ICSharpCode.Decompiler.ILAst if (leftBoolVal != 0) { newExpr = condExpr; } else { - newExpr = new ILExpression(ILCode.LogicNot, null, condExpr); + newExpr = new ILExpression(ILCode.LogicNot, null, condExpr) { InferredType = typeSystem.Boolean }; } - } else if (retTypeIsBoolean && trueExpr.Match(ILCode.Ldc_I4, out leftBoolVal)) { + } else if ((retTypeIsBoolean || TypeAnalysis.IsBoolean(falseExpr.InferredType)) && trueExpr.Match(ILCode.Ldc_I4, out leftBoolVal) && (leftBoolVal == 0 || leftBoolVal == 1)) { // It can be expressed as logical expression if (leftBoolVal != 0) { newExpr = MakeLeftAssociativeShortCircuit(ILCode.LogicOr, condExpr, falseExpr); } else { newExpr = MakeLeftAssociativeShortCircuit(ILCode.LogicAnd, new ILExpression(ILCode.LogicNot, null, condExpr), falseExpr); } - } else if (retTypeIsBoolean && falseExpr.Match(ILCode.Ldc_I4, out rightBoolVal)) { + } else if ((retTypeIsBoolean || TypeAnalysis.IsBoolean(trueExpr.InferredType)) && falseExpr.Match(ILCode.Ldc_I4, out rightBoolVal) && (rightBoolVal == 0 || rightBoolVal == 1)) { // It can be expressed as logical expression if (rightBoolVal != 0) { newExpr = MakeLeftAssociativeShortCircuit(ILCode.LogicOr, new ILExpression(ILCode.LogicNot, null, condExpr), trueExpr); @@ -343,10 +343,10 @@ namespace ICSharpCode.Decompiler.ILAst ILExpression current = right; while(current.Arguments[0].Match(code)) current = current.Arguments[0]; - current.Arguments[0] = new ILExpression(code, null, left, current.Arguments[0]); + current.Arguments[0] = new ILExpression(code, null, left, current.Arguments[0]) { InferredType = typeSystem.Boolean }; return right; } else { - return new ILExpression(code, null, left, right); + return new ILExpression(code, null, left, right) { InferredType = typeSystem.Boolean }; } }