Class GenericCodeFormatter<InsnType extends IInstruction>

java.lang.Object
com.pnfsoftware.jeb.core.units.code.asm.render.GenericCodeFormatter<InsnType>
Type Parameters:
InsnType - instruction type formatted by this formatter

@Ser public class GenericCodeFormatter<InsnType extends IInstruction> extends Object
A base formatter used by the INativeCodeUnit. Plugins implementing a processor may override some public and protected methods to customize their disassembly.
  • Field Details

    • MNEMONIC_RIGHT_PADDING_LENGTH_DEFAULT

      public static final int MNEMONIC_RIGHT_PADDING_LENGTH_DEFAULT
      Default right padding length for instruction mnemonics.
      See Also:
  • Constructor Details

    • GenericCodeFormatter

      public GenericCodeFormatter()
      Create a generic code formatter.
    • GenericCodeFormatter

      public GenericCodeFormatter(IVirtualMemory mem)
      Create a formatter bound to a virtual memory.

      For testing only.

      Parameters:
      mem - virtual memory
  • Method Details

    • setCodeUnit

      public void setCodeUnit(INativeCodeUnit<InsnType> pbcu)
      Set the code unit formatted by this object.
      Parameters:
      pbcu - native code unit
    • getCodeUnit

      public INativeCodeUnit<InsnType> getCodeUnit()
      Retrieve the code unit formatted by this object.
      Returns:
      native code unit
    • getMemory

      public IVirtualMemory getMemory()
      Retrieve the virtual memory being formatted.
      Returns:
      virtual memory
    • getEndianness

      public Endianness getEndianness()
      Retrieve the formatter endianness.
      Returns:
      endianness
    • getRawDataDeclarator

      public String getRawDataDeclarator(int bitsize)
      The default implementation returns "dX". Can be overridden.
      Parameters:
      bitsize - raw data size in bits
      Returns:
      raw data declarator
    • getDataSeparator

      public String getDataSeparator()
      The default implementation returns the comma string. Can be overridden.
      Returns:
      data separator
    • getArrayElementPerLine

      public int getArrayElementPerLine()
      The default implementation returns 10. Can be overridden.
      Returns:
      a number >= 1
    • formatDataDeclarator

      public void formatDataDeclarator(int size, CodeDocumentPart out)
      Format a data declarator.
      Parameters:
      size - data size in bytes
      out - output document part
    • getProcedureDefinitionStart

      public String getProcedureDefinitionStart()
      Get the procedure definition start marker.
      Returns:
      procedure start marker
    • getProcedureDefinitionEnd

      public String getProcedureDefinitionEnd()
      Get the procedure definition end marker.
      Returns:
      procedure end marker
    • getLabelPrefix

      public String getLabelPrefix()
      Get the assembly label prefix. Can be overridden.
      Returns:
      label prefix
    • getLabelSuffix

      public String getLabelSuffix()
      Can be overridden.
      Returns:
      label suffix
    • getOperandSeparator

      public String getOperandSeparator()
      Can be overridden.
      Returns:
      operand separator
    • getMemoryAccessSizeInfo

      public String getMemoryAccessSizeInfo(InsnType insn, IInstructionOperandSized opnd)
      Can be overridden.
      Parameters:
      insn - instruction
      opnd - operand
      Returns:
      memory access size text
    • getMemoryAccessPrefix

      public String getMemoryAccessPrefix()
      The default implementation returns the opening square bracket. Can be overridden.
      Returns:
      memory access prefix
    • getMemoryAccessSuffix

      public String getMemoryAccessSuffix()
      The default implementation returns the closing square bracket. Can be overridden.
      Returns:
      memory access suffix
    • getMemoryAccessSegmentInfo

      public String getMemoryAccessSegmentInfo(InsnType insn, IInstructionOperand opnd)
      The default implementation returns the empty string. Can be overridden.
      Parameters:
      insn - instruction
      opnd - operand
      Returns:
      memory access segment text
    • getInlineCommentString

      public String getInlineCommentString()
      The default implementation returns a semi-column string. Can be overridden.
      Returns:
      inline comment marker
    • getMultiLineCommentBegin

      public String getMultiLineCommentBegin()
      The default implementation returns "slash-*". Can be overridden.
      Returns:
      multiline comment opening marker
    • getMultiLineCommentEnd

      public String getMultiLineCommentEnd()
      The default implementation returns "*-slash". Can be overridden.
      Returns:
      multiline comment closing marker
    • getRegisterName

      public String getRegisterName(long registerIdentifier)
      The default implementation returns "rX". Should be overridden.
      Parameters:
      registerIdentifier - register identifier
      Returns:
      a register name, never null (on error, the implementation should return a default safe string)
    • generateHeader

      public String generateHeader()
      Generate the disassembly header. The default implementation returns the boilerplate line "Code Disassembly".
      Returns:
      disassembly header
    • generateExtraSegmentHeader

      public String generateExtraSegmentHeader(ISegmentInformation segment)
      Generate an optional extra segment header.
      Parameters:
      segment - segment information
      Returns:
      extra segment header, or null
    • generateExtraSectionHeader

      public String generateExtraSectionHeader(ISegmentInformation segment)
      Generate an optional extra section header.
      Parameters:
      segment - section information
      Returns:
      extra section header, or null
    • generateExtraMethodComment

      public String generateExtraMethodComment(long address)
      Generate an optional string that will be prepended to a function header comment. The default implementation returns null.
      Parameters:
      address - function address
      Returns:
      extra method comment, or null
    • generateExtraComment

      public CharSequence generateExtraComment(long insnAddress, InsnType instruction)
      Generate an optional string that will be displayed at the line instruction. The default implementation returns null.
      Parameters:
      insnAddress - instruction address
      instruction - instruction. Can be null if no instruction is defined at this address.
      Returns:
      extra comment, or null
    • formatInstruction

      public void formatInstruction(long address, InsnType insn, CodeDocumentPart out)
      Displays only pure assembly code: Mnemonic and Operands, without address and bytecode.
      Parameters:
      address - instruction address
      insn - instruction
      out - output document part
    • formatPrefix

      protected int formatPrefix(long address, InsnType insn, CodeDocumentPart out)
      Format an instruction prefix.
      Parameters:
      address - instruction address
      insn - instruction
      out - output document part
      Returns:
      number of formatted characters
    • formatMnemonic

      protected void formatMnemonic(long address, InsnType insn, boolean prependPrefix, CodeDocumentPart out)
      Format an instruction mnemonic.
      Parameters:
      address - instruction address
      insn - instruction
      prependPrefix - if true, prepend the instruction prefix
      out - output document part
    • generateMnemonic

      protected String generateMnemonic(long address, InsnType insn)
      Generate an instruction mnemonic.
      Parameters:
      address - instruction address
      insn - instruction
      Returns:
      mnemonic
    • setMnemonicRightPaddingLength

      public final void setMnemonicRightPaddingLength(int length)
      Set the mnemonic right padding length.
      Parameters:
      length - padding length
    • getMnemonicRightPaddingLength

      public final int getMnemonicRightPaddingLength()
      Get the mnemonic right padding length.
      Returns:
      padding length
    • getCountOfFormattedOperands

      protected int getCountOfFormattedOperands(InsnType insn)
      Get the number of operands to format.
      Parameters:
      insn - instruction
      Returns:
      operand count
    • formatOperands

      protected void formatOperands(long address, InsnType insn, CodeDocumentPart out)
      Format instruction operands.
      Parameters:
      address - instruction address
      insn - instruction
      out - output document part
    • formatOperand

      public void formatOperand(long address, InsnType insn, IInstructionOperand opnd, int opndIndexGlobal, int opndDepth, CodeDocumentPart out)
      Generic operand formatter. Can be overridden, although not recommended. If the processor uses custom instructions, that is, instructions that are neither IInstructionOperandGeneric or IInstructionOperandCMA, the processor should override this method.
      Parameters:
      address - instruction address
      insn - instruction
      opnd - operand
      opndIndexGlobal - global operand index
      opndDepth - operand nesting depth
      out - output document part
    • addPrefix

      protected final void addPrefix(InsnType insn, IInstructionOperandGeneric opnd, CodeDocumentPart out)
      Add an operand prefix.
      Parameters:
      insn - instruction
      opnd - operand
      out - output document part
    • addSuffix

      protected final void addSuffix(InsnType insn, IInstructionOperandGeneric opnd, CodeDocumentPart out)
      Add an operand suffix.
      Parameters:
      insn - instruction
      opnd - operand
      out - output document part
    • formatImm

      public final void formatImm(long address, IInstructionOperandSized opnd, long value, int opndIndexGlobal, CodeDocumentPart out)
      Format an immediate operand value.
      Parameters:
      address - instruction address
      opnd - operand
      value - immediate value
      opndIndexGlobal - global operand index
      out - output document part
    • formatAddress

      public final void formatAddress(long address, CodeDocumentPart out)
      Format an address.
      Parameters:
      address - address to format
      out - output document part
    • formatRelativeAddress

      public final void formatRelativeAddress(IInstructionOperandGeneric opnd, long address, CodeDocumentPart out)
      Format a relative address.
      Parameters:
      opnd - operand
      address - address to format
      out - output document part
    • formatAddress

      public void formatAddress(long address, IInstructionOperandGeneric opnd, CodeDocumentPart out)
      Format an address regarding getDefaultAddressFormatter()
      Parameters:
      address - address to represent
      opnd - optional; IFF address is a relative address
      out - output document part
    • formatRegister

      protected void formatRegister(long regValue, CodeDocumentPart out)
      Format a register.
      Parameters:
      regValue - register value
      out - output document part
    • getDefaultNumberFormatter

      public NumberFormatter getDefaultNumberFormatter()
      Get the default number formatter.
      Returns:
      default number formatter
    • getNumberFormatter

      public NumberFormatter getNumberFormatter(INativeDataItem item)
      Get a number formatter for a data item.
      Parameters:
      item - data item
      Returns:
      number formatter
    • getNumberFormatter

      public NumberFormatter getNumberFormatter(IInstructionOperand opnd, boolean createIfNone)
      Get a number formatter for an operand.
      Parameters:
      opnd - operand
      createIfNone - if true, create a formatter if none exists
      Returns:
      number formatter, or null
    • getDefaultAddressFormatter

      public AddressFormatter getDefaultAddressFormatter()
      Get the default address formatter.
      Returns:
      default address formatter
    • getBestClassIdForAddress

      public ItemClassIdentifiers getBestClassIdForAddress(long address)
      Get the best item class identifier for an address.
      Parameters:
      address - address
      Returns:
      item class identifier
    • createItemIdForMnemonic

      public final long createItemIdForMnemonic(InsnType insn)
      Create an item identifier for a mnemonic.
      Parameters:
      insn - instruction
      Returns:
      item identifier
    • createItemIdForAddress

      public final long createItemIdForAddress(long address)
      Create an item identifier for an address.
      Parameters:
      address - address
      Returns:
      item identifier
    • createItemIdForImmediate

      public final long createItemIdForImmediate(long insnAddress, int opndIndexGlobal)
      Create an item identifier for an immediate.
      Parameters:
      insnAddress - instruction address
      opndIndexGlobal - global operand index
      Returns:
      item identifier
    • createItemIdForDefaultRegister

      public long createItemIdForDefaultRegister(long regCode)
      Default method to retrieve item id of a register. By default, Bitsize and bitstart are ignored.
      Parameters:
      regCode - Code Register, see RegisterUtil for details
      Returns:
      createItemIdForRegister with filtered register based on pure id.
    • createItemIdForRegister

      public final long createItemIdForRegister(long regCode)
      Create an item identifier for a register.
      Parameters:
      regCode - register code
      Returns:
      item identifier
    • setDoNotAttemptImmediateToAddressResolution

      public void setDoNotAttemptImmediateToAddressResolution(boolean doNotResolveImmAsAddr)
      Set whether immediate-to-address resolution is disabled.
      Parameters:
      doNotResolveImmAsAddr - true to disable immediate-to-address resolution
    • isDoNotAttemptImmediateToAddressResolution

      public boolean isDoNotAttemptImmediateToAddressResolution()
      Determine whether immediate-to-address resolution is disabled.
      Returns:
      true if immediate-to-address resolution is disabled
    • getCachedBooleanProperty

      protected boolean getCachedBooleanProperty(String propname)
      Convenience method to retrieve and cache (for 5 second) a code unit's property.
      Parameters:
      propname - code unit's property name
      Returns:
      the property value (on failure: false)
    • getCachedIntegerProperty

      protected int getCachedIntegerProperty(String propname)
      Convenience method to retrieve and cache (for 5 second) a code unit's property.
      Parameters:
      propname - code unit's property name
      Returns:
      the property value (on failure: 0)
    • getCachedStringProperty

      protected String getCachedStringProperty(String propname)
      Convenience method to retrieve and cache (for 5 second) a code unit's property.
      Parameters:
      propname - code unit's property name
      Returns:
      the property value (on failure: the empty string)