Ghidra is a software reverse engineering (SRE) framework
Merge branch 'GP-0_caheckman_PR-6578_LukeSerne_decomp-rule-int-or-zero-compare' into GP-4859_RuleOrCompare
caheckman2ef83410be29
diff --cc Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex c7c2e45f7a,45718760ea..02faf8d6d1--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@@ -5521,7 -5518,7 +5521,8 @@@ void ActionDatabase::universalAction(Aractprop->addRule( new RuleOrMultiBool("analysis") );actprop->addRule( new RuleXorSwap("analysis") );actprop->addRule( new RuleLzcountShiftBool("analysis") );+ actprop->addRule( new RuleFloatSign("analysis") );+ actprop->addRule( new RuleOrCompare("analysis") );actprop->addRule( new RuleSubvarAnd("subvar") );actprop->addRule( new RuleSubvarSubpiece("subvar") );actprop->addRule( new RuleSplitFlow("subvar") );diff --cc Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 8237905051,51e3313ec9..35cad45220--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@@ -10453,93 -10434,62 +10453,152 @@@ int4 RuleLzcountShiftBool::applyOp(Pcodreturn 0;}+/// \class RuleFloatSign+/// \brief Convert floating-point \e sign bit manipulation into FLOAT_ABS or FLOAT_NEG+///+/// Transform floating-point specific operations+/// -- `x & 0x7fffffff => ABS(f)`+/// -- 'x ^ 0x80000000 => -f`+///+/// A Varnode is determined to be floating-point by participation in other floating-point operations,+/// not based on the data-type of the Varnode.+void RuleFloatSign::getOpList(vector<uint4> &oplist) const++{+ uint4 list[] = { CPUI_FLOAT_EQUAL, CPUI_FLOAT_NOTEQUAL, CPUI_FLOAT_LESS, CPUI_FLOAT_LESSEQUAL, CPUI_FLOAT_NAN,+ CPUI_FLOAT_ADD, CPUI_FLOAT_DIV, CPUI_FLOAT_MULT, CPUI_FLOAT_SUB, CPUI_FLOAT_NEG, CPUI_FLOAT_ABS,+ CPUI_FLOAT_SQRT, CPUI_FLOAT_FLOAT2FLOAT, CPUI_FLOAT_CEIL, CPUI_FLOAT_FLOOR, CPUI_FLOAT_ROUND,+ CPUI_FLOAT_INT2FLOAT, CPUI_FLOAT_TRUNC };+ oplist.insert(oplist.end(),list,list+18);+}++int4 RuleFloatSign::applyOp(PcodeOp *op,Funcdata &data)++{+ int4 res = 0;+ OpCode opc = op->code();+ if (opc != CPUI_FLOAT_INT2FLOAT) {+ Varnode *vn = op->getIn(0);+ if (vn->isWritten()) {+ PcodeOp *signOp = vn->getDef();+ OpCode resCode = TypeOp::floatSignManipulation(signOp);+ if (resCode != CPUI_MAX) {+ data.opRemoveInput(signOp, 1);+ data.opSetOpcode(signOp, resCode);+ res = 1;+ }+ }+ if (op->numInput() == 2) {+ vn = op->getIn(1);+ if (vn->isWritten()) {+ PcodeOp *signOp = vn->getDef();+ OpCode resCode = TypeOp::floatSignManipulation(signOp);+ if (resCode != CPUI_MAX) {+ data.opRemoveInput(signOp, 1);+ data.opSetOpcode(signOp, resCode);+ res = 1;+ }+ }+ }+ }+ if (op->isBoolOutput() || opc == CPUI_FLOAT_TRUNC)+ return res;+ list<PcodeOp *>::const_iterator iter;+ Varnode *outvn = op->getOut();+ for(iter=outvn->beginDescend();iter!=outvn->endDescend();++iter) {+ PcodeOp *readOp = *iter;+ OpCode resCode = TypeOp::floatSignManipulation(readOp);+ if (resCode != CPUI_MAX) {+ data.opRemoveInput(readOp, 1);+ data.opSetOpcode(readOp, resCode);+ res = 1;+ }+ }+ return res;+}++/// \class RuleFloatSignCleanup+/// \brief Convert floating-point \e sign bit manipulation into FLOAT_ABS or FLOAT_NEG+///+/// A Varnode is determined to be floating-point by examining its data-type.+void RuleFloatSignCleanup::getOpList(vector<uint4> &oplist) const++{+ oplist.push_back(CPUI_INT_AND);+ oplist.push_back(CPUI_INT_XOR);+}++int4 RuleFloatSignCleanup::applyOp(PcodeOp *op,Funcdata &data)++{+ if (op->getOut()->getType()->getMetatype() != TYPE_FLOAT) {+ return 0;+ }+ OpCode opc = TypeOp::floatSignManipulation(op);+ if (opc == CPUI_MAX)+ return 0;+ data.opRemoveInput(op, 1);+ data.opSetOpcode(op, opc);+ return 1;+}++ /// \class RuleOrCompare+ /// \brief Simplify INT_OR in comparisons with 0.++///+ /// `(V | W) == 0` => '(V == 0) && (W == 0)'+ /// `(V | W) != 0` => '(V != 0) || (W != 0)'+ void RuleOrCompare::getOpList(vector<uint4> &oplist) const++ {+ oplist.push_back(CPUI_INT_EQUAL);+ oplist.push_back(CPUI_INT_NOTEQUAL);+ }++ int4 RuleOrCompare::applyOp(PcodeOp *op,Funcdata &data)++ {+ // make sure the comparison is against 0+ if (! op->getIn(1)->constantMatch(0)) return 0;++ // make sure the other operand is an INT_OR+ PcodeOp *or_op = op->getIn(0)->getDef();+ if (or_op == (PcodeOp *)0) return 0;+ if (or_op->code() != CPUI_INT_OR) return 0;++ Varnode* V = or_op->getIn(0);+ Varnode* W = or_op->getIn(1);++ // make sure V and W are in SSA form+ if (V->isFree()) return 0;+ if (W->isFree()) return 0;++ // construct the new segment:+ // if the original condition was INT_EQUAL: BOOL_AND(INT_EQUAL(V, 0:|V|), INT_EQUAL(W, 0:|W|))+ // if the original condition was INT_NOTEQUAL: BOOL_OR(INT_NOTEQUAL(V, 0:|V|), INT_NOTEQUAL(W, 0:|W|))+ Varnode* zero_V = data.newConstant(V->getSize(), 0);+ Varnode* zero_W = data.newConstant(W->getSize(), 0);+ PcodeOp* eq_V = data.newOp(2, op->getAddr());+ data.opSetOpcode(eq_V, op->code());+ data.opSetInput(eq_V, V, 0);+ data.opSetInput(eq_V, zero_V, 1);+ PcodeOp* eq_W = data.newOp(2, op->getAddr());+ data.opSetOpcode(eq_W, op->code());+ data.opSetInput(eq_W, W, 0);+ data.opSetInput(eq_W, zero_W, 1);++ Varnode* eq_V_out = data.newUniqueOut(1, eq_V);+ Varnode* eq_W_out = data.newUniqueOut(1, eq_W);++ // make sure the comparisons' output is already defined+ data.opInsertBefore(eq_V, op);+ data.opInsertBefore(eq_W, op);++ // change the original INT_EQUAL into a BOOL_AND, and INT_NOTEQUAL becomes BOOL_OR+ data.opSetOpcode(op, op->code() == CPUI_INT_EQUAL ? CPUI_BOOL_AND : CPUI_BOOL_OR);+ data.opSetInput(op, eq_V_out, 0);+ data.opSetInput(op, eq_W_out, 1);++ return 1;+ }+} // End namespace ghidradiff --cc Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hhindex d83484e2e8,e8dec8cc7b..20fd92145c--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh@@@ -1644,27 -1643,16 +1644,38 @@@ publicvirtual int4 applyOp(PcodeOp *op,Funcdata &data);};+class RuleFloatSign : public Rule {+public:+ RuleFloatSign(const string &g) : Rule( g, 0, "floatsign") {} ///< Constructor+ virtual Rule *clone(const ActionGroupList &grouplist) const {+ if (!grouplist.contains(getGroup())) return (Rule *)0;+ return new RuleFloatSign(getGroup());+ }+ virtual void getOpList(vector<uint4> &oplist) const;+ virtual int4 applyOp(PcodeOp *op,Funcdata &data);+};++class RuleFloatSignCleanup : public Rule {+public:+ RuleFloatSignCleanup(const string &g) : Rule( g, 0, "floatsigncleanup") {} ///< Constructor+ virtual Rule *clone(const ActionGroupList &grouplist) const {+ if (!grouplist.contains(getGroup())) return (Rule *)0;+ return new RuleFloatSignCleanup(getGroup());+ }+ virtual void getOpList(vector<uint4> &oplist) const;+ virtual int4 applyOp(PcodeOp *op,Funcdata &data);+};++ class RuleOrCompare : public Rule {+ public:- RuleOrCompare(const string &g) : Rule( g, 0, "ruleorcompare") {} ///< Constructor++ RuleOrCompare(const string &g) : Rule( g, 0, "orcompare") {} ///< Constructor+ virtual Rule *clone(const ActionGroupList &grouplist) const {+ if (!grouplist.contains(getGroup())) return (Rule *)0;+ return new RuleOrCompare(getGroup());+ }+ virtual void getOpList(vector<uint4> &oplist) const;+ virtual int4 applyOp(PcodeOp *op,Funcdata &data);+ };+} // End namespace ghidra#endif