Writing a Managed JIT in C# with CoreCLR
Nice! This sort of direction is definitely the way managed runtimes are going and should go, long term.<p>Here&#x27;s a quick comparison vs Graal, which is the same thing but for the HotSpot JVM.<p>Java 9+ has Java-side interfaces for extending the compiler called JVMCI. So you don&#x27;t need the deep hacking with the C++ vtables and other such mangling, that I guess could be quite fragile in the face of future changes to the runtime. There&#x27;s a clean plugin API to let you write a compiler entirely in managed code. You can drop it in as a JAR, set some command line flags, and it&#x27;ll be used.<p>HotSpot has an interpreter (written in C++ for now). The CLR never did, instead it uses a model where all code is compiled on first access. This means Graal avoids the re-entrancy issues that Alexandre solves with a counter in a TLS slot. Instead Graal is invoked asynchronously whilst the main program continues to run. And Graal is itself interpreted until it warms up, at which point it starts to compile itself.<p>The ICorJitCompiler interface the .NET JIT has to implement is much simpler than the HotSpot equivalent:<p><a href="http:&#x2F;&#x2F;lafo.ssw.uni-linz.ac.at&#x2F;javadoc&#x2F;graalvm&#x2F;jdk.internal.jvmci.compiler&#x2F;javadoc&#x2F;" rel="nofollow">http:&#x2F;&#x2F;lafo.ssw.uni-linz.ac.at&#x2F;javadoc&#x2F;graalvm&#x2F;jdk.internal....</a><p>Whilst the compile method is kind of the same, the CompilationResult structure is far harder to fill out. That&#x27;s because HotSpot heavily uses de-optimisation to make things go fast, so the compiler isn&#x27;t just responsible for production of machine code but also de-optimisation metadata. That metadata enables converting stack frames of optimised code back into interpreter stack frames and is quite complex to produce, in fact it affects the design of the whole compiler. It&#x27;s also required to produce safepoint metadata, GC pointer maps and more.<p><a href="http:&#x2F;&#x2F;lafo.ssw.uni-linz.ac.at&#x2F;javadoc&#x2F;graalvm&#x2F;jdk.internal.jvmci.code&#x2F;javadoc&#x2F;jdk&#x2F;internal&#x2F;jvmci&#x2F;code&#x2F;CompilationResult.html?is-external=true" rel="nofollow">http:&#x2F;&#x2F;lafo.ssw.uni-linz.ac.at&#x2F;javadoc&#x2F;graalvm&#x2F;jdk.internal....</a><p>There&#x27;s also an additional reflection API that provides access to things that the compiler needs, like raw bytecodes.<p>The author says finally<p><i>The benefits to have a Managed JIT wouldn’t be visible in short-term or even medium-term while it could open many possibilities in the long run, but for a project of the - legacy - size of .NET, this is probably too much to ask.</i><p>But is it too much to ask? Java not only has Graal but Project Metropolis, the goal of which is to explore conversion of HotSpot into Java. HotSpot is older and more complex than the CLR, but apparently they don&#x27;t consider it impossible. And SubstrateVM is the spiritual successor to Jikes; a JVM written entirely in Java, using magic methods and classes to model pointer arithmetic for the garbage collector and the compiler in AOT mode to produce the final binary image.<p>The real issue is not .NET&#x27;s size or legacy, but rather, Microsoft&#x27;s level of investment in it and&#x2F;or their strategic choices. The .NET team are paying a heavy price for the lack of investment into portability in recent years, and have been frequently distracted by the many re-spins and backwards compatibility breaks. They have also tended to push complexity and performance into the C# language rather than the runtime. Perhaps in hindsight the Java approach has worked out with less tech debt and a better path to the future.