Source file reachability.ml
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
(** This module computes the reachability of states in a parser automaton. It is
used to reason about the behavior of an LR(1) automaton after conflict
resolution (with some transitions removed).
The module implements algorithms for partitioning lookahead symbols with
identical behaviors, and uses these partitions to determine the cost of
reaching each state with a given lookahead. *)
open Fix.Indexing
open Utils
open Misc
open Info
module type S = sig
type g
type reduction = {
production: g production index;
lookahead: g terminal indexset;
steps: g transition index list;
state: g lr1 index;
}
val unreduce : g goto_transition index -> reduction list
module Classes : sig
val for_edge : g goto_transition index -> g terminal indexset array
val for_lr1 : g lr1 index -> g terminal indexset array
val pre_transition : g transition index -> g terminal indexset array
val post_transition : g transition index -> g terminal indexset array
end
module Coercion : sig
type pre = Pre_identity | Pre_singleton of int
val pre : 'a indexset array -> 'a indexset array -> pre option
type forward = int array array
type backward = int array
type infix = { forward : forward; backward : backward; }
val infix : ?lookahead:'a indexset -> 'a indexset array -> 'a indexset array -> infix
end
module Tree : sig
include CARDINAL
val leaf : g transition index -> n index
val split : n index -> (g transition index, n index * n index) either
type equations = {
nullable_lookaheads: g terminal indexset;
nullable: reduction list;
non_nullable: (reduction * n index) list;
}
val goto_equations : g goto_transition index -> equations
val pre_classes : n index -> g terminal indexset array
val post_classes : n index -> g terminal indexset array
end
module Cell : sig
include CARDINAL
type row = int
type column = int
val encode : Tree.n index -> pre:row -> post:column -> n index
val decode : n index -> Tree.n index * row * column
type goto
val goto : goto cardinal
val is_goto : n index -> goto index option
val of_goto : goto index -> n index
val goto_encode : g goto_transition index -> pre:row -> post:column -> goto index
val goto_decode : goto index -> g goto_transition index * row * column
val iter_goto : g goto_transition index -> (goto index -> unit) -> unit
end
module Analysis : sig
val cost : Cell.n index -> int
val finite : Cell.n index -> bool
end
end
type 'g t = (module S with type g = 'g)
type ('g, 'cell) t_cell = (module S with type g = 'g and type Cell.n = 'cell)
let make (type g) (g : g grammar) : g t = (module struct
type nonrec g = g
let quick_subset = IndexSet.quick_subset
type reduction = {
production: g production index;
lookahead: g terminal indexset;
steps: g transition index list;
state: g lr1 index;
}
let unreduce : g goto_transition index -> reduction list =
let predecessors =
Vector.init (Lr1.cardinal g) @@ fun lr1 ->
iterate [lr1, []] @@ fun states ->
let expand acc (state, steps) =
IndexSet.fold (fun tr acc ->
(Transition.source g tr, tr :: steps) :: acc
) (Transition.predecessors g state) acc
in
List.fold_left expand [] states
in
let table = Vector.make (Transition.goto g) [] in
let add_reduction lr1 (production, lookahead) =
if Production.kind g production = `REGULAR then begin
let lhs = Production.lhs g production in
let rhs = Production.rhs g production in
let states =
Array.fold_right
(fun _ pred -> Lazy.force pred.lnext)
rhs
predecessors.:(lr1)
in
List.iter (fun (source, steps) ->
table.@(Transition.find_goto g source lhs) <-
List.cons { production; lookahead; steps; state=lr1 }
) states.lvalue
end
in
let get_reductions lr1 =
match Lr1.default_reduction g lr1 with
| Some prod ->
[prod, Terminal.all g]
| None ->
IndexSet.fold
(fun red acc -> (Reduction.production g red, Reduction.lookaheads g red) :: acc)
(Reduction.from_lr1 g lr1) []
in
Index.iter (Lr1.cardinal g)
(fun lr1 -> List.iter (add_reduction lr1) (get_reductions lr1));
Vector.get table
module Classes = struct
module Node = (val Sum.make (Lr1.cardinal g) (Transition.goto g))
module Gr = struct
type node = Node.n index
let n = cardinal Node.n
let index = Index.to_int
let visit_lr1 f lr1 =
match Lr1.incoming g lr1 with
| Some sym when Symbol.is_nonterminal g sym ->
IndexSet.iter (fun tr ->
match Transition.split g tr with
| L nt -> f (Node.inj_r nt)
| R _ -> assert false
)
(Transition.predecessors g lr1)
| _ -> ()
let successors f i =
match Node.prj i with
| L lr1 -> visit_lr1 f lr1
| R e -> List.iter
(fun {state; _} -> f (Node.inj_l state))
(unreduce e)
let iter f = Index.iter Node.n f
end
module Scc = Tarjan.Run(Gr)
let classes = Vector.make Node.n IndexSet.Set.empty
let classes_of acc node =
let acc = ref acc in
begin match Node.prj node with
| L lr1 ->
Gr.visit_lr1 (fun n -> acc := IndexSet.Set.union classes.:(n) !acc) lr1
| R edge ->
List.iter (fun {lookahead; state; _} ->
let base = classes.:(Node.inj_l state) in
let base =
if lookahead != Terminal.all g
then IndexSet.Set.map (IndexSet.inter lookahead) base
else base
in
acc := IndexSet.Set.union (IndexSet.Set.add lookahead base) !acc
) (unreduce edge)
end;
!acc
let partition_sets sets =
sets
|> IndexSet.Set.elements
|> IndexRefine.partition
|> IndexSet.Set.of_list
let visit_scc _ nodes =
let coarse_classes =
partition_sets (List.fold_left classes_of IndexSet.Set.empty nodes)
in
match nodes with
| [node] -> classes.:(node) <- coarse_classes
| nodes ->
List.iter begin fun node ->
match Node.prj node with
| L _ -> ()
| R e ->
let coarse = ref IndexSet.empty in
List.iter
(fun {lookahead; _} ->
coarse := IndexSet.union lookahead !coarse)
(unreduce e);
classes.:(node) <-
partition_sets (IndexSet.Set.map (IndexSet.inter !coarse) coarse_classes)
end nodes;
List.iter begin fun node ->
match Node.prj node with
| R _ -> ()
| L lr1 ->
let acc = ref IndexSet.Set.empty in
Gr.visit_lr1 (fun n -> acc := IndexSet.Set.union classes.:(n) !acc) lr1;
classes.:(node) <- partition_sets !acc
end nodes
let () = Scc.rev_topological_iter visit_scc
let () = Index.iter (Lr1.cardinal g) (fun lr1 ->
match Lr1.incoming g lr1 with
| Some sym when Symbol.is_nonterminal g sym -> ()
| None | Some _ ->
classes.:(Node.inj_l lr1) <- IndexSet.Set.singleton (Terminal.all g)
)
let classes =
let prepare l =
let a = Array.of_seq (IndexSet.Set.to_seq l) in
Array.sort IndexSet.compare_minimum a;
a
in
Vector.map prepare classes
let for_edge nte =
classes.:(Node.inj_r nte)
let for_lr1 st =
classes.:(Node.inj_l st)
let t_singletons =
Vector.init (Terminal.cardinal g) (fun t -> [|IndexSet.singleton t|])
let all_terminals =
[|Terminal.all g|]
let pre_transition tr =
match Transition.split g tr with
| L _goto -> for_lr1 (Transition.source g tr)
| R shift -> t_singletons.:(Transition.shift_symbol g shift)
let post_transition tr =
match Transition.split g tr with
| L edge -> for_edge edge
| R _ -> all_terminals
end
let () = stopwatch 2 "reachability: computed classes"
module ConsedTree () : sig
include CARDINAL
module Inner : CARDINAL
val leaf : g transition index -> n index
val node : n index -> n index -> n index
val inject : Inner.n index -> n index
val split : n index -> (g transition index, Inner.n index) either
module FreezeTree() : sig
val define : Inner.n index -> n index * n index
end
end = struct
module Inner = Gensym()
include (val Sum.make (Transition.any g) Inner.n)
let leaf = inj_l
let inject = inj_r
let split = prj
type pack = n index * n index
let pack t u = (t, u)
let unpack x = x
let node_table : (pack, Inner.n index) Hashtbl.t = Hashtbl.create 7
let node l r =
let p = pack l r in
let node_index =
try Hashtbl.find node_table p
with Not_found ->
let i = Inner.fresh () in
Hashtbl.add node_table p i;
i
in
inj_r node_index
module FreezeTree() =
struct
let rev_index = Vector.make' Inner.n
(fun () -> let dummy = Index.of_int n 0 in (dummy, dummy))
let define ix = rev_index.:(ix)
let () =
Hashtbl.iter
(fun pair index -> rev_index.:(index) <- unpack pair)
node_table
end
end
module Coercion = struct
type pre =
| Pre_identity
| Pre_singleton of int
let pre outer inner =
if outer == inner then
Some Pre_identity
else (
assert (Array.length inner = 1);
assert (IndexSet.is_singleton inner.(0));
let t = IndexSet.choose inner.(0) in
match Utils.Misc.array_findi (fun _ ts -> IndexSet.mem t ts) 0 outer with
| i -> Some (Pre_singleton i)
| exception Not_found ->
None
)
type forward = int array array
type backward = int array
type infix = { forward: forward; backward: backward }
let infix ?lookahead pre_classes post_classes =
let forward_size = Array.make (Array.length pre_classes) 0 in
let backward =
Array.map (fun ca ->
let keep = match lookahead with
| None -> true
| Some la -> quick_subset ca la
in
if keep then (
match
Utils.Misc.array_findi
(fun _ cb -> quick_subset ca cb) 0 pre_classes
with
| exception Not_found -> -1
| i -> forward_size.(i) <- 1 + forward_size.(i); i
) else (-1)
) post_classes
in
let forward = Array.map (fun sz -> Array.make sz 0) forward_size in
Array.iteri (fun i_pre i_f ->
if i_f <> -1 then (
let pos = forward_size.(i_f) - 1 in
forward_size.(i_f) <- pos;
forward.(i_f).(pos) <- i_pre
)
) backward;
{ forward; backward }
end
module Tree = struct
include ConsedTree()
type equations = {
nullable_lookaheads: g terminal indexset;
nullable: reduction list;
non_nullable: (reduction * n index) list;
}
let goto_equations =
tabulate_finset (Transition.goto g) @@ fun tr ->
let first_dim =
Array.length (Classes.pre_transition (Transition.of_goto g tr))
in
let transition_size tr' =
Array.length (Classes.post_transition tr')
in
let rec import_mcop = function
| Mcop.Matrix l -> leaf l
| Mcop.Product (l, r) -> node (import_mcop l) (import_mcop r)
in
let solve_ccost_path red =
let dimensions = first_dim :: List.map transition_size red.steps in
match Mcop.dynamic_solution (Array.of_list dimensions) with
| exception Mcop.Empty -> Either.Left red
| solution ->
let steps = Array.of_list red.steps in
let solution = Mcop.map_solution (fun i -> steps.(i)) solution in
Either.Right (red, import_mcop solution)
in
let nullable, non_nullable =
List.partition_map solve_ccost_path (unreduce tr)
in
{
nullable_lookaheads =
List.fold_left (fun set red -> IndexSet.union red.lookahead set)
IndexSet.empty nullable;
nullable;
non_nullable;
}
include FreezeTree()
let table_pre = Vector.make Inner.n [||]
let table_post = Vector.make Inner.n [||]
let pre_classes t = match split t with
| L tr -> Classes.pre_transition tr
| R ix -> table_pre.:(ix)
let post_classes t = match split t with
| L tr -> Classes.post_transition tr
| R ix -> table_post.:(ix)
let pre_count t = Array.length (pre_classes t)
let post_count t = Array.length (post_classes t)
let () =
Index.iter Inner.n @@ fun node ->
let l, r = define node in
table_pre.:(node) <- pre_classes l;
table_post.:(node) <- post_classes r
let split i = match split i with
| L _ as result -> result
| R n -> R (define n)
end
let () = stopwatch 2 "reachability: constructed tree"
module Cell : sig
include CARDINAL
type row = int
type column = int
val encode : Tree.n index -> pre:row -> post:column -> n index
val decode : n index -> Tree.n index * row * column
val first_cell : Tree.n index -> n index
type goto
val goto : goto cardinal
val is_goto : n index -> goto index option
val of_goto : goto index -> n index
val goto_encode : g goto_transition index -> pre:row -> post:column -> goto index
val goto_decode : goto index -> g goto_transition index * row * column
val iter_goto : g goto_transition index -> (goto index -> unit) -> unit
end = struct
type row = int
type column = int
let n, pre_bits, post_bits =
let max_pre = ref 0 in
let max_post = ref 0 in
let n = ref 0 in
let bits_needed n =
let i = ref 0 in
while 1 lsl !i <= n
do incr i; done;
!i
in
Index.iter Tree.n begin fun node ->
let pre = Tree.pre_count node in
let post = Tree.post_count node in
n := !n + pre * post;
max_pre := Int.max pre !max_pre;
max_post := Int.max post !max_post;
end;
(!n, bits_needed !max_pre, bits_needed !max_post)
include Const(struct let cardinal = n end)
let mapping = Vector.make n 0
let first_cell =
let index = ref 0 in
Vector.init Tree.n @@ fun node ->
let first_index = !index in
let base = (node :> int) lsl (pre_bits + post_bits) in
let pre_count = Tree.pre_count node in
let post_count = Tree.post_count node in
for pre = 0 to pre_count - 1 do
let base = base lor (pre lsl post_bits) in
for post = 0 to post_count - 1 do
mapping.:(Index.of_int n !index) <- base lor post;
incr index
done
done;
first_index
let decode ix =
let i = mapping.:(ix) in
let post = i land (1 lsl post_bits - 1) in
let i = i lsr post_bits in
let pre = i land (1 lsl pre_bits - 1) in
(Index.of_int Tree.n (i lsr pre_bits), pre, post)
let encode i =
let first = first_cell.:(i) in
let post_count = Tree.post_count i in
fun ~pre ~post ->
Index.of_int n (first + pre * post_count + post)
let first_goto_node, first_goto_cell, last_goto_cell =
match cardinal (Transition.goto g) with
| 0 -> (0, 0, -1)
| n ->
let tr i = Transition.of_goto g (Index.of_int (Transition.goto g) i) in
let first_goto_node = Tree.leaf (tr 0) in
let first = first_cell.:(first_goto_node) in
let last = Tree.leaf (tr (n - 1)) in
let next = Index.of_int Tree.n ((last :> int) + 1) in
((first_goto_node :> int), first, first_cell.:(next) - 1)
module Goto = Const(struct
let cardinal = last_goto_cell - first_goto_cell + 1
end)
type goto = Goto.n
let goto = Goto.n
let is_goto (i : n index) =
let i = (i :> int) in
if first_goto_cell <= i && i <= last_goto_cell
then Some (Index.of_int goto (i - first_goto_cell))
else None
let of_goto (g : goto index) =
Index.of_int n (first_goto_cell + (g :> int))
let goto_decode (gt : goto index) =
let n, pre, post = decode (of_goto gt) in
let gt = Index.of_int (Transition.goto g) ((n :> int) - first_goto_cell) in
(gt, pre, post)
let goto_encode i =
let node = Tree.leaf (Transition.of_goto g i) in
let first = first_cell.:(node) - first_goto_cell in
let post_count = Tree.post_count node in
fun ~pre ~post ->
Index.of_int goto (first + pre * post_count + post)
let iter_goto (gt : g goto_transition index) f =
let i = (gt :> int) in
let index_of i = first_cell.:(Index.of_int Tree.n (first_goto_node + i)) in
for j = index_of i to index_of (i + 1) - 1 do
f (Index.of_int goto (j - first_goto_cell))
done
let first_cell i = Index.of_int n first_cell.:(i)
end
let () = stopwatch 2 "reachability: indexed matrix cells"
module Reverse_dependencies = struct
type t =
| Leaf of g goto_transition index * Coercion.pre * Coercion.forward
| Inner of Tree.Inner.n index * Coercion.infix
let occurrences : (Tree.n, t list) vector =
Vector.make Tree.n []
let () =
Index.iter (Transition.goto g) begin fun tr ->
let node = Tree.leaf (Transition.of_goto g tr) in
let pre = Tree.pre_classes node in
let post = Tree.post_classes node in
List.iter begin fun ({lookahead; _}, node') ->
match Coercion.pre pre (Tree.pre_classes node') with
| None ->
()
| Some coerce_pre ->
let post' = Tree.post_classes node' in
let coerce_post = Coercion.infix post' post ~lookahead in
occurrences.@(node') <-
List.cons (Leaf (tr, coerce_pre, coerce_post.Coercion.forward))
end (Tree.goto_equations tr).non_nullable
end;
Index.iter Tree.Inner.n begin fun node ->
let (l, r) = Tree.define node in
let c1 = Tree.post_classes l in
let c2 = Tree.pre_classes r in
let coercion = Coercion.infix c1 c2 in
let dep = Inner (node, coercion) in
assert (Array.length c2 = Array.length coercion.Coercion.backward);
occurrences.@(l) <- List.cons dep;
occurrences.@(r) <- List.cons dep
end
let visit_occurrences index
~visit_goto
~from_left ~acc ~acc_right
~from_right
=
let node, i_pre, i_post = Cell.decode index in
let update_dep = function
| Leaf (parent, pre, post) ->
let i_pre' = match pre with
| Coercion.Pre_singleton i -> i
| Coercion.Pre_identity -> i_pre
in
let encode = Cell.goto_encode parent in
Array.iter (fun i_post' -> visit_goto (encode ~pre:i_pre' ~post:i_post'))
post.(i_post)
| Inner (parent, inner) ->
let l, r = Tree.define parent in
let encode_p = Cell.encode (Tree.inject parent) in
if l = node then (
let encode_r = Cell.encode r in
for i_post' = 0 to Array.length (Tree.post_classes r) - 1 do
let acc =
Array.fold_left
(fun acc i_pre' ->
acc_right acc (encode_r ~pre:i_pre' ~post:i_post'))
acc inner.Coercion.forward.(i_post)
in
from_left
~right:acc
~parent:(encode_p ~pre:i_pre ~post:i_post')
done
) else (
assert (r = node);
match inner.Coercion.backward.(i_pre) with
| -1 -> ()
| l_post ->
let encode_l = Cell.encode l in
for i_pre = 0 to Array.length (Tree.pre_classes l) - 1 do
from_right
~left:(encode_l ~pre:i_pre ~post:l_post)
~parent:(encode_p ~pre:i_pre ~post:i_post)
done
)
in
List.iter update_dep occurrences.:(node)
end
let () = stopwatch 2 "reachability: reversed matrix dependencies"
module Solver = struct
let min_cost a b : int =
if a < b then a else b
let initialize_shift ~visit_root tr =
let node = Tree.leaf (Transition.of_shift g tr) in
assert (Array.length (Tree.pre_classes node) = 1);
assert (Array.length (Tree.post_classes node) = 1);
visit_root (Cell.first_cell node) 1
let initialize_goto ~visit_root tr =
let node = Tree.leaf (Transition.of_goto g tr) in
let eqn = Tree.goto_equations tr in
if IndexSet.is_not_empty eqn.nullable_lookaheads then (
let pre = Tree.pre_classes node in
let post = Tree.post_classes node in
let encode = Cell.encode node in
let update_cell i_post c_post i_pre c_pre =
if not (IndexSet.disjoint c_pre c_post) then
visit_root (encode ~pre:i_pre ~post:i_post) 0
in
let update_col i_post c_post =
if quick_subset c_post eqn.nullable_lookaheads then
Array.iteri (update_cell i_post c_post) pre
in
Array.iteri update_col post
)
let costs = Vector.make Cell.n max_int
module Graph = struct
type variable = Cell.n index
let foreach_root visit_root =
Index.iter (Transition.shift g) (initialize_shift ~visit_root);
Index.iter (Transition.goto g) (initialize_goto ~visit_root)
let foreach_successor index cost f =
assert (cost < max_int);
Reverse_dependencies.visit_occurrences index
~visit_goto:(fun cell -> f (Cell.of_goto cell) cost)
~acc:max_int
~acc_right:(fun cost right -> min_cost cost costs.:(right))
~from_left:(fun ~right ~parent ->
if right < max_int then
f parent (cost + right))
~from_right:(fun ~left ~parent ->
let left = costs.:(left) in
if left < max_int then
f parent (left + cost)
)
end
module Property = struct
type property = int
let leq_join = min_cost
end
module BoolMap() = struct
let table = Boolvector.make Cell.n false
let get t = Boolvector.test table t
let set t x =
if x
then Boolvector.set table t
else Boolvector.clear table t
end
include Fix.DataFlow.ForCustomMaps(Property)(Graph)(struct
let get i = Vector.get costs i
let set i x = Vector.set costs i x
end)(BoolMap())
module Bool_or = struct
type property = bool
let leq_join = (||)
end
module Finite = BoolMap()
module FiniteGraph = struct
type variable = Cell.n index
let count = Vector.make Cell.goto 0
let () =
Index.iter Cell.n (fun cell ->
Reverse_dependencies.visit_occurrences cell
~visit_goto:(fun goto -> count.@(goto) <- succ)
~acc:()
~acc_right:(fun () _ -> ())
~from_left:(fun ~right:() ~parent:_ -> ())
~from_right:(fun ~left:_ ~parent:_ -> ())
)
let foreach_root visit_root =
Index.iter (Transition.shift g) (fun sh ->
let node = Tree.leaf (Transition.of_shift g sh) in
visit_root (Cell.first_cell node) true
);
Index.iter (Transition.goto g) (fun gt ->
Cell.iter_goto gt (fun gt' ->
let index = Cell.of_goto gt' in
if costs.:(index) < max_int && count.:(gt') = 0 then
visit_root index true
)
)
let foreach_successor index finite f =
if finite then
Reverse_dependencies.visit_occurrences index
~visit_goto:(fun gt ->
let count' = count.:(gt) - 1 in
count.:(gt) <- count';
assert (count' >= 0);
if count' = 0 then
f index true
)
~acc:true ~acc_right:(fun acc right -> acc && Finite.get right)
~from_left:(fun ~right ~parent -> if right then f parent true)
~from_right:(fun ~left ~parent ->
if Finite.get left then f parent true)
end
include Fix.DataFlow.ForCustomMaps(Bool_or)(FiniteGraph)(Finite)(BoolMap())
let () = stopwatch 2 "solved minimal costs"
end
module Analysis = struct
let cost = Vector.get Solver.costs
let finite = Solver.Finite.get
end
end)