Class OperandStack

java.lang.Object
org.codehaus.groovy.classgen.asm.OperandStack

public class OperandStack extends Object
Tracks the JVM operand stack during bytecode generation, maintaining a parallel list of ClassNode type descriptors for each slot. Used to emit correct pop/cast/box instructions and to verify stack discipline.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an operand stack tracker backed by the given controller.
  • Method Summary

    Modifier and Type
    Method
    Description
    box()
    Boxes the top operand if it is a primitive type, emitting the appropriate wrapper-creation bytecode, and returns the resulting (possibly wrapped) type.
    void
    castToBool(int mark, boolean emptyDefault)
    ensure last marked parameter on the stack is a primitive boolean if mark==stack size, we assume an empty expression or statement.
    void
    doAsType(ClassNode targetType)
    Performs an as-coercion of the top operand to targetType.
    void
    doGroovyCast(ClassNode targetType)
    do Groovy cast for top level element
    void
    Performs a Groovy cast of the top operand to the declared origin type of v.
    void
    dup()
    duplicate top element
    int
    Returns the number of tracked entries on the operand stack.
    Returns the type of the top element on the tracked operand stack without removing it.
    org.objectweb.asm.Label
    jump(int ifIns)
    Emits a conditional jump instruction and returns the newly created target label.
    void
    jump(int ifIns, org.objectweb.asm.Label label)
    Emits a conditional jump to the given label.
    void
    load(ClassNode type, int idx)
    Emits a load instruction for the given type from the specified local variable slot and pushes the type onto the tracked stack.
    void
    loadOrStoreVariable(BytecodeVariable variable, boolean useReferenceDirectly)
    Either loads or stores a local variable depending on the current LHS flag.
    void
    pop()
    remove operand stack top element using bytecode pop
    void
    popDownTo(int elements)
    Pops all stack entries above elements, emitting the appropriate POP or POP2 instruction for each popped value.
    void
    push operand on stack
    void
    pushBool(boolean value)
    Pushes an integer literal (0 or 1) representing a boolean value and tracks it as boolean on the operand stack.
    void
    load the constant on the operand stack.
    void
    Pushes the name expression as a String onto the operand stack.
    void
    remove(int amount)
    Remove amount elements from the operand stack, without using pop.
    void
    replace top level element with new element of given type
    void
    replace(ClassNode type, int n)
    replace n top level elements with new element of given type
    void
    Stores the top operand stack value into variable, casting to the variable's declared type.
    void
    swap two top level operands

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • OperandStack

      public OperandStack(WriterController controller)
      Creates an operand stack tracker backed by the given controller.
      Parameters:
      controller - the writer controller for the current compilation
  • Method Details

    • getStackLength

      public int getStackLength()
      Returns the number of tracked entries on the operand stack.
    • popDownTo

      public void popDownTo(int elements)
      Pops all stack entries above elements, emitting the appropriate POP or POP2 instruction for each popped value.
      Parameters:
      elements - the target stack depth to reduce to
    • castToBool

      public void castToBool(int mark, boolean emptyDefault)
      ensure last marked parameter on the stack is a primitive boolean if mark==stack size, we assume an empty expression or statement. was used and we will use the value given in emptyDefault as boolean if mark==stack.size()-1 the top element will be cast to boolean using Groovy truth. In other cases we throw a GroovyBugError
    • pop

      public void pop()
      remove operand stack top element using bytecode pop
    • jump

      public org.objectweb.asm.Label jump(int ifIns)
      Emits a conditional jump instruction and returns the newly created target label. The boolean operand is removed from the tracked stack.
      Parameters:
      ifIns - the branch opcode (e.g. IFEQ, IFNE)
      Returns:
      the label that the jump will branch to
    • jump

      public void jump(int ifIns, org.objectweb.asm.Label label)
      Emits a conditional jump to the given label. The boolean operand is removed from the tracked stack.
      Parameters:
      ifIns - the branch opcode (e.g. IFEQ, IFNE)
      label - the branch target
    • dup

      public void dup()
      duplicate top element
    • box

      public ClassNode box()
      Boxes the top operand if it is a primitive type, emitting the appropriate wrapper-creation bytecode, and returns the resulting (possibly wrapped) type.
      Returns:
      the type of the top operand after boxing
    • remove

      public void remove(int amount)
      Remove amount elements from the operand stack, without using pop. For example after a method invocation
    • push

      public void push(ClassNode type)
      push operand on stack
    • swap

      public void swap()
      swap two top level operands
    • replace

      public void replace(ClassNode type)
      replace top level element with new element of given type
    • replace

      public void replace(ClassNode type, int n)
      replace n top level elements with new element of given type
    • doGroovyCast

      public void doGroovyCast(ClassNode targetType)
      do Groovy cast for top level element
    • doGroovyCast

      public void doGroovyCast(Variable v)
      Performs a Groovy cast of the top operand to the declared origin type of v.
      Parameters:
      v - the variable whose origin type is the cast target
    • doAsType

      public void doAsType(ClassNode targetType)
      Performs an as-coercion of the top operand to targetType.
      Parameters:
      targetType - the target type
    • pushConstant

      public void pushConstant(ConstantExpression expression)
      load the constant on the operand stack.
    • pushDynamicName

      public void pushDynamicName(Expression name)
      Pushes the name expression as a String onto the operand stack. Constant string expressions are pushed as LDC literals; other expressions are cast to String via the Groovy runtime.
      Parameters:
      name - the expression representing the method or property name
    • loadOrStoreVariable

      public void loadOrStoreVariable(BytecodeVariable variable, boolean useReferenceDirectly)
      Either loads or stores a local variable depending on the current LHS flag. If in LHS context, stores the top of the operand stack into variable; otherwise loads the variable's value onto the stack.
      Parameters:
      variable - the bytecode variable to load or store
      useReferenceDirectly - if true, loads the Reference wrapper directly rather than unboxing it
    • storeVar

      public void storeVar(BytecodeVariable variable)
      Stores the top operand stack value into variable, casting to the variable's declared type. Handles both plain variables and Reference-wrapped closure-shared variables.
      Parameters:
      variable - the bytecode variable slot to store into
    • load

      public void load(ClassNode type, int idx)
      Emits a load instruction for the given type from the specified local variable slot and pushes the type onto the tracked stack.
      Parameters:
      type - the type of the value to load
      idx - the local variable slot index
    • pushBool

      public void pushBool(boolean value)
      Pushes an integer literal (0 or 1) representing a boolean value and tracks it as boolean on the operand stack.
      Parameters:
      value - the boolean value to push
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getTopOperand

      public ClassNode getTopOperand()
      Returns the type of the top element on the tracked operand stack without removing it.