forked from iskiselev/JSIL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsafeCodeTransforms.cs
More file actions
171 lines (147 loc) · 6.73 KB
/
Copy pathUnsafeCodeTransforms.cs
File metadata and controls
171 lines (147 loc) · 6.73 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
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 JSIL.Translator;
using Mono.Cecil;
namespace JSIL.Transforms {
public class UnsafeCodeTransforms : JSAstVisitor {
public readonly Configuration Configuration;
public readonly TypeSystem TypeSystem;
public readonly MethodTypeFactory MethodTypes;
public UnsafeCodeTransforms (
Configuration configuration,
TypeSystem typeSystem,
MethodTypeFactory methodTypes
) {
Configuration = configuration;
TypeSystem = typeSystem;
MethodTypes = methodTypes;
}
public void VisitNode (JSResultReferenceExpression rre) {
var invocation = (rre.Referent as JSInvocationExpression);
// When passing a packed array element directly to a function, use a proxy instead.
// This allows hoisting to operate when the call is inside a loop, and it reduces GC pressure (since we basically make the struct unpack occur on-demand).
if (
(ParentNode is JSInvocationExpression) &&
(invocation != null) &&
(invocation.JSMethod != null) &&
invocation.JSMethod.Reference.FullName.Contains("JSIL.Runtime.IPackedArray") &&
invocation.JSMethod.Reference.FullName.Contains("get_Item(") &&
Configuration.CodeGenerator.AggressivelyUseElementProxies.GetValueOrDefault(false)
) {
var elementType = invocation.GetActualType(TypeSystem);
var replacement = new JSNewPackedArrayElementProxy(
invocation.ThisReference, invocation.Arguments.First(),
elementType
);
ParentNode.ReplaceChild(rre, replacement);
VisitReplacement(replacement);
return;
}
VisitChildren(rre);
}
public void VisitNode (JSReadThroughPointerExpression rtpe) {
JSExpression newPointer, offset;
if (ExtractOffsetFromPointerExpression(rtpe.Pointer, TypeSystem, out newPointer, out offset)) {
var replacement = new JSReadThroughPointerExpression(newPointer, rtpe.ElementType, offset);
ParentNode.ReplaceChild(rtpe, replacement);
VisitReplacement(replacement);
} else {
VisitChildren(rtpe);
}
}
public void VisitNode (JSWriteThroughPointerExpression wtpe) {
JSExpression newPointer, offset;
if (ExtractOffsetFromPointerExpression(wtpe.Left, TypeSystem, out newPointer, out offset)) {
var replacement = new JSWriteThroughPointerExpression(newPointer, wtpe.Right, wtpe.ActualType, offset);
ParentNode.ReplaceChild(wtpe, replacement);
VisitReplacement(replacement);
} else {
VisitChildren(wtpe);
}
}
public void VisitNode (JSPointerAddExpression pae) {
VisitChildren(pae);
}
public void VisitNode (JSPointerDeltaExpression pde) {
VisitChildren(pde);
}
public void VisitNode (JSPointerComparisonExpression pce) {
VisitChildren(pce);
}
public void VisitNode (JSBinaryOperatorExpression boe) {
var leftType = boe.Left.GetActualType(TypeSystem);
var rightType = boe.Right.GetActualType(TypeSystem);
// We can end up with a pointer literal in an arithmetic expression.
// In this case we want to switch it back to a normal integer literal so that the math operations work.
var leftPointer = boe.Left as JSPointerLiteral;
var rightPointer = boe.Right as JSPointerLiteral;
if (!(boe.Operator is JSAssignmentOperator)) {
if (leftPointer != null)
boe.ReplaceChild(boe.Left, JSLiteral.New(leftPointer.Value));
if (rightPointer != null)
boe.ReplaceChild(boe.Right, JSLiteral.New(rightPointer.Value));
}
JSExpression replacement = null;
if (leftType.IsPointer && TypeUtil.IsIntegral(rightType)) {
if (
(boe.Operator == JSOperator.Add) ||
(boe.Operator == JSOperator.AddAssignment)
) {
replacement = new JSPointerAddExpression(
boe.Left, boe.Right,
boe.Operator == JSOperator.AddAssignment
);
} else if (
(boe.Operator == JSOperator.Subtract) ||
(boe.Operator == JSOperator.SubtractAssignment)
) {
// FIXME: Int32 is probably wrong
replacement = new JSPointerAddExpression(
boe.Left,
new JSUnaryOperatorExpression(JSOperator.Negation, boe.Right, TypeSystem.Int32),
boe.Operator == JSOperator.SubtractAssignment
);
}
} else if (leftType.IsPointer && rightType.IsPointer) {
if (boe.Operator == JSOperator.Subtract) {
// FIXME: Int32 is probably wrong
replacement = new JSPointerDeltaExpression(
boe.Left, boe.Right, TypeSystem.Int32
);
} else if (boe.Operator is JSComparisonOperator) {
replacement = new JSPointerComparisonExpression(boe.Operator, boe.Left, boe.Right, boe.ActualType);
}
}
if (replacement != null) {
ParentNode.ReplaceChild(boe, replacement);
VisitReplacement(replacement);
} else {
VisitChildren(boe);
}
}
public static bool ExtractOffsetFromPointerExpression (JSExpression pointer, TypeSystem typeSystem, out JSExpression newPointer, out JSExpression offset) {
offset = null;
newPointer = pointer;
var boe = pointer as JSBinaryOperatorExpression;
if (boe == null)
return false;
var rightType = boe.Right.GetActualType(typeSystem);
var resultType = boe.GetActualType(typeSystem);
if (!resultType.IsPointer)
return false;
if (!TypeUtil.IsIntegral(rightType))
return false;
if (boe.Operator != JSOperator.Add)
return false;
newPointer = boe.Left;
offset = boe.Right;
return true;
}
}
}