+ let find h tag fl s1 s2 : SList.t*bool*(merge_conf array) =
+ let l1 = get h tag in
+ let l2 = get l1 (Uid.to_int fl.Formlistlist.Node.id) in
+ let l3 = get l2 (Uid.to_int s1.SList.Node.id) in
+ Obj.magic (get l3 (Uid.to_int s2.SList.Node.id))
+
+ let is_valid b = (Obj.magic b) != 2
+ let get_replace tab idx default =
+ let e = get tab idx in
+ if e == default then
+ let ne = Array.copy e in (set tab idx ne;ne)
+ else e
+
+ let add h tag fl s1 s2 (data: SList.t*bool*(merge_conf array)) =
+ let l1 = get_replace h tag default_line1 in
+ let l2 = get_replace l1 (Uid.to_int fl.Formlistlist.Node.id) default_line2 in
+ let l3 = get_replace l2 (Uid.to_int s1.SList.Node.id) default_line3 in
+ set l3 (Uid.to_int s2.SList.Node.id) (Obj.repr data)
+ end
+
+
+
+
+ let top_down ?(noright=false) a tree t slist ctx slot_size td_trans h_fold2=
+ let pempty = empty_size slot_size in
+ let rempty = Array.make slot_size RS.empty in
+ (* evaluation starts from the right so we put sl1,res1 at the end *)
+ let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
+ let res = Array.copy rempty in
+ let r,b,btab = Fold2Res.find h_fold2 tag fll sl1 sl2 in
+ if Fold2Res.is_valid b then
+ begin
+ if b then for i=0 to slot_size - 1 do
+ res.(0) <- RS.merge btab.(0) t res1.(0) res2.(0);
+ done;
+ r,res
+ end
+ else
+ begin
+ let btab = Array.make slot_size NO in
+ let rec fold l1 l2 fll i aq ab =
+ match fll.Formlistlist.Node.node,
+ l1.SList.Node.node,
+ l2.SList.Node.node
+ with
+ | Formlistlist.Cons(fl,fll),
+ SList.Cons(s1,ll1),
+ SList.Cons(s2,ll2) ->
+ let r',conf = eval_formlist tag s1 s2 fl in
+ let _ = btab.(i) <- conf
+ in
+ fold ll1 ll2 fll (i+1) (SList.cons r' aq) ((conf!=NO)||ab)
+ | _ -> aq,ab
+ in
+ let r,b = fold sl1 sl2 fll 0 SList.nil false in
+ Fold2Res.add h_fold2 tag fll sl1 sl2 (r,b,btab);
+ if b then for i=0 to slot_size - 1 do
+ res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
+ done;
+ r,res;
+ end
+ in
+
+ let null_result = (pempty,Array.copy rempty) in
+ let empty_res = null_result in
+
+ let rec loop t ctx slist _ =
+ if t == Tree.nil then null_result else
+ let tag = Tree.tag tree t in
+ (CodeCache.get_fun td_trans slist tag) t ctx slist tag false
+ (* get_trans t ctx slist tag false
+ (CodeCache.get_opcode td_trans slist tag)
+ *)
+ and loop_tag t ctx slist tag =
+ if t == Tree.nil then null_result else
+ (CodeCache.get_fun td_trans slist tag) t ctx slist tag false
+ (* get_trans t ctx slist tag false
+ (CodeCache.get_opcode td_trans slist tag) *)
+
+ and loop_no_right t ctx slist _ =
+ if t == Tree.nil then null_result else
+ let tag = Tree.tag tree t in
+ (CodeCache.get_fun td_trans slist tag) t ctx slist tag true
+ (* get_trans t ctx slist tag true
+ (CodeCache.get_opcode td_trans slist tag) *)
+ (*
+ and get_trans t ctx slist tag noright opcode =
+ match opcode with
+ | OpCode.K0 fll ->
+ eval_fold2_slist fll t tag empty_res empty_res
+
+ | OpCode.K1 (fll,first,llist,tag1) ->
+ eval_fold2_slist fll t tag empty_res
+ (loop_tag (first t) t llist tag1)
+
+ | OpCode.K2 (fll,first,llist) ->
+ eval_fold2_slist fll t tag empty_res
+ (loop (first t) t llist)
+
+ | OpCode.K3 (fll,next,rlist,tag2) ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ empty_res
+ | OpCode.K4 (fll,next,rlist) ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist)
+ empty_res
+
+ | OpCode.K5 (fll,next,rlist,tag2,first,llist,tag1) ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop_tag (first t) t llist tag1)
+
+ | OpCode.K6 (fll,next,rlist,first,llist,tag1) ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist)
+ (loop_tag (first t) t llist tag1)
+
+ | OpCode.K7 (fll,next,rlist,tag2,first,llist) ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop (first t) t llist)
+
+ | OpCode.K8 (fll,next,rlist,first,llist) ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist)
+ (loop (first t) t llist)
+
+ | OpCode.KDefault _ ->
+ mk_trans t ctx tag slist noright
+ *)
+ and mk_trans t ctx slist tag noright =
+ let fl_list,llist,rlist,ca,da,sa,fa =
+ SList.fold
+ (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
+ let fl,ll,rr,ca,da,sa,fa =
+ StateSet.fold
+ (fun q acc ->
+ List.fold_left
+ (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
+ (ts,t) ->
+ if (TagSet.mem tag ts)
+ then
+ let _,_,_,f,_ = t.Transition.node in
+ let (child,desc,below),(sibl,foll,after) = Formula.st f in
+ (Formlist.cons t fl_acc,
+ StateSet.union ll_acc below,
+ StateSet.union rl_acc after,
+ StateSet.union child c_acc,
+ StateSet.union desc d_acc,
+ StateSet.union sibl s_acc,
+ StateSet.union foll f_acc)
+ else acc ) acc (
+ try Hashtbl.find a.trans q
+ with
+ Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
+ q;[]
+ )
+
+ ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
+ in (Formlistlist.cons fl fll_acc), (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
+ slist (Formlistlist.nil,SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
+ in
+ (* Logic to chose the first and next function *)
+ let tags_child,tags_below,tags_siblings,tags_after = Tree.tags tree tag in
+ let d_f = Algebra.decide a tags_child tags_below (StateSet.union ca da) true in
+ let d_n = Algebra.decide a tags_siblings tags_after (StateSet.union sa fa) false in
+ let f_kind,first = choose_jump_down tree d_f
+ and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
+ else choose_jump_next tree d_n in
+ let empty_res = null_result in
+ let fll = fl_list in
+ let cont =
+ match f_kind,n_kind with
+ | `NIL,`NIL -> (*OpCode.K0(fl_list) *)
+ fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res empty_res
+
+ | _,`NIL -> (
+ match f_kind with
+ |`TAG(tag1) -> (*OpCode.K1(fl_list,first,llist,tag1) *)
+ fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res
+ (loop_tag (first t) t llist tag1)
+ | _ -> (* OpCode.K2(fl_list,first,llist) *)
+ fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res
+ (loop (first t) t llist tag)
+ )
+ | `NIL,_ -> (
+ match n_kind with
+ |`TAG(tag2) -> (*OpCode.K3(fl_list,next,rlist,tag2) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ empty_res
+
+ | _ -> (*OpCode.K4(fl_list,next,rlist) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist tag)
+ empty_res
+
+ )
+
+ | `TAG(tag1),`TAG(tag2) -> (*OpCode.K5(fl_list,next,rlist,tag2,first,llist,tag1) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop_tag (first t) t llist tag1)
+
+ | `TAG(tag1),`ANY -> (* OpCode.K6(fl_list,next,rlist,first,llist,tag1) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist tag)
+ (loop_tag (first t) t llist tag1)
+
+ | `ANY,`TAG(tag2) -> (* OpCode.K7(fl_list,next,rlist,tag2,first,llist) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop (first t) t llist tag)
+
+
+ | _,_ -> (*OpCode.K8(fl_list,next,rlist,first,llist) *)
+ (*if SList.equal slist rlist && SList.equal slist llist
+ then
+ let rec loop t ctx =
+ if t == Tree.nil then empty_res else
+ let r1 = loop (first t) t
+ and r2 = loop (next t ctx) ctx
+ in
+ eval_fold2_slist fl_list t (Tree.tag tree t) r2 r1
+ in loop
+ else *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist tag)
+ (loop (first t) t llist tag)
+
+
+
+ in
+ CodeCache.set_fun td_trans slist tag cont;
+ cont t ctx slist tag noright
+ in
+ let _ = CodeCache.init mk_trans in
+ (if noright then loop_no_right else loop) t ctx slist Tag.dummy
+
+
+ let run_top_down a tree =
+ let init = SList.cons a.init SList.nil in
+ let _,res = top_down a tree Tree.root init Tree.root 1 (CodeCache.create BIG_A_SIZE) (Fold2Res.create 1024)
+ in
+ D_IGNORE_(
+ output_trace a tree "trace.html"
+ (RS.fold (fun t a -> IntSet.add (Tree.id tree t) a) res.(0) IntSet.empty),
+ res.(0))
+ ;;
+
+
+
+
+
+ module Code3Cache =
+ struct
+ let get = Array.get
+ let set = Array.set
+ let realloc a new_size default =
+ let old_size = Array.length a in
+ if old_size == new_size then a
+ else if new_size == 0 then [||]
+ else let na = Array.create new_size default in
+ Array.blit a 0 na 0 old_size;na
+
+ type fun_tree = [`Tree] Tree.node -> [`Tree] Tree.node -> StateSet.t -> Tag.t -> StateSet.t*RS.t
+ and t = { mutable table : fun_tree array array;
+ mutable default_elm : fun_tree;
+ mutable default_line : fun_tree array;
+ (* statistics *)
+ mutable access : int;
+ mutable miss : int;
+ }
+
+
+ let create () =
+ { table = [||];
+ default_elm = (fun _ _ _ _ -> failwith "Uninitialized Code3Cache.t structure\n");
+ default_line = [||];
+ access = 0;
+ miss = 0 }
+
+ let init h f =
+ let default_line = Array.create SMALL_A_SIZE f in
+ begin
+ h.table <- Array.create SMALL_A_SIZE default_line;
+ h.default_elm <- f;
+ h.default_line <- default_line;
+ h.access <- 0;
+ h.miss <- 0
+ end
+
+ let next_power_of_2 n =
+ let rec loop i acc =
+ if acc == 0 then i
+ else loop (i+1) (acc lsr 1)
+ in
+ 1 lsl (loop 0 n)