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