Small refactoring:
[SXSI/xpathcomp.git] / src / runtime.ml
1 INCLUDE "debug.ml"
2 INCLUDE "trace.ml"
3 INCLUDE "utils.ml"
4
5 open Format
6 open Ata
7 module type S = sig
8   type result_set
9   val top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
10   val bottom_up_run : Ata.t -> Tree.t -> Compile.text_query * string -> result_set
11   val grammar_run : Ata.t -> Grammar2.t -> unit -> result_set
12
13 end
14
15 module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
16   struct
17
18     type result_set = U.NS.t;;
19
20     let eval_form auto s1 s2 f =
21       let rec loop f =
22         match Formula.expr f with
23           | Formula.False | Formula.True | Formula.Pred _ -> f, []
24           | Formula.Atom(`Left, b, q) ->
25               Formula.of_bool (b == (StateSet.mem q s1)),
26               if b && StateSet.mem q auto.topdown_marking_states then [ResJIT.LEFT q] else []
27           | Formula.Atom (`Right, b, q) ->
28               Formula.of_bool(b == (StateSet.mem q s2)),
29               if b && StateSet.mem q auto.topdown_marking_states then [ResJIT.RIGHT q] else []
30           | Formula.Atom (`Epsilon, _, _) -> assert false
31
32           | Formula.Or(f1, f2) ->
33               let b1, i1 = loop f1 in
34               let b2, i2 = loop f2 in
35               Formula.or_pred b1 b2, i1 @ i2
36           | Formula.And(f1, f2) ->
37               let b1, i1 = loop f1 in
38               let b2, i2 = loop f2 in
39               Formula.and_pred b1 b2, i1 @ i2
40       in
41       loop f
42
43
44     let eval_trans auto s1 s2 trans =
45       Translist.fold
46         (fun t ((a_st, a_op, a_todo) as acc)->
47            let q, _, m, f = Transition.node t in
48            let form, ops = eval_form auto s1 s2 f in
49            match Formula.expr form with
50              | Formula.True ->
51                StateSet.add q a_st,
52                (q, (if m then (ResJIT.SELF() :: ops) else ops)):: a_op,
53                a_todo
54              | Formula.False -> acc
55              | Formula.Pred p -> a_st, a_op,
56                (p.Tree.Predicate.node, q, [(q,(if m then (ResJIT.SELF() :: ops) else ops))]) :: a_todo
57              | _ -> assert false
58         ) trans (StateSet.empty, [], [])
59
60
61
62     module L3JIT =
63       struct
64
65         type opcode = (t -> t -> t -> Tree.t -> Tree.node -> StateSet.t * t)
66
67         type t = opcode Cache.Lvl3.t
68
69         let dummy _ _ _ _ _ = failwith "Uninitialized L3JIT"
70
71
72         let show_stats a =
73           let count = ref 0 in
74           Cache.Lvl3.iteri (fun _ _ _ _ b -> if not b then incr count) a;
75           eprintf "%!L3JIT: %i used entries\n%!" !count
76         let create () =
77           let v = Cache.Lvl3.create 1024 dummy in
78           if !Options.verbose then at_exit (fun () -> show_stats v);
79           v
80
81         let find t tlist s1 s2 =
82           Cache.Lvl3.find t
83             (Uid.to_int s2.StateSet.Node.id)
84             (Uid.to_int s1.StateSet.Node.id)
85             (Uid.to_int tlist.Translist.Node.id)
86
87         let add t tlist s1 s2 v =
88           Cache.Lvl3.add t
89             (Uid.to_int s2.StateSet.Node.id)
90             (Uid.to_int s1.StateSet.Node.id)
91             (Uid.to_int tlist.Translist.Node.id)
92             v
93
94         let compile auto trl s1 s2 =
95           let orig_s1, orig_s2 =
96             Translist.fold (fun t (a1, a2) ->
97                           let _, _, _, f = Transition.node t in
98                           let (_, _, fs1), (_, _, fs2) = Formula.st f in
99                             (StateSet.union a1 fs1, StateSet.union a2 fs2)
100                        ) trl (StateSet.empty, StateSet.empty)
101           in
102           let ns1 = StateSet.inter s1 orig_s1
103           and ns2 = StateSet.inter s2 orig_s2 in
104           let res, ops, todo = eval_trans auto ns1 ns2 trl in
105           let code, not_marking = ResJIT.compile ops in
106           let todo_code, todo_notmarking =
107             List.fold_left (fun (l, b) (p, q, o) -> let c, b' = ResJIT.compile o in
108                                          (p, q, c)::l, b && b')
109               ([], not_marking) todo
110           in
111           let opcode = res, code, todo_notmarking, todo_code in
112           opcode
113
114         let gen_code auto tlist s1 s2 =
115           let res, code, not_marking, todo_code = compile auto tlist s1 s2 in
116           let f =
117             if todo_code == [] then
118               if not_marking then begin fun empty_slot sl1 sl2 _ node ->
119                 let slot1_empty = sl1 == empty_slot
120                 and slot2_empty = sl2 == empty_slot in
121                 if slot1_empty && slot2_empty then res,sl2
122                 else
123                   let sl =
124                     if slot2_empty then
125                       if slot1_empty then
126                         Array.copy empty_slot
127                       else sl1
128                     else sl2
129                   in
130                   U.exec sl sl1 sl2 node code;
131                   res, sl
132               end
133               else (* marking *) begin fun empty_slot sl1 sl2 _ node ->
134                 let sl =
135                   if sl2 == empty_slot  then
136                     if sl1 == empty_slot then
137                       Array.copy empty_slot
138                     else sl1
139                   else sl2
140                 in
141                 U.exec sl sl1 sl2 node code;
142                 res, sl
143               end
144               else (* todo != [] *)
145               begin fun empty_slot sl1 sl2 tree node ->
146                 let sl =
147                   if sl2 == empty_slot  then
148                     if sl1 == empty_slot then
149                       Array.copy empty_slot
150                     else sl1
151                   else sl2
152                 in
153                 U.exec sl sl1 sl2 node code;
154                 List.fold_left
155                   (fun ares (p, q, code) ->
156                     if !p tree node then begin
157                       if code != ResJIT.Nil then U.exec sl sl1 sl2 node code;
158                       StateSet.add q ares
159                     end
160                     else ares) res todo_code, sl
161
162               end
163           in
164           f
165
166         let cache_apply cache auto tlist s1 s2 =
167           let f = gen_code auto tlist s1 s2 in
168           TRACE("grammar", 2, __ "Inserting: %i, %a, %a\n%!"
169             (Uid.to_int tlist.Translist.Node.id) StateSet.print s1 StateSet.print s2);
170           add cache tlist s1 s2 f; f
171       end
172
173 DEFINE LOOP (t, states, ctx) = (
174   let _t = t in
175   TRACE("top-down-run", 3,
176         __ "Entering node %i with loop (tag %s, context %i) with states %a\n%!"
177           (Node.to_int _t)
178           (Tag.to_string (Tree.tag tree _t))
179           (Node.to_int (ctx))
180           (StateSet.print) (states));
181   if _t == Tree.nil then nil_res
182   else
183     let tag = Tree.tag tree _t in
184       l2jit_dispatch
185         _t tag (states) (ctx) (L2JIT.find cache2 tag (states))
186 )
187
188 DEFINE LOOP_TAG (t, states, tag, ctx) = (
189   let _t = (t) in (* to avoid duplicating expression t *)
190   TRACE("top-down-run", 3,
191         __ "Entering node %i with loop_tag (tag %s, context %i) with states %a\n%!"
192           (Node.to_int _t)
193           (Tag.to_string (tag))
194           (Node.to_int (ctx))
195           (StateSet.print) (states));
196   if _t == Tree.nil then nil_res
197   else
198     l2jit_dispatch
199       _t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states)))
200
201 DEFINE LOOP(t, states, ctx) = loop (t) (states) (ctx)
202 DEFINE LOOP_TAG(t, states, tag, ctx) = loop_tag (t) (states) (ctx) (tag)
203
204     let top_down_run auto tree root states ctx =
205       let res_len = StateSet.max_elt auto.states + 1 in
206       let empty_slot = Array.create res_len U.NS.empty in
207       let nil_res = auto.bottom_states, empty_slot in
208       let cache3 = L3JIT.create () in
209       let mark_subtree  =
210         fun s subtree -> if subtree != U.NS.empty then
211           let r = Array.copy empty_slot in
212           r.(auto.last) <- subtree;
213           s,r
214         else
215           s,empty_slot
216       in
217       let l3jit_dispatch trl s1 s2 t sl1 sl2 =
218         let f = L3JIT.find cache3 trl s1 s2 in
219         if f == L3JIT.dummy then (L3JIT.cache_apply cache3 auto trl s1 s2) empty_slot sl1 sl2 tree t
220         else f empty_slot sl1 sl2 tree t
221
222       in
223       let cache2 = L2JIT.create () in
224
225       let rec loop t states ctx =
226         if t == Tree.nil then nil_res
227         else
228           let tag = Tree.tag tree t in
229           l2jit_dispatch
230             t tag (states) (ctx) (L2JIT.find cache2 tag (states))
231       and loop_tag t states ctx tag =
232         if t == Tree.nil then nil_res
233         else
234           l2jit_dispatch
235             t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states))
236       and l2jit_dispatch t tag states ctx opcode =
237         match opcode with
238           | L2JIT.RETURN -> nil_res
239           | L2JIT.CACHE ->
240               let opcode = L2JIT.compile cache2 auto tree tag states in
241                 l2jit_dispatch t tag states ctx opcode
242
243           | L2JIT.LEFT (tr_list, instr) ->
244               let res1, slot1 =
245                 l2jit_dispatch_instr t (Tree.closing tree t) instr
246               in
247                 l3jit_dispatch tr_list res1 auto.bottom_states t slot1 empty_slot
248
249           | L2JIT.RIGHT (tr_list, instr) ->
250             let res2, slot2 =
251               l2jit_dispatch_instr t ctx instr
252             in
253             l3jit_dispatch tr_list auto.bottom_states res2 t empty_slot slot2
254
255           | L2JIT.BOTH (tr_list, instr1, instr2) ->
256               let res1, slot1 =
257                 l2jit_dispatch_instr t (Tree.closing tree t) instr1
258               in
259               let res2, slot2 =
260                 l2jit_dispatch_instr t ctx instr2
261               in
262                 l3jit_dispatch tr_list res1 res2 t slot1 slot2
263
264     and l2jit_dispatch_instr t ctx instr =
265       match instr with
266         | L2JIT.FIRST_CHILD s -> LOOP ((Tree.first_child tree t), s, ctx)
267         | L2JIT.NEXT_SIBLING s -> LOOP ((Tree.next_sibling tree t), s, ctx)
268
269         | L2JIT.FIRST_ELEMENT s -> LOOP ((Tree.first_element tree t), s, ctx)
270         | L2JIT.NEXT_ELEMENT s -> LOOP ((Tree.next_element tree t), s, ctx)
271
272         | L2JIT.TAGGED_DESCENDANT (s, tag) ->
273           LOOP_TAG ((Tree.tagged_descendant tree t tag), s, tag, ctx)
274
275         | L2JIT.TAGGED_FOLLOWING (s, tag) ->
276           LOOP_TAG((Tree.tagged_following_before tree t tag ctx), s, tag, ctx)
277
278         | L2JIT.SELECT_DESCENDANT (s, _, us) ->
279           LOOP((Tree.select_descendant tree t us), s, ctx)
280
281         | L2JIT.SELECT_FOLLOWING (s, pt, us) ->
282           LOOP ((Tree.select_following_before tree t us ctx), s, ctx)
283
284         | L2JIT.TAGGED_CHILD (s, tag) ->
285           LOOP_TAG((Tree.tagged_child tree t tag), s, tag, ctx)
286
287         | L2JIT.TAGGED_FOLLOWING_SIBLING (s, tag) ->
288           LOOP_TAG((Tree.tagged_following_sibling tree t tag), s, tag, ctx)
289
290         | L2JIT.SELECT_CHILD (s, _, us) ->
291           LOOP ((Tree.select_child tree t us), s, ctx)
292
293         | L2JIT.SELECT_FOLLOWING_SIBLING (s, _, us) ->
294           LOOP ((Tree.select_following_sibling tree t us), s, ctx)
295
296         | L2JIT.TAGGED_SUBTREE(s, tag) ->
297           mark_subtree s (U.NS.subtree_tags tree t tag)
298
299         | L2JIT.ELEMENT_SUBTREE(s) ->
300           mark_subtree s (U.NS.subtree_elements tree t)
301       in
302       let r = LOOP (root, states, ctx) in
303       (*L3JIT.stats err_formatter cache3; *)
304       r
305
306     let full_top_down_run auto states tree root =
307       (*Ata.init (); *)
308       top_down_run auto tree root states (Tree.closing tree root)
309
310     let top_down_run auto tree root =
311       (*Ata.init (); *)
312       let res, slot = full_top_down_run auto auto.init tree root in
313
314       slot.(StateSet.min_elt auto.topdown_marking_states)
315
316
317     (*** Bottom-up evaluation function **)
318
319     let ns_print fmt t =
320       Format.fprintf fmt "{ ";
321       U.NS.iter begin fun node ->
322         Format.fprintf fmt "%a " Node.print node;
323       end t;
324       Format.fprintf fmt "}"
325
326     let slot_print fmt t =
327       Array.iteri begin fun state ns ->
328         Format.eprintf "%a -> %a\n" State.print state ns_print ns;
329       end t
330
331
332     let eval_trans auto tree parent res1 res2 = assert false
333
334     let rec uniq = function
335       | ([] | [ _ ]) as l -> l
336       | e1 :: ((e2 :: ll) as l) -> if e1 == e2 then uniq l
337         else e1 :: e2 :: (uniq ll);;
338
339     let bottom_up_run auto tree (query, pat) =
340       let array = time ~msg:"Timing text query" (Tree.full_text_query query tree) pat in
341       let leaves = Array.to_list array in
342       let states = auto.states in
343       let res_len = (StateSet.max_elt states) + 1 in
344       let empty_slot = Array.create res_len U.NS.empty in
345       let nil_res = auto.bottom_states, empty_slot in
346       let cache = Cache.Lvl3.create 1024 L3JIT.dummy in
347       let rec loop_leaves l acc =
348         match l with
349             [] -> acc
350           | node :: ll ->
351             let res, lll = bottom_up_next node ll Tree.nil in
352             if (lll <> []) then
353               begin
354                 eprintf "Leftover nodes: %i\n" (List.length lll);
355               end;
356             res
357
358       and bottom_up_next node rest stop =
359         let fs = Tree.first_child tree node in
360         let res1 =
361           if fs == Tree.nil then nil_res
362           else full_top_down_run auto states tree fs
363         in
364         move_up node res1 true rest stop
365
366       and move_up node res is_left rest stop =
367         if node == stop then res, rest
368         else
369           let prev_sibling = Tree.prev_sibling tree node in
370           let is_left' = prev_sibling == Tree.nil in
371           let real_parent = Tree.parent tree node in
372           let parent =
373             if is_left' then real_parent else max (Tree.first_child tree real_parent) stop
374           in
375           (* let parent = if is_left' then Tree.parent tree node else prev_sibling in *)
376           let (s1, sl1), (s2, sl2), rest' =
377             if is_left then match rest with
378                 [] -> res, nil_res, rest
379               | next :: rest' ->
380                 if Tree.is_right_descendant tree node next
381                 then
382                   let res2, rest' = bottom_up_next next rest' node in
383                   res, res2, rest'
384                 else res, nil_res, rest
385             else
386               nil_res, res, rest
387           in
388           let tag = Tree.tag tree node in
389           let id1 = Uid.to_int s1.StateSet.Node.id in
390           let id2 = Uid.to_int s2.StateSet.Node.id in
391           let code =
392             let code = Cache.Lvl3.find cache tag id1 id2 in
393             if code == L3JIT.dummy then
394               let trl =
395                 StateSet.fold
396                   (fun q acc ->
397                     List.fold_left (fun acc' (labels, tr) ->
398                       if labels == TagSet.any || TagSet.mem tag labels
399                       then Translist.cons tr acc' else acc')
400                       acc
401                       (Hashtbl.find auto.trans q)
402                   )
403                   states
404                   Translist.nil
405               in
406               let code = L3JIT.gen_code auto trl s1 s2 in
407               Cache.Lvl3.add cache tag id1 id2 code; code
408             else code
409           in
410           let res' = code empty_slot sl1 sl2 tree node in
411           move_up parent res' is_left' rest' stop
412       in
413       let _, slot = loop_leaves leaves (nil_res) in
414       slot.(StateSet.min_elt auto.topdown_marking_states)
415
416 let get_trans g auto tag states =
417   StateSet.fold (fun q tr_acc ->
418     List.fold_left
419       (fun ((lstates, rstates, tacc) as acc) (ts, trs) ->
420         if TagSet.mem (Tag.translate tag) ts then
421           if not (TagSet.mem Tag.attribute ts) && Grammar2.is_attribute g tag
422           then acc
423               else
424             let _, _, _, phi = Transition.node trs in
425                 let (_,_,l), (_,_,r) = Formula.st phi in
426                 (StateSet.union l lstates,
427                  StateSet.union r rstates,
428                  Translist.cons trs tacc)
429         else acc)
430       tr_acc (Hashtbl.find auto.trans q)
431   ) states (StateSet.empty, StateSet.empty, Translist.nil)
432
433 (*  Grammar run *)
434 let dispatch_param0 conf id2 y0 y1 =
435   match conf with
436   | Grammar2.C0 | Grammar2.C2 -> Grammar2.Node0 id2
437   | Grammar2.C1 | Grammar2.C5 -> Grammar2.Node1(id2,y0)
438   | Grammar2.C3 | Grammar2.C6 -> y0
439   | Grammar2.C4 -> Grammar2.Node2(id2, y0, y1)
440
441 let dispatch_param1 conf id2 y0 y1 =
442   match conf with
443   | Grammar2.C2 -> y0
444   | Grammar2.C3 -> Grammar2.Node0 id2
445   | Grammar2.C5 -> y1
446   | Grammar2.C6 -> Grammar2.Node1(id2, y1)
447   | _ -> Grammar2.dummy_param
448
449     module K_down = struct
450       type t = Grammar2.n_symbol * StateSet.t
451       let hash (x,y) = HASHINT2(Node.to_int x, Uid.to_int y.StateSet.Node.id)
452       let equal (x1,y1) (x2,y2) = x1 == x2 && y1 == y2
453     end
454
455     module K_up = struct
456       type t = Grammar2.n_symbol * StateSet.t * StateSet.t * StateSet.t
457       let hash (a,b,c,d) =
458         HASHINT4 (Node.to_int a,
459                   Uid.to_int b.StateSet.Node.id,
460                   Uid.to_int c.StateSet.Node.id,
461                   Uid.to_int d.StateSet.Node.id)
462       let equal (a1, b1, c1, d1) (a2, b2, c2, d2) =
463         a1 == a2 && b1  == b2 && c1 == c2 && d1 == d2
464     end
465
466     module DCache =
467       struct
468         include Hashtbl.Make(K_down)
469         let dummy = StateSet.singleton State.dummy
470         let notfound l = l.(0) == dummy && l.(1) == dummy
471         let find h k =
472           try
473             find h k
474           with
475             Not_found ->
476               let a = [| dummy; dummy |] in
477               add h k a;
478               a
479       end
480     module UCache = Hashtbl.Make(K_up)
481     type result = {
482       in0 : StateSet.t;
483       in1 : StateSet.t;
484       out0 : StateSet.t * U.t;
485       out1 : StateSet.t * U.t;
486       main : StateSet.t * U.t
487     }
488     let mk_empty e =
489       { in0 = StateSet.empty;
490         in1 = StateSet.empty;
491         out0 = e;
492         out1 = e;
493         main = e
494       }
495     let mk_nil s v  =
496       {
497         mk_empty (s,v) with
498           out0 = StateSet.empty,v;
499           out1 = StateSet.empty,v;
500       }
501
502     let grammar_run auto g () =
503       let dummy_leaf = Grammar2.dummy_param in
504       let dummy_set = StateSet.singleton State.dummy in
505       let res_len = (StateSet.max_elt auto.states) + 1 in
506       let empty_slot = Array.create res_len U.NS.empty in
507       let nil_res = mk_nil auto.bottom_states empty_slot in
508       let empty_res = mk_empty (StateSet.empty, empty_slot) in
509       let cache3 = L3JIT.create () in
510       let dummy2 = (StateSet.empty, StateSet.empty, Translist.nil) in
511       let cache2 = Cache.Lvl2.create 512 dummy2 in
512       let rule_counter = ref 0 in
513       let preorder_counter = ref 0 in
514       let dcache = DCache.create 1023 in
515       let ucache = UCache.create 1023 in
516       let term_array = [| StateSet.empty; StateSet.empty |] in
517       let get_trans tag states =
518         let c = Cache.Lvl2.find cache2 tag (Uid.to_int states.StateSet.Node.id) in
519         if c == dummy2 then
520           let c = get_trans g auto tag states in
521           begin
522             Cache.Lvl2.add cache2 tag (Uid.to_int states.StateSet.Node.id) c;
523             c
524           end
525         else c
526       in
527       let lambda = ref 0 in
528       let rec start_loop idx states =
529         TRACE("grammar", 2, __ "Node %i\n%!" (Node.to_int idx));
530         if states == dummy_set then nil_res else
531         if idx < Node.null then nil_res
532         else begin
533           let symbol = Grammar2.start_tag g idx in
534           let fc = Grammar2.start_first_child g idx in
535           let ns = Grammar2.start_next_sibling g fc in
536           if Grammar2.is_terminal g symbol then
537             let t = Grammar2.terminal symbol in
538               terminal_loop t states (Grammar2.Leaf (~-1,0,term_array, fc)) (Grammar2.Leaf (~-1,1,term_array, ns))
539           else
540             let nt = Grammar2.non_terminal symbol in
541             incr lambda;
542             let lmbd = !lambda in
543             let y0 = (Grammar2.Leaf (lmbd,0, term_array, fc))
544             and y1 = (Grammar2.Leaf (lmbd,1, term_array, ns)) in
545             rule_loop nt states y0 y1
546         end
547       and rule_loop (t : Grammar2.n_symbol) states y0 y1 =
548         if t = Node.nil || states == dummy_set then nil_res else
549           let () = incr rule_counter in
550           if !rule_counter land 65535 == 0 then begin Gc.minor() end;
551 (*        let k = (t, states) in*)
552 (*        let pstates = DCache.find dcache k in
553           let notfound = DCache.notfound pstates in *)
554           let rhs = Grammar2.get_rule g t in
555           let id1 = Grammar2.get_id1 rhs in
556           let id2 = Grammar2.get_id2 rhs in
557           let conf = Grammar2.get_conf rhs in
558 (*        if notfound then*)
559             let ny0 = dispatch_param0 conf id2 y0 y1 in
560             let ny1 = dispatch_param1 conf id2 y0 y1 in
561             let res = dispatch_loop id1 states ny0 ny1 in
562 (*          pstates.(0) <- res.in0;
563             pstates.(1) <- res.in1; *)
564             res (*
565             UCache.add ucache (t, states, fst res.out0, fst res.out1)
566               res.main;
567             let h = Hashtbl.create 7 in
568             for i = 0 to res_len - 1 do
569               Hashtbl.add h (0, i) (snd res.out0).(i);
570               Hashtbl.add h (1, i) (snd res.out1).(i);
571             done;
572             { res with
573               main = ((fst res.main), (U.close h (snd res.main)));
574             } *)
575 (*
576             else
577               let res0 = partial_loop y0 pstates.(0) in
578               let res1 = partial_loop y1 pstates.(1) in
579               let k2 = (t, states, fst res0.main, fst res1.main) in
580               let s, r =
581                 try
582                   UCache.find ucache k2
583                 with
584                 Not_found ->
585                   let ores0 = { res0 with main = fst res0.main, U.var 0 (snd res0.main) }
586                   and ores1 = { res1 with main = fst res1.main, U.var 1 (snd res1.main) }
587                   in
588                   let res = dispatch_loop id1 states (Grammar2.Cache (0,ores0)) (Grammar2.Cache (1, ores1)) in
589                   UCache.add ucache k2 res.main;
590                   res.main
591               in
592               let h = Hashtbl.create 7 in
593               for i = 0 to res_len - 1 do
594                 Hashtbl.add h (0, i) (snd res0.main).(i);
595                 Hashtbl.add h (1, i) (snd res1.main).(i);
596               done;
597               { in0 = pstates.(0);
598                 in1 = pstates.(1);
599                 out0 = res0.main;
600                 out1 = res1.main;
601                 main = s, U.close h r;
602               }
603 *)
604       and dispatch_loop id1 states ny0 ny1 =
605           if Grammar2.is_non_terminal g id1 then
606             rule_loop (Grammar2.non_terminal id1) states ny0 ny1
607           else
608             terminal_loop (Grammar2.terminal id1) states ny0 ny1
609
610       and terminal_loop (symbol : Grammar2.t_symbol) states y0 y1 =
611
612         if symbol == Grammar2.nil_symbol || symbol = Node.nil || states == dummy_set then nil_res else begin
613           let tag = Grammar2.tag symbol in
614           let lst, rst, trans = get_trans tag states in
615           let res0 = partial_loop y0 lst in
616           let res1 = partial_loop y1 rst in
617           let s1, slot1 = res0.main
618           and s2, slot2 = res1.main in
619           let opcode = L3JIT.find cache3 trans s1 s2 in
620           let node = Node.of_int !preorder_counter in
621           incr preorder_counter;
622           let res =
623             if opcode == L3JIT.dummy then
624               (L3JIT.cache_apply cache3 auto trans s1 s2) empty_slot slot1 slot2 (Obj.magic ()) node
625             else
626               opcode empty_slot slot1 slot2 (Obj.magic())  (node)
627           in
628           { in0 = lst;
629             in1 = rst;
630             out0 = res0.main;
631             out1 = res1.main;
632             main = res }
633         end
634
635       and partial_loop l states =
636         if l == dummy_leaf then nil_res else
637           match l with
638           | Grammar2.Cache (_, r) -> r
639           | Grammar2.Leaf (_,_, _, id) -> start_loop id states
640           | Grammar2.Node0 id ->
641             if (Grammar2.terminal id) == Grammar2.nil_symbol then nil_res
642             else
643               rule_loop (Grammar2.non_terminal id) states dummy_leaf dummy_leaf
644
645           | Grammar2.Node1 (id, y0) ->
646             rule_loop (Grammar2.non_terminal id) states y0 dummy_leaf
647           | Grammar2.Node2 (id, y0, y1) ->
648             if Grammar2.is_terminal g id then
649             terminal_loop (Grammar2.terminal id) states y0 y1
650             else
651               rule_loop (Grammar2.non_terminal id) states y0 y1
652       in
653
654       let (_, slot) = (start_loop (Node.null) auto.init).main in
655       slot.(StateSet.min_elt auto.topdown_marking_states)
656     ;;
657
658
659
660
661
662
663
664   end
665