Skip to content

Bytecode instructions

hydrophobis edited this page Mar 25, 2025 · 2 revisions

Load/Store Instructions:

  • aaload(): Loads a reference from an array onto the stack.
  • aastore(): Stores a reference into an array.
  • aconst_null(): Pushes null onto the stack.
  • aload(index): Loads a reference from a local variable at the specified index. Uses different opcodes for indices 0-3 or others.
  • anewarray(class_index): Creates a new array of references of a specified class.
  • areturn(): Returns a reference from a method.
  • arraylength(): Pushes the length of an array onto the stack.
  • astore(index): Stores a reference into a local variable at the specified index.

Arithmetic Instructions:

  • add(): Adds two integers or floats on the stack.
  • sub(): Subtracts one integer or float from another on the stack.
  • mul(): Multiplies two integers or floats on the stack.
  • div(): Divides one integer or float by another on the stack.
  • rem(): Calculates the remainder of division for integers or floats.

Type Conversion Instructions:

  • i2f(): Converts an integer to a float.
  • f2i(): Converts a float to an integer.
  • i2l(): Converts an integer to a long.
  • l2i(): Converts a long to an integer.
  • f2l(): Converts a float to a long.
  • l2f(): Converts a long to a float.

Branching Instructions:

  • goto(offset): Jumps to a specified offset in the bytecode.
  • if_icmpeq(offset): Jumps if two integers are equal.
  • if_icmpne(offset): Jumps if two integers are not equal.
  • if_icmplt(offset): Jumps if the first integer is less than the second.
  • if_icmpge(offset): Jumps if the first integer is greater than or equal to the second.
  • if_icmpgt(offset): Jumps if the first integer is greater than the second.
  • if_icmple(offset): Jumps if the first integer is less than or equal to the second.

Method Invocation Instructions:

  • invokestatic(method_index): Invokes a static method.
  • invokevirtual(method_index): Invokes an instance method.
  • invokespecial(method_index): Invokes a special method (like constructors or superclass methods).
  • invokeinterface(method_index): Invokes an interface method.

Object Manipulation Instructions:

  • new(class_index): Creates a new object of the class specified by class_index.
  • checkcast(class_index): Checks if an object is an instance of the specified class.
  • instanceof(class_index): Checks if an object is an instance of the specified class.

Return Instructions:

  • return(): Exits the current method without returning a value.
  • ireturn(): Returns an integer from the current method.
  • freturn(): Returns a float from the current method.
  • lreturn(): Returns a long from the current method.
  • dreturn(): Returns a double from the current method.
  • areturn(): Returns a reference from the current method.

Stack Manipulation Instructions:

  • pop(): Removes the top value from the stack.
  • dup(): Duplicates the top value of the stack.
  • swap(): Swaps the top two values of the stack.

Special Instructions:

  • nop(): Does nothing (used for padding or placeholders).
  • ldc(constant_index): Loads a constant from the constant pool onto the stack.
  • lookupswitch(): A table-based switch statement with dynamic offsets.
  • tableswitch(): A jump table-based switch statement with a range of values.

Clone this wiki locally