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