Florian

Florian

  • NA
  • 4
  • 5.2k

Native C# compiler

Feb 3 2012 2:42 PM
I was just thinking  about the following...

"A C# compiler that compiles C# source code to  native machine code that doesn't need the Common Language Runtime (CLR) to  execute."

How can this be accomplished?

Well, theoretically it  shouldn't be so hard. It's already possible to generate native machine code for  managed assemblies using the NGEN utility. The only problem is: These  assemblies still need the CLR to be executed. So, how can we get rid of this  (heavy) dependency?

The idea is: A small, native C# runtime library,  like the GNU libc for C.

What about memory management? The C# language  doesn't support explicit memory management, and if there is no CLR, how do we  handle "new"? Well, one solution are smart pointers. The C# compiler/runtime  library could take care of object lifetime management and use smart  pointers under the hood.

What about reflection and interoperability? The  native libraries could still use the CLI metadata format. In fact,  a similar approach is used already in Windows 8 for the Windows Runtime:  Native COM objects are exposed through .winmd files to the C#  compiler.

Well, would such a compiler be useful to anybody? I like  the C# language a lot and would prefer to use it for system programming over C++  because it has a very clean structure and is well architected. The problem is  the CLR, its the security model etc. which comes at a pretty high cost, not to  speak of crossing the managed/unmanaged code barrier using platform invocation  services. Having a C# compiler that translates your source files directly to  machine code, with a neat C# runtime library instead of a CLR as the only  dependency, doesn't seem such a bad idea to me.

What do you think of this  topic?

Answers (3)