File: BlockBuilder.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (195 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 
// BlockBuilder.cs
// 
// Authors:
// 	Alexander Chebaturkin (chebaturkin@gmail.com)
// 
// Copyright (C) 2011 Alexander Chebaturkin
// 
// 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 Mono.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.AST.Visitors;
using Mono.CodeContracts.Static.ControlFlow.Blocks;
using Mono.CodeContracts.Static.DataStructures;

namespace Mono.CodeContracts.Static.ControlFlow.Subroutines.Builders {
	class BlockBuilder<TLabel> : ILVisitorBase<TLabel, Dummy, Dummy, BlockWithLabels<TLabel>, bool>,
	                             IAggregateVisitor<TLabel, BlockWithLabels<TLabel>, bool> {
		private readonly SubroutineBuilder<TLabel> builder;
		private BlockWithLabels<TLabel> current_block;

		private BlockBuilder (SubroutineBuilder<TLabel> builder)
		{
			this.builder = builder;
		}

		private SubroutineBase<TLabel> CurrentSubroutine
		{
			get { return this.builder.CurrentSubroutine; }
		}

		#region IAggregateVisitor<Label,BlockWithLabels<Label>,bool> Members
		public override bool Branch (TLabel pc, TLabel target, bool leavesExceptionBlock, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			CurrentSubroutine.AddSuccessor (currentBlock, EdgeTag.Branch, CurrentSubroutine.GetTargetBlock (target));

			return true;
		}

		public override bool BranchCond (TLabel pc, TLabel target, BranchOperator bop, Dummy value1, Dummy value2, BlockWithLabels<TLabel> currentBlock)
		{
			return HandleConditionalBranch (pc, target, true, currentBlock);
		}

		public override bool BranchFalse (TLabel pc, TLabel target, Dummy cond, BlockWithLabels<TLabel> currentBlock)
		{
			return HandleConditionalBranch (pc, target, false, currentBlock);
		}

		public override bool BranchTrue (TLabel pc, TLabel target, Dummy cond, BlockWithLabels<TLabel> currentBlock)
		{
			return HandleConditionalBranch (pc, target, true, currentBlock);
		}

		public override bool Throw (TLabel pc, Dummy exception, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			return true;
		}

		public override bool Rethrow (TLabel pc, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			return true;
		}

		public override bool EndFinally (TLabel pc, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			CurrentSubroutine.AddSuccessor (currentBlock, EdgeTag.EndSubroutine, CurrentSubroutine.Exit);
			return true;
		}

		public override bool Return (TLabel pc, Dummy source, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			CurrentSubroutine.AddSuccessor (currentBlock, EdgeTag.Return, CurrentSubroutine.Exit);
			CurrentSubroutine.AddReturnBlock (currentBlock);

			return true;
		}

		public override bool Nop (TLabel pc, BlockWithLabels<TLabel> currentBlock)
		{
			return false;
		}

		public override bool LoadField (TLabel pc, Field field, Dummy dest, Dummy obj, BlockWithLabels<TLabel> data)
		{
			if (CurrentSubroutine.IsMethod) {
				var methodInfo = (IMethodInfo) CurrentSubroutine;
				Property property;
				if (this.builder.MetaDataProvider.IsPropertyGetter (methodInfo.Method, out property))
					this.builder.SubroutineFacade.AddReads (methodInfo.Method, field);
			}
			this.current_block.AddLabel (pc);
			return false;
		}

		public override bool StoreField (TLabel pc, Field field, Dummy obj, Dummy value, BlockWithLabels<TLabel> data)
		{
			if (CurrentSubroutine.IsMethod) {
				var methodInfo = (IMethodInfo) CurrentSubroutine;
				Property property;
				if (this.builder.MetaDataProvider.IsPropertySetter (methodInfo.Method, out property))
					this.builder.SubroutineFacade.AddReads (methodInfo.Method, field);
			}
			this.current_block.AddLabel (pc);
			return false;
		}

		public override bool EndOld (TLabel pc, TLabel matchingBegin, TypeNode type, Dummy dest, Dummy source, BlockWithLabels<TLabel> data)
		{
			this.current_block.AddLabel (pc);
			CurrentSubroutine.AddSuccessor (this.current_block, EdgeTag.EndOld, CurrentSubroutine.Exit);
			return false;
		}

		public bool Aggregate (TLabel pc, TLabel aggregateStart, bool canBeTargetOfBranch, BlockWithLabels<TLabel> data)
		{
			TraceAggregateSequentally (aggregateStart);
			return false;
		}
		#endregion

		public static BlockWithLabels<TLabel> BuildBlocks (TLabel entry, SubroutineBuilder<TLabel> subroutineBuilder)
		{
			var blockBuilder = new BlockBuilder<TLabel> (subroutineBuilder);
			blockBuilder.TraceAggregateSequentally (entry);
			if (blockBuilder.current_block == null)
				return null;

			SubroutineBase<TLabel> subroutine = blockBuilder.CurrentSubroutine;

			subroutine.AddSuccessor (blockBuilder.current_block, EdgeTag.FallThroughReturn, subroutine.Exit);
			subroutine.AddReturnBlock (blockBuilder.current_block);

			return blockBuilder.current_block;
		}

		private void TraceAggregateSequentally (TLabel currentLabel)
		{
			do {
				if (this.builder.IsBlockStart (currentLabel))
					this.current_block = this.builder.RecordInformationForNewBlock (currentLabel, this.current_block);
				if (this.builder.CodeProvider.Decode<BlockBuilder<TLabel>, BlockWithLabels<TLabel>, bool> (currentLabel, this, this.current_block))
					this.current_block = null;
			} while (this.builder.CodeProvider.Next (currentLabel, out currentLabel));
		}

		public override bool DefaultVisit (TLabel pc, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			return false;
		}

		private bool HandleConditionalBranch (TLabel pc, TLabel target, bool isTrueBranch, BlockWithLabels<TLabel> currentBlock)
		{
			currentBlock.AddLabel (pc);
			EdgeTag trueTag = isTrueBranch ? EdgeTag.True : EdgeTag.False;
			EdgeTag falseTag = isTrueBranch ? EdgeTag.False : EdgeTag.True;

			AssumeBlock<TLabel> trueBlock = CurrentSubroutine.NewAssumeBlock (pc, trueTag);
			this.builder.RecordInformationSameAsOtherBlock (trueBlock, this.current_block);
			CurrentSubroutine.AddSuccessor (currentBlock, trueTag, trueBlock);
			CurrentSubroutine.AddSuccessor (trueBlock, EdgeTag.FallThrough, CurrentSubroutine.GetTargetBlock (target));

			AssumeBlock<TLabel> falseBlock = CurrentSubroutine.NewAssumeBlock (pc, falseTag);
			this.builder.RecordInformationSameAsOtherBlock (falseBlock, this.current_block);
			CurrentSubroutine.AddSuccessor (currentBlock, falseTag, falseBlock);
			this.current_block = falseBlock;

			return false;
		}
	}
}