Defunctorize the ResJIT module.
[SXSI/xpathcomp.git] / src / runtime.ml
index 40c9e97..498a1dc 100644 (file)
@@ -1,6 +1,7 @@
 INCLUDE "debug.ml"
 INCLUDE "trace.ml"
 INCLUDE "utils.ml"
+let l3jit_dummy _ _ _ _ _ = failwith "Uninitialized L3JIT"
 
 open Format
 open Ata
@@ -57,27 +58,32 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
             | _ -> assert false
        ) trans (StateSet.empty, [], [])
 
-
-
     module L3JIT =
       struct
 
-       type opcode = (t -> t -> t -> Tree.t -> Tree.node -> StateSet.t * t)
+       type opcode = (U.t -> U.t -> U.t -> Tree.t -> Tree.node -> StateSet.t * U.t)
 
        type t = opcode Cache.Lvl3.t
-
        let dummy _ _ _ _ _ = failwith "Uninitialized L3JIT"
 
-       let create () = Cache.Lvl3.create 1024 dummy
-       let find t tlist s1 s2 =
+
+       let show_stats (a : t) =
+         let count = ref 0 in
+         Cache.Lvl3.iteri (fun _ _ _ _ b -> if not b then incr count) a;
+         eprintf "%!L3JIT: %i used entries\n%!" !count
+
+       let create () =
+         let v = Cache.Lvl3.create 1024 dummy in
+         if !Options.verbose then at_exit (fun () -> show_stats v);
+         v
+
+       let find (t : t) tlist s1 s2 =
          Cache.Lvl3.find t
            (Uid.to_int s2.StateSet.Node.id)
            (Uid.to_int s1.StateSet.Node.id)
            (Uid.to_int tlist.Translist.Node.id)
 
-
-
-       let add t tlist s1 s2 v =
+       let add (t : t) tlist s1 s2 v =
          Cache.Lvl3.add t
            (Uid.to_int s2.StateSet.Node.id)
            (Uid.to_int s1.StateSet.Node.id)
@@ -88,7 +94,7 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
          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
@@ -158,13 +164,11 @@ module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
 
        let cache_apply cache auto tlist s1 s2 =
          let f = gen_code auto tlist s1 s2 in
-         TRACE("grammar", 2, __ "Inserting: %i, %a, %a\n%!"
-           (Uid.to_int tlist.Translist.Node.id) StateSet.print s1 StateSet.print s2);
          add cache tlist s1 s2 f; f
       end
 
 DEFINE LOOP (t, states, ctx) = (
-  let _t = (t) in
+  let _t = t in
   TRACE("top-down-run", 3,
        __ "Entering node %i with loop (tag %s, context %i) with states %a\n%!"
          (Node.to_int _t)
@@ -191,22 +195,32 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
     l2jit_dispatch
       _t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states)))
 
-
     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
       let nil_res = auto.bottom_states, empty_slot in
-      let cache3 = L3JIT.create () in
-
-      let l3jit_dispatch trl s1 s2 t sl1 sl2 =
+      let cache3 = ResJIT.create () in
+      let mark_subtree s subtree =
+       if subtree != U.NS.empty then
+         let r = Array.copy empty_slot in
+         r.(auto.last) <- subtree;
+         s,r
+       else
+         s,empty_slot
+      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
+       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 l3jit_dispatch trl s1 s2 t sl1 sl2 =
+       ResJIT.update U.exec cache3 auto trl s1 s2 empty_slot sl1 sl2 tree t
       in
       let cache2 = L2JIT.create () in
 
-
       let rec l2jit_dispatch t tag states ctx opcode =
        match opcode with
          | L2JIT.RETURN -> nil_res
@@ -216,24 +230,27 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
 
          | L2JIT.LEFT (tr_list, instr) ->
              let res1, slot1 =
-               l2jit_dispatch_instr t tag states (Tree.closing tree t) instr
+               l2jit_dispatch_instr t (Tree.closing tree t) instr
              in
                l3jit_dispatch tr_list res1 auto.bottom_states t slot1 empty_slot
 
          | L2JIT.RIGHT (tr_list, instr) ->
-           let res2, slot2 = l2jit_dispatch_instr t tag states ctx instr in
-             l3jit_dispatch tr_list auto.bottom_states res2 t empty_slot slot2
+           let res2, slot2 =
+             l2jit_dispatch_instr t ctx instr
+           in
+           l3jit_dispatch tr_list auto.bottom_states res2 t empty_slot slot2
 
          | L2JIT.BOTH (tr_list, instr1, instr2) ->
              let res1, slot1 =
-               l2jit_dispatch_instr t tag states (Tree.closing tree t) instr1
+               l2jit_dispatch_instr t (Tree.closing tree t) instr1
+             in
+             let res2, slot2 =
+               l2jit_dispatch_instr t ctx instr2
              in
-             let res2, slot2 = l2jit_dispatch_instr t tag states ctx instr2 in
                l3jit_dispatch tr_list res1 res2 t slot1 slot2
 
-    and l2jit_dispatch_instr t tag states ctx instr =
+    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)
 
@@ -241,49 +258,34 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
        | L2JIT.NEXT_ELEMENT s -> LOOP ((Tree.next_element tree t), s, ctx)
 
        | L2JIT.TAGGED_DESCENDANT (s, tag) ->
-           LOOP_TAG ((Tree.tagged_descendant tree t tag), s, tag, ctx)
+         LOOP_TAG ((Tree.tagged_descendant tree t tag), s, tag, ctx)
 
        | L2JIT.TAGGED_FOLLOWING (s, tag) ->
-           LOOP_TAG((Tree.tagged_following_before tree t tag ctx), s, tag, ctx)
+         LOOP_TAG((Tree.tagged_following_before tree t tag ctx), s, tag, ctx)
 
        | L2JIT.SELECT_DESCENDANT (s, _, us) ->
-           LOOP((Tree.select_descendant tree t us), s, ctx)
+         LOOP((Tree.select_descendant tree t us), s, ctx)
 
        | L2JIT.SELECT_FOLLOWING (s, pt, us) ->
-           LOOP ((Tree.select_following_before tree t us ctx), s, ctx)
+         LOOP ((Tree.select_following_before tree t us ctx), s, ctx)
 
        | L2JIT.TAGGED_CHILD (s, tag) ->
-           LOOP_TAG((Tree.tagged_child tree t tag), s, tag, ctx)
+         LOOP_TAG((Tree.tagged_child tree t tag), s, tag, ctx)
 
        | L2JIT.TAGGED_FOLLOWING_SIBLING (s, tag) ->
-           LOOP_TAG((Tree.tagged_following_sibling tree t tag), s, tag, ctx)
+         LOOP_TAG((Tree.tagged_following_sibling tree t tag), s, tag, ctx)
 
        | L2JIT.SELECT_CHILD (s, _, us) ->
-           LOOP ((Tree.select_child tree t us), s, ctx)
+         LOOP ((Tree.select_child tree t us), s, ctx)
 
        | L2JIT.SELECT_FOLLOWING_SIBLING (s, _, us) ->
-           LOOP ((Tree.select_following_sibling tree t us), s, ctx)
+         LOOP ((Tree.select_following_sibling tree t us), s, ctx)
 
        | L2JIT.TAGGED_SUBTREE(s, tag) ->
-
-         let count = U.NS.subtree_tags tree t tag in
-         if count != U.NS.empty then
-           let r = Array.copy empty_slot in
-           r.(auto.last) <- count;
-           s,r
-         else
-           s,empty_slot
+         mark_subtree s (U.NS.subtree_tags tree t tag)
 
        | L2JIT.ELEMENT_SUBTREE(s) ->
-
-         let count = U.NS.subtree_elements tree t in
-         if count != U.NS.empty then
-           let r = Array.copy empty_slot in
-           r.(auto.last) <- count;
-           s,r
-         else
-           s,empty_slot
-
+         mark_subtree s (U.NS.subtree_elements tree t)
       in
       let r = LOOP (root, states, ctx) in
       (*L3JIT.stats err_formatter cache3; *)
