Skip to content

Commit a840251

Browse files
committed
WI #2045 Change Review
1 parent c673a8a commit a840251

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

TypeCobol.Analysis/Graph/CfgAbstractInterpretation.Environment.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@ public class Environment
2626
/// <summary>
2727
/// The DFS Run stack
2828
/// </summary>
29-
private Stack<BasicBlock<N, D>> _dFSStack;
29+
private Stack<BasicBlock<N, D>> _dfsStack;
3030

3131
/// <summary>
3232
/// Already Visited Block
3333
/// </summary>
3434
private System.Collections.BitArray _visited;
35-
3635
/// <summary>
3736
/// Current CFG being run.
3837
/// </summary>
3938
public ControlFlowGraph<N, D> Cfg { get; private set; }
40-
private IObserver[] _observers;
39+
private readonly IObserver[] _observers;
4140
/// <summary>
4241
/// Some metrics calculated.
4342
/// </summary>
44-
public Metrics Metrics { get; private set; }
43+
private Metrics _metrics;
4544
public Environment(params IObserver[] observers)
4645
{
4746
this._observers = observers;
@@ -50,13 +49,17 @@ public Environment(params IObserver[] observers)
5049
public virtual Metrics Run(ControlFlowGraph<N, D> cfg)
5150
{
5251
this.Cfg = cfg;
53-
_interpretationStack = new Stack<BasicBlock<N, D>>();
52+
if (_interpretationStack == null)
53+
_interpretationStack = new Stack<BasicBlock<N, D>>();
5454
// We use an iterative DFS algorithm.
55-
_visited = new System.Collections.BitArray(Cfg.AllBlocks.Count);
56-
_dFSStack = new Stack<BasicBlock<N, D>>();
57-
this.Metrics = new Metrics();
58-
Run(Cfg.RootBlock, null);
59-
return this.Metrics;
55+
_visited = new System.Collections.BitArray(cfg.AllBlocks.Count);
56+
if (_dfsStack == null)
57+
_dfsStack = new Stack<BasicBlock<N, D>>();
58+
this._metrics = new Metrics();
59+
Run(cfg.RootBlock, null);
60+
System.Diagnostics.Debug.Assert(_interpretationStack.Count == 0);
61+
System.Diagnostics.Debug.Assert(_dfsStack.Count == 0);
62+
return this._metrics;
6063
}
6164

6265
/// <summary>
@@ -68,31 +71,31 @@ protected virtual void Run(BasicBlock<N, D> startBlock, BasicBlock<N, D> stopBlo
6871
{
6972
if (_visited.Get(startBlock.Index))
7073
return;
71-
_dFSStack.Push(startBlock);
72-
while (_dFSStack.Count > 0)
74+
_dfsStack.Push(startBlock);
75+
while (_dfsStack.Count > 0)
7376
{
74-
BasicBlock<N, D> block = _dFSStack.Pop();
77+
BasicBlock<N, D> block = _dfsStack.Pop();
7578
if (block == stopBlock)
7679
return;
7780
if (!_visited[block.Index])
7881
{
7982
_visited.Set(block.Index, true);
80-
Metrics.NodeCount++;
83+
_metrics.NodeCount++;
8184
EnterBlock(block);
8285
IterateBlock(block);
8386
BasicBlock<N, D> nextFlowBlock = InterpretContext(block); ;
84-
Metrics.EdgeCount += block.SuccessorEdges.Count;
87+
_metrics.EdgeCount += block.SuccessorEdges.Count;
8588
LeaveBlock(block);
8689
if (nextFlowBlock == null)
8790
{
8891
foreach (var edge in block.SuccessorEdges)
8992
{
90-
_dFSStack.Push(Cfg.SuccessorEdges[edge]);
93+
_dfsStack.Push(Cfg.SuccessorEdges[edge]);
9194
}
9295
}
9396
else
9497
{
95-
_dFSStack.Push(nextFlowBlock);
98+
_dfsStack.Push(nextFlowBlock);
9699
}
97100
}
98101
}
@@ -149,7 +152,7 @@ public void LeaveBlock(BasicBlock<N, D> block)
149152
private BasicBlock<N, D> CheckRelocatedBlock(MultiBranchContext<N, D> ctx, BasicBlock<N, D> fromBlock)
150153
{
151154
var relocIndex = ctx.GetRelocatedBlockIndex(fromBlock.Index);
152-
return relocIndex >= 0 ? this.Cfg.AllBlocks[relocIndex] : fromBlock;
155+
return relocIndex >= 0 ? Cfg.AllBlocks[relocIndex] : fromBlock;
153156
}
154157

155158
/// <summary>
@@ -167,7 +170,7 @@ private BasicBlock<N, D> InterpretContext(BasicBlock<N, D> block)
167170

168171
_interpretationStack.Push(block);
169172

170-
Metrics.ControlSubgraphCount++;
173+
_metrics.ControlSubgraphCount++;
171174
if (block.Context.SubContexts != null)
172175
{
173176
//Instruction with sub context : run all sub context.

0 commit comments

Comments
 (0)