跪拜 Guibai
← All articles
Java · JVM · Backend

The Byte-Level Anatomy of JVM Instructions: Opcodes, Operands, and Constant Pool References

By 金銀銅鐵 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

For anyone writing JVM tools, debugging performance, or generating bytecode, knowing the exact byte layout of instructions is foundational. It clarifies why constants are handled differently based on width rather than type, and how the interpreter's efficiency depends on a compact, predictable encoding.

Summary

Every JVM instruction starts with a one-byte opcode, optionally followed by operands. The interpreter loop fetches the opcode, fetches operands if any, executes, and advances the program counter. The design choice of a single-byte opcode caps the instruction set at 256 entries, and forces a tradeoff: many instructions encode extra information (like local variable slots) directly into the opcode, while others rely on operand bytes that must be assembled at runtime.

Concrete examples illustrate the forms: iadd has no operands; bipush carries a 1-byte signed immediate; sipush carries a 2-byte big-endian immediate; iinc takes two operands (a slot index and a signed byte constant); and ldc references a constant pool entry by a 1-byte index. Larger constants, like 1000000, exceed the immediate ranges and must live in the constant pool, accessed via ldc (1-byte index) or ldc_w (2-byte index). long and double values, which occupy two slots, are always fetched with ldc2_w, which has no non-wide variant.

The article also explains the difference between 100 and 1000000: they are both ints, but their widths differ. It includes a Swing-based demo that visualizes the byte stream, operand stack, and local variables step by step, and provides instructions to reproduce the byte dumps using javap and xxd.

Takeaways
— Each JVM instruction consists of a one-byte opcode followed by zero or more operands; the interpreter fetches opcode, operands, then executes.
— Instructions like iadd have no operands; iload_1 encodes the slot number into the opcode itself, so it occupies one byte.
— bipush uses a signed 1-byte immediate, range -128 to 127; sipush uses a signed 2-byte immediate, range -32768 to 32767, stored big-endian.
— iinc takes two operand bytes: a local variable slot index and a signed byte increment; it does not touch the operand stack.
— Constants that don't fit in an immediate (like 1000000) are stored in the constant pool and accessed via ldc (1-byte index) or ldc_w (2-byte index).
— long and double values always go through ldc2_w, which uses a 2-byte index and has no non-wide variant.
— Multi-byte operands are always big-endian: high-order byte first, computed as (byte1 << 8) | byte2.
— The choice between bipush and sipush depends on the value's width, not its type; 200 requires sipush, while -128 can still use bipush.
Conclusions

The one-byte opcode constraint is a fundamental design decision that trades instruction set size for compactness; it leads to duplicated instruction forms (iload_1 vs iload) and pushes large constants out of the instruction stream.

The distinction between 100 and 1000000 shows that the JVM's encoding is tuned for common small values; this has implications for code generators that want to minimize class file size.

The fact that ldc2_w has no non-wide variant suggests that long and double constants are rare enough in typical code to justify always using a wider index, simplifying the interpreter's logic.

Concepts & terms
opcode
The single byte at the start of each JVM instruction that specifies the operation to be performed. It determines the instruction's behavior and the format of any following operands.
big-endian
The byte order used for multi-byte operands in JVM bytecode: the most significant byte comes first. For example, the 16-bit value 0x03E8 is stored as 03 E8.
constant pool
A per-class table of constants and symbolic references (strings, integers, long/double values, method and field references) that instructions access by index. This is where large numeric values are stored when they don't fit in the instruction stream.
local variable slot
A 32-bit storage unit in the frame's local variable array. Each slot holds an int, float, reference, or a byte of a long/double pair. Instructions like iload and iinc reference these slots by number.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