-----------------------------------------------------------------------------

INTRODUCTION

This release of Mercury contains a port to the ECMA Common Language
Infrastructure (CLI), i.e. Microsoft .NET or Mono.  The Mercury
compiler will generate C# source code that can be compiled into
bytecode suitable for running on the .NET or Mono runtime systems.

The port is mostly complete, but some parts of the Mercury standard
library are not yet implemented (for a full list see the FAQ below).

The port is currently targeted at C# 2.0 or higher.

NOTE: a previous backend also targetted the .NET runtime, by generating IL
(Intermediate Language), rather than going via C#.  That backend is
out-of-date and may be removed in the future.

PREREQUISITES

In order to try this system you will need

        - Either Microsoft.NET or Mono 2.8 or above.


VERSIONS OF THE .NET FRAMEWORK ON MONO

On Mono, the C# compiler has a number of aliases: gmcs, mcs, dmcs.  The alias
with which the C# compiler is invoked controls which version of the .NET
frameworks is used by default.  For Mono 2.8, the defaults are:

    gmcs - .NET version 2.0
    dmcs - .NET version 4.0
    mcs  - .NET version 2.0

You can specify what C# compiler Mercury should use at configuration time using
configure's `--with-csharp-compiler' option.  The output from the configure
script will report which version of the .NET framework is going be used, for
example:

    checking version of .NET libraries... 4.0.0.0

-----------------------------------------------------------------------------

THE C# GRADE

The Mercury compiler currently supports the grade `csharp'.
The csharp grade is enabled by using any of the options
`--grade csharp', `--target csharp', or just `--csharp'.

To run a Mercury program using the csharp grade, you need to build the Mercury
library and runtime in the csharp grade, using the Mercury source distribution.

You can now build programs such as hello.m or calculator.m in the samples
directory.

	cd samples
	mmc --make --csharp hello

Note that when building programs using the csharp grade you *must* use
mmc --make.

-----------------------------------------------------------------------------

RUNNING C# GRADE PROGRAMS ON UNIX WITH MONO

For the example in the previous section on a Unix (or more generally,
non-Windows) system using Mono, the Mercury compiler will generate a process
assembly, e.g. hello.exe, and a wrapper shell script named hello.

The wrapper shell script will set the MONO_PATH environment variable
to point to the location of the Mercury standard library assemblies.
It will then invoke CLI execution environment on the above process assembly,
i.e. you can run the program by simply running the wrapper shell script,
for example

      ./hello

-----------------------------------------------------------------------------

RUNNING C# GRADE PROGRAMS ON WINDOWS WITH .NET

On Windows, the Mercury compiler will only generate a process assembly, e.g.
hello.exe.  (On Windows there is no need to generate a wrapper shell script.)

With .NET, the library assemblies (.dlls) for the Mercury standard
libraries must either (1) reside in (or under) the same directory as the process
assembly (.exe) or (2) be entered into the global assembly cache (GAC).
If neither of these things is done then execution will abort with a message that
begins:

     Unhandled Exception: System.IO.FileNotFoundException: Could not load file
     or assembly 'mer_std',  Version=...

For (1), you will need to copy the library assemblies from the Mercury library
installation into the same directory as the process assembly.
The files for the library assemblies are located in

     <prefix>\lib\mercury\lib\csharp

where <prefix> is the location of the Mercury installation.
Copy all of the dll files in the above directory into that of the process
assembly.

To enter assemblies into the GAC, run the following command for each
assembly.

            gacutil /i mer_std.dll

Assemblies can be removed from the GAC by doing, for example

    gacutil /u mer_std.dll

-----------------------------------------------------------------------------

USING C#

The Mercury standard library has not been fully ported to C# yet.
The use of unimplemented procedures will result in a run-time error,
with a message such as "Sorry, not implemented: foreign code for this
function", and a stack trace.

If you find missing functionality, you can interface to C# using Mercury's
foreign language interface.

For example:

:- pred to_string(T::in, string::out) is det.
:- pragma foreign_proc("C#", to_string(T::in, Str::out), [],
"
        Str = T.ToString();
").

The implementation will include this C# code in the module's .cs file, and
you can then call the predicate to_string exactly the same as if it were
implemented using pure Mercury code.

For more information about the foreign language interface, see the Mercury
Language Reference Manual, which you can find at:

     <http://www.mercurylang.org/information/documentation.html>

-----------------------------------------------------------------------------

BUILDING THE MERCURY COMPILER IN THE C# GRADE

Building the Mercury compiler and other related tools in the C# grade
is NOT generally supported and should be considered experimental.
In particular, a Mercury compiler built in the C# grade may be slower than
normal and some features may not be available.

However, if you want to give it a try, the required steps are:

(1) Ensure that you have an existing working Mercury compiler in your PATH
and a clean version of the Mercury source tree.

(2) Run aclocal -I m4; autoconf; ./configure as normal.

(3) Add the line:

      GRADE=csharp

    to a file named Mmake.params at the top-level of the source tree.

(4) Begin the build process using the following command:

    $ mmake --use-mmc-make GRADE=csharp

The C# version of the compiler MUST be built using mmake's --use-mmc-make
option; the build will not work otherwise.  Setting the variable GRADE in the
invocation of mmake is currently necessary in order to avoid some variable
definition ordering problems in Mmake.workspace.

-----------------------------------------------------------------------------

FREQUENTLY ASKED QUESTIONS (FAQS)

Q. What are the advantages of using the C# back-end?

A. The main advantage is easy access to the wide range of libraries for the
   .NET platform, and the portability you get from using CIL bytecode.


Q. What features are not yet implemented for the C# back-end?

A. The following implementation features are not supported:

	Mercury-level debugging (but see next question)
	Mercury-level profiling
	trailing
	tabling

   In addition, the following individual procedures are incompletely
   implemented:

	io.read_binary/{3,4}:
	io.write_binary/{3,4}:
		io.read_binary is broken.

	benchmarking.report_stats/0:
	benchmarking.report_full_memory_stats/0:
		Memory usage statistics are not yet available, and cpu time
		is not the same as in the C backends, as per time.m.

	store.arg_ref/5:
	store.new_arg_ref/5:
		Due to the absence of RTTI, dynamic type checking is missing
		for these predicates.  They should be used with care.

   This list is not complete.


Q. How do I debug Mercury programs on .NET?

A. The only Mercury-level debugger available for C# grades is the
   source-to-source debugger; see README.ssdebug.


-----------------------------------------------------------------------------
