1 | .once
|
2 |
|
3 | .include "control-flow.dl"
|
4 |
|
5 | // Types
|
6 | // =====
|
7 |
|
8 | // Objects can live in either local variables or object members.
|
9 | .type Place = LocalVariable { f: Function, v: symbol }
|
10 | | ObjectMember { o: symbol, m: symbol }
|
11 |
|
12 | // Places can be referenced by variables and object members.
|
13 | .type Reference = VariableRef { v: symbol }
|
14 | | MemberRef { o: symbol, m: symbol }
|
15 |
|
16 | // Facts and Relations (Inputs)
|
17 | // ============================
|
18 |
|
19 | // Variable (or object) `r` is allocated, or assigned a literal value, in
|
20 | // statement `s` of `f`.
|
21 | .decl define(f:Function, s:Statement, p:Place)
|
22 | .input define
|
23 |
|
24 | // Reference `r` is assigned to a place `p` in statement `s` of `f`.
|
25 | .decl assign(f:Function, s:Statement, p:Place, r:Reference)
|
26 | .input assign
|
27 |
|
28 | // Place `a` is aliased by place `b`.
|
29 | .decl alias(a: Place, b: Place)
|
30 | .output alias
|
31 |
|
32 | // Rules and Outputs
|
33 | // =================
|
34 |
|
35 | // `p` is implicitly defined when it is assigned to.
|
36 | define(f, s, p) :- assign(f, s, p, _).
|
37 |
|
38 | // Simple aliasing with locals.
|
39 | // a = ...
|
40 | // b = a
|
41 | alias($LocalVariable(f, a), $LocalVariable(f, b)) :-
|
42 | define(f, s1, $LocalVariable(f, a)),
|
43 | assign(f, s2, $LocalVariable(f, b), $VariableRef(a)),
|
44 | CFGraph.reachable(f, s1, f, s2).
|
45 |
|
46 | // LocalVariables can also be aliased by object members. Note the special function name
|
47 | // and statement number.
|
48 | // a = ...
|
49 | // b.m = a
|
50 | alias($LocalVariable(f, a), $ObjectMember(b, m)) :-
|
51 | define(f, s1, $LocalVariable(f, a)),
|
52 | assign(f, s2, $ObjectMember(b, m), $VariableRef(a)),
|
53 | CFGraph.reachable(f, s1, f, s2).
|
54 |
|
55 | // Objects members can be aliased too. Note the special function name and
|
56 | // statement number.
|
57 | // a.m = ...
|
58 | // b = a.m
|
59 | alias($ObjectMember(a, m), $LocalVariable(f, b)) :-
|
60 | assign(f, _, $LocalVariable(f, b), $MemberRef(a, m)).
|
61 | // a.m = ...
|
62 | // b.m2 = a.m1
|
63 | alias($ObjectMember(a, m1), $ObjectMember(b, m2)) :-
|
64 | assign(_, _, $ObjectMember(b, m2), $MemberRef(a, m1)).
|
65 |
|
66 | // Inductive case.
|
67 | alias(a, c) :- alias(a, b), alias(b, c).
|