# Bytecode

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Bytecode
> Markdown URL: https://mediated.wiki/source/Bytecode.md
> Source: https://en.wikipedia.org/wiki/Bytecode
> Source revision: 1352480992
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Instruction set designed to be run by a software interpreter

"Portable code" and "P-code" redirect here. For other uses, see [software portability](/source/Software_portability) and [P-Code (disambiguation)](/source/P-Code_(disambiguation)).

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Bytecode" – news · newspapers · books · scholar · JSTOR (January 2009) (Learn how and when to remove this message)

Program execution General concepts Code Translation Compiler Compile time Optimizing compiler Linking Execution Runtime system Executable Interpreter Virtual machine Intermediate representation (IR) Types of code Source code Object code Bytecode Machine code Microcode Compilation strategies Ahead-of-time (AOT) Just-in-time (JIT) Tracing just-in-time Compile and go system Precompilation Transcompilation Recompilation Meta-tracing Notable runtimes Android Runtime (ART) BEAM (Erlang) Common Language Runtime (CLR) and Mono CPython and PyPy crt0 (C target-specific initializer) Java virtual machine (JVM) LuaJIT Objective-C and Swift's V8 and Node.js Zend Engine (PHP) Notable compilers & toolchains GNU Compiler Collection (GCC) LLVM and Clang MSVC Glasgow Haskell Compiler (GHC) v t e

**Bytecode** (also called **portable code** or **p-code**) is an [instruction set](/source/Instruction_set) designed for efficient execution by a software [interpreter](/source/Interpreter_(computing)). Unlike [human-readable](/source/Human-readable_code)[1] [source code](/source/Source_code), bytecodes are compact numeric codes, constants, and references (normally numeric addresses) that encode the result of [compiler](/source/Compiler) parsing and performing [semantic analysis](/source/Semantic_analysis_(compilers)) of things like type, scope, and nesting depths of program objects.

The name *bytecode* stems from instruction sets that have one-[byte](/source/Byte) [opcodes](/source/Opcode) followed by optional parameters. [Intermediate representations](/source/Intermediate_representation) such as bytecode may be output by [programming language](/source/Programming_language) implementations to ease [interpretation](/source/Interpreter_(computing)), or it may be used to reduce hardware and [operating system](/source/Operating_system) dependence by allowing the same code to run [cross-platform](/source/Cross-platform), on different devices. Bytecode may often be either directly executed on a [virtual machine](/source/Virtual_machine) (a [p-code machine](/source/P-code_machine), i.e., interpreter), or it may be further compiled into [machine code](/source/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](/source/Stack_machine) are the most common, but virtual [register machines](/source/Register_machine) have been built also.[2][3] Different parts may often be stored in separate files, similar to [object modules](/source/Object_file), 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](/source/Just-in-time_compilation)* (JIT) compilers, translate bytecode into [machine code](/source/Machine_code) as necessary at [runtime](/source/Run_time_(program_lifecycle_phase)). This makes the virtual machine hardware-specific but does not lose the portability of the bytecode. For example, [Java](/source/Java_(programming_language)) and [Smalltalk](/source/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).[4]

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](/source/Java_(programming_language)), [Raku](/source/Raku_(programming_language)), [Python](/source/Python_(programming_language)), [PHP](/source/PHP),[a] [Tcl](/source/Tcl_(programming_language)), [mawk](/source/AWK) and [Forth](/source/Forth_(programming_language)) (however, Forth is seldom compiled via bytecodes in this way, and its virtual machine is more generic instead). The implementation of [Perl](/source/Perl) and [Ruby](/source/Ruby_(programming_language)) 1.8 instead work by walking an [abstract syntax tree](/source/Abstract_syntax_tree) representation derived from the source code.

More recently, the authors of [V8](/source/V8_(JavaScript_engine))[1] and [Dart](/source/Dart_(programming_language))[7] 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.[8]

## Examples

- [ActionScript](/source/ActionScript) executes in the ActionScript Virtual Machine (AVM), which is part of Flash Player and [AIR](/source/Adobe_AIR). ActionScript code is typically transformed into bytecode format by a [compiler](/source/Compiler). Examples of compilers include one built into Adobe Flash Professional and one built into Adobe Flash Builder and available in the [Adobe Flex SDK](/source/Apache_Flex).

- [Adobe Flash](/source/Adobe_Flash) objects

- [BANCStar](https://en.wikipedia.org/w/index.php?title=BANCStar_programming_language&action=edit&redlink=1), originally bytecode for an interface-building tool but used also as a language

- [Berkeley Packet Filter](/source/Berkeley_Packet_Filter)

- [EBPF](/source/EBPF)

- Berkeley Pascal[9]

- Byte Code Engineering Library

- C to [Java virtual machine](/source/Java_virtual_machine) compilers

- [CLISP](/source/CLISP) implementation of [Common Lisp](/source/Common_Lisp) used to compile only to bytecode for many years; however, now it also supports compiling to native code with the help of [GNU lightning](/source/GNU_lightning)

- [CMUCL](/source/CMUCL) and Scieneer Common Lisp implementations of [Common Lisp](/source/Common_Lisp) can compile either to native code or to bytecode, which is far more compact

- [Common Intermediate Language](/source/Common_Intermediate_Language) executed by [Common Language Runtime](/source/Common_Language_Runtime), used by [.NET](/source/.NET) languages such as [C#](/source/C_Sharp_(programming_language))

- [Dalvik](/source/Dalvik_(software)) bytecode, designed for the [Android](/source/Android_(operating_system)) platform, is executed by the [Dalvik virtual machine](/source/Dalvik_virtual_machine)

- Dis bytecode, designed for the [Inferno (operating system)](/source/Inferno_(operating_system)), is executed by the [Dis virtual machine](/source/Dis_virtual_machine)

- [EiffelStudio](/source/EiffelStudio) for the [Eiffel](/source/Eiffel_(programming_language)) programming language

- EM, the [Amsterdam Compiler Kit](/source/Amsterdam_Compiler_Kit) virtual machine used as an intermediate compiling language and as a modern bytecode language

- [Emacs](/source/Emacs) is a text editor with most of its functions implemented by [Emacs Lisp](/source/Emacs_Lisp), its built-in dialect of [Lisp](/source/Lisp_(programming_language)). These features are compiled into bytecode. This architecture allows users to customize the editor with a high level language, which after compiling into bytecode yields reasonable performance.

- [Embeddable Common Lisp](/source/Embeddable_Common_Lisp) implementation of [Common Lisp](/source/Common_Lisp) can compile to bytecode or C code

- [Common Lisp](/source/Common_Lisp) provides a disassemble function[10] which prints to the standard output the underlying code of a specified function. The result is implementation-dependent and may or may not resolve to bytecode. Its inspection can be utilized for debugging and optimization purposes.[11] [Steel Bank Common Lisp](/source/Steel_Bank_Common_Lisp), for instance, produces:

- (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] ; #<FDEFINITION object for PRINT> ; 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

- Ericsson implementation of [Erlang](/source/Erlang_(programming_language)) uses BEAM bytecodes

- [Ethereum](/source/Ethereum)'s Virtual Machine (EVM) is the runtime environment, using its own bytecode, for transaction execution in Ethereum (smart contracts).

- [Icon](/source/Icon_(programming_language))[12] and [Unicon](/source/Unicon_(programming_language))[13] programming languages

- [Infocom](/source/Infocom) used the [Z-machine](/source/Z-machine) to make its software applications more portable

- [Java bytecode](/source/Java_bytecode), which is executed by the [Java virtual machine](/source/Java_virtual_machine) - [ASM](/source/ObjectWeb_ASM) - [BCEL](https://en.wikipedia.org/w/index.php?title=BCEL&action=edit&redlink=1) - Javassist

- [Keiko bytecode](/source/Keiko_bytecode) used by the [Oberon-2](/source/Oberon-2) programming language to make it and the [Oberon operating system](/source/Oberon_operating_system) more portable.

- [KEYB](/source/KEYB_(command)), the [MS-DOS](/source/MS-DOS)/[PC DOS](/source/PC_DOS) keyboard driver with its resource file [KEYBOARD.SYS](/source/KEYBOARD.SYS) containing layout information and short [p-code](/source/P-code_machine) sequences executed by an interpreter inside the resident driver.[14][15]

- [LLVM IR](/source/LLVM_IR)

- LSL, a scripting language used in virtual worlds compiles into bytecode running on a virtual machine. Second Life has the original Mono version, Inworldz developed the Phlox version.

- [Lua](/source/Lua) language uses a register-based bytecode virtual machine

- m-code of the [MATLAB](/source/MATLAB) language[16]

- [Malbolge](/source/Malbolge) is an [esoteric](/source/Esoteric_programming_language) [machine language](/source/Machine_language) for a ternary virtual machine.

- [Microsoft P-code](/source/P-code_machine#Microsoft_P-code) used in [Visual C++](/source/Visual_C%2B%2B) and [Visual Basic](/source/Visual_Basic_(classic))

- [Multiplan](/source/Multiplan)[17]

- [O-code](/source/O-code) of the [BCPL](/source/BCPL) programming language

- [OCaml](/source/OCaml) language optionally compiles to a compact bytecode form

- [p-code](/source/P-code_machine) of [UCSD Pascal](/source/UCSD_Pascal) implementation of the [Pascal](/source/Pascal_(programming_language)) language

- [Parrot virtual machine](/source/Parrot_virtual_machine)

- [Pick BASIC](/source/Pick_operating_system) also referred to as Data BASIC or [MultiValue BASIC](/source/MultiValue#MultiValue_DataBASIC)

- The [R environment for statistical computing](/source/R_(programming_language)) offers a bytecode compiler through the compiler package, now standard with R version 2.13.0. It is possible to compile this version of R so that the base and recommended packages exploit this.[18]

- [Pyramid 2000](/source/Pyramid_2000) adventure game

- [Python](/source/Python_(programming_language)) scripts are compiled on execution to Python's bytecode language, and the compiled files (.pyc) are cached inside the script's folder

- 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

- [Scheme 48](/source/Scheme_48) implementation of Scheme using bytecode interpreter

- Bytecodes of many implementations of the [Smalltalk](/source/Smalltalk) language

- The [Spin interpreter](/source/Parallax_Propeller#Built_in_Spin_bytecode_interpreter) built into the [Parallax](/source/Parallax%2C_Inc._(company)) Propeller [microcontroller](/source/Microcontroller)

- The [SQLite](/source/SQLite) database engine translates SQL statements into a bespoke byte-code format.[19]

- Apple [SWEET16](/source/SWEET16)

- [Tcl](/source/Tcl_(programming_language))

- [TIMI](/source/IBM_i#TIMI) is used by compilers on the [IBM i](/source/IBM_i) platform.

- [Tiny BASIC](/source/Tiny_BASIC#Implementation_in_a_virtual_machine)

- [Visual Basic for Applications](/source/Visual_Basic_for_Applications) compiles to bytecode.

- [Visual FoxPro](/source/Visual_FoxPro) compiles to bytecode

- [WebAssembly](/source/WebAssembly)

- [YARV](/source/YARV) and [Rubinius](/source/Rubinius) for [Ruby](/source/Ruby_(programming_language))

- [ZCODE](/source/ZCODE)

- [Zend Engine](/source/Zend_Engine) opcodes for [PHP](/source/PHP)

## See also

Look up ***[bytecode](https://en.wiktionary.org/wiki/bytecode)*** in Wiktionary, the free dictionary.

- [Computing platform](/source/Computing_platform) – Environment in which a piece of software is executed

- [Intermediate representation](/source/Intermediate_representation) – Data structure or code used by a compiler

- [Runtime system](/source/Runtime_system) – Computer system that provides behavior needed by running code

## Notes

1. **[^](#cite_ref-7)** PHP has [just-in-time compilation](/source/Just-in-time_compilation) in PHP 8,[5][6] and before while not on in the default version, had options like [HHVM](/source/HHVM). For older versions of PHP: Although [PHP](/source/PHP) opcodes are generated each time the program is launched, and are always interpreted and not [just-in-time compiled](/source/Just-in-time_compiled).

## References

1. ^ [***a***](#cite_ref-Dynamic_Machine_Code_1-0) [***b***](#cite_ref-Dynamic_Machine_Code_1-1) ["Dynamic Machine Code Generation"](https://chromium.googlesource.com/external/github.com/v8/v8.wiki/+/eb80fb157da30e8c838e758f178de674e47648ed/Design-Elements.md). Google Inc. [Archived](https://web.archive.org/web/20170305155607/https://github.com/v8/v8/wiki/Design%20Elements) from the original on 2017-03-05. Retrieved 2024-12-01.

1. **[^](#cite_ref-Jucs_Lua_2-0)** ["The Implementation of Lua 5.0"](http://www.jucs.org/jucs_11_7/the_implementation_of_lua/jucs_11_7_1159_1176_defigueiredo.html). (NB. This involves a register-based virtual machine.)

1. **[^](#cite_ref-Dalvik_3-0)** ["Dalvik VM"](https://web.archive.org/web/20130518021154/http://source.android.com/tech/dalvik/dalvik-bytecode.html). Archived from [the original](http://source.android.com/tech/dalvik/dalvik-bytecode.html) on 2013-05-18. Retrieved 2012-10-29. (NB. This VM is register based.)

1. **[^](#cite_ref-Byte_Machine_4-0)** ["Byte Code Vs Machine Code"](http://www.allaboutcomputing.net/2014/07/byte-code-vs-machine-code.html). *www.allaboutcomputing.net*. Retrieved 2017-10-23.

1. **[^](#cite_ref-5)** O’Phinney, Matthew Weier. ["Exploring the New PHP JIT Compiler"](https://www.zend.com/blog/exploring-new-php-jit-compiler). *Zend by Perforce*. Retrieved 2021-02-19.

1. **[^](#cite_ref-6)** ["PHP 8: The JIT - stitcher.io"](https://stitcher.io/blog/php-jit). *stitcher.io*. Retrieved 2021-02-19.

1. **[^](#cite_ref-Loitsch_Bytecode_8-0)** Loitsch, Florian. ["Why Not a Bytecode VM?"](https://web.archive.org/web/20130512215811/http://www.dartlang.org/articles/why-not-bytecode/). [Google](/source/Google). Archived from [the original](http://www.dartlang.org/articles/why-not-bytecode/) on 2013-05-12.

1. **[^](#cite_ref-Javascript_9-0)** ["JavaScript myth: JavaScript needs a standard bytecode"](https://2ality.com/2012/01/bytecode-myth.html). *2ality.com*.

1. **[^](#cite_ref-10)** G., Adam Y. (2022-07-11). ["Berkeley Pascal"](https://github.com/adamyg/berkeley_pascal). *[GitHub](/source/GitHub)*. Retrieved 2022-01-08.

1. **[^](#cite_ref-11)** ["CLHS: Function DISASSEMBLE"](http://www.lispworks.com/documentation/HyperSpec/Body/f_disass.htm). *www.lispworks.com*.

1. **[^](#cite_ref-12)** Collective (2023-12-13). ["The Common Lisp Cookbook – Performance Tuning and Tips"](https://lispcookbook.github.io/cl-cookbook/performance.html). *lispcookbook.github.io*.

1. **[^](#cite_ref-Arizona_Icom_13-0)** ["The Implementation of the Icon Programming Language"](https://web.archive.org/web/20160305123148/http://www.cs.arizona.edu/icon/ftp/doc/ib1up.pdf) (PDF). Archived from [the original](http://www.cs.arizona.edu/icon/ftp/doc/ib1up.pdf) (PDF) on 2016-03-05. Retrieved 2011-09-09.

1. **[^](#cite_ref-Icon_Unicon_14-0)** ["The Implementation of Icon and Unicon a Compendium"](https://unicon.sourceforge.net/book/ib.pdf) (PDF). [Archived](https://ghostarchive.org/archive/20221009/http://unicon.sourceforge.net/book/ib.pdf) (PDF) from the original on 2022-10-09.

1. **[^](#cite_ref-Paul_2001_KEYBOARD_15-0)** Paul, Matthias R. (2001-12-30). ["KEYBOARD.SYS internal structure"](https://groups.google.com/d/msg/comp.os.msdos.programmer/l_IuSHsBDWQ/887rJF9IYmMJ). [Newsgroup](/source/Usenet_newsgroup): [comp.os.msdos.programmer](news:comp.os.msdos.programmer). Retrieved 2016-09-17. […] In fact, the format is basically the same in [MS-DOS](/source/MS-DOS) 3.3 - 8.0, [PC DOS](/source/PC_DOS) 3.3 - 2000, including Russian, Lithuanian, Chinese and Japanese issues, as well as in Windows NT, 2000, and XP […]. There are minor differences and incompatibilities, but the general format has not changed over the years. […] Some of the data entries contain normal tables […] However, most entries contain *executable code* interpreted by some kind of [p-code interpreter](/source/P-code_machine) at *[runtime](/source/Run_time_(program_lifecycle_phase))*, including conditional branches and the like. This is why the [KEYB](/source/KEYB_(DOS_command)) driver has such a huge memory footprint compared to table-driven keyboard drivers which can be done in 3 - 4 Kb getting the same level of function except for the interpreter. […]{{[cite newsgroup](https://en.wikipedia.org/wiki/Template:Cite_newsgroup)}}: CS1 maint: deprecated archival service ([link](https://en.wikipedia.org/wiki/Category:CS1_maint:_deprecated_archival_service))

1. **[^](#cite_ref-Mendelson_2001_KEYBOARD_16-0)** [Mendelson, Edward](/source/Edward_Mendelson) (2001-07-20). ["How to Display the Euro in MS-DOS and Windows DOS"](http://www.columbia.edu/~em36/wpdos/eurodos.html). Display the euro symbol in full-screen MS-DOS (including Windows 95 or Windows 98 full-screen DOS). [Archived](https://web.archive.org/web/20160917201248/http://www.columbia.edu/~em36/wpdos/eurodos.html) from the original on 2016-09-17. Retrieved 2016-09-17. […] Matthias [R.] Paul […] warns that the [IBM PC DOS](/source/IBM_PC_DOS) version of the keyboard driver uses some internal procedures that are not recognized by the [Microsoft](/source/Microsoft) driver, so, if possible, you should use the [IBM](/source/IBM) versions of both [KEYB.COM](/source/KEYB.COM) and [KEYBOARD.SYS](/source/KEYBOARD.SYS) instead of mixing Microsoft and IBM versions […] (NB. What is meant by "procedures" here are some additional bytecodes in the IBM KEYBOARD.SYS file not supported by the Microsoft version of the KEYB driver.)

1. **[^](#cite_ref-Patent_6973644_17-0)** ["United States Patent 6,973,644"](https://web.archive.org/web/20170305001731/http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=6973644.PN.&OS=PN/6973644&RS=PN/6973644). Archived from [the original](http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=6973644.PN.&OS=PN/6973644&RS=PN/6973644) on 2017-03-05. Retrieved 2009-05-21.

1. **[^](#cite_ref-Multiplan_18-0)** *Microsoft C Pcode Specifications*. p. 13. [Multiplan](/source/Multiplan) wasn't compiled to [machine code](/source/Machine_code), but to a kind of byte-code which was run by an [interpreter](/source/Interpreter_(computing)), in order to make Multiplan portable across the widely varying hardware of the time. This byte-code distinguished between the machine-specific [floating point format](/source/Floating_point_format) to calculate on, and an external (standard) format, which was [binary coded decimal](/source/Binary_coded_decimal) (BCD). The PACK and UNPACK instructions converted between the two.

1. **[^](#cite_ref-cran_r_19-0)** ["R Installation and Administration"](https://cran.r-project.org/doc/manuals/R-admin.html#Byte_002dcompiler). *cran.r-project.org*.

1. **[^](#cite_ref-SQLite_20-0)** ["The SQLite Bytecode Engine"](https://web.archive.org/web/20170414044139/http://sqlite.org/opcode.html). Archived from [the original](https://www.sqlite.org/opcode.html) on 2017-04-14. Retrieved 2016-08-29.

---
Adapted from the Wikipedia article [Bytecode](https://en.wikipedia.org/wiki/Bytecode) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Bytecode?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
