-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplifyLoops.cs
More file actions
161 lines (138 loc) · 6.2 KB
/
Copy pathSimplifyLoops.cs
File metadata and controls
161 lines (138 loc) · 6.2 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ICSharpCode.Decompiler.ILAst;
using JSIL.Ast;
using JSIL.Internal;
using Mono.Cecil;
namespace JSIL.Transforms {
public class SimplifyLoops : JSAstVisitor {
public readonly TypeSystem TypeSystem;
public readonly bool PostSwitchTransform;
public SimplifyLoops (TypeSystem typeSystem, bool postSwitchTransform) {
TypeSystem = typeSystem;
PostSwitchTransform = postSwitchTransform;
}
public void VisitNode (JSWhileLoop whileLoop) {
JSVariable initVariable = null, lastVariable = null;
JSBinaryOperator initOperator = null;
JSExpression initValue = null;
var prevEStmt = PreviousSibling as JSExpressionStatement;
var prevVDS = PreviousSibling as JSVariableDeclarationStatement;
if (prevEStmt != null) {
var boe = prevEStmt.Expression as JSBinaryOperatorExpression;
if (
(boe != null) &&
(boe.Operator is JSAssignmentOperator) &&
(boe.Left is JSVariable)
) {
initVariable = (JSVariable)boe.Left;
initOperator = boe.Operator;
initValue = boe.Right;
}
} else if (prevVDS != null) {
var decl = prevVDS.Declarations.FirstOrDefault(
(d) => !d.IsNull
);
if (decl != null) {
initVariable = (JSVariable)decl.Left;
initOperator = decl.Operator;
initValue = decl.Right;
}
}
var lastStatement = whileLoop.Statements.LastOrDefault();
while ((lastStatement != null) && (lastStatement.GetType() == typeof(JSBlockStatement)))
lastStatement = ((JSBlockStatement)lastStatement).Statements.LastOrDefault();
var lastExpressionStatement = lastStatement as JSExpressionStatement;
if (lastExpressionStatement != null) {
var lastUoe = lastExpressionStatement.Expression as JSUnaryOperatorExpression;
var lastBoe = lastExpressionStatement.Expression as JSBinaryOperatorExpression;
if ((lastUoe != null) && (lastUoe.Operator is JSUnaryMutationOperator)) {
lastVariable = lastUoe.Expression as JSVariable;
} else if ((lastBoe != null) && (lastBoe.Operator is JSAssignmentOperator)) {
lastVariable = lastBoe.Left as JSVariable;
if (
(lastVariable != null) &&
!lastBoe.Right.SelfAndChildrenRecursive.Any(
(n) => lastVariable.Equals(n)
)
) {
lastVariable = null;
}
}
}
var lastIfStatement = lastStatement as JSIfStatement;
if (
(lastIfStatement != null) &&
whileLoop.Condition is JSBooleanLiteral &&
((JSBooleanLiteral)whileLoop.Condition).Value
) {
var innerStatement = lastIfStatement.TrueClause;
while (innerStatement is JSBlockStatement) {
var bs = (JSBlockStatement)innerStatement;
if (bs.Statements.Count != 1) {
innerStatement = null;
break;
}
innerStatement = bs.Statements[0];
}
var eStmt = innerStatement as JSExpressionStatement;
if (eStmt != null) {
var breakExpr = eStmt.Expression as JSBreakExpression;
if ((breakExpr != null) && (breakExpr.TargetLoop == whileLoop.Index)) {
whileLoop.ReplaceChildRecursive(lastIfStatement, new JSNullStatement());
var doLoop = new JSDoLoop(
new JSUnaryOperatorExpression(JSOperator.LogicalNot, lastIfStatement.Condition, TypeSystem.Boolean),
whileLoop.Statements.ToArray()
);
doLoop.Index = whileLoop.Index;
ParentNode.ReplaceChild(whileLoop, doLoop);
VisitChildren(doLoop);
return;
}
}
}
bool cantBeFor = false;
if ((initVariable != null) && (lastVariable != null) &&
!initVariable.Equals(lastVariable)
) {
cantBeFor = true;
} else if ((initVariable ?? lastVariable) == null) {
cantBeFor = true;
} else if (!whileLoop.Condition.SelfAndChildrenRecursive.Any(
(n) => (initVariable ?? lastVariable).Equals(n)
)) {
cantBeFor = true;
} else if (
!PostSwitchTransform && (
(lastStatement is JSSwitchStatement) ||
(lastStatement is JSLabelGroupStatement)
)
) {
cantBeFor = true;
}
if (!cantBeFor) {
JSStatement initializer = null, increment = null;
if (initVariable != null) {
initializer = PreviousSibling as JSStatement;
ParentNode.ReplaceChild(PreviousSibling, new JSNullStatement());
}
if (lastVariable != null) {
increment = lastExpressionStatement;
whileLoop.ReplaceChildRecursive(lastExpressionStatement, new JSNullStatement());
}
var forLoop = new JSForLoop(
initializer, whileLoop.Condition, increment,
whileLoop.Statements.ToArray()
);
forLoop.Index = whileLoop.Index;
ParentNode.ReplaceChild(whileLoop, forLoop);
VisitChildren(forLoop);
} else {
VisitChildren(whileLoop);
}
}
}
}