-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimOp-Arrays.dl
257 lines (233 loc) · 11 KB
/
PrimOp-Arrays.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
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
/*
HINT: is interpreted -/+
Arrays
primop effectful
+ "newArray#" :: T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
primop pure
- "sameMutableArray#" :: {"MutableArray#" %s %a} -> {"MutableArray#" %s %a} -> T_Int64
primop effectful
+ "readArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" %a}
+ "writeArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.(##)"}
primop pure
- "sizeofArray#" :: {"Array#" %a} -> T_Int64
- "sizeofMutableArray#" :: {"MutableArray#" %s %a} -> T_Int64
+ "indexArray#" :: {"Array#" %a} -> T_Int64 -> {"GHC.Prim.Unit#" %a}
primop effectful
+ "unsafeFreezeArray#" :: {"MutableArray#" %s %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"Array#" %a}}
+ "unsafeThawArray#" :: {"Array#" %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
+ "copyArray#" :: {"Array#" %a} -> T_Int64 -> {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
+ "copyMutableArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
+ "cloneArray#" :: {"Array#" %a} -> T_Int64 -> T_Int64 -> {"Array#" %a}
+ "cloneMutableArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
+ "freezeArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"Array#" %a}}
+ "thawArray#" :: {"Array#" %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
+ "casArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> %a -> %a -> {"State#" %s} -> {"GHC.Prim.(#,#)" T_Int64 %a}
Small Arrays
primop effectful
+ "newSmallArray#" :: T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
primop pure
- "sameSmallMutableArray#" :: {"SmallMutableArray#" %s %a} -> {"SmallMutableArray#" %s %a} -> T_Int64
primop effectful
+ "readSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" %a}
+ "writeSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.(##)"}
primop pure
- "sizeofSmallArray#" :: {"SmallArray#" %a} -> T_Int64
- "sizeofSmallMutableArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64
+ "indexSmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> {"GHC.Prim.Unit#" %a}
primop effectful
+ "unsafeFreezeSmallArray#" :: {"SmallMutableArray#" %s %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallArray#" %a}}
+ "unsafeThawSmallArray#" :: {"SmallArray#" %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
+ "copySmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
+ "copySmallMutableArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
+ "cloneSmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> T_Int64 -> {"SmallArray#" %a}
+ "cloneSmallMutableArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
+ "freezeSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallArray#" %a}}
+ "thawSmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
+ "casSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> %a -> %a -> {"State#" %s} -> {"GHC.Prim.(#,#)" T_Int64 %a}
*/
/*
Question:
should we distinct Array and MutableArray?
can we reuse Array for each container like primitives?
Answer:
At first I go with the simplest approach: single reusable Array.
*/
// IDEA: Array is a kind of domain specific TypeVarPointsTo
.decl Array(ext_result:Variable, ty_node:Variable, item:Variable)
.output Array
// "newArray#" :: T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
// "newSmallArray#" :: T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
// new array
USED("PrimOp-Arrays-01")
//Called(r, op),
Array(r, ty_node, item) :-
( op = "newArray#"
; op = "newSmallArray#"
),
Call(r, op, _),
// initial item
CallArgument(r, 1, item),
// extract result node
RetTup1Node0(op, ty_node),
NEW_REACHABLE(r)
.
// CHECKED
// "readArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" %a}
// "readSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" %a}
// "indexArray#" :: {"Array#" %a} -> T_Int64 -> {"GHC.Prim.Unit#" %a}
// "indexSmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> {"GHC.Prim.Unit#" %a}
// NOTE: have common implementation because they use the first argument and set the return value with the same abstract type
USED("PrimOp-Arrays-02")
//Called(r, op),
TypeVarPointsTo(r, ty_node, item) :-
( op = "readArray#"
; op = "readSmallArray#"
; op = "indexArray#"
; op = "indexSmallArray#"
),
Call(r, op, _),
// lookup array items
CallArgument(r, 0, arr),
ExternalOrigin(arr, ext_result, arr_node),
Array(ext_result, arr_node, item),
// lookup result node
RetTup1Node0(op, ty_node),
NEW_REACHABLE(r)
.
// CHECKED
// "writeArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.(##)"}
// "writeSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> %a -> {"State#" %s} -> {"GHC.Prim.(##)"}
// extend array
USED("PrimOp-Arrays-03")
//Called(r, op),
Array(ext_result, ty_node, item) :-
( op = "writeArray#"
; op = "writeSmallArray#"
),
Call(r, op, _),
// item to write
CallArgument(r, 2, item),
// lookup array
CallArgument(r, 0, arr),
ExternalOrigin(arr, ext_result, ty_node),
// validation
Array(ext_result, ty_node, _),
NEW_REACHABLE(r)
.
// CHECKED
// "unsafeFreezeArray#" :: {"MutableArray#" %s %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"Array#" %a}}
// "unsafeFreezeSmallArray#" :: {"SmallMutableArray#" %s %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallArray#" %a}}
// "unsafeThawArray#" :: {"Array#" %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
// "unsafeThawSmallArray#" :: {"SmallArray#" %a} -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
// reuse array ; alias
USED("PrimOp-Arrays-04")
//Called(r, op),
TypeVarPointsTo(r, ty_node, arr) :- // bypass argument
( op = "unsafeFreezeArray#"
; op = "unsafeFreezeSmallArray#"
; op = "unsafeThawArray#"
; op = "unsafeThawSmallArray#"
),
Call(r, op, _),
// lookup array items
CallArgument(r, 0, arr),
// lookup result node
RetTup1Node0(op, ty_node),
NEW_REACHABLE(r)
.
// CHECKED
// "copyArray#" :: {"Array#" %a} -> T_Int64 -> {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
// "copySmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
// "copyMutableArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
// "copySmallMutableArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.(##)"}
// extend array
USED("PrimOp-Arrays-05")
//Called(r, op),
Array(dst_ext_result, dst_arr_node, src_item) :-
( op = "copyArray#"
; op = "copySmallArray#"
; op = "copyMutableArray#"
; op = "copySmallMutableArray#"
),
Call(r, op, _),
// lookup src array items
CallArgument(r, 0, src_arr),
ExternalOrigin(src_arr, src_ext_result, src_arr_node),
Array(src_ext_result, src_arr_node, src_item),
// lookup dst array items
CallArgument(r, 2, dst_arr),
ExternalOrigin(dst_arr, dst_ext_result, dst_arr_node),
Array(dst_ext_result, dst_arr_node, _),
NEW_REACHABLE(r)
.
// CHECKED
// "cloneArray#" :: {"Array#" %a} -> T_Int64 -> T_Int64 -> {"Array#" %a}
// "cloneSmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> T_Int64 -> {"SmallArray#" %a}
// new array
USED("PrimOp-Arrays-06")
//Called(r, op),
Array(r, arr_node, src_item) :-
( op = "cloneArray#"
; op = "cloneSmallArray#"
),
Call(r, op, _),
// lookup src array items
CallArgument(r, 0, src_arr),
ExternalOrigin(src_arr, src_ext_result, src_arr_node),
Array(src_ext_result, src_arr_node, src_item),
// extract result node
ExternalReturnType(op, arr_node),
NEW_REACHABLE(r)
.
// CHECKED
// "cloneMutableArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
// "cloneSmallMutableArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
// "freezeArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"Array#" %a}}
// "freezeSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallArray#" %a}}
// "thawArray#" :: {"Array#" %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"MutableArray#" %s %a}}
// "thawSmallArray#" :: {"SmallArray#" %a} -> T_Int64 -> T_Int64 -> {"State#" %s} -> {"GHC.Prim.Unit#" {"SmallMutableArray#" %s %a}}
// new array
USED("PrimOp-Arrays-07")
//Called(r, op),
Array(r, arr_node, src_item) :-
( op = "cloneMutableArray#"
; op = "cloneSmallMutableArray#"
; op = "freezeArray#"
; op = "freezeSmallArray#"
; op = "thawArray#"
; op = "thawSmallArray#"
),
Call(r, op, _),
// lookup src array items
CallArgument(r, 0, src_arr),
ExternalOrigin(src_arr, src_ext_result, src_arr_node),
Array(src_ext_result, src_arr_node, src_item),
// extract result node
RetTup1Node0(op, arr_node),
NEW_REACHABLE(r)
.
// CHECKED
// "casArray#" :: {"MutableArray#" %s %a} -> T_Int64 -> %a -> %a -> {"State#" %s} -> {"GHC.Prim.(#,#)" T_Int64 %a}
// "casSmallArray#" :: {"SmallMutableArray#" %s %a} -> T_Int64 -> %a -> %a -> {"State#" %s} -> {"GHC.Prim.(#,#)" T_Int64 %a}
// extend array
USED("PrimOp-Arrays-08")
//Called(r, op),
TypeVarPointsTo(r, ty_node, item), // can return any array item
TypeVarPointsTo(r, ty_node, new_item),
Array(ext_result, arr_node, new_item) :-
( op = "casArray#"
; op = "casSmallArray#"
),
Call(r, op, _),
// new item
CallArgument(r, 3, new_item),
// lookup array
CallArgument(r, 0, arr),
ExternalOrigin(arr, ext_result, arr_node),
// array items
Array(ext_result, arr_node, item),
// extract result node
RetTup(op, "GHC.Prim.(#,#)", 1, ty_node),
NEW_REACHABLE(r)
.
// CHECKED