decompiler 1.0.0
Public Types | Public Member Functions | Static Public Attributes | Private Attributes | Friends | List of all members
ghidra::ParserContext Class Reference

Context maintained while parsing a single instruction. More...

#include <context.hh>

Public Types

enum  parse_state { uninitialized = 0 , disassembly = 1 , pcode = 2 }
 Possible states of the ParserContext. More...
 

Public Member Functions

 ParserContext (ContextCache *ccache, Translate *trans)
 Constructor. More...
 
 ~ParserContext (void)
 Destructor.
 
uint1 * getBuffer (void)
 Get bytes in the stream at the point this instruction is encoded.
 
void initialize (AddrSpace *spc, int4 maxstate=INITIAL_STATE_NUM)
 Preallocate nodes for constructor trees. More...
 
parse_state getParserState (void) const
 Get the overall state of the parse.
 
void setParserState (parse_state st)
 Update the overall parse state.
 
void deallocateState (ParserWalkerChange &walker)
 Clear any existing constructor tree. More...
 
void allocateOperand (int4 i, ParserWalkerChange &walker)
 Allocate a new child node in the constructor tree. More...
 
void setAddr (const Address &ad)
 Set the starting address of the instruction.
 
void setNaddr (const Address &ad)
 Set the ending address of the instruction.
 
void setCalladdr (const Address &ad)
 Set the address of the call being overridden.
 
void addCommit (TripleSymbol *sym, int4 num, uintm mask, bool flow, ConstructState *point)
 Add a formal SLEIGH context change command. More...
 
void clearCommits (void)
 Clear all context commits.
 
void applyCommits (void)
 Apply any pending commits to the context cache.
 
const Address & getAddr (void) const
 Get the starting address of the current instruction.
 
const Address & getNaddr (void) const
 Get the address of the next instruction.
 
const Address & getN2addr (void) const
 Get the address of the instruction after the next.
 
const Address & getDestAddr (void) const
 Get the destination address (inst_dest) for the overriden call.
 
const Address & getRefAddr (void) const
 Get the reference address (inst_ref) for the p-code snippet.
 
AddrSpace * getCurSpace (void) const
 Get the address space of the current instruction.
 
AddrSpace * getConstSpace (void) const
 Get the address space for constants.
 
uintm getInstructionBytes (int4 byteoff, int4 numbytes, uint4 off) const
 Get the specified instruction bytes. More...
 
uintm getContextBytes (int4 byteoff, int4 numbytes) const
 Get bytes from the local context. More...
 
uintm getInstructionBits (int4 startbit, int4 size, uint4 off) const
 Get the specific range of bits from the instruction stream. More...
 
uintm getContextBits (int4 startbit, int4 size) const
 Get the specific range of bits from the local context. More...
 
void setContextWord (int4 i, uintm val, uintm mask)
 Modify a context word, using given mask and value.
 
void loadContext (void)
 Pull context words associated with the starting address into the local array.
 
int4 getLength (void) const
 Get the length of the current instruction.
 
void setDelaySlot (int4 val)
 Set (the number of instruction bytes) in the delay slot.
 
int4 getDelaySlot (void) const
 Get the number of instruction bytes in the delay slot.
 
void expandState (int4 amount)
 Expand the number of available nodes for the constructor tree. More...
 

Static Public Attributes

static constexpr int4 MAX_DEPTH = 32
 Maximum subconstructor depth in a single instruction.
 
static constexpr int4 MAX_OPERAND = 20
 Maximum operands for a single constructor.
 
static constexpr int4 MAX_INSTRUCTION_LEN = 16
 Maximum number of bytes in a single instruction.
 
static constexpr int4 INITIAL_STATE_NUM = 64
 Recommended number of initial states.
 
static constexpr int4 STATE_GROWTH = 64
 

Private Attributes

Translate * translate
 The parent instruction parser.
 
parse_state parsestate
 Overall state of the parse.
 
AddrSpace * const_space
 Address space for constants.
 
uint1 buf [MAX_INSTRUCTION_LEN]
 Buffer of bytes in the instruction stream.
 
uintm * context
 Pointer to local context.
 
int4 contextsize
 Number of entries in local context array.
 
ContextCache * contcache
 Interface for getting/setting context.
 
vector< ContextSet > contextcommit
 Changes to SLEIGH context slated by this instruction.
 
Address addr
 Address of start of instruction.
 
Address naddr
 Address of next instruction.
 
Address n2addr
 Address of instruction after the next.
 
Address calladdr
 For injections, this is the address of the call being overridden.
 
vector< ConstructState * > state
 Available nodes for the constructor tree.
 
ConstructState * base_state
 Root node of the constructor tree.
 
int4 alloc
 Number of unallocated ConstructState nodes remaining.
 
int4 delayslot
 delayslot depth
 

Friends

class ParserWalker
 
class ParserWalkerChange
 

Detailed Description

Context maintained while parsing a single instruction.

This contains:

Member Enumeration Documentation

◆ parse_state

Possible states of the ParserContext.

Enumerator
uninitialized 

Instruction has not been parsed at all.

disassembly 

Instruction is parsed in preparation for disassembly.

pcode 

Instruction is parsed in preparation for generating p-code.

Constructor & Destructor Documentation

◆ ParserContext()

ghidra::ParserContext::ParserContext ( ContextCache *  ccache,
Translate *  trans 
)

Constructor.

Parameters
ccacheis the cache to use for formal context changes
transis the parent parser

References contcache, context, contextsize, ghidra::ContextDatabase::getContextSize(), ghidra::ContextCache::getDatabase(), parsestate, translate, and uninitialized.

Member Function Documentation

◆ addCommit()

void ghidra::ParserContext::addCommit ( TripleSymbol *  sym,
int4  num,
uintm  mask,
bool  flow,
ConstructState *  point 
)

Add a formal SLEIGH context change command.

Parameters
symis a symbol that resolves to the address where the setting takes effect
numis the index of the context word being affected
maskindicates the bits within the context word that are affected
flowis true if the context change flows forward from the point where it is set
pointis the parse point where the change was made

References context, contextcommit, ghidra::ContextSet::flow, ghidra::ContextSet::mask, ghidra::ContextSet::num, ghidra::ContextSet::point, ghidra::ContextSet::sym, and ghidra::ContextSet::value.

◆ allocateOperand()

void ghidra::ParserContext::allocateOperand ( int4  i,
ParserWalkerChange &  walker 
)
inline

Allocate a new child node in the constructor tree.

The next available node is linked to the current active node in the walker at the given operand index. The child node becomes the new active node for the walker. The underlying constructor is not yet assigned.

Parameters
iis the operand index of the new child
walkeris the walker for the parse

References alloc, ghidra::ParserWalker::breadcrumb, ghidra::ConstructState::ct, ghidra::ParserWalker::depth, expandState(), MAX_DEPTH, MAX_OPERAND, ghidra::ConstructState::parent, ghidra::ParserWalker::point, ghidra::ConstructState::resolve, state, and STATE_GROWTH.

Referenced by ghidra::Sleigh::resolve(), and ghidra::InjectPayloadSleigh::setupParameters().

◆ deallocateState()

void ghidra::ParserContext::deallocateState ( ParserWalkerChange &  walker)
inline

Clear any existing constructor tree.

The tree is reset to a single root node and the walker is prepared for a new parse

Parameters
walkeris the walker to initialize for a traversal

References alloc, ghidra::ParserWalker::baseState(), ghidra::ParserWalkerChange::context, and state.

Referenced by ghidra::InjectPayloadSleigh::inject(), ghidra::ExecutablePcodeSleigh::inject(), and ghidra::Sleigh::resolve().

◆ expandState()

void ghidra::ParserContext::expandState ( int4  amount)

Expand the number of available nodes for the constructor tree.

This can be called in the middle of a parse to accommodate larger constructor trees.

Parameters
amountis the number of additional nodes to add

References alloc, MAX_OPERAND, and state.

Referenced by allocateOperand().

◆ getContextBits()

uintm ghidra::ParserContext::getContextBits ( int4  startbit,
int4  size 
) const

Get the specific range of bits from the local context.

Parameters
startbitis the offset of the first bit
sizeis the number of bits to return
Returns
the requested range of bits (in the least significant positions and padded out with zero bits)

References context, and contextsize.

Referenced by ghidra::ParserWalker::getContextBits().

◆ getContextBytes()

uintm ghidra::ParserContext::getContextBytes ( int4  bytestart,
int4  size 
) const

Get bytes from the local context.

Parameters
bytestartis the offset of the first byte to grab
sizeis the number of bytes to grab
Returns
the context bytes in a packed value

References context, and contextsize.

Referenced by ghidra::ParserWalker::getContextBytes().

◆ getInstructionBits()

uintm ghidra::ParserContext::getInstructionBits ( int4  startbit,
int4  size,
uint4  off 
) const

Get the specific range of bits from the instruction stream.

Get bits from the instruction stream assuming big endian encoding.

Parameters
startbitis the offset of the first bit (within the instruction stream)
sizeis the number of bits to grab
offis the number of bytes in the instruction already read
Returns
the requested range of bits (in the least significant positions and padded out with zero bits)

References buf, and MAX_INSTRUCTION_LEN.

Referenced by ghidra::ParserWalker::getInstructionBits().

◆ getInstructionBytes()

uintm ghidra::ParserContext::getInstructionBytes ( int4  bytestart,
int4  size,
uint4  off 
) const

Get the specified instruction bytes.

Get bytes from the instruction stream into a packed value assuming a big endian encoding.

Parameters
bytestartis the number of bytes to skip
sizeis the number of bytes to pack
offis the number of bytes in the instruction already read
Returns
the packed bytes from the instruction

References buf, and MAX_INSTRUCTION_LEN.

Referenced by ghidra::ParserWalker::getInstructionBytes().

◆ initialize()

void ghidra::ParserContext::initialize ( AddrSpace *  spc,
int4  maxstate = INITIAL_STATE_NUM 
)

Preallocate nodes for constructor trees.

Parameters
spcis the address space used for constants
maxstateis the number of nodes to allocate (initially)

References base_state, const_space, MAX_OPERAND, and state.

Referenced by ghidra::DisassemblyCache::initialize(), and ghidra::PcodeInjectLibrarySleigh::parseInject().

Member Data Documentation

◆ STATE_GROWTH

constexpr int4 ghidra::ParserContext::STATE_GROWTH = 64
staticconstexpr

Number of states to add for each expansion

Referenced by allocateOperand().


The documentation for this class was generated from the following files: