This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Cell.cs
534 lines (431 loc) · 17.1 KB
/
Cell.cs
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//
// Storage cells (l-values) in intermediate language
//
using System;
using Microsoft.LiveLabs.Extras;
using Microsoft.LiveLabs.JavaScript.JST;
using JST = Microsoft.LiveLabs.JavaScript.JST;
namespace Microsoft.LiveLabs.CST
{
public enum CellFlavor
{
Variable,
Field,
Element,
Box,
StatePCPseudo
}
// ----------------------------------------------------------------------
// Cell
// ----------------------------------------------------------------------
public abstract class Cell : IEquatable<Cell>
{
[CanBeNull]
public readonly Location Loc;
public abstract CellFlavor Flavor { get; }
// Identifier of variable cell, otherwise null
public abstract JST.Identifier IsVariable { get; }
// True if cell contents are fixed
public abstract bool IsReadOnly(CompilationEnvironment env);
// True if cell can be determined without any "significant" computational work.
// WARNING: However, it may have read effects (but never write or exception effects).
public abstract bool IsCheap { get; }
public abstract TypeRef Type(CompilationEnvironment compEnv);
public abstract void AccumUsage(CompilationEnvironment compEnv, bool isAlwaysUsed, Usage usage, bool asPointer);
public abstract void AccumEffects(EffectsContext fxCtxt, CallContext callCtxt, EvalTimes evalTimes);
public abstract void AccumLvalueEffects(EffectsContext fxCtxt);
public abstract Cell Simplify(SimplifierContext ctxt);
public abstract void Append(CSTWriter w);
public Expression AddressOf()
{
return new AddressOfExpression(this);
}
public Expression Read()
{
return new ReadExpression(new AddressOfExpression(this));
}
public Expression Write(Expression value)
{
return new WriteExpression(new AddressOfExpression(this), value);
}
public override string ToString()
{
return CSTWriter.WithAppendDebug(Append);
}
public bool Equals(Cell other)
{
return Flavor == other.Flavor && EqualBody(other);
}
public override bool Equals(object obj)
{
var other = obj as Cell;
return other != null && Equals(other);
}
public override int GetHashCode()
{
throw new InvalidOperationException("derived type must override");
}
protected abstract bool EqualBody(Cell other);
}
// ----------------------------------------------------------------------
// VariableCell
// ----------------------------------------------------------------------
public class VariableCell : Cell
{
[NotNull]
public readonly JST.Identifier Id;
public VariableCell(JST.Identifier id)
{
Id = id;
}
public override CellFlavor Flavor { get { return CellFlavor.Variable; } }
public override JST.Identifier IsVariable { get { return Id; } }
public override bool IsReadOnly(CompilationEnvironment compEnv) { return compEnv.Variable(Id).IsReadOnly; }
// The cell itself is a value, however the contents may be changing
public override bool IsCheap { get { return true; } }
public override TypeRef Type(CompilationEnvironment compEnv)
{
return compEnv.Variable(Id).Type;
}
public override void AccumUsage(CompilationEnvironment compEnv, bool isAlwaysUsed, Usage usage, bool asPointer)
{
usage.SeenVariable(Id, isAlwaysUsed);
if (asPointer)
usage.SeenVariablePointer(Id, isAlwaysUsed);
}
public override void AccumEffects(EffectsContext fxCtxt, CallContext callCtxt, EvalTimes evalTimes)
{
if (callCtxt != null && callCtxt.IsOk)
{
var idx = default(int);
if (callCtxt.Parameters.TryGetValue(Id, out idx))
{
if (callCtxt.SeenParameters[idx].HasValue)
{
if (callCtxt.SeenParameters[idx].Value)
// More than once syntactic occurence of this parameter
callCtxt.Fail();
// Remember we've seen a syntactic occurence of this parameter
callCtxt.SeenParameters[idx] = true;
if (!callCtxt.AllReadOnly)
{
for (var i = 0; i < idx; i++)
{
if (callCtxt.SeenParameters[i].HasValue && !callCtxt.SeenParameters[i].Value)
// Evaluating this parameter before earlier parameters
callCtxt.Fail();
}
}
switch (evalTimes.Value)
{
case EvalTimesEnum.Once:
break;
case EvalTimesEnum.Opt:
if (!callCtxt.AllReadOnly)
// May not always evaluate argument
callCtxt.Fail();
break;
case EvalTimesEnum.AtLeastOnce:
case EvalTimesEnum.Any:
// May evaluate argument more than once
callCtxt.Fail();
break;
default:
throw new ArgumentOutOfRangeException("evalTimes");
}
if (!fxCtxt.AccumEffects.CommutableWith(callCtxt.ArgumentEffects[idx]))
// Cannot defer evaluation of argument to occurance of parameter
callCtxt.Fail();
}
// else: argument is a value, so no constraints on occurences of parameter
}
}
if (!fxCtxt.IsHidden(Id))
fxCtxt.IncludeEffects(JST.Effects.Read(Id));
}
public override void AccumLvalueEffects(EffectsContext fxCtxt)
{
if (!fxCtxt.IsHidden(Id))
fxCtxt.IncludeEffects(JST.Effects.Write(Id));
}
public override Cell Simplify(SimplifierContext ctxt)
{
return ctxt.ApplyCell(Id);
}
public override void Append(CSTWriter w)
{
w.AppendName(Id.Value);
}
protected override bool EqualBody(Cell other)
{
var temp = (VariableCell)other;
return Id.Equals(temp.Id);
}
public override int GetHashCode()
{
var res = 0x08ba4799u;
res ^= (uint)Id.GetHashCode();
return (int)res;
}
}
// ----------------------------------------------------------------------
// FieldCell
// ----------------------------------------------------------------------
public class FieldCell : Cell
{
[CanBeNull]
public readonly Expression Object; // null <=> static field
[NotNull]
public readonly FieldRef Field;
public FieldCell(Expression obj, FieldRef field)
{
Object = obj;
Field = field;
}
public override CellFlavor Flavor { get { return CellFlavor.Field; } }
public override JST.Identifier IsVariable { get { return null; } }
public override bool IsReadOnly(CompilationEnvironment env) { return false; }
public override bool IsCheap { get { return Object == null || Object.IsCheap; } }
public override TypeRef Type(CompilationEnvironment compEnv)
{
var fieldEnv = Field.Enter(compEnv);
return fieldEnv.SubstituteType(fieldEnv.Field.FieldType);
}
public override void AccumUsage(CompilationEnvironment compEnv, bool isAlwaysUsed, Usage usage, bool asPointer)
{
if (Object != null)
Object.AccumUsage(compEnv, isAlwaysUsed, usage, false);
usage.SeenField(Field, isAlwaysUsed);
}
public override void AccumEffects(EffectsContext fxCtxt, CallContext callCtxt, EvalTimes evalTimes)
{
if (Object != null)
Object.AccumEffects(fxCtxt, callCtxt, evalTimes);
fxCtxt.IncludeEffects(JST.Effects.ReadHeap);
}
public override void AccumLvalueEffects(EffectsContext fxCtxt)
{
fxCtxt.IncludeEffects(JST.Effects.WriteHeap);
}
public override Cell Simplify(SimplifierContext ctxt)
{
if (Object == null)
return this;
else
{
var simpObject = Object.Simplify(ctxt);
return new FieldCell(simpObject, Field);
}
}
public override void Append(CSTWriter w)
{
if (Object == null)
Field.DefiningType.Append(w);
else
Object.Append(w, 1);
w.Append('.');
w.AppendName(Field.Name);
}
protected override bool EqualBody(Cell other)
{
var field = (FieldCell)other;
if (Object == null && field.Object != null || Object != null && field.Object == null)
return false;
if (Object != null && !Object.Equals(field.Object))
return false;
return Field.Equals(field.Field);
}
public override int GetHashCode()
{
var res = 0xfb1fa3ccu;
if (Object != null)
res = Constants.Rot3(res) ^ (uint)Object.GetHashCode();
res = Constants.Rot3(res) ^ (uint)Field.GetHashCode();
return (int)res;
}
}
// ----------------------------------------------------------------------
// ElementCell
// ----------------------------------------------------------------------
public class ElementCell : Cell
{
[NotNull]
public readonly Expression Array;
[NotNull]
public readonly Expression Index;
public readonly bool isReadOnly;
public ElementCell(Expression array, Expression index, bool isReadOnly)
{
Array = array;
Index = index;
this.isReadOnly = isReadOnly;
}
public override CellFlavor Flavor { get { return CellFlavor.Element; } }
public override JST.Identifier IsVariable { get { return null; } }
// NOTE: Even if this cell is read-only, underlying array may be mutated, thus return false.
public override bool IsReadOnly(CompilationEnvironment compEnv) { return false; }
// Array indexing may throw
public override bool IsCheap { get { return false; } }
public override TypeRef Type(CompilationEnvironment compEnv)
{
var arr = Array.Type(compEnv);
return arr.Arguments[0];
}
public override void AccumUsage(CompilationEnvironment compEnv, bool isAlwaysUsed, Usage usage, bool asPointer)
{
Array.AccumUsage(compEnv, isAlwaysUsed, usage, false);
Index.AccumUsage(compEnv, isAlwaysUsed, usage, false);
}
public override void AccumEffects(EffectsContext fxCtxt, CallContext callCtxt, EvalTimes evalTimes)
{
Array.AccumEffects(fxCtxt, callCtxt, evalTimes);
Index.AccumEffects(fxCtxt, callCtxt, evalTimes);
fxCtxt.IncludeEffects(JST.Effects.ReadHeap);
}
public override void AccumLvalueEffects(EffectsContext fxCtxt)
{
fxCtxt.IncludeEffects(JST.Effects.WriteHeap);
}
public override Cell Simplify(SimplifierContext ctxt)
{
var simpArray = Array.Simplify(ctxt);
var simpIndex = Index.Simplify(ctxt);
return new ElementCell(simpArray, simpIndex, isReadOnly);
}
public override void Append(CSTWriter w)
{
Array.Append(w, 1);
w.Append('[');
Index.Append(w, 0);
w.Append(']');
}
protected override bool EqualBody(Cell other)
{
var elem = (ElementCell)other;
return Array.Equals(elem.Array) && Index.Equals(elem.Index) && isReadOnly == elem.isReadOnly;
}
public override int GetHashCode()
{
var res = isReadOnly ? 0x8ea5e9f8u : 0xdb3222f8u;
res ^= (uint)Array.GetHashCode();
res = Constants.Rot3(res) ^ (uint)Index.GetHashCode();
return (int)res;
}
}
// ----------------------------------------------------------------------
// BoxCell
// ----------------------------------------------------------------------
public class BoxCell : Cell
{
[NotNull]
public readonly Expression Box;
[NotNull]
public readonly TypeRef ValueType;
public BoxCell(Expression box, TypeRef valueType)
{
Box = box;
ValueType = valueType;
}
public override CellFlavor Flavor { get { return CellFlavor.Box; } }
public override JST.Identifier IsVariable { get { return null; } }
public override bool IsReadOnly(CompilationEnvironment compEnv) { return true; }
// Unboxing may throw
public override bool IsCheap { get { return false; } }
public override TypeRef Type(CompilationEnvironment compEnv)
{
return compEnv.SubstituteType(ValueType);
}
public override void AccumUsage(CompilationEnvironment compEnv, bool isAlwaysUsed, Usage usage, bool asPointer)
{
Box.AccumUsage(compEnv, isAlwaysUsed, usage, false);
}
public override void AccumEffects(EffectsContext fxCtxt, CallContext callCtxt, EvalTimes evalTimes)
{
Box.AccumEffects(fxCtxt, callCtxt, evalTimes);
fxCtxt.IncludeEffects(JST.Effects.ReadHeap);
}
public override void AccumLvalueEffects(EffectsContext fxCtxt)
{
fxCtxt.IncludeEffects(JST.Effects.WriteHeap);
}
public override Cell Simplify(SimplifierContext ctxt)
{
var simpBox = Box.Simplify(ctxt);
return new BoxCell(simpBox, ValueType);
}
public override void Append(CSTWriter w)
{
w.Append("box(");
Box.Append(w, 0);
w.Append(':');
ValueType.Append(w);
w.Append(')');
}
protected override bool EqualBody(Cell other)
{
var box = (BoxCell)other;
return Box.Equals(box.Box) && ValueType.Equals(box.ValueType);
}
public override int GetHashCode()
{
var res = 0x3c7516dfu;
res ^= (uint)Box.GetHashCode();
res = Constants.Rot3(res) ^ (uint)ValueType.GetHashCode();
return (int)res;
}
}
// ----------------------------------------------------------------------
// StatePCPseudoCell
// ----------------------------------------------------------------------
// Represents cell of current basic block id within state structure
public class StatePCPseudoCell : Cell
{
[NotNull]
public readonly JST.Identifier StateId; // temporary holding machine state
public StatePCPseudoCell(JST.Identifier stateId)
{
StateId = stateId;
}
public override CellFlavor Flavor { get { return CellFlavor.StatePCPseudo; } }
public override bool IsReadOnly(CompilationEnvironment compEnv) { return false; }
public override bool IsCheap { get { return true; } }
public override JST.Identifier IsVariable { get { return null; } }
public override void AccumUsage(CompilationEnvironment compEnv, bool isAlwaysUsed, Usage usage, bool asPointer)
{
usage.SeenVariable(StateId, isAlwaysUsed);
}
public override void AccumEffects(EffectsContext fxCtxt, CallContext callCtxt, EvalTimes evalTimes)
{
fxCtxt.IncludeEffects(JST.Effects.Read(StateId));
fxCtxt.IncludeEffects(JST.Effects.ReadHeap);
}
public override void AccumLvalueEffects(EffectsContext fxCtxt)
{
fxCtxt.IncludeEffects(JST.Effects.WriteHeap);
}
public override TypeRef Type(CompilationEnvironment compEnv)
{
return compEnv.Global.Int32Ref;
}
public override Cell Simplify(SimplifierContext ctxt)
{
return this;
}
public override void Append(CSTWriter w)
{
w.Append(StateId.Value);
w.Append(".PC");
}
protected override bool EqualBody(Cell other)
{
var state = (StatePCPseudoCell)other;
return StateId.Equals(state.StateId);
}
public override int GetHashCode()
{
var res = 0x2f2f2218u;
res ^= (uint)StateId.GetHashCode();
return (int)res;
}
}
}