Class AssemblyConstructState

java.lang.Object
ghidra.app.plugin.assembler.sleigh.sem.AbstractAssemblyState
ghidra.app.plugin.assembler.sleigh.sem.AssemblyConstructState

public class AssemblyConstructState extends AbstractAssemblyState
The state corresponding to a sub-table operand

This is roughly analogous to ConstructState, but for assembly. It records the assembly semantic, i.e., SLEIGH constructor, and the child states, one for each operand in the constructor. It's implementation of resolve(AssemblyResolvedPatterns, Collection) encapsulates, perhaps the very kernel of, machine-code generation. Operands can have there own complexity, but most of the core machine-code concepts of SLEIGH are handled by constructors.

  • Field Details

  • Constructor Details

    • AssemblyConstructState

      public AssemblyConstructState(AbstractAssemblyTreeResolver<?> resolver, List<AssemblyConstructorSemantic> path, int shift, AssemblyConstructorSemantic sem, List<AbstractAssemblyState> children)
      Construct the state for a selected SLEIGH constructor of a sub-table operand

      The operand's length is computed from the constructors length and the shifts and lengths of its generated operands.

      Parameters:
      resolver - the resolver
      path - the path for diagnostics
      shift - the (right) shift of this operand
      sem - the selected SLEIGH constructor
      children - the child state for each operand in the constructor
  • Method Details

    • computeEnd

      protected static int computeEnd(List<? extends AbstractAssemblyState> operands)
      Compute the farthest end byte (exclusive) among the given operands
      Parameters:
      operands - the operands
      Returns:
      the farthest end byte
    • computeHash

      public int computeHash()
      Description copied from class: AbstractAssemblyState
      Pre compute this nodes hash
      Specified by:
      computeHash in class AbstractAssemblyState
      Returns:
      the hash
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in class AbstractAssemblyState
    • toString

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

      Generate machine (partial) code for this node

      Currently, this is used to generate machine-code from a generated assembly instruction prototype, but it is not used to apply recursive constructors, i.e., for prefix generation. TODO: That should change. This performs the reverse of the machine-code parsing process, both in concept and in implementation. First, it descends to the children. Each child is a AbstractAssemblyState, i.e., either another constructor, or a value operand. (There are also specializations for dealing with hidden constructor and value operands.) Then it solves context changes, in the reverse order of the specification. Finally, it applies the patterns, in order to satisfy the constraints specified by the constructor. As a final detail, it records, for diagnostic purposes, the intermediate child patterns into the parent pattern.

      Specified by:
      resolve in class AbstractAssemblyState
      Parameters:
      fromRight - the accumulated patterns thus far, from the right sibling or left-most child
      errors - a place to collect error reports
      Returns:
      the stream of generated patterns, as accumulated
    • resolvePatterns

      protected Stream<AssemblyResolvedPatterns> resolvePatterns(AssemblyResolvedPatterns fromMutations, Collection<AssemblyResolvedError> errors)
      Apply each possible pattern for the selected constructor
      Parameters:
      fromMutations - the assembly pattern after mutations were solved
      errors - a place to collect errors
      Returns:
      the stream of patterns, as accumulated with fromMutations
    • resolveMutations

      protected Stream<AssemblyResolvedPatterns> resolveMutations(AssemblyResolvedPatterns fromChildren, Collection<AssemblyResolvedError> errors)
      Solve the mutations for the selected constructor
      Parameters:
      fromChildren - the assembly pattern as accumulated from the left-most child
      errors - a place to collect errors
      Returns:
      the stream of patterns, as accumulated with fromChildren
    • resolveRemainingChildren

      protected Stream<AssemblyResolvedPatterns> resolveRemainingChildren(AssemblyResolvedPatterns fromRight, Collection<AssemblyResolvedError> errors, List<AbstractAssemblyState> children)
      A recursive function from resolving all children right-to-left and accumulating the patterns

      This pops the right-most child in children, resolves it, and then recurses, passing the accumulated patterns in as fromRight with the remaining children. Stream.flatMap(java.util.function.Function) makes this somewhat fluent, given the possibility of multiple resolutions.

      Parameters:
      fromRight - the assembly pattern as accumulated from the right sibling. If this is the right-most sibling, then this is the pattern accumulated from the parent's right sibling, as so on. If no such sibling exists, it is the unrestricted (empty) pattern.
      errors - a place to collect errors
      children - the remaining children to resolve
      Returns:
      the stream of accumulated patterns