Search code examples
assemblyjvmemulationmachine-code

Can machine code be transpiled to jvm bytecode?


I was wondering, the jvm is designed to translate bytecode to machine code, but I can't seem to find any information on whether the inverse operation is even possible. it seems like if it was possible it would happen more often, as it would allow you to run any binary anywhere the jvm runs.

so, obviously, there's something I'm missing here, but I'm not sure what. of course there are apps that would depend on system-dependent frameworks, but for simple apps that depend solely on, say, the c standard library, it seems like it should be possible? does anybody know why this sort of thing simply isn't attempted?


Solution

  • It is not practical to convert an arbitrary machine code binary into JVM byte code.  For example, there are instructions in C++ programs & libraries that have no equivalent in JVM byte code.  You would have to divine the meaning of the program from its binary and translate that intent into something.  In the general case of an arbitrary machine code program, we don't know how to automate this.

    What you could do instead, however, is write a machine code simulator in Java that would interpret machine code programs and system calls for all possible instruction sets and operating systems — you could then take any program binary and simulate it using the JVM!  We know how to do this: it is a simple matter of programming.

    It would be a lot of effort (probably hundreds of thousands of person hours) and have the obvious downside of performance degradation: we would expect programs to run at a very, very small, tiny fraction of their native execution performance.  It would also risk numerous bugs and require extensive testing, so, in short, a big project.