Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I want to write a simple C# compiler

I want to write a simple C# compiler

Scheduled Pinned Locked Moved C#
csharptutorialquestionlearning
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fired Fish Gmail
    wrote on last edited by
    #1

    Now I am watching the book, dragon book. I want to know how to write a C# compiler, just only a simple compiler which can complie C# Hello world prgram. Who can tell me how to do it?

    N L 2 Replies Last reply
    0
    • F Fired Fish Gmail

      Now I am watching the book, dragon book. I want to know how to write a C# compiler, just only a simple compiler which can complie C# Hello world prgram. Who can tell me how to do it?

      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #2

      See here[^]. It seems to be a good starting point. :)

      F 1 Reply Last reply
      0
      • N Nuri Ismail

        See here[^]. It seems to be a good starting point. :)

        F Offline
        F Offline
        Fired Fish Gmail
        wrote on last edited by
        #3

        thanks, I will see this aticle. I try to write a hello world program using C#. But I find that if not using the .net framwork, this C# program cannot run. Perhaps, I will try to write a c or c++ compiler and then write this C# compiler.

        1 Reply Last reply
        0
        • F Fired Fish Gmail

          Now I am watching the book, dragon book. I want to know how to write a C# compiler, just only a simple compiler which can complie C# Hello world prgram. Who can tell me how to do it?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          How simple? What are you going to leave out? (unsafe code? arrays? return values? method arguments? nonstatic methods? local variables?) It's actually not all that hard (compared to native languages), it's just a stack architecture so you don't need to worry about register allocation and the peculiarities of x86, and since the JIT compiler does a lot of optimizations anyway you can just skip all that (no SSA stage needed to get a reasonable output) Hello World is just this (debug output, as MSIL in text):

          .class private auto ansi beforefieldinit Program
          extends [mscorlib]System.Object
          {
          .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
          {
          .maxstack 8
          L_0000: ldarg.0
          L_0001: call instance void [mscorlib]System.Object::.ctor()
          L_0006: ret
          }

          .method private hidebysig static void Main(string\[\] args) cil managed
          {
              .entrypoint
              .maxstack 8
              L\_0000: nop 
              L\_0001: ldstr "Hello World!"
              L\_0006: call void \[mscorlib\]System.Console::WriteLine(string)
              L\_000b: nop 
              L\_000c: ret 
          }
          

          }

          You could skip the nops.

          F 1 Reply Last reply
          0
          • L Lost User

            How simple? What are you going to leave out? (unsafe code? arrays? return values? method arguments? nonstatic methods? local variables?) It's actually not all that hard (compared to native languages), it's just a stack architecture so you don't need to worry about register allocation and the peculiarities of x86, and since the JIT compiler does a lot of optimizations anyway you can just skip all that (no SSA stage needed to get a reasonable output) Hello World is just this (debug output, as MSIL in text):

            .class private auto ansi beforefieldinit Program
            extends [mscorlib]System.Object
            {
            .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
            {
            .maxstack 8
            L_0000: ldarg.0
            L_0001: call instance void [mscorlib]System.Object::.ctor()
            L_0006: ret
            }

            .method private hidebysig static void Main(string\[\] args) cil managed
            {
                .entrypoint
                .maxstack 8
                L\_0000: nop 
                L\_0001: ldstr "Hello World!"
                L\_0006: call void \[mscorlib\]System.Console::WriteLine(string)
                L\_000b: nop 
                L\_000c: ret 
            }
            

            }

            You could skip the nops.

            F Offline
            F Offline
            Fired Fish Gmail
            wrote on last edited by
            #5

            Thanks for your reply. I do not understand the .net framework architecture. I know that the last code program is writed with Microsoft middle language. (Just as assmebly). But I do not know how to compile the C# code to the MSIL and compile the MSIL to the exe file, in my compiler.

            L 1 Reply Last reply
            0
            • F Fired Fish Gmail

              Thanks for your reply. I do not understand the .net framework architecture. I know that the last code program is writed with Microsoft middle language. (Just as assmebly). But I do not know how to compile the C# code to the MSIL and compile the MSIL to the exe file, in my compiler.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Fired.Fish.Gmail wrote:

              I do not understand the .net framework architecture.

              What do you mean, exactly?

              Fired.Fish.Gmail wrote:

              But I do not know how to compile the C# code to the MSIL

              The techniques you'd have to use depend on how many features of C# you want to include, for example if you'd have only a single expression you could just post-order dump your AST and be done with it, otherwise it takes more work but never really a lot (for a decent example you could look at the mono compiler)

              Fired.Fish.Gmail wrote:

              compile the MSIL to the exe file

              You could do it yourself (but it's not easy, especially the metadata tables), or use ilasm.exe (included in .NET framework), or use Reflection.Emit

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups