bytecode

term

programming

Kept in this browser · My desk

Instructions for an imaginary processor, produced by compiling a program once and executed by a small virtual machine on any real machine that has one.

Niklaus Wirth used the idea to spread Pascal around 1970: "to postulate a hypothetical computer, which would be simple to realize on various other main frames", giving Pascal-P, "P for portable". The Java class file, CPython's .pyc files, Lua's virtual machine and V8's Ignition all work the same way.

What it is. Bytecode is a set of instructions for an imaginary processor: a compiler translates the program once into these instructions, and a small virtual machine executes them on any real machine that has one. It sits between compiling and interpreting: the expensive work (parsing, checking, choosing instructions) is done once, and the result is compact and portable. The name comes from instructions whose opcodes fit in a byte.

Where it came from. Niklaus Wirth used the idea to spread Pascal around 1970: "It was my idea to postulate a hypothetical computer, which would be simple to realize on various other main frames, and for which we would build a Pascal compiler at ETH." The result was Pascal-P, "P for portable", which could be brought up on a new machine by implementing the hypothetical computer "with relatively little effort using readily available tools (assemblers)"; the first clients came from Tony Hoare's group in Belfast, whose assistants carried two heavy cartons of punched cards to Zurich. The authors of Lua later traced the stack-based virtual machines of their day to that line, "a trend that started with Pascal's P-machine" and continued with the Java Virtual Machine and Microsoft's .Net.

Two designs. A stack machine pushes operands and pops them for each operation: CPython 3.15.2 turns total = total + v * v into a load of total and v, another load of v, BINARY_OP 5 (*), BINARY_OP 0 (+) and STORE_FAST. A register machine names its operands: V8's Ignition encodes the same line in 11 bytes as Ldar a1, Mul a1, Add a0, Star a0, Return, with an implicit accumulator register. Lua moved from the first design to the second: for ten years, from 1993, it used a stack-based virtual machine, and "Since 2003, with the release of Lua 5.0, Lua uses a register-based virtual machine", whose registers live in an activation record on the stack; its authors call it the first register-based virtual machine to have wide use.

A worked example. A Java method that returns the sum of two int parameters compiles to four one-byte instructions, which the specification's opcode table lets anyone decode: 26 (0x1a) iload_0 pushes the first argument, 27 (0x1b) iload_1 the second, 96 (0x60) iadd pops both and pushes their sum, and 172 (0xac) ireturn returns it. The operands are implicit, taken from the stack, which is why stack bytecode is so compact; the price is more instructions than a register design for the same work. In Python the same exploration is one command, python -m dis on a file, and in Java javap -c on a class.

The file formats. A Java class file begins with the magic number 0xCAFEBABE, followed by version numbers, a constant pool and the methods' bytecode. Python caches bytecode in .pyc files; Python Enhancement Proposal (PEP) 552, accepted for Python 3.7, grew the .pyc header from three 32-bit words to four, the first "the magic number, versioning the bytecode and pyc format", the second a bit field that allows deterministic, hash-based files for reproducible builds. WebAssembly modules begin with the magic \0asm and version 1. Bytecode formats belong to implementations, not languages: CPython's instruction names change between releases, every instruction has taken 2 bytes since Python 3.6, and Python's glossary warns that bytecodes "are not expected to work between different Python virtual machines, nor to be stable between Python releases". The Java Virtual Machine specification states the separation plainly: the Java Virtual Machine (JVM) knows only class files, which contain bytecodes and a symbol table, and any language that can be expressed in them can run on it.

Bytecode and safety. Because bytecode arrives from outside, a virtual machine must not trust it. The JVM specification devotes a section to verification of class files "by type checking", and OpenJDK's HotSpot glossary describes bytecode verification as the linking step in which a method's bytecodes are analysed "to ensure type-safety"; a class that fails is never run. CPython, by contrast, does not verify .pyc files, so a writable cache directory is a place to plant code. Bytecode also keeps much of the source's structure (names, constants, control flow), which makes it far easier to decompile than machine code.

What goes wrong. Version mismatches (a .pyc or class file from another release), caches that outlive their source, trusting unverified bytecode, and assuming bytecode hides the logic of a program are the common failures. Performance is the other limit: an interpreter dispatching bytecode is slower than machine code, which is why just-in-time (JIT) compilers translate the hot parts.

Neighbours. Bytecode is produced by a compiler, executed by an interpreter, translated by just-in-time compilation, read with a disassembler such as Python's dis, and made of opcodes like a processor's machine code. Brazilian practice says bytecode.

Also known as: byte code, p-code, virtual machine instructions, código de bytes

Sources

← Back to B Back to the glossary