Browse Source

Further improvements to boolean logic decompilation

pull/205/head
Pent Ploompuu 15 years ago
parent
commit
970eaa0da1
  1. 29
      ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs

29
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)
if (expr.Code == ILCode.TernaryOp && TypeAnalysis.IsBoolean(expr.Arguments[2].InferredType) && (a = expr.Arguments[1]).Code == ILCode.Ldc_I4 && (int)a.Operand == 0) { switch ((int)a.Operand) {
expr.Code = ILCode.LogicAnd; // "ternaryop(a, ldc.i4.0, b)" becomes "logicand(logicnot(a), b)" if the inferred type for expression "b" is boolean
expr.InferredType = expr.Arguments[2].InferredType; case 0:
a = new ILExpression(ILCode.LogicNot, null, expr.Arguments[0]) { ILRanges = a.ILRanges }; expr.Code = ILCode.LogicAnd;
if (!SimplifyLogicNotArgument(a)) expr.Arguments[0] = a; a = new ILExpression(ILCode.LogicNot, null, expr.Arguments[0]) { ILRanges = a.ILRanges };
expr.Arguments.RemoveAt(1); if (!SimplifyLogicNotArgument(a)) expr.Arguments[0] = a;
res = expr; goto common;
modified = true; // "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; return res;
} }

Loading…
Cancel
Save