-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecomposeMutationOperators.cs
More file actions
220 lines (188 loc) · 8.42 KB
/
Copy pathDecomposeMutationOperators.cs
File metadata and controls
220 lines (188 loc) · 8.42 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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 DecomposeMutationOperators : JSAstVisitor {
public readonly TypeSystem TypeSystem;
public readonly TypeInfoProvider TypeInfo;
public readonly IFunctionSource FunctionSource;
public readonly bool DecomposeAllMutations;
public DecomposeMutationOperators (
TypeSystem typeSystem, TypeInfoProvider typeInfo,
IFunctionSource functionSource, bool decomposeAllMutations
) {
TypeSystem = typeSystem;
TypeInfo = typeInfo;
FunctionSource = functionSource;
DecomposeAllMutations = decomposeAllMutations;
}
public static JSExpression MakeLhsForAssignment (JSExpression rhs) {
var fa = rhs as JSFieldAccess;
var pa = rhs as JSPropertyAccess;
if ((fa != null) && (fa.IsWrite == false))
return new JSFieldAccess(fa.ThisReference, fa.Field, true);
else if ((pa != null) && (pa.IsWrite == false))
return new JSPropertyAccess(
pa.ThisReference, pa.Property, true, pa.TypeQualified, pa.OriginalType, pa.OriginalMethod, pa.IsVirtualCall
);
return rhs;
}
public static JSExpression MakeReadVersion (JSExpression e) {
var fa = e as JSFieldAccess;
var pa = e as JSPropertyAccess;
if ((fa != null) && fa.IsWrite)
return new JSFieldAccess(fa.ThisReference, fa.Field, false);
else if ((pa != null) && pa.IsWrite)
return new JSPropertyAccess(
pa.ThisReference, pa.Property, false, pa.TypeQualified, pa.OriginalType, pa.OriginalMethod, pa.IsVirtualCall
);
return e;
}
public static JSBinaryOperatorExpression MakeUnaryMutation (
JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
TypeReference type, TypeSystem typeSystem
) {
var newValue = new JSBinaryOperatorExpression(
mutationOperator, expressionToMutate, JSLiteral.New(1),
type
);
var assignment = new JSBinaryOperatorExpression(
JSOperator.Assignment,
MakeLhsForAssignment(expressionToMutate), newValue, type
);
assignment = ConvertReadExpressionToWriteExpression(assignment, typeSystem);
return assignment;
}
// Decomposing a compound assignment can leave us with a read expression on the left-hand side
// if the compound assignment was targeting a pointer or a reference.
// We fix this by converting the mundane binary operator expression representing the assignment
// into a specialized one that represents a pointer write or reference write.
private static JSBinaryOperatorExpression ConvertReadExpressionToWriteExpression (
JSBinaryOperatorExpression boe, TypeSystem typeSystem
) {
var rtpe = boe.Left as JSReadThroughPointerExpression;
if (rtpe != null)
return new JSWriteThroughPointerExpression(rtpe.Pointer, boe.Right, boe.ActualType, rtpe.OffsetInBytes);
var rtre = boe.Left as JSReadThroughReferenceExpression;
if (rtre != null)
return new JSWriteThroughReferenceExpression(rtre.Variable, boe.Right);
return boe;
}
public static JSExpression DeconstructMutationAssignment (
JSNode parentNode, JSBinaryOperatorExpression boe, TypeSystem typeSystem, TypeReference intermediateType
) {
var assignmentOperator = boe.Operator as JSAssignmentOperator;
if (assignmentOperator == null)
return null;
JSBinaryOperator replacementOperator;
if (!IntroduceEnumCasts.ReverseCompoundAssignments.TryGetValue(assignmentOperator, out replacementOperator))
return null;
var leftType = boe.Left.GetActualType(typeSystem);
var newBoe = new JSBinaryOperatorExpression(
JSOperator.Assignment, MakeLhsForAssignment(boe.Left),
new JSBinaryOperatorExpression(
replacementOperator, MakeReadVersion(boe.Left), boe.Right, intermediateType
),
leftType
);
var result = ConvertReadExpressionToWriteExpression(newBoe, typeSystem);
if (parentNode is JSExpressionStatement) {
return result;
} else {
var comma = new JSCommaExpression(
newBoe, MakeReadVersion(boe.Left)
);
return comma;
}
}
public void VisitNode (JSBinaryOperatorExpression boe) {
var resultType = boe.GetActualType(TypeSystem);
var resultIsIntegral = TypeUtil.Is32BitIntegral(resultType);
var resultIsPointer = TypeUtil.IsPointer(resultType);
JSExpression replacement;
if (
(resultIsIntegral || DecomposeAllMutations) &&
((replacement = DeconstructMutationAssignment(ParentNode, boe, TypeSystem, resultType)) != null)
) {
ParentNode.ReplaceChild(boe, replacement);
VisitReplacement(replacement);
return;
}
if (boe.Operator == JSOperator.Assignment)
{
replacement = ConvertReadExpressionToWriteExpression(boe, TypeSystem);
if (replacement != boe)
{
ParentNode.ReplaceChild(boe, replacement);
VisitReplacement(replacement);
return;
}
}
VisitChildren(boe);
}
public void VisitNode (JSUnaryOperatorExpression uoe) {
var type = uoe.Expression.GetActualType(TypeSystem);
var isIntegral = TypeUtil.Is32BitIntegral(type);
if (isIntegral && (uoe.Operator is JSUnaryMutationOperator)) {
var replacement = DecomposeUnaryMutation(
uoe,
() => TemporaryVariable.ForFunction(
Stack.Last() as JSFunctionExpression, type, FunctionSource
),
type, TypeSystem
);
ParentNode.ReplaceChild(uoe, replacement);
VisitReplacement(replacement);
return;
}
VisitChildren(uoe);
}
public static JSExpression DecomposeUnaryMutation (
JSUnaryOperatorExpression uoe, Func<JSVariable> makeTemporaryVariable, TypeReference type, TypeSystem typeSystem
) {
if (
(uoe.Operator == JSOperator.PreIncrement) ||
(uoe.Operator == JSOperator.PreDecrement)
) {
var assignment = MakeUnaryMutation(
uoe.Expression,
(uoe.Operator == JSOperator.PreDecrement)
? JSOperator.Subtract
: JSOperator.Add,
type, typeSystem
);
return assignment;
} else if (
(uoe.Operator == JSOperator.PostIncrement) ||
(uoe.Operator == JSOperator.PostDecrement)
) {
// FIXME: Terrible hack
var tempVariable = makeTemporaryVariable();
var makeTempCopy = new JSBinaryOperatorExpression(
JSOperator.Assignment, tempVariable, uoe.Expression, type
);
var assignment = MakeUnaryMutation(
uoe.Expression,
(uoe.Operator == JSOperator.PostDecrement)
? JSOperator.Subtract
: JSOperator.Add,
type, typeSystem
);
var comma = new JSCommaExpression(
makeTempCopy,
assignment,
tempVariable
);
return comma;
} else {
throw new NotImplementedException("Unary mutation not supported: " + uoe);
}
}
}
}