forked from mozart/mozart2-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternMatching.oz
412 lines (395 loc) · 13.3 KB
/
PatternMatching.oz
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
%%%
%%% Author:
%%% Leif Kornstaedt <[email protected]>
%%%
%%% Copyright:
%%% Leif Kornstaedt, 1998-1999
%%%
%%% Last change:
%%% $Date$ by $Author$
%%% $Revision$
%%%
%%% This file is part of Mozart, an implementation of Oz 3:
%%% http://www.mozart-oz.org
%%%
%%% See the file "LICENSE" or
%%% http://www.mozart-oz.org/LICENSE.html
%%% for information on usage and redistribution
%%% of this file, and for a DISCLAIMER OF ALL
%%% WARRANTIES.
%%%
%%
%% Clause = Pattern#Statement
%% Pattern = [Pos#Test]
%% Pos = [FeatureV]
%% Test = scalar(Scalar)
%% | record(Label Arity)
%% | get([Feature#Reg])
%% | nonbasic(LabelV ArityV)
%% | label(LabelV)
%% | feature(FeatureV)
%% | equal(Pos)
%% | constant(VariableOccurrence)
%% | expr(SideCondition)
%%
%% Scalar = number
%% | literal
%%
%% Label = literal
%% Arity = int
%% | [Feature] % must be sorted
%% Feature = int
%% | literal
%%
%% LabelV = Label | VariableOccurrence
%% ArityV = [FeatureV] % variables to the back, sorted by printname
%% FeatureV = Feature | VariableOccurrence
%%
%% Tree = node(Pos Test Tree Tree Count Shared)
%% | leaf(Statement Count Shared)
%% | default
%% Count = cell
%% Shared = VInstr % vShared(...)
%%
local
fun {MayMoveOver Test1 Test2}
case Test1 of scalar(L1) then
case Test2 of scalar(L2) then L1 \= L2
[] record(_ _) then true
[] nonbasic(_ _) then true
[] label(L2) then {IsLiteral L2} andthen L1 \= L2
else false
end
[] record(L1 _) then
case Test2 of scalar(_) then true
[] record(_ _) then Test1 \= Test2
[] nonbasic(_ _) then
%--** this approximation could be improved
false
[] label(L2) then
{IsLiteral L1} andthen {IsLiteral L2} andthen L1 \= L2
else false
end
[] nonbasic(_ _) then
%--** this approximation could be improved
false
[] label(L1) then
{IsLiteral L1} andthen
case Test2 of scalar(L2) then L1 \= L2
[] record(L2 _) then L1 \= L2
[] nonbasic(L2 _) then {IsLiteral L2} andthen L1 \= L2
[] label(L2) then
%--** this could be more fine-grained by considering features
{IsLiteral L2} andthen L1 \= L2
else false
end
else false
end
end
fun {FindTest Tree Pos0 Test0 ?NewTree ?Hole ?RestTree}
case Tree of node(Pos Test ThenTree ElseTree Count Shared) then
if Pos \= Pos0 then false
elseif Test == Test0 then
NewTree = node(Pos Test Hole ElseTree Count Shared)
RestTree = ThenTree
true
elseif {MayMoveOver Test0 Test} then NewElseTree in
if {FindTest ElseTree Pos0 Test0 ?NewElseTree ?Hole ?RestTree} then
NewTree = node(Pos Test ThenTree NewElseTree Count Shared)
true
else false
end
else false
end
else false
end
end
fun {PatternToTree Pattern Then}
case Pattern of nil then
leaf(Then {NewCell 0} _)
[] Pos#Test|Rest then
node(Pos Test {PatternToTree Rest Then} default {NewCell 0} _)
end
end
proc {MergeSub Pattern Then Tree ?NewTree}
case Pattern of nil then
%% Tree is unreachable
NewTree = leaf(Then {NewCell 0} _)
[] Pos#Test|Rest then
case Tree of node(!Pos _ _ _ _ _) andthen Hole RestTree in
{FindTest Tree Pos Test ?NewTree ?Hole ?RestTree}
then
Hole = {MergeSub Rest Then RestTree}
else ThenTree in
ThenTree = {PatternToTree Rest Then}
NewTree = node(Pos Test ThenTree Tree {NewCell 0} _)
end
end
end
fun {MergePatternIntoTree Pattern#Then Tree}
{MergeSub Pattern Then Tree}
end
fun {ClipTree Pos0 Test0 Tree}
case Tree of node(Pos Test _ ElseTree _ _) then
if Pos == Pos0 andthen {MayMoveOver Test0 Test} then
{ClipTree Pos0 Test0 ElseTree}
else
Tree
end
[] leaf(_ _ _) then
Tree
end
end
fun {PropagateElses Tree DefaultTree}
case Tree of node(Pos Test ThenTree ElseTree Count Shared) then
NewElseTree NewDefaultTree NewThenTree
in
NewElseTree = {PropagateElses ElseTree DefaultTree}
NewDefaultTree = {ClipTree Pos Test NewElseTree}
NewThenTree = {PropagateElses ThenTree NewDefaultTree}
node(Pos Test NewThenTree NewElseTree Count Shared)
[] leaf(_ _ _) then
Tree
[] default then
case DefaultTree of node(_ _ _ _ Count _) then
{Assign Count {Access Count} + 1}
[] leaf(_ Count _) then
{Assign Count {Access Count} + 1}
end
DefaultTree
end
end
in
fun {BuildTree Clauses Else}
{PropagateElses
{FoldR Clauses MergePatternIntoTree default} leaf(Else {NewCell 0} _)}
end
end
fun {PosToReg Pos0 Mapping}
case Mapping of Pos#Reg|Mr then
if Pos == Pos0 then Reg
else {PosToReg Pos0 Mr}
end
end
end
local
fun {IsIndexable Test}
case Test of scalar(_) then true
[] record(_ _) then true
else false
end
end
local
proc {MakeHTEntry Pos Test Mapping ThenTree CS ?VHashTableEntry}
VThen NewMapping
in
case Test of scalar(X) then
VHashTableEntry = onScalar(X VThen)
NewMapping = Mapping
[] record(Label Arity) then Regs VGet in
if {IsInt Arity} then
Regs = {ForThread Arity 1 ~1
fun {$ In Feature}
{Append Pos [Feature]}#{CS newReg($)}|In
end nil}
else
Regs = {Map Arity
fun {$ Feature}
{Append Pos [Feature]}#{CS newReg($)}
end}
end
{FoldL Regs
proc {$ VHd _#Reg VTl}
VHd = vGetVariable(_ Reg VTl)
end VGet VThen}
VHashTableEntry = onRecord(Label Arity VGet)
NewMapping = {Append Regs Mapping}
end
{CodeGenPattern ThenTree NewMapping VThen nil unit CS}
end
proc {MakeMatchSub Tree Mapping Pos0 CS ?VHashTableEntries ?VElse}
case Tree of node(Pos Test ThenTree ElseTree Count _) then
if Pos == Pos0 andthen {Access Count} == 0
andthen {IsIndexable Test}
then Rest in
VHashTableEntries =
{MakeHTEntry Pos Test Mapping ThenTree CS}|Rest
{MakeMatchSub ElseTree Mapping Pos0 CS ?Rest ?VElse}
else
VHashTableEntries = nil
{CodeGenPattern Tree Mapping VElse nil unit CS}
end
else
VHashTableEntries = nil
{CodeGenPattern Tree Mapping VElse nil unit CS}
end
end
in
proc {MakeMatch Reg Tree Mapping Pos0 VHd VTl Coord CS}
node(Pos Test ThenTree ElseTree _ _) = Tree
VHashTableEntries Rest VElse VInter1 VInter2
in
VHashTableEntries = {MakeHTEntry Pos Test Mapping ThenTree CS}|Rest
{MakeMatchSub ElseTree Mapping Pos0 CS ?Rest ?VElse}
VInter1 = vMatch(_ Reg VElse VHashTableEntries Coord VInter2)
{StepPoint Coord 'conditional' VHd VTl VInter1 VInter2}
end
end
fun {MakeRecordArgument Feature}
if {IsObject Feature} then value({Feature reg($)})
else constant(Feature)
end
end
fun {MakeArityList Fs VHd VTl CS}
case Fs of F|Fr then ArgIn VInter1 ConsReg NewArg in
ArgIn = {MakeArityList Fr VHd VInter1 CS}
{CS newReg(?ConsReg)}
NewArg = {MakeRecordArgument F}
VInter1 = vEquateRecord(_ '|' 2 ConsReg [NewArg ArgIn] VTl)
value(ConsReg)
[] nil then
VHd = VTl
constant(nil)
end
end
proc {MakeEquation Feature VHd VTl CS ?Reg}
if {IsObject Feature} then
VHd = VTl
{Feature reg(?Reg)}
else
{CS newReg(?Reg)}
VHd = vEquateConstant(_ Feature Reg VTl)
end
end
proc {CodeGenSub Tree Mapping VHd VTl Coord CS}
node(Pos Test ThenTree ElseTree _ _) = Tree Reg
in
Reg = {PosToReg Pos Mapping}
case Test of scalar(_) then
{MakeMatch Reg Tree Mapping Pos VHd VTl Coord CS}
[] record(_ _) then
{MakeMatch Reg Tree Mapping Pos VHd VTl Coord CS}
[] get(Regs0) then
{CodeGenPattern ThenTree
{FoldR Regs0 fun {$ F#Reg In} {Append Pos [F]}#Reg|In end Mapping}
VHd VTl Coord CS}
[] expr(SideCondition) then ThenVInstr ElseVInstr in
{SideCondition codeGenTest(ThenVInstr ElseVInstr VHd VTl CS)}
{CodeGenPattern ThenTree Mapping ThenVInstr nil unit CS}
{CodeGenPattern ElseTree Mapping ElseVInstr nil unit CS}
else
TestReg TestVInstr TestProc TestArgs TestVOs
TestVInter1 TestVInter2 TestVInter3 VInstr1 VInstr11 VInstr2
Regs EmitGets ElseVInstr
in
{CS newReg(?TestReg)}
case Test of nonbasic(LabelV ArityV) then ArityReg VInter2 LabelReg in
{CS newReg(?ArityReg)}
case ArityV of F|Fr then VInter1 Arg1 Argr in
Arg1 = {MakeRecordArgument F}
Argr = {MakeArityList Fr VHd VInter1 CS}
VInter1 = vEquateRecord(_ '|' 2 ArityReg [Arg1 Argr] VInter2)
[] nil then
VHd = vEquateConstant(_ nil ArityReg VInter2)
end
LabelReg = {MakeEquation LabelV VInter2 TestVInstr CS}
TestProc = 'Record.test'
TestArgs = [Reg LabelReg ArityReg TestReg]
Regs = {Map ArityV
fun {$ FeatureV}
{Append Pos [FeatureV]}#{CS newReg($)}
end}
EmitGets = true
[] label(LabelV) then LabelReg in
%--** perhaps we could use indexing for the label
LabelReg = {MakeEquation LabelV VHd TestVInstr CS}
TestProc = 'Record.testLabel'
TestArgs = [Reg LabelReg TestReg]
Regs = nil
EmitGets = false
[] feature(FeatureV) then FeatureReg ResultReg in
{CS newReg(?ResultReg)}
FeatureReg = {MakeEquation FeatureV VHd TestVInstr CS}
TestProc = 'Record.testFeature'
TestArgs = [Reg FeatureReg TestReg ResultReg]
Regs = [{Append Pos [FeatureV]}#ResultReg]
EmitGets = false
[] equal(Pos0) then Reg0 in
Reg0 = {PosToReg Pos0 Mapping}
VHd = TestVInstr
TestProc = '=='
TestArgs = [Reg Reg0 TestReg]
Regs = nil
EmitGets = false
[] constant(VO) then Reg0 in
{VO reg(?Reg0)}
VHd = TestVInstr
TestProc = '=='
TestArgs = [Reg Reg0 TestReg]
Regs = nil
EmitGets = false
end
TestVOs = {Map TestArgs
fun {$ Reg} {New PseudoVariableOccurrence init(Reg)} end}
{MakeRunTimeProcApplication TestProc unit TestVOs CS
TestVInter1 TestVInter2}
{MakeException kernel boolCaseType unit [TestVOs.1] CS ElseVInstr nil}
TestVInter2 = vTestBool(_ TestReg VInstr1 VInstr2 ElseVInstr
unit TestVInter3)
{StepPoint Coord 'conditional' TestVInstr VTl TestVInter1 TestVInter3}
if EmitGets then
{FoldL Regs
proc {$ VHd Pos#DestReg VTl} F in
F = {List.last Pos}
if {IsObject F} then
VHd = vCallBuiltin(_ 'Value.\'.\'' [Reg {F reg($)} DestReg]
Coord VTl)
else
VHd = vInlineDot(_ Reg F DestReg false Coord VTl)
end
end VInstr1 VInstr11}
else
VInstr1 = VInstr11
end
{CodeGenPattern ThenTree {Append Regs Mapping} VInstr11 nil unit CS}
{CodeGenPattern ElseTree Mapping VInstr2 nil unit CS}
end
end
proc {CodeGenPattern Tree Mapping VHd VTl Coord CS}
case Tree of node(_ _ _ _ Count Shared) then
case {Access Count} of 0 then
{CodeGenSub Tree Mapping VHd VTl Coord CS}
else
VHd = Shared
VTl = nil
if {IsFree Shared} then Label VInstr in
{CS newLabel(?Label)}
Shared = vShared(_ _ Label VInstr)
{CodeGenSub Tree Mapping VInstr nil Coord CS}
end
end
[] leaf(Statement Count Shared) then
case {Access Count} of 0 then
{Statement codeGenPattern(Mapping VHd VTl CS)}
else
VHd = Shared
if {IsFree Shared} then Label VInstr in
{CS newLabel(?Label)}
Shared = vShared(_ _ Label VInstr)
{Statement codeGenPattern(Mapping VInstr nil CS)}
end
end
end
end
in
proc {OptimizePatterns ArbiterReg Clauses Else VHd VTl Coord CS}
Tree = {BuildTree Clauses Else}
Mapping = [nil#ArbiterReg]
in
case Tree of leaf(Statement _ _) then
{Statement codeGenPattern(Mapping VHd VTl CS)}
else
{CodeGenPattern Tree Mapping VHd VTl Coord CS}
end
end
end