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 60% 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 * Pre-processing is not supported.
67 * Compiler does not pass around line/col information from tokenizer for error reporting.
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>
79 Interesting and Fun hacks to the compiler:
82 * Finishing the JB port from Java to C#. If you are
83 interested in working on this, please contact the project admin on SourceForge:
84 <a href="http://sourceforge.net/projects/jb2csharp/">
85 http://sourceforge.net/projects/jb2csharp/</a>
87 More on JB at: <a href="http://www.cs.colorado.edu/~dennis/software/jb.html">
88 http://www.cs.colorado.edu/~dennis/software/jb.html</a>
90 JB will allow us to move from the Berkeley Yacc
91 based Jay to a Bison-based compiler (better error
92 reporting and recovery).
94 * Semantic Analysis: Return path coverage and
95 initialization before use coverage are two great
96 features of C# that help reduce the number of bugs
97 in applications. It is one interesting hack.
99 * Enum resolutions: it is another fun hack, as enums can be defined
100 in terms of themselves (<tt>enum X { a = b + 1, b = 5 }</tt>).
104 ** Questions and Answers
106 Q: Why not write a C# front-end for GCC?
108 A: I wanted to learn about C#, and this was an exercise in this
109 task. The resulting compiler is highly object-oriented, which has
110 lead to a very nice, easy to follow and simple implementation of
113 I found that the design of this compiler is very similar to
114 Guavac's implementation.
116 Targeting the CIL/MSIL byte codes would require to re-architecting
117 GCC, as GCC is mostly designed to be used for register machines.
119 The GCC Java engine that generates Java byte codes cheats: it does
120 not use the GCC backend; it has a special backend just for Java, so
121 you can not really generate Java bytecodes from the other languages
124 Q: If your C# compiler is written in C#, how do you plan on getting
125 this working on a non-Microsoft environment.
127 We will do this through an implementation of the CLI Virtual
128 Execution System for Unix (our JIT engine).
132 A: No, currently I am using Jay which is a port of Berkeley Yacc to
133 Java that I later ported to C#. This means that error recovery is
134 not as nice as I would like to, and for some reason error
135 productions are not being caught.
137 In the future I want to port one of the Bison/Java ports to C# for
140 Q: Should someone work on a GCC front-end to C#?
142 A: I would love if someone does, and we would love to help anyone that
143 takes on that task, but we do not have the time or expertise to
144 build a C# compiler with the GCC engine. I find it a lot more fun
145 personally to work on C# on a C# compiler, which has an intrinsic
148 We can provide help and assistance to anyone who would like to work
151 Q: Should someone make a GCC backend that will generate CIL images?
153 A: I would love to see a backend to GCC that generates CIL images. It
154 would provide a ton of free compilers that would generate CIL
155 code. This is something that people would want to look into
156 anyways for Windows interoperation in the future.
158 Again, we would love to provide help and assistance to anyone
159 interested in working in such a project.
161 Q: What about making a front-end to GCC that takes CIL images and
162 generates native code?
164 A: I would love to see this, specially since GCC supports this same
165 feature for Java Byte Codes. You could use the metadata library
166 from Mono to read the byte codes (ie, this would be your
167 "front-end") and generate the trees that get passed to the
170 Ideally our implementation of the CLI will be available as a shared
171 library that could be linked with your application as its runtime
174 Again, we would love to provide help and assistance to anyone
175 interested in working in such a project.
177 Q: But would this work around the GPL in the GCC compiler and allow
178 people to work on non-free front-ends?
180 A: People can already do this by targeting the JVM byte codes (there
181 are about 130 compilers for various languages that target the JVM).
183 Q: Why are you writing a JIT engine instead of a front-end to GCC?
185 A: The JIT engine and runtime engine will be able to execute CIL
186 executables generated on Windows.
188 You might also want to look at the <a href="faq.html#gcc">GCC</a>
189 section on the main FAQ