From 970eaa0da1a25c9f112ad60f923d6863387ed4ca Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Wed, 25 May 2011 03:25:47 +0300 Subject: [PATCH] Further improvements to boolean logic decompilation --- .../ILAst/PeepholeTransform.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs b/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs index d8e3c12f4..d735e1e89 100644 --- a/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs +++ b/ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs @@ -911,16 +911,25 @@ namespace ICSharpCode.Decompiler.ILAst } } - // "ternaryop(a, ldc.i4.0, b)" becomes "logicand(logicnot(a), b)" if the inferred type for expression "b" is boolean - if (expr.Code == ILCode.TernaryOp && TypeAnalysis.IsBoolean(expr.Arguments[2].InferredType) && (a = expr.Arguments[1]).Code == ILCode.Ldc_I4 && (int)a.Operand == 0) { - expr.Code = ILCode.LogicAnd; - expr.InferredType = expr.Arguments[2].InferredType; - a = new ILExpression(ILCode.LogicNot, null, expr.Arguments[0]) { ILRanges = a.ILRanges }; - if (!SimplifyLogicNotArgument(a)) expr.Arguments[0] = a; - expr.Arguments.RemoveAt(1); - res = expr; - modified = true; - } + 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; }