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

63 lines, 47 significant
1.once
2
3.include "control-flow.dl"
4.include "call-graph.dl"
5
6// Types
7// =====
8
9// Objects can be refered to by either local variables or object members.
10.type Reference = LocalVariable { f: Function, v: symbol }
11 | ObjectMember { o: symbol, m: symbol }
12
13.type Value = HeapObject { h: symbol } | Ref { r: Reference } | Empty {}
14
15// Facts and Relations
16// ===================
17// The facts and relations below use live variable analysis to determine when
18// variables need stack roots. See
19// https://en.wikipedia.org/wiki/Live-variable_analysis for more details.
20//
21// A variable is considered *live* at given statement if it might be used by a
22// future statement.
23
24// `f` assigns `v` is assigned to `r` in statement `s`.
25.decl assign(f:Function, s:Statement, r:Reference, v:Value)
26.input assign
27
28// `f` uses `r` in statement `s`.
29.decl use(f:Function, s:Statement, r:Reference)
30.input use
31
32// The set of variables considered live on the way in to a statement.
33.decl live_vars_in(f:Function, s:Statement, r:Reference)
34
35// The set of variables considered live on the way out of a statement.
36.decl live_vars_out(f:Function, s:Statement, r:Reference)
37
38// The set of references that a function should generate stack roots for.
39.decl stack_root_vars(f:Function, r: Reference)
40.output stack_root_vars
41
42// Rules
43// =====
44
45// See the definition of the GEN set at https://en.wikipedia.org/wiki/Live-variable_analysis
46live_vars_in(f, s, r) :- use(f, s, r).
47// See the definition of the KILL set at https://en.wikipedia.org/wiki/Live-variable_analysis
48live_vars_in(f, s, r) :- !assign(f, s, r, _), live_vars_out(f, s, r).
49
50// The set of live variables leaving a statement is the union of the inbound
51// live variables of the statements sucessors in the control flow graph.
52live_vars_out(f, s1, r) :- cf_edge(f, s1, s2), live_vars_in(f, s2, r).
53
54// All variables considered live after a statement that directly invokes the GC
55// must be rooted.
56stack_root_vars(f, r) :- call(f, s, "mylib.MaybeCollect"), live_vars_out(f, s, r).
57
58// If a function invokes the GC, directly or indirectly, all of its heap-managed
59// arguments must be rooted.
60stack_root_vars(f, $LocalVariable(f, v)) :- might_collect(f, _), assign(f, 0, $LocalVariable(f, v), $Empty()).
61
62// All members of context managers must be rooted.
63stack_root_vars(f, $ObjectMember("self", m)) :- match(".*ctx_.*__init__", f), assign(f, _, $ObjectMember("self", m), _).