views:

425

answers:

4

"A bytecode program is normally executed by parsing the instructions one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or "just-in-time" (JIT) compilers, translate bytecode into machine language as necessary at runtime: this makes the virtual machine unportable."

A question about this paragraph is that: After the bytecode gets processed, what's the difference between a parsed instruction and machine language (or machine code)?

+2  A: 

There's no difference - JIT compiler is done exactly for that - it produces machine code that is executed on the hardware.

sharptooth
+5  A: 

JIT is different to a byte code interpreter.

Consider the following C function:

int sum() {
   return 5 + 6;
}

This will be compiled directly machine code. The exact instructions on say x86 and ARM processors will be different.

If we wrote a basic bytecode interpreter it might look something like this:

for(;;) {
   switch(*currentInstruction++) {
   case OP_PUSHINT:
      *stack++ = nextInt(currentInstruction);
      break;
   case OP_ADD:
      --stack;
      stack[-1].add(*stack);
      break;
   case OP_RETURN:
      return stack[-1];
   }
}

This can then interpret the following set of instructions:

OP_PUSHINT (5)
OP_PUSHINT (6)
OP_ADD
OP_RETURN

If you compiled the byte code interpreter on both x86 or ARM then you would be able to run the same byte code without doing any further rewriting of the interpreter.

If you wrote a JIT compiler you would need to emit processor specific instructions (machine code) for each supported processor, whereas the byte code interpreter is relying on the C++ compiler to emit the processor specific instructions.

Steven
A: 

Ultimately it all boils down to machine instructions.

  1. Native App - contains machine instructions that are executed directly.
  2. JIT App - bytecode is compiled into machine instructions and executed.
  3. Translated App - bytecode is translated by a virtual machine that is a Native App.

As you can tell, with #1, you have the least overhead while with #3, you have the most overhead. So, performance should be fastest on #1 and just as fast on #2 after the initial compilation overhead.

sybreon
A: 
Norman Ramsey