Lots of more work on the disassembler and supporting runtime
[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 parse C#
5         programs and create an internal tree representation of the
6         program.  MCS can parse itself.  
7
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.  
13
14 ** Phases of the compiler
15
16         The compiler has a number of phases:
17
18                 * Lexical analyzer: hand-coded lexical analyzer that
19                   provides tokens to the parser.
20
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.
25
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
32                   Expr class.
33
34                 * Parent class resolution: before the actual code
35                   generation, we need to resolve the parents and
36                   interfaces for interface, classe and struct
37                   definitions.
38
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
42                   are finished.
43
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. 
47
48 ** Current pending tasks
49
50         Array declarations are currently being ignored, 
51
52         PInvoke is not supported.
53
54         Pre-processing is not supported.
55
56         Attribute declarations and passing currently ignored.
57
58         Compiler does not pass around line/col information from tokenizer for error reporting.
59
60         Jay does not work correctly with `error' productions, making parser errors hard to point.
61
62 ** Questions and Answers
63
64 Q: Why not write a C# front-end for GCC?
65
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
69    the compiler.
70
71    I found that the design of this compiler is very similar to
72    Guavac's implementation.
73
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.
76    
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
80    supported by GCC. 
81
82 Q: If your C# compiler is written in C#, how do you plan on getting
83    this working on a non-Microsoft environment.
84
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.
88
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.
95
96 Q: Do you use Bison?
97
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.  
102
103    In the future I want to port one of the Bison/Java ports to C# for
104    the parser.
105
106 Q: How do I compile it?
107
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.
112
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
115    them.
116
117    It might be simple to port Jay.cs to Windows, but I have not tried
118    this. 
119