1 * MCS: The Ximian C# compiler
3 MCS is currently able to compile many C# programs (there is
4 a test suite included that you can use).
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.
10 The compiler can compile itself, but the resulting image contains
11 a few errors that we are quickly fixing.
13 MCS was able to parse itself on April 2001, MCS compiled itself
14 for the first time on December 28 2001. We hope to have the compiler
15 self hosting on Linux by the end of January.
17 A test suite is being built currently to track the progress of
18 the compiler and various programs are routinely compiled and
21 ** Phases of the compiler
23 The compiler has a number of phases:
26 * Lexical analyzer: hand-coded lexical analyzer that
27 provides tokens to the parser.
29 * The Parser: the parser is implemented using Jay (A
30 Berkeley Yacc port to Java, that I ported to C#).
31 The parser does minimal work and syntax checking,
32 and only constructs a parsed tree.
34 Each language element gets its own class. The code
35 convention is to use an uppercase name for the
36 language element. So a C# class and its associated
37 information is kept in a "Class" class, a "struct"
38 in a "Struct" class and so on. Statements derive
39 from the "Statement" class, and Expressions from the
42 * Parent class resolution: before the actual code
43 generation, we need to resolve the parents and
44 interfaces for interface, classe and struct
47 * Semantic analysis: since C# can not resolve in a
48 top-down pass what identifiers actually mean, we
49 have to postpone this decision until the above steps
52 * Code generation: The code generation is done through
53 the System.Reflection.Emit API.
58 ** Current pending tasks
63 * Extern declarations are missing.
69 * Finish constant folding, it is complete enough
70 to compile itself, but it is lacking error reporting.
72 * Redo the way we deal with built-in operators.
75 Interesting and Fun hacks to the compiler:
79 * Jay does not work correctly with `error'
80 productions, making parser errors hard to point. It
81 would be best to port the Bison-To-Java compiler to
82 become Bison-to-C# compiler.
84 Nick Drochak has started a project on SourceForge for this.
85 You can find the project at: <a href="http://sourceforge.net/projects/jb2csharp/">
86 http://sourceforge.net/projects/jb2csharp/</a>
88 * Semantic Analysis: Return path coverage and
89 initialization before use coverage are two great
90 features of C# that help reduce the number of bugs
91 in applications. It is one interesting hack.
95 ** Questions and Answers
97 Q: Why not write a C# front-end for GCC?
99 A: I wanted to learn about C#, and this was an exercise in this
100 task. The resulting compiler is highly object-oriented, which has
101 lead to a very nice, easy to follow and simple implementation of
104 I found that the design of this compiler is very similar to
105 Guavac's implementation.
107 Targeting the CIL/MSIL byte codes would require to re-architecting
108 GCC, as GCC is mostly designed to be used for register machines.
110 The GCC Java engine that generates Java byte codes cheats: it does
111 not use the GCC backend; it has a special backend just for Java, so
112 you can not really generate Java bytecodes from the other languages
115 Q: If your C# compiler is written in C#, how do you plan on getting
116 this working on a non-Microsoft environment.
118 We will do this through an implementation of the CLI Virtual
119 Execution System for Unix (our JIT engine).
121 Our JIT engine is working for the purposes of using the compiler.
122 The supporting class libraries are being worked on to fully support
127 A: No, currently I am using Jay which is a port of Berkeley Yacc to
128 Java that I later ported to C#. This means that error recovery is
129 not as nice as I would like to, and for some reason error
130 productions are not being caught.
132 In the future I want to port one of the Bison/Java ports to C# for
135 Q: Should someone work on a GCC front-end to C#?
137 A: I would love if someone does, and we would love to help anyone that
138 takes on that task, but we do not have the time or expertise to
139 build a C# compiler with the GCC engine. I find it a lot more fun
140 personally to work on C# on a C# compiler, which has an intrinsic
143 We can provide help and assistance to anyone who would like to work
146 Q: Should someone make a GCC backend that will generate CIL images?
148 A: I would love to see a backend to GCC that generates CIL images. It
149 would provide a ton of free compilers that would generate CIL
150 code. This is something that people would want to look into
151 anyways for Windows interoperation in the future.
153 Again, we would love to provide help and assistance to anyone
154 interested in working in such a project.
156 Q: What about making a front-end to GCC that takes CIL images and
157 generates native code?
159 A: I would love to see this, specially since GCC supports this same
160 feature for Java Byte Codes. You could use the metadata library
161 from Mono to read the byte codes (ie, this would be your
162 "front-end") and generate the trees that get passed to the
165 Ideally our implementation of the CLI will be available as a shared
166 library that could be linked with your application as its runtime
169 Again, we would love to provide help and assistance to anyone
170 interested in working in such a project.
172 Q: But would this work around the GPL in the GCC compiler and allow
173 people to work on non-free front-ends?
175 A: People can already do this by targeting the JVM byte codes (there
176 are about 130 compilers for various languages that target the JVM).
178 Q: Why are you writing a JIT engine instead of a front-end to GCC?
180 A: The JIT engine and runtime engine will be able to execute CIL
181 executables generated on Windows.
183 You might also want to look at the <a href="faq.html#gcc">GCC</a>
184 section on the main FAQ