Update HACKING file with new build instructions
[SXSI/xpathcomp.git] / src / runtime.ml
1 INCLUDE "debug.ml"
2 INCLUDE "utils.ml"
3
4 open Format
5 open Ata
6 module type S = sig
7   type result_set
8   val top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
9   val bottom_up_run : Ata.t -> Tree.t -> Compile.text_query * string -> result_set
10 end
11
12 module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
13   struct
14
15     type result_set = U.NS.t;;
16
17     let eval_form auto s1 s2 f =
18       let rec loop f =
19         match Formula.expr f with
20           | Formula.False | Formula.True | Formula.Pred _ -> f, []
21           | Formula.Atom(`Left, b, q) ->
22               Formula.of_bool (b == (StateSet.mem q s1)),
23               if b && StateSet.mem q auto.topdown_marking_states then [ResJIT.LEFT q] else []
24           | Formula.Atom (`Right, b, q) ->
25               Formula.of_bool(b == (StateSet.mem q s2)),
26               if b && StateSet.mem q auto.topdown_marking_states then [ResJIT.RIGHT q] else []
27
28           | Formula.Or(f1, f2) ->
29               let b1, i1 = loop f1 in
30               let b2, i2 = loop f2 in
31               Formula.or_pred b1 b2, i1 @ i2
32           | Formula.And(f1, f2) ->
33               let b1, i1 = loop f1 in
34               let b2, i2 = loop f2 in
35               Formula.and_pred b1 b2, i1 @ i2
36       in
37       loop f
38
39
40     let eval_trans auto s1 s2 trans =
41       Translist.fold
42         (fun t ((a_st, a_op, a_todo) as acc)->
43            let q, _, m, f = Transition.node t in
44            let form, ops = eval_form auto s1 s2 f in
45            match Formula.expr form with
46              | Formula.True ->
47                StateSet.add q a_st,
48                (q, (if m then (ResJIT.SELF() :: ops) else ops)):: a_op,
49                a_todo
50              | Formula.False -> acc
51              | Formula.Pred p -> a_st, a_op,
52                (p.Tree.Predicate.node, q, [(q,(if m then (ResJIT.SELF() :: ops) else ops))]) :: a_todo
53              | _ -> assert false
54         ) trans (StateSet.empty, [], [])
55
56
57
58     module L3JIT =
59       struct
60
61         type opcode = (t -> t -> t -> Tree.t -> Tree.node -> StateSet.t * t)
62
63         type t = opcode Cache.t Cache.t Cache.t
64
65         let dummy _ _ _ _ _ = failwith "Uninitialized L3JIT"
66
67         let create () = Cache.Lvl3.create 1024 dummy
68
69         let stats fmt d =
70           let d = Cache.Lvl3.to_array d in
71           let len = Array.fold_left
72             (fun acc a ->
73                Array.fold_left (fun acc2 a2 -> Array.length a2 + acc2) acc a) 0 d
74           in
75
76           let lvl1 =
77             Array.fold_left
78               (fun acc a -> if Array.length a == 0 then acc else acc+1) 0 d in
79           let lvl2 = Array.fold_left
80             (fun acc a ->
81                Array.fold_left (fun acc2 a2 -> if Array.length a2 == 0 then acc2 else acc2+1)
82                  acc a) 0 d
83           in
84           let lvl3 = Array.fold_left
85             (fun acc a ->
86                Array.fold_left (fun acc2 a2 ->
87                                   Array.fold_left
88                                     (fun acc3 a3 -> if a3 == dummy then acc3 else acc3+1) acc2 a2)
89                  acc a) 0 d
90       in
91         fprintf fmt "L3JIT Statistics:
92 \t%i entries
93 \t%i used L1 lines
94 \t%i used L2 lines
95 \t%i used L3 lines
96 \ttable size: %ikb\n"
97           len lvl1 lvl2 lvl3 (Ocaml.size_kb d)
98
99         let find t tlist s1 s2 =
100           Cache.Lvl3.find t
101             (Uid.to_int tlist.Translist.Node.id)
102             (Uid.to_int s1.StateSet.Node.id)
103             (Uid.to_int s2.StateSet.Node.id)
104
105         let add t tlist s1 s2 v =
106           Cache.Lvl3.add t
107             (Uid.to_int tlist.Translist.Node.id)
108             (Uid.to_int s1.StateSet.Node.id)
109             (Uid.to_int s2.StateSet.Node.id)
110             v
111
112         let compile auto trl s1 s2 =
113           let orig_s1, orig_s2 =
114             Translist.fold (fun t (a1, a2) ->
115                           let _, _, _, f = Transition.node t in
116                           let (_, _, fs1), (_, _, fs2) = Formula.st f in
117                             (StateSet.union s1 fs1, StateSet.union s2 fs2)
118                        ) trl (StateSet.empty, StateSet.empty)
119           in
120           let ns1 = StateSet.inter s1 orig_s1
121           and ns2 = StateSet.inter s2 orig_s2 in
122           let res, ops, todo = eval_trans auto ns1 ns2 trl in
123           let code, not_marking = ResJIT.compile ops in
124           let todo_code, todo_notmarking =
125             List.fold_left (fun (l, b) (p, q, o) -> let c, b' = ResJIT.compile o in
126                                          (p, q, c)::l, b && b')
127               ([], not_marking) todo
128           in
129           let opcode = res, code, todo_notmarking, todo_code in
130           opcode
131
132         let gen_code auto tlist s1 s2 =
133           let res, code, not_marking, todo_code = compile auto tlist s1 s2 in
134           let f =
135             if todo_code == [] then
136               if not_marking then begin fun empty_slot sl1 sl2 _ node ->
137                 let slot1_empty = sl1 == empty_slot
138                 and slot2_empty = sl2 == empty_slot in
139                 if slot1_empty && slot2_empty then res,sl2
140                 else
141                   let sl =
142                     if slot2_empty then
143                       if slot1_empty then
144                         Array.copy empty_slot
145                       else sl1
146                     else sl2
147                   in
148                   U.exec sl sl1 sl2 node code;
149                   res, sl
150               end
151               else (* marking *) begin fun empty_slot sl1 sl2 _ node ->
152                 let sl =
153                   if sl2 == empty_slot  then
154                     if sl1 == empty_slot then
155                       Array.copy empty_slot
156                     else sl1
157                   else sl2
158                 in
159                 U.exec sl sl1 sl2 node code;
160                 res, sl
161               end
162               else (* todo != [] *)
163               begin fun empty_slot sl1 sl2 tree node ->
164                 let sl =
165                   if sl2 == empty_slot  then
166                     if sl1 == empty_slot then
167                       Array.copy empty_slot
168                     else sl1
169                   else sl2
170                 in
171                 U.exec sl sl1 sl2 node code;
172                 List.fold_left
173                   (fun ares (p, q, code) ->
174                     if !p tree node then begin
175                       if code != ResJIT.Nil then U.exec sl sl1 sl2 node code;
176                       StateSet.add q ares
177                     end
178                     else ares) res todo_code, sl
179
180               end
181           in
182           f
183
184         let cache_apply cache auto tlist s1 s2 =
185           let f = gen_code auto tlist s1 s2 in
186           add cache tlist s1 s2 f; f
187       end
188
189 DEFINE LOOP (t, states, ctx) = (
190   let _t = (t) in
191   if _t == Tree.nil then nil_res
192   else
193     let tag = Tree.tag tree _t in
194       l2jit_dispatch
195         _t tag (states) (ctx) (L2JIT.find cache2 tag (states))
196 )
197
198 DEFINE LOOP_TAG (t, states, tag, ctx) = (
199   let _t = (t) in (* to avoid duplicating expression t *)
200   if _t == Tree.nil then nil_res
201   else
202     l2jit_dispatch
203       _t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states)))
204
205     let top_down_run auto tree root states ctx =
206       let res_len = (StateSet.max_elt auto.states) + 1 in
207       let empty_slot = Array.create res_len U.NS.empty in
208       let nil_res = auto.bottom_states, empty_slot in
209       let cache3 = L3JIT.create () in
210
211       let l3jit_dispatch trl s1 s2 t sl1 sl2 =
212         let f = L3JIT.find cache3 trl s1 s2 in
213         if f == L3JIT.dummy then (L3JIT.cache_apply cache3 auto trl s1 s2) empty_slot sl1 sl2 tree t
214         else f empty_slot sl1 sl2 tree t
215
216       in
217       let cache2 = L2JIT.create () in
218
219       let () = D_TRACE_(at_exit (fun () -> L2JIT.stats Format.err_formatter cache2)) in
220
221       let rec l2jit_dispatch t tag states ctx opcode =
222         match opcode with
223           | L2JIT.RETURN () -> nil_res
224           | L2JIT.CACHE () ->
225               let opcode = L2JIT.compile cache2 auto tree tag states in
226                 l2jit_dispatch t tag states ctx opcode
227
228           | L2JIT.LEFT (tr_list, instr) ->
229               let res1, slot1 =
230                 l2jit_dispatch_instr t tag states (Tree.closing tree t) instr true
231               in
232                 l3jit_dispatch tr_list res1 auto.bottom_states t slot1 empty_slot
233
234           | L2JIT.RIGHT (tr_list, instr) ->
235             let res2, slot2 = l2jit_dispatch_instr t tag states ctx instr false in
236               l3jit_dispatch tr_list auto.bottom_states res2 t empty_slot slot2
237
238           | L2JIT.BOTH (tr_list, instr1, instr2) ->
239               let res1, slot1 =
240                 l2jit_dispatch_instr t tag states (Tree.closing tree t) instr1 true
241               in
242               let res2, slot2 = l2jit_dispatch_instr t tag states ctx instr2 false in
243                 l3jit_dispatch tr_list res1 res2 t slot1 slot2
244
245     and l2jit_dispatch_instr t tag states ctx instr _left =
246       match instr with
247         | L2JIT.NOP () -> nil_res
248         | L2JIT.FIRST_CHILD s -> LOOP ((Tree.first_child tree t), s, ctx)
249         | L2JIT.NEXT_SIBLING s -> LOOP ((Tree.next_sibling tree t), s, ctx)
250
251         | L2JIT.FIRST_ELEMENT s -> LOOP ((Tree.first_element tree t), s, ctx)
252         | L2JIT.NEXT_ELEMENT s -> LOOP ((Tree.next_element tree t), s, ctx)
253
254         | L2JIT.TAGGED_DESCENDANT (s, tag) ->
255             LOOP_TAG ((Tree.tagged_descendant tree t tag), s, tag, ctx)
256
257         | L2JIT.TAGGED_FOLLOWING (s, tag) ->
258             LOOP_TAG((Tree.tagged_following_before tree t tag ctx), s, tag, ctx)
259
260         | L2JIT.SELECT_DESCENDANT (s, _, us) ->
261             LOOP((Tree.select_descendant tree t us), s, ctx)
262
263         | L2JIT.SELECT_FOLLOWING (s, pt, us) ->
264             LOOP ((Tree.select_following_before tree t us ctx), s, ctx)
265
266         | L2JIT.TAGGED_CHILD (s, tag) ->
267             LOOP_TAG((Tree.tagged_child tree t tag), s, tag, ctx)
268
269         | L2JIT.TAGGED_FOLLOWING_SIBLING (s, tag) ->
270             LOOP_TAG((Tree.tagged_following_sibling tree t tag), s, tag, ctx)
271
272         | L2JIT.SELECT_CHILD (s, _, us) ->
273             LOOP ((Tree.select_child tree t us), s, ctx)
274
275         | L2JIT.SELECT_FOLLOWING_SIBLING (s, _, us) ->
276             LOOP ((Tree.select_following_sibling tree t us), s, ctx)
277
278         | L2JIT.TAGGED_SUBTREE(s, tag) ->
279
280           let count = U.NS.subtree_tags tree t tag in
281                 if count != U.NS.empty then
282                   let r = Array.copy empty_slot in
283                     r.(auto.last) <- count;
284                     s,r
285                 else
286                   s,empty_slot
287
288         | L2JIT.ELEMENT_SUBTREE(s) ->
289
290               let count = U.NS.subtree_elements tree t in
291                 if count != U.NS.empty then
292                   let r = Array.copy empty_slot in
293                     r.(auto.last) <- count;
294                     s,r
295                 else
296                   s,empty_slot
297
298     in
299       LOOP (root, states, ctx)
300
301     let full_top_down_run auto states tree root =
302       (*Ata.init (); *)
303       top_down_run auto tree root states (Tree.closing tree root)
304
305     let top_down_run auto tree root =
306       (*Ata.init (); *)
307       let res, slot = full_top_down_run auto auto.init tree root in
308       slot.(StateSet.min_elt auto.topdown_marking_states)
309
310
311     (*** Bottom-up evaluation function **)
312
313     let ns_print fmt t =
314       Format.fprintf fmt "{ ";
315       U.NS.iter begin fun node ->
316         Format.fprintf fmt "%a " Node.print node;
317       end t;
318       Format.fprintf fmt "}"
319
320     let slot_print fmt t =
321       Array.iteri begin fun state ns ->
322         Format.eprintf "%a -> %a\n" State.print state ns_print ns;
323       end t
324
325
326     let eval_trans auto tree parent res1 res2 = assert false
327
328
329     let bottom_up_run auto tree (query, pat) =
330       let leaves = Array.to_list (Tree.full_text_query query tree pat) in
331       let states = auto.states in
332       let res_len = (StateSet.max_elt states) + 1 in
333       let empty_slot = Array.create res_len U.NS.empty in
334       let nil_res = auto.bottom_states, empty_slot in
335       let cache = Cache.Lvl3.create 1024 L3JIT.dummy in
336       let rec loop_leaves l acc =
337         match l with
338             [] -> acc
339           | node :: ll ->
340             let res, lll = bottom_up_next node ll Tree.nil in
341             if (lll <> []) then Printf.eprintf "Leftover elements\n%!";
342             res
343
344       and bottom_up_next node rest stop =
345         let fs = Tree.first_child tree node in
346         let res1 =
347           if fs == Tree.nil then nil_res
348           else full_top_down_run auto states tree fs
349         in
350         move_up node res1 true rest stop
351
352       and move_up node res is_left rest stop =
353         if node == stop then res, rest
354         else
355           let prev_sibling = Tree.prev_sibling tree node in
356           let is_left' = prev_sibling == Tree.nil in
357           let real_parent = Tree.parent tree node in
358           let parent =
359             if is_left' then real_parent else max (Tree.first_child tree real_parent) stop
360           in
361           (* let parent = if is_left' then Tree.parent tree node else prev_sibling in *)
362           let (s1, sl1), (s2, sl2), rest' =
363             if is_left then match rest with
364                 [] -> res, nil_res, rest
365               | next :: rest' ->
366                 if Tree.is_right_descendant tree node next
367                 then
368                   let res2, rest' = bottom_up_next next rest' node in
369                   res, res2, rest'
370                 else res, nil_res, rest
371             else
372               nil_res, res, rest
373           in
374           let tag = Tree.tag tree node in
375           let id1 = Uid.to_int s1.StateSet.Node.id in
376           let id2 = Uid.to_int s2.StateSet.Node.id in
377           let code =
378             let code = Cache.Lvl3.find cache tag id1 id2 in
379             if code == L3JIT.dummy then
380               let trl =
381                 StateSet.fold
382                   (fun q acc ->
383                     List.fold_left (fun acc' (labels, tr) ->
384                       if labels == TagSet.any || TagSet.mem tag labels
385                       then Translist.cons tr acc' else acc')
386                       acc
387                       (Hashtbl.find auto.trans q)
388                   )
389                   states
390                   Translist.nil
391               in
392               let code = L3JIT.gen_code auto trl s1 s2 in
393               Cache.Lvl3.add cache tag id1 id2 code; code
394             else code
395           in
396           let res' = code empty_slot sl1 sl2 tree node in
397           move_up parent res' is_left' rest' stop
398       in
399       let _, slot = loop_leaves leaves (nil_res) in
400       slot.(StateSet.min_elt auto.topdown_marking_states)
401
402
403   end
404