I want to write a simple C# compiler
-
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?
-
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?
-
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.
-
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?
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.
-
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.
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.
-
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.
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