Navigation
  • Start
  • Use
  • Contribute

CSharp Compiler

From Mono

Table of contents
1 Introduction
2 Specification
3 Working with MCS

3.1 Obtaining MCS
3.2 Running MCS
3.3 Reporting Bugs in MCS

1 Implementation details

1.1 CIL Optimizations.

1.1.1 Open bugs

2 Slides
3 History
4 Questions and Answers

4.1 What is the difference between mcs and gmcs
4.2 Why are there two compilers mcs and gmcs?

Introduction

The Mono C# compiler is considered feature complete for C# 1.0 and almost complete for 2.0 (ECMA). The 1.0 compiler has received more testing and is considered more mature than our 2.0 compiler, bug reports can be filed on our bug tracking system.

Mono's C# compiler is called MCS and it is able to compile itself and many more C# programs (there is a test suite included that you can use). It is routinely used to compile Mono, roughly four million lines of C# code and a few other projects.

The compiler is also fairly fast. On a IBM ThinkPad t40 it compiles 18,000 lines of C# code per second.

The compiler now defaults to the 2.x language specification, the following features of C# 2.0 are supported at this point:

  • anonymous methods (msdn2.microsoft.com/library/0yw3tz5k.aspx),
  • iterators (msdn2.microsoft.com/library/dscyy5s0.aspx),
  • partial classes (msdn2.microsoft.com/library/wa80x488.aspx),
  • static classes (msdn2.microsoft.com/library/79b3xss3.aspx),
  • covariance and contravariance (msdn2.microsoft.com/library/ms173174.aspx),
  • property accessor accessibility (msdn2.microsoft.com/library/75e8y5dd.aspx),
  • fixed buffers (msdn2.microsoft.com/library/zycewsya.aspx),
  • external assembly alias (msdn2.microsoft.com/library/e59b22c5(en-us,vs.80).aspx),
  • namespace alias qualifier (msdn2.microsoft.com/library/htccxtad(en-us,vs.80).aspx) and
  • inline warning control (msdn2.microsoft.com/library/441722ys.aspx).

Generics (msdn2.microsoft.com/library/zycewsya.aspx), Nullable Types (msdn2.microsoft.com/library/zycewsya.aspx) and friend assemblies (msdn2.microsoft.com/library/0tke9fxk(en-us,vs.80).aspx) are supported on our 'gmcs' compiler (also included).

The mcs compiler produces assemblies that reference the 1.x class libraries, while the gmcs compiler generates assemblies that reference the 2.x assemblies.

Specification

The C# 2.0 specification is availble on edition of the ECMA 334 standard (www.ecma-international.org/publications/standards/Ecma-334.htm|third).

An on-line and hyperlinked version of the C# 1.0 specification is available from Jon Jagger's (www.jaggersoft.com) site here (www.jaggersoft.com/csharp_standard/).

The specification shipped with Monodoc, and available on our web downloads is based on Jon's original XML documentation that he extracted from the ECMA 334 specification.

Working with MCS

Obtaining MCS

The Mono C# compiler is part of the `mcs' module in the Mono SVN you can get it from our Anonymous SVN server, or you can get nightly download page.

You can also browse or download a snapshot of the compiler alone:

  • mcs, C# 1.5: browse the sources (svn.myrealbox.com/viewcvs/trunk/mcs/mcs/) or just get a tarball (svn.myrealbox.com/viewcvs/trunk/mcs/mcs.tar.gz?view=tar).
  • gmcs, C# 2.0: browse the sources (svn.myrealbox.com/viewcvs/trunk/mcs/gmcs/) or just get a tarball (svn.myrealbox.com/viewcvs/trunk/gmcs/gmcs.tar.gz?view=tar).

Running MCS

MCS is written in C# and uses heavily the .NET APIs (in particular it uses Reflection and Reflection.Emit). MCS runs on Linux with the Mono runtime and on Windows with both the .NET runtime and the Mono runtime.

Reporting Bugs in MCS

When you report a bug, try to provide a small test case that would show the error so we can include this as part of the Mono C# regression test suite. If the bug is an error or a warning that we do not flag, write a sample program called `csXXXX.cs' where XXXX is the code number that is used by the Microsoft C# compiler that illustrates the problem. That way we can also do regression tests on the invalid input.

Implementation details

The compiler is documented in the file mcs/docs/compiler (svn.myrealbox.com/viewcvs/trunk/mcs/docs/compiler?rev=35465&view=auto)

CIL Optimizations.

The compiler performs a number of simple optimizations on its input: constant folding (this is required by the C# language spec) and can perform dead code elimination.

Other more interesting optimizations like hoisting are not possible at this point since the compiler output at this point does not generate an intermediate representation that is suitable to perform basic block computation.

Adding an intermediate layer to enable the basic block computation to the compiler should be a simple task, but we are considering having a generic CIL optimizer. Since all the information that is required to perform basic block-based optimizations is available at the CIL level, we might just skip this step altogether and have just a generic IL optimizer that would perform hoisting on arbitrary CIL programs, not only those produced by MCS.

If this tool is further expanded to perform constant folding (not needed for our C# compiler, as it is already in there) and dead code elimination, other compiler authors might be able to use this generic CIL optimizer in their projects reducing their time to develop a production compiler.

Open bugs

See the bugs page for more information. A test suite is maintained to track the progress of the compiler and various programs are routinely compiled and ran.

Slides

Slides for the Mono C# Compiler presentation at .NET ONE are available here (primates.ximian.com/~miguel/slides-europe-nov-2002/Mono_C_Sharp_Overview_1007.sxi) in StarOffice format.

History

MCS was able to parse itself on April 2001, MCS compiled itself for the first time on December 28 2001. MCS became self hosting on January 3rd, 2002.

The Mono Runtime and the Mono execution engine were able to make our compiler self hosting on March 12, 2002.

On July 2003 work started on the generics support of mcs. Since the core of the compiler was used in production and in the development of Mono itself, a fork of the compiler was created. This fork is `gmcs'. The `gmcs' fork merges all of the changes from mcs on a regular basis and will eventually become the default compiler.

Support for the third edition of the C# standard became available on the Mono 1.1.8 release in the summer of 2005.

Questions and Answers

What is the difference between mcs and gmcs

gmcs is just a branch of mcs that contains support for generics and nullable types (and hence requires the 2.x libraries). Other than that they are identical.

Why are there two compilers mcs and gmcs?

mcs is our production compiler. When we started work on generics we were very close to the release of Mono 1.0. Support for generics affect many areas in the compiler, and we did not want to introduce instabilities so a branch of the compiler was created.

Mono 1.2 will ship with 'mcs' as its default compiler, but we will make 'gmcs' the default compiler shortly after and 'mcs' will be retired.

The improvements in general happen in mcs, and then they are ported to gmcs on a regular basis.

Retrieved from "www.mono-project.com/CSharp_Compiler"
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.