[m-rev.] for review: implement solutions on the IL backend

Tyson Dowd trd at miscrit.be
Mon Aug 6 23:58:50 AEST 2001


On 05-Aug-2001, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> Pete, you seem to have ignored these review comments and committed your
> code anyway.
> 
> This is really not acceptable.
> Please address the review comments ASAP or back out your change.

Pete doesn't have the time to fix this at the moment, so I will do it.

> 
> On 15-Jul-2001, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> > On 14-Jul-2001, Peter Ross <peter.ross at miscrit.be> wrote:
> > > compiler/ml_code_gen.m:
> > >     Avoid generating casts and assignments from MR_Box to MR_Word as
> > >     these operations aren't type-safe on the IL backend.
> > 
> > What do you mean by "aren't type-safe"?
> > A little more explanation here might help.
> > 
> > > Index: compiler/ml_code_gen.m
> > > +ml_gen_pragma_c_decl(ml_c_arg(_Var, MaybeNameAndMode, Type), Decl) -->
> > > +	=(MLDSGenInfo),
> > > +	{ ml_gen_info_get_module_info(MLDSGenInfo, ModuleInfo) },
> > > +	{ module_info_globals(ModuleInfo, Globals) },
> > > +	{ globals__get_target(Globals, Target) },
> > > +	{
> > >  		MaybeNameAndMode = yes(ArgName - _Mode),
> > >  		\+ var_is_singleton(ArgName)
> > >  	->
> > > -		export__type_to_type_string(Type, TypeString),
> > > +		( 
> > > +			type_util__var(Type, _),
> > > +			Target = il
> > > +		->
> > > +			TypeString = "MR_Box"
> > > +		;
> > > +			export__type_to_type_string(Type, TypeString)
> > > +		),
> > 
> > I think "Target = il" is not the right test here, or at least not the best test.
> > This is really a property of the foreign language interface, rather than of
> > the target language, so you should be switching on
> > `Lang = managedcplusplus' rather than `Target = il',
> > where `Lang' is obtained via `foreign_language(Attributes, Lang)'
> > (you'll need to pass it down to ml_gen_pragma_c_decl).
> > 
> > This difference between the C interface and the MC++ interface really
> > ought to be documented.  Unfortunately the MC++ foreign code interface
> > is not documented at all, so for now I just suggest you put something
> > commented out (with @c) in doc/reference_manual.texi, e.g. see patch below.

The MC++ interface really needs to be fixed so that it doesn't rely on
the C foreign language interfacing at all.  This is a medium size job
that I don't have time to do just yet.

Documenting it is Sourceforge task number 34988.

One piece of documentation that really should be known to you is that
functions don't work in Managed C++.

> > Also, this test should be done in all of the calls to
> > export__type_to_type_string from this file.  (Make a new predicate to
> > abstract the common code.)  It might even be better in the long run to
> > do this inside export__type_to_type_string itself (i.e. to make the
> > foreign_language a parameter to that routine), but that can probably
> > wait for another day.

I think the export code is best left to just handling C code -- we
re-use far too much of it for MC++ as it is.  It would be much easier to
maintain if we just implement MC++ the same way as C# (Task 34698).

Here is the diff addressing your concerns.  I am testing it now.

===================================================================


Estimated hours taken: 1
Branches: main

compiler/ml_code_gen.m:
	Address fjh's review comments regarding the fixes required to
	get solutions working in the .NET backend.
	


Index: compiler/ml_code_gen.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_code_gen.m,v
retrieving revision 1.97
diff -u -r1.97 ml_code_gen.m
--- compiler/ml_code_gen.m	2001/07/20 14:13:54	1.97
+++ compiler/ml_code_gen.m	2001/08/06 13:55:44
@@ -2134,7 +2134,7 @@
 	%
 	% Generate <declaration of one local variable for each arg>
 	%
-	ml_gen_pragma_c_decls(ArgList, ArgDeclsList),
+	ml_gen_pragma_c_decls(Lang, ArgList, ArgDeclsList),
 
 	%
 	% Generate definitions of the FAIL, SUCCEED, SUCCEED_LAST,
@@ -2156,12 +2156,12 @@
 	%
 	% Generate code to set the values of the input variables.
 	%
-	ml_gen_pragma_c_input_arg_list(ArgList, AssignInputsList),
+	ml_gen_pragma_c_input_arg_list(Lang, ArgList, AssignInputsList),
 
 	%
 	% Generate code to assign the values of the output variables.
 	%
-	ml_gen_pragma_c_output_arg_list(ArgList, Context,
+	ml_gen_pragma_c_output_arg_list(Lang, ArgList, Context,
 		AssignOutputsList, ConvDecls, ConvStatements),
 
 	%
@@ -2521,6 +2521,9 @@
 ml_gen_ordinary_pragma_c_proc(CodeModel, Attributes,
 		PredId, _ProcId, ArgVars, ArgDatas, OrigArgTypes,
 		C_Code, Context, MLDS_Decls, MLDS_Statements) -->
+
+	{ foreign_language(Attributes, Lang) },
+
 	%
 	% Combine all the information about the each arg
 	%
@@ -2530,17 +2533,17 @@
 	%
 	% Generate <declaration of one local variable for each arg>
 	%
-	ml_gen_pragma_c_decls(ArgList, ArgDeclsList),
+	ml_gen_pragma_c_decls(Lang, ArgList, ArgDeclsList),
 
 	%
 	% Generate code to set the values of the input variables.
 	%
-	ml_gen_pragma_c_input_arg_list(ArgList, AssignInputsList),
+	ml_gen_pragma_c_input_arg_list(Lang, ArgList, AssignInputsList),
 
 	%
 	% Generate code to assign the values of the output variables.
 	%
-	ml_gen_pragma_c_output_arg_list(ArgList, Context,
+	ml_gen_pragma_c_output_arg_list(Lang, ArgList, Context,
 		AssignOutputsList, ConvDecls, ConvStatements),
 
 	%
@@ -2709,41 +2712,32 @@
 
 %---------------------------------------------------------------------------%
 
-% ml_gen_pragma_c_decls generates C code to declare the arguments
-% for a `pragma c_code' declaration.
+% ml_gen_pragma_c_decls generates C code to declare the arguments
+% for a `pragma foreign_proc' declaration.
 %
-:- pred ml_gen_pragma_c_decls(list(ml_c_arg)::in,
+:- pred ml_gen_pragma_c_decls(foreign_language::in, list(ml_c_arg)::in,
 		list(target_code_component)::out,
 		ml_gen_info::in, ml_gen_info::out) is det.
 
-ml_gen_pragma_c_decls([], []) --> [].
-ml_gen_pragma_c_decls([Arg|Args], [Decl|Decls]) -->
-	ml_gen_pragma_c_decl(Arg, Decl),
-	ml_gen_pragma_c_decls(Args, Decls).
+ml_gen_pragma_c_decls(_, [], []) --> [].
+ml_gen_pragma_c_decls(Lang, [Arg|Args], [Decl|Decls]) -->
+	ml_gen_pragma_c_decl(Lang, Arg, Decl),
+	ml_gen_pragma_c_decls(Lang, Args, Decls).
 
 % ml_gen_pragma_c_decl generates C code to declare an argument
-% of a `pragma c_code' declaration.
+% of a `pragma foreign_proc' declaration.
 %
-:- pred ml_gen_pragma_c_decl(ml_c_arg::in, target_code_component::out,
+:- pred ml_gen_pragma_c_decl(foreign_language::in,
+		ml_c_arg::in, target_code_component::out,
 		ml_gen_info::in, ml_gen_info::out) is det.
 
-ml_gen_pragma_c_decl(ml_c_arg(_Var, MaybeNameAndMode, Type), Decl) -->
-	=(MLDSGenInfo),
-	{ ml_gen_info_get_module_info(MLDSGenInfo, ModuleInfo) },
-	{ module_info_globals(ModuleInfo, Globals) },
-	{ globals__get_target(Globals, Target) },
+ml_gen_pragma_c_decl(Lang, ml_c_arg(_Var, MaybeNameAndMode, Type),
+		Decl) -->
 	{
 		MaybeNameAndMode = yes(ArgName - _Mode),
 		\+ var_is_singleton(ArgName)
 	->
-		( 
-			type_util__var(Type, _),
-			Target = il
-		->
-			TypeString = "MR_Box"
-		;
-			export__type_to_type_string(Type, TypeString)
-		),
+		TypeString = foreign_type_to_type_string(Lang, Type),
 		string__format("\t%s %s;\n", [s(TypeString), s(ArgName)],
 			DeclString)
 	;
@@ -2753,6 +2747,17 @@
 	},
 	{ Decl = raw_target_code(DeclString, []) }.
 
+:- func foreign_type_to_type_string(foreign_language, prog_data__type) = string.
+foreign_type_to_type_string(Lang, Type) = TypeString :-
+	( 
+		type_util__var(Type, _),
+		Lang = managed_cplusplus
+	->
+		TypeString = "MR_Box"
+	;
+		export__type_to_type_string(Type, TypeString)
+	).
+
 %-----------------------------------------------------------------------------%
 
 % var_is_singleton determines whether or not a given pragma_c variable
@@ -2772,22 +2777,23 @@
 
 %-----------------------------------------------------------------------------%
 
-:- pred ml_gen_pragma_c_input_arg_list(list(ml_c_arg)::in,
-		list(target_code_component)::out,
+:- pred ml_gen_pragma_c_input_arg_list(foreign_language::in,
+		list(ml_c_arg)::in, list(target_code_component)::out,
 		ml_gen_info::in, ml_gen_info::out) is det.
 
-ml_gen_pragma_c_input_arg_list(ArgList, AssignInputs) -->
-	list__map_foldl(ml_gen_pragma_c_input_arg, ArgList, AssignInputsList),
+ml_gen_pragma_c_input_arg_list(Lang, ArgList, AssignInputs) -->
+	list__map_foldl(ml_gen_pragma_c_input_arg(Lang), ArgList,
+		AssignInputsList),
 	{ list__condense(AssignInputsList, AssignInputs) }.
 
-% ml_gen_pragma_c_input_arg generates C code to assign the value of an input
-% arg for a `pragma c_code' declaration.
+% ml_gen_pragma_c_input_arg generates C code to assign the value of an
+% input arg for a `pragma foreign_proc' declaration.
 %
-:- pred ml_gen_pragma_c_input_arg(ml_c_arg::in,
+:- pred ml_gen_pragma_c_input_arg(foreign_language::in, ml_c_arg::in,
 		list(target_code_component)::out,
 		ml_gen_info::in, ml_gen_info::out) is det.
 
-ml_gen_pragma_c_input_arg(ml_c_arg(Var, MaybeNameAndMode, OrigType),
+ml_gen_pragma_c_input_arg(Lang, ml_c_arg(Var, MaybeNameAndMode, OrigType),
 		AssignInput) -->
 	=(MLDSGenInfo),
 	{ ml_gen_info_get_module_info(MLDSGenInfo, ModuleInfo) },
@@ -2813,25 +2819,25 @@
 		{ module_info_globals(ModuleInfo, Globals) },
 		{ globals__lookup_bool_option(Globals, highlevel_data,
 			HighLevelData) },
-		{ globals__get_target(Globals, Target) },
 		{ HighLevelData = yes ->
 			% In general, the types used for the C interface
 			% are not the same as the types used by
 			% --high-level-data, so we always use a cast here.
 			% (Strictly speaking the cast is not needed for
 			% a few cases like `int', but it doesn't do any harm.)
-			export__type_to_type_string(OrigType, TypeString),
+			TypeString = foreign_type_to_type_string(Lang,
+				OrigType),
 			string__format("(%s)", [s(TypeString)], Cast)
 		;
 			% For --no-high-level-data, we only need to use
 			% a cast is for polymorphic types, which are
 			% `Word' in the C interface but `MR_Box' in the
 			% MLDS back-end.
-			% Except for --grade ilc, where polymorphic types
+			% Except for MC++, where polymorphic types
 			% are MR_Box.
 			( 
 				type_util__var(OrigType, _),
-				Target \= il
+				Lang \= managed_cplusplus
 			->
 				Cast = "(MR_Word) "
 			;
@@ -2852,18 +2858,19 @@
 		{ AssignInput = [] }
 	).
 
-:- pred ml_gen_pragma_c_output_arg_list(list(ml_c_arg)::in, prog_context::in,
+:- pred ml_gen_pragma_c_output_arg_list(foreign_language::in,
+		list(ml_c_arg)::in, prog_context::in,
 		list(target_code_component)::out,
 		mlds__defns::out, mlds__statements::out,
 		ml_gen_info::in, ml_gen_info::out) is det.
 
-ml_gen_pragma_c_output_arg_list([], _, [], [], []) --> [].
-ml_gen_pragma_c_output_arg_list([C_Arg | C_Args], Context, Components,
-		ConvDecls, ConvStatements) -->
-	ml_gen_pragma_c_output_arg(C_Arg, Context, Components1,
+ml_gen_pragma_c_output_arg_list(_, [], _, [], [], []) --> [].
+ml_gen_pragma_c_output_arg_list(Lang, [C_Arg | C_Args], Context,
+		Components, ConvDecls, ConvStatements) -->
+	ml_gen_pragma_c_output_arg(Lang, C_Arg, Context, Components1,
 			ConvDecls1, ConvStatements1),
-	ml_gen_pragma_c_output_arg_list(C_Args, Context, Components2,
-			ConvDecls2, ConvStatements2),
+	ml_gen_pragma_c_output_arg_list(Lang, C_Args, Context,
+			Components2, ConvDecls2, ConvStatements2),
 	{ Components = list__append(Components1, Components2) },
 	{ ConvDecls = list__append(ConvDecls1, ConvDecls2) },
 	{ ConvStatements = list__append(ConvStatements1, ConvStatements2) }.
@@ -2871,12 +2878,13 @@
 % ml_gen_pragma_c_output_arg generates C code to assign the value of an output
 % arg for a `pragma c_code' declaration.
 %
-:- pred ml_gen_pragma_c_output_arg(ml_c_arg::in, prog_context::in,
+:- pred ml_gen_pragma_c_output_arg(foreign_language::in,
+		ml_c_arg::in, prog_context::in,
 		list(target_code_component)::out,
 		mlds__defns::out, mlds__statements::out,
 		ml_gen_info::in, ml_gen_info::out) is det.
 
-ml_gen_pragma_c_output_arg(ml_c_arg(Var, MaybeNameAndMode, OrigType),
+ml_gen_pragma_c_output_arg(Lang, ml_c_arg(Var, MaybeNameAndMode, OrigType),
 		Context, AssignOutput, ConvDecls, ConvOutputStatements) -->
 	=(MLDSGenInfo),
 	{ ml_gen_info_get_module_info(MLDSGenInfo, ModuleInfo) },
@@ -2904,7 +2912,8 @@
 			% Note that we can't easily obtain the type string
 			% for the RHS of the assignment, so instead we
 			% cast the LHS.
-			export__type_to_type_string(OrigType, TypeString),
+			TypeString = foreign_type_to_type_string(Lang,
+				OrigType),
 			string__format("*(%s *)&", [s(TypeString)], LHS_Cast),
 			RHS_Cast = ""
 		;



Tyson.
--------------------------------------------------------------------------
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