2001-12-27 Gaurav Vaish <gvaish@iitk.ac.in>
[mono.git] / web / c-sharp
1 * MCS: The Ximian C# compiler
2
3         MCS is currently able to compile small 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         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.
13
14         A test suite is being built currently to track the progress of
15         the compiler and various programs are routinely compiled and
16         ran.
17
18 ** Phases of the compiler
19
20         The compiler has a number of phases:
21
22         <ul>
23                 * Lexical analyzer: hand-coded lexical analyzer that
24                   provides tokens to the parser.
25
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.
30
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
37                   Expr class.
38
39                 * Parent class resolution: before the actual code
40                   generation, we need to resolve the parents and
41                   interfaces for interface, classe and struct
42                   definitions.
43
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
47                   are finished.
48
49                 * Code generation: The code generation is done through
50                   the System.Reflection.Emit API.
51
52         </ul>
53
54 <a name="tasks">
55 ** Current pending tasks
56
57         Simple tasks:
58
59         <ul>
60                 * Extern declarations are missing.
61         </ul>
62
63         Larger tasks:
64
65         <ul>
66                 * Finish constant folding.
67         </ul>
68
69         Interesting and Fun hacks to the compiler:
70
71         <ul>
72
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. 
77                   
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>
81
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.
86
87         </ul>
88
89 ** Questions and Answers
90
91 Q: Why not write a C# front-end for GCC?
92
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
96    the compiler.
97
98    I found that the design of this compiler is very similar to
99    Guavac's implementation.
100
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.
103    
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
107    supported by GCC. 
108
109 Q: If your C# compiler is written in C#, how do you plan on getting
110    this working on a non-Microsoft environment.
111
112    We will do this through an implementation of the CLI Virtual
113    Execution System for Unix (our JIT engine). 
114
115 Q: Do you use Bison?
116
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.  
121
122    In the future I want to port one of the Bison/Java ports to C# for
123    the parser.
124
125 Q: Should someone work on a GCC front-end to C#?
126
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
131    beauty.
132
133    We can provide help and assistance to anyone who would like to work
134    on this task.
135
136 Q: Should someone make a GCC backend that will generate CIL images?
137
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.
142
143    Again, we would love to provide help and assistance to anyone
144    interested in working in such a project.
145
146 Q: What about making a front-end to GCC that takes CIL images and
147    generates native code?
148
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
153    optimizer.
154
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
157    support. 
158
159    Again, we would love to provide help and assistance to anyone
160    interested in working in such a project.
161    
162 Q: But would this work around the GPL in the GCC compiler and allow
163    people to work on non-free front-ends?
164
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).
167
168 Q: Why are you writing a JIT engine instead of a front-end to GCC?
169
170 A: The JIT engine and runtime engine will be able to execute CIL
171    executables generated on Windows.
172
173 You might also want to look at the <a href="faq.html#gcc">GCC</a>
174 section on the main FAQ