Browse Source

build lut as we go

Simon Krajewski 12 năm trước cách đây
mục cha
commit
46d54d351f
1 tập tin đã thay đổi với 7 bổ sung6 xóa
  1. 7 6
      matcher.ml

+ 7 - 6
matcher.ml

@@ -101,6 +101,7 @@ type matcher = {
 	ctx : typer;
 	ctx : typer;
 	need_val : bool;
 	need_val : bool;
 	dt_cache : (dt,int) Hashtbl.t;
 	dt_cache : (dt,int) Hashtbl.t;
+	dt_lut : dt DynArray.t;
 	mutable dt_count : int;
 	mutable dt_count : int;
 	mutable outcomes : (pat list,out) PMap.t;
 	mutable outcomes : (pat list,out) PMap.t;
 	mutable toplevel_or : bool;
 	mutable toplevel_or : bool;
@@ -812,6 +813,7 @@ let get_cache mctx dt =
 	with Not_found ->
 	with Not_found ->
 		Hashtbl.replace mctx.dt_cache dt mctx.dt_count;
 		Hashtbl.replace mctx.dt_cache dt mctx.dt_count;
 		mctx.dt_count <- mctx.dt_count + 1;
 		mctx.dt_count <- mctx.dt_count + 1;
+		DynArray.add mctx.dt_lut dt;
 		dt
 		dt
 
 
 let rec compile mctx stl pmat =
 let rec compile mctx stl pmat =
@@ -958,6 +960,7 @@ let make_dt ctx e cases def with_type p =
 		toplevel_or = false;
 		toplevel_or = false;
 		used_paths = Hashtbl.create 0;
 		used_paths = Hashtbl.create 0;
 		dt_cache = Hashtbl.create 0;
 		dt_cache = Hashtbl.create 0;
+		dt_lut = DynArray.create ();
 		dt_count = 0;
 		dt_count = 0;
 	} in
 	} in
 	(* flatten cases *)
 	(* flatten cases *)
@@ -1107,7 +1110,7 @@ let make_dt ctx e cases def with_type p =
 		| Some (WithTypeResume t2) -> (try unify_raise ctx t2 t p with Error (Unify l,p) -> raise (Typer.WithTypeError (l,p)))
 		| Some (WithTypeResume t2) -> (try unify_raise ctx t2 t p with Error (Unify l,p) -> raise (Typer.WithTypeError (l,p)))
 		| _ -> assert false
 		| _ -> assert false
 	end;
 	end;
-	dt,!var_inits,t,mctx.dt_cache
+	dt,!var_inits,t,DynArray.to_array mctx.dt_lut
 
 
 (* Conversion to Typed AST *)
 (* Conversion to Typed AST *)
 
 
@@ -1116,7 +1119,7 @@ type cctx = {
 	v_lookup : (string,tvar) Hashtbl.t;
 	v_lookup : (string,tvar) Hashtbl.t;
 	out_type : t;
 	out_type : t;
 	mutable eval_stack : (pvar * st) list list;
 	mutable eval_stack : (pvar * st) list list;
-	dt_lookup : (int,dt) Hashtbl.t;
+	dt_lookup : dt array;
 }
 }
 
 
 let mk_const ctx p = function
 let mk_const ctx p = function
@@ -1184,7 +1187,7 @@ let replace_locals cctx e =
 let rec to_typed_ast cctx dt =
 let rec to_typed_ast cctx dt =
 	match dt with
 	match dt with
 	| Goto i ->
 	| Goto i ->
-		to_typed_ast cctx (Hashtbl.find cctx.dt_lookup i)
+		to_typed_ast cctx (cctx.dt_lookup.(i))
 	| Out(e,eo,dt) ->
 	| Out(e,eo,dt) ->
 		replace_locals cctx begin match eo,dt with
 		replace_locals cctx begin match eo,dt with
 			| Some eg,None ->
 			| Some eg,None ->
@@ -1367,9 +1370,7 @@ and to_array_switch cctx t st cases =
 (* Main *)
 (* Main *)
 
 
 let match_expr ctx e cases def with_type p =
 let match_expr ctx e cases def with_type p =
-	let dt,var_inits,t,dtl = make_dt ctx e cases def with_type p in
-	let lut = Hashtbl.create 0 in
-	Hashtbl.iter (fun k v -> Hashtbl.replace lut v k) dtl;
+	let dt,var_inits,t,lut = make_dt ctx e cases def with_type p in
 	let cctx = {
 	let cctx = {
 		ctx = ctx;
 		ctx = ctx;
 		out_type = mk_mono();
 		out_type = mk_mono();