- type t = int*Ptset.t*Ptset.t
- let equal (a,b,c) (d,e,f) =
- a==d && (Ptset.equal b e) && (Ptset.equal c f)
- let hash (a,b,c) =
- a+17*(Ptset.hash b) + 31*(Ptset.hash c)
- end)
-
- let hfeval = HFEval.create 4097
-
-
-(* let miss = ref 0
- let call = ref 0
- let timeref = ref 0.0
- let timerefall = ref 0.0
- let time f x =
- incr call;
- let t1 = Unix.gettimeofday ()
- in let r = f x
- in
- timeref := !timeref +. ((Unix.gettimeofday()) -. t1);
- r
-
- let timeall f x =
- let t1 = Unix.gettimeofday ()
- in let r = f x
- in
- timerefall := !timerefall +. ((Unix.gettimeofday()) -. t1);
- r
-
-*)
-
-
- let eval_form_bool f s1 s2 =
- let rec eval f = match f.pos with
- | Atom(`Left,b,q) -> if b == (Ptset.mem q s1) then (true,true,false) else false,false,false
- | Atom(`Right,b,q) -> if b == (Ptset.mem q s2) then (true,false,true) else false,false,false
- (* test some inlining *)
- | True -> true,true,true
- | False -> false,false,false
- | _ ->
- try
- HFEval.find hfeval (f.fid,s1,s2)
- with
- | Not_found -> let r =
- match f.pos with
- | Or(f1,f2) ->
- let b1,rl1,rr1 = eval f1
- in
- if b1 && rl1 && rr1 then (true,true,true)
- else
- let b2,rl2,rr2 = eval f2
- in
- let rl1,rr1 = if b1 then rl1,rr1 else false,false
- and rl2,rr2 = if b2 then rl2,rr2 else false,false
- in (b1 || b2, rl1||rl2,rr1||rr2)
- | And(f1,f2) ->
- let b1,rl1,rr1 = eval f1 in
- if b1 && rl1 && rr1 then (true,true,true)
- else if b1
- then let b2,rl2,rr2 = eval f2 in
- if b2 then (true,rl1||rl2,rr1||rr2)
- else (false,false,false)
- else (false,false,false)
- | _ -> assert false
- in
- HFEval.add hfeval (f.fid,s1,s2) r;
- r
- in eval f
-
-
- module HFEvalDir = Hashtbl.Make(
+ let get = Array.unsafe_get
+ let set = Array.set
+
+ type fun_tree = [`Tree] Tree.node -> [`Tree] Tree.node -> SList.t -> Tag.t -> bool -> SList.t*RS.t array
+ type t = fun_tree array array
+
+ let dummy = fun _ _ _ _ _ -> failwith "Uninitializd CodeCache"
+ let default_line = Array.create 1024 dummy (* 1024 = max_tag *)
+ let create n = Array.create n default_line
+ let init f =
+ for i = 0 to (Array.length default_line) - 1
+ do
+ default_line.(i) <- f
+ done
+
+ let get_fun h slist tag =
+ get (get h (Uid.to_int slist.SList.Node.id)) tag
+
+ let set_fun (h : t) slist tag (data : fun_tree) =
+ let tab = get h (Uid.to_int slist.SList.Node.id) in
+ let line = if tab == default_line then
+ let x = Array.copy tab in
+ (set h (Uid.to_int slist.SList.Node.id) x;x)
+ else tab
+ in
+ set line tag data
+
+ end
+
+ let empty_size n =
+ let rec loop acc = function 0 -> acc
+ | n -> loop (SList.cons StateSet.empty acc) (n-1)
+ in loop SList.nil n
+
+
+ module Fold2Res = struct
+ let get = Array.unsafe_get
+ let set = Array.set
+ external field1 : Obj.t -> int = "%field1"
+ type t = Obj.t array array array array
+ let dummy_val = Obj.repr ((),2,())
+
+ let default_line3 = Array.create BIG_A_SIZE dummy_val
+ let default_line2 = Array.create BIG_A_SIZE default_line3
+ let default_line1 = Array.create BIG_A_SIZE default_line2
+
+ let create n = Array.create n default_line1
+
+ 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 =