@@ -314,12 +316,14 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
        Format.eprintf "%a -> %a\n" State.print state ns_print ns;
       end t
 
-
-    let eval_trans auto tree parent res1 res2 = assert false
-
+    let rec uniq = function
+      | ([] | [ _ ]) as l -> l
+      | e1 :: ((e2 :: ll) as l) -> if e1 == e2 then uniq l
+       else e1 :: e2 :: (uniq ll);;
 
     let bottom_up_run auto tree (query, pat) =
-      let leaves = Array.to_list (Tree.full_text_query query tree pat) in
+      let array = time ~msg:"Timing text query" (Tree.full_text_query query tree) pat in
+      let leaves = Array.to_list array in
       let states = auto.states in
       let res_len = (StateSet.max_elt states) + 1 in
       let empty_slot = Array.create res_len U.NS.empty in
@@ -330,7 +334,10 @@ DEFINE LOOP_TAG (t, states, tag, ctx) = (
            [] -> acc
          | node :: ll ->
            let res, lll = bottom_up_next node ll Tree.nil in
-           if (lll <> []) then Printf.eprintf "Leftover elements\n%!";
+           if (lll <> []) then
+             begin
+               eprintf "Leftover nodes: %i\n" (List.length lll);
+             end;
            res
 
       and bottom_up_next node rest stop =
@@ -400,7 +407,7 @@ let get_trans g auto tag states =
          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)
@@ -526,19 +533,19 @@ let dispatch_param1 conf id2 y0 y1 =
        if t = Node.nil || states == dummy_set then nil_res else
          let () = incr rule_counter in
          if !rule_counter land 65535 == 0 then begin Gc.minor() end;
-         let k = (t, states) in
-         let pstates = DCache.find dcache k in
-         let notfound = DCache.notfound pstates in
+(*       let k = (t, states) in*)
+(*       let pstates = DCache.find dcache k in
+         let notfound = DCache.notfound pstates in *)
          let rhs = Grammar2.get_rule g t in
          let id1 = Grammar2.get_id1 rhs in
          let id2 = Grammar2.get_id2 rhs in
          let conf = Grammar2.get_conf rhs in
-         if notfound then
+(*       if notfound then*)
            let ny0 = dispatch_param0 conf id2 y0 y1 in
            let ny1 = dispatch_param1 conf id2 y0 y1 in
            let res = dispatch_loop id1 states ny0 ny1 in
-           pstates.(0) <- res.in0;
-           pstates.(1) <- res.in1;
+(*         pstates.(0) <- res.in0;
+           pstates.(1) <- res.in1; *)
            res (*
            UCache.add ucache (t, states, fst res.out0, fst res.out1)
              res.main;
@@ -550,7 +557,7 @@ let dispatch_param1 conf id2 y0 y1 =
            { res with
              main = ((fst res.main), (U.close h (snd res.main)));
            } *)
-
+(*
            else
              let res0 = partial_loop y0 pstates.(0) in
              let res1 = partial_loop y1 pstates.(1) in
@@ -578,7 +585,7 @@ let dispatch_param1 conf id2 y0 y1 =
                out1 = res1.main;
                main = s, U.close h r;
              }
-
+*)
       and dispatch_loop id1 states ny0 ny1 =
          if Grammar2.is_non_terminal g id1 then
            rule_loop (Grammar2.non_terminal id1) states ny0 ny1
@@ -641,3 +648,113 @@ let dispatch_param1 conf id2 y0 y1 =
 
   end
 
