Bytecode Explained

Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normally numeric addresses) that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.

The name bytecode stems from instruction sets that have one-byte opcodes followed by optional parameters. Intermediate representations such as bytecode may be output by programming language implementations to ease interpretation, or it may be used to reduce hardware and operating system dependence by allowing the same code to run cross-platform, on different devices. Bytecode may often be either directly executed on a virtual machine (a p-code machine, i.e., interpreter), or it may be further compiled into machine code for better performance.

Since bytecode instructions are processed by software, they may be arbitrarily complex, but are nonetheless often akin to traditional hardware instructions: virtual stack machines are the most common, but virtual register machines have been built also. Different parts may often be stored in separate files, similar to object modules, but dynamically loaded during execution.

Execution

A bytecode program may be executed by parsing and directly executing 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 code as necessary at runtime. This makes the virtual machine hardware-specific but does not lose the portability of the bytecode. For example, Java and Smalltalk code is typically stored in bytecode format, which is typically then JIT compiled to translate the bytecode to machine code before execution. This introduces a delay before a program is run, when the bytecode is compiled to native machine code, but improves execution speed considerably compared to interpreting source code directly, normally by around an order of magnitude (10x).

Because of its performance advantage, today many language implementations execute a program in two phases, first compiling the source code into bytecode, and then passing the bytecode to the virtual machine. There are bytecode based virtual machines of this sort for Java, Raku, Python, PHP, Tcl, mawk and Forth (however, Forth is seldom compiled via bytecodes in this way, and its virtual machine is more generic instead). The implementation of Perl and Ruby 1.8 instead work by walking an abstract syntax tree representation derived from the source code.

More recently, the authors of V8 and Dart have challenged the notion that intermediate bytecode is needed for fast and efficient VM implementation. Both of these language implementations currently do direct JIT compiling from source code to machine code with no bytecode intermediary.

Examples

(disassemble '(lambda (x) (print x)))

disassembly for (LAMBDA (X))
  • 2436F6DF: 850500000F22 TEST EAX, [#x220F0000] ; no-arg-parsing entry point
  • E5: 8BD6 MOV EDX, ESI
  • E7: 8B05A8F63624 MOV EAX, [#x2436F6A8] ; #
  • ED: B904000000 MOV ECX, 4
  • F2: FF7504 PUSH DWORD PTR [EBP+4]
  • F5: FF6005 JMP DWORD PTR [EAX+5]
  • F8: CC0A BREAK 10 ; error trap
  • FA: 02 BYTE #X02
  • FB: 18 BYTE #X18 ; INVALID-ARG-COUNT-ERROR
  • FC: 4F BYTE #X4F ; ECX
  • Compiled code can be analysed and investigated using a built-in tool for debugging the low-level bytecode. The tool can be initialized from the shell, for example:

    >>> import dis # "dis" - Disassembler of Python byte code into mnemonics.>>> dis.dis('print("Hello, World!")') 1 0 LOAD_NAME 0 (print) 2 LOAD_CONST 0 ('Hello, World!') 4 CALL_FUNCTION 1 6 RETURN_VALUE

    See also

    Notes and References

    1. Web site: G.. Adam Y.. Berkeley Pascal. . 2022-07-11. 2022-01-08.
    2. Web site: CLHS: Function DISASSEMBLE. www.lispworks.com.
    3. Web site: Performance Tuning and Tips. lispcookbook.github.io.