val top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
val bottom_up_run : Ata.t -> Tree.t -> Compile.text_query * string -> result_set
val grammar_run : Ata.t -> Grammar2.t -> unit -> result_set
-
+ val naive_top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
+ val twopass_top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
end
module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
let eval_trans auto s1 s2 trans =
+ TRACE("top-down-run", 2, __ "Evaluating transition list:\n%!");
+ TRACE("top-down-run", 2, __ "%a\n%!" Translist.print trans);
Translist.fold
(fun t ((a_st, a_op, a_todo) as acc)->
let q, _, m, f = Transition.node t in
let orig_s1, orig_s2 =
Translist.fold (fun t (a1, a2) ->
let _, _, _, f = Transition.node t in
- let (_, _, fs1), (_, _, fs2) = Formula.st f in
+ let fs1, fs2 = Formula.st f in
(StateSet.union a1 fs1, StateSet.union a2 fs2)
) trl (StateSet.empty, StateSet.empty)
in
let ns1 = StateSet.inter s1 orig_s1
and ns2 = StateSet.inter s2 orig_s2 in
- let res, ops, todo = eval_trans auto ns1 ns2 trl in
+ let res, ops, todo = eval_trans auto orig_s1 orig_s2 trl in
let code, not_marking = ResJIT.compile ops in
let todo_code, todo_notmarking =
List.fold_left (fun (l, b) (p, q, o) -> let c, b' = ResJIT.compile o in
else sl1
else sl2
in
+ eprintf "Here 1\n%!";
U.exec sl sl1 sl2 node code;
res, sl
end
else sl1
else sl2
in
+ eprintf "Here 2\n%!";
U.exec sl sl1 sl2 node code;
res, sl
end
l2jit_dispatch
_t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states)))
-DEFINE LOOP(t, states, ctx) = loop (t) (states) (ctx)
-DEFINE LOOP_TAG(t, states, tag, ctx) = loop_tag (t) (states) (ctx) (tag)
-
let top_down_run auto tree root states ctx =
let res_len = StateSet.max_elt auto.states + 1 in
let empty_slot = Array.create res_len U.NS.empty in
in
let cache2 = L2JIT.create () in
- let rec loop t states ctx =
- if t == Tree.nil then nil_res
- else
- let tag = Tree.tag tree t in
- l2jit_dispatch
- t tag (states) (ctx) (L2JIT.find cache2 tag (states))
- and loop_tag t states ctx tag =
- if t == Tree.nil then nil_res
- else
- l2jit_dispatch
- t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states))
- and l2jit_dispatch t tag states ctx opcode =
+ let rec l2jit_dispatch t tag states ctx opcode =
match opcode with
| L2JIT.RETURN -> nil_res
| L2JIT.CACHE ->
- let opcode = L2JIT.compile cache2 auto tree tag states in
- l2jit_dispatch t tag states ctx opcode
+ eprintf "New configuration\n%!";
+ let opcode = L2JIT.compile cache2 auto tree tag states in
+ l2jit_dispatch t tag states ctx opcode
| L2JIT.LEFT (tr_list, instr) ->
let res1, slot1 =
and l2jit_dispatch_instr t ctx instr =
match instr with
+ | L2JIT.NOP () -> nil_res
| L2JIT.FIRST_CHILD s -> LOOP ((Tree.first_child tree t), s, ctx)
| L2JIT.NEXT_SIBLING s -> LOOP ((Tree.next_sibling tree t), s, ctx)
then acc
else
let _, _, _, phi = Transition.node trs in
- let (_,_,l), (_,_,r) = Formula.st phi in
+ let l, r = Formula.st phi in
(StateSet.union l lstates,
StateSet.union r rstates,
Translist.cons trs tacc)
;;
+ (* Slow reference top-down implementation *)
+ let naive_top_down auto tree root states ctx =
+ let res_len = StateSet.max_elt auto.states + 1 in
+ let empty_slot = Array.create res_len U.NS.empty in
+ let nil_res = auto.bottom_states, empty_slot in
+ let cache3 = L3JIT.create () in
+ let l3jit_dispatch trl s1 s2 t sl1 sl2 =
+ let f = L3JIT.find cache3 trl s1 s2 in
+ if f == L3JIT.dummy then (L3JIT.cache_apply cache3 auto trl s1 s2) empty_slot sl1 sl2 tree t
+ else f empty_slot sl1 sl2 tree t
+ in
+ let dummy = Translist.nil, StateSet.singleton State.dummy, StateSet.singleton State.dummy in
+ let cache2 = Cache.Lvl2.create 512 dummy in
+ let rec loop t states ctx =
+ if states == StateSet.empty then nil_res
+ else if t == Tree.nil then (*StateSet.inter states auto.bottom_states, empty_slot *) nil_res
+ else
+ let tag = Tree.tag tree t in
+
+ let trans, lstates, rstates =
+ let c = Cache.Lvl2.find cache2 (Uid.to_int states.StateSet.Node.id) tag in
+ if c == dummy then
+ let c = Ata.get_trans auto states tag in
+ Cache.Lvl2.add cache2 (Uid.to_int states.StateSet.Node.id) tag c;
+ c
+ else c
+ in
+ let s1, res1 = loop (Tree.first_child tree t) lstates ctx
+ and s2, res2 = loop (Tree.next_sibling tree t) rstates ctx in
+ l3jit_dispatch trans s1 s2 t res1 res2
+ in
+ loop root states ctx
+
+
+
+
+ let naive_top_down_run auto tree root =
+ let res, slot = naive_top_down auto tree root auto.init (Tree.closing tree root) in
+ slot.(StateSet.min_elt auto.topdown_marking_states)
+
+
+
+ let eval_form auto s1 s2 f =
+ let rec loop f =
+ match Formula.expr f with
+ | Formula.False | Formula.True | Formula.Pred _ -> f
+ | Formula.Atom(`Left, b, q) ->
+ Formula.of_bool (b == (StateSet.mem q s1))
+ | Formula.Atom (`Right, b, q) ->
+ Formula.of_bool(b == (StateSet.mem q s2))
+ | Formula.Atom (`Epsilon, _, _) -> assert false
+
+ | Formula.Or(f1, f2) ->
+ let b1 = loop f1 in
+ let b2 = loop f2 in
+ Formula.or_pred b1 b2
+ | Formula.And(f1, f2) ->
+ let b1 = loop f1 in
+ let b2 = loop f2 in
+ Formula.and_pred b1 b2
+ in
+ loop f
+
+ let eval_trans auto s1 s2 trans =
+ Translist.fold
+ (fun t ((a_st, mark) as acc)->
+ let q, _, m, f = Transition.node t in
+ let form = eval_form auto s1 s2 f in
+ match Formula.expr form with
+ | Formula.True -> StateSet.add q a_st, mark || m
+ | Formula.False -> acc
+ | _ -> assert false
+ ) trans (StateSet.empty, false)
+
+
+ let set a i v =
+ TRACE("twopass", 2, __ "Setting node %i to state %a\n%!"
+ i StateSet.print v);
+ a.(i) <- v
+
+ let twopass_top_down states_array auto tree root states ctx =
+ let dummy3 = StateSet.singleton State.dummy in
+ let cache3 = Cache.Lvl3.create 512 dummy3 in
+ let dummy2 = Translist.nil, StateSet.singleton State.dummy, StateSet.singleton State.dummy in
+ let cache2 = Cache.Lvl2.create 512 dummy2 in
+ let attributes = TagSet.inj_positive (Tree.attribute_tags tree) in
+ let rec loop t states ctx =
+ if t == Tree.nil then auto.bottom_states
+ else if states == StateSet.empty then
+ let () = set states_array (Node.to_int t) auto.bottom_states in
+ auto.bottom_states
+ else
+ let tag = Tree.tag tree t in
+ TRACE("twopass", 2, __ "Traversing node %i (tag %s) in states %a\n%!" (Node.to_int t) (Tag.to_string tag)
+ StateSet.print states
+ );
+ let trans, lstates, rstates =
+ let c = Cache.Lvl2.find cache2 (Uid.to_int states.StateSet.Node.id) tag in
+ if c == dummy2 then
+ let c = Ata.get_trans ~attributes:attributes auto states tag in
+ Cache.Lvl2.add cache2 (Uid.to_int states.StateSet.Node.id) tag c;
+ c
+ else c
+ in
+ TRACE("twopass", 2, __ "\nTransitions are:\n%!");
+ TRACE("twopass", 2, __ "\nTransitions are:\n%a\n%!"
+ Translist.print trans
+ );
+ let s1 = loop (Tree.first_child tree t) lstates ctx
+ and s2 = loop (Tree.next_sibling tree t) rstates ctx in
+ let st =
+ let c = Cache.Lvl3.find cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id)
+ in
+ if c == dummy3 then
+ let c, _ = eval_trans auto s1 s2 trans in
+ Cache.Lvl3.add cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id) c;c
+ else c
+ in
+ set states_array (Node.to_int t) st;
+ st
+ in
+ loop root states ctx, (dummy2, cache2)
+
+
+ type action = Nop | Mark | Dummy
+
+ let twopass_top_down_scan states_array (dummy2, cache2) auto tree root states ctx =
+ let attributes = TagSet.inj_positive (Tree.attribute_tags tree) in
+ let cache3 = Cache.Lvl3.create 512 Dummy in
+ let rec loop t states acc =
+ if states == StateSet.empty || t = Tree.nil then acc
+ else
+ let tag = Tree.tag tree t in
+ let trans, _, _ =
+ let c = Cache.Lvl2.find cache2 (Uid.to_int states.StateSet.Node.id) tag in
+ if c == dummy2 then
+ let c = Ata.get_trans ~attributes:attributes auto states tag in
+ Cache.Lvl2.add cache2 (Uid.to_int states.StateSet.Node.id) tag c;
+ c
+ else c
+ in
+ let fs = Tree.first_child tree t in
+ let ns = Tree.next_sibling tree t in
+ let s1 = if fs != Tree.nil then states_array.(Node.to_int fs) else auto.bottom_states
+ and s2 = if ns != Tree.nil then states_array.(Node.to_int ns) else auto.bottom_states
+ in
+ let mark =
+ let c = Cache.Lvl3.find cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id)
+ in
+ if c == Dummy then
+ let _, c = eval_trans auto s1 s2 trans in
+ let c = if c then Mark else Nop in
+ Cache.Lvl3.add cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id) c;c
+ else c
+ in
+ TRACE("twopass", 2, __ "Evaluating node %i (tag %s).\n%!States=%a\n%!"
+ (Node.to_int t)
+ (Tag.to_string tag)
+ StateSet.print states
+ );
+ TRACE("twopass", 2, __ "Translist=%a\nLeft=%a\nRight=%a\nMark=%s\n\n%!"
+ Translist.print trans
+ StateSet.print s1
+ StateSet.print s2
+ (match mark with
+ Dummy -> "Dummy"
+ | Mark -> "Mark"
+ | Nop -> "Nop"));
+ if mark == Mark then
+ loop ns s2 (loop fs s1 (U.NS.snoc acc t))
+ else
+ loop ns s2 (loop fs s1 acc)
+ in
+ loop root states U.NS.empty
+
+ let twopass_top_down_run auto tree root =
+ let len = Node.to_int (Tree.closing tree root) + 1 in
+ TRACE("twopass", 2, __ "Creating array of size: %i\n%!" len);
+ let states_array = Array.make len StateSet.empty in
+ let _, cache =
+ twopass_top_down states_array auto tree root auto.init Tree.nil
+ in
+ twopass_top_down_scan states_array cache auto tree root auto.init Tree.nil
+
+
+
+
+