c-sharp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. * MCS: The Ximian C# compiler
  2. MCS began as an experiment to learn the features of C# by
  3. writing a large C# program. MCS is currently able to compile
  4. small C# programs.
  5. All type, field, method, delegates definitions are now emitted
  6. and the body of constructors and methods is being generated
  7. for a subset of the language. Although MCS can parse itself,
  8. it cant not yet compile itself. Most statements are generated
  9. correctly and about 60% of the C# expressions are supported.
  10. Work is progressing quickly on various fronts in the C#
  11. compiler.
  12. A test suite is being built currently to track the progress of
  13. the compiler.
  14. ** Phases of the compiler
  15. The compiler has a number of phases:
  16. <ul>
  17. * Lexical analyzer: hand-coded lexical analyzer that
  18. provides tokens to the parser.
  19. * The Parser: the parser is implemented using Jay (A
  20. Berkeley Yacc port to Java, that I ported to C#).
  21. The parser does minimal work and syntax checking,
  22. and only constructs a parsed tree.
  23. Each language element gets its own class. The code
  24. convention is to use an uppercase name for the
  25. language element. So a C# class and its associated
  26. information is kept in a "Class" class, a "struct"
  27. in a "Struct" class and so on. Statements derive
  28. from the "Statement" class, and Expressions from the
  29. Expr class.
  30. * Parent class resolution: before the actual code
  31. generation, we need to resolve the parents and
  32. interfaces for interface, classe and struct
  33. definitions.
  34. * Semantic analysis: since C# can not resolve in a
  35. top-down pass what identifiers actually mean, we
  36. have to postpone this decision until the above steps
  37. are finished.
  38. * Code generation: The compiler recently started generating IL
  39. executables that contain interfaces. Work is
  40. progressing in other areas.
  41. The code generation is done through the System.Reflection.Emit API.
  42. </ul>
  43. <a name="tasks">
  44. ** Current pending tasks
  45. Simple tasks:
  46. <ul>
  47. * PInvoke declarations are not supported.
  48. * Pre-processing is not supported.
  49. * Compiler does not pass around line/col information from tokenizer for error reporting.
  50. * Jay does not work correctly with `error'
  51. productions, making parser errors hard to point. It
  52. would be best to port the Bison-To-Java compiler to
  53. become Bison-to-C# compiler.
  54. Nick Drochak has started a project on SourceForge for this.
  55. You can find the project at: <a href="http://sourceforge.net/projects/jb2csharp/">
  56. http://sourceforge.net/projects/jb2csharp/</a>
  57. </ul>
  58. Interesting and Fun hacks to the compiler:
  59. <ul>
  60. * Finishing the JB port from Java to C#. If you are
  61. interested in working on this, please contact the project admin on SourceForge:
  62. <a href="http://sourceforge.net/projects/jb2csharp/">
  63. http://sourceforge.net/projects/jb2csharp/</a>
  64. More on JB at: <a href="http://www.cs.colorado.edu/~dennis/software/jb.html">
  65. http://www.cs.colorado.edu/~dennis/software/jb.html</a>
  66. JB will allow us to move from the Berkeley Yacc
  67. based Jay to a Bison-based compiler (better error
  68. reporting and recovery).
  69. * Semantic Analysis: Return path coverage and
  70. initialization before use coverage are two great
  71. features of C# that help reduce the number of bugs
  72. in applications. It is one interesting hack.
  73. * Enum resolutions: it is another fun hack, as enums can be defined
  74. in terms of themselves (<tt>enum X { a = b + 1, b = 5 }</tt>).
  75. </ul>
  76. ** Questions and Answers
  77. Q: Why not write a C# front-end for GCC?
  78. A: I wanted to learn about C#, and this was an exercise in this
  79. task. The resulting compiler is highly object-oriented, which has
  80. lead to a very nice, easy to follow and simple implementation of
  81. the compiler.
  82. I found that the design of this compiler is very similar to
  83. Guavac's implementation.
  84. Targeting the CIL/MSIL byte codes would require to re-architecting
  85. GCC, as GCC is mostly designed to be used for register machines.
  86. The GCC Java engine that generates Java byte codes cheats: it does
  87. not use the GCC backend; it has a special backend just for Java, so
  88. you can not really generate Java bytecodes from the other languages
  89. supported by GCC.
  90. Q: If your C# compiler is written in C#, how do you plan on getting
  91. this working on a non-Microsoft environment.
  92. We will do this through an implementation of the CLI Virtual
  93. Execution System for Unix (our JIT engine).
  94. Q: Do you use Bison?
  95. A: No, currently I am using Jay which is a port of Berkeley Yacc to
  96. Java that I later ported to C#. This means that error recovery is
  97. not as nice as I would like to, and for some reason error
  98. productions are not being caught.
  99. In the future I want to port one of the Bison/Java ports to C# for
  100. the parser.
  101. Q: Should someone work on a GCC front-end to C#?
  102. A: I would love if someone does, and we would love to help anyone that
  103. takes on that task, but we do not have the time or expertise to
  104. build a C# compiler with the GCC engine. I find it a lot more fun
  105. personally to work on C# on a C# compiler, which has an intrinsic
  106. beauty.
  107. We can provide help and assistance to anyone who would like to work
  108. on this task.
  109. Q: Should someone make a GCC backend that will generate CIL images?
  110. A: I would love to see a backend to GCC that generates CIL images. It
  111. would provide a ton of free compilers that would generate CIL
  112. code. This is something that people would want to look into
  113. anyways for Windows interoperation in the future.
  114. Again, we would love to provide help and assistance to anyone
  115. interested in working in such a project.
  116. Q: What about making a front-end to GCC that takes CIL images and
  117. generates native code?
  118. A: I would love to see this, specially since GCC supports this same
  119. feature for Java Byte Codes. You could use the metadata library
  120. from Mono to read the byte codes (ie, this would be your
  121. "front-end") and generate the trees that get passed to the
  122. optimizer.
  123. Ideally our implementation of the CLI will be available as a shared
  124. library that could be linked with your application as its runtime
  125. support.
  126. Again, we would love to provide help and assistance to anyone
  127. interested in working in such a project.
  128. Q: But would this work around the GPL in the GCC compiler and allow
  129. people to work on non-free front-ends?
  130. A: People can already do this by targeting the JVM byte codes (there
  131. are about 130 compilers for various languages that target the JVM).
  132. Q: Why are you writing a JIT engine instead of a front-end to GCC?
  133. A: The JIT engine and runtime engine will be able to execute CIL
  134. executables generated on Windows.
  135. You might also want to look at the <a href="faq.html#gcc">GCC</a>
  136. section on the main FAQ