mirror of https://github.com/icsharpcode/ILSpy.git
80 changed files with 467 additions and 340 deletions
@ -0,0 +1,109 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using ICSharpCode.Decompiler.FlowAnalysis; |
||||||
|
using ICSharpCode.Decompiler.IL.ControlFlow; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.IL.Transforms |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Per-block IL transform.
|
||||||
|
/// </summary>
|
||||||
|
public interface IBlockTransform |
||||||
|
{ |
||||||
|
void Run(Block block, BlockTransformContext context); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class holding various arguments for <see cref="IBlockTransform.Run(ILFunction, BlockTransformContext)"/>.
|
||||||
|
/// </summary>
|
||||||
|
public class BlockTransformContext : ILTransformContext |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// The container currently being processed.
|
||||||
|
/// </summary>
|
||||||
|
public BlockContainer Container { get; set; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The block to process.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Should be identical to the <c>block</c> parameter to <c>IBlockTransform.Run</c>.
|
||||||
|
/// </remarks>
|
||||||
|
public Block Block { get; set; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The control flow node corresponding to the block being processed.
|
||||||
|
/// </summary>
|
||||||
|
public ControlFlowNode ControlFlowNode { get; set; } |
||||||
|
|
||||||
|
internal readonly Dictionary<Block, ControlFlowNode> cfg = new Dictionary<Block, ControlFlowNode>(); |
||||||
|
|
||||||
|
public BlockTransformContext(ILTransformContext context) : base(context) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the ControlFlowNode for the block.
|
||||||
|
/// Precondition: the block belonged to the <c>Container</c> at the start of the block transforms
|
||||||
|
/// (when the control flow graph was created).
|
||||||
|
/// </summary>
|
||||||
|
public ControlFlowNode GetNode(Block block) |
||||||
|
{ |
||||||
|
return cfg[block]; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// IL transform that runs a list of per-block transforms.
|
||||||
|
/// </summary>
|
||||||
|
public class BlockILTransform : IILTransform |
||||||
|
{ |
||||||
|
readonly IBlockTransform[] blockTransforms; |
||||||
|
|
||||||
|
public BlockILTransform(params IBlockTransform[] blockTransforms) |
||||||
|
{ |
||||||
|
this.blockTransforms = blockTransforms; |
||||||
|
} |
||||||
|
|
||||||
|
public void Run(ILFunction function, ILTransformContext context) |
||||||
|
{ |
||||||
|
var blockContext = new BlockTransformContext(context); |
||||||
|
foreach (var container in function.Descendants.OfType<BlockContainer>()) { |
||||||
|
context.CancellationToken.ThrowIfCancellationRequested(); |
||||||
|
var cfg = LoopDetection.BuildCFG(container); |
||||||
|
Dominance.ComputeDominance(cfg[0], context.CancellationToken); |
||||||
|
|
||||||
|
blockContext.Container = container; |
||||||
|
blockContext.cfg.Clear(); |
||||||
|
for (int i = 0; i < cfg.Length; i++) { |
||||||
|
blockContext.cfg.Add(container.Blocks[i], cfg[i]); |
||||||
|
} |
||||||
|
VisitBlock(cfg[0], blockContext); |
||||||
|
// TODO: handle unreachable code?
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void VisitBlock(ControlFlowNode cfgNode, BlockTransformContext context) |
||||||
|
{ |
||||||
|
// First, process the children in the dominator tree.
|
||||||
|
// The ConditionDetection transform requires dominated blocks to
|
||||||
|
// be already processed.
|
||||||
|
foreach (var child in cfgNode.DominatorTreeChildren) { |
||||||
|
VisitBlock(child, context); |
||||||
|
} |
||||||
|
|
||||||
|
context.ControlFlowNode = cfgNode; |
||||||
|
context.Block = (Block)cfgNode.UserData; |
||||||
|
context.Block.CheckInvariant(ILPhase.Normal); |
||||||
|
foreach (var transform in blockTransforms) { |
||||||
|
context.CancellationToken.ThrowIfCancellationRequested(); |
||||||
|
transform.Run(context.Block, context); |
||||||
|
context.Block.CheckInvariant(ILPhase.Normal); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
// Copyright (c) 2016 Daniel Grunwald
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace ICSharpCode.Decompiler.IL.Transforms |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Exception thrown when an IL transform runs into the <see cref="Stepper.MaxStepCount"/> limit.
|
||||||
|
/// </summary>
|
||||||
|
public class StepLimitReachedException : Exception |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Helper class that manages recording transform steps.
|
||||||
|
/// </summary>
|
||||||
|
public class Stepper |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Gets whether stepping of built-in transforms is supported in this build of ICSharpCode.Decompiler.
|
||||||
|
/// Usually only debug builds support transform stepping.
|
||||||
|
/// </summary>
|
||||||
|
public static bool SteppingAvailable { |
||||||
|
get { |
||||||
|
#if STEP
|
||||||
|
return true; |
||||||
|
#else
|
||||||
|
return false; |
||||||
|
#endif
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this method immediately before performing a transform step.
|
||||||
|
/// Used for debugging the IL transforms. Has no effect in release mode.
|
||||||
|
///
|
||||||
|
/// May throw <see cref="StepLimitReachedException"/> in debug mode.
|
||||||
|
/// </summary>
|
||||||
|
public void Step(string description, ILInstruction near) |
||||||
|
{ |
||||||
|
// TODO: implement stepping
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue