1 * MCS: The Ximian C# compiler
3 MCS is currently able to compile small 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 Although MCS has been able to parse itself since April,
11 it can not yet compile itself. We are working hard towards
12 mkaing the compiler self hosting in Linux.
14 A test suite is being built currently to track the progress of
15 the compiler and various programs are routinely compiled and
18 ** Phases of the compiler
20 The compiler has a number of phases:
23 * Lexical analyzer: hand-coded lexical analyzer that
24 provides tokens to the parser.
26 * The Parser: the parser is implemented using Jay (A
27 Berkeley Yacc port to Java, that I ported to C#).
28 The parser does minimal work and syntax checking,
29 and only constructs a parsed tree.
31 Each language element gets its own class. The code
32 convention is to use an uppercase name for the
33 language element. So a C# class and its associated
34 information is kept in a "Class" class, a "struct"
35 in a "Struct" class and so on. Statements derive
36 from the "Statement" class, and Expressions from the
39 * Parent class resolution: before the actual code
40 generation, we need to resolve the parents and
41 interfaces for interface, classe and struct
44 * Semantic analysis: since C# can not resolve in a
45 top-down pass what identifiers actually mean, we
46 have to postpone this decision until the above steps
49 * Code generation: The code generation is done through
50 the System.Reflection.Emit API.
55 ** Current pending tasks
60 * Extern declarations are missing.
66 * Finish constant folding.
69 Interesting and Fun hacks to the compiler:
73 * Jay does not work correctly with `error'
74 productions, making parser errors hard to point. It
75 would be best to port the Bison-To-Java compiler to
76 become Bison-to-C# compiler.
78 Nick Drochak has started a project on SourceForge for this.
79 You can find the project at: <a href="http://sourceforge.net/projects/jb2csharp/">
80 http://sourceforge.net/projects/jb2csharp/</a>
82 * Semantic Analysis: Return path coverage and
83 initialization before use coverage are two great
84 features of C# that help reduce the number of bugs
85 in applications. It is one interesting hack.
89 ** Questions and Answers
91 Q: Why not write a C# front-end for GCC?
93 A: I wanted to learn about C#, and this was an exercise in this
94 task. The resulting compiler is highly object-oriented, which has
95 lead to a very nice, easy to follow and simple implementation of
98 I found that the design of this compiler is very similar to
99 Guavac's implementation.
101 Targeting the CIL/MSIL byte codes would require to re-architecting
102 GCC, as GCC is mostly designed to be used for register machines.
104 The GCC Java engine that generates Java byte codes cheats: it does
105 not use the GCC backend; it has a special backend just for Java, so
106 you can not really generate Java bytecodes from the other languages
109 Q: If your C# compiler is written in C#, how do you plan on getting
110 this working on a non-Microsoft environment.
112 We will do this through an implementation of the CLI Virtual
113 Execution System for Unix (our JIT engine).
117 A: No, currently I am using Jay which is a port of Berkeley Yacc to
118 Java that I later ported to C#. This means that error recovery is
119 not as nice as I would like to, and for some reason error
120 productions are not being caught.
122 In the future I want to port one of the Bison/Java ports to C# for
125 Q: Should someone work on a GCC front-end to C#?
127 A: I would love if someone does, and we would love to help anyone that
128 takes on that task, but we do not have the time or expertise to
129 build a C# compiler with the GCC engine. I find it a lot more fun
130 personally to work on C# on a C# compiler, which has an intrinsic
133 We can provide help and assistance to anyone who would like to work
136 Q: Should someone make a GCC backend that will generate CIL images?
138 A: I would love to see a backend to GCC that generates CIL images. It
139 would provide a ton of free compilers that would generate CIL
140 code. This is something that people would want to look into
141 anyways for Windows interoperation in the future.
143 Again, we would love to provide help and assistance to anyone
144 interested in working in such a project.
146 Q: What about making a front-end to GCC that takes CIL images and
147 generates native code?
149 A: I would love to see this, specially since GCC supports this same
150 feature for Java Byte Codes. You could use the metadata library
151 from Mono to read the byte codes (ie, this would be your
152 "front-end") and generate the trees that get passed to the
155 Ideally our implementation of the CLI will be available as a shared
156 library that could be linked with your application as its runtime
159 Again, we would love to provide help and assistance to anyone
160 interested in working in such a project.
162 Q: But would this work around the GPL in the GCC compiler and allow
163 people to work on non-free front-ends?
165 A: People can already do this by targeting the JVM byte codes (there
166 are about 130 compilers for various languages that target the JVM).
168 Q: Why are you writing a JIT engine instead of a front-end to GCC?
170 A: The JIT engine and runtime engine will be able to execute CIL
171 executables generated on Windows.
173 You might also want to look at the <a href="faq.html#gcc">GCC</a>
174 section on the main FAQ