d90a01602f7d3761693961ddbfee17913ed1839e
[mono.git] / web / c-sharp
1 * MCS: The Ximian C# compiler
2
3         MCS is currently able to compile many C# programs (there is
4         a test suite included that you can use).  
5
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.
9
10         The compiler can compile itself, but the resulting image contains
11         a few errors that we are quickly fixing.
12
13         MCS was able to parse itself on April 2001, MCS compiled itself
14         for the first time on December 28 2001.  We hope to have the compiler
15         self hosting on Linux by the end of January.
16
17         A test suite is being built currently to track the progress of
18         the compiler and various programs are routinely compiled and
19         ran.
20
21 ** Phases of the compiler
22
23         The compiler has a number of phases:
24
25         <ul>
26                 * Lexical analyzer: hand-coded lexical analyzer that
27                   provides tokens to the parser.
28
29                 * The Parser: the parser is implemented using Jay (A
30                   Berkeley Yacc port to Java, that I ported to C#).
31                   The parser does minimal work and syntax checking,
32                   and only constructs a parsed tree.
33
34                   Each language element gets its own class.  The code
35                   convention is to use an uppercase name for the
36                   language element.  So a C# class and its associated
37                   information is kept in a "Class" class, a "struct"
38                   in a "Struct" class and so on.  Statements derive
39                   from the "Statement" class, and Expressions from the
40                   Expr class.
41
42                 * Parent class resolution: before the actual code
43                   generation, we need to resolve the parents and
44                   interfaces for interface, classe and struct
45                   definitions.
46
47                 * Semantic analysis: since C# can not resolve in a
48                   top-down pass what identifiers actually mean, we
49                   have to postpone this decision until the above steps
50                   are finished.
51
52                 * Code generation: The code generation is done through
53                   the System.Reflection.Emit API.
54
55         </ul>
56
57 <a name="tasks">
58 ** Current pending tasks
59
60         Simple tasks:
61
62         <ul>
63                 * Extern declarations are missing.
64         </ul>
65
66         Larger tasks:
67
68         <ul>
69                 * Finish constant folding, it is complete enough
70                   to compile itself, but it is lacking error reporting.
71
72                 * Redo the way we deal with built-in operators.
73         </ul>
74
75         Interesting and Fun hacks to the compiler:
76
77         <ul>
78
79                 * Jay does not work correctly with `error'
80                   productions, making parser errors hard to point.  It
81                   would be best to port the Bison-To-Java compiler to
82                   become Bison-to-C# compiler. 
83                   
84                   Nick Drochak has started a project on SourceForge for this.
85                   You can find the project at: <a href="http://sourceforge.net/projects/jb2csharp/">
86                   http://sourceforge.net/projects/jb2csharp/</a>
87
88                 * Semantic Analysis: Return path coverage and
89                   initialization before use coverage are two great
90                   features of C# that help reduce the number of bugs
91                   in applications.  It is one interesting hack.
92
93         </ul>
94
95 ** Questions and Answers
96
97 Q: Why not write a C# front-end for GCC?
98
99 A: I wanted to learn about C#, and this was an exercise in this
100    task.  The resulting compiler is highly object-oriented, which has
101    lead to a very nice, easy to follow and simple implementation of
102    the compiler.
103
104    I found that the design of this compiler is very similar to
105    Guavac's implementation.
106
107    Targeting the CIL/MSIL byte codes would require to re-architecting
108    GCC, as GCC is mostly designed to be used for register machines.
109    
110    The GCC Java engine that generates Java byte codes cheats: it does
111    not use the GCC backend; it has a special backend just for Java, so
112    you can not really generate Java bytecodes from the other languages
113    supported by GCC. 
114
115 Q: If your C# compiler is written in C#, how do you plan on getting
116    this working on a non-Microsoft environment.
117
118    We will do this through an implementation of the CLI Virtual
119    Execution System for Unix (our JIT engine). 
120
121    Our JIT engine is working for the purposes of using the compiler.
122    The supporting class libraries are being worked on to fully support
123    the compiler.
124
125 Q: Do you use Bison?
126
127 A: No, currently I am using Jay which is a port of Berkeley Yacc to
128    Java that I later ported to C#.  This means that error recovery is
129    not as nice as I would like to, and for some reason error
130    productions are not being caught.  
131
132    In the future I want to port one of the Bison/Java ports to C# for
133    the parser.
134
135 Q: Should someone work on a GCC front-end to C#?
136
137 A: I would love if someone does, and we would love to help anyone that
138    takes on that task, but we do not have the time or expertise to
139    build a C# compiler with the GCC engine.  I find it a lot more fun
140    personally to work on C# on a C# compiler, which has an intrinsic
141    beauty.
142
143    We can provide help and assistance to anyone who would like to work
144    on this task.
145
146 Q: Should someone make a GCC backend that will generate CIL images?
147
148 A: I would love to see a backend to GCC that generates CIL images.  It
149    would provide a ton of free compilers that would generate CIL
150    code.  This is something that people would want to look into
151    anyways for Windows interoperation in the future.
152
153    Again, we would love to provide help and assistance to anyone
154    interested in working in such a project.
155
156 Q: What about making a front-end to GCC that takes CIL images and
157    generates native code?
158
159 A: I would love to see this, specially since GCC supports this same
160    feature for Java Byte Codes.  You could use the metadata library
161    from Mono to read the byte codes (ie, this would be your
162    "front-end") and generate the trees that get passed to the
163    optimizer.
164
165    Ideally our implementation of the CLI will be available as a shared
166    library that could be linked with your application as its runtime
167    support. 
168
169    Again, we would love to provide help and assistance to anyone
170    interested in working in such a project.
171    
172 Q: But would this work around the GPL in the GCC compiler and allow
173    people to work on non-free front-ends?
174
175 A: People can already do this by targeting the JVM byte codes (there
176    are about 130 compilers for various languages that target the JVM).
177
178 Q: Why are you writing a JIT engine instead of a front-end to GCC?
179
180 A: The JIT engine and runtime engine will be able to execute CIL
181    executables generated on Windows.
182
183 You might also want to look at the <a href="faq.html#gcc">GCC</a>
184 section on the main FAQ