OILS / mycpp / datalog / dataflow.dl View on Github | oilshell.org

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