2001-11-09 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / doc / 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 80% 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                 * Extern declarations are missing.
66
67                 * Pre-processing is not supported.
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         Larger tasks:
80
81         <ul>
82                 * Implement constant expression evaluator.
83
84                 * Implement constant declarations.
85
86                 * Implement enumerations.
87         </ul>
88
89         Interesting and Fun hacks to the compiler:
90
91         <ul>
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>
96
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>
99
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).
103
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.
108
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>). 
111
112         </ul>
113
114 ** Questions and Answers
115
116 Q: Why not write a C# front-end for GCC?
117
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
121    the compiler.
122
123    I found that the design of this compiler is very similar to
124    Guavac's implementation.
125
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.
128    
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
132    supported by GCC. 
133
134 Q: If your C# compiler is written in C#, how do you plan on getting
135    this working on a non-Microsoft environment.
136
137    We will do this through an implementation of the CLI Virtual
138    Execution System for Unix (our JIT engine). 
139
140 Q: Do you use Bison?
141
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.  
146
147    In the future I want to port one of the Bison/Java ports to C# for
148    the parser.
149
150 Q: Should someone work on a GCC front-end to C#?
151
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
156    beauty.
157
158    We can provide help and assistance to anyone who would like to work
159    on this task.
160
161 Q: Should someone make a GCC backend that will generate CIL images?
162
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.
167
168    Again, we would love to provide help and assistance to anyone
169    interested in working in such a project.
170
171 Q: What about making a front-end to GCC that takes CIL images and
172    generates native code?
173
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
178    optimizer.
179
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
182    support. 
183
184    Again, we would love to provide help and assistance to anyone
185    interested in working in such a project.
186    
187 Q: But would this work around the GPL in the GCC compiler and allow
188    people to work on non-free front-ends?
189
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).
192
193 Q: Why are you writing a JIT engine instead of a front-end to GCC?
194
195 A: The JIT engine and runtime engine will be able to execute CIL
196    executables generated on Windows.
197
198 You might also want to look at the <a href="faq.html#gcc">GCC</a>
199 section on the main FAQ