[m-dev.] for review: Java backend

Julien Fischer juliensf at students.cs.mu.oz.au
Thu Mar 1 17:56:13 AEDT 2001


I've addressed all Fergus's review comments minus the one about
treating `dummy_var' as a special case in the output_fully_qualified_name
predicate.  So far it hasn't caused any problems as it is; why exactly
do you want it treated as a special case?

--------------------
Addition to log message:


compiler/c_util.m:
	Added comment about Java backend.


Here is a diff of c_util.m and a relative diff of mlds_to_java.m,
java_util.m:

Index: compiler/c_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/c_util.m,v
retrieving revision 1.7
diff -u -r1.7 c_util.m
--- compiler/c_util.m	2001/01/20 15:42:42	1.7
+++ compiler/c_util.m	2001/03/01 05:04:41
@@ -8,7 +8,8 @@
 % Main author: fjh.
 
 % This module defines utility routines that are useful when
-% generating and/or emitting C code.
+% generating and/or emitting C code.  Changes to this module may require
+% changes to be made to java_util.m
 
 %-----------------------------------------------------------------------------%
 


diff -u java_util.m java_util.m
--- java_util.m
+++ java_util.m
@@ -8,7 +8,8 @@
 % Main author: juliensf.
 
 % This module defines utility routines that are used by the
-% Java backend. 
+% Java backend.  Much of the code below is similar to that in c_util.m;
+% changes made to this module may require changes c_util.m. 
 
 %-----------------------------------------------------------------------------%
 
@@ -71,17 +72,24 @@
 
 :- implementation.
 :- import_module list.
+:- import_module error_util.
 
 %-----------------------------------------------------------------------------%
 
 % XXX I'm not sure what the first six of these should be
 %     for the Java backend.
-java_util__unary_prefix_op(mktag,		"").
-java_util__unary_prefix_op(tag,			"").
-java_util__unary_prefix_op(unmktag,		"").
-java_util__unary_prefix_op(mkbody,		"").
-java_util__unary_prefix_op(unmkbody,		"").
-java_util__unary_prefix_op(hash_string,		"").
+java_util__unary_prefix_op(mktag, _) :-
+	unexpected(this_file, "Java backend does not support tags").
+java_util__unary_prefix_op(tag,	_) :-
+	unexpected(this_file, "Java backend does not support tags").
+java_util__unary_prefix_op(unmktag, _) :-
+	unexpected(this_file, "Java backend does not support tags").
+java_util__unary_prefix_op(mkbody, _) :-
+	unexpected(this_file, "Java backend does not support tags").
+java_util__unary_prefix_op(unmkbody, _) :-
+	unexpected(this_file, "Java backend does not support tags").
+java_util__unary_prefix_op(hash_string, _) :-
+	sorry(this_file, "hash_string operators not supported yet").
 java_util__unary_prefix_op(bitwise_complement,	"~").
 java_util__unary_prefix_op(not,			"!").
 
@@ -170,6 +178,9 @@
 java_util__is_keyword("throws").
 java_util__is_keyword("transient").
 java_util__is_keyword("true").
+
+:- func this_file = string.
+this_file = "java_util.m".
 
 :- end_module java_util.
 %-----------------------------------------------------------------------------%
diff -u compiler/mlds_to_java.m compiler/mlds_to_java.m
--- compiler/mlds_to_java.m
+++ compiler/mlds_to_java.m
@@ -174,7 +174,8 @@
 	; Builtin = user_type 
 	).
 
-	% Succeeds iff the Rval represents an integer constant.  %
+	% Succeeds iff the Rval represents an integer constant.  
+	%
 :- pred rval_is_int_const(mlds__rval).
 :- mode rval_is_int_const(in) is semidet.
 
@@ -184,27 +185,19 @@
 	% Succeeds iff the Rval represents an enumeration
 	% object in the Java backend.  We need to check both Rval's
 	% that are variables and Rval's that are casts.  
-	% We need to know this in order to append the a field name 
-	% to to object so we can access the value of the enumeration object.
-	%
-	% XXX Perhaps we need to do this for other unop's?
+	% We need to know this in order to append the field name 
+	% to object so we can access the value of the enumeration object.
 	%
 :- pred rval_is_enum_object(mlds__rval).
 :- mode rval_is_enum_object(in) is semidet.
 
 rval_is_enum_object(Rval) :-
-	( 
 		Rval = lval(Lval),
 		Lval = var(_, VarType),
-		type_is_enum(VarType)
-	;
-		Rval = unop(cast(_), Rval1),
-		rval_is_enum_object(Rval1)
-	).
-
+		type_is_enum(VarType).
 
 	% Succeeds iff a given string matches the unqualified
-	% interface name of a interface in the Java runtime system.
+	% interface name of a interface in Mercury's Java runtime system.
 	%
 :- pred interface_is_special(string).
 :- mode interface_is_special(in) is semidet.
@@ -218,12 +211,13 @@
 % Code to mangle names, enforce Java code conventions regarding class names
 % etc.
 % XXX None of this stuff works as it should. The idea is that class
-% names should be uppercase, while method names and package specifiers
-% should be lowercase.  The current implementation of the MLDS makes
-% this rather harder to achieve than it might initially seem.  The current
-% position is that coding conventions are only enforced on library modules.
+% names should start with an uppercase letter, while method names and 
+% package specifiers should start with a lowercase letter.  
+% The current implementation of the MLDS makes this rather harder to achieve 
+% than it might initially seem.  The current position is that coding 
+% conventions are only enforced on library modules.
 % This is need as Java compilers don't take too well to compiling
-% classes named char, int, float etc.
+% classes named `char',`int', `float' etc.
 % XXX It might be nice if the name mangling code was taken out of which
 % ever LLDS module it's hiding in and put in a seperate one.
 %
@@ -319,7 +313,7 @@
 	{ MLDS_ModuleName = mercury_module_name_to_mlds(ModuleName) }, 
 	{ Defns1 = Defns0 },
 	% XXX The code to transform special precdicates isn't working yet.
-	% { transform_special_predicates(ModuleName, Defns0, Defns1) },
+	%{ transform_special_predicates(ModuleName, Defns0, Defns1) },
 	%
 	% Output transformed MLDS as Java souce.  
 	%
@@ -499,7 +493,7 @@
 	%
 	% Transform a list of function arguments into a list of local
 	% variable declarations of the same name and type.  Create
-	% initializers that initalize each new local variable to the
+	% initializers that initialize each new local variable to the
 	% correct element in the `args' array.
 	%
 :- pred generate_wrapper_decls(mercury_module_name, mlds__context, 
@@ -511,15 +505,13 @@
 		Count, [Defn | Defns]) :-
 	Arg = Name - Type,
 	Flags = ml_gen_local_var_decl_flags,
