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