UPdate web page
[mono.git] / web / c-sharp
1 * MCS: The Ximian C# compiler
2
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
5         small C# programs.  
6
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.
12
13         Work is progressing quickly on various fronts in the C#
14         compiler.
15
16         A test suite is being built currently to track the progress of
17         the compiler.
18
19 ** Phases of the compiler
20
21         The compiler has a number of phases:
22
23         <ul>
24                 * Lexical analyzer: hand-coded lexical analyzer that
25                   provides tokens to the parser.
26
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.
31
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
38                   Expr class.
39
40                 * Parent class resolution: before the actual code
41                   generation, we need to resolve the parents and
42                   interfaces for interface, classe and struct
43                   definitions.
44
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
48                   are finished.
49
50                 * Code generation: The compiler recently started generating IL 
51                   executables that contain interfaces.  Work is
52                   progressing in other areas.
53
54                   The code generation is done through the System.Reflection.Emit API. 
55         </ul>
56
57 <a name="tasks">
58 ** Current pending tasks
59
60         Simple tasks:
61
62         <ul>
63                 * PInvoke declarations are not supported.
64
65                 * Pre-processing is not supported.
66
67                 * Compiler does not pass around line/col information from tokenizer for error reporting.
68
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. 
73                   
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>
77         </ul>
78
79         Interesting and Fun hacks to the compiler:
80
81         <ul>
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>
86
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>
89
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).
93
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.
98
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>). 
101
102         </ul>
103
104 ** Questions and Answers
105
106 Q: Why not write a C# front-end for GCC?
107
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
111    the compiler.
112
113    I found that the design of this compiler is very similar to
114    Guavac's implementation.
115
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.
118    
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
122    supported by GCC. 
123
124 Q: If your C# compiler is written in C#, how do you plan on getting
125    this working on a non-Microsoft environment.
126
127    We will do this through an implementation of the CLI Virtual
128    Execution System for Unix (our JIT engine). 
129
130 Q: Do you use Bison?
131
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.  
136
137    In the future I want to port one of the Bison/Java ports to C# for
138    the parser.
139
140 Q: Should someone work on a GCC front-end to C#?
141
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
146    beauty.
147
148    We can provide help and assistance to anyone who would like to work
149    on this task.
150
151 Q: Should someone make a GCC backend that will generate CIL images?
152
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.
157
158    Again, we would love to provide help and assistance to anyone
159    interested in working in such a project.
160
161 Q: What about making a front-end to GCC that takes CIL images and
162    generates native code?
163
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
168    optimizer.
169
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
172    support. 
173
174    Again, we would love to provide help and assistance to anyone
175    interested in working in such a project.
176    
177 Q: But would this work around the GPL in the GCC compiler and allow
178    people to work on non-free front-ends?
179
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).
182
183 Q: Why are you writing a JIT engine instead of a front-end to GCC?
184
185 A: The JIT engine and runtime engine will be able to execute CIL
186    executables generated on Windows.
187
188 You might also want to look at the <a href="faq.html#gcc">GCC</a>
189 section on the main FAQ