[m-rev.] Java back-end milestone

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Dec 2 20:59:37 AEDT 2003


Estimated hours taken: 6
Branches: main

More work on the Java back-end.  The standard library now compiles
in grade `java', and hello world (the version using io__write_string,
and linked against the standard library in library/*.m, not the
hand-coded one in java/library/*.java) now works!

compiler/make_hlds.m:
	Ignore `pragma type_spec' declarations for the Java back-end.
	This works around a problem where javac was failing to compile
	some of our generated code due to it overflowing limits on
	file name length for the names of the .class files for some
	nested classes.

compiler/mlds_to_java.m:
	Add some comments.  Add myself to the "main authors" list.

library/string.m:
	Provide Java definitions of string__first_char and
	string__unsafe_index.  (These are needed for string.append,
	which is used by private_builtin.sorry.)

library/io.m:
	Provide Java definitions of io__write_{string,int,char,float}/3.

java/runtime/TypeCtorInfo_Struct.java:
	Fix a cut-and-paste error.

java/runtime/TypeInfo_Struct.java:
	Improve the implementation of the TypeInfo_Struct(Object)
	constructor so that it doesn't throw exceptions during
	the initialization of the standard library.

java/runtime/FA_TypeInfo_Struct1.java:
	Make this type inherit from TypeInfo_Struct.

Workspace: /home/jupiter/fjh/ws-jupiter/mercury
Index: compiler/make_hlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make_hlds.m,v
retrieving revision 1.455
diff -u -d -r1.455 make_hlds.m
--- compiler/make_hlds.m	28 Nov 2003 20:40:12 -0000	1.455
+++ compiler/make_hlds.m	1 Dec 2003 13:23:15 -0000
@@ -813,7 +813,19 @@
 	;
 		Pragma = type_spec(_, _, _, _, _, _, _, _)
 	->
