-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBy.dl
167 lines (148 loc) · 3.67 KB
/
CBy.dl
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
/*
Handles:
- genuine value origins
- pattern matching
*/
// origin: node or external
.decl NodeOrigin(v:Variable, value:CodeName)
.decl ExternalOrigin(v:Variable, ext_result:Variable, ty_var:Variable)
.output NodeOrigin
.output ExternalOrigin
/*
SECTION: calling a constructor
handle stg style constructor calling:
let con = [tag a b c]
letS val = con $
*/
USED("CBy-1")
NodeOrigin(r, val) :-
REACHABLE(r)
Call(r, f, 0),
EvalMode(r, "strict"),
NodeOrigin(f, val).
// CHECKED
// TODO: better model forcing ; what about externals?
/*
Error(ext, "forced external name"),
Error(r, "forced external") :-
Call(r, f, 0),
EvalMode(r, "strict"),
!NodeOrigin(f,_),
!PNode(f, _, _, _),
ExternalOrigin(f, ext_result, _),
Call(ext_result, ext, _).
*/
USED("CBy-1-external")
ExternalOrigin(r, ext_result, ty_var) :-
REACHABLE(r)
Call(r, f, 0),
EvalMode(r, "strict"),
ExternalOrigin(f, ext_result, ty_var).
// CHECKED
// SECTION: Origin
USED("CBy-2")
NodeOrigin(n, n) :- Node(n, _).
// CHECKED
USED("CBy-3")
ExternalOrigin(r, r, ty_node) :-
Call(r, f, _),
EvalMode(r, "strict"),
ExternalFunction(f, _, _),
ExternalReturnType(f, ty_node).
// CHECKED
// Move
USED("CBy-4")
PointsTo(result, value) :-
REACHABLE(result)
Move(result, value).
// CHECKED
// SECTION: Case + Alt
.decl MatchedAlt(case_result:symbol, alt_name:symbol)
.output MatchedAlt
// scrutinee projection to alt value ; NodeOrigin
USED("CBy-5")
MatchedAlt(case_result, alt_val),
Reachable(alt_val),
ReachableCode(alt_val),
PointsTo(alt_val, scrut_val) :-
Case(case_result, scrut),
Alt(case_result, alt_val, tag),
NodeOrigin(scrut, scrut_val),
REACHABLE(case_result)
Node(scrut_val, tag).
// CHECKED
// scrutinee projection to alt value ; ExternalOrigin
USED("CBy-6")
MatchedAlt(case_result, alt_val),
Reachable(alt_val),
ReachableCode(alt_val),
ExternalOrigin(alt_val, ext_result, ty_var) :-
Case(case_result, scrut),
Alt(case_result, alt_val, tag),
REACHABLE(case_result)
ExternalOrigin(scrut, ext_result, ty_var),
TypeNode(ty_var, tag).
// CHECKED
// alt params from Node
USED("CBy-7")
PointsTo(p, val) :-
ReachableCode(alt),
NodeOrigin(alt, node),
Node(node, tag),
Alt(_, alt, tag),
AltParameter(alt, i, p),
NodeArgument(node, i, val).
// CHECKED
// alt params from External (i.e. ... -> {"GHC.Prim.Unit#" %a} or ... -> {"GHC.Prim.Unit#" T_Int})
USED("CBy-8")
ExternalOrigin(p, ext_result, ty_var) :-
ReachableCode(alt),
Alt(_, alt, tag),
AltParameter(alt, i, p),
ExternalOrigin(alt, ext_result, ty_node),
TypeNode(ty_node, tag),
TypeNodeArgument(ty_node, i, ty_var).
// CHECKED
//////////////////////////
// workaround for tagToEnum#
USED("CBy-8-tagToEnum#-workaround")
TagValue(alt, tag),
MatchedAlt(case_result, alt),
Reachable(alt),
ReachableCode(alt) :-
Case(case_result, scrut),
REACHABLE(case_result)
ExternalOrigin(scrut, ext_result, _),
Call(ext_result, "tagToEnum#", _),
Alt(case_result, alt, tag).
// CHECKED
//////////////////////////
// alt result
USED("CBy-9")
PointsTo(case_result, val) :-
Case(case_result, _),
Alt(case_result, alt, _),
REACHABLE(case_result)
ReachableCode(alt),
ReturnValue(alt, val).
// CHECKED
/*
TODO:
- handle default alternatives properly ; restrict alt_value to unhandled values only
*/
// default alternatives
USED("CBy-10")
MatchedAlt(case_result, alt_val),
Reachable(alt_val),
ReachableCode(alt_val),
PointsTo(alt_val, scrut) :-
Case(case_result, scrut),
Alt(case_result, alt_val, "default:"),
NEW_REACHABLE(case_result)
.
// CHECKED
/*
Question:
Do we need to distinct external and interpreted-external origins?
Does this makes difference in perfomrmance?
*/