82 points · by sigsev_251 · 4y ago · 89 comments
OpenTV provided such a 'middleware' where the main language was C on such a VM, and was (possibly still is) widely used.
I am possibly conflating JVM with Java, however I was under the impression they was designed as one to begin with.
Set top boxes were a very early use case. It coincided with the boom of these types of systems that were growing past the traditional “embedded” boundaries. Gosling and the team were working on that when it was still Oak and hadn’t been branded.
There’s an amazing video of him telling the story. I think it’s on YouTube.
EDIT: I say crappier due to security flaws that were present with JVM and Applets early on
Website, source repo, and paper seem to still be available, try archive.org for dead links:
The code is available at https://github.com/oracle/graal/tree/master/sulong
With projects like loom, valhalla, graalVM, JVM/Java is everything a modern language/runtime needs, plus a lot lot more.
I frankly believe the only other commercially viable language that has similar philosophy to JVM development, is Rust.
Opinions?
The JVM is a specification which describes a pretend computer and its instruction set.
This TruffleC doesn't translate C to Java and run a Java program. This compiles C to bytecode which operates on the JVM.
Whatever Java does or doesn't support is irrelevant to this compiler. TruffleC has nothing to do with the Java programming language at all.
Just like you can compile C and get a memory address of a stack or heap location on any physical computer supported by a C compiler, likewise you can compile C with TruffleC and get a memory address within the stack or heap of the pretend computer called the JVM.
This must be how it works, unless the JVM itself has no concept of memory addresses, which seems very unlikely to me. Let me know if I am wrong?
No, it compiles C to an AST, which it then interprets. The AST, which is also the interpreter in the Truffle design, are then partially evaluated to produce machine code. No bytecode is generated at any point, and in fact you can run it on a JVM that doesn't use byteocde, and then there is no bytecode anywhere.
I would hope that there's a much more efficient way to do it, this idea is just evidence that it could be done in principle. But I don't see what that more efficient way would be. You certainly need to keep a secret reference to each JVM object somehow because C doesn't require you to keep any pointer to an object e.g.
intptr_t x = (intptr_t)malloc(sizeof(int));
*(int*)x = 99;
bool did_subtract_50 = false;
if (x > 50) {
did_subtract_50 = true;
x -= 50;
}
// Now there is no pointer or even integer that contains the address
// ... later ...
// Retrieve the address and use and free it
int* y = (int*)(x + 50 * did_subtract_50);
printf("value: %d\n", *y);
free(y);https://www.semanticscholar.org/paper/TruffleC%3A-dynamic-ex...
For instance, what happens when you call a function-pointer?
Using a combination of native memory and JVM managed memory, depending on what the memory is needed for.
> For instance, what happens when you call a function-pointer?
This is a good example - because TruffleC can inline-cache a function-pointer, inlining the called function!
All this is in the linked paper, of course.
Truffle can give these scripting languages a huge boost in performance (TrufflyRuby is 3x faster than the second fastest implementation), but the JVM “doesn’t like to” rely on FFI all that much - and also, truffle is polyglot with the ability to optimize between different languages. So by creating an LLVM interpreter, ruby or python calling into that can be also optimized by e.g. inlining, in certain cases bettering the performance compared to native FFI.
Other than becoming truly cross-platform, running on top of a singular runtime gives it the ability to observe these parts as well (which in itself a huge advantage because the JVM has some killer observability tools), so for example project loom might be applicable to a python script using C libs for IO, putting the whole on a virtual thread and making its blocking calls unblocking magically.