From fe1b9dcef073695aefe2cd85e339d20b5119fbec Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 25 Nov 2016 14:20:15 +0100 Subject: [PATCH] Add steps for transforms. --- .../IL/Instructions/ILFunction.cs | 2 ++ .../IL/Transforms/Stepper.cs | 22 ++++++++++++++----- ILSpy/Languages/ILAstLanguage.cs | 21 +++++------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs index d45515432..7652d1c27 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs @@ -82,7 +82,9 @@ namespace ICSharpCode.Decompiler.IL this.CheckInvariant(ILPhase.Normal); foreach (var transform in transforms) { context.CancellationToken.ThrowIfCancellationRequested(); + context.Stepper.StartGroup(transform.GetType().Name); transform.Run(this, context); + context.Stepper.EndGroup(keepIfEmpty: true); this.CheckInvariant(ILPhase.Normal); } } diff --git a/ICSharpCode.Decompiler/IL/Transforms/Stepper.cs b/ICSharpCode.Decompiler/IL/Transforms/Stepper.cs index 48056242e..da977d141 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/Stepper.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/Stepper.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -60,7 +61,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms public ILInstruction Position { get; set; } public int Step { get; set; } - public ICollection Children { get; } = new List(); + public IList Children { get; } = new List(); } readonly Stack groups; @@ -79,7 +80,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms /// /// May throw in debug mode. /// - public void Step(string description, ILInstruction near) + public void Step(string description, ILInstruction near = null) { StepInternal(description, near); } @@ -88,7 +89,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms { if (step >= StepLimit) throw new StepLimitReachedException(); - var stepNode = new Node { Description = $"{description} ({step})", Position = near, Step = step }; + var stepNode = new Node { + Description = $"{step}: {description}", + Position = near, + Step = step + }; var p = groups.PeekOrDefault(); if (p != null) p.Children.Add(stepNode); @@ -98,14 +103,19 @@ namespace ICSharpCode.Decompiler.IL.Transforms return stepNode; } - public void StartGroup(string description, ILInstruction near) + public void StartGroup(string description, ILInstruction near = null) { groups.Push(StepInternal(description, near)); } - public void EndGroup() + public void EndGroup(bool keepIfEmpty = false) { - groups.Pop(); + var node = groups.Pop(); + if (!keepIfEmpty && node.Children.Count == 0) { + var col = groups.PeekOrDefault()?.Children ?? steps; + Debug.Assert(col.Last() == node); + col.RemoveAt(col.Count - 1); + } } } } diff --git a/ILSpy/Languages/ILAstLanguage.cs b/ILSpy/Languages/ILAstLanguage.cs index 3d079376c..9d0de15a9 100644 --- a/ILSpy/Languages/ILAstLanguage.cs +++ b/ILSpy/Languages/ILAstLanguage.cs @@ -99,22 +99,9 @@ namespace ICSharpCode.ILSpy { yield return new TypedIL(); CSharpDecompiler decompiler = new CSharpDecompiler(ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll), new DecompilerSettings()); - for (int i = 0; i <= decompiler.ILTransforms.Count; i++) { - yield return MakeDebugLanguage(decompiler.ILTransforms.Take(i)); - var loop = decompiler.ILTransforms.ElementAtOrDefault(i) as LoopingTransform; - if (loop != null) { - for (int j = 1; j <= loop.Transforms.Count; j++) { - yield return MakeDebugLanguage(decompiler.ILTransforms.Take(i).Concat(loop.Transforms.Take(j))); - } - } - } + yield return new BlockIL(decompiler.ILTransforms.ToList()); } - - static ILAstLanguage MakeDebugLanguage(IEnumerable transforms) - { - return new BlockIL(transforms.ToList()); - } - + public override string FileExtension { get { return ".il"; @@ -155,7 +142,7 @@ namespace ICSharpCode.ILSpy { readonly IReadOnlyList transforms; - public BlockIL(IReadOnlyList transforms) : base(transforms.Count == 0 ? "ILAst (blocks)" : "ILAst (" + transforms.Last().GetType().Name + ")") + public BlockIL(IReadOnlyList transforms) : base("ILAst") { this.transforms = transforms; } @@ -170,6 +157,8 @@ namespace ICSharpCode.ILSpy ILFunction il = reader.ReadIL(method.Body, options.CancellationToken); ILTransformContext context = new ILTransformContext { Settings = options.DecompilerSettings, TypeSystem = typeSystem }; context.Stepper.StepLimit = options.StepLimit; + // Because the UI only allows viewing the result of a step, add a dummy initial step. + context.Stepper.Step("Initial"); try { il.RunTransforms(transforms, context); } catch (StepLimitReachedException) {