Java bytecodeFrom Wikipedia, the free encyclopediaJava bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. In fact, Sun Microsystems, the original creators of the Java programming language, the Java virtual machine and other components of the Java Runtime Environment (JRE), have set aside 3 values to be permanently unimplemented.[1]
[edit] Relation to JavaA Java programmer does not need to be aware of or understand Java bytecode at all. However, as suggested in the IBM developerWorks journal, "Understanding bytecode and what bytecode is likely to be generated by a Java compiler helps the Java programmer in the same way that knowledge of assembler helps the C or C++ programmer."[2]. [edit] Instructions
As each byte has 256 potential values, there are 256 possible opcodes. Of these, 0x00 through 0xca, 0xfe, and 0xff are assigned values. 0xba is unused for historical reasons. 0xca is reserved as a breakpoint instruction for debuggers and is not used by the language. Similarly, 0xfe and 0xff are not used by the language, and are reserved for internal use by the virtual machine. Instructions fall into a number of broad groups:
There are also a few instructions for a number of more specialized tasks such as exception throwing, synchronization, etc. Many instructions have prefixes and/or suffixes referring to the types of operands they operate on. These are "i", "l", "s", "b", "c", "f", "d", and "a", standing for, respectively, "integer", "long", "short", "byte", "character", "float", "double", and "reference". For example, "iadd" will add two integers, while "dadd" will add two doubles. The "const", "load", and "store" instructions may also take a suffix of the form "_n", where n is a number from 0-3 for "load" and "store". The maximum n for "const" differs by type. The "const" instructions push a value of the specified type onto the stack. For example "iconst_5" will push an integer 5, while "dconst_1" will push a double 1. There is also an "aconst_null", which pushes "null". The n for the "load" and "store" instructions specifies the location in the variable table to load from or store to. The "aload_0" instruction pushes the object in variable 0 onto the stack (this is usually the "this" object). "istore_1" stores the integer on the top of the stack into variable 1. For variables with higher numbers the suffix is dropped and operators must be used. [edit] Computational modelThe computational model of Java bytecode is that of a stack-oriented programming language. For example, assembly code for an x86 processor might look like this:
This code would add two values and move the result to a different location. Similar disassembled bytecode might look like this:
Here, the two values to be added are pushed onto the stack, where they are retrieved by the addition instruction, summed, and the result placed back on the stack. The storage instruction then moves the top value of the stack into a variable location. The numbers in front of the instructions simply represent the offset of each instruction from the beginning of the method. This stack-oriented model extends to the object oriented aspects of the language as well. A method call called "getName()", for example, may look like the following:
[edit] ExampleConsider the following Java code: outer:
[edit] GenerationMain article: List of JVM languages
The most common language targeting Java Virtual Machine by producing Java bytecode is Java. Originally only one compiler existed, the javac compiler from Sun Microsystems, which compiles Java source code to Java bytecode; but because all the specifications for Java bytecode are now available, other parties have supplied compilers that produce Java bytecode. Examples of other compilers include:
Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembler code may be also generated by machine, for example by compiler targeting Java virtual machine. Notable Java assemblers include:
Others have developed compilers, for different programming languages, in order to target the Java virtual machine, such as:
JavaFX code is also compiled to Java bytecode. [edit] ExecutionJava bytecode is designed to be executed in a Java virtual machine. There are several virtual machines available today, both free and commercial products. Further information: Java virtual machine
If executing Java bytecode in a Java virtual machine is not desirable, a developer can also compile Java source code or Java bytecode directly to native machine code with tools such as the GNU Compiler for Java. Some ARM processors have the ability to execute bytecode directly (see Jazelle). [edit] Support for dynamic languagesMain article: list of JVM languages
The Java Virtual Machine has currently no built-in support for dynamically typed languages, because the existing JVM instruction set is statically typed - in the sense that method calls have their signatures type-checked at compile time, without a mechanism to defer this decision to run time, or to choose the method dispatch by an alternative approach.[5] JSR 292 (Supporting Dynamically
Typed Languages on the JavaTM Platform) [6]
propose to add a new [edit] See also
[edit] References
[edit] External links
|
|