Class Instruction

java.lang.Object
net.covers1624.coffeegrinder.bytecode.Instruction
All Implemented Interfaces:
net.covers1624.quack.util.Copyable<Instruction>
Direct Known Subclasses:
AbstractInvoke, AbstractLoop, ArrayLen, Assert, Binary, Block, BlockContainer, ClassDecl, Compare, Comparison, CompoundAssignment, DeadCode, FieldDecl, IfInstruction, InvokeDynamic, Leave, LocalVariable, LogicAnd, LogicOr, MethodDecl, MethodReference, NewArray, Reference, Return, SimpleInstruction, Store, Switch, SwitchTable, SwitchTable.SwitchSection, Synchronized, Ternary, TryCatch.TryCatchHandler, TryInstruction, Unary, Yield

public abstract class Instruction extends Object implements net.covers1624.quack.util.Copyable<Instruction>
Represents an Instruction within an AST tree.

Created by covers1624 on 21/9/21.

  • Constructor Details

    • Instruction

      protected Instruction()
  • Method Details

    • getResultType

      public abstract AType getResultType()
      Gets the stack type of the value produced by this Instruction.
      Returns:
      The AType.
    • copy

      public Instruction copy()
      Deep copy this instruction.

      This many not be supported by all instructions.

      Implementors should use copy-constructor syntax.

      Specified by:
      copy in interface net.covers1624.quack.util.Copyable<Instruction>
      Returns:
      The copied instruction.
      Throws:
      UnsupportedOperationException - If this instruction does not support being copied.
    • getDirectFlags

      public abstract EnumBitSet<InstructionFlag> getDirectFlags()
      Any direct flags for this Instruction.

      The return value should be cached statically inside the Instruction impl.

      Returns:
      An EnumBitSet representing the flags.
      See Also:
    • getFlags

      public final EnumBitSet<InstructionFlag> getFlags()
      Gets the flags describing the behavior of this Instruction. This method wil compute the flags on-demand and caches the result, until some changes the AST invalidate the cache.
      Returns:
      An EnumBitSet representing the flags.
      See Also:
    • hasFlag

      public final boolean hasFlag(InstructionFlag flag)
      Returns weather this instruction exposes this specific flag either directly, or from one of its children.
      Parameters:
      flag - The flag to check.
      Returns:
      If the flag is present.
    • hasDirectFlag

      public final boolean hasDirectFlag(InstructionFlag flag)
      Returns weather this instruction exposes this specific flag directly.
      Parameters:
      flag - The flag to check.
      Returns:
      If the flag is present.
    • accept

      public abstract <R, C> R accept(InsnVisitor<R,C> visitor, C ctx)
      Pass this Instruction through the supplied InsnVisitor.
      Parameters:
      visitor - The InsnVisitor.
      ctx - The context.
      Returns:
      The return result from the InsnVisitor.
    • accept

      public final <R> R accept(InsnVisitor<R,None> visitor)
      Passes this Instruction through the supplied InsnVisitor. This method will assume the generic of None for the Context generic provided by InsnVisitor
      Parameters:
      visitor - The InsnVisitor.
      Returns:
      The return result from the InsnVisitor.
    • getParentOrNull

      @Nullable public final @Nullable Instruction getParentOrNull()
      The Instruction which this instruction is a child of.
      Returns:
      The parent Instruction or null
      See Also:
    • getParent

      public final Instruction getParent()
      The Instruction which this instruction is a child of.
      Returns:
      The parent Instruction.
      See Also:
    • getNextSiblingOrNull

      @Nullable public final @Nullable Instruction getNextSiblingOrNull()
      This Instructions next sibling instruction.
      Returns:
      The next sibling Instruction or null.
      See Also:
    • getNextSibling

      public final Instruction getNextSibling()
      This Instructions next sibling instruction.
      Returns:
      The next sibling Instruction.
      See Also:
    • getPrevSiblingOrNull

      @Nullable public final @Nullable Instruction getPrevSiblingOrNull()
      This Instructions previous sibling instruction.
      Returns:
      The previous sibling Instruction or null.
      See Also:
    • getPrevSibling

      public final Instruction getPrevSibling()
      This Instructions previous sibling instruction.
      Returns:
      The previous sibling Instruction.
      See Also:
    • getFirstChildOrNull

      @Nullable public final @Nullable Instruction getFirstChildOrNull()
      This Instructions first child instruction.
      Returns:
      The first child Instruction or null if this instruction has no children.
      See Also:
    • getFirstChild

      public final Instruction getFirstChild()
      This Instructions first child instruction.
      Returns:
      The first child Instruction.
      See Also:
    • getLastChildOrNull

      @Nullable public final @Nullable Instruction getLastChildOrNull()
      This Instructions last child instruction.
      Returns:
      The last child Instruction or null if this instruction has no children.
      See Also:
    • getLastChild

      public final Instruction getLastChild()
      This Instructions last child instruction.
      Returns:
      The last child Instruction.
      See Also:
    • getChildren

      public final net.covers1624.quack.collection.FastStream<Instruction> getChildren()
      Gets a FastStream containing this Instructions children.
      Returns:
      The FastStream.
    • getDescendants

      public final net.covers1624.quack.collection.FastStream<Instruction> getDescendants()
      Gets a FastStream iterating all descendants including this Instruction in post-order.
      Returns:
      The FastStream.
    • descendantsOfType

      public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> descendantsOfType(Class<? extends R> type)
      Returns a filtered FastStream of descendants in post-order.
      Parameters:
      type - The Instruction class type to filter by.
      Returns:
      The filtered FastStream.
    • descendantsWhere

      public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> descendantsWhere(Predicate<Instruction> filter)
      Returns a filtered FastStream of descendants in post-order.

      This function is intended to be used by static matching methods.

      Parameters:
      filter - The filter to apply.
      Returns:
      The filtered FastStream.
    • descendantsMatching

      public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> descendantsMatching(Function<Instruction,@Nullable R> filter)
      Returns a filtered FastStream of descendants in post-order.

      This function is intended to be used by static matching methods which return null to indicate a failed match.

      Parameters:
      filter - The filter to apply.
      Returns:
      The filtered FastStream.
    • descendantsToList

      public final <R extends Instruction> LinkedList<R> descendantsToList(Class<? extends R> type)
      Returns a LinkedList of descendants in post-order.
      Parameters:
      type - The Instruction class type to filter by.
      Returns:
      The LinkedList.
    • descendantsToListWhere

      public final <R extends Instruction> LinkedList<R> descendantsToListWhere(Predicate<Instruction> filter)
      Returns a LinkedList of descendants in post-order.

      This function is intended to be used by static matching methods.

      Parameters:
      filter - The filter to apply.
      Returns:
      The LinkedList.
    • firstAncestorOfType

      public final <R extends Instruction> R firstAncestorOfType(Class<? extends R> type)
      Returns the first parent Instruction with the provided type.
      Parameters:
      type - The Instruction class type to filter by.
      Returns:
      The first parent Instruction.
    • ancestorsOfType

      public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> ancestorsOfType(Class<? extends R> type)
      Returns a filtered FastStream of ancestors.
      Parameters:
      type - The Instruction class type to filter by.
      Returns:
      The filtered FastStream.
    • isDescendantOf

      public final boolean isDescendantOf(@Nullable @Nullable Instruction possibleAncestor)
      Checks if this Instruction or one of its parents is possibleAncestor.
      Parameters:
      possibleAncestor - The ancestor to check against.
      Returns:
      If this Instruction is a descendant of possibleAncestor.
    • addRef

      public final void addRef()
      Increments the reference count of this instruction.

      This should generally not be directly used.

      See Also:
    • releaseRef

      public final void releaseRef()
      Decrements the reference count of this instruction.

      This should generally not be directly used.

      See Also:
    • isConnected

      public final boolean isConnected()
      Used to check if this Instruction has any references and is connected to a tree.
      Returns:
      If this Instruction is connected.
    • remove

      public final void remove()
      Removes this Instruction from an InstructionCollection.
      Throws:
      UnsupportedOperationException - If this Instruction is not contained within a collection.
    • insertBefore

      public final void insertBefore(Instruction value)
      Inserts an Instruction before this one inside an InstructionCollection.
      Parameters:
      value - The Instruction to insert.
      Throws:
      UnsupportedOperationException - If this Instruction is not contain
    • insertAfter

      public final void insertAfter(Instruction value)
      Inserts an Instruction after this one inside an InstructionCollection.
      Parameters:
      value - The Instruction to insert.
      Throws:
      UnsupportedOperationException - If this Instruction is not contain
    • replaceWith

      public final <T extends Instruction> T replaceWith(T value)
      Replace this Instruction in the tree with another Instruction.

      This function will efficiently remove and unlink the instruction from wherever else it was in the tree prior if required.

      Parameters:
      value - The value to replace with.
    • getTag

      @Nullable public final @Nullable InsnTag getTag()
      An optional data tag for this instruction.
      Returns:
      The tag.
    • setTag

      public final void setTag(@Nullable @Nullable InsnTag tag)
      Sets the optional data tag for this instruction.
      Parameters:
      tag - The data tag. null to clear.s
      Throws:
      IllegalStateException - If this Instruction already has a tag set.
    • getBytecodeOffset

      public final int getBytecodeOffset()
      Gets the offset of this instruction in bytecode.
      Returns:
      The offset.
    • setBytecodeOffset

      public final void setBytecodeOffset(int bytecodeOffset)
      Set the offset for this instruction in bytecode.
      Parameters:
      bytecodeOffset - The offset.
    • withOffsets

      public final <T extends Instruction> T withOffsets(Instruction sourceInsn)
      Sets the bytecode offset and tracked source line of this instruction to that of the given instruction.
      Parameters:
      sourceInsn - The source instruction to copy from.
      Returns:
      This instruction.
    • setOffsets

      public final void setOffsets(Instruction sourceInsn)
      Sets the bytecode offset and tracked source line of this instruction to that of the given instruction.
      Parameters:
      sourceInsn - The source instruction to copy from.
    • getSourceLine

      public final int getSourceLine()
      Gets the captured source line as represented by the LineNumberTable.
      Returns:
      The line number.
    • setSourceLine

      public final void setSourceLine(int sourceLine)
      Sets the captured source line.
      Parameters:
      sourceLine - The source line.
    • toString

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

      public String toString(DebugPrintOptions opts)
    • invalidateFlags

      protected void invalidateFlags()
    • onConnected

      @MustBeInvokedByOverriders protected void onConnected()
    • onDisconnected

      @MustBeInvokedByOverriders protected void onDisconnected()
    • onChildModified

      protected void onChildModified()
    • computeFlags

      protected EnumBitSet<InstructionFlag> computeFlags()
      Called to compute the exposed flags exposed via getFlags().
      Returns:
      The computed flags.