web page updates
[mono.git] / web / c-sharp
1 * MCS: The Ximian C# compiler
2
3         MCS is currently able to compile itself and many more C#
4         programs (there is a test suite included that you can use).
5         It is routinely used to compile Mono, roughly half a million
6         lines of C# code.
7
8         We are in feature completion mode right now.  There are still
9         a couple of areas that are not covered by the Mono compiler, but
10         they are very very few at this point (security attributes),
11         you can also browse the MCS <a href="http://bugzilla.ximian.com/buglist.cgi?product=Mono%2FMCS&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&email1=&emailtype1=substring&emailassigned_to1=1&email2=&emailtype2=substring&emailreporter2=1&changedin=&chfieldfrom=&chfieldto=Now&chfieldvalue=&short_desc=&short_desc_type=substring&long_desc=&long_desc_type=substring&bug_file_loc=&bug_file_loc_type=substring&keywords=&keywords_type=anywords&op_sys_details=&op_sys_details_type=substring&version_details=&version_details_type=substring&cmdtype=doit&newqueryname=&order=Reuse+same+sort+as+last+time&form_name=query">bugs</a> from Bugzilla.
12
13         A test suite is maintained to track the progress of
14         the compiler and various programs are routinely compiled and
15         ran.
16
17 ** Slides
18
19         Slides for the Mono C# Compiler presentation at .NET ONE are
20         available <a
21         href=http://primates.ximian.com/~miguel/slides-europe-nov-2002/Mono_C_Sharp_Overview_1007.sxi">here</a>
22         in StarOffice format.
23
24 ** Obtaining MCS
25
26         The Mono C# compiler is part of the `mcs' module in the Mono CVS
27         you can get it from our <a href="anoncvs.html">Anonymous CVS</a> server,
28         or you can get nightly <a href="download.html">download page</a>.
29
30 ** Running MCS
31
32         MCS is written in C# and uses heavily the .NET APIs.  MCS runs
33         on Linux with the Mono runtime and on Windows with both the
34         .NET runtime and the Mono runtime.
35
36 ** Reporting Bugs in MCS
37
38         When you report a bug, try to provide a small test case that would
39         show the error so we can include this as part of the Mono C# regression
40         test suite.
41
42         If the bug is an error or a warning that we do not flag, write
43         a sample program called `csXXXX.cs' where XXXX is the code number
44         that is used by the Microsoft C# compiler that illustrates the 
45         problem.  That way we can also do regression tests on the invalid
46         input.  
47
48 ** Phases of the compiler
49
50         The compiler has a number of phases:
51
52         <ul>
53                 * Lexical analyzer: hand-coded lexical analyzer that
54                   provides tokens to the parser.
55
56                 * The Parser: the parser is implemented using Jay (A
57                   Berkeley Yacc port to Java, that I ported to C#).
58                   The parser does minimal work and syntax checking,
59                   and only constructs a parsed tree.
60
61                   Each language element gets its own class.  The code
62                   convention is to use an uppercase name for the
63                   language element.  So a C# class and its associated
64                   information is kept in a "Class" class, a "struct"
65                   in a "Struct" class and so on.  Statements derive
66                   from the "Statement" class, and Expressions from the
67                   Expr class.
68
69                 * Parent class resolution: before the actual code
70                   generation, we need to resolve the parents and
71                   interfaces for interface, classe and struct
72                   definitions.
73
74                 * Semantic analysis: since C# can not resolve in a
75                   top-down pass what identifiers actually mean, we
76                   have to postpone this decision until the above steps
77                   are finished.
78
79                 * Code generation: The code generation is done through
80                   the System.Reflection.Emit API.
81         </ul>
82
83 ** CIL Optimizations.
84
85         The compiler performs a number of simple optimizations on its input:
86         constant folding (this is required by the C# language spec) and 
87         can perform dead code elimination.
88
89         Other more interesting optimizations like hoisting are not possible
90         at this point since the compiler output at this point does not
91         generate an intermediate representation that is suitable to
92         perform basic block computation.  
93
94         Adding an intermediate layer to enable the basic block
95         computation to the compiler should be a simple task, but we
96         are considering having a generic CIL optimizer.  Since all the
97         information that is required to perform basic block-based
98         optimizations is available at the CIL level, we might just skip
99         this step altogether and have just a generic IL optimizer that
100         would perform hoisting on arbitrary CIL programs, not only
101         those produced by MCS.  
102
103         If this tool is further expanded to perform constant folding
104         (not needed for our C# compiler, as it is already in there)
105         and dead code elimination, other compiler authors might be
106         able to use this generic CIL optimizer in their projects
107         reducing their time to develop a production compiler. 
108
109 ** History
110
111         MCS was able to parse itself on April 2001, MCS compiled itself
112         for the first time on December 28 2001.  MCS became self hosting
113         on January 3rd, 2002. 
114
115         The Mono Runtime and the Mono execution engine were able to make
116         our compiler self hosting on March 12, 2002.
117
118 ** Questions and Answers
119
120 Q: Why not write a C# front-end for GCC?
121
122 A: I wanted to learn about C#, and this was an exercise in this
123    task.  The resulting compiler is highly object-oriented, which has
124    lead to a very nice, easy to follow and simple implementation of
125    the compiler.
126
127    I found that the design of this compiler is very similar to
128    Guavac's implementation.
129
130    Targeting the CIL/MSIL byte codes would require to re-architecting
131    GCC, as GCC is mostly designed to be used for register machines.
132    
133    The GCC Java engine that generates Java byte codes cheats: it does
134    not use the GCC backend; it has a special backend just for Java, so
135    you can not really generate Java bytecodes from the other languages
136    supported by GCC. 
137
138 Q: If your C# compiler is written in C#, how do you plan on getting
139    this working on a non-Microsoft environment.
140
141    We will do this through an implementation of the CLI Virtual
142    Execution System for Unix (our JIT engine). 
143
144    Our JIT engine is working for the purposes of using the compiler.
145    The supporting class libraries are being worked on to fully support
146    the compiler.
147
148 Q: Do you use Bison?
149
150 A: No, currently I am using Jay which is a port of Berkeley Yacc to
151    Java that I later ported to C#.  This means that error recovery is
152    not as nice as I would like to, and for some reason error
153    productions are not being caught.  
154
155    In the future I want to port one of the Bison/Java ports to C# for
156    the parser.
157
158 Q: Should someone work on a GCC front-end to C#?
159
160 A: I would love if someone does, and we would love to help anyone that
161    takes on that task, but we do not have the time or expertise to
162    build a C# compiler with the GCC engine.  I find it a lot more fun
163    personally to work on C# on a C# compiler, which has an intrinsic
164    beauty.
165
166    We can provide help and assistance to anyone who would like to work
167    on this task.
168
169 Q: Should someone make a GCC backend that will generate CIL images?
170
171 A: I would love to see a backend to GCC that generates CIL images.  It
172    would provide a ton of free compilers that would generate CIL
173    code.  This is something that people would want to look into
174    anyways for Windows interoperation in the future.
175
176    Again, we would love to provide help and assistance to anyone
177    interested in working in such a project.
178
179 Q: What about making a front-end to GCC that takes CIL images and
180    generates native code?
181
182 A: I would love to see this, specially since GCC supports this same
183    feature for Java Byte Codes.  You could use the metadata library
184    from Mono to read the byte codes (ie, this would be your
185    "front-end") and generate the trees that get passed to the
186    optimizer.
187
188    Ideally our implementation of the CLI will be available as a shared
189    library that could be linked with your application as its runtime
190    support. 
191
192    Again, we would love to provide help and assistance to anyone
193    interested in working in such a project.
194    
195 Q: But would this work around the GPL in the GCC compiler and allow
196    people to work on non-free front-ends?
197
198 A: People can already do this by targeting the JVM byte codes (there
199    are about 130 compilers for various languages that target the JVM).
200
201 Q: Why are you writing a JIT engine instead of a front-end to GCC?
202
203 A: The JIT engine and runtime engine will be able to execute CIL
204    executables generated on Windows.
205
206 You might also want to look at the <a href="faq.html#gcc">GCC</a>
207 section on the main FAQ