Search code examples
c#osdevngen

Using ngen.exe to compile a kernel


I understand that the Cosmos and SharpOS have made their own compilers to build binary code from C#, but could you use Microsoft's .NET AOT to do the same thing? Compiling C# to x86, that is. I assume you would have to leave out using statements, much like OS development when it comes to include statements. Any feedback appreciated. Thanks

EDIT: The main reason I want to use C# in kernel development is for the memory management that managed code brings with it, especially the single address space property. Would it be necessary to boot a JIT compiler, then compile C#? Or is the environment still too crippled at that point? Thanks for feedback


Solution

  • Short answer: No, using ngen.exe / an AOT compiler would not be enough to compile a kernel.

    Long answer: ngen.exe / an AOT compiler cuts out the job of the JIT compiler, but the CLR is a lot more than just a JIT compiler - even without referencing any other assemblies it also provides things like memory management, garbage collection, type checking and exception handling (and a whole load of other things besides).

    Yes using ngen does take you a step closer to producing a kernel in C#, but it doesn't take you that much closer - you still have a whole load of other problems to solve, ones which are more easily solved by writing a compiler that targets your intended environment (rather than the CLR).

    Update: If you want the "managed" part of C# then you need to create yourself a managed environment to run your code in, i.e. garbage collector etc... (or try and get an existing on like the CLR to work, which would probably be more difficult). This is a pretty herculean task even with the support of an underlying OS, doing this when you are the OS even more so (although by no means impossible - after all this is exactly what projects like Singularity and Cosmos have done).