Ghidra is a software reverse engineering (SRE) framework
Hide implied extensions for arithmetic ops
caheckman8860979785bf
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/options.ccindex f0b2a4f460..d998c5ec7f 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/options.cc@@ -394,6 +394,21 @@ string OptionNoCastPrinting::apply(Architecture *glb,const string &p1,const strireturn "No cast printing turned "+prop;}+/// \class OptionHideExtensions+/// \brief Toggle whether implied extensions (ZEXT or SEXT) are printed+string OptionHideExtensions::apply(Architecture *glb,const string &p1,const string &p2,const string &p3) const++{+ bool val = onOrOff(p1);+ PrintC *lng = dynamic_cast<PrintC *>(glb->print);+ if (lng == (PrintC *)0)+ return "Can only toggle extension hiding for C language";+ lng->setHideImpliedExts(val);+ string prop;+ prop = val ? "on" : "off";+ return "Implied extension hiding turned "+prop;+}+/// \class OptionMaxLineWidth/// \brief Set the maximum number of characters per decompiled line///diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/options.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/options.hhindex 372d6eb83e..ad166b1289 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/options.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/options.hh@@ -144,6 +144,12 @@ public:virtual string apply(Architecture *glb,const string &p1,const string &p2,const string &p3) const;};+class OptionHideExtensions : public ArchOption {+public:+ OptionHideExtensions(void) { name="hideextensions"; } ///< Constructor+ virtual string apply(Architecture *glb,const string &p1,const string &p2,const string &p3) const;+};+class OptionMaxLineWidth : public ArchOption {public:OptionMaxLineWidth(void) { name = "maxlinewidth"; } ///< Constructordiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.ccindex 218a794a8f..ecb93d8d70 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.cc@@ -18,6 +18,7 @@// Operator tokens for expressions// token #in prec assoc optype space bump+OpToken PrintC::hidden = { "", 1, 70, false, OpToken::hiddenfunction, 0, 0, (OpToken *)0 };OpToken PrintC::scope = { "::", 2, 70, true, OpToken::binary, 0, 0, (OpToken *)0 };OpToken PrintC::object_member = { ".", 2, 66, true, OpToken::binary, 0, 0, (OpToken *)0 };OpToken PrintC::pointer_member = { "->", 2, 66, true, OpToken::binary, 0, 0, (OpToken *)0 };@@ -98,6 +99,7 @@ PrintC::PrintC(Architecture *g,const string &nm) : PrintLanguage(g,nm)option_convention = true;option_nocasts = false;option_unplaced = false;+ option_hide_exts = true;nullToken = "NULL";// Set the flip tokens@@ -319,6 +321,21 @@ void PrintC::opTypeCast(const PcodeOp *op)pushVnImplied(op->getIn(0),op,mods);}+/// The syntax represents the given op using a function with one input,+/// where the function name is not printed. The input expression is simply printed+/// without adornment inside the larger expression, with one minor difference.+/// The hidden operator protects against confusing evaluation order between+/// the operators inside and outside the hidden function. If both the inside+/// and outside operators are the same associative token, the hidden token+/// makes sure the inner expression is surrounded with parentheses.+/// \param op is the given PcodeOp+void PrintC::opHiddenFunc(const PcodeOp *op)++{+ pushOp(&hidden,op);+ pushVnImplied(op->getIn(0),op,mods);+}+void PrintC::opCopy(const PcodeOp *op){@@ -579,7 +596,7 @@ void PrintC::opIntZext(const PcodeOp *op){if (castStrategy->isZextCast(op->getOut()->getHigh()->getType(),op->getIn(0)->getHigh()->getType())) {if (isExtensionCastImplied(op))- pushVnImplied(op->getIn(0),op,mods);+ opHiddenFunc(op);elseopTypeCast(op);}@@ -592,7 +609,7 @@ void PrintC::opIntSext(const PcodeOp *op){if (castStrategy->isSextCast(op->getOut()->getHigh()->getType(),op->getIn(0)->getHigh()->getType())) {if (isExtensionCastImplied(op))- pushVnImplied(op->getIn(0),op,mods);+ opHiddenFunc(op);elseopTypeCast(op);}@@ -1265,6 +1282,8 @@ bool PrintC::printCharacterConstant(ostream &s,const Address &addr,int4 charsizebool PrintC::isExtensionCastImplied(const PcodeOp *op) const{+ if (!option_hide_exts)+ return false; // If hiding extensions is not on, we must always print extensionconst Varnode *outVn = op->getOut();if (outVn->isExplicit()) {@@ -1272,9 +1291,32 @@ bool PrintC::isExtensionCastImplied(const PcodeOp *op) constelse {PcodeOp *expOp = outVn->loneDescend();if (expOp != (PcodeOp *)0) {- OpCode opc = expOp->code();- if (opc == CPUI_PTRADD)- return true;+ type_metatype metatype = outVn->getHigh()->getType()->getMetatype();+ Varnode *otherVn;+ int4 slot;+ switch(expOp->code()) {+ case CPUI_PTRADD:+ return true;+ case CPUI_INT_ADD:+ case CPUI_INT_SUB:+ case CPUI_INT_MULT:+ case CPUI_INT_DIV:+ case CPUI_INT_AND:+ case CPUI_INT_OR:+ case CPUI_INT_XOR:+ case CPUI_INT_LESS:+ case CPUI_INT_LESSEQUAL:+ case CPUI_INT_SLESS:+ case CPUI_INT_SLESSEQUAL:+ slot = expOp->getSlot(outVn);+ otherVn = expOp->getIn(1-slot);+ // Check if the expression involves an explicit variable of the right integer type+ if (otherVn->isExplicit() && otherVn->getHigh()->getType()->getMetatype() == metatype)+ return true;+ break;+ default:+ break;+ }}}return false;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.hhindex c27e9ff495..65f67031ce 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printc.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printc.hh@@ -62,6 +62,7 @@ struct PartialSymbolEntry {/// - etc.class PrintC : public PrintLanguage {protected:+ static OpToken hidden; ///< Hidden functional (that may force parentheses)static OpToken scope; ///< The sub-scope/namespace operatorstatic OpToken object_member; ///< The \e member operatorstatic OpToken pointer_member; ///< The \e points \e to \e member operator@@ -116,6 +117,7 @@ protected:bool option_convention; ///< Set to \b true if we should print calling conventionbool option_nocasts; ///< Don't print a cast if \b truebool option_unplaced; ///< Set to \b true if we should display unplaced comments+ bool option_hide_exts; ///< Set to \b true if we should hide implied extension operationsstring nullToken; ///< Token to use for 'null'CommentSorter commsorter; ///< Container/organizer for comments in the current function@@ -154,6 +156,7 @@ protected:void emitCommentFuncHeader(const Funcdata *fd); ///< Emit comments in the given function's headervoid opFunc(const PcodeOp *op); ///< Push a \e functional expression based on the given p-code op to the RPN stackvoid opTypeCast(const PcodeOp *op); ///< Push the given p-code op using type-cast syntax to the RPN stack+ void opHiddenFunc(const PcodeOp *op); ///< Push the given p-code op as a hidden tokenstatic bool hasCharTerminator(uint1 *buffer,int4 size,int4 charsize);bool printCharacterConstant(ostream &s,const Address &addr,int4 charsize) const;bool isExtensionCastImplied(const PcodeOp *op) const;@@ -196,6 +199,7 @@ public:void setCStyleComments(void) { setCommentDelimeter("/* "," */",false); } ///< Set c-style "/* */" comment delimitersvoid setCPlusPlusStyleComments(void) { setCommentDelimeter("// ","",true); } ///< Set c++-style "//" comment delimitersvoid setDisplayUnplaced(bool val) { option_unplaced = val; } ///< Toggle whether \e unplaced comments are displayed in the header+ void setHideImpliedExts(bool val) { option_hide_exts = val; } ///< Toggle whether implied extensions are hiddenvirtual ~PrintC(void) {}virtual void adjustTypeOperators(void);virtual void setCommentStyle(const string &nm);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.ccindex 94fe032e51..d91c59a0c9 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc@@ -52,55 +52,6 @@ PrintLanguageCapability *PrintLanguageCapability::findCapability(const string &nreturn (PrintLanguageCapability *)0;}-/// \brief Determine if the given token should be emitted in its own parenthetic expression-///-/// This token is being emitted. Check if its input expression, ending with the given-/// operator token, needs to be surrounded by parentheses to convey the proper meaning.-/// \param op2 is the input token to \b this operator-/// \param stage is the stage of \b this operator currently being printed-/// \return \b true if \b op2 (as input to \b this) should be parenthesized-bool OpToken::parentheses(const OpToken &op2,int4 stage) const--{- switch(type) {- case space:- case binary:- if (precedence > op2.precedence) return true;- if (precedence < op2.precedence) return false;- if (associative && (this == &op2)) return false;- // If operators are adjacent to each other, the- // operator printed first must be evaluated first- // In this case op2 must be evaluated first, so we- // check if it is printed first (in first stage of binary)- if ((op2.type==postsurround)&&(stage==0)) return false;- return true;- case unary_prefix:- if (precedence > op2.precedence) return true;- if (precedence < op2.precedence) return false;- // if (associative && (this == &op2)) return false;- if ((op2.type==unary_prefix)||(op2.type==presurround)) return false;- return true;- case postsurround:- if (stage==1) return false; // Inside the surround- if (precedence > op2.precedence) return true;- if (precedence < op2.precedence) return false;- // If the precedences are equal, we know this postsurround- // comes after, so op2 being first doesn't need parens- if ((op2.type==postsurround)||(op2.type==binary)) return false;- // if (associative && (this == &op2)) return false;- return true;- case presurround:- if (stage==0) return false; // Inside the surround- if (precedence > op2.precedence) return true;- if (precedence < op2.precedence) return false;- // if (associative && (this == &op2)) return false;- if ((op2.type==unary_prefix)||(op2.type==presurround)) return false;- return true;- }-- return true;-}-/// \param g is the Architecture that owns and will use this PrintLanguage/// \param nm is the formal name of the languagePrintLanguage::PrintLanguage(Architecture *g,const string &nm)@@ -178,7 +129,7 @@ void PrintLanguage::pushOp(const OpToken *tok,const PcodeOp *op)}else {emitOp(revpol.back());- paren = revpol.back().tok->parentheses(*tok,revpol.back().visited);+ paren = parentheses(tok);if (paren)id = emit->openParen('(');else@@ -308,6 +259,68 @@ void PrintLanguage::pushVnLHS(const Varnode *vn,const PcodeOp *op)}}+/// The token at the top of the stack is being emitted. Check if its input expression,+/// ending with the given operator token, needs to be surrounded by parentheses to convey+/// the proper meaning.+/// \param op2 is the input token to \b this operator+/// \param stage is the stage of \b this operator currently being printed+/// \return \b true if \b op2 (as input to \b this) should be parenthesized+bool PrintLanguage::parentheses(const OpToken *op2)++{+ ReversePolish &top( revpol.back() );+ const OpToken *topToken = top.tok;+ int4 stage = top.visited;+ switch(topToken->type) {+ case OpToken::space:+ case OpToken::binary:+ if (topToken->precedence > op2->precedence) return true;+ if (topToken->precedence < op2->precedence) return false;+ if (topToken->associative && (topToken == op2)) return false;+ // If operators are adjacent to each other, the+ // operator printed first must be evaluated first+ // In this case op2 must be evaluated first, so we+ // check if it is printed first (in first stage of binary)+ if ((op2->type==OpToken::postsurround)&&(stage==0)) return false;+ return true;+ case OpToken::unary_prefix:+ if (topToken->precedence > op2->precedence) return true;+ if (topToken->precedence < op2->precedence) return false;+ // if (associative && (this == &op2)) return false;+ if ((op2->type==OpToken::unary_prefix)||(op2->type==OpToken::presurround)) return false;+ return true;+ case OpToken::postsurround:+ if (stage==1) return false; // Inside the surround+ if (topToken->precedence > op2->precedence) return true;+ if (topToken->precedence < op2->precedence) return false;+ // If the precedences are equal, we know this postsurround+ // comes after, so op2 being first doesn't need parens+ if ((op2->type==OpToken::postsurround)||(op2->type==OpToken::binary)) return false;+ // if (associative && (this == &op2)) return false;+ return true;+ case OpToken::presurround:+ if (stage==0) return false; // Inside the surround+ if (topToken->precedence > op2->precedence) return true;+ if (topToken->precedence < op2->precedence) return false;+ // if (associative && (this == &op2)) return false;+ if ((op2->type==OpToken::unary_prefix)||(op2->type==OpToken::presurround)) return false;+ return true;+ case OpToken::hiddenfunction:+ if ((stage==0)&&(revpol.size() > 1)) { // If there is an unresolved previous token+ // New token is printed next to the previous token.+ const OpToken *prevToken = revpol[revpol.size()-2].tok;+ if (prevToken->type != OpToken::binary && prevToken->type != OpToken::unary_prefix)+ return false;+ if (prevToken->precedence < op2->precedence) return false;+ // If precedence is equal, make sure we don't treat two tokens as associative,+ // i.e. we should have parentheses+ }+ return true;+ }++ return true;+}+/// An OpToken directly from the RPN is sent to the low-level emitter,/// resolving any final spacing or parentheses./// \param entry is the RPN entry to be emitted@@ -351,6 +364,8 @@ void PrintLanguage::emitOp(const ReversePolish &entry)if (entry.visited != 1) return;emit->spaces(entry.tok->spacing,entry.tok->bump);break;+ case OpToken::hiddenfunction:+ return; // Never directly prints anything}}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hhindex 433ab7a75b..cf307ae772 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.hh@@ -87,6 +87,7 @@ public:postsurround, ///< Function or array operator formpresurround, ///< Modifier form (like a cast operation)space, ///< No explicitly printed token+ hiddenfunction ///< Operation that isn't explicitly printed};const char *print; ///< Printing characters for the tokenint4 stage; ///< Additional elements consumed from the RPN stack when emitting this token@@ -96,7 +97,6 @@ public:int4 spacing; ///< Spaces to print around operatorint4 bump; ///< Spaces to indent if we break hereOpToken *negate; ///< The token representing the negation of this token- bool parentheses(const OpToken &op2,int4 stage) const;};/// \brief The base class API for emitting a high-level language@@ -263,6 +263,7 @@ protected:void pushVnExplicit(const Varnode *vn,const PcodeOp *op); ///< Push an explicit variable onto the RPN stackvoid pushVnLHS(const Varnode *vn,const PcodeOp *op); ///< Push a variable as the left-hand side of an expression+ bool parentheses(const OpToken *op2); ///< Determine if the given token should be emitted in its own parenthetic expressionvoid emitOp(const ReversePolish &entry); ///< Send an operator token from the RPN to the emittervoid emitAtom(const Atom &atom); ///< Send an variable token from the RPN to the emitterstatic bool unicodeNeedsEscape(int4 codepoint); ///< Determine if the given codepoint needs to be escaped