-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplifyOperators.cs
More file actions
198 lines (168 loc) · 7.56 KB
/
Copy pathSimplifyOperators.cs
File metadata and controls
198 lines (168 loc) · 7.56 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
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
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 SimplifyOperators : JSAstVisitor {
public readonly TypeSystem TypeSystem;
public readonly JSILIdentifier JSIL;
public readonly JSSpecialIdentifiers JS;
public readonly Dictionary<JSBinaryOperator, JSBinaryOperator> InvertedOperators = new Dictionary<JSBinaryOperator, JSBinaryOperator> {
{ JSOperator.LessThan, JSOperator.GreaterThanOrEqual },
{ JSOperator.LessThanOrEqual, JSOperator.GreaterThan },
{ JSOperator.GreaterThan, JSOperator.LessThanOrEqual },
{ JSOperator.GreaterThanOrEqual, JSOperator.LessThan },
{ JSOperator.Equal, JSOperator.NotEqual },
{ JSOperator.NotEqual, JSOperator.Equal }
};
public readonly Dictionary<JSBinaryOperator, JSAssignmentOperator> CompoundAssignments = new Dictionary<JSBinaryOperator, JSAssignmentOperator> {
{ JSOperator.Add, JSOperator.AddAssignment },
{ JSOperator.Subtract, JSOperator.SubtractAssignment },
{ JSOperator.Multiply, JSOperator.MultiplyAssignment }
};
public readonly Dictionary<JSBinaryOperator, JSUnaryOperator> PrefixOperators = new Dictionary<JSBinaryOperator, JSUnaryOperator> {
{ JSOperator.Add, JSOperator.PreIncrement },
{ JSOperator.Subtract, JSOperator.PreDecrement }
};
public SimplifyOperators (JSILIdentifier jsil, JSSpecialIdentifiers js, TypeSystem typeSystem) {
JSIL = jsil;
JS = js;
TypeSystem = typeSystem;
}
public void VisitNode (JSUnaryOperatorExpression uoe) {
var isBoolean = TypeUtil.IsBoolean(uoe.GetActualType(TypeSystem));
if (isBoolean) {
if (uoe.Operator == JSOperator.IsTrue) {
ParentNode.ReplaceChild(
uoe, uoe.Expression
);
VisitReplacement(uoe.Expression);
return;
} else if (uoe.Operator == JSOperator.LogicalNot) {
var nestedUoe = uoe.Expression as JSUnaryOperatorExpression;
var boe = uoe.Expression as JSBinaryOperatorExpression;
JSBinaryOperator newOperator;
if ((boe != null) &&
InvertedOperators.TryGetValue(boe.Operator, out newOperator)
) {
var newBoe = new JSBinaryOperatorExpression(
newOperator, boe.Left, boe.Right, boe.ActualType
);
ParentNode.ReplaceChild(uoe, newBoe);
VisitReplacement(newBoe);
return;
} else if (
(nestedUoe != null) &&
(nestedUoe.Operator == JSOperator.LogicalNot)
) {
var nestedExpression = nestedUoe.Expression;
ParentNode.ReplaceChild(uoe, nestedExpression);
VisitReplacement(nestedExpression);
return;
}
}
}
VisitChildren(uoe);
}
public void VisitNode (JSBinaryOperatorExpression boe) {
if (!boe.CanSimplify) {
VisitChildren(boe);
return;
}
JSExpression left, right, nestedLeft;
if (!JSReferenceExpression.TryDereference(JSIL, boe.Left, out left))
left = boe.Left;
if (!JSReferenceExpression.TryDereference(JSIL, boe.Right, out right))
right = boe.Right;
var nestedBoe = right as JSBinaryOperatorExpression;
var isAssignment = (boe.Operator == JSOperator.Assignment);
var leftNew = left as JSNewExpression;
var rightNew = right as JSNewExpression;
var leftVar = left as JSVariable;
if (
isAssignment && (nestedBoe != null) &&
(left.IsConstant || (leftVar != null) || left is JSDotExpressionBase) &&
!(ParentNode is JSVariableDeclarationStatement)
) {
if (!nestedBoe.CanSimplify) {
VisitChildren(boe);
return;
}
JSUnaryOperator prefixOperator;
JSAssignmentOperator compoundOperator;
if (!JSReferenceExpression.TryDereference(JSIL, nestedBoe.Left, out nestedLeft))
nestedLeft = nestedBoe.Left;
var rightLiteral = nestedBoe.Right as JSIntegerLiteral;
var areEqual = left.Equals(nestedLeft);
if (
areEqual &&
PrefixOperators.TryGetValue(nestedBoe.Operator, out prefixOperator) &&
(rightLiteral != null) && (rightLiteral.Value == 1)
) {
var newUoe = new JSUnaryOperatorExpression(
prefixOperator, boe.Left,
boe.GetActualType(TypeSystem)
);
ParentNode.ReplaceChild(boe, newUoe);
VisitReplacement(newUoe);
return;
} else if (
areEqual &&
CompoundAssignments.TryGetValue(nestedBoe.Operator, out compoundOperator)
) {
var newBoe = new JSBinaryOperatorExpression(
compoundOperator, boe.Left, nestedBoe.Right,
boe.GetActualType(TypeSystem)
);
ParentNode.ReplaceChild(boe, newBoe);
VisitReplacement(newBoe);
return;
}
} else if (
isAssignment && (leftNew != null) &&
(rightNew != null)
) {
// FIXME
/*
var rightType = rightNew.Type as JSDotExpressionBase;
if (
(rightType != null) &&
(rightType.Member.Identifier == "CollectionInitializer")
) {
var newInitializer = new JSInitializerApplicationExpression(
boe.Left, new JSArrayExpression(
TypeSystem.Object,
rightNew.Arguments.ToArray()
)
);
ParentNode.ReplaceChild(boe, newInitializer);
VisitReplacement(newInitializer);
return;
}
*/
} else if (
isAssignment && (leftVar != null) &&
leftVar.IsThis
) {
var leftType = leftVar.GetActualType(TypeSystem);
if (!TypeUtil.IsStruct(leftType)) {
ParentNode.ReplaceChild(boe, new JSUntranslatableExpression(boe));
return;
} else {
var newInvocation = JSInvocationExpression.InvokeStatic(
JSIL.CopyMembers, new[] { boe.Right, leftVar }
);
ParentNode.ReplaceChild(boe, newInvocation);
VisitReplacement(newInvocation);
return;
}
}
VisitChildren(boe);
}
}
}