Ask HN: Why are there no traditional language compilers that target the JVM?
Here is the More Than You Ever Wanted To Know answer whilst I wait for a build to go green.<p>Firstly, for the last few years when you talk about this topic you have to distinguish between the &quot;old world&quot; JVMs which can only be given bytecode as input, and GraalVM, which is a superset of those JVMs and enables language implementations to use the JVM&#x27;s features whilst bypassing the bytecode layer entirely.<p>Java byte code has some problems that make it unsuitable for C-like languages. Most obviously you can&#x27;t do pointer arithmetic or arbitrary casts, which is a fundamental requirement for real C code. This doesn&#x27;t mean it can&#x27;t be done, these are all Turing complete machines after all, but it means there&#x27;s no point because performance would be very poor due to all the workarounds. For C++ the gap gets wider because Java has its own ideas about how objects work that aren&#x27;t the same as those in C++, e.g. multiple inheritance, so you can&#x27;t implement C++ classes as Java classes.<p>These sorts of problems affect any language that is semantically too far away from Java, like scripting languages. JVM bytecode is fairly well designed, is reasonably general, and the invokedynamic bytecode was put in there to make scripting languages easier. But it&#x27;s a high level bytecode so the semantic mismatch is still there.<p>For a long time this problem seemed inherent to the design space. Any VM bytecode language you can design will end up encoding <i>some</i> assumptions about language design into it, if it doesn&#x27;t then you&#x27;ve just got assembly language and we already have those. In effect, trying to create a universal bytecode is like trying to create a universal programming language.<p>But the JVM guys didn&#x27;t give up. Sun&#x2F;Oracle Labs spent many years on research and the result is Truffle. Instead of asking people to encode their language into a universal ISA so the JVM can understand it, Truffle is an API. It comes as part of GraalVM. You write an interpreter for your language using this API and compile <i>that</i> to JVM bytecode instead (it doesn&#x27;t have to be written in Java but it does have to be a language that can produce bytecode). This interpreter is then fused with the code of your target language at runtime and fed through a very advanced optimizing compiler (Graal) which generates machine code for your language. This code is then &quot;installed&quot; into the running JVM using another API called JVMCI, and then has access to all the normal JVM services like the garbage collectors, profiling, deoptimization, observability, standard libraries, OS abstractions, ability to call to&#x2F;from bytecode world and so on.<p>With Truffle you don&#x27;t think about the low level details of all that. You just write an interpreter. You do have the learn the API - your generated machine code will be relatively slow until you start using the API to annotate your interpreter and optimize it for common cases. But the API is pretty comprehensive and offers a lot of functionality, like language interop, debugging, tracing, hot swapping ...<p>The first languages implemented with Truffle were scripting languages. But the technique is general. It&#x27;s not scripting or JVM specific and there&#x27;s no specific reason the language your interpreter reads has to be textual. So they implemented an interpreter for LLVM bitcode. Now you can compile C&#x2F;C++&#x2F;FORTRAN&#x2F;Rust using LLVM, and then execute that on the JVM. Performance is good, not quite as fast as natively compiled with GCC but in the general area. It&#x27;s a fairly specialized thing to do and today is mostly used for running Python&#x2F;Ruby extensions. However, because the whole thing is virtualized you can do some mind-bending tricks with it, for example, you can eliminate all the memory errors in the software run this way and then sandbox it without using kernel sandboxing. So it has some potentially big security benefits.<p>And that&#x27;s how you can run C or any other language on the JVM without killing performance.