-	string__int_to_string(Count, Index),
-	string__append("args[", Index, UnqualName0),
-	string__append(UnqualName0, "]", UnqualName),
-	NewVarName = qual(mercury_module_name_to_mlds(ModuleName), UnqualName),
-	%		
+	ArrayIndex = const(int_const(Count)),		
+	NewVarName = qual(mercury_module_name_to_mlds(ModuleName), "args"),
+	NewArgLval = var(NewVarName, mlds__generic_type),
+	%	
 	% Package everything together.
 	%
-	NewArgLval = var(NewVarName, mlds__generic_type),
-	Initializer = lval(NewArgLval),
+	Initializer = binop(array_index, lval(NewArgLval), ArrayIndex),
 	Body = mlds__data(Type, init_obj(Initializer)),	
 	Defn = mlds__defn(Name, Context, Flags, Body),
 	%	
@@ -605,9 +597,9 @@
 output_src_end(Indent, ModuleName) -->
 	io__write_string("}\n"),
 	indent_line(Indent),
-	io__write_string("/* :- end_module "),
+	io__write_string("// :- end_module "),
 	prog_out__write_sym_name(ModuleName),
-	io__write_string(". */\n").
+	io__write_string(".\n").
 
 	% Output a Java comment saying that the file was automatically
 	% generated and give details such as the compiler version.
@@ -657,7 +649,7 @@
 	output_data_defn(Name, Type, Initializer).
 output_defn_body(Indent, Name, Context, 
 		mlds__function(MaybePredProcId, Signature, MaybeBody)) -->
-	mlds_output_maybe(MaybePredProcId, mlds_output_pred_proc_id),
+	output_maybe(MaybePredProcId, output_pred_proc_id),
 	output_func(Indent, Name, Context, Signature, MaybeBody).
 output_defn_body(Indent, Name, Context, mlds__class(ClassDefn)) -->
 	output_class(Indent, Name, Context, ClassDefn).
@@ -896,11 +888,11 @@
 	unexpected(this_file, 
 		"get_type_initializer: variable has unknown_type"). 
 
-:- pred mlds_output_maybe(maybe(T), pred(T, io__state, io__state),
+:- pred output_maybe(maybe(T), pred(T, io__state, io__state),
 		io__state, io__state).
-:- mode mlds_output_maybe(in, pred(in, di, uo) is det, di, uo) is det.
+:- mode output_maybe(in, pred(in, di, uo) is det, di, uo) is det.
 
-mlds_output_maybe(MaybeValue, OutputAction) -->
+output_maybe(MaybeValue, OutputAction) -->
 	( { MaybeValue = yes(Value) } ->
 		OutputAction(Value)
 	;
@@ -913,7 +905,7 @@
 
 output_initializer(Type, Initializer) -->
 	io__write_string(" = "),
-	( { mlds_needs_initialization(Initializer) = yes } ->
+	( { needs_initialization(Initializer) = yes } ->
 		( { Initializer = init_obj(Rval) } ->
 			( 
 				{ type_is_object(Type) },
@@ -955,14 +947,14 @@
 		io__write_string(get_java_type_initializer(Type))
 	).
 
-:- func mlds_needs_initialization(mlds__initializer) = bool.
-:- mode mlds_needs_initialization(in) = out is det.
+:- func needs_initialization(mlds__initializer) = bool.
+:- mode needs_initialization(in) = out is det.
 
-mlds_needs_initialization(no_initializer) = no.
-mlds_needs_initialization(init_obj(_)) = yes.
-mlds_needs_initialization(init_struct([])) = no.
-mlds_needs_initialization(init_struct([_|_])) = yes.
-mlds_needs_initialization(init_array(_)) = yes.
+needs_initialization(no_initializer) = no.
+needs_initialization(init_obj(_)) = yes.
+needs_initialization(init_struct([])) = no.
+needs_initialization(init_struct([_|_])) = yes.
+needs_initialization(init_array(_)) = yes.
 
 :- pred output_initializer_body(mlds__initializer, io__state, io__state).
 :- mode output_initializer_body(in,di, uo) is det.
@@ -982,19 +974,19 @@
 % Code to output function declarations/definitions
 %
 
-:- pred mlds_output_pred_proc_id(pred_proc_id, io__state, io__state).
-:- mode mlds_output_pred_proc_id(in, di, uo) is det.
+:- pred output_pred_proc_id(pred_proc_id, io__state, io__state).
+:- mode output_pred_proc_id(in, di, uo) is det.
 
-mlds_output_pred_proc_id(proc(PredId, ProcId)) -->
+output_pred_proc_id(proc(PredId, ProcId)) -->
 	globals__io_lookup_bool_option(auto_comments, AddComments),
 	( { AddComments = yes } ->
-		io__write_string("/* pred_id: "),
+		io__write_string("// pred_id: "),
 		{ pred_id_to_int(PredId, PredIdNum) },
 		io__write_int(PredIdNum),
 		io__write_string(", proc_id: "),
 		{ proc_id_to_int(ProcId, ProcIdNum) },
 		io__write_int(ProcIdNum),
-		io__write_string(" */\n")
+		io__nl	
 	;
 		[]
 	).
@@ -1063,25 +1055,6 @@
 	io__write_char(' '),
 	output_fully_qualified_name(qual(ModuleName, Name)).
 
-:- pred output_param_types(mlds__arguments, io__state, io__state).
-:- mode output_param_types(in, di, uo) is det.
-
-output_param_types(Parameters) -->
-	io__write_char('('),
-	( { Parameters = [] } ->
-		io__write_string("void")
-	;
-		io__write_list(Parameters, ", ", output_param_type)
-	),
-	io__write_char(')').
-
-:- pred output_param_type(pair(mlds__entity_name, mlds__type),
-		io__state, io__state).
-:- mode output_param_type(in, di, uo) is det.
-
-output_param_type(_Name - Type) -->
-	output_type(Type).
-
 %-----------------------------------------------------------------------------%
 %
 % Code to output names of various entities
@@ -2028,9 +2001,9 @@
 output_rval(lval(Lval)) -->
 	output_lval(Lval).
 		
-output_rval(mkword(_Tag, _Rval)) -->
+output_rval(mkword(_, _)) -->
 	{ unexpected(this_file, 
-		"output_rval: mkword(_,_) not yet implemented") }.
+		"output_rval: tags not supported in Java") }.
 
 output_rval(const(Const)) -->
 	output_rval_const(Const).
@@ -2042,7 +2015,7 @@
 	output_binop(Op, Rval1, Rval2).
 
 output_rval(mem_addr(_Lval)) -->
-	{ unexpected(this_file, "output_rval: mem_addr(_) no supported") }.
+	{ unexpected(this_file, "output_rval: mem_addr(_) not supported") }.
 
 :- pred output_unop(mlds__unary_op, mlds__rval, io__state, io__state).
 :- mode output_unop(in, in, di, uo) is det.
@@ -2063,7 +2036,7 @@
 	io__write_string("("),
 	output_type(Type),
 	io__write_string(") "),
-	output_rval(Exprn).
+	output_rval_maybe_with_enum(Exprn).
 
 :- pred output_boxed_rval(mlds__type, mlds__rval, io__state, io__state).
 :- mode output_boxed_rval(in, in, di, uo) is det.


--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list