Contents

JVM: Instruction Set Summary

Here’s a short overview of the instruction set for the JVM.

Interpreter Loop

  • JVM interpreter works as follows: in works in the loop and at each iteration it:
    • calculates the program counter (pc) and fetches the opcode (atimically)
    • if the opcode has operands:
      • fetches the operands
    • executes the action for the opcode
  • number and size of operands depend on opcode
  • if operand is more than one byte, it is stored in big-endian order (highest-order byte first)
  • bytecode instrunction stream is single-byte aligned (exceptions: lookupswitch and tableswitch - padded to keep 4-byte alignment for operands)

Types

  • most instructions are typed: a prefix letter denotes type of operand(s)): iint, llong, sshort, bbyte, cchar, ffloat, ddouble, areference, for example: iadd, bipush, ldiv etc
  • most don’t have a version for byte, char or short type, and there are none for boolean: the values are sign-extended to int type first and then int operation is performed
  • and some aren’t: goto or arraylength
  • common literal values like 0, 1, 2 have their own opcodes (performance)

Instructions

The instrunctions of the bytecode can be categorized depending of the type of operation they perform. For this reason the JVM spec distinguishes following groups or categories of instructions:

Load and Store

Load and store graphics
  • these instructions (load and store) transfer values between local variables and operand stack of the VM frame
    • variants for types i, l, f, d, a
    • each variant has an instrunction that takes argument (like: iload, lload etc) and has also a family of no-arg instrunctions, for which an operand is implicit and coded in the mnemonic (iload_0 loads 0-th local variable array element, iload_2 loads 2-nd local variable array element, iconst_m1 loads constant)
    • forms for type int are usually used for types byte, char and short
  • four types of instrunctions:
    • load: a local var -> the stack
    • store: the stack -> local var
    • load: constant -> the stack (e.g. bipush, sipush, _ldc, ldc_w, aconsts_null, iconst_m1)
    • gain access to more local variables (wide: allows indexing to local variable array with 16-bit index; used as instruction “modifier” that precedes other load/store instruction)

Arithmetic instructions

Arithmetic instructions graphics
  • compute a reasult from two values on operand stack, puhsing the result back to operand stack
  • two types: operating on integer values and operating on floating-point values, e.g.:
    • arithmetic:
      • Add: iadd, ladd, fadd, dadd.
      • Subtract: isub, lsub, fsub, dsub.
      • Multiply: imul, lmul, fmul, dmul.
      • Divide: idiv, ldiv, fdiv, ddiv.
      • Remainder: irem, lrem, frem, drem.
      • Negate: ineg, lneg, fneg, dneg
    • bits:
      • Shift: ishl, ishr, iushr, lshl, lshr, lushr.
      • Bitwise OR: ior, lor.
      • Bitwise AND: iand, land.
      • Bitwise exclusive OR: ixor, lxor.
    • Local variable increment: iinc.
    • Comparison: dcmpg, dcmpl, fcmpg, fcmpl, lcmp.
  • integer datatypes: no overflow indication; can throw ArithmeticException in division (idiv and ldiv) and remainder (irem and lrem) when divisor is 0
  • floatin-point datatypes: also, no overflow (operation that overflows generates signed infinity; operation that underflows procuces signed zero or subnormal value)

Type Conversion

Type conversion graphics

Allow conversion bewteen JVM types.

  • widening: i2l, i2f, i2d, l2f, l2d, and f2d (don’t loose information about magnitude; i2l, l2f, l2d may loose precision; no runtime excption)
    • byte, char and short are internally widen to int; char to int zero-extends to wider format, other types have sign-extension
  • narrowing: i2b, i2c, i2s, l2i, f2i, f2l, d2i, d2l, and d2f
    • int or float to T discards n lowest-order bits
    • NaN -> 0
    • too small (neg. val of large magnitude or neg. infnity) -> smallest int/long
    • too big (pos. val of large magnitude or pos. infnity) -> biggest int/long
    • values rounded to int value: round toward zero policy
    • d2f: round toward nearest policy
  • no exceptions

Object creation

Object creation graphics
  • new: new instance of a class
  • newarray, anewarray, multianewarray - create a new array
  • getstatic, putstatic, getfield, putfield - access static fields and normal fields
  • [T]aload - load array component onto operand stack (for all possible Ts: b, c, s, i, l, f, d, a)
  • [T]astore - store value from operand stack to an array component
  • arraylength - get the length of an array
  • instanceof, checkcast - check propertis of class/arrays

Manipulation of operand stack

Stack manipulaiton graphics

Direct manipulation of operand stack: pop, pop2, dup, dup2, dup_x1, dup2_x1, dup_x2, dup2_x2, swap

Control transfer

Control transfer graphics
  • conditional branch: ifeq, ifne, iflt, ifle, ifgt, ifge, ifnull, ifnonnull, if_icmpeq, if_icmpne, if_icmplt, if_icmple, if_icmpgt if_icmpge, if_acmpeq, if_acmpne.
  • compound conditional branch: tableswitch, lookupswitch.
  • unconditional branch: goto, goto_w, jsr, jsr_w, ret.

Method invocation

Method invocation graphics
  • invokevirtual - invokes instance method, dispatching on virtual type of object
  • invokeinterface - invokes interface method, searching the methods implemented by particular run-time object
  • invokespecial - invokes instance mehtod with special handling (e.g. instance initialization)
  • invokestatic - invokes static method
  • invokedynamic - calls a method that depends on what wa linked in bootsrtap method before first execution

Return instructions

Return instructions graphics
  • ireturn , lreturn, freturn, dreturn, and areturn
  • return - for void methods

Exceptions and synchronization

Type conversion graphics

Throwing exception

  • athrow

Synchronization

  • method-level: flag ACC_SYNCHRONIZED in method_info structure (checked by method invocation instrunction); if present: thread enters monitor, calls method, exits monitor before normal/abrupt completion
  • synchronized block: two opcodes: monitorenter and monitorexit

Class Libraries

There are classes in the standard library that require cooperation from JVM (some special support), for example:

  • reflection (java.lang.reflect) and Class class
  • loading or creation of class or interface (ClassLoader class)
  • linking nd initialization of a class/interface
  • security (java.security)
  • multithreading (Thread class)
  • weak references (java.lang.ref)

Ten wpis jest częścią serii jvm.

Wszystkie wpisy w tej serii: