Enum Class SleighProgramCompiler

java.lang.Object
java.lang.Enum<SleighProgramCompiler>
ghidra.pcode.exec.SleighProgramCompiler
All Implemented Interfaces:
Serializable, Comparable<SleighProgramCompiler>, Constable

public enum SleighProgramCompiler extends Enum<SleighProgramCompiler>
Methods for compiling p-code programs for various purposes

Depending on the purpose, special provisions may be necessary around the execution of the resulting program. Many utility methods are declared public here because they, well, they have utility. The main public methods of this class, however, all start with compile....

  • Field Details

  • Method Details

    • values

      public static SleighProgramCompiler[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static SleighProgramCompiler valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • createParser

      public static PcodeParser createParser(SleighLanguage language)
      Create a p-code parser for the given language
      Parameters:
      language - the language
      Returns:
      a parser
    • compileTemplate

      public static ConstructTpl compileTemplate(Language language, PcodeParser parser, String sourceName, String source)
      Compile the given source into a p-code template
      Parameters:
      language - the language
      parser - the parser
      sourceName - the name of the program, for error diagnostics
      source - the Sleigh source
      Returns:
      the constructor template
      See Also:
    • buildOps

      public static List<PcodeOp> buildOps(Language language, ConstructTpl template) throws UnknownInstructionException, MemoryAccessException, IOException
      Construct a list of p-code ops from the given template
      Parameters:
      language - the language generating the template and p-code
      template - the template
      Returns:
      the list of p-code ops
      Throws:
      UnknownInstructionException - in case of crossbuilds, the target instruction is unknown
      MemoryAccessException - in case of crossbuilds, the target address cannot be accessed
      IOException - for errors in during emitting
    • addParserSymbols

      protected static void addParserSymbols(PcodeParser parser, Map<Integer,ghidra.pcodeCPort.slghsymbol.UserOpSymbol> symbols)
      Add extra user-op symbols to the parser's table

      The map cannot contain symbols whose user-op indices are already defined by the language.

      Parameters:
      parser - the parser to modify
      symbols - the map of extra symbols
    • addNilSymbol

      protected static ghidra.pcodeCPort.slghsymbol.VarnodeSymbol addNilSymbol(PcodeParser parser)
      Add a symbol for unwanted result

      This is basically a hack to avoid NPEs when no output varnode is given.

      Parameters:
      parser - the parser to add the symbol to
      Returns:
      the nil symbol
    • constructProgram

      public static <T extends PcodeProgram> T constructProgram(SleighProgramCompiler.PcodeProgramConstructor<T> ctor, SleighLanguage language, ConstructTpl template, Map<Integer,ghidra.pcodeCPort.slghsymbol.UserOpSymbol> libSyms)
      Invoke the given constructor with the given template and library symbols
      Type Parameters:
      T - the type of the p-code program
      Parameters:
      ctor - the constructor, often a method reference to ::new
      language - the language producing the p-code
      template - the p-code constructor template
      libSyms - the map of symbols by userop ID
      Returns:
      the p-code program
    • compileProgram

      public static PcodeProgram compileProgram(PcodeParser parser, SleighLanguage language, String sourceName, String source, PcodeUseropLibrary<?> library)
      Compile the given Sleigh source into a simple p-code program with the given parser

      This is suitable for modifying program state using Sleigh statements. Most likely, in scripting, or perhaps in a Sleigh repl. The library given during compilation must match the library given for execution, at least in its binding of userop IDs to symbols.

      Parameters:
      parser - the parser to use
      language - the language of the target p-code machine
      sourceName - a diagnostic name for the Sleigh source
      source - the Sleigh source
      library - the userop library or stub library for binding userop symbols
      Returns:
      the compiled p-code program
    • compileProgram

      public static PcodeProgram compileProgram(SleighLanguage language, String sourceName, String source, PcodeUseropLibrary<?> library)
      Compile the given Sleigh source into a simple p-code program
      See Also:
    • compileExpression

      public static PcodeExpression compileExpression(PcodeParser parser, SleighLanguage language, String expression)
      Compile the given Sleigh expression into a p-code program that can evaluate it, using the given parser

      TODO: Currently, expressions cannot be compiled for a user-supplied userop library. The evaluator p-code program uses its own library as a means of capturing the result; however, userop libraries are easily composed. It should be easy to add that feature if needed.

      Parameters:
      language - the languge of the target p-code machine
      expression - the Sleigh expression to be evaluated
      Returns:
      a p-code program whose PcodeExpression.evaluate(PcodeExecutor) method will evaluate the expression on the given executor and its state.
    • compileExpression

      public static PcodeExpression compileExpression(SleighLanguage language, String expression)
      Compile the given Sleigh expression into a p-code program that can evaluate it
      See Also:
    • paramSym

      public static ghidra.pcodeCPort.slghsymbol.VarnodeSymbol paramSym(Language language, ghidra.pcodeCPort.sleighbase.SleighBase sleigh, String opName, String paramName, Varnode arg)
      Generate a Sleigh symbol for context when compiling a userop definition
      Parameters:
      language - the language of the target p-code machine
      sleigh - a means of translating address spaces between execution and compilation contexts
      opName - a diagnostic name for the userop in which this parameter applies
      paramName - the symbol name for the parameter
      arg - the varnode to bind to the parameter symbol
      Returns:
      the named Sleigh symbol bound to the given varnode
    • compileUserop

      public static PcodeProgram compileUserop(SleighLanguage language, String opName, List<String> params, String source, PcodeUseropLibrary<?> library, List<Varnode> args)
      Compile the definition of a p-code userop from Sleigh source into a p-code program

      TODO: Defining a userop from Sleigh source is currently a bit of a hack. It would be nice if there were a formalization of Sleigh/p-code subroutines. At the moment, the control flow for subroutines is handled out of band, which actually works fairly well. However, parameter passing and returning results is not well defined. The current solution is to alias the parameters to their arguments, implementing a pass-by-reference scheme. Similarly, the output variable is aliased to the symbol named SleighPcodeUseropDefinition.OUT_SYMBOL_NAME, which could be problematic if no output variable is given. In this setup, the use of temporary variables is tenuous, since no provision is made to ensure a subroutine's allocation of temporary variables do not collide with those of callers lower in the stack. This could be partly resolved by creating a fresh unique space for each invocation, but then it becomes necessary to copy values from the caller's to the callee's. If we're strict about parameters being inputs, this is straightforward. If parameters can be used to communicate results, then we may need parameter attributes to indicate in, out, or inout. Of course, having a separate unique space per invocation implies the executor state can't simply have one unique space. Likely, the PcodeFrame would come to own its own unique space, but the PcodeExecutorState should probably still manufacture it.

      Parameters:
      language - the language of the target p-code machine
      opName - the name of the userop (used only for diagnostics here)
      params - the names of parameters in order. Index 0 names the output symbol, probably SleighPcodeUseropDefinition.OUT_SYMBOL_NAME
      source - the Sleigh source
      library - the userop library or stub library for binding userop symbols
      args - the varnode arguments in order. Index 0 is the output varnode.
      Returns:
      a p-code program that implements the userop for the given arguments