+ ;;
+
+
+
+
+
+ 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)
+
+ let get_fun h slist tag =
+ let _ = h.access <- h.access + 1 in
+ let idx = Uid.to_int slist.StateSet.Node.id in
+ let line =
+ if idx >= Array.length h.table then
+ let new_tab = realloc h.table (next_power_of_2 idx) h.default_line in
+ let _ = h.miss <- h.miss + 1; h.table <- new_tab in h.default_line
+ else Array.unsafe_get h.table idx
+ in
+ if tag >= Array.length line then
+ let new_line = realloc line (next_power_of_2 tag) h.default_elm in
+ let _ = h.miss <- h.miss + 1; Array.unsafe_set h.table idx new_line in h.default_elm
+ else Array.unsafe_get line tag
+
+ let set_fun (h : t) slist tag (data : fun_tree) =
+ let idx = Uid.to_int slist.StateSet.Node.id in
+ let line =
+ if idx >= Array.length h.table then
+ let new_tab = realloc h.table (next_power_of_2 idx) h.default_line in
+ let _ = h.table <- new_tab in h.default_line
+ else Array.unsafe_get h.table idx
+ in
+ let line = if line == h.default_line then
+ let l = Array.copy line in Array.unsafe_set h.table idx l;l
+ else line in
+ let line = if tag >= Array.length line then
+ let new_line = realloc line (next_power_of_2 tag) h.default_elm in
+ let _ = Array.unsafe_set h.table idx new_line in new_line
+ else line
+ in
+ Array.unsafe_set line tag data
+
+
+ let dump h = Array.iteri
+ (fun id line -> if line != h.default_line then
+ begin
+ StateSet.print Format.err_formatter (StateSet.with_id (Uid.of_int id));
+ Format.fprintf Format.err_formatter " -> ";
+ Array.iteri (fun tag clos ->
+ if clos != h.default_elm then
+ Format.fprintf Format.err_formatter " (%s,%s) "
+ (Tag.to_string tag) (Trace.get_funname clos)) line;
+ Format.fprintf Format.err_formatter "\n%!"
+ end
+ ) h.table;
+ Format.fprintf Format.err_formatter "Cache hits: %i, Cache misses: %i, ratio = %f\n%!"
+ h.access h.miss ((float_of_int h.miss)/. (float_of_int h.access));
+ Format.fprintf Format.err_formatter "Size: %i kb\n%!"
+ (((2+(Array.length h.default_line)+
+ (Array.fold_left (fun acc l ->acc + (if l == h.default_line then 0 else Array.length l))
+ (Array.length h.table) h.table)) * Sys.word_size) / 1024)
+
+ end
+
+ module StaticEnv =
+ struct
+
+ type t = { stack : Obj.t array;
+ mutable top : int; }
+
+ let create () = { stack = Array.create BIG_A_SIZE (Obj.repr 0); top = 0 }
+ let add t e =
+ let _ = if t.top >= Array.length t.stack then failwith "Static Env overflow" in
+ let i = t.top in Array.unsafe_set t.stack i e; t.top <- i + 1; i
+
+ let get t i :'a = Obj.magic (Array.unsafe_get t.stack i)
+ end
+
+ module Fold3Res = 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 1024 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 : StateSet.t*bool*merge_conf =
+ let l1 = get h (Uid.to_int fl.Formlist.Node.id) in
+ let l2 = get l1 (Uid.to_int s1.StateSet.Node.id) in
+ let l3 = get l2 (Uid.to_int s2.StateSet.Node.id) in
+ Obj.magic (get l3 tag)
+
+ let is_valid b = b != (Obj.magic dummy_val)
+ 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: StateSet.t*bool*merge_conf) =
+ let l1 = get_replace h (Uid.to_int fl.Formlist.Node.id) default_line1 in
+ let l2 = get_replace l1 (Uid.to_int s1.StateSet.Node.id) default_line2 in
+ let l3 = get_replace l2 (Uid.to_int s2.StateSet.Node.id) default_line3 in
+ set l3 tag (Obj.repr data)
+ end
+
+
+ let empty_res = StateSet.empty,RS.empty
+
+ let top_down1 a tree t slist ctx td_trans h_fold2 =
+ (* evaluation starts from the right so we put sl1,res1 at the end *)
+ let env = StaticEnv.create () in
+ let slist_reg = ref StateSet.empty in
+ let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
+ let data = Fold3Res.find h_fold2 tag fll sl1 sl2 in
+ if Fold3Res.is_valid data then
+ let r,b,conf = data in
+ (r,if b then RS.merge conf t res1 res2 else RS.empty)
+ else
+ let r,conf = eval_formlist tag sl1 sl2 fll in
+ let b = conf <> NO in
+ (Fold3Res.add h_fold2 tag fll sl1 sl2 (r,b,conf);
+ (r, if b then RS.merge conf t res1 res2 else RS.empty))
+
+ in
+ let loop t ctx slist _ =
+ if t == Tree.nil then empty_res else
+ let tag = Tree.tag tree t in
+ (Code3Cache.get_fun td_trans slist tag) t ctx slist tag
+
+ in
+ let loop_tag t ctx slist tag =
+ if t == Tree.nil then empty_res else
+ (Code3Cache.get_fun td_trans slist tag) t ctx slist tag
+
+ in
+ let mk_trans t ctx slist tag =
+ let fl_list,llist,rlist,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;[]
+ )
+
+ ) slist (Formlist.nil,StateSet.empty,StateSet.empty,
+ 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 = choose_jump_next tree d_n in
+ (*let f_kind, first = `ANY, (Tree.first_element tree)
+ and n_kind, next = `ANY, (Tree.next_element_below tree) in *)
+ let cont =
+ match f_kind,n_kind with
+ | `NIL,`NIL ->
+ fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res empty_res
+
+ | _,`NIL -> (
+ match f_kind with
+ |`TAG(tag1) ->
+ (fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res
+ (loop_tag (first t) t llist tag1))
+ | _ ->
+ fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res
+ (loop (first t) t llist tag)
+ )
+ | `NIL,_ -> (
+ match n_kind with
+ |`TAG(tag2) ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ empty_res
+
+ | _ ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop (next t ctx) ctx rlist tag)
+ empty_res
+
+ )
+
+ | `TAG(tag1),`TAG(tag2) ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop_tag (first t) t llist tag1)
+
+ | `TAG(tag1),`ANY ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop (next t ctx) ctx rlist tag)
+ (loop_tag (first t) t llist tag1)
+
+ | `ANY,`TAG(tag2) ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop (first t) t llist tag)
+
+
+ | _,_ ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop (next t ctx) ctx rlist tag)
+ (loop (first t) t llist tag)
+
+
+
+ in
+ let _ = Trace.register_funname cont
+ (Printf.sprintf "{first=%s, next=%s}" (Trace.get_funname first) (Trace.get_funname next))
+ in
+ Code3Cache.set_fun td_trans slist tag cont;
+ cont
+ in
+ let cache_take_trans t ctx slist tag =
+ let cont = mk_trans t ctx slist tag in
+ cont t ctx slist tag
+ in
+ Code3Cache.init td_trans (cache_take_trans);
+ loop t ctx slist Tag.dummy
+
+
+ let run_top_down1 a tree =
+ let code_cache = Code3Cache.create () in
+ let fold_cache = Fold3Res.create BIG_A_SIZE in
+ let _,res = top_down1 a tree Tree.root a.init Tree.root code_cache fold_cache
+ in
+(* Code3Cache.dump code_cache; *)
+ res
+