Преглед на файлове

encode basic types directly

I'm not sure if I want to keep this optimization, but the changes to com.basic seem like a good idea regardless. At the moment it might be possible to get a hold of the ominous `m` monomorph in Common.create, and binding that one to anything would lead to interesting results.
Simon Krajewski преди 1 година
родител
ревизия
d30cd71dbe

+ 21 - 14
src/compiler/hxb/hxbReader.ml

@@ -62,15 +62,6 @@ class hxb_reader
 		Option.may (fun tinfos -> prerr_endline (Printf.sprintf "  Current type: %s" (s_type_path tinfos.mt_path))) current_type;
 		Option.may (fun e -> prerr_endline (Printf.sprintf "  Last texpr: %s" (TPrinting.s_expr_debug e))) last_texpr
 
-	val mutable tvoid = None
-	method get_tvoid =
-		match tvoid with
-		| Some tvoid -> tvoid
-		| None ->
-			let t = type_of_module_type (self#resolve_type [] "StdTypes" "Void") in
-			tvoid <- Some t;
-			t
-
 	(* Primitives *)
 
 	method read_u8 =
@@ -702,7 +693,8 @@ class hxb_reader
 			let a = self#read_abstract_ref in
 			let tl = self#read_types in
 			TAbstract(a,tl)
-		| 30 -> TFun([],self#get_tvoid)
+		| 30 ->
+			TFun([],api#basic_types.tvoid)
 		| 31 ->
 			let f () =
 				let name = self#read_string in
@@ -712,7 +704,7 @@ class hxb_reader
 				(name,opt,t)
 			in
 			let args = self#read_list f in
-			TFun(args,self#get_tvoid)
+			TFun(args,api#basic_types.tvoid)
 		| 32 ->
 			let f () =
 				let name = self#read_string in
@@ -737,8 +729,18 @@ class hxb_reader
 			let empty = self#read_bool in
 			if empty then mk_anon (ref Closed)
 			else TAnon self#read_anon_ref
-		| 51 -> TAnon self#read_anon_ref
-		| i -> error (Printf.sprintf "Bad type instance id: %i" i)
+		| 51 ->
+			TAnon self#read_anon_ref
+		| 100 ->
+			api#basic_types.tint
+		| 101 ->
+			api#basic_types.tfloat
+		| 102 ->
+			api#basic_types.tbool
+		| 103 ->
+			api#basic_types.tstring
+		| i ->
+			error (Printf.sprintf "Bad type instance id: %i" i)
 
 	method read_types =
 		self#read_list (fun () -> self#read_type_instance)
@@ -1272,7 +1274,12 @@ class hxb_reader
 	method read_abstract (a : tabstract) =
 		self#read_common_module_type (Obj.magic a);
 		a.a_impl <- self#read_option (fun () -> self#read_class_ref);
-		a.a_this <- self#read_type_instance;
+		begin match self#read_u8 with
+			| 0 ->
+				a.a_this <- TAbstract(a,extract_param_types a.a_params)
+			| _ ->
+				a.a_this <- self#read_type_instance;
+		end;
 		a.a_from <- self#read_list (fun () -> self#read_type_instance);
 		a.a_to <- self#read_list (fun () -> self#read_type_instance);
 		a.a_enum <- self#read_bool;

+ 1 - 0
src/compiler/hxb/hxbReaderApi.ml

@@ -5,4 +5,5 @@ class virtual hxb_reader_api = object(self)
 	method virtual make_module : path -> string -> module_def
 	method virtual add_module : module_def -> unit
 	method virtual resolve_type : string list -> string -> string -> module_type
+	method virtual basic_types : basic_types
 end

+ 14 - 1
src/compiler/hxb/hxbWriter.ml

@@ -449,6 +449,14 @@ class ['a] hxb_writer
 			self#write_type_instance t;
 		in
 		match t with
+		| TAbstract ({a_path = ([],"Int")},[]) ->
+			chunk#write_byte 100
+		| TAbstract ({a_path = ([],"Float")},[]) ->
+			chunk#write_byte 101
+		| TAbstract ({a_path = ([],"Bool")},[]) ->
+			chunk#write_byte 102
+		| TInst ({cl_path = ([],"String")},[]) ->
+			chunk#write_byte 103
 		| TMono r ->
 			Monomorph.close r;
 			begin match r.tm_type with
@@ -1324,7 +1332,12 @@ class ['a] hxb_writer
 		end;
 		self#write_common_module_type (Obj.magic a);
 		chunk#write_option a.a_impl self#write_class_ref;
-		self#write_type_instance a.a_this;
+		if Meta.has Meta.CoreType a.a_meta then
+			chunk#write_byte 0
+		else begin
+			chunk#write_byte 1;
+			self#write_type_instance a.a_this;
+		end;
 		chunk#write_list a.a_from self#write_type_instance;
 		chunk#write_list a.a_to self#write_type_instance;
 		chunk#write_bool a.a_enum

+ 3 - 0
src/compiler/server.ml

@@ -432,6 +432,9 @@ class hxb_reader_api_server
 			end
 		with Not_found ->
 			NoModule
+
+	method basic_types =
+		ctx.com.basic
 end
 
 let handle_cache_bound_objects com cbol =

+ 11 - 7
src/context/common.ml

@@ -782,7 +782,6 @@ let get_config com =
 let memory_marker = [|Unix.time()|]
 
 let create compilation_step cs version args display_mode =
-	let m = Type.mk_mono() in
 	let rec com = {
 		compilation_step = compilation_step;
 		cs = cs;
@@ -850,12 +849,12 @@ let create compilation_step cs version args display_mode =
 		filter_messages = (fun _ -> ());
 		pass_debug_messages = DynArray.create();
 		basic = {
-			tvoid = m;
-			tint = m;
-			tfloat = m;
-			tbool = m;
+			tvoid = mk_mono();
+			tint = mk_mono();
+			tfloat = mk_mono();
+			tbool = mk_mono();
+			tstring = mk_mono();
 			tnull = (fun _ -> die "Could use locate abstract Null<T> (was it redefined?)" __LOC__);
-			tstring = m;
 			tarray = (fun _ -> die "Could not locate class Array<T> (was it redefined?)" __LOC__);
 		};
 		file_lookup_cache = new hashtbl_lookup;
@@ -891,7 +890,12 @@ let clone com is_macro_context =
 	let t = com.basic in
 	{ com with
 		cache = None;
-		basic = { t with tvoid = t.tvoid };
+		basic = { t with
+			tint = mk_mono();
+			tfloat = mk_mono();
+			tbool = mk_mono();
+			tstring = mk_mono();
+		};
 		main_class = None;
 		features = Hashtbl.create 0;
 		callbacks = new compiler_callbacks;

+ 3 - 0
src/context/display/displayJson.ml

@@ -137,6 +137,9 @@ class hxb_reader_api_com
 		with Not_found ->
 			let mc = cc#get_hxb_module m_path in
 			self#read_hxb (IO.input_bytes mc.mc_bytes)
+
+	method basic_types =
+		com.basic
 end
 
 let find_module com cc path p =

+ 3 - 0
src/typing/typeloadModule.ml

@@ -795,6 +795,9 @@ class hxb_reader_api_typeload
 	method resolve_type (pack : string list) (mname : string) (tname : string) =
 		let m = load_module ctx (pack,mname) p in
 		List.find (fun t -> snd (t_path t) = tname) m.m_types
+
+	method basic_types =
+		ctx.com.basic
 end
 
 let rec get_reader ctx p =

+ 18 - 5
src/typing/typerEntry.ml

@@ -92,10 +92,20 @@ let create com macros =
 		| TAbstractDecl a ->
 			(match snd a.a_path with
 			| "Void" -> ctx.t.tvoid <- TAbstract (a,[]);
-			| "Float" -> ctx.t.tfloat <- TAbstract (a,[]);
-			| "Int" -> ctx.t.tint <- TAbstract (a,[])
-			| "Bool" -> ctx.t.tbool <- TAbstract (a,[])
-			| "Dynamic" -> t_dynamic_def := TAbstract(a,extract_param_types a.a_params);
+			| "Float" ->
+				let t = (TAbstract (a,[])) in
+				Type.unify t ctx.t.tfloat;
+				ctx.t.tfloat <- t
+			| "Int" ->
+				let t = (TAbstract (a,[])) in
+				Type.unify t ctx.t.tint;
+				ctx.t.tint <- t
+			| "Bool" ->
+				let t = (TAbstract (a,[])) in
+				Type.unify t ctx.t.tbool;
+				ctx.t.tbool <- t
+			| "Dynamic" ->
+				t_dynamic_def := TAbstract(a,extract_param_types a.a_params);
 			| "Null" ->
 				let mk_null t =
 					try
@@ -117,7 +127,10 @@ let create com macros =
 	) ctx.g.std_types.m_types;
 	let m = TypeloadModule.load_module ctx ([],"String") null_pos in
 	List.iter (fun mt -> match mt with
-		| TClassDecl c -> ctx.t.tstring <- TInst (c,[])
+		| TClassDecl c ->
+			let t = (TInst (c,[])) in
+			Type.unify t ctx.t.tstring;
+			ctx.t.tstring <- t
 		| _ -> ()
 	) m.m_types;
 	let m = TypeloadModule.load_module ctx ([],"Std") null_pos in