Skip to content

Commit dc9a4c4

Browse files
committed
WI #2045 Change Review
1 parent 95f2622 commit dc9a4c4

File tree

4 files changed

+35
-47
lines changed

4 files changed

+35
-47
lines changed

TypeCobol.Analysis.Test/BasicCfgInstrs/Declaratives1.int

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ Leaving block : 4
7171
=======
7272
METRICS
7373
=======
74-
{EdgeCount=27; NodeCount=24; ControlSubgraphCount=1; HighCyclomaticComplexity=5; HighEssentialComplexityPath=4}
74+
{EdgeCount=28; NodeCount=24; ControlSubgraphCount=1; HighCyclomaticComplexity=6; HighEssentialComplexityPath=5}

TypeCobol.Analysis.Test/BasicCfgInstrs/MixPerformEvaluateIf0.int

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,4 @@ Leaving block : 57
170170
=======
171171
METRICS
172172
=======
173-
{EdgeCount=78; NodeCount=58; ControlSubgraphCount=19; HighCyclomaticComplexity=22; HighEssentialComplexityPath=3}
173+
{EdgeCount=76; NodeCount=58; ControlSubgraphCount=19; HighCyclomaticComplexity=20; HighEssentialComplexityPath=1}

TypeCobol.Analysis/Graph/CfgAbstractInterpretation.Environment.cs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,47 @@ public class Environment
1616
/// <summary>
1717
/// The Abstract Interpretation Stack
1818
/// </summary>
19-
private Stack<BasicBlock<N, D>> InterpretationStack { get; set; }
19+
private Stack<BasicBlock<N, D>> _interpretationStack;
2020

2121
/// <summary>
2222
/// The current execution stack
2323
/// </summary>
24-
public IReadOnlyCollection<BasicBlock<N, D>> ExecutionStack => InterpretationStack;
24+
public IReadOnlyCollection<BasicBlock<N, D>> ExecutionStack => _interpretationStack;
2525

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>
34-
private System.Collections.BitArray Visited;
34+
private System.Collections.BitArray _visited;
3535

3636
/// <summary>
3737
/// Current CFG being run.
3838
/// </summary>
39-
public readonly ControlFlowGraph<N, D> Cfg;
39+
public ControlFlowGraph<N, D> Cfg { get; private set; }
4040
private IObserver[] _observers;
4141
/// <summary>
4242
/// Some metrics calculated.
4343
/// </summary>
44-
public readonly Metrics Metrics;
45-
public Environment(ControlFlowGraph<N, D> cfg, params IObserver[] observers)
44+
public Metrics Metrics { get; private set; }
45+
public Environment(params IObserver[] observers)
4646
{
47-
this.Cfg = cfg;
4847
this._observers = observers;
49-
InterpretationStack = new Stack<BasicBlock<N, D>>();
50-
// We use an iterative DFS algorithm.
51-
Visited = new System.Collections.BitArray(cfg.AllBlocks.Count);
52-
DFSStack = new Stack<BasicBlock<N, D>>();
53-
this.Metrics = new Metrics();
5448
}
5549

56-
public virtual void Run()
50+
public virtual Metrics Run(ControlFlowGraph<N, D> cfg)
5751
{
58-
Run(Cfg.RootBlock, null);
52+
this.Cfg = cfg;
53+
_interpretationStack = new Stack<BasicBlock<N, D>>();
54+
// 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;
5960
}
6061

6162
/// <summary>
@@ -65,17 +66,17 @@ public virtual void Run()
6566
/// <param name="stopBlock">The stopping branch which will not be executed.</param>
6667
protected virtual void Run(BasicBlock<N, D> startBlock, BasicBlock<N, D> stopBlock)
6768
{
68-
if (Visited.Get(startBlock.Index))
69+
if (_visited.Get(startBlock.Index))
6970
return;
70-
DFSStack.Push(startBlock);
71-
while (DFSStack.Count > 0)
71+
_dFSStack.Push(startBlock);
72+
while (_dFSStack.Count > 0)
7273
{
73-
BasicBlock<N, D> block = DFSStack.Pop();
74+
BasicBlock<N, D> block = _dFSStack.Pop();
7475
if (block == stopBlock)
7576
return;
76-
if (!Visited[block.Index])
77+
if (!_visited[block.Index])
7778
{
78-
Visited.Set(block.Index, true);
79+
_visited.Set(block.Index, true);
7980
Metrics.NodeCount++;
8081
EnterBlock(block);
8182
IterateBlock(block);
@@ -86,28 +87,18 @@ protected virtual void Run(BasicBlock<N, D> startBlock, BasicBlock<N, D> stopBlo
8687
{
8788
nextFlowBlock = InterpretContext(block);
8889
}
89-
90-
LeaveBlock(block);
90+
Metrics.EdgeCount += block.SuccessorEdges.Count;
91+
LeaveBlock(block);
9192
if (nextFlowBlock == null)
9293
{
9394
foreach (var edge in block.SuccessorEdges)
9495
{
95-
DFSStack.Push(Cfg.SuccessorEdges[edge]);
96-
Metrics.EdgeCount++;
96+
_dFSStack.Push(Cfg.SuccessorEdges[edge]);
9797
}
9898
}
9999
else
100100
{
101-
DFSStack.Push(nextFlowBlock);
102-
// If the multi branch context instruction has a successor
103-
// to the next flow block, add one edge.
104-
foreach (var edge in block.SuccessorEdges)
105-
{
106-
if (Cfg.SuccessorEdges[edge] == nextFlowBlock)
107-
{
108-
Metrics.EdgeCount++;
109-
}
110-
}
101+
_dFSStack.Push(nextFlowBlock);
111102
}
112103
}
113104
}
@@ -119,7 +110,7 @@ protected virtual void Run(BasicBlock<N, D> startBlock, BasicBlock<N, D> stopBlo
119110
/// <param name="block"></param>
120111
private void IterateBlock(BasicBlock<N, D> block)
121112
{
122-
if (_observers != null)
113+
if (_observers != null && block.Instructions != null)
123114
{
124115
foreach (var i in block.Instructions)
125116
{
@@ -174,30 +165,28 @@ private BasicBlock<N, D> CheckRelocatedBlock(MultiBranchContext<N, D> ctx, Basic
174165
/// <returns>The next block</returns>
175166
private BasicBlock<N, D> InterpretContext(BasicBlock<N, D> block)
176167
{
177-
InterpretationStack.Push(block);
168+
_interpretationStack.Push(block);
178169

179170
Metrics.ControlSubgraphCount++;
180171
if (block.Context.SubContexts != null)
181172
{
182173
//Instruction with sub context : run all sub context.
183174
foreach (var ctx in block.Context.SubContexts)
184175
{
185-
Metrics.EdgeCount++;
186176
Run(CheckRelocatedBlock(ctx, ctx.OriginBlock), CheckRelocatedBlock(ctx, ctx.NextFlowBlock));
187177
}
188178
}
189179
else
190180
{
191181
foreach (var b in block.Context.Branches)
192182
{
193-
Metrics.EdgeCount++;
194183
Run(CheckRelocatedBlock(block.Context, b), CheckRelocatedBlock(block.Context, block.Context.NextFlowBlock));
195184
}
196185
}
197186

198-
System.Diagnostics.Debug.Assert(this.InterpretationStack.Count > 0 &&
199-
this.InterpretationStack.Peek() == block);
200-
this.InterpretationStack.Pop();
187+
System.Diagnostics.Debug.Assert(this._interpretationStack.Count > 0 &&
188+
this._interpretationStack.Peek() == block);
189+
this._interpretationStack.Pop();
201190
return CheckRelocatedBlock(block.Context, block.Context.NextFlowBlock);
202191
}
203192

TypeCobol.Analysis/Graph/CfgAbstractInterpretation.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ public partial class CfgAbstractInterpretation<N, D>
2121
/// <returns>Metrics collected during abstract interpretation</returns>
2222
public static Metrics Run(ControlFlowGraph<N, D> cfg, params IObserver[] observers)
2323
{
24-
Environment env = new Environment(cfg, observers);
25-
env.Run();
26-
return env.Metrics;
24+
Environment env = new Environment(observers);
25+
return env.Run(cfg);
2726
}
2827
}
2928
}

0 commit comments

Comments
 (0)