+
+type 'a result_ops = {
+  empty : 'a;
+  subtree_tags : Tree.t -> Tree.node -> Tag.t -> 'a;
+  subtree_elements : Tree.t -> Tree.node -> 'a;
+  exec : 'a array -> 'a array -> 'a array -> Tree.node -> ResJIT.code -> unit;
+}
+
+let top_down_run rs auto tree root states ctx =
+  let res_len = StateSet.max_elt auto.states + 1 in
+  let empty_slot = Array.create res_len rs.empty in
+  let nil_res = auto.bottom_states, empty_slot in
+  let cache3 = ResJIT.create () in
+  let mark_subtree s subtree =
+    if subtree != rs.empty then
+      let r = Array.copy empty_slot in
+      r.(auto.last) <- subtree;
+      s,r
+    else
+      s,empty_slot
+  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 l3jit_dispatch trl s1 s2 t sl1 sl2 =
+    ResJIT.update rs.exec cache3 auto trl s1 s2 empty_slot sl1 sl2 tree t
+  in
+  let cache2 = L2JIT.create () in
+
+  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
+
+    | L2JIT.LEFT (tr_list, instr) ->
+      let res1, slot1 =
+               l2jit_dispatch_instr t (Tree.closing tree t) instr
+      in
+      l3jit_dispatch tr_list res1 auto.bottom_states t slot1 empty_slot
+
+    | L2JIT.RIGHT (tr_list, instr) ->
+           let res2, slot2 =
+             l2jit_dispatch_instr t ctx instr
+           in
+           l3jit_dispatch tr_list auto.bottom_states res2 t empty_slot slot2
+
+    | L2JIT.BOTH (tr_list, instr1, instr2) ->
+      let res1, slot1 =
+       l2jit_dispatch_instr t (Tree.closing tree t) instr1
+      in
+      let res2, slot2 =
+       l2jit_dispatch_instr t ctx instr2
+      in
+      l3jit_dispatch tr_list res1 res2 t slot1 slot2
+
+  and l2jit_dispatch_instr t ctx instr =
+    match instr with
+    | L2JIT.FIRST_CHILD s -> LOOP ((Tree.first_child tree t), s, ctx)
+    | L2JIT.NEXT_SIBLING s -> LOOP ((Tree.next_sibling tree t), s, ctx)
+
+    | L2JIT.FIRST_ELEMENT s -> LOOP ((Tree.first_element tree t), s, ctx)
+    | L2JIT.NEXT_ELEMENT s -> LOOP ((Tree.next_element tree t), s, ctx)
+
+    | L2JIT.TAGGED_DESCENDANT (s, tag) ->
+      LOOP_TAG ((Tree.tagged_descendant tree t tag), s, tag, ctx)
+
+    | L2JIT.TAGGED_FOLLOWING (s, tag) ->
+      LOOP_TAG((Tree.tagged_following_before tree t tag ctx), s, tag, ctx)
+
+    | L2JIT.SELECT_DESCENDANT (s, _, us) ->
+      LOOP((Tree.select_descendant tree t us), s, ctx)
+
+    | L2JIT.SELECT_FOLLOWING (s, pt, us) ->
+      LOOP ((Tree.select_following_before tree t us ctx), s, ctx)
+
+    | L2JIT.TAGGED_CHILD (s, tag) ->
+      LOOP_TAG((Tree.tagged_child tree t tag), s, tag, ctx)
+
+    | L2JIT.TAGGED_FOLLOWING_SIBLING (s, tag) ->
+      LOOP_TAG((Tree.tagged_following_sibling tree t tag), s, tag, ctx)
+
+    | L2JIT.SELECT_CHILD (s, _, us) ->
+      LOOP ((Tree.select_child tree t us), s, ctx)
+
+    | L2JIT.SELECT_FOLLOWING_SIBLING (s, _, us) ->
+      LOOP ((Tree.select_following_sibling tree t us), s, ctx)
+
+    | L2JIT.TAGGED_SUBTREE(s, tag) ->
+      mark_subtree s (rs.subtree_tags tree t tag)
+
+    | L2JIT.ELEMENT_SUBTREE(s) ->
+      mark_subtree s (rs.subtree_elements tree t)
+  in
+  let r = LOOP (root, states, ctx) in
+      (*L3JIT.stats err_formatter cache3; *)
+  r
+
+let full_top_down_run rs auto states tree root =
+  top_down_run rs auto tree root states (Tree.closing tree root)
+
+let top_down_run rs auto tree root =
+  let res, slot = full_top_down_run rs auto auto.init tree root in
+  slot.(StateSet.min_elt auto.topdown_marking_states)