[m-rev.] for review: minor cleanup to mlds_to_ilasm.m

Fergus Henderson fjh at cs.mu.OZ.AU
Thu May 3 05:11:22 AEST 2001


For review by Tyson.

----------

Estimated hours taken: 1
Branches: main

compiler/mlds_to_ilasm.m:
	Clean up and generalize the code for handling foreign code:
	- Handle the case where a module contains code in more than one
	  foreign language, e.g. both C# and Managed C++.
	  (the rest of the compiler doesn't handles that case yet, though).
	- Make it easier to add new foreign languages.
	- Report an error if there is any foreign language that we don't
	  know about (e.g. C).

tests/hard_coded/Mmakefile:
tests/hard_coded/csharp_test.m:
	Add a simple test of the C# interface.
	The test is not yet enabled, since it doesn't work.

Workspace: /home/mars/fjh/ws1/mercury
Index: compiler/mlds_to_ilasm.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_ilasm.m,v
retrieving revision 1.12
diff -u -d -r1.12 mlds_to_ilasm.m
--- compiler/mlds_to_ilasm.m	2001/05/02 11:36:39	1.12
+++ compiler/mlds_to_ilasm.m	2001/05/02 18:50:21
@@ -55,38 +55,48 @@
 	module_name_to_file_name(ModuleName, ".il", yes, ILAsmFile),
 	output_to_file(ILAsmFile, output_assembler(MLDS), Result),
 
-		% Put the pragma C code into a C++ file.
-		% This is temporary, when we have pragma foreign
-		% we should just put managed C++ foreign code into
-		% this file.
 	( { Result = yes(ForeignLangs) } ->
-		( { set__member(managed_cplusplus, ForeignLangs) } ->
-			module_name_to_file_name(ModuleName,
-				"__c_code.cpp", yes, CPPFile),
-			output_to_file(CPPFile, output_mcpp_code(MLDS))
-		; { set__member(csharp, ForeignLangs) } ->
-			module_name_to_file_name(ModuleName,
-				"__csharp_code.cs", yes, CSFile),
-			output_to_file(CSFile, output_csharp_code(MLDS))
-		;
-			[]
-		)
+		% Output any outline foreign_code to the appropriate foreign
+		% language file.
+		list__foldl(output_foreign_file(MLDS),
+			set__to_sorted_list(ForeignLangs))
 	;
+		% An I/O error occurred; output_to_file has already reported
+		% an error message, so we don't need to do anything here.
 		[]
 	).
 
+:- pred output_foreign_file(mlds::in, foreign_language::in,
+		io__state::di, io__state::uo) is det.
+
+output_foreign_file(MLDS, ForeignLang) -->
+	{ ModuleName = mlds__get_module_name(MLDS) },
+	{ handle_foreign_lang(ForeignLang, Extension, CodeGenerator) },
+	module_name_to_file_name(ModuleName, Extension, yes, File),
+	output_to_file(File, (pred(di, uo) is det --> CodeGenerator(MLDS))).
+
+:- pred handle_foreign_lang(foreign_language::in, string::out,
+		pred(mlds, io__state, io__state)::out(pred(in, di, uo) is det))
+		is det.
+
+handle_foreign_lang(managed_cplusplus, "__c_code.cpp", output_mcpp_code).
+handle_foreign_lang(csharp, "__csharp_code.cpp", output_csharp_code).
+handle_foreign_lang(c, _, _) :-
+	sorry(this_file, "language C foreign code not supported").
+
 	%
-	% Generate the `.il' file
+	% Generate the `.il' file.
+	% Returns the set of foreign language
 	%
 :- pred output_assembler(mlds, set(foreign_language), io__state, io__state).
 :- mode output_assembler(in, out, di, uo) is det.
 
-output_assembler(MLDS, ContainsCCode) -->
+output_assembler(MLDS, ForeignLangs) -->
 	{ MLDS = mlds(ModuleName, _ForeignCode, _Imports, _Defns) },
 	output_src_start(ModuleName), 
 	io__nl,
 
-	generate_il(MLDS, ILAsm0, ContainsCCode),
+	generate_il(MLDS, ILAsm0, ForeignLangs),
 
 		% Perform peephole optimization if requested.
 	globals__io_lookup_bool_option(optimize_peep, Peephole),
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.113
diff -u -d -r1.113 Mmakefile
--- tests/hard_coded/Mmakefile	2001/04/29 18:22:01	1.113
+++ tests/hard_coded/Mmakefile	2001/05/02 19:09:13
@@ -123,6 +123,8 @@
 	write_reg1 \
 	write_reg2
 
+# XXX csharp_test doesn't work yet (not even in il* grades)
+#
 # XXX copy_pred does not work in the hl* grades (e.g. hlc.gc),
 # because the MLDS back-end doesn't generate the closure layout
 # information needed to copy closures.
Index: tests/hard_coded/csharp_test.m
===================================================================
RCS file: csharp_test.m
diff -N csharp_test.m
--- /dev/null	Wed Apr 11 00:52:25 2001
+++ csharp_test.m	Thu May  3 05:07:07 2001
@@ -0,0 +1,29 @@
+% A test of the C# interface.
+
+:- module csharp_test.
+:- interface.
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+main -->
+	csharp_write_string("Hello, world\n").
+
+:- pragma(foreign_decl, "C#", "
+	// some C Sharp declarations
+").
+
+:- pragma(foreign_code, "C#", "
+	// some C Sharp code
+").
+
+:- pred csharp_write_string(string::in, io__state::di, io__state::uo) is det.
+
+:- pragma(foreign_code, "C#",
+	csharp_write_string(Message::in, _IO0::di, _IO::uo),
+	[will_not_call_mercury],
+"
+	// a C sharp procedure
+").

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  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