-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionCache.cs
More file actions
334 lines (275 loc) · 12.7 KB
/
Copy pathFunctionCache.cs
File metadata and controls
334 lines (275 loc) · 12.7 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using JSIL.Ast;
using JSIL.Internal;
using JSIL.Threading;
using JSIL.Transforms;
using Mono.Cecil;
namespace JSIL {
public interface IFunctionSource {
JSFunctionExpression GetExpression (QualifiedMemberIdentifier method);
FunctionAnalysis1stPass GetFirstPass (QualifiedMemberIdentifier method, QualifiedMemberIdentifier forMethod);
FunctionAnalysis2ndPass GetSecondPass (JSMethod method, QualifiedMemberIdentifier forMethod);
void InvalidateFirstPass (QualifiedMemberIdentifier method);
void InvalidateSecondPass (QualifiedMemberIdentifier method);
}
public class FunctionCache : IFunctionSource, IDisposable {
public class Entry {
public readonly TrackedLock StaticAnalysisDataLock;
public readonly QualifiedMemberIdentifier Identifier;
public MethodInfo Info;
public MethodReference Reference;
public SpecialIdentifiers SpecialIdentifiers;
public JSFunctionExpression Expression;
public FunctionAnalysis1stPass FirstPass;
public FunctionAnalysis2ndPass SecondPass;
public volatile bool TransformPipelineHasCompleted = false;
public MethodDefinition Definition {
get {
return Info.Member;
}
}
public Entry (QualifiedMemberIdentifier identifier, TrackedLockCollection lockCollection) {
Identifier = identifier;
StaticAnalysisDataLock = new TrackedLock(lockCollection, () => String.Format("{0}", this.Identifier.ToString()));
}
}
protected struct PopulatedCacheEntryArgs {
public MethodInfo Info;
public MethodReference Method;
public ILBlockTranslator Translator;
public JSVariable[] Parameters;
public JSBlockStatement Body;
}
protected struct NullCacheEntryArgs {
public MethodInfo Info;
public MethodReference Method;
}
public readonly ITypeInfoSource TypeInfo;
public readonly MethodTypeFactory MethodTypes;
public readonly ConcurrentHashQueue<QualifiedMemberIdentifier> PendingTransformsQueue;
public readonly ConcurrentDictionary<QualifiedMemberIdentifier, FunctionTransformPipeline> ActiveTransformPipelines;
protected readonly ConcurrentCache<QualifiedMemberIdentifier, Entry> Cache;
protected readonly ConcurrentCache<QualifiedMemberIdentifier, Entry>.CreatorFunction<JSMethod> MakeCacheEntry;
protected readonly ConcurrentCache<QualifiedMemberIdentifier, Entry>.CreatorFunction<PopulatedCacheEntryArgs> MakePopulatedCacheEntry;
protected readonly ConcurrentCache<QualifiedMemberIdentifier, Entry>.CreatorFunction<NullCacheEntryArgs> MakeNullCacheEntry;
protected readonly QualifiedMemberIdentifier.Comparer Comparer;
protected readonly TrackedLockCollection Locks = new TrackedLockCollection();
public FunctionCache (ITypeInfoSource typeInfo) {
TypeInfo = typeInfo;
Comparer = new QualifiedMemberIdentifier.Comparer(typeInfo);
Cache = new ConcurrentCache<QualifiedMemberIdentifier, Entry>(
Environment.ProcessorCount, 4096, Comparer
);
PendingTransformsQueue = new ConcurrentHashQueue<QualifiedMemberIdentifier>(
Math.Max(1, Environment.ProcessorCount / 4), 4096, Comparer
);
ActiveTransformPipelines = new ConcurrentDictionary<QualifiedMemberIdentifier, FunctionTransformPipeline>(
Math.Max(1, Environment.ProcessorCount / 4), 128, Comparer
);
MethodTypes = new MethodTypeFactory();
MakeCacheEntry = (id, method) => {
PendingTransformsQueue.TryEnqueue(id);
return new Entry(id, Locks) {
Info = method.Method,
Reference = method.Reference,
SecondPass = new FunctionAnalysis2ndPass(this, method.Method)
};
};
MakePopulatedCacheEntry = (id, args) => {
var result = new JSFunctionExpression(
new JSMethod(args.Method, args.Info, MethodTypes),
args.Translator.Variables,
args.Parameters,
args.Body,
MethodTypes
);
PendingTransformsQueue.TryEnqueue(id);
return new Entry(id, Locks) {
Info = args.Info,
Reference = args.Method,
Expression = result,
SpecialIdentifiers = args.Translator.SpecialIdentifiers
};
};
MakeNullCacheEntry = (id, args) =>
new Entry(id, Locks) {
Info = args.Info,
Reference = args.Method,
Expression = null
};
}
public bool TryGetExpression (QualifiedMemberIdentifier method, out JSFunctionExpression function) {
Entry entry;
if (!Cache.TryGet(method, out entry)) {
function = null;
return false;
}
function = entry.Expression;
return true;
}
public Entry GetCacheEntry (QualifiedMemberIdentifier method, bool throwOnFail = true) {
Entry entry;
if (!Cache.TryGet(method, out entry)) {
if (throwOnFail)
throw new KeyNotFoundException("No cache entry for method '" + method + "'.");
else
return null;
}
return entry;
}
public JSFunctionExpression GetExpression (QualifiedMemberIdentifier method) {
var entry = GetCacheEntry(method);
return entry.Expression;
}
private FunctionAnalysis1stPass _GetOrCreateFirstPass (Entry entry) {
if (entry.FirstPass == null) {
var analyzer = new StaticAnalyzer(entry.Definition.Module.TypeSystem, this);
entry.FirstPass = analyzer.FirstPass(entry.Identifier, entry.Expression);
}
return entry.FirstPass;
}
private static bool TryAcquireStaticAnalysisDataLock (Entry entry, QualifiedMemberIdentifier method) {
const int lockTimeoutMs = 33;
var result = entry.StaticAnalysisDataLock.TryBlockingEnter(recursive: true, timeoutMs: lockTimeoutMs);
if (!result.Success) {
if (result.FailureReason == TrackedLockFailureReason.Deadlock)
throw new StaticAnalysisDataTemporarilyUnavailableException(method);
else
return false;
} else {
// Detect too-deep recursion and abort.
if (entry.StaticAnalysisDataLock.RecursionDepth > 1) {
entry.StaticAnalysisDataLock.Exit();
return false;
}
}
return true;
}
public FunctionAnalysis1stPass GetFirstPass (QualifiedMemberIdentifier method, QualifiedMemberIdentifier forCaller) {
var entry = GetCacheEntry(method, false);
if ((entry == null) || (entry.Expression == null))
return null;
if (!TryAcquireStaticAnalysisDataLock(entry, method))
return null;
try {
return _GetOrCreateFirstPass(entry);
} finally {
entry.StaticAnalysisDataLock.Exit();
}
}
private FunctionAnalysis2ndPass CreateSecondPassForKnownMethod (FunctionAnalysis1stPass firstPass) {
return new FunctionAnalysis2ndPass(this, firstPass, true);
}
private FunctionAnalysis2ndPass CreateSecondPassForOverridableMethod (MethodInfo method, FunctionAnalysis1stPass firstPass) {
// FIXME: Existing code is probably wrong in terms of static analysis for virtual method calls. Welp.
return new FunctionAnalysis2ndPass(this, firstPass, false);
}
private FunctionAnalysis2ndPass CreateSecondPassForAbstractMethod (MethodInfo method) {
return new FunctionAnalysis2ndPass(this, method);
}
private FunctionAnalysis2ndPass _GetOrCreateSecondPass (Entry entry) {
if (
(entry.SecondPass == null) &&
(entry.Expression != null)
) {
if (
entry.Definition.IsAbstract ||
// HACK: Fixes ExpressionsExecution and ExpressionsTest???
(entry.FirstPass == null)
)
entry.SecondPass = CreateSecondPassForAbstractMethod(entry.Info);
else if (entry.Definition.IsVirtual)
entry.SecondPass = CreateSecondPassForOverridableMethod(entry.Info, entry.FirstPass);
else
entry.SecondPass = CreateSecondPassForKnownMethod(entry.FirstPass);
}
return entry.SecondPass;
}
public FunctionAnalysis2ndPass GetSecondPass (JSMethod method, QualifiedMemberIdentifier forCaller) {
if (method == null)
return null;
var id = method.QualifiedIdentifier;
Entry entry = Cache.GetOrCreate(
id, method, MakeCacheEntry
);
if (entry == null)
return null;
GetFirstPass(id, forCaller);
if (!TryAcquireStaticAnalysisDataLock(entry, method.QualifiedIdentifier))
return null;
try {
return _GetOrCreateSecondPass(entry);
} finally {
entry.StaticAnalysisDataLock.Exit();
}
}
public void InvalidateFirstPass (QualifiedMemberIdentifier method) {
Entry entry;
if (!Cache.TryGet(method, out entry))
throw new KeyNotFoundException("No cache entry for method '" + method + "'.");
entry.StaticAnalysisDataLock.BlockingEnter(recursive: true);
entry.FirstPass = null;
entry.SecondPass = null;
entry.StaticAnalysisDataLock.Exit();
}
public void InvalidateSecondPass (QualifiedMemberIdentifier method) {
Entry entry;
if (!Cache.TryGet(method, out entry))
throw new KeyNotFoundException("No cache entry for method '" + method + "'.");
entry.StaticAnalysisDataLock.BlockingEnter(recursive: true);
entry.SecondPass = null;
entry.StaticAnalysisDataLock.Exit();
}
internal JSFunctionExpression Create (
MethodInfo info, MethodDefinition methodDef, MethodReference method,
QualifiedMemberIdentifier identifier, ILBlockTranslator translator,
JSVariable[] parameters, JSBlockStatement body
) {
var args = new PopulatedCacheEntryArgs {
Info = info,
Method = method,
Translator = translator,
Parameters = parameters,
Body = body,
};
return Cache.GetOrCreate(identifier, args, MakePopulatedCacheEntry).Expression;
}
internal void CreateNull (
MethodInfo info, MethodReference method,
QualifiedMemberIdentifier identifier
) {
var args = new NullCacheEntryArgs {
Info = info,
Method = method
};
Cache.TryCreate(identifier, args, MakeNullCacheEntry);
}
public void Dispose () {
Cache.Dispose();
PendingTransformsQueue.Clear();
MethodTypes.Dispose();
}
}
public abstract class TemporarilySuspendTransformPipelineException : Exception {
public readonly QualifiedMemberIdentifier Identifier;
protected TemporarilySuspendTransformPipelineException (QualifiedMemberIdentifier identifier) {
Identifier = identifier;
}
}
public class StaticAnalysisDataTemporarilyUnavailableException : TemporarilySuspendTransformPipelineException {
public StaticAnalysisDataTemporarilyUnavailableException (QualifiedMemberIdentifier identifier)
: base (identifier) {
}
public override string Message {
get {
return String.Format("Static analysis data for the function '{0}' is temporarily unavailable because the function is being transformed. Please re-run this transform later.", Identifier);
}
}
}
}