This repository was archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathUnstacker.cs
337 lines (328 loc) · 14.1 KB
/
Unstacker.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if !MinimalReader && !CodeContracts
using System;
using System.Collections;
using System.Diagnostics;
#if CCINamespace
namespace Microsoft.Cci{
#else
namespace System.Compiler{
#endif
/// <summary>
/// Walks a normalized IR, removing push, pop and dup instructions, replacing them with references to local variables.
/// Requires all Blocks to be basic blocks. I.e. any transfer statement is always the last statement in a block.
/// (This precondition is established by Reader but not by Normalizer.)
/// </summary>
public class Unstacker : StandardVisitor{
private TrivialHashtable/*!*/ SucessorBlock = new TrivialHashtable();
private TrivialHashtable/*!*/ StackLocalsAtEntry = new TrivialHashtable();
private LocalsStack/*!*/ localsStack = new LocalsStack();
public Unstacker(){
//^ base();
}
public override Statement VisitAssignmentStatement(AssignmentStatement assignment){
if (assignment == null) return null;
assignment.Source = this.VisitExpression(assignment.Source);
assignment.Target = this.VisitTargetExpression(assignment.Target);
return assignment;
}
public override Expression VisitBinaryExpression(BinaryExpression binaryExpression){
if (binaryExpression == null) return null;
binaryExpression.Operand2 = this.VisitExpression(binaryExpression.Operand2);
binaryExpression.Operand1 = this.VisitExpression(binaryExpression.Operand1);
if (binaryExpression.Type == null) binaryExpression.Type = binaryExpression.Operand1.Type; //Hack: need proper inferencing
return binaryExpression;
}
public override Block VisitBlock(Block block){
if (block == null) return null;
LocalsStack stackLocalsAtEntry = (LocalsStack)this.StackLocalsAtEntry[block.UniqueKey];
if (stackLocalsAtEntry == null){
//Unreachable code, or the very first block
stackLocalsAtEntry = new LocalsStack();
}
this.localsStack = stackLocalsAtEntry.Clone();
base.VisitBlock(block);
Block successor = (Block)this.SucessorBlock[block.UniqueKey];
if (successor != null) {
//Dropping off into successor.
LocalsStack targetStack = (LocalsStack)this.StackLocalsAtEntry[successor.UniqueKey];
if (targetStack != null && targetStack.top >= 0) {
//Another predecessor block has already decided what the stack for the successor block is going to look like.
//Reconcile the stack from this block with the stack expected by the successor block.
this.localsStack.Transfer(targetStack, block.Statements);
}
else {
this.StackLocalsAtEntry[successor.UniqueKey] = this.localsStack;
}
}
return block;
}
public override Statement VisitBranch(Branch branch){
if (branch == null) return null;
if (branch.Target == null) return null;
branch.Condition = this.VisitExpression(branch.Condition);
int n = this.localsStack.top+1;
LocalsStack targetStack = (LocalsStack)this.StackLocalsAtEntry[branch.Target.UniqueKey];
if (targetStack == null){
this.StackLocalsAtEntry[branch.Target.UniqueKey] = this.localsStack.Clone();
return branch;
}
//Target block has an entry stack that is different from the current stack. Need to copy stack before branching.
if (n <= 0) return branch; //Empty stack, no need to copy
StatementList statements = new StatementList(n+1);
this.localsStack.Transfer(targetStack, statements);
statements.Add(branch);
return new Block(statements);
}
public override Statement VisitSwitchInstruction(SwitchInstruction switchInstruction) {
if (switchInstruction == null) return null;
switchInstruction.Expression = this.VisitExpression(switchInstruction.Expression);
for (int i = 0, n = switchInstruction.Targets == null ? 0 : switchInstruction.Targets.Count; i < n; i++){
Block target = switchInstruction.Targets[i];
if (target == null) continue;
this.StackLocalsAtEntry[target.UniqueKey] = this.localsStack.Clone();
}
return switchInstruction;
}
public override ExpressionList VisitExpressionList(ExpressionList expressions) {
if (expressions == null) return null;
for (int i = expressions.Count-1; i >= 0; i--)
expressions[i] = this.VisitExpression(expressions[i]);
return expressions;
}
public override Statement VisitExpressionStatement(ExpressionStatement statement){
if (statement == null) return null;
Expression e = statement.Expression = this.VisitExpression(statement.Expression);
if (e == null || e.Type == CoreSystemTypes.Void) return statement;
if (e.NodeType == NodeType.Dup) return this.localsStack.Dup();
return this.localsStack.Push(e);
}
public override Expression VisitExpression(Expression expression){
if (expression == null) return null;
switch(expression.NodeType){
case NodeType.Dup:
case NodeType.Arglist:
return expression;
case NodeType.Pop:
UnaryExpression uex = expression as UnaryExpression;
if (uex != null){
Expression e = uex.Operand = this.VisitExpression(uex.Operand);
if (e == null) return null;
uex.Type = CoreSystemTypes.Void;
return uex;
}
return this.localsStack.Pop();
default:
return (Expression)this.Visit(expression);
}
}
public override Expression VisitIndexer(Indexer indexer){
if (indexer == null) return null;
indexer.Operands = this.VisitExpressionList(indexer.Operands);
indexer.Object = this.VisitExpression(indexer.Object);
return indexer;
}
public override Method VisitMethod(Method method){
// body might not have been materialized, so make sure we do that first!
Block body = method.Body;
if (method == null) return null;
BlockSorter blockSorter = new BlockSorter();
BlockList sortedBlocks = blockSorter.SortedBlocks;
this.SucessorBlock = blockSorter.SuccessorBlock;
this.StackLocalsAtEntry = new TrivialHashtable();
this.localsStack = new LocalsStack();
ExceptionHandlerList ehandlers = method.ExceptionHandlers;
for (int i = 0, n = ehandlers == null ? 0 : ehandlers.Count; i < n; i++){
ExceptionHandler ehandler = ehandlers[i];
if (ehandler == null) continue;
Block handlerStart = ehandler.HandlerStartBlock;
if (handlerStart == null) continue;
LocalsStack lstack = new LocalsStack();
this.StackLocalsAtEntry[handlerStart.UniqueKey] = lstack;
if (ehandler.HandlerType == NodeType.Catch) {
lstack.exceptionHandlerType = CoreSystemTypes.Object;
if (ehandler.FilterType != null) lstack.exceptionHandlerType = ehandler.FilterType;
} else if (ehandler.HandlerType == NodeType.Filter) {
lstack.exceptionHandlerType = CoreSystemTypes.Object;
if (ehandler.FilterExpression != null) {
lstack = new LocalsStack();
lstack.exceptionHandlerType = CoreSystemTypes.Object;
this.StackLocalsAtEntry[ehandler.FilterExpression.UniqueKey] = lstack;
}
}
}
blockSorter.VisitMethodBody(body);
for (int i = 0, n = sortedBlocks.Count; i < n; i++) {
Block b = sortedBlocks[i];
if (b == null) { Debug.Assert(false); continue; }
this.VisitBlock(b);
}
return method;
}
public override Expression VisitMethodCall(MethodCall call){
if (call == null) return null;
call.Operands = this.VisitExpressionList(call.Operands);
call.Callee = this.VisitExpression(call.Callee);
return call;
}
public override Expression VisitTernaryExpression(TernaryExpression expression){
if (expression == null) return null;
expression.Operand3 = this.VisitExpression(expression.Operand3);
expression.Operand2 = this.VisitExpression(expression.Operand2);
expression.Operand1 = this.VisitExpression(expression.Operand1);
return expression;
}
private class LocalsStack{
private Local[]/*!*/ elements;
internal int top = -1;
internal TypeNode exceptionHandlerType;
private void Grow(){
int n = this.elements.Length;
Local[] newElements = new Local[n+8];
for (int i = 0; i < n; i++) newElements[i] = this.elements[i];
this.elements = newElements;
}
internal LocalsStack(){
this.elements = new Local[8];
//^ base();
}
private LocalsStack(LocalsStack/*!*/ other) {
this.top = other.top;
this.exceptionHandlerType = other.exceptionHandlerType;
Local[] otherElements = other.elements;
int n = otherElements.Length;
Local[] elements = this.elements = new Local[n];
//^ base();
n = this.top+1;
for (int i = 0; i < n; i++)
elements[i] = otherElements[i];
}
internal LocalsStack/*!*/ Clone(){
return new LocalsStack(this);
}
internal AssignmentStatement Dup(){
int i = this.top;
Expression topVal;
if (this.top == -1 && this.exceptionHandlerType != null) {
topVal = new Expression(NodeType.Dup, this.exceptionHandlerType);
}else{
Debug.Assert(i >= 0 && i < this.elements.Length);
topVal = this.elements[i];
//^ assume topVal != null;
}
Local dup = new Local(topVal.Type);
if ((i = ++this.top) >= this.elements.Length) this.Grow();
this.elements[i] = dup;
return new AssignmentStatement(dup, topVal);
}
internal AssignmentStatement Push(Expression/*!*/ expr) {
//Debug.Assert(expr != null && expr.Type != null);
int i = ++this.top;
Debug.Assert(i >= 0);
if (i >= this.elements.Length) this.Grow();
Local loc = this.elements[i];
if (loc == null || loc.Type != expr.Type)
this.elements[i] = loc = new Local(expr.Type);
return new AssignmentStatement(loc, expr);
}
internal Expression Pop(){
if (this.top == -1 && this.exceptionHandlerType != null){
TypeNode t = this.exceptionHandlerType;
this.exceptionHandlerType = null;
return new Expression(NodeType.Pop, t);
}
int i = this.top--;
Debug.Assert(i >= 0 && i < this.elements.Length);
return this.elements[i];
}
internal void Transfer(LocalsStack/*!*/ targetStack, StatementList/*!*/ statements) {
Debug.Assert(targetStack != null);
if (targetStack == this) return;
int n = this.top;
Debug.Assert(n == targetStack.top);
for (int i = 0; i <= n; i++){
Local sloc = this.elements[i];
Local tloc = targetStack.elements[i];
if (sloc == tloc) continue;
Debug.Assert(sloc != null && tloc != null);
statements.Add(new AssignmentStatement(tloc, sloc));
}
}
}
private class BlockSorter : StandardVisitor{
private TrivialHashtable/*!*/ VisitedBlocks = new TrivialHashtable();
private TrivialHashtable/*!*/ BlocksThatDropThrough = new TrivialHashtable();
private bool lastBranchWasUnconditional = false;
internal BlockList/*!*/ SortedBlocks = new BlockList();
internal TrivialHashtable/*!*/ SuccessorBlock = new TrivialHashtable();
internal BlockSorter(){
//^ base();
}
internal void VisitMethodBody(Block body) {
if (body == null) return;
StatementList statements = body.Statements;
if (statements == null) return;
Block previousBlock = null;
for (int i = 0, n = statements.Count; i < n; i++) {
Block b = statements[i] as Block;
if (b == null) { Debug.Assert(false); continue; }
if (previousBlock != null && this.BlocksThatDropThrough[previousBlock.UniqueKey] != null) {
this.SuccessorBlock[previousBlock.UniqueKey] = b;
}
this.VisitBlock(b);
previousBlock = b;
}
}
public override Block VisitBlock(Block block) {
if (block == null) return null;
if (this.VisitedBlocks[block.UniqueKey] != null) {
return block;
}
this.VisitedBlocks[block.UniqueKey] = block;
this.SortedBlocks.Add(block);
this.lastBranchWasUnconditional = false;
base.VisitBlock(block);
if (!this.lastBranchWasUnconditional)
this.BlocksThatDropThrough[block.UniqueKey] = block;
return block;
}
public override Statement VisitBranch(Branch branch){
if (branch == null) return null;
if (branch.Target == null) return null;
this.VisitBlock(branch.Target);
this.lastBranchWasUnconditional = branch.Condition == null;
return branch;
}
public override Statement VisitEndFilter(EndFilter endFilter) {
endFilter.Value = this.VisitExpression(endFilter.Value);
this.lastBranchWasUnconditional = true;
return endFilter;
}
public override Statement VisitEndFinally(EndFinally endFinally) {
this.lastBranchWasUnconditional = true;
return endFinally;
}
public override Statement VisitReturn(Return ret) {
this.lastBranchWasUnconditional = true;
return ret;
}
public override Statement VisitSwitchInstruction(SwitchInstruction switchInstruction) {
if (switchInstruction == null) return null;
switchInstruction.Expression = this.VisitExpression(switchInstruction.Expression);
for (int i = 0, n = switchInstruction.Targets == null ? 0 : switchInstruction.Targets.Count; i < n; i++){
Block target = switchInstruction.Targets[i];
if (target == null) continue;
this.VisitBlock(target);
}
return switchInstruction;
}
public override Statement VisitThrow(Throw Throw) {
this.lastBranchWasUnconditional = true;
return Throw;
}
}
}
}
#endif