Package ghidra.pcode.emu.jit.gen.var

# Interface VarGen<V extends JitVar>

Type Parameters:
  `V` - the class of p-code variable node in the use-def graph

All Superinterfaces:
  `ValGen<V>`

All Known Subinterfaces:
  `LocalVarGen<V>`, `MemoryVarGen<V>`

All Known Implementing Classes:
  `DirectMemoryVarGen`, `InputVarGen`, `LocalOutVarGen`, `MemoryOutVarGen`, `MissingVarGen`

public interface VarGen<V extends JitVar> extends ValGen<V>
The bytecode generator for a specific use-def variable (operand) access

For a table of value types, their use-def types, their generator classes, and relevant read/write opcodes, see `JitVal`. This interface is an extension of the `JitVal` interface that allows writing. The only non-`JitVar` `JitVal` is `JitConstVal`. As such, most of the variable-access logic is actually contained here.

See Also:
  - `ValGen`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Interface
  Description
  `static final record`
  `VarGen.BlockTransition`
  A means to emit bytecode on transitions between `blocks`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static VarGen.BlockTransition`
  `computeBlockTransition(JitCodeGenerator gen, JitControlFlowModel.JitBlock from, JitControlFlowModel.JitBlock to)`
  Compute the retired and birthed varnodes for a transition between the given blocks.
  `static void`
  `generateBirthCode(JitCodeGenerator gen, Set<Varnode> toBirth, org.objectweb.asm.MethodVisitor rv)`
  For block transitions: emit bytecode that births (loads) variables from the `state` into their allocated JVM locals.
  `static void`
  `generateRetireCode(JitCodeGenerator gen, Set<Varnode> toRetire, org.objectweb.asm.MethodVisitor rv)`
  For block transitions: emit bytecode the retires (writes) variables into the `state` from their allocated JVM locals.
  `static void`
  `generateValInitCode(JitCodeGenerator gen, Varnode vn)`
  Emit bytecode necessary to support access to the given varnode
  `static void`
  `generateValReadCodeDirect(JitCodeGenerator gen, JitType type, Varnode vn, org.objectweb.asm.MethodVisitor rv)`
  Emit bytecode that loads the given varnode with the given p-code type from the `state` onto the JVM stack.
  `static JitType`
  `generateValReadCodeDirect(JitCodeGenerator gen, JitVarnodeVar v, JitTypeBehavior typeReq, org.objectweb.asm.MethodVisitor rv)`
  Emit bytecode that loads the given use-def variable from the `state` onto the JVM stack.
  `static void`
  `generateValWriteCodeDirect(JitCodeGenerator gen, JitType type, Varnode vn, org.objectweb.asm.MethodVisitor rv)`
  Emit bytecode that writes the given varnode with the given p-code type in the `state` from the JVM stack.
  `static void`
  `generateValWriteCodeDirect(JitCodeGenerator gen, JitVarnodeVar v, JitType type, org.objectweb.asm.MethodVisitor rv)`
  Emit bytecode that writes the given use-def variable in the `state` from the JVM stack.
  `void`
  `generateVarWriteCode(JitCodeGenerator gen, V v, JitType type, TypeConversions.Ext ext, org.objectweb.asm.MethodVisitor rv)`
  Write a value from the stack into the given variable
  `static <V extends JitVar>
  VarGen<V>`
  `lookup(V v)`
  Lookup the generator for a given p-code variable use-def node

  ### Methods inherited from interface ghidra.pcode.emu.jit.gen.var.ValGen

  `generateValInitCode, generateValReadCode`

- ## Method Details

  - ### lookup

    static <V extends JitVar> VarGen<V> lookup(V v)
    Lookup the generator for a given p-code variable use-def node
    Type Parameters:
      `V` - the class of the variable
    Parameters:
      `v` - the `JitVar` whose generator to look up
    Returns:
      the generator

  - ### generateValInitCode

    static void generateValInitCode(JitCodeGenerator gen, Varnode vn)
    Emit bytecode necessary to support access to the given varnode

    This applies to all varnode types: `memory`, `unique`, and `register`, but not `const`. For memory varnodes, we need to pre-fetch the byte arrays backing their pages, so we can access them at the translation site. For unique and register varnodes, we also need to pre-fetch the byte arrays backing their pages, so we can birth and retire them at `transitions`. Technically, the methods for generating the read and write code will already call `JitCodeGenerator.requestFieldForArrDirect(Address)`; however, we'd like to ensure the fields appear in the classfile in a comprehensible order, so we have the generator iterate the variables in address order and invoke this method, where we make the request first.

    Parameters:
      `gen` - the code generator
      `vn` - the varnode

  - ### generateValReadCodeDirect

    static void generateValReadCodeDirect(JitCodeGenerator gen, JitType type, Varnode vn, org.objectweb.asm.MethodVisitor rv)
    Emit bytecode that loads the given varnode with the given p-code type from the `state` onto the JVM stack.

    This is used for direct memory accesses and for register/unique scope transitions. The JVM type of the stack variable is determined by the `type` argument.

    Parameters:
      `gen` - the code generator
      `type` - the p-code type of the variable
      `vn` - the varnode to read from the state
      `rv` - the visitor for the `run` method

  - ### generateValReadCodeDirect

    static JitType generateValReadCodeDirect(JitCodeGenerator gen, JitVarnodeVar v, JitTypeBehavior typeReq, org.objectweb.asm.MethodVisitor rv)
    Emit bytecode that loads the given use-def variable from the `state` onto the JVM stack.

    The actual type is determined by resolving the `typeReq` argument against the given variable. Since the variable is being loaded directly from the state, which is just raw bytes/bits, we ignore the "assigned" type and convert directly the type required by the operand.

    Parameters:
      `gen` - the code generator
      `v` - the use-def variable node
      `typeReq` - the type (behavior) required by the operand.
      `rv` - the visitor for the `run` method
    Returns:
      the resulting p-code type (which also describes the JVM type) of the value on the JVM stack

  - ### generateValWriteCodeDirect

    static void generateValWriteCodeDirect(JitCodeGenerator gen, JitType type, Varnode vn, org.objectweb.asm.MethodVisitor rv)
    Emit bytecode that writes the given varnode with the given p-code type in the `state` from the JVM stack.

    This is used for direct memory accesses and for register/unique scope transitions. The expected JVM type of the stack variable is described by the `type` argument.

    Parameters:
      `gen` - the code generator
      `type` - the p-code type of the variable
      `vn` - the varnode to write in the state
      `rv` - the visitor for the `run` method

  - ### generateValWriteCodeDirect

    static void generateValWriteCodeDirect(JitCodeGenerator gen, JitVarnodeVar v, JitType type, org.objectweb.asm.MethodVisitor rv)
    Emit bytecode that writes the given use-def variable in the `state` from the JVM stack.

    The expected type is given by the `type` argument. Since the variable is being written directly into the state, which is just raw bytes/bits, we ignore the "assigned" type and convert using the given type instead.

    Parameters:
      `gen` - the code generator
      `v` - the use-def variable node
      `type` - the p-code type of the value on the stack, as required by the operand
      `rv` - the visitor for the `run` method

  - ### generateBirthCode

    static void generateBirthCode(JitCodeGenerator gen, Set<Varnode> toBirth, org.objectweb.asm.MethodVisitor rv)
    For block transitions: emit bytecode that births (loads) variables from the `state` into their allocated JVM locals.
    Parameters:
      `gen` - the code generator
      `toBirth` - the set of varnodes to load
      `rv` - the visitor for the `run` method

  - ### generateRetireCode

    static void generateRetireCode(JitCodeGenerator gen, Set<Varnode> toRetire, org.objectweb.asm.MethodVisitor rv)
    For block transitions: emit bytecode the retires (writes) variables into the `state` from their allocated JVM locals.
    Parameters:
      `gen` - the code generator
      `toRetire` - the set of varnodes to write
      `rv` - the visitor for the `run` method

  - ### computeBlockTransition

    static VarGen.BlockTransition computeBlockTransition(JitCodeGenerator gen, JitControlFlowModel.JitBlock from, JitControlFlowModel.JitBlock to)
    Compute the retired and birthed varnodes for a transition between the given blocks.

    Either block may be `null` to indicate entering or leaving the passage. Additionally, the `to` block should be `null` when generating transitions around a hazard.

    Parameters:
      `gen` - the code generator
      `from` - the block control flow is leaving (whether by branch or fall through)
      `to` - the block control flow is entering
    Returns:
      the means of generating bytecode at the transition

  - ### generateVarWriteCode

    void generateVarWriteCode(JitCodeGenerator gen, V v, JitType type, TypeConversions.Ext ext, org.objectweb.asm.MethodVisitor rv)
    Write a value from the stack into the given variable
    Parameters:
      `gen` - the code generator
      `v` - the variable to write
      `type` - the p-code type (which also determines the expected JVM type) of the value on the stack
      `ext` - the kind of extension to apply when adjusting from varnode size to JVM size
      `rv` - the visitor for the `run` method