[m-rev.] diff: equivalence type fixes for java backend

Peter Wang novalazy at gmail.com
Wed Sep 22 14:12:20 AEST 2010


Branches: main, 10.04

java/runtime/TypeInfo_Struct.java:
        Collapse equivalences when comparing type_infos for equality.

java/runtime/TypeCtorRep.java:
        Duplicate the required constants which otherwise are in
        private_builtin.m.  They should all be moved here but I don't want to
        make that change on the 10.04 branch.

library/rtti_implementation.m:
        Collapse equivalences in type_info_num_functors.

tests/hard_coded/construct_bug.m:
        Use text streams in this test case, as the current hacky implementation
        of `io.write_binary' doesn't work on the Java backend.

diff --git a/java/runtime/TypeCtorRep.java b/java/runtime/TypeCtorRep.java
index 3f95156..1f374c8 100644
--- a/java/runtime/TypeCtorRep.java
+++ b/java/runtime/TypeCtorRep.java
@@ -9,6 +9,13 @@ package jmercury.runtime;
 public class TypeCtorRep implements java.io.Serializable {
 	
 	// Constants are in private_builtin.m, named MR_TYPECTOR_REP_*.
+	//
+	// XXX We have to duplicate these here for use by TypeInfo_Struct.java
+	// In fact, all the values should be moved here, and the compiler
+	// should generate references to these constants instead of those in
+	// private_builtin.
+	public static final int MR_TYPECTOR_REP_EQUIV		= 6;
+	public static final int MR_TYPECTOR_REP_EQUIV_GROUND	= 29;
 
 	// Instance variable for TypeCtorRep objects.
 	
diff --git a/java/runtime/TypeInfo_Struct.java b/java/runtime/TypeInfo_Struct.java
index f8a8fdd..bae46b3 100644
--- a/java/runtime/TypeInfo_Struct.java
+++ b/java/runtime/TypeInfo_Struct.java
@@ -115,14 +115,21 @@ public class TypeInfo_Struct extends PseudoTypeInfo
 			return true;
 		}
 
-		if (!type_ctor.unify(ti.type_ctor)) {
+		TypeInfo_Struct self = this.collapse_equivalences();
+		ti = ti.collapse_equivalences();
+
+		if (self == ti) {
+			return true;
+		}
+
+		if (!self.type_ctor.unify(ti.type_ctor)) {
 			return false;
 		}
 
 		int len1 = 0;
 		int len2 = 0;
-		if (args != null) {
-			len1 = args.length;
+		if (self.args != null) {
+			len1 = self.args.length;
 		}
 		if (ti.args != null) {
 			len2 = ti.args.length;
@@ -132,13 +139,29 @@ public class TypeInfo_Struct extends PseudoTypeInfo
 		}
 
 		for (int i = 0; i < len1; i++) {
-			if (!args[i].unify(ti.args[i])) {
+			if (!self.args[i].unify(ti.args[i])) {
 				return false;
 			}
 		}
 		return true;
 	}
 
+	private TypeInfo_Struct collapse_equivalences() {
+		TypeInfo_Struct ti = this;
+
+		/* Look past equivalences */
+		while (ti.type_ctor.type_ctor_rep.value ==
+				TypeCtorRep.MR_TYPECTOR_REP_EQUIV_GROUND
+			|| ti.type_ctor.type_ctor_rep.value ==
+				TypeCtorRep.MR_TYPECTOR_REP_EQUIV)
+		{
+			ti = TypeInfo_Struct.maybe_new(
+				ti.type_ctor.type_layout.layout_equiv());
+		}
+
+		return ti;
+	}
+
 	private void sanity_check() {
 		assert type_ctor != null;
 
diff --git a/library/rtti_implementation.m b/library/rtti_implementation.m
index 83519c7..75484a1 100644
--- a/library/rtti_implementation.m
+++ b/library/rtti_implementation.m
@@ -269,11 +269,11 @@ type_info_num_functors(TypeInfo, NumFunctors) :-
         ),
         NumFunctors = 1
     ;
-        TypeCtorRep = tcr_equiv_ground,
-        error("rtti_implementation num_functors for equiv_ground type")
-    ;
-        TypeCtorRep = tcr_equiv,
-        error("rtti_implementation num_functors for equiv type")
+        ( TypeCtorRep = tcr_equiv_ground
+        ; TypeCtorRep = tcr_equiv
+        ),
+        NewTypeInfo = collapse_equivalences(TypeInfo),
+        type_info_num_functors(NewTypeInfo, NumFunctors)
     ;
         ( TypeCtorRep = tcr_subgoal
         ; TypeCtorRep = tcr_int
diff --git a/tests/hard_coded/construct_bug.m b/tests/hard_coded/construct_bug.m
index 2aac78f..1a6805b 100644
--- a/tests/hard_coded/construct_bug.m
+++ b/tests/hard_coded/construct_bug.m
@@ -35,34 +35,34 @@ main(!IO) :-
         count(["C","C1"], !S)
         ), construct_bug_submodule.init, Map),
 
-    io.open_binary_output(FileName, Result, !IO),
+    io.open_output(FileName, Result, !IO),
     ( Result = ok(Temp_ORDIE_Out_OutStream) ->
         OutStream = Temp_ORDIE_Out_OutStream
     ;
         error( "Failed to write to '" ++ FileName ++ "'.")
     ),
-    io.write_binary(OutStream, Map, !IO),
-    close_binary_output(OutStream, !IO),
+    io.write(OutStream, Map, !IO),
+    io.write_string(OutStream, ".", !IO),
+    close_output(OutStream, !IO),
 
     io.write_string("Saved the map to file: " ++ FileName ++ "\n", !IO),
 
-    io.open_binary_input(FileName, Result2, !IO),
+    io.open_input(FileName, Result2, !IO),
     ( Result2 = ok(Temp_ORDIE_Out_InStream) ->
         InStream = Temp_ORDIE_Out_InStream
     ;
         error( "Failed to open '" ++ FileName ++ "'.")
     ),
-    io.read_binary(InStream, MayDataTerm, !IO),
-    io.close_binary_input(InStream, !IO),
+    io.read(InStream, MayDataTerm, !IO),
+    io.close_input(InStream, !IO),
     (
         MayDataTerm = ok(ReadMap `with_type` stat)
     ;
         MayDataTerm = eof,
         error("Unexpected end of file: '" ++ FileName ++ "'.")
     ;
-        MayDataTerm = error(E),
-        error("Error reading term from: '" ++ FileName ++ "': "
-            ++ io.error_message(E) ++ ".")
+        MayDataTerm = error(E, _),
+        error("Error reading term from: '" ++ FileName ++ "': " ++ E ++ ".")
     ),
 
     io.write_string("Loaded the map from file: " ++ FileName ++ "\n", !IO),

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list