-		add_pragma_type_spec(Pragma, Context, !Module, !Info, !IO)
+		%
+		% XXX For the Java back-end, `pragma type_spec' can
+		% result in class names that exceed the limits on file
+		% name length.  So we ignore these pragmas for the
+		% Java back-end.
+		%
+		globals__io_get_target(Target, !IO),
+		( Target = java ->
+			true
+		;
+			add_pragma_type_spec(Pragma, Context, !Module, !Info,
+				!IO)
+		)
 	;
  		Pragma = termination_info(PredOrFunc, SymName, ModeList,
  			MaybeArgSizeInfo, MaybeTerminationInfo)
Index: compiler/mlds_to_java.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_java.m,v
retrieving revision 1.47
diff -u -d -r1.47 mlds_to_java.m
--- compiler/mlds_to_java.m	1 Dec 2003 13:16:53 -0000	1.47
+++ compiler/mlds_to_java.m	2 Dec 2003 09:42:46 -0000
@@ -5,7 +5,7 @@
 %-----------------------------------------------------------------------------%
 %
 % mlds_to_java - Convert MLDS to Java code.
-% Main authors: juliensf, mjwybrow. 
+% Main authors: juliensf, mjwybrow, fjh. 
 %
 % DONE:
 %	det and semidet predicates
@@ -28,8 +28,15 @@
 % 		(Java does not allow the name of a nested class to be
 % 		the same as its enclosing class)
 %	General code cleanup
-%	handle static ground terms
-%	RTTI (requires static ground terms)
+%	handle static ground terms(?)
+%	RTTI: currently we generate RTTI data which compiles,
+%		but which is not actually usable.  The class
+%		hierarchy of the RTTI classes in java/runtime/*.java
+%		needs quite a bit of work.  It is implemented in a way
+%		that mirrors what we do for C, but in C we rely on being
+%		able to coerce between different types that have the same
+%		initial fields (e.g. FA_TypeInfo_Struct and TypeInfo_Struct),
+%		and that doesn't work in Java.
 %	generate names of classes etc. correctly (mostly same as IL backend)
 %	support foreign_import_module for Java
 %	handle foreign code written in C 
Index: java/runtime/FA_TypeInfo_Struct1.java
===================================================================
RCS file: /home/mercury1/repository/mercury/java/runtime/FA_TypeInfo_Struct1.java,v
retrieving revision 1.1
diff -u -d -r1.1 FA_TypeInfo_Struct1.java
--- java/runtime/FA_TypeInfo_Struct1.java	8 Jul 2003 10:30:00 -0000	1.1
+++ java/runtime/FA_TypeInfo_Struct1.java	2 Dec 2003 09:56:04 -0000
@@ -6,9 +6,10 @@
 
 package mercury.runtime;
 
-public class FA_TypeInfo_Struct1 extends PseudoTypeInfo {
+public class FA_TypeInfo_Struct1 extends TypeInfo_Struct {
 	public FA_TypeInfo_Struct1(TypeCtorInfo_Struct type_ctor_info,
 			Object[] args) {
 		// XXX stub only
+		super(type_ctor_info);
 	}
 }
Index: java/runtime/TypeCtorInfo_Struct.java
===================================================================
RCS file: /home/mercury1/repository/mercury/java/runtime/TypeCtorInfo_Struct.java,v
retrieving revision 1.4
diff -u -d -r1.4 TypeCtorInfo_Struct.java
--- java/runtime/TypeCtorInfo_Struct.java	1 Dec 2003 06:55:51 -0000	1.4
+++ java/runtime/TypeCtorInfo_Struct.java	1 Dec 2003 13:30:44 -0000
@@ -42,7 +42,7 @@
 		type_functors = (mercury.runtime.TypeFunctors)
 			name_ordered_functor_descs;
 		type_layout = (mercury.runtime.TypeLayout)
-			name_ordered_functor_descs;
+			value_ordered_functor_descs;
 		type_ctor_flags = flags;
 	}
 }
Index: java/runtime/TypeInfo_Struct.java
===================================================================
RCS file: /home/mercury1/repository/mercury/java/runtime/TypeInfo_Struct.java,v
retrieving revision 1.2
diff -u -d -r1.2 TypeInfo_Struct.java
--- java/runtime/TypeInfo_Struct.java	1 Dec 2003 13:17:06 -0000	1.2
+++ java/runtime/TypeInfo_Struct.java	2 Dec 2003 07:52:01 -0000
@@ -28,12 +28,6 @@
 		args = ti.args;
 	}
 
-	// XXX a temp hack just to get things to link
-	public TypeInfo_Struct(java.lang.Object ti)
-	{
-		throw new java.lang.Error("TypeInfo_Struct(Object)");
-	}
-
 	//
 	// constructors for fixed-arity type_infos
 	//
@@ -81,5 +75,25 @@
 		// assert arity == 2;
 		type_ctor = tc;
 		args = new PseudoTypeInfo[] { a1, a2 };
+	}
+
+	// XXX a temp hack just to get things to run
+	public TypeInfo_Struct(java.lang.Object obj)
+	{
+		try {
+			TypeInfo_Struct ti = (TypeInfo_Struct) obj;
+			type_ctor = ti.type_ctor;
+			args = ti.args;
+		} catch (java.lang.Exception e) {
+			try {
+				TypeCtorInfo_Struct tci =
+					(TypeCtorInfo_Struct) obj;
+				type_ctor = tci;
+				args = null;
+			} catch (java.lang.Exception e2) {
+				throw new java.lang.Error(
+					"TypeInfo_Struct(Object)");
+			}
+		}
 	}
 }
Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.313
diff -u -d -r1.313 io.m
--- library/io.m	27 Nov 2003 13:27:03 -0000	1.313
+++ library/io.m	2 Dec 2003 09:10:17 -0000
@@ -1664,7 +1664,8 @@
 
 :- type io__stream --->		io__stream(c_pointer).
 :- pragma foreign_type("C", io__stream, "MercuryFilePtr").
-:- pragma foreign_type("il", io__stream, "class [mercury]mercury.io__csharp_code.MR_MercuryFileStruct").
+:- pragma foreign_type("il", io__stream,
+	"class [mercury]mercury.io__csharp_code.MR_MercuryFileStruct").
 
 	% a unique identifier for an IO stream
 :- type io__stream_id == int.
@@ -5550,6 +5551,31 @@
 	[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 	mercury_current_binary_output.stream.Flush();
+").
+
+:- pragma foreign_proc("Java", 
+	io__write_string(Message::in, _IO0::di, _IO::uo),
+	[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
+"
+	System.out.print(Message);
+").
+:- pragma foreign_proc("Java", 
+	io__write_char(Character::in, _IO0::di, _IO::uo),
+	[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
+"
+	System.out.print(Character);
+").
+:- pragma foreign_proc("Java",
+	io__write_int(Val::in, _IO0::di, _IO::uo),
+	[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
+"
+	System.out.print(Val);
+").
+:- pragma foreign_proc("Java",
+	io__write_float(Val::in, _IO0::di, _IO::uo),
+	[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
+"
+	System.out.print(Val);
 ").
 
 io__write_float(Float) -->
Index: library/string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.208
diff -u -d -r1.208 string.m
--- library/string.m	12 Nov 2003 16:24:01 -0000	1.208
+++ library/string.m	2 Dec 2003 08:56:15 -0000
@@ -2994,6 +2994,12 @@
 "
 	Ch = Str[Index];
 ").
+:- pragma foreign_proc("Java", 
+	string__unsafe_index(Str::in, Index::in, Ch::uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	Ch = Str.charAt(Index);
+").
 string__unsafe_index(Str, Index, Char) :-
 	( string__first_char(Str, First, Rest) ->
 		( Index = 0 ->
@@ -3548,6 +3554,14 @@
 		System.String.Compare(Str, 1, Rest, 0, len) == 0
 	);
 ").
+:- pragma foreign_proc("Java",
+	string__first_char(Str::in, First::in, Rest::in),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	succeeded = (Str.length() == Rest.length() + 1 &&
+		Str.charAt(0) == First &&
+		Str.endsWith(Rest));
+").
 
 /*
 :- mode string__first_char(in, uo, in) is semidet.	% implied
@@ -3572,6 +3586,22 @@
 		SUCCESS_INDICATOR = false;
 	}
 ").
+:- pragma foreign_proc("Java",
+	string__first_char(Str::in, First::uo, Rest::in),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	
+	if (Str.length() == Rest.length() + 1
+		&& Str.endsWith(Rest))
+	{
+		succeeded = true;
+		First = Str.charAt(0);
+	} else {
+		succeeded = false;
+		// XXX to avoid uninitialized var warning
+		First = (char) 0;
+	}
+").
 
 /*
 :- mode string__first_char(in, in, uo) is semidet.	% implied
@@ -3601,11 +3631,25 @@
 	int len = Str.Length;
 	if (len > 0) {
 		SUCCESS_INDICATOR = (First == Str[0]);
-		Rest = (Str).Substring(1);
+		Rest = Str.Substring(1);
 	} else {
 		SUCCESS_INDICATOR = false;
 	}
 }").
+:- pragma foreign_proc("Java",
+	string__first_char(Str::in, First::in, Rest::uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"{
+	int len = Str.length();
+	if (len > 0) {
+		succeeded = (First == Str.charAt(0));
+		Rest = Str.substring(1);
+	} else {
+		succeeded = false;
+		// XXX to avoid uninitialized var warning
+		Rest = null;
+	}
+}").
 
 /*
 :- mode string__first_char(in, uo, uo) is semidet.
@@ -3637,10 +3681,25 @@
 		SUCCESS_INDICATOR = false;
 	} else {
 		First = Str[0];
-		Rest = (Str).Substring(1);
+		Rest = Str.Substring(1);
 		SUCCESS_INDICATOR = true;
         }
 }").
+:- pragma foreign_proc("Java", 
+	string__first_char(Str::in, First::uo, Rest::uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"{
+	if (Str.length() == 0) {
+		succeeded = false;
+		// XXX to avoid uninitialized var warnings:
+		First = (char) 0;
+		Rest = null;
+	} else {
+		First = Str.charAt(0);
+		Rest = Str.substring(1);
+		succeeded = true;
+        }
+}").
 
 /*
 :- mode string__first_char(uo, in, in) is det.
@@ -3661,6 +3720,13 @@
 	string FirstStr;
 	FirstStr = new System.String(First, 1);
 	Str = System.String.Concat(FirstStr, Rest);
+}").
+:- pragma foreign_proc("Java",
+	string__first_char(Str::uo, First::in, Rest::in),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"{
+	java.lang.String FirstStr = java.lang.String.valueOf(First);
+	Str = FirstStr.concat(Rest);
 }").
 
 %-----------------------------------------------------------------------------%

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list