Back to series
· 8 min read

Bytecode and Virtual Machine


1. What Is Bytecode?

Bytecode is an intermediate instruction set designed for a virtual execution environment. It is usually produced by a compiler from source code and does not correspond directly to the machine instructions of any physical CPU; instead, it is interpreted by a virtual machine or further compiled into native machine code.

Typical bytecode execution systems include:

  • Java: Java source code is compiled into JVM bytecode and executed by the Java Virtual Machine.
  • Python: CPython compiles source code into Python bytecode, which is then executed by the interpreter.
  • C#: C# is compiled into CIL (Common Intermediate Language) and processed by the .NET runtime.
  • JavaScript: Engines typically convert source code into bytecode or another intermediate representation internally, then execute it through an interpreter and JIT compiler.

1. Core Characteristics of Bytecode

Characteristic Description
Platform independence Not bound directly to a specific CPU instruction set
Abstract instructions Uses virtual instructions to describe operations such as computation, jumps, and calls
Easy to verify Instruction structure and type constraints can be checked before execution
Interpretable execution The virtual machine can read and execute instructions one at a time
Can be further compiled Can be compiled into native machine code via JIT
Easy to distribute Can run on platforms with a compatible runtime

Note: the platform independence of bytecode does not mean the program has no platform dependencies at all.

A program may still depend on operating system APIs, the file system, system libraries, hardware capabilities, and runtime versions. Cross-platform execution requires the target environment to provide a compatible virtual machine and runtime dependencies.

2. Differences Between Bytecode and Machine Code

Aspect Bytecode Machine Code
Target Virtual machine instruction set Physical CPU instruction set
Executed by Virtual machine or runtime CPU
Platform dependence Usually low Usually high
Execution method Interpretation or JIT compilation Direct execution by the CPU
Portability Relatively strong, depends on a compatible runtime Usually weak
Runtime overhead May include interpretation, verification, and other overhead Usually no bytecode interpretation overhead

For example, iadd in JVM bytecode represents integer addition. It is not a specific addition instruction of x86 or ARM, but an abstract operation defined by the JVM instruction set.

2. What Is a Virtual Machine?

A virtual machine (VM) is an execution environment built in software that provides programs with an abstracted computing platform.

In a bytecode system, a virtual machine implements a virtual instruction set and is responsible for loading, verifying, and executing bytecode, as well as managing runtime resources.

A virtual machine is not merely an instruction translator; it typically also handles:

  • Class or module loading.
  • Bytecode verification.
  • Instruction interpretation.
  • JIT (just-in-time) compilation.
  • Memory allocation and garbage collection.
  • Thread and synchronization management.
  • Exception handling.
  • Runtime type checking.
  • Security and access control.

1. Basic Structure of a Virtual Machine

Taking a typical language-runtime virtual machine as an example, it can be abstracted into the following parts:

Component Core responsibility
Class loader / module loader Loads the program and its dependencies
Bytecode verifier Checks instruction validity and safety constraints
Execution engine Interprets bytecode or invokes compiled code
Runtime data areas Manages stacks, the heap, method metadata, and so on
Garbage collector Reclaims memory that is no longer in use
Native interface Interacts with the operating system and native code

The internal structure of different virtual machines is not exactly the same. For example, the JVM specification defines an abstract runtime structure, while a concrete JVM implementation may use different interpreters, compilers, and garbage collection strategies.

2. Execution Flow of a Virtual Machine

```mermaid flowchart TB A["Source Code"] --> B["Compiler"] B --> C["Bytecode File"] C --> D["Loader"] D --> E["Bytecode Verification"] E --> F["Runtime Execution Engine"] F --> G["Interpreted Execution"] F --> H["JIT Compilation"] G --> I["Program Result"] H --> I style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155 style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976 style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155 style D fill:#FFF5DE,stroke:#E8D29A,color:#334155 style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155 style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976 style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155 style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155 style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155 linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Interpreted execution and JIT compilation here are not stages that every virtual machine must have at the same time. The specific execution strategy depends on the language implementation and runtime design.

III. How Is Bytecode Generated?

Bytecode is usually generated by a compiler. The compiler analyzes the source code and converts it into a sequence of instructions suitable for execution by the virtual machine.

Take Java as an example:

public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }
}

Compile it with the Java compiler:

javac Calculator.java

This generates:

Calculator.class

You can inspect the bytecode with javap:

javap -c Calculator

The output looks something like this:

public static int add(int, int);
  Code:
     0: iload_0
     1: iload_1
     2: iadd
     3: ireturn

This piece of bytecode can be broken down as follows:

Instruction Meaning
iload_0 Load the integer at index 0 in the local variable table onto the operand stack
iload_1 Load the integer at index 1 in the local variable table onto the operand stack
iadd Pop two integers, perform addition, and push the result onto the operand stack
ireturn Return the integer result

Here, i stands for the integer type, load stands for load, add stands for addition, and return stands for return.

1. Bytecode and Source Code Do Not Correspond One-to-One

Source code expresses the semantics of a high-level language, whereas bytecode expresses the operations performed by the virtual machine.

For example:

int result = a + b;

After compilation, it may contain multiple instructions such as loading variables, performing the addition, and storing the result.

Complex control structures may also be converted into:

  • Conditional jump instructions.
  • Unconditional jump instructions.
  • Method invocation instructions.
  • Stack manipulation instructions.
  • Exception-handling-related structures.

Therefore, bytecode is not a simple textual substitution of the source code; it is an execution representation generated by the compiler after semantic analysis, optimization, and instruction selection.

2. Why Does JVM Bytecode Use Stack-Based Instructions?

The JVM is a typical stack-based virtual machine.

Most of its computational instructions take their inputs from the operand stack and push the results back onto the operand stack.

For example:

iload_0
iload_1
iadd
ireturn

Its execution process can be represented as:

flowchart TB
    A["Initial Operand Stack"] --> B["Load Variable a"]
    B --> C["Load Variable b"]
    C --> D["Execute iadd"]
    D --> E["Return Result"]

    style A fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155

    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

The specific stack states are as follows:

Instruction Executed Operand Stack State (Bottom → Top)
Initial state Empty
iload_0 [a]
iload_1 [a, b]
iadd [a + b]
ireturn Return a + b

A stack-based instruction set offers several advantages:

  1. Instructions do not need to explicitly specify a large number of operand registers.
  2. The instruction format is relatively simple.
  3. It makes it easy to build an abstract execution model that works across platforms.
  4. Bytecode verification can use stack-state analysis to determine whether instruction execution is legal.

However, stack-based instructions may also produce a large number of stack operations. During compilation, a JIT compiler can convert these abstract operations into register operations that are better suited to the target CPU.

IV. The Two Main Execution Modes of a Virtual Machine

A virtual machine executes bytecode mainly in two ways: interpretation and just-in-time compilation.

1. Interpretation

The interpreter reads bytecode instructions, identifies the instruction type, and performs the corresponding operation.

For example:

iload_0
iload_1
iadd
ireturn

The interpreter processes these instructions in sequence, completing integer loading, addition, and return.

Execution process:

flowchart TB
    A["Read Bytecode Instruction"] --> B["Decode Instruction Opcode"]
    B --> C["Fetch Operands"]
    C --> D["Execute Corresponding Operation"]
    D --> E{"Any Instructions Left?"}
    E -->|"Yes"| A
    E -->|"No"| F["End Execution"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155

    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Advantages:

  • Relatively low startup cost.
  • No need to first compile all code into native machine code.
  • Facilitates the implementation of dynamic language features.
  • Provides good runtime flexibility.

Disadvantages:

  • Requires continuous instruction fetching and dispatching.
  • May incur additional interpretation overhead.
  • The execution efficiency of hot code is usually lower than that of optimized native machine code.

Interpretation does not mean that every instruction must go through complex text parsing. In practice, interpreters usually operate directly on precompiled instruction encodings and employ optimized instruction dispatch mechanisms.

2. Just-In-Time Compilation (JIT)

JIT is a technique that compiles bytecode or another intermediate representation into native machine code while the program is running.

Unlike traditional AOT (Ahead-Of-Time) compilation, JIT can optimize using information collected while the program runs.

For example:

  • Method call frequency.
  • Branch execution behavior.
  • Object type distribution.
  • Loop iteration counts.
  • Runtime call relationships.

JIT typically identifies frequently executed code and compiles it as hot code.

flowchart TB
    A["Program startup"] --> B["Interpreted execution"]
    B --> C["Collect runtime information"]
    C --> D{"Has the hotness threshold been reached?"}
    D -->|"No"| B
    D -->|"Yes"| E["JIT compilation"]
    E --> F["Generate native machine code"]
    F --> G["Execute compiled code"]
    G --> H{"Do the optimization assumptions hold?"}
    H -->|"Yes"| G
    H -->|"No"| I["Deoptimization"]
    I --> B

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style I fill:#F3EDFC,stroke:#C4B2E3,color:#334155

    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Note that JIT does not necessarily have to go through interpreted execution first. Different runtimes can adopt different strategies, for example:

  • First execute through interpretation, then compile hot code.
  • First perform faster baseline compilation, then perform deeper optimization.
  • Selectively compile part of the code based on runtime information.

3. Core JIT Optimization Techniques

The advantage of JIT is not only converting bytecode into machine code; more importantly, it uses runtime information for dynamic optimization.

Common techniques include:

Optimization Technique Core Principle
Method Inlining Embed the code of the called method at the call site
Escape Analysis Analyze whether an object escapes the current scope or thread
Scalar Replacement Break some objects into independent fields to reduce object allocation
Loop Optimization Perform optimizations such as loop unrolling and strength reduction on loops
Constant Folding Compute determinable constant expressions at compile time
Dead Code Elimination Remove code that does not affect program results
Type Specialization Generate specialized code based on runtime type information

Among them, method inlining is a very important optimization technique.

For example:

public int square(int x) {
    return x * x;
}

public int calculate(int value) {
    return square(value) + 1;
}

If JIT determines that square() is a method suitable for inlining, it may convert the call to something like:

public int calculate(int value) {
    return value * value + 1;
}

This is only an illustration to demonstrate the effect of inlining, and does not mean that the compiler will necessarily generate exactly the same source code or machine instructions.

Inlining can reduce method call overhead and create conditions for subsequent optimizations such as constant propagation and dead code elimination.

4. Deoptimization

JIT optimizations are usually based on certain runtime assumptions.

For example, if a method almost always receives the same object type during execution, JIT may generate type-specialized code based on this.

If a type that does not match the assumption appears later, the original optimized code may no longer be able to continue executing safely.

At this point, the runtime can trigger deoptimization:

  • Exit the current optimized machine code.
  • Restore the execution state of the interpreter or a lower optimization level based on the runtime state.
  • Continue executing the program.
  • Recollect information and recompile when necessary.