1 * MCS: The Ximian C# compiler
3 MCS began as an experiment to learn the features of C# by
4 writing a large C# program. MCS is currently able to compile
7 All type, field, method, delegates definitions are now emitted
8 and the body of constructors and methods is being generated
9 for a subset of the language. Although MCS can parse itself,
10 it cant not yet compile itself. Most statements are generated
11 correctly and about 80% of the C# expressions are supported.
13 Work is progressing quickly on various fronts in the C#
16 A test suite is being built currently to track the progress of
19 ** Phases of the compiler
21 The compiler has a number of phases:
24 * Lexical analyzer: hand-coded lexical analyzer that
25 provides tokens to the parser.
27 * The Parser: the parser is implemented using Jay (A
28 Berkeley Yacc port to Java, that I ported to C#).
29 The parser does minimal work and syntax checking,
30 and only constructs a parsed tree.
32 Each language element gets its own class. The code
33 convention is to use an uppercase name for the
34 language element. So a C# class and its associated
35 information is kept in a "Class" class, a "struct"
36 in a "Struct" class and so on. Statements derive
37 from the "Statement" class, and Expressions from the
40 * Parent class resolution: before the actual code
41 generation, we need to resolve the parents and
42 interfaces for interface, classe and struct
45 * Semantic analysis: since C# can not resolve in a
46 top-down pass what identifiers actually mean, we
47 have to postpone this decision until the above steps
50 * Code generation: The compiler recently started generating IL
51 executables that contain interfaces. Work is
52 progressing in other areas.
54 The code generation is done through the System.Reflection.Emit API.
58 ** Current pending tasks
63 * PInvoke declarations are not supported.
65 * Extern declarations are missing.
67 * Pre-processing is not supported.
69 * Jay does not work correctly with `error'
70 productions, making parser errors hard to point. It
71 would be best to port the Bison-To-Java compiler to
72 become Bison-to-C# compiler.
74 Nick Drochak has started a project on SourceForge for this.
75 You can find the project at: <a href="http://sourceforge.net/projects/jb2csharp/">
76 http://sourceforge.net/projects/jb2csharp/</a>
82 * Implement constant expression evaluator.
84 * Implement constant declarations.
86 * Implement enumerations.
89 Interesting and Fun hacks to the compiler:
92 * Finishing the JB port from Java to C#. If you are
93 interested in working on this, please contact the project admin on SourceForge:
94 <a href="http://sourceforge.net/projects/jb2csharp/">
95 http://sourceforge.net/projects/jb2csharp/</a>
97 More on JB at: <a href="http://www.cs.colorado.edu/~dennis/software/jb.html">
98 http://www.cs.colorado.edu/~dennis/software/jb.html</a>
100 JB will allow us to move from the Berkeley Yacc
101 based Jay to a Bison-based compiler (better error
102 reporting and recovery).
104 * Semantic Analysis: Return path coverage and
105 initialization before use coverage are two great
106 features of C# that help reduce the number of bugs
107 in applications. It is one interesting hack.
109 * Enum resolutions: it is another fun hack, as enums can be defined
110 in terms of themselves (<tt>enum X { a = b + 1, b = 5 }</tt>).
114 ** Questions and Answers
116 Q: Why not write a C# front-end for GCC?
118 A: I wanted to learn about C#, and this was an exercise in this
119 task. The resulting compiler is highly object-oriented, which has
120 lead to a very nice, easy to follow and simple implementation of
123 I found that the design of this compiler is very similar to
124 Guavac's implementation.
126 Targeting the CIL/MSIL byte codes would require to re-architecting
127 GCC, as GCC is mostly designed to be used for register machines.
129 The GCC Java engine that generates Java byte codes cheats: it does
130 not use the GCC backend; it has a special backend just for Java, so
131 you can not really generate Java bytecodes from the other languages
134 Q: If your C# compiler is written in C#, how do you plan on getting
135 this working on a non-Microsoft environment.
137 We will do this through an implementation of the CLI Virtual
138 Execution System for Unix (our JIT engine).
142 A: No, currently I am using Jay which is a port of Berkeley Yacc to
143 Java that I later ported to C#. This means that error recovery is
144 not as nice as I would like to, and for some reason error
145 productions are not being caught.
147 In the future I want to port one of the Bison/Java ports to C# for
150 Q: Should someone work on a GCC front-end to C#?
152 A: I would love if someone does, and we would love to help anyone that
153 takes on that task, but we do not have the time or expertise to
154 build a C# compiler with the GCC engine. I find it a lot more fun
155 personally to work on C# on a C# compiler, which has an intrinsic
158 We can provide help and assistance to anyone who would like to work
161 Q: Should someone make a GCC backend that will generate CIL images?
163 A: I would love to see a backend to GCC that generates CIL images. It
164 would provide a ton of free compilers that would generate CIL
165 code. This is something that people would want to look into
166 anyways for Windows interoperation in the future.
168 Again, we would love to provide help and assistance to anyone
169 interested in working in such a project.
171 Q: What about making a front-end to GCC that takes CIL images and
172 generates native code?
174 A: I would love to see this, specially since GCC supports this same
175 feature for Java Byte Codes. You could use the metadata library
176 from Mono to read the byte codes (ie, this would be your
177 "front-end") and generate the trees that get passed to the
180 Ideally our implementation of the CLI will be available as a shared
181 library that could be linked with your application as its runtime
184 Again, we would love to provide help and assistance to anyone
185 interested in working in such a project.
187 Q: But would this work around the GPL in the GCC compiler and allow
188 people to work on non-free front-ends?
190 A: People can already do this by targeting the JVM byte codes (there
191 are about 130 compilers for various languages that target the JVM).
193 Q: Why are you writing a JIT engine instead of a front-end to GCC?
195 A: The JIT engine and runtime engine will be able to execute CIL
196 executables generated on Windows.
198 You might also want to look at the <a href="faq.html#gcc">GCC</a>
199 section on the main FAQ