c-sharp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 parse C#
  4. programs and create an internal tree representation of the
  5. program. MCS can parse itself.
  6. MCS now does type checking at the class, interface and struct
  7. levels and can resolve the class hierarchy and as of last week
  8. can generate interface code.
  9. Work is progressing quickly on various fronts in the C#
  10. compiler. Recently I started using the System.Reflection API
  11. to load system type definitions and avoid self-population of
  12. types in the compiler and dropped my internal Type
  13. representation in favor of using the CLI's System.Type.
  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. * Array declarations are currently being ignored,
  48. * PInvoke declarations are not supported.
  49. * Pre-processing is not supported.
  50. * Attribute declarations and passing currently ignored.
  51. * Compiler does not pass around line/col information from tokenizer for error reporting.
  52. * Jay does not work correctly with `error'
  53. productions, making parser errors hard to point. It
  54. would be best to port the Bison-To-Java compiler to
  55. become Bison-to-C# compiler.
  56. Nick Drochak has started a project on SourceForge for this.
  57. You can find the project at: <a href="http://sourceforge.net/projects/jb2csharp/">
  58. http://sourceforge.net/projects/jb2csharp/</a>
  59. </ul>
  60. Interesting and Fun hacks to the compiler:
  61. <ul>
  62. * Finishing the JB port from Java to C#. If you are
  63. interested in working on this, please contact the project admin on SourceForge:
  64. <a href="http://sourceforge.net/projects/jb2csharp/">
  65. http://sourceforge.net/projects/jb2csharp/</a>
  66. More on JB at: <a href="http://www.cs.colorado.edu/~dennis/software/jb.html">
  67. http://www.cs.colorado.edu/~dennis/software/jb.html</a>
  68. JB will allow us to move from the Berkeley Yacc
  69. based Jay to a Bison-based compiler (better error
  70. reporting and recovery).
  71. * Semantic Analysis: Return path coverage and
  72. initialization before use coverage are two great
  73. features of C# that help reduce the number of bugs
  74. in applications. It is one interesting hack.
  75. * Enum resolutions: it is another fun hack, as enums can be defined
  76. in terms of themselves (<tt>enum X { a = b + 1, b = 5 }</tt>).
  77. </ul>
  78. ** Questions and Answers
  79. Q: Why not write a C# front-end for GCC?
  80. A: I wanted to learn about C#, and this was an exercise in this
  81. task. The resulting compiler is highly object-oriented, which has
  82. lead to a very nice, easy to follow and simple implementation of
  83. the compiler.
  84. I found that the design of this compiler is very similar to
  85. Guavac's implementation.
  86. Targeting the CIL/MSIL byte codes would require to re-architecting
  87. GCC, as GCC is mostly designed to be used for register machines.
  88. The GCC Java engine that generates Java byte codes cheats: it does
  89. not use the GCC backend; it has a special backend just for Java, so
  90. you can not really generate Java bytecodes from the other languages
  91. supported by GCC.
  92. Q: If your C# compiler is written in C#, how do you plan on getting
  93. this working on a non-Microsoft environment.
  94. We will do this through an implementation of the CLI Virtual
  95. Execution System for Unix (our JIT engine).
  96. Q: Do you use Bison?
  97. A: No, currently I am using Jay which is a port of Berkeley Yacc to
  98. Java that I later ported to C#. This means that error recovery is
  99. not as nice as I would like to, and for some reason error
  100. productions are not being caught.
  101. In the future I want to port one of the Bison/Java ports to C# for
  102. the parser.
  103. Q: Should someone work on a GCC front-end to C#?
  104. A: I would love if someone does, and we would love to help anyone that
  105. takes on that task, but we do not have the time or expertise to
  106. build a C# compiler with the GCC engine. I find it a lot more fun
  107. personally to work on C# on a C# compiler, which has an intrinsic
  108. beauty.
  109. We can provide help and assistance to anyone who would like to work
  110. on this task.
  111. Q: Should someone make a GCC backend that will generate CIL images?
  112. A: I would love to see a backend to GCC that generates CIL images. It
  113. would provide a ton of free compilers that would generate CIL
  114. code. This is something that people would want to look into
  115. anyways for Windows interoperation in the future.
  116. Again, we would love to provide help and assistance to anyone
  117. interested in working in such a project.
  118. Q: What about making a front-end to GCC that takes CIL images and
  119. generates native code?
  120. A: I would love to see this, specially since GCC supports this same
  121. feature for Java Byte Codes. You could use the metadata library
  122. from Mono to read the byte codes (ie, this would be your
  123. "front-end") and generate the trees that get passed to the
  124. optimizer.
  125. Ideally our implementation of the CLI will be available as a shared
  126. library that could be linked with your application as its runtime
  127. support.
  128. Again, we would love to provide help and assistance to anyone
  129. interested in working in such a project.
  130. Q: But would this work around the GPL in the GCC compiler and allow
  131. people to work on non-free front-ends?
  132. A: People can already do this by targeting the JVM byte codes (there
  133. are about 130 compilers for various languages that target the JVM).
  134. Q: Why are you writing a JIT engine instead of a front-end to GCC?
  135. A: The JIT engine and runtime engine will be able to execute CIL
  136. executables generated on Windows.
  137. You might also want to look at the <a href="faq.html#gcc">GCC</a>
  138. section on the main FAQ