-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheSignatures.cs
More file actions
371 lines (304 loc) · 14.9 KB
/
Copy pathCacheSignatures.cs
File metadata and controls
371 lines (304 loc) · 14.9 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSIL.Ast;
using JSIL.Internal;
using Mono.Cecil;
namespace JSIL.Transforms {
public class SignatureCacher : JSAstVisitor {
public struct CachedSignatureRecord {
public readonly MethodReference Method;
public readonly MethodSignature Signature;
public readonly bool IsConstructor;
public CachedSignatureRecord (MethodReference method, MethodSignature signature, bool isConstructor) {
Method = method;
Signature = signature;
IsConstructor = isConstructor;
}
public override int GetHashCode () {
return Method.Name.GetHashCode() ^ Signature.GetHashCode() ^ IsConstructor.GetHashCode();
}
public bool Equals (ref CachedSignatureRecord rhs) {
var result =
(
(Method == rhs.Method) ||
(Method.FullName == rhs.Method.FullName)
) &&
Signature.Equals(rhs.Signature) &&
(IsConstructor == rhs.IsConstructor);
if (!result)
return false;
else
return result;
}
public override bool Equals (object obj) {
if (obj is CachedSignatureRecord) {
var rhs = (CachedSignatureRecord)obj;
return Equals(ref rhs);
} else
return base.Equals(obj);
}
}
public class CachedSignatureRecordComparer : IEqualityComparer<CachedSignatureRecord> {
public bool Equals (CachedSignatureRecord x, CachedSignatureRecord y) {
return x.Equals(ref y);
}
public int GetHashCode (CachedSignatureRecord obj) {
return obj.GetHashCode();
}
}
public struct CachedInterfaceMemberRecord {
public readonly TypeReference InterfaceType;
public readonly string InterfaceMember;
public CachedInterfaceMemberRecord (TypeReference declaringType, string memberName) {
InterfaceType = declaringType;
InterfaceMember = memberName;
}
public bool Equals (ref CachedInterfaceMemberRecord rhs) {
var result =
TypeUtil.TypesAreEqual(InterfaceType, rhs.InterfaceType, true) &&
InterfaceMember.Equals(rhs.InterfaceMember);
if (!result)
return false;
else
return result;
}
public override bool Equals (object obj) {
if (obj is CachedInterfaceMemberRecord) {
var rhs = (CachedInterfaceMemberRecord)obj;
return Equals(ref rhs);
} else
return base.Equals(obj);
}
public override int GetHashCode () {
return InterfaceType.Name.GetHashCode() ^ InterfaceMember.GetHashCode();
}
}
public class CachedInterfaceMemberRecordComparer : IEqualityComparer<CachedInterfaceMemberRecord> {
public bool Equals (CachedInterfaceMemberRecord x, CachedInterfaceMemberRecord y) {
return x.Equals(ref y);
}
public int GetHashCode (CachedInterfaceMemberRecord obj) {
return obj.GetHashCode();
}
}
public class CacheSet {
private static readonly CachedSignatureRecordComparer Comparer = new CachedSignatureRecordComparer();
private static readonly CachedInterfaceMemberRecordComparer InterfaceMemberComparer = new CachedInterfaceMemberRecordComparer();
public readonly Dictionary<CachedSignatureRecord, int> Signatures;
public readonly Dictionary<CachedInterfaceMemberRecord, int> InterfaceMembers;
public CacheSet () {
Signatures = new Dictionary<CachedSignatureRecord, int>(Comparer);
InterfaceMembers = new Dictionary<CachedInterfaceMemberRecord, int>(InterfaceMemberComparer);
}
}
public readonly bool LocalCachingEnabled;
public readonly Dictionary<MemberIdentifier, CacheSet> LocalCachedSets;
public readonly CacheSet Global = new CacheSet();
private readonly Stack<JSFunctionExpression> FunctionStack = new Stack<JSFunctionExpression>();
public SignatureCacher (TypeInfoProvider typeInfo, bool localCachingEnabled) {
LocalCachedSets = new Dictionary<MemberIdentifier, CacheSet>(
new MemberIdentifier.Comparer(typeInfo)
);
VisitNestedFunctions = true;
LocalCachingEnabled = localCachingEnabled;
}
private CacheSet GetCacheSet (bool cacheLocally) {
CacheSet result = Global;
if (cacheLocally && LocalCachingEnabled) {
var fn = FunctionStack.Peek();
if ((fn.Method == null) || (fn.Method.Method == null))
return Global;
var functionIdentifier = fn.Method.Method.Identifier;
if (!LocalCachedSets.TryGetValue(functionIdentifier, out result))
result = LocalCachedSets[functionIdentifier] = new CacheSet();
}
return result;
}
private void CacheSignature (MethodReference method, MethodSignature signature, bool isConstructor) {
Func<GenericParameter, bool> filter =
(gp) => {
// If the generic parameter can be expanded given the type that declared the method, don't cache locally.
var resolved = MethodSignature.ResolveGenericParameter(gp, method.DeclaringType);
// Note that we have to ensure the resolved type is not generic either. A generic parameter can resolve to a
// *different* generic parameter, and that is still correct - i.e. SomeMethod<A> calls SomeMethod<B>,
// in that case resolving B will yield A.
if ((resolved != gp) && (resolved != null) && !TypeUtil.IsOpenType(resolved))
return false;
var ownerMethod = gp.Owner as MethodReference;
if (ownerMethod == null)
return true;
if (ownerMethod == method)
return false;
// FIXME: Nulls?
else if (ownerMethod.Resolve() == method.Resolve())
return false;
else
return true;
};
var cacheLocally = false;
if (TypeUtil.IsOpenType(signature.ReturnType, filter))
cacheLocally = true;
else if (signature.ParameterTypes.Any((gp) => TypeUtil.IsOpenType(gp, filter)))
cacheLocally = true;
else if (TypeUtil.IsOpenType(method.DeclaringType, filter))
cacheLocally = true;
var set = GetCacheSet(cacheLocally);
var record = new CachedSignatureRecord(method, signature, isConstructor);
if (!set.Signatures.ContainsKey(record))
set.Signatures.Add(record, set.Signatures.Count);
}
private void CacheInterfaceMember (TypeReference declaringType, string memberName) {
var cacheLocally = false;
if (TypeUtil.IsOpenType(declaringType))
cacheLocally = true;
var unexpandedType = declaringType;
if (!TypeUtil.ExpandPositionalGenericParameters(unexpandedType, out declaringType))
declaringType = unexpandedType;
var set = GetCacheSet(cacheLocally);
var record = new CachedInterfaceMemberRecord(declaringType, memberName);
if (!set.InterfaceMembers.ContainsKey(record))
set.InterfaceMembers.Add(record, set.InterfaceMembers.Count);
}
private JSRawOutputIdentifier MakeRawOutputIdentifierForIndex (TypeReference type, int index, bool isSignature) {
if (isSignature)
return new JSRawOutputIdentifier(
type,
"$s{0:X2}", index
);
else
return new JSRawOutputIdentifier(
type,
"$im{0:X2}", index
);
}
public void VisitNode (JSFunctionExpression fe) {
FunctionStack.Push(fe);
try {
VisitChildren(fe);
} finally {
var functionIdentifier = fe.Method.Method.Identifier;
CacheSet localSet;
if (LocalCachedSets.TryGetValue(functionIdentifier, out localSet)) {
var trType = fe.Method.Reference.Module.TypeSystem.SystemType();
int i = 0;
foreach (var kvp in localSet.Signatures) {
var record = kvp.Key;
var stmt = new JSVariableDeclarationStatement(new JSBinaryOperatorExpression(
JSOperator.Assignment,
MakeRawOutputIdentifierForIndex(trType, kvp.Value, true),
new JSLocalCachedSignatureExpression(trType, record.Method, record.Signature, record.IsConstructor),
trType
));
fe.Body.Statements.Insert(i++, stmt);
}
foreach (var kvp in localSet.InterfaceMembers) {
var record = kvp.Key;
var stmt = new JSVariableDeclarationStatement(new JSBinaryOperatorExpression(
JSOperator.Assignment,
MakeRawOutputIdentifierForIndex(trType, kvp.Value, false),
new JSLocalCachedInterfaceMemberExpression(trType, record.InterfaceType, record.InterfaceMember),
trType
));
fe.Body.Statements.Insert(i++, stmt);
}
}
FunctionStack.Pop();
}
}
public void VisitNode(JSMethodOfExpression methodOf)
{
CacheSignature(methodOf.Reference, methodOf.Method.Signature, false);
}
public void VisitNode (JSInvocationExpression invocation) {
var jsm = invocation.JSMethod;
MethodInfo method = null;
if (jsm != null)
method = jsm.Method;
bool isOverloaded = (method != null) &&
method.IsOverloadedRecursive &&
!method.Metadata.HasAttribute("JSIL.Meta.JSRuntimeDispatch");
if (isOverloaded && JavascriptAstEmitter.CanUseFastOverloadDispatch(method))
isOverloaded = false;
if ((method != null) && method.DeclaringType.IsInterface) {
// HACK
if (!PackedArrayUtil.IsPackedArrayType(jsm.Reference.DeclaringType))
CacheInterfaceMember(jsm.Reference.DeclaringType, jsm.Identifier);
}
if ((jsm != null) && (method != null) && isOverloaded)
CacheSignature(jsm.Reference, method.Signature, false);
VisitChildren(invocation);
}
public void VisitNode (JSNewExpression newexp) {
var ctor = newexp.Constructor;
var isOverloaded = (ctor != null) &&
ctor.IsOverloadedRecursive &&
!ctor.Metadata.HasAttribute("JSIL.Meta.JSRuntimeDispatch");
// New improved ConstructorSignature.Construct is faster than fast overload dispatch! :)
/*
if (isOverloaded && JavascriptAstEmitter.CanUseFastOverloadDispatch(ctor))
isOverloaded = false;
*/
if (isOverloaded)
CacheSignature(newexp.ConstructorReference, ctor.Signature, true);
VisitChildren(newexp);
}
public void CacheSignaturesForFunction (JSFunctionExpression function) {
Visit(function);
}
/// <summary>
/// Writes a method signature to the output.
/// </summary>
public void WriteSignatureToOutput (
JavascriptFormatter output, JSFunctionExpression enclosingFunction,
MethodReference methodReference, MethodSignature methodSignature,
TypeReferenceContext referenceContext,
bool forConstructor
) {
int index;
var record = new CachedSignatureRecord(methodReference, methodSignature, forConstructor);
if ((enclosingFunction.Method != null) && (enclosingFunction.Method.Method != null)) {
var functionIdentifier = enclosingFunction.Method.Method.Identifier;
CacheSet localSignatureSet;
if (LocalCachedSets.TryGetValue(functionIdentifier, out localSignatureSet)) {
if (localSignatureSet.Signatures.TryGetValue(record, out index)) {
output.WriteRaw("$s{0:X2}", index);
return;
}
}
}
if (!Global.Signatures.TryGetValue(record, out index))
output.Signature(methodReference, methodSignature, referenceContext, forConstructor, true);
else
output.WriteRaw("$S{0:X2}()", index);
}
/// <summary>
/// Writes an interface member reference to the output.
/// </summary>
public void WriteInterfaceMemberToOutput (
JavascriptFormatter output, Compiler.Extensibility.IAstEmitter astEmitter, JSFunctionExpression enclosingFunction,
JSMethod jsMethod, JSExpression method,
TypeReferenceContext referenceContext
) {
int index;
var record = new CachedInterfaceMemberRecord(jsMethod.Reference.DeclaringType, jsMethod.Identifier);
if ((enclosingFunction.Method != null) || (enclosingFunction.Method.Method != null)) {
var functionIdentifier = enclosingFunction.Method.Method.Identifier;
CacheSet localSignatureSet;
if (LocalCachedSets.TryGetValue(functionIdentifier, out localSignatureSet)) {
if (localSignatureSet.InterfaceMembers.TryGetValue(record, out index)) {
output.WriteRaw("$im{0:X2}", index);
return;
}
}
}
if (!Global.InterfaceMembers.TryGetValue(record, out index)) {
output.Identifier(jsMethod.Reference.DeclaringType, referenceContext, false);
output.Dot();
astEmitter.Emit(method);
} else
output.WriteRaw("$IM{0:X2}()", index);
}
}
}