You are on page 1of 17

JVM Instruction Set Architecture

Instructions
A Java virtual machine instruction consists of a one-byte opcode specifying the operation to be performed, followed by zero or more operands supplying arguments or data that are used by the operation Operands are not required, there are many instructions that consist of only the opcode One-byte instructions allow for up to 256 instructions but only about 200 are used in class files, leaving room for more instructions to be added Each instruction has a mnemonic name which is mapped to the binary one-byte opcode

JVM Instruction Set Architecture


Instruction Format
The mnemonic operation names often include the data type followed by the operation name
iadd, ladd, fadd, dadd int, long, float, double

Instruction set of JVM


Instructions fall into a number of broad groups: Load and store (e.g. aload_0,istore) Arithmetic and logic (e.g. ladd,fcmpl) Type conversion (e.g. i2b,d2i) Object creation and manipulation (new,putfield) Operand stack management (e.g. swap,dup2) Control transfer (e.g. ifeq,goto) Method invocation and return (e.g. invokespecial,areturn)

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 as given in next slide. For example, "iadd" will add two integers, while "dadd" will add two doubles.

Prefix/Suffix Operand Type i integer l long s short b byte c character f float d double a reference

JVM Data Types

The model of computation of Java bytecode is that of a stack-oriented programming language. 0 iload_1 //The iload_1 (integer load) instruction pushes slot 1 to the operand stack 1 iload_2 2 iadd 3 istore_3 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.

Load Instructions The load instructions load a value on the operand stack. iload, fload. Load respectively a value of type int, float onto the operand stack. bipush. Push a byte. sipush. Store Instructions The store instructions store a value from the operand stack in a variable. The following instructions store a value in a local variable: istore, fstore, lstore, dstore, astore Arithmetic Instructions The arithmetic instructions take one or two values from the operand stack, compute a result and push that result on the operand stack. The available operations include addition, subtraction, multiplication, division, remainder, shifting and logical operations like bitwise or. All of these instructions only operate on values of a specific type.

Conversion Instructions The conversion instructions convert the numeric type of a value to another. Values of type int, float, long and double can be converted to each other.
JVM supports conversion operations that convert from one data type to another, these include both data types in the operation name
i2l, i2f, i2d, l2f, l2d, f2d

Class Instances and Array Instructions The instructions presented here create and manipulate class instances and arrays. The following instructions create an object or array: new. Create new class instance. newarray. Create new array.

The following instructions access a field of a class or class instance: getfield. Load the value of a field of an object in a variable. putfield. Store a value from a variable into a field of an object The following instructions load or store values from or to an array: baload, caload, saload, iaload, faload, laload, daload, a aload. Load a value of respectively type byte or boolean, char, short, int, float, long and double from an array. bastore, castore, sastore, iastore, fastore, lastore, dasto re, aastore. Store a value of respectively type byte or boolean, char, short, int, float, long and d ouble in an array.

Stack Manipulation Instructions Stack manipulation instructions directly manipulate the operand stack. pop. Pop one word from the operand stack pop2. Pop two words from the operand stack dup. Duplicate one word on top of the operand stack. dup2. Duplicate two words on top of the operand stack.

Branch and Comparison Instructions The branch instructions transfer control between instructions. There are instructions to compare a value of type int on the opernad stack to zero in all possible ways and branch on success. Other conditional branch instructions include comparing a reference on the operand stack to null and comparing values of type int or type reference. All branching instructions have the offset to the instruction to branch to as their operands. The tableswitch instruction is used when the case values of the switch can be efficiently used as indices into the table of target offsets. Besides instructions to jump to and return from a subroutine, there also are the following unconditional branch instructions: goto

Method Instructions The instructions presented here are used for method invocation or return. invokevirtual. Invoke instance method invokespecial. Invoke instance method, special handling for superclass method invocations. invokeinterface. Invoke interface method. invokestatic. Invoke class method. return. Return void from method. ireturn, freturn, lreturn, dreturn, areturn. Return respectively int, float, long, double and reference from method, where ireturn is also used to return from methods of types byte, char and short

Miscellaneous Instructions athrow instruction, used to throw exceptions synchronization instructions.

JVM (Architecture) Runtime Data Areas


JVM Heap
The heap is a data area shared by all JVM threads Memory from the heap is allocated for instances of classes and arrays Can be either of fixed size or dynamic Does not to be in contiguous memory space Maintained by an automatic storage management system or garbage collector

JVM Runtime Data Areas


Method Area
The method area is also shared among all JVM threads It stores per-class structures
such as the runtime constant pool, field and method data, code for methods and constructors, including the special methods used in class and instance initialization

The method area is logically part of the heap, but depending on the implementation it may or may not be garbage collected or compacted

JVM Runtime Data Areas


Runtime Constant Pool
The runtime constant pool is a per-class runtime representation of the constant pool table in a class file It contains numeric constants as well as method and field references that are resolved at runtime This is similar to a symbol table for a conventional programming language, although it stores a wider range of data

You might also like