decompiler  1.0.0
Public Member Functions | Static Public Member Functions | Static Private Member Functions | List of all members
ghidra::RuleDivOpt Class Reference

Convert INT_MULT and shift forms into INT_DIV or INT_SDIV. More...

Inheritance diagram for ghidra::RuleDivOpt:
ghidra::Rule

Public Member Functions

 RuleDivOpt (const string &g)
 Constructor.
 
virtual Ruleclone (const ActionGroupList &grouplist) const
 Clone the Rule. More...
 
virtual void getOpList (vector< uint4 > &oplist) const
 List of op codes this rule operates on. More...
 
virtual int4 applyOp (PcodeOp *op, Funcdata &data)
 Attempt to apply this Rule. More...
 
- Public Member Functions inherited from ghidra::Rule
 Rule (const string &g, uint4 fl, const string &nm)
 Construct given group, properties name. More...
 
virtual ~Rule (void)
 Destructor.
 
const string & getName (void) const
 Return the name of this Rule.
 
const string & getGroup (void) const
 Return the group this Rule belongs to.
 
uint4 getNumTests (void)
 Get number of attempted applications.
 
uint4 getNumApply (void)
 Get number of successful applications.
 
void setBreak (uint4 tp)
 Set a breakpoint on this Rule.
 
void clearBreak (uint4 tp)
 Clear a breakpoint on this Rule.
 
void clearBreakPoints (void)
 Clear all breakpoints on this Rule.
 
void turnOnWarnings (void)
 Enable warnings for this Rule.
 
void turnOffWarnings (void)
 Disable warnings for this Rule.
 
bool isDisabled (void) const
 Return true if this Rule is disabled.
 
void setDisable (void)
 Disable this Rule (within its pool)
 
void clearDisable (void)
 Enable this Rule (within its pool)
 
bool checkActionBreak (void)
 Check if an action breakpoint is turned on. More...
 
uint4 getBreakPoint (void) const
 Return breakpoint toggles.
 
virtual void reset (Funcdata &data)
 Reset this Rule. More...
 
virtual void resetStats (void)
 Reset Rule statistics. More...
 
virtual void printStatistics (ostream &s) const
 Print statistics for this Rule. More...
 

Static Public Member Functions

static VarnodefindForm (PcodeOp *op, int4 &n, uintb &y, int4 &xsize, OpCode &extopc)
 Check for INT_(S)RIGHT and/or SUBPIECE followed by INT_MULT. More...
 

Static Private Member Functions

static uintb calcDivisor (uintb n, uint8 y, int4 xsize)
 Calculate the divisor. More...
 
static void moveSignBitExtraction (Varnode *firstVn, Varnode *replaceVn, Funcdata &data)
 Replace sign-bit extractions from the first given Varnode with the second Varnode. More...
 
static bool checkFormOverlap (PcodeOp *op)
 If form rooted at given PcodeOp is superseded by an overlapping form. More...
 

Additional Inherited Members

- Public Types inherited from ghidra::Rule
enum  typeflags { type_disable = 1, rule_debug = 2, warnings_on = 4, warnings_given = 8 }
 Properties associated with a Rule. More...
 

Detailed Description

Convert INT_MULT and shift forms into INT_DIV or INT_SDIV.

The unsigned and signed variants are:

Member Function Documentation

◆ applyOp()

int4 ghidra::RuleDivOpt::applyOp ( PcodeOp op,
Funcdata data 
)
virtual

Attempt to apply this Rule.

This method contains the main logic for applying the Rule. It must use a given PcodeOp as the point at which the Rule applies. If it does apply, changes are made directly to the function and 1 (non-zero) is returned, otherwise 0 is returned.

Parameters
opis the given PcodeOp where the Rule may apply
datais the function to which to apply

Reimplemented from ghidra::Rule.

References ghidra::CPUI_INT_ADD, ghidra::CPUI_INT_DIV, ghidra::CPUI_INT_SDIV, ghidra::CPUI_INT_SEXT, ghidra::CPUI_INT_SRIGHT, ghidra::CPUI_INT_ZEXT, ghidra::CPUI_SUBPIECE, ghidra::AddTreeState::data, ghidra::PcodeOp::getAddr(), ghidra::PcodeOp::getOut(), ghidra::Varnode::getSize(), ghidra::Funcdata::newConstant(), ghidra::Funcdata::newOp(), ghidra::Funcdata::newUniqueOut(), ghidra::Funcdata::opInsertBefore(), ghidra::Funcdata::opSetInput(), and ghidra::Funcdata::opSetOpcode().

◆ calcDivisor()

uintb ghidra::RuleDivOpt::calcDivisor ( uintb  n,
uint8  y,
int4  xsize 
)
staticprivate

Calculate the divisor.

Given the multiplicative encoding y and the n, the power of 2, Compute:

divisor = 2^n / (y-1)

Do some additional checks on the parameters as an optimized encoding of a divisor.

Parameters
nis the power of 2
yis the multiplicative coefficient
xsizeis the maximum power of 2
Returns
the divisor or 0 if the checks fail

◆ checkFormOverlap()

bool ghidra::RuleDivOpt::checkFormOverlap ( PcodeOp op)
staticprivate

If form rooted at given PcodeOp is superseded by an overlapping form.

A form ending in a SUBPIECE, may be contained in a working form ending at the SUBPIECE followed by INT_SRIGHT. The containing form would supersede.

Parameters
opis the root of the form to check
Returns
true if it is (possibly) contained in a superseding form

References ghidra::Varnode::beginDescend(), ghidra::PcodeOp::code(), ghidra::CPUI_INT_RIGHT, ghidra::CPUI_INT_SRIGHT, ghidra::CPUI_SUBPIECE, ghidra::Varnode::endDescend(), ghidra::PcodeOp::getIn(), ghidra::PcodeOp::getOut(), and ghidra::Varnode::isConstant().

◆ clone()

virtual Rule* ghidra::RuleDivOpt::clone ( const ActionGroupList grouplist) const
inlinevirtual

Clone the Rule.

If this Rule is a member of one of the groups in the grouplist, this returns a clone of the Rule, otherwise NULL is returned.

Parameters
grouplistis the list of groups being cloned
Returns
the cloned Rule or NULL

Implements ghidra::Rule.

References ghidra::ActionGroupList::contains().

◆ findForm()

Varnode * ghidra::RuleDivOpt::findForm ( PcodeOp op,
int4 &  n,
uintb &  y,
int4 &  xsize,
OpCode extopc 
)
static

Check for INT_(S)RIGHT and/or SUBPIECE followed by INT_MULT.

Look for the forms:

  • sub(ext(X) * y,c) or
  • sub(ext(X) * y,c) >> n or
  • (ext(X) * y) >> n

Looks for truncation/multiplication consistent with an optimized division. The truncation can come as either a SUBPIECE operation and/or right shifts. The numerand and the amount it has been extended is discovered. The extension can be, but doesn't have to be, an explicit INT_ZEXT or INT_SEXT. If the form doesn't match NULL is returned. If the Varnode holding the extended numerand matches the final operand size, it is returned, otherwise the unextended numerand is returned. The total truncation, the multiplicative constant, the numerand size, and the extension type are all passed back.

Parameters
opis the root of the expression
nis the reference that will hold the total number of bits of truncation
ywill hold the multiplicative constant
xsizewill hold the number of (non-zero) bits in the numerand
extopcholds whether the extension is INT_ZEXT or INT_SEXT
Returns
the extended numerand if possible, or the unextended numerand, or NULL

References ghidra::PcodeOp::code(), ghidra::CPUI_INT_MULT, ghidra::CPUI_INT_RIGHT, ghidra::CPUI_INT_SEXT, ghidra::CPUI_INT_SRIGHT, ghidra::CPUI_INT_ZEXT, ghidra::CPUI_MAX, ghidra::CPUI_SUBPIECE, ghidra::Varnode::getDef(), ghidra::PcodeOp::getIn(), ghidra::Varnode::getNZMask(), ghidra::Varnode::getOffset(), ghidra::PcodeOp::getOut(), ghidra::Varnode::getSize(), ghidra::Varnode::isConstant(), ghidra::Varnode::isConstantExtended(), ghidra::Varnode::isFree(), and ghidra::Varnode::isWritten().

◆ getOpList()

void ghidra::RuleDivOpt::getOpList ( vector< uint4 > &  oplist) const
virtual

List of op codes this rule operates on.

Populate the given array with all possible OpCodes this Rule might apply to. By default, this method returns all possible OpCodes

Parameters
oplistis the array to populate

Reimplemented from ghidra::Rule.

References ghidra::CPUI_INT_RIGHT, ghidra::CPUI_INT_SRIGHT, and ghidra::CPUI_SUBPIECE.

◆ moveSignBitExtraction()

void ghidra::RuleDivOpt::moveSignBitExtraction ( Varnode firstVn,
Varnode replaceVn,
Funcdata data 
)
staticprivate

Replace sign-bit extractions from the first given Varnode with the second Varnode.

Look for either:

  • V >> 0x1f
  • V s>> 0x1f

Allow for the value to be COPYed around.

Parameters
firstVnis the first given Varnode
replaceVnis the Varnode to replace it with in each extraction
datais the function holding the Varnodes

References ghidra::Varnode::beginDescend(), ghidra::PcodeOp::code(), ghidra::CPUI_COPY, ghidra::CPUI_INT_AND, ghidra::CPUI_INT_RIGHT, ghidra::CPUI_INT_SRIGHT, ghidra::Varnode::endDescend(), ghidra::Varnode::getDef(), ghidra::PcodeOp::getIn(), ghidra::Varnode::getOffset(), ghidra::PcodeOp::getOut(), ghidra::Varnode::getSize(), ghidra::Varnode::isConstant(), ghidra::Varnode::isWritten(), and ghidra::Funcdata::opSetInput().


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