1 * MCS: The Ximian C# compiler
3 MCS began as an experiment to learn the features of C# by
4 writing a large C# program. MCS is currently able to parse C#
5 programs and create an internal tree representation of the
6 program. MCS can parse itself.
8 Work is progressing quickly on various fronts in the C#
9 compiler. Recently I started using the System.Reflection API
10 to load system type definitions and avoid self-population of
11 types in the compiler and dropped my internal Type
12 representation in favor of using .NET's System.Type.
14 ** Phases of the compiler
16 The compiler has a number of phases:
18 * Lexical analyzer: hand-coded lexical analyzer that
19 provides tokens to the parser.
21 * The Parser: the parser is implemented using Jay (A
22 Berkeley Yacc port to Java, that I ported to C#).
23 The parser does minimal work and syntax checking,
24 and only constructs a parsed tree.
26 Each language element gets its own class. The code
27 convention is to use an uppercase name for the
28 language element. So a C# class and its associated
29 information is kept in a "Class" class, a "struct"
30 in a "Struct" class and so on. Statements derive
31 from the "Statement" class, and Expressions from the
34 * Parent class resolution: before the actual code
35 generation, we need to resolve the parents and
36 interfaces for interface, classe and struct
39 * Semantic analysis: since C# can not resolve in a
40 top-down pass what identifiers actually mean, we
41 have to postpone this decision until the above steps
44 * Code generation: nothing done so far, but I do not
45 expect this to be hard, as I will just use
46 System.Reflection.Emit to generate the code.
48 ** Current pending tasks
50 Array declarations are currently being ignored,
52 PInvoke is not supported.
54 Pre-processing is not supported.
56 Attribute declarations and passing currently ignored.
58 Compiler does not pass around line/col information from tokenizer for error reporting.
60 Jay does not work correctly with `error' productions, making parser errors hard to point.
62 ** Questions and Answers
64 Q: Why not write a C# front-end for GCC?
66 A: I wanted to learn about C#, and this was an exercise in this
67 task. The resulting compiler is highly object-oriented, which has
68 lead to a very nice, easy to follow and simple implementation of
71 I found that the design of this compiler is very similar to
72 Guavac's implementation.
74 Targeting the CIL/MSIL byte codes would require to re-architecting
75 GCC, as GCC is mostly designed to be used for register machines.
77 The GCC Java engine that generates Java byte codes cheats: it does
78 not use the GCC backend; it has a special backend just for Java, so
79 you can not really generate Java bytecodes from the other languages
82 Q: If your C# compiler is written in C#, how do you plan on getting
83 this working on a non-Microsoft environment.
85 The compiler will have two output mechanisms: IL code or C code.
86 A compiled version of the compiler could be run on Unix
87 using the JIT runtime.
89 The C output generation bit is just intended to be a temporary
90 measure to allow Unix hackers to contribute to the effort without
91 requiring Windows and Microsoft's .NET implementation to work on
92 the compiler. So the MCS C# compiler will compile itself to C,
93 this code then compiled to an executable on Unix and voila! We
94 have a native compiler for GNU/Linux.
98 A: No, currently I am using Jay which is a port of Berkeley Yacc to
99 Java that I later ported to C#. This means that error recovery is
100 not as nice as I would like to, and for some reason error
101 productions are not being caught.
103 In the future I want to port one of the Bison/Java ports to C# for
106 Q: How do I compile it?
108 A: Compiling MCS currently requires you to run my port of <a
109 href="http://primates.ximian.com/~miguel/code/jay.cs.tar.gz">Jay to
110 C#</a> on a Unix system to generate the parser, and then you need
111 to use Microsoft's .NET csc.exe compiler to compile the compiler.
113 You only need to compile the compiler compiler (C code), the
114 samples are Java samples that I did not port, and you do not need
117 It might be simple to port Jay.cs to Windows, but I have not tried