From d430ee056bf0487ee8b0efdfd9407b8bb87b9b01 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 20 Mar 2011 17:32:30 +0100 Subject: [PATCH] Recombine variables after ILAst optimizations so that there is only a single C# variable for a single IL variable. --- .../ILAst/ILAstOptimizer.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs b/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs index 034d93722..0d3666f3d 100644 --- a/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs +++ b/ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs @@ -38,6 +38,7 @@ namespace ICSharpCode.Decompiler.ILAst InlineVariables3, CachedDelegateInitialization, IntroduceFixedStatements, + RecombineVariables, TypeInference2, RemoveRedundantCode3, None @@ -180,6 +181,9 @@ namespace ICSharpCode.Decompiler.ILAst } } + if (abortBeforeStep == ILAstOptimizationStep.RecombineVariables) return; + RecombineVariables(method); + if (abortBeforeStep == ILAstOptimizationStep.TypeInference2) return; TypeAnalysis.Reset(method); TypeAnalysis.Run(context, method); @@ -458,6 +462,25 @@ namespace ICSharpCode.Decompiler.ILAst } } + void RecombineVariables(ILBlock method) + { + // Recombine variables that were split when the ILAst was created + // This ensures that a single IL variable is a single C# variable (gets assigned only one name) + // The DeclareVariables transformation might then split up the C# variable again if it is used indendently in two separate scopes. + Dictionary dict = new Dictionary(); + foreach (ILExpression expr in method.GetSelfAndChildrenRecursive()) { + ILVariable v = expr.Operand as ILVariable; + if (v != null && v.OriginalVariable != null) { + ILVariable combinedVariable; + if (!dict.TryGetValue(v.OriginalVariable, out combinedVariable)) { + dict.Add(v.OriginalVariable, v); + combinedVariable = v; + } + expr.Operand = combinedVariable; + } + } + } + void ReportUnassignedILRanges(ILBlock method) { var unassigned = ILRange.Invert(method.GetSelfAndChildrenRecursive().SelectMany(e => e.ILRanges), context.CurrentMethod.Body.CodeSize).ToList(); @@ -511,7 +534,7 @@ namespace ICSharpCode.Decompiler.ILAst } /// - /// The expression has no effect on the program and can be removed + /// The expression has no effect on the program and can be removed /// if its return value is not needed. /// public static bool HasNoSideEffects(this ILExpression expr)