is bytecode treated as instruction set for JVM?

2019-03-31 21:40发布

问题:

I was reading about instruction set in wiki and I came across this paragraph:

Some virtual machines that support bytecode as their ISA such as Smalltalk, the Java virtual machine, and Microsoft's Common Language Runtime, implement this by translatin the bytecode for commonly used code paths into native machine code. In addition, these virtual machines execute less frequently used code paths by interpretation (see: Just-in-time compilation). Transmeta implemented the x86 instruction set atop VLIW processors in this fashion.

What does this exactly mean? is the bytecodes are ISA for JVM and JVM in-turn support ISA of processors.

回答1:

Yes, it is as you guessed. The JVM/JRE uses Java bytecode as its instruction set and each JVM needs to be compiled on and be runnable on the native/local hardware (and therefore the local instruction set). This diagram from Wikipedia illustrates this well I think:

The JRE/JVM needs to be compiled for the specific hardware it runs on, though the Java bytecode definitions and interpretations by the JVM itself stay the same. As you point out, the Java bytecode can be seen as a kind of abstraction layer between the Java source code and the local machine/binary code. It does allow for a separation of concerns between the typical Java programmer and needing to know anything machine-specific, as almost all of that is handled by the JVM/JRE.



回答2:

is the bytecodes are ISA for JVM

The byte code is the JVMs instructions.

and JVM in-turn support ISA of processors.

but the real processor does the real work, so the JVM turns these into native instructions. First it does it interperated which is simple, but slower to execute and once the code is optimised (which is expensive) the code runs fast as raw native instructions.



回答3:

The JVM basically simulates a CPU for a Java program. Just as a CPU executes assembled opcodes natively on the hardware, the JVM executes Java opcodes, but strictly in software.

What does this exactly mean? is the bytecodes are ISA for JVM and JVM in-turn support ISA of processors.

An ISA (Instruction Set Architecture) specifies the entire set of disciplines and techniques that apply to writing low-level software that runs directly on the CPU. It includes a set of opcodes, which are uncomposable direct CPU commands. The JVM recognizes its own set of bytecodes (i.e. 8-bit opcodes) that direct the JVM to carry out interpreter-primitive instructions. So, yes, the bytecode specification makes up part of the JVM's ISA.

The JVM iterates through the list of opcodes executing them one by one using its own memory to emulate hardware components (e.g. stack, register, main memory) and using primitive arithmetic and logic operations to emulate the ALU. These emulated components also make up the JVM's ISA. This is the basic construction of any interpreter, give or take. However, to improve the runtime of Java applications, the JVM compiles "hotspots" to machine-specific code for optimal performance. Hotspots are sections of the code that are ran frequently. This is known as "Just-In-Time" compilation, and can be done while the program is executing. This technique brings Java's performance a lot closer to that of compiled languages. JIT is used in the .NET framework as well.

Each operating system has its own JVM implementation, which may also vary by the device's ISA. For example, you may have a JVM written for Linux-Arm, Linux-x86, or Windows-x86. The JVM itself may be written in a platform-independent (sort of anyway) language like C, but its JIT compiler must support compilation to the device's instruction set.