The Artima Developer Community
Sponsored Link

Bytecode Basics
A First Look at the Bytecodes of the Java Virtual Machine
by Bill Venners
First Published in JavaWorld, September 1996

<<  Page 4 of 7  >>

Advertisement

Pushing local variables onto the stack
Local variables are stored in a special section of the stack frame. The stack frame is the portion of the stack being used by the currently executing method. Each stack frame consists of three sections -- the local variables, the execution environment, and the operand stack. Pushing a local variable onto the stack actually involves moving a value from the local variables section of the stack frame to the operand section. The operand section of the currently executing method is always the top of the stack, so pushing a value onto the operand section of the current stack frame is the same as pushing a value onto the top of the stack.

The Java stack is a last-in, first-out stack of 32-bit slots. Because each slot in the stack occupies 32 bits, all local variables occupy at least 32 bits. Local variables of type long and double, which are 64-bit quantities, occupy two slots on the stack. Local variables of type byte or short are stored as local variables of type int, but with a value that is valid for the smaller type. For example, an int local variable which represents a byte type will always contain a value valid for a byte (-128 <= value <= 127).

Each local variable of a method has a unique index. The local variable section of a method's stack frame can be thought of as an array of 32-bit slots, each one addressable by the array index. Local variables of type long or double, which occupy two slots, are referred to by the lower of the two slot indexes. For example, a double that occupies slots two and three would be referred to by an index of two.

Several opcodes exist that push int and float local variables onto the operand stack. Some opcodes are defined that implicitly refer to a commonly used local variable position. For example, iload_0 loads the int local variable at position zero. Other local variables are pushed onto the stack by an opcode that takes the local variable index from the first byte following the opcode. The iload instruction is an example of this type of opcode. The first byte following iload is interpreted as an unsigned 8-bit index that refers to a local variable.

Unsigned 8-bit local variable indexes, such as the one that follows the iload instruction, limit the number of local variables in a method to 256. A separate instruction, called wide, can extend an 8-bit index by another 8 bits. This raises the local variable limit to 64 kilobytes. The wide opcode is followed by an 8-bit operand. The wide opcode and its operand can precede an instruction, such as iload, that takes an 8-bit unsigned local variable index. The JVM combines the 8-bit operand of the wide instruction with the 8-bit operand of the iload instruction to yield a 16-bit unsigned local variable index.

The opcodes that push int and float local variables onto the stack are shown in the following table:

Opcode Operand(s) Description
iload vindex pushes int from local variable position vindex
iload_0 (none) pushes int from local variable position zero
iload_1 (none) pushes int from local variable position one
iload_2 (none) pushes int from local variable position two
iload_3 (none) pushes int from local variable position three
fload vindex pushes float from local variable position vindex
fload_0 (none) pushes float from local variable position zero
fload_1 (none) pushes float from local variable position one
fload_2 (none) pushes float from local variable position two
fload_3 (none) pushes float from local variable position three

The next table shows the instructions that push local variables of type long and double onto the stack. These instructions move 64 bits from the local variable section of the stack frame to the operand section.

Opcode Operand(s) Description
lload vindex pushes long from local variable positions vindex and (vindex + 1)
lload_0 (none) pushes long from local variable positions zero and one
lload_1 (none) pushes long from local variable positions one and two
lload_2 (none) pushes long from local variable positions two and three
lload_3 (none) pushes long from local variable positions three and four
dload vindex pushes double from local variable positions vindex and (vindex + 1)
dload_0 (none) pushes double from local variable positions zero and one
dload_1 (none) pushes double from local variable positions one and two
dload_2 (none) pushes double from local variable positions two and three
dload_3 (none) pushes double from local variable positions three and four

The final group of opcodes that push local variables move 32-bit object references from the local variables section of the stack frame to the operand section. These opcodes are shown in the following table:

Opcode Operand(s) Description
aload vindex pushes object reference from local variable position vindex
aload_0 (none) pushes object reference from local variable position zero
aload_1 (none) pushes object reference from local variable position one
aload_2 (none) pushes object reference from local variable position two
aload_3 (none) pushes object reference from local variable position three

<<  Page 4 of 7  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us