diff options
| -rw-r--r-- | binaryninjaapi.h | 21 | ||||
| -rw-r--r-- | highlevelil.cpp | 9 | ||||
| -rw-r--r-- | lang/c/pseudoc.cpp | 193 | ||||
| -rw-r--r-- | lang/c/pseudoc.h | 10 | ||||
| -rw-r--r-- | lang/rust/pseudorust.cpp | 203 | ||||
| -rw-r--r-- | lang/rust/pseudorust.h | 14 | ||||
| -rw-r--r-- | languagerepresentation.cpp | 21 | ||||
| -rw-r--r-- | python/examples/pseudo_python.py | 170 | ||||
| -rw-r--r-- | python/highlevelil.py | 10 | ||||
| -rw-r--r-- | python/languagerepresentation.py | 23 |
10 files changed, 338 insertions, 336 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 17b9cfa8..3207b1da 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -13623,9 +13623,8 @@ namespace BinaryNinja { std::vector<DisassemblyTextLine> GetExprText( ExprId expr, bool asFullAst = true, DisassemblySettings* settings = nullptr); std::vector<DisassemblyTextLine> GetExprText( - const HighLevelILInstruction& instr, bool asFullAst = true, DisassemblySettings* settings = nullptr); - std::vector<DisassemblyTextLine> GetInstructionText( - size_t i, bool asFullAst = true, DisassemblySettings* settings = nullptr); + const HighLevelILInstruction& instr, DisassemblySettings* settings = nullptr); + std::vector<DisassemblyTextLine> GetInstructionText(size_t i, DisassemblySettings* settings = nullptr); Confidence<Ref<Type>> GetExprType(size_t expr); Confidence<Ref<Type>> GetExprType(const HighLevelILInstruction& expr); @@ -13671,25 +13670,22 @@ namespace BinaryNinja { \param instr The instruction to emit lines for. \param settings The settings for disassembly (optional). - \param asFullAst Whether to emit full AST or single expressions. \param precedence The current operator precedence level. \param statement Whether the instruction is a statement or an expression. \return A list of lines of tokens for the instruction. */ std::vector<DisassemblyTextLine> GetExprText(const HighLevelILInstruction& instr, DisassemblySettings* settings, - bool asFullAst = true, BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, - bool statement = false); + BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false); /*! Generates lines for the given High Level IL instruction in the style of the linear view. To get the lines for the entire function, pass the root instruction of a HighLevelILFunction. \param instr The instruction to emit lines for. \param settings The settings for disassembly (optional). - \param asFullAst Whether to emit full AST or single expressions. \return A list of lines of tokens for the instruction. */ std::vector<DisassemblyTextLine> GetLinearLines( - const HighLevelILInstruction& instr, DisassemblySettings* settings, bool asFullAst = true); + const HighLevelILInstruction& instr, DisassemblySettings* settings); /*! Generates lines for a single High Level IL basic block. @@ -13748,13 +13744,12 @@ namespace BinaryNinja { \param instr The instruction to emit tokens for. \param tokens The token emitter to use. \param settings The disassembly settings to use (may be NULL). - \param asFullAst Whether to emit full AST or single expressions. \param precedence The current operator precedence level. \param statement Whether the instruction is a statement or an expression. */ virtual void GetExprText(const HighLevelILInstruction& instr, HighLevelILTokenEmitter& tokens, - DisassemblySettings* settings, bool asFullAst = true, - BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false) = 0; + DisassemblySettings* settings, BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, + bool statement = false) = 0; /*! This method can be overridden to emit tokens at the start of a function. @@ -13800,8 +13795,8 @@ namespace BinaryNinja { protected: void GetExprText(const HighLevelILInstruction& instr, HighLevelILTokenEmitter& tokens, - DisassemblySettings* settings, bool asFullAst = true, - BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false) override; + DisassemblySettings* settings, BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, + bool statement = false) override; }; class TypePrinter; diff --git a/highlevelil.cpp b/highlevelil.cpp index 606cc772..121dc406 100644 --- a/highlevelil.cpp +++ b/highlevelil.cpp @@ -504,17 +504,16 @@ vector<DisassemblyTextLine> HighLevelILFunction::GetExprText(ExprId expr, bool a vector<DisassemblyTextLine> HighLevelILFunction::GetExprText( - const HighLevelILInstruction& instr, bool asFullAst, DisassemblySettings* settings) + const HighLevelILInstruction& instr, DisassemblySettings* settings) { - return GetExprText(instr.exprIndex, asFullAst, settings); + return GetExprText(instr.exprIndex, instr.ast, settings); } -vector<DisassemblyTextLine> HighLevelILFunction::GetInstructionText( - size_t i, bool asFullAst, DisassemblySettings* settings) +vector<DisassemblyTextLine> HighLevelILFunction::GetInstructionText(size_t i, DisassemblySettings* settings) { HighLevelILInstruction instr = GetInstruction(i); - return GetExprText(instr, asFullAst, settings); + return GetExprText(instr, settings); } diff --git a/lang/c/pseudoc.cpp b/lang/c/pseudoc.cpp index 4f2174d6..47d566d1 100644 --- a/lang/c/pseudoc.cpp +++ b/lang/c/pseudoc.cpp @@ -246,20 +246,20 @@ void PseudoCFunction::AppendSingleSizeToken( void PseudoCFunction::AppendComparison(const string& comparison, const HighLevelILInstruction& instr, - HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, + HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint) { const auto leftExpr = instr.GetLeftExpr(); const auto rightExpr = instr.GetRightExpr(); - GetExprTextInternal(leftExpr, emitter, settings, asFullAst, precedence, false, signedHint); + GetExprTextInternal(leftExpr, emitter, settings, precedence, false, signedHint); emitter.Append(OperationToken, comparison); - GetExprTextInternal(rightExpr, emitter, settings, asFullAst, precedence, false, signedHint); + GetExprTextInternal(rightExpr, emitter, settings, precedence, false, signedHint); } void PseudoCFunction::AppendTwoOperand(const string& operand, const HighLevelILInstruction& instr, - HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, + HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint) { const auto& twoOperand = instr.AsTwoOperand(); @@ -291,9 +291,9 @@ void PseudoCFunction::AppendTwoOperand(const string& operand, const HighLevelILI emitter.Append(OperationToken, "COMBINE"); emitter.AppendOpenParen(); - GetExprTextInternal(high, emitter, settings, asFullAst); + GetExprTextInternal(high, emitter, settings); emitter.Append(TextToken, ", "); - GetExprTextInternal(low, emitter, settings, asFullAst); + GetExprTextInternal(low, emitter, settings); emitter.AppendCloseParen(); } @@ -317,7 +317,7 @@ void PseudoCFunction::AppendTwoOperand(const string& operand, const HighLevelILI } } - GetExprTextInternal(leftExpr, emitter, settings, asFullAst, leftPrecedence, false, signedHint); + GetExprTextInternal(leftExpr, emitter, settings, leftPrecedence, false, signedHint); auto lessThanZero = [](uint64_t value, uint64_t width) -> bool { return ((1UL << ((width * 8) - 1UL)) & value) != 0; @@ -343,7 +343,7 @@ void PseudoCFunction::AppendTwoOperand(const string& operand, const HighLevelILI } emitter.Append(OperationToken, operand); - GetExprTextInternal(rightExpr, emitter, settings, asFullAst, precedence, false, signedHint); + GetExprTextInternal(rightExpr, emitter, settings, precedence, false, signedHint); } @@ -521,15 +521,14 @@ void PseudoCFunction::AppendFieldTextTokens(const HighLevelILInstruction& var, u void PseudoCFunction::GetExprText(const HighLevelILInstruction& instr, HighLevelILTokenEmitter& tokens, - DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, bool statement) + DisassemblySettings* settings, BNOperatorPrecedence precedence, bool statement) { - GetExprTextInternal(instr, tokens, settings, asFullAst, precedence, statement); + GetExprTextInternal(instr, tokens, settings, precedence, statement); } void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, HighLevelILTokenEmitter& tokens, - DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, bool statement, - optional<bool> signedHint) + DisassemblySettings* settings, BNOperatorPrecedence precedence, bool statement, optional<bool> signedHint) { // The lambdas in this function are here to reduce stack frame size of this function. Without them, // complex expression can cause the process to crash from a stack overflow. @@ -699,9 +698,9 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H // normal source code. auto next = i; ++next; - if (asFullAst && (instr.exprIndex == GetHighLevelILFunction()->GetRootExpr().exprIndex) && (exprs.size() > 1) && - (next == exprs.end()) && ((*i).operation == HLIL_RET) && - ((*i).GetSourceExprs<HLIL_RET>().size() == 0)) + if (instr.ast && (instr.exprIndex == GetHighLevelILFunction()->GetRootExpr().exprIndex) + && (exprs.size() > 1) && (next == exprs.end()) && ((*i).operation == HLIL_RET) + && ((*i).GetSourceExprs<HLIL_RET>().size() == 0)) continue; // If the statement is one that contains additional blocks of code, insert a scope separator @@ -730,7 +729,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H needSeparator = hasBlocks; // Emit the lines for the statement itself - GetExprTextInternal(*i, tokens, settings, true, TopLevelOperatorPrecedence, true); + GetExprTextInternal(*i, tokens, settings, TopLevelOperatorPrecedence, true); tokens.NewLine(); } }(); @@ -743,18 +742,18 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto updateExpr = instr.GetUpdateExpr<HLIL_FOR>(); const auto loopExpr = instr.GetLoopExpr<HLIL_FOR>(); - if (asFullAst) + if (instr.ast) { tokens.Append(KeywordToken, "for "); tokens.AppendOpenParen(); if (initExpr.operation != HLIL_NOP) - GetExprTextInternal(initExpr, tokens, settings, asFullAst); + GetExprTextInternal(initExpr, tokens, settings); tokens.Append(TextToken, "; "); if (condExpr.operation != HLIL_NOP) - GetExprTextInternal(condExpr, tokens, settings, asFullAst); + GetExprTextInternal(condExpr, tokens, settings); tokens.Append(TextToken, "; "); if (updateExpr.operation != HLIL_NOP) - GetExprTextInternal(updateExpr, tokens, settings, asFullAst); + GetExprTextInternal(updateExpr, tokens, settings); tokens.AppendCloseParen(); if (function->IsInstructionCollapsed(instr)) { @@ -764,7 +763,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { auto scopeType = HighLevelILFunction::GetExprScopeType(loopExpr); tokens.BeginScope(scopeType); - GetExprTextInternal(loopExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(loopExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.EndScope(scopeType); tokens.FinalizeScope(); } @@ -789,9 +788,9 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.Append(KeywordToken, "if "); tokens.AppendOpenParen(); - GetExprTextInternal(condExpr, tokens, settings, asFullAst); + GetExprTextInternal(condExpr, tokens, settings); tokens.AppendCloseParen(); - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -802,7 +801,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { auto scopeType = HighLevelILFunction::GetExprScopeType(trueExpr); tokens.BeginScope(scopeType); - GetExprTextInternal(trueExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(trueExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.EndScope(scopeType); } //tokens.SetCurrentExpr(falseExpr); @@ -810,7 +809,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { tokens.ScopeContinuation(false); tokens.Append(KeywordToken, "else "); - GetExprTextInternal(falseExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(falseExpr, tokens, settings, TopLevelOperatorPrecedence, true); } else if (falseExpr.operation != HLIL_NOP) { @@ -826,7 +825,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { auto scopeType = HighLevelILFunction::GetExprScopeType(falseExpr); tokens.BeginScope(scopeType); - GetExprTextInternal(falseExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(falseExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.EndScope(scopeType); tokens.FinalizeScope(); } @@ -847,7 +846,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); GetExprTextInternal(condExpr, tokens, settings); tokens.AppendCloseParen(); - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -858,7 +857,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { auto scopeType = HighLevelILFunction::GetExprScopeType(loopExpr); tokens.BeginScope(scopeType); - GetExprTextInternal(loopExpr, tokens, settings, true, TopLevelOperatorPrecedence, true); + GetExprTextInternal(loopExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.EndScope(scopeType); tokens.FinalizeScope(); } @@ -870,7 +869,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H [&]() { const auto loopExpr = instr.GetLoopExpr<HLIL_DO_WHILE>(); const auto condExpr = instr.GetConditionExpr<HLIL_DO_WHILE>(); - if (asFullAst) + if (instr.ast) { tokens.Append(KeywordToken, "do"); if (function->IsInstructionCollapsed(instr)) @@ -887,7 +886,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { auto scopeType = HighLevelILFunction::GetExprScopeType(loopExpr); tokens.BeginScope(scopeType); - GetExprTextInternal(loopExpr, tokens, settings, true, TopLevelOperatorPrecedence, true); + GetExprTextInternal(loopExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.EndScope(scopeType); tokens.ScopeContinuation(true); @@ -918,9 +917,9 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.Append(KeywordToken, "switch "); tokens.AppendOpenParen(); - GetExprTextInternal(condExpr, tokens, settings, asFullAst); + GetExprTextInternal(condExpr, tokens, settings); tokens.AppendCloseParen(); - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -932,7 +931,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.BeginScope(SwitchScopeType); for (const auto caseExpr: caseExprs) { - GetExprTextInternal(caseExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(caseExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.NewLine(); } @@ -948,7 +947,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H else { tokens.BeginScope(CaseScopeType); - GetExprTextInternal(defaultExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(defaultExpr, tokens, settings, TopLevelOperatorPrecedence, true); tokens.EndScope(CaseScopeType); tokens.FinalizeScope(); } @@ -969,13 +968,13 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { const auto& valueExpr = valueExprs[index]; tokens.Append(KeywordToken, "case "); - GetExprTextInternal(valueExpr, tokens, settings, asFullAst); + GetExprTextInternal(valueExpr, tokens, settings); tokens.Append(TextToken, ":"); if (index != valueExprs.size() - 1) tokens.NewLine(); } - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -986,7 +985,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { tokens.PrependCollapseIndicator(function, instr); tokens.BeginScope(CaseScopeType); - GetExprTextInternal(trueExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, true); + GetExprTextInternal(trueExpr, tokens, settings, TopLevelOperatorPrecedence, true); static const std::vector<BNHighLevelILOperation> operations { HLIL_CONTINUE, HLIL_NORET, HLIL_UNREACHABLE, HLIL_JUMP, HLIL_GOTO, HLIL_TAILCALL}; @@ -1048,7 +1047,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_ZX>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > UnaryOperatorPrecedence; @@ -1057,7 +1056,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); AppendSizeToken(instr.size, false, tokens); tokens.AppendCloseParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence, false, false); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence, false, false); if (parens) tokens.AppendCloseParen(); if (statement) @@ -1070,7 +1069,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_SX>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > UnaryOperatorPrecedence; @@ -1079,7 +1078,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); AppendSizeToken(instr.size, true, tokens); tokens.AppendCloseParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence, false, true); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence, false, true); if (parens) tokens.AppendCloseParen(); if (statement) @@ -1092,7 +1091,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto destExpr = instr.GetDestExpr<HLIL_CALL>(); const auto parameterExprs = instr.GetParameterExprs<HLIL_CALL>(); - GetExprTextInternal(destExpr, tokens, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(destExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendOpenParen(); vector<FunctionParameter> namedParams; @@ -1129,7 +1128,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H } if (!renderedAsString) - GetExprText(parameterExpr, tokens, settings, asFullAst); + GetExprText(parameterExpr, tokens, settings); } tokens.AppendCloseParen(); if (statement) @@ -1162,9 +1161,9 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_ARRAY_INDEX>(); const auto indexExpr = instr.GetIndexExpr<HLIL_ARRAY_INDEX>(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendOpenBracket(); - GetExprTextInternal(indexExpr, tokens, settings, asFullAst); + GetExprTextInternal(indexExpr, tokens, settings); tokens.AppendCloseBracket(); if (statement) tokens.AppendSemicolon(); @@ -1229,7 +1228,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H appearsDead = false; } - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, AssignmentOperatorPrecedence); if (appearsDead) tokens.EndForceZeroConfidence(); @@ -1454,16 +1453,16 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto high = destExpr.GetHighExpr<HLIL_SPLIT>(); const auto low = destExpr.GetLowExpr<HLIL_SPLIT>(); - GetExprTextInternal(high, tokens, settings, asFullAst, precedence); + GetExprTextInternal(high, tokens, settings, precedence); tokens.Append(OperationToken, " = "); tokens.Append(OperationToken, "HIGH"); AppendSingleSizeToken(high.size, OperationToken, tokens); tokens.AppendOpenParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); tokens.AppendCloseParen(); tokens.AppendSemicolon(); tokens.NewLine(); - GetExprTextInternal(low, tokens, settings, asFullAst, precedence); + GetExprTextInternal(low, tokens, settings, precedence); } else { @@ -1577,7 +1576,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H } } - GetExprTextInternal(destExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(destExpr, tokens, settings, precedence); if (assignUpdateOperator.has_value() && assignUpdateSource.has_value()) tokens.Append(OperationToken, assignUpdateOperator.value()); else @@ -1612,14 +1611,13 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H } else { - GetExprTextInternal(assignUpdateSource.value(), tokens, settings, asFullAst, - AssignmentOperatorPrecedence, false, assignSignedHint); + GetExprTextInternal(assignUpdateSource.value(), tokens, settings, AssignmentOperatorPrecedence, + false, assignSignedHint); } } else { - GetExprTextInternal( - srcExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence, false, assignSignedHint); + GetExprTextInternal(srcExpr, tokens, settings, AssignmentOperatorPrecedence, false, assignSignedHint); } if (isSplit) @@ -1638,9 +1636,9 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto destExprs = instr.GetDestExprs<HLIL_ASSIGN_UNPACK>(); const auto firstExpr = destExprs[0]; - GetExprTextInternal(firstExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprTextInternal(firstExpr, tokens, settings, AssignmentOperatorPrecedence); tokens.Append(OperationToken, " = "); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, AssignmentOperatorPrecedence); if (statement) tokens.AppendSemicolon(); }(); @@ -1672,7 +1670,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.Append(TextToken, "*"); tokens.AppendCloseParen(); } - GetExprTextInternal(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.Append(OperationToken, " + "); tokens.AppendIntegerTextToken(instr, fieldOffset, instr.size); @@ -1700,7 +1698,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.Append(TextToken, "*"); tokens.AppendCloseParen(); } - GetExprTextInternal(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); if (!settings || settings->IsOptionSet(ShowTypeCasts)) { tokens.AppendCloseParen(); @@ -1709,7 +1707,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H } else { - GetExprTextInternal(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); } AppendFieldTextTokens(srcExpr, fieldOffset, memberIndex, instr.size, tokens, false); @@ -1829,7 +1827,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendCloseParen(); } - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); } @@ -1847,13 +1845,13 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.Append(AnnotationToken, "/* tailcall */"); tokens.NewLine(); tokens.Append(KeywordToken, "return "); - GetExprTextInternal(destExpr, tokens, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(destExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendOpenParen(); for (size_t index{}; index < parameterExprs.size(); index++) { const auto& parameterExpr = parameterExprs[index]; if (index != 0) tokens.Append(TextToken, ", "); - GetExprTextInternal(parameterExpr, tokens, settings, asFullAst); + GetExprTextInternal(parameterExpr, tokens, settings); } tokens.AppendCloseParen(); if (statement) @@ -1868,7 +1866,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H if (parens) tokens.AppendOpenParen(); tokens.Append(OperationToken, "&"); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -1881,7 +1879,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > EqualityOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendComparison(" == ", instr, tokens, settings, asFullAst, EqualityOperatorPrecedence); + AppendComparison(" == ", instr, tokens, settings, EqualityOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -1908,7 +1906,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > EqualityOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendComparison(" == ", instr, tokens, settings, asFullAst, EqualityOperatorPrecedence); + AppendComparison(" == ", instr, tokens, settings, EqualityOperatorPrecedence); if (parens) tokens.AppendCloseParen(); } @@ -1922,7 +1920,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > EqualityOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendComparison(" != ", instr, tokens, settings, asFullAst, EqualityOperatorPrecedence); + AppendComparison(" != ", instr, tokens, settings, EqualityOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -1943,7 +1941,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > EqualityOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendComparison(" != ", instr, tokens, settings, asFullAst, EqualityOperatorPrecedence); + AppendComparison(" != ", instr, tokens, settings, EqualityOperatorPrecedence); if (parens) tokens.AppendCloseParen(); } @@ -1964,7 +1962,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H cmpSigned = false; else if (instr.operation == HLIL_CMP_SLT) cmpSigned = true; - AppendComparison(" < ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" < ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -1984,7 +1982,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H cmpSigned = false; else if (instr.operation == HLIL_CMP_SLE) cmpSigned = true; - AppendComparison(" <= ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" <= ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2004,7 +2002,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H cmpSigned = false; else if (instr.operation == HLIL_CMP_SGE) cmpSigned = true; - AppendComparison(" >= ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" >= ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2024,7 +2022,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H cmpSigned = false; else if (instr.operation == HLIL_CMP_SGT) cmpSigned = true; - AppendComparison(" > ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" > ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2041,7 +2039,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(instr.size == 0 ? " && " : " & ", instr, tokens, settings, asFullAst, + AppendTwoOperand(instr.size == 0 ? " && " : " & ", instr, tokens, settings, instr.size == 0 ? LogicalAndOperatorPrecedence : BitwiseAndOperatorPrecedence); if (parens) tokens.AppendCloseParen(); @@ -2058,7 +2056,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(instr.size == 0 ? " || " : " | ", instr, tokens, settings, asFullAst, + AppendTwoOperand(instr.size == 0 ? " || " : " | ", instr, tokens, settings, instr.size == 0 ? LogicalOrOperatorPrecedence : BitwiseOrOperatorPrecedence); if (parens) tokens.AppendCloseParen(); @@ -2073,7 +2071,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H precedence == BitwiseOrOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" ^ ", instr, tokens, settings, asFullAst, BitwiseXorOperatorPrecedence); + AppendTwoOperand(" ^ ", instr, tokens, settings, BitwiseXorOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2091,7 +2089,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" + ", instr, tokens, settings, asFullAst, AddOperatorPrecedence); + AppendTwoOperand(" + ", instr, tokens, settings, AddOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2117,7 +2115,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H // Yes tokens.Append(OperationToken, "ADJ"); tokens.AppendOpenParen(); - GetExprTextInternal(left, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(left, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendCloseParen(); return; } @@ -2129,7 +2127,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" - ", instr, tokens, settings, asFullAst, SubOperatorPrecedence); + AppendTwoOperand(" - ", instr, tokens, settings, SubOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2144,7 +2142,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" - ", instr, tokens, settings, asFullAst, SubOperatorPrecedence); + AppendTwoOperand(" - ", instr, tokens, settings, SubOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2157,7 +2155,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > ShiftOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" << ", instr, tokens, settings, asFullAst, ShiftOperatorPrecedence); + AppendTwoOperand(" << ", instr, tokens, settings, ShiftOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2171,7 +2169,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > ShiftOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" >> ", instr, tokens, settings, asFullAst, ShiftOperatorPrecedence); + AppendTwoOperand(" >> ", instr, tokens, settings, ShiftOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2195,7 +2193,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H mulSigned = false; else if (instr.operation == HLIL_MULS_DP) mulSigned = true; - AppendTwoOperand(" * ", instr, tokens, settings, asFullAst, MultiplyOperatorPrecedence, mulSigned); + AppendTwoOperand(" * ", instr, tokens, settings, MultiplyOperatorPrecedence, mulSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2219,7 +2217,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H divSigned = false; else if (instr.operation == HLIL_DIVS || instr.operation == HLIL_DIVS_DP) divSigned = true; - AppendTwoOperand(" / ", instr, tokens, settings, asFullAst, DivideOperatorPrecedence, divSigned); + AppendTwoOperand(" / ", instr, tokens, settings, DivideOperatorPrecedence, divSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2242,7 +2240,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H modSigned = false; else if (instr.operation == HLIL_MODS || instr.operation == HLIL_MODS_DP) modSigned = true; - AppendTwoOperand(" % ", instr, tokens, settings, asFullAst, DivideOperatorPrecedence, modSigned); + AppendTwoOperand(" % ", instr, tokens, settings, DivideOperatorPrecedence, modSigned); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2425,7 +2423,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); tokens.Append(OperationToken, "-"); tokens.AppendOpenParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence, false, true); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence, false, true); tokens.AppendCloseParen(); if (parens) tokens.AppendCloseParen(); @@ -2439,7 +2437,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_FLOAT_CONV>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); return; } const auto floatType = @@ -2454,7 +2452,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); tokens.Append(TypeNameToken, floatType.c_str()); tokens.AppendCloseParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2467,7 +2465,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_FLOAT_TO_INT>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); return; } @@ -2477,7 +2475,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); AppendSizeToken(instr.size, true, tokens); tokens.AppendCloseParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2492,7 +2490,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H bool parens = precedence > TernaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, TernaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, TernaryOperatorPrecedence); tokens.Append(OperationToken, " ? "); tokens.AppendIntegerTextToken(instr, 1, 1); tokens.Append(OperationToken, " : "); @@ -2509,7 +2507,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_INT_TO_FLOAT>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); return; } const auto floatType = @@ -2524,7 +2522,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); tokens.Append(TypeNameToken, floatType.c_str()); tokens.AppendCloseParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) @@ -2544,7 +2542,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H { const auto& parameterExpr = parameterExprs[index]; if (index != 0) tokens.Append(TextToken, ", "); - GetExprTextInternal(parameterExpr, tokens, settings, asFullAst); + GetExprTextInternal(parameterExpr, tokens, settings); } tokens.AppendCloseParen(); if (statement) @@ -2562,7 +2560,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto& srcExpr = srcExprs[index]; if (index == 0) tokens.Append(TextToken, " "); if (index != 0) tokens.Append(TextToken, ", "); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst); + GetExprTextInternal(srcExpr, tokens, settings); } if (statement) tokens.AppendSemicolon(); @@ -2641,8 +2639,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H } else { - GetExprTextInternal( - srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); symbolType = OtherSymbolResult; } @@ -2698,7 +2695,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H } else { - GetExprTextInternal(srcExpr, tokens, settings, true, AddOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, AddOperatorPrecedence); } tokens.Append(OperationToken, " + "); @@ -2849,7 +2846,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H const auto srcExpr = instr.GetSourceExpr<HLIL_LOW_PART>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, precedence); + GetExprTextInternal(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > UnaryOperatorPrecedence; @@ -2858,7 +2855,7 @@ void PseudoCFunction::GetExprTextInternal(const HighLevelILInstruction& instr, H tokens.AppendOpenParen(); AppendSizeToken(instr.size, signedHint.value_or(true), tokens); tokens.AppendCloseParen(); - GetExprTextInternal(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprTextInternal(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (statement) diff --git a/lang/c/pseudoc.h b/lang/c/pseudoc.h index 664e7695..024be2f1 100644 --- a/lang/c/pseudoc.h +++ b/lang/c/pseudoc.h @@ -24,10 +24,10 @@ class PseudoCFunction: public BinaryNinja::LanguageRepresentationFunction void AppendSizeToken(size_t size, bool isSigned, BinaryNinja::HighLevelILTokenEmitter& emitter); void AppendSingleSizeToken(size_t size, BNInstructionTextTokenType type, BinaryNinja::HighLevelILTokenEmitter& emitter); void AppendComparison(const std::string& comparison, const BinaryNinja::HighLevelILInstruction& instr, - BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, bool asFullAst, + BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint = std::nullopt); void AppendTwoOperand(const std::string& operand, const BinaryNinja::HighLevelILInstruction& instr, - BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, bool asFullAst, + BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint = std::nullopt); void AppendTwoOperandFunction(const std::string& function, const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, bool sizeToken = true); @@ -36,15 +36,15 @@ class PseudoCFunction: public BinaryNinja::LanguageRepresentationFunction void AppendFieldTextTokens(const BinaryNinja::HighLevelILInstruction& var, uint64_t offset, size_t memberIndex, size_t size, BinaryNinja::HighLevelILTokenEmitter& tokens, bool deref, bool displayDeref = true); void GetExprTextInternal(const BinaryNinja::HighLevelILInstruction& instr, - BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, bool asFullAst = true, + BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false, std::optional<bool> signedHint = std::nullopt); protected: void InitTokenEmitter(BinaryNinja::HighLevelILTokenEmitter& tokens) override; void GetExprText(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens, - BinaryNinja::DisassemblySettings* settings, bool asFullAst = true, - BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, bool statement = false) override; + BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, + bool statement = false) override; void BeginLines( const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens) override; void EndLines( diff --git a/lang/rust/pseudorust.cpp b/lang/rust/pseudorust.cpp index 0f41678f..d12b8e32 100644 --- a/lang/rust/pseudorust.cpp +++ b/lang/rust/pseudorust.cpp @@ -250,20 +250,20 @@ void PseudoRustFunction::AppendSingleSizeToken( void PseudoRustFunction::AppendComparison(const string& comparison, const HighLevelILInstruction& instr, - HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, + HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint) { const auto leftExpr = instr.GetLeftExpr(); const auto rightExpr = instr.GetRightExpr(); - GetExprText(leftExpr, emitter, settings, asFullAst, precedence, InnerExpression, signedHint); + GetExprText(leftExpr, emitter, settings, precedence, InnerExpression, signedHint); emitter.Append(OperationToken, comparison); - GetExprText(rightExpr, emitter, settings, asFullAst, precedence, InnerExpression, signedHint); + GetExprText(rightExpr, emitter, settings, precedence, InnerExpression, signedHint); } void PseudoRustFunction::AppendTwoOperand(const string& operand, const HighLevelILInstruction& instr, - HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, + HighLevelILTokenEmitter& emitter, DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint) { const auto& twoOperand = instr.AsTwoOperand(); @@ -295,9 +295,9 @@ void PseudoRustFunction::AppendTwoOperand(const string& operand, const HighLevel emitter.Append(OperationToken, "COMBINE"); emitter.AppendOpenParen(); - GetExprText(high, emitter, settings, asFullAst); + GetExprText(high, emitter, settings); emitter.Append(TextToken, ", "); - GetExprText(low, emitter, settings, asFullAst); + GetExprText(low, emitter, settings); emitter.AppendCloseParen(); } @@ -306,25 +306,25 @@ void PseudoRustFunction::AppendTwoOperand(const string& operand, const HighLevel const auto exprType = leftExpr.GetType(); if (exprType && exprType->IsPointer()) { - GetExprText(leftExpr, emitter, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprText(leftExpr, emitter, settings, MemberAndFunctionOperatorPrecedence); emitter.Append(TextToken, "."); emitter.Append(OperationToken, "byte_offset"); emitter.AppendOpenParen(); if (operand == " - ") { emitter.Append(OperationToken, "-"); - GetExprText(rightExpr, emitter, settings, asFullAst, UnaryOperatorPrecedence); + GetExprText(rightExpr, emitter, settings, UnaryOperatorPrecedence); } else { - GetExprText(rightExpr, emitter, settings, asFullAst); + GetExprText(rightExpr, emitter, settings); } emitter.AppendCloseParen(); return; } } - GetExprText(leftExpr, emitter, settings, asFullAst, leftPrecedence, InnerExpression, signedHint); + GetExprText(leftExpr, emitter, settings, leftPrecedence, InnerExpression, signedHint); auto lessThanZero = [](uint64_t value, uint64_t width) -> bool { return ((1UL << ((width * 8) - 1UL)) & value) != 0; @@ -350,7 +350,7 @@ void PseudoRustFunction::AppendTwoOperand(const string& operand, const HighLevel } emitter.Append(OperationToken, operand); - GetExprText(rightExpr, emitter, settings, asFullAst, precedence, InnerExpression, signedHint); + GetExprText(rightExpr, emitter, settings, precedence, InnerExpression, signedHint); } @@ -563,7 +563,7 @@ bool PseudoRustFunction::IsMutable(const Variable& var) const void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLevelILTokenEmitter& tokens, - DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, ExpressionType exprType, + DisassemblySettings* settings, BNOperatorPrecedence precedence, ExpressionType exprType, std::optional<bool> signedHint) { // The lambdas in this function are here to reduce stack frame size of this function. Without them, @@ -731,9 +731,9 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe // normal source code. auto next = i; ++next; - if (asFullAst && (instr.exprIndex == GetHighLevelILFunction()->GetRootExpr().exprIndex) && (exprs.size() > 1) && - (next == exprs.end()) && ((*i).operation == HLIL_RET) && - ((*i).GetSourceExprs<HLIL_RET>().size() == 0)) + if (instr.ast && (instr.exprIndex == GetHighLevelILFunction()->GetRootExpr().exprIndex) + && (exprs.size() > 1) && (next == exprs.end()) && ((*i).operation == HLIL_RET) + && ((*i).GetSourceExprs<HLIL_RET>().size() == 0)) continue; // If the statement is one that contains additional blocks of code, insert a scope separator @@ -762,7 +762,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe needSeparator = hasBlocks; // Emit the lines for the statement itself - GetExprText(*i, tokens, settings, true, TopLevelOperatorPrecedence, + GetExprText(*i, tokens, settings, TopLevelOperatorPrecedence, exprType == TrailingStatementExpression && next == exprs.end() ? TrailingStatementExpression : StatementExpression); tokens.NewLine(); @@ -777,7 +777,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto updateExpr = instr.GetUpdateExpr<HLIL_FOR>(); const auto loopExpr = instr.GetLoopExpr<HLIL_FOR>(); - if (asFullAst) + if (instr.ast) { tokens.Append(KeywordToken, "for "); @@ -804,12 +804,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe if (stepBy) tokens.AppendOpenParen(); - GetExprText(initExpr.GetSourceExpr<HLIL_VAR_INIT>(), tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprText( + initExpr.GetSourceExpr<HLIL_VAR_INIT>(), tokens, settings, AssignmentOperatorPrecedence); if (condExpr.operation == HLIL_CMP_SLT || condExpr.operation == HLIL_CMP_ULT) tokens.Append(TextToken, ".."); else tokens.Append(TextToken, "..="); - GetExprText(condExpr.GetRightExpr(), tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprText(condExpr.GetRightExpr(), tokens, settings, AssignmentOperatorPrecedence); if (stepBy) { @@ -817,8 +818,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe tokens.Append(TextToken, "."); tokens.Append(OperationToken, "step_by"); tokens.AppendOpenParen(); - GetExprText(updateExpr.GetSourceExpr<HLIL_ASSIGN>().GetRightExpr<HLIL_ADD>(), - tokens, settings, asFullAst); + GetExprText(updateExpr.GetSourceExpr<HLIL_ASSIGN>().GetRightExpr<HLIL_ADD>(), tokens, settings); tokens.AppendCloseParen(); } } @@ -826,13 +826,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { // For loop isn't directly representable in standard Rust if (initExpr.operation != HLIL_NOP) - GetExprText(initExpr, tokens, settings, asFullAst); + GetExprText(initExpr, tokens, settings); tokens.Append(TextToken, "; "); if (condExpr.operation != HLIL_NOP) - GetExprText(condExpr, tokens, settings, asFullAst); + GetExprText(condExpr, tokens, settings); tokens.Append(TextToken, "; "); if (updateExpr.operation != HLIL_NOP) - GetExprText(updateExpr, tokens, settings, asFullAst); + GetExprText(updateExpr, tokens, settings); } if (function->IsInstructionCollapsed(instr)) @@ -843,7 +843,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { auto scopeType = HighLevelILFunction::GetExprScopeType(loopExpr); tokens.BeginScope(scopeType); - GetExprText(loopExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, StatementExpression); + GetExprText(loopExpr, tokens, settings, TopLevelOperatorPrecedence, StatementExpression); tokens.EndScope(scopeType); tokens.FinalizeScope(); } @@ -863,8 +863,8 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto falseExpr = instr.GetFalseExpr<HLIL_IF>(); tokens.Append(KeywordToken, "if "); - GetExprText(condExpr, tokens, settings, asFullAst); - if (!asFullAst) + GetExprText(condExpr, tokens, settings); + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -875,7 +875,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { auto scopeType = HighLevelILFunction::GetExprScopeType(trueExpr); tokens.BeginScope(scopeType); - GetExprText(trueExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, exprType); + GetExprText(trueExpr, tokens, settings, TopLevelOperatorPrecedence, exprType); tokens.EndScope(scopeType); } //tokens.SetCurrentExpr(falseExpr); @@ -883,7 +883,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { tokens.ScopeContinuation(false); tokens.Append(KeywordToken, "else "); - GetExprText(falseExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, exprType); + GetExprText(falseExpr, tokens, settings, TopLevelOperatorPrecedence, exprType); } else if (falseExpr.operation != HLIL_NOP) { @@ -898,7 +898,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { auto scopeType = HighLevelILFunction::GetExprScopeType(falseExpr); tokens.BeginScope(scopeType); - GetExprText(falseExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, exprType); + GetExprText(falseExpr, tokens, settings, TopLevelOperatorPrecedence, exprType); tokens.EndScope(scopeType); tokens.FinalizeScope(); } @@ -924,7 +924,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe tokens.Append(KeywordToken, "while "); GetExprText(condExpr, tokens, settings); } - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -935,7 +935,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { auto scopeType = HighLevelILFunction::GetExprScopeType(loopExpr); tokens.BeginScope(scopeType); - GetExprText(loopExpr, tokens, settings, true, TopLevelOperatorPrecedence, StatementExpression); + GetExprText(loopExpr, tokens, settings, TopLevelOperatorPrecedence, StatementExpression); tokens.EndScope(scopeType); tokens.FinalizeScope(); } @@ -946,7 +946,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe [&]() { const auto loopExpr = instr.GetLoopExpr<HLIL_DO_WHILE>(); const auto condExpr = instr.GetConditionExpr<HLIL_DO_WHILE>(); - if (asFullAst) + if (instr.ast) { tokens.Append(KeywordToken, "do"); if (function->IsInstructionCollapsed(instr)) @@ -961,7 +961,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { auto scopeType = HighLevelILFunction::GetExprScopeType(loopExpr); tokens.BeginScope(scopeType); - GetExprText(loopExpr, tokens, settings, true, TopLevelOperatorPrecedence, StatementExpression); + GetExprText(loopExpr, tokens, settings, TopLevelOperatorPrecedence, StatementExpression); tokens.EndScope(scopeType); tokens.ScopeContinuation(true); tokens.Append(KeywordToken, "while "); @@ -986,9 +986,9 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto defaultExpr = instr.GetDefaultExpr<HLIL_SWITCH>(); tokens.Append(KeywordToken, "match "); - GetExprText(condExpr, tokens, settings, asFullAst); + GetExprText(condExpr, tokens, settings); tokens.BeginScope(SwitchScopeType); - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -999,7 +999,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { for (const auto caseExpr: caseExprs) { - GetExprText(caseExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, exprType); + GetExprText(caseExpr, tokens, settings, TopLevelOperatorPrecedence, exprType); tokens.NewLine(); } @@ -1015,7 +1015,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe else { tokens.BeginScope(CaseScopeType); - GetExprText(defaultExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, exprType); + GetExprText(defaultExpr, tokens, settings, TopLevelOperatorPrecedence, exprType); tokens.EndScope(CaseScopeType); tokens.FinalizeScope(); } @@ -1038,11 +1038,11 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto& valueExpr = valueExprs[index]; if (index != 0) tokens.Append(TextToken, " | "); - GetExprText(valueExpr, tokens, settings, asFullAst); + GetExprText(valueExpr, tokens, settings); } tokens.Append(TextToken, " =>"); - if (!asFullAst) + if (!instr.ast) return; if (function->IsInstructionCollapsed(instr)) @@ -1052,7 +1052,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe else { tokens.BeginScope(CaseScopeType); - GetExprText(trueExpr, tokens, settings, asFullAst, TopLevelOperatorPrecedence, exprType); + GetExprText(trueExpr, tokens, settings, TopLevelOperatorPrecedence, exprType); tokens.EndScope(CaseScopeType); tokens.FinalizeScope(); } @@ -1080,13 +1080,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_ZX>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence, InnerExpression, false); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence, InnerExpression, false); tokens.Append(KeywordToken, " as "); AppendSizeToken(instr.size, false, tokens); if (parens) @@ -1101,13 +1101,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_SX>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence, InnerExpression, true); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence, InnerExpression, true); tokens.Append(KeywordToken, " as "); AppendSizeToken(instr.size, true, tokens); if (parens) @@ -1122,7 +1122,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto destExpr = instr.GetDestExpr<HLIL_CALL>(); const auto parameterExprs = instr.GetParameterExprs<HLIL_CALL>(); - GetExprText(destExpr, tokens, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprText(destExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendOpenParen(); vector<FunctionParameter> namedParams; @@ -1159,7 +1159,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe } if (!renderedAsString) - GetExprText(parameterExpr, tokens, settings, asFullAst); + GetExprText(parameterExpr, tokens, settings); } tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -1192,9 +1192,9 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_ARRAY_INDEX>(); const auto indexExpr = instr.GetIndexExpr<HLIL_ARRAY_INDEX>(); - GetExprText(srcExpr, tokens, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendOpenBracket(); - GetExprText(indexExpr, tokens, settings, asFullAst); + GetExprText(indexExpr, tokens, settings); tokens.AppendCloseBracket(); if (exprType != InnerExpression) tokens.AppendSemicolon(); @@ -1263,7 +1263,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe appearsDead = false; } - GetExprText(srcExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, AssignmentOperatorPrecedence); if (appearsDead) tokens.EndForceZeroConfidence(); @@ -1494,16 +1494,16 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto high = destExpr.GetHighExpr<HLIL_SPLIT>(); const auto low = destExpr.GetLowExpr<HLIL_SPLIT>(); - GetExprText(high, tokens, settings, asFullAst, precedence); + GetExprText(high, tokens, settings, precedence); tokens.Append(OperationToken, " = "); tokens.Append(OperationToken, "HIGH"); AppendSingleSizeToken(high.size, OperationToken, tokens); tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); tokens.AppendCloseParen(); tokens.AppendSemicolon(); tokens.NewLine(); - GetExprText(low, tokens, settings, asFullAst, precedence); + GetExprText(low, tokens, settings, precedence); } else { @@ -1617,7 +1617,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe } } - GetExprText(destExpr, tokens, settings, asFullAst, precedence); + GetExprText(destExpr, tokens, settings, precedence); if (assignUpdateOperator.has_value() && assignUpdateSource.has_value()) tokens.Append(OperationToken, assignUpdateOperator.value()); else @@ -1652,14 +1652,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe } else { - GetExprText(assignUpdateSource.value(), tokens, settings, asFullAst, AssignmentOperatorPrecedence, + GetExprText(assignUpdateSource.value(), tokens, settings, AssignmentOperatorPrecedence, InnerExpression, assignSignHint); } } else { - GetExprText(srcExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence, InnerExpression, - assignSignHint); + GetExprText(srcExpr, tokens, settings, AssignmentOperatorPrecedence, InnerExpression, assignSignHint); } if (isSplit) @@ -1678,9 +1677,9 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto destExprs = instr.GetDestExprs<HLIL_ASSIGN_UNPACK>(); const auto firstExpr = destExprs[0]; - GetExprText(firstExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprText(firstExpr, tokens, settings, AssignmentOperatorPrecedence); tokens.Append(OperationToken, " = "); - GetExprText(srcExpr, tokens, settings, asFullAst, AssignmentOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, AssignmentOperatorPrecedence); if (exprType != InnerExpression) tokens.AppendSemicolon(); }(); @@ -1700,7 +1699,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe if (!settings || settings->IsOptionSet(ShowTypeCasts)) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.Append(TextToken, "."); tokens.Append(OperationToken, "byte_offset"); @@ -1737,7 +1736,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe tokens.AppendOpenParen(); srcPrecedence = LowUnaryOperatorPrecedence; } - GetExprText(srcExpr, tokens, settings, true, srcPrecedence); + GetExprText(srcExpr, tokens, settings, srcPrecedence); if (!settings || settings->IsOptionSet(ShowTypeCasts)) { tokens.Append(KeywordToken, " as "); @@ -1754,7 +1753,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe } else { - GetExprText(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); } AppendFieldTextTokens(srcExpr, fieldOffset, memberIndex, instr.size, tokens, false); @@ -1878,7 +1877,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe srcPrecedence = LowUnaryOperatorPrecedence; } - GetExprText(srcExpr, tokens, settings, asFullAst, srcPrecedence); + GetExprText(srcExpr, tokens, settings, srcPrecedence); if (!settings || settings->IsOptionSet(ShowTypeCasts)) { @@ -1911,13 +1910,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe tokens.NewLine(); if (exprType != TrailingStatementExpression) tokens.Append(KeywordToken, "return "); - GetExprText(destExpr, tokens, settings, asFullAst, MemberAndFunctionOperatorPrecedence); + GetExprText(destExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendOpenParen(); for (size_t index{}; index < parameterExprs.size(); index++) { const auto& parameterExpr = parameterExprs[index]; if (index != 0) tokens.Append(TextToken, ", "); - GetExprText(parameterExpr, tokens, settings, asFullAst); + GetExprText(parameterExpr, tokens, settings); } tokens.AppendCloseParen(); if (exprType == StatementExpression) @@ -1932,7 +1931,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe if (parens) tokens.AppendOpenParen(); tokens.Append(OperationToken, "&"); - GetExprText(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, UnaryOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -1946,7 +1945,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > EqualityOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendComparison(" == ", instr, tokens, settings, asFullAst, EqualityOperatorPrecedence); + AppendComparison(" == ", instr, tokens, settings, EqualityOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -1960,7 +1959,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > EqualityOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendComparison(" != ", instr, tokens, settings, asFullAst, EqualityOperatorPrecedence); + AppendComparison(" != ", instr, tokens, settings, EqualityOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -1980,7 +1979,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe cmpSigned = false; else if (instr.operation == HLIL_CMP_SLT) cmpSigned = true; - AppendComparison(" < ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" < ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2000,7 +1999,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe cmpSigned = false; else if (instr.operation == HLIL_CMP_SLE) cmpSigned = true; - AppendComparison(" <= ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" <= ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2020,7 +2019,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe cmpSigned = false; else if (instr.operation == HLIL_CMP_SGE) cmpSigned = true; - AppendComparison(" >= ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" >= ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2040,7 +2039,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe cmpSigned = false; else if (instr.operation == HLIL_CMP_SGT) cmpSigned = true; - AppendComparison(" > ", instr, tokens, settings, asFullAst, CompareOperatorPrecedence, cmpSigned); + AppendComparison(" > ", instr, tokens, settings, CompareOperatorPrecedence, cmpSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2057,7 +2056,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(instr.size == 0 ? " && " : " & ", instr, tokens, settings, asFullAst, + AppendTwoOperand(instr.size == 0 ? " && " : " & ", instr, tokens, settings, instr.size == 0 ? LogicalAndOperatorPrecedence : BitwiseAndOperatorPrecedence); if (parens) tokens.AppendCloseParen(); @@ -2074,7 +2073,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(instr.size == 0 ? " || " : " | ", instr, tokens, settings, asFullAst, + AppendTwoOperand(instr.size == 0 ? " || " : " | ", instr, tokens, settings, instr.size == 0 ? LogicalOrOperatorPrecedence : BitwiseOrOperatorPrecedence); if (parens) tokens.AppendCloseParen(); @@ -2089,7 +2088,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe precedence == BitwiseOrOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" ^ ", instr, tokens, settings, asFullAst, BitwiseXorOperatorPrecedence); + AppendTwoOperand(" ^ ", instr, tokens, settings, BitwiseXorOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2119,7 +2118,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" + ", instr, tokens, settings, asFullAst, opPrecedence); + AppendTwoOperand(" + ", instr, tokens, settings, opPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2145,7 +2144,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe // Yes tokens.Append(OperationToken, "ADJ"); tokens.AppendOpenParen(); - GetExprText(left, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprText(left, tokens, settings, MemberAndFunctionOperatorPrecedence); tokens.AppendCloseParen(); return; } @@ -2168,7 +2167,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" - ", instr, tokens, settings, asFullAst, opPrecedence); + AppendTwoOperand(" - ", instr, tokens, settings, opPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2183,7 +2182,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe precedence == BitwiseXorOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" - ", instr, tokens, settings, asFullAst, SubOperatorPrecedence); + AppendTwoOperand(" - ", instr, tokens, settings, SubOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2196,7 +2195,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > ShiftOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" << ", instr, tokens, settings, asFullAst, ShiftOperatorPrecedence); + AppendTwoOperand(" << ", instr, tokens, settings, ShiftOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2210,7 +2209,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > ShiftOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - AppendTwoOperand(" >> ", instr, tokens, settings, asFullAst, ShiftOperatorPrecedence); + AppendTwoOperand(" >> ", instr, tokens, settings, ShiftOperatorPrecedence); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2234,7 +2233,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe mulSigned = false; else if (instr.operation == HLIL_MULS_DP) mulSigned = true; - AppendTwoOperand(" * ", instr, tokens, settings, asFullAst, MultiplyOperatorPrecedence, mulSigned); + AppendTwoOperand(" * ", instr, tokens, settings, MultiplyOperatorPrecedence, mulSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2258,7 +2257,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe divSigned = false; else if (instr.operation == HLIL_DIVS || instr.operation == HLIL_DIVS_DP) divSigned = true; - AppendTwoOperand(" / ", instr, tokens, settings, asFullAst, DivideOperatorPrecedence, divSigned); + AppendTwoOperand(" / ", instr, tokens, settings, DivideOperatorPrecedence, divSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2281,7 +2280,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe modSigned = false; else if (instr.operation == HLIL_MODS || instr.operation == HLIL_MODS_DP) modSigned = true; - AppendTwoOperand(" % ", instr, tokens, settings, asFullAst, DivideOperatorPrecedence, modSigned); + AppendTwoOperand(" % ", instr, tokens, settings, DivideOperatorPrecedence, modSigned); if (parens) tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2443,7 +2442,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe tokens.AppendOpenParen(); tokens.Append(OperationToken, "-"); tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, UnaryOperatorPrecedence, InnerExpression, true); + GetExprText(srcExpr, tokens, settings, UnaryOperatorPrecedence, InnerExpression, true); tokens.AppendCloseParen(); if (parens) tokens.AppendCloseParen(); @@ -2457,7 +2456,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_FLOAT_CONV>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); return; } const auto floatType = "f" + std::to_string(instr.size * 8); @@ -2465,7 +2464,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence); tokens.Append(KeywordToken, " as "); tokens.Append(TypeNameToken, floatType.c_str()); if (parens) @@ -2480,14 +2479,14 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_FLOAT_TO_INT>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence); tokens.Append(KeywordToken, " as "); AppendSizeToken(instr.size, true, tokens); if (parens) @@ -2504,7 +2503,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence); tokens.Append(KeywordToken, " as "); AppendSizeToken(instr.size, true, tokens); if (parens) @@ -2519,7 +2518,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_INT_TO_FLOAT>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); return; } const auto floatType = "f" + std::to_string(instr.size * 8); @@ -2527,7 +2526,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence); tokens.Append(KeywordToken, " as "); tokens.Append(TypeNameToken, floatType.c_str()); if (parens) @@ -2549,7 +2548,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe { const auto& parameterExpr = parameterExprs[index]; if (index != 0) tokens.Append(TextToken, ", "); - GetExprText(parameterExpr, tokens, settings, asFullAst); + GetExprText(parameterExpr, tokens, settings); } tokens.AppendCloseParen(); if (exprType != InnerExpression) @@ -2561,11 +2560,11 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe [&]() { const auto srcExprs = instr.GetSourceExprs<HLIL_RET>(); - if (!asFullAst || exprType != TrailingStatementExpression) + if (!instr.ast || exprType != TrailingStatementExpression) tokens.Append(KeywordToken, "return"); if (srcExprs.size() != 0) { - if (!asFullAst || exprType != TrailingStatementExpression) + if (!instr.ast || exprType != TrailingStatementExpression) tokens.Append(TextToken, " "); if (srcExprs.size() > 1) tokens.AppendOpenParen(); @@ -2574,7 +2573,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto& srcExpr = srcExprs[index]; if (index != 0) tokens.Append(TextToken, ", "); - GetExprText(srcExpr, tokens, settings, asFullAst); + GetExprText(srcExpr, tokens, settings); } if (srcExprs.size() > 1) tokens.AppendCloseParen(); @@ -2656,7 +2655,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe } else { - GetExprText(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); symbolType = OtherSymbolResult; } @@ -2699,7 +2698,7 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe } else { - GetExprText(srcExpr, tokens, settings, true, MemberAndFunctionOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, MemberAndFunctionOperatorPrecedence); } tokens.Append(TextToken, "."); @@ -2866,13 +2865,13 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe const auto srcExpr = instr.GetSourceExpr<HLIL_LOW_PART>(); if (settings && !settings->IsOptionSet(ShowTypeCasts)) { - GetExprText(srcExpr, tokens, settings, asFullAst, precedence); + GetExprText(srcExpr, tokens, settings, precedence); return; } bool parens = precedence > LowUnaryOperatorPrecedence; if (parens) tokens.AppendOpenParen(); - GetExprText(srcExpr, tokens, settings, asFullAst, LowUnaryOperatorPrecedence); + GetExprText(srcExpr, tokens, settings, LowUnaryOperatorPrecedence); tokens.Append(KeywordToken, " as "); AppendSizeToken(instr.size, signedHint.value_or(true), tokens); if (parens) @@ -2900,9 +2899,9 @@ void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLe void PseudoRustFunction::GetExprText(const HighLevelILInstruction& instr, HighLevelILTokenEmitter& tokens, - DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, bool statement) + DisassemblySettings* settings, BNOperatorPrecedence precedence, bool statement) { - GetExprText(instr, tokens, settings, asFullAst, precedence, statement ? TrailingStatementExpression : InnerExpression); + GetExprText(instr, tokens, settings, precedence, statement ? TrailingStatementExpression : InnerExpression); } diff --git a/lang/rust/pseudorust.h b/lang/rust/pseudorust.h index a6494319..a2ab9584 100644 --- a/lang/rust/pseudorust.h +++ b/lang/rust/pseudorust.h @@ -31,10 +31,10 @@ class PseudoRustFunction: public BinaryNinja::LanguageRepresentationFunction void AppendSizeToken(size_t size, bool isSigned, BinaryNinja::HighLevelILTokenEmitter& emitter); void AppendSingleSizeToken(size_t size, BNInstructionTextTokenType type, BinaryNinja::HighLevelILTokenEmitter& emitter); void AppendComparison(const std::string& comparison, const BinaryNinja::HighLevelILInstruction& instr, - BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, bool asFullAst, + BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint = std::nullopt); void AppendTwoOperand(const std::string& operand, const BinaryNinja::HighLevelILInstruction& instr, - BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, bool asFullAst, + BinaryNinja::HighLevelILTokenEmitter& emitter, BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence, std::optional<bool> signedHint = std::nullopt); void AppendTwoOperandFunction(const std::string& function, const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, bool sizeToken = true); @@ -47,14 +47,14 @@ class PseudoRustFunction: public BinaryNinja::LanguageRepresentationFunction bool IsMutable(const BinaryNinja::Variable& var) const; void GetExprText(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens, - BinaryNinja::DisassemblySettings* settings, bool asFullAst = true, - BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, ExpressionType exprType = InnerExpression, - std::optional<bool> signedHint = std::nullopt); + BinaryNinja::DisassemblySettings* settings, BNOperatorPrecedence precedence = TopLevelOperatorPrecedence, + ExpressionType exprType = InnerExpression, std::optional<bool> signedHint = std::nullopt); protected: virtual void InitTokenEmitter(BinaryNinja::HighLevelILTokenEmitter& tokens) override; - virtual void GetExprText(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens, - BinaryNinja::DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, bool statement) override; + virtual void GetExprText(const BinaryNinja::HighLevelILInstruction& instr, + BinaryNinja::HighLevelILTokenEmitter& tokens, BinaryNinja::DisassemblySettings* settings, + BNOperatorPrecedence precedence, bool statement) override; virtual void BeginLines(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens) override; virtual void EndLines(const BinaryNinja::HighLevelILInstruction& instr, BinaryNinja::HighLevelILTokenEmitter& tokens) override; diff --git a/languagerepresentation.cpp b/languagerepresentation.cpp index 906b4901..6e3bf01b 100644 --- a/languagerepresentation.cpp +++ b/languagerepresentation.cpp @@ -31,13 +31,12 @@ LanguageRepresentationFunction::LanguageRepresentationFunction(BNLanguageReprese } -vector<DisassemblyTextLine> LanguageRepresentationFunction::GetExprText(const HighLevelILInstruction& instr, - DisassemblySettings* settings, bool asFullAst, BNOperatorPrecedence precedence, bool statement) +vector<DisassemblyTextLine> LanguageRepresentationFunction::GetExprText( + const HighLevelILInstruction& instr, DisassemblySettings* settings, BNOperatorPrecedence precedence, bool statement) { size_t count = 0; - BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionExprText(m_object, - instr.function->GetObject(), instr.exprIndex, settings ? settings->GetObject() : nullptr, - asFullAst, precedence, statement, &count); + BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionExprText(m_object, instr.function->GetObject(), + instr.exprIndex, settings ? settings->GetObject() : nullptr, instr.ast, precedence, statement, &count); vector<DisassemblyTextLine> result; result.reserve(count); @@ -58,11 +57,11 @@ vector<DisassemblyTextLine> LanguageRepresentationFunction::GetExprText(const Hi vector<DisassemblyTextLine> LanguageRepresentationFunction::GetLinearLines( - const HighLevelILInstruction& instr, DisassemblySettings* settings, bool asFullAst) + const HighLevelILInstruction& instr, DisassemblySettings* settings) { size_t count = 0; BNDisassemblyTextLine* lines = BNGetLanguageRepresentationFunctionLinearLines(m_object, instr.function->GetObject(), - instr.exprIndex, settings ? settings->GetObject() : nullptr, asFullAst, &count); + instr.exprIndex, settings ? settings->GetObject() : nullptr, instr.ast, &count); vector<DisassemblyTextLine> result; result.reserve(count); @@ -167,10 +166,10 @@ void LanguageRepresentationFunction::GetExprTextCallback(void* ctxt, BNHighLevel { LanguageRepresentationFunction* func = (LanguageRepresentationFunction*)ctxt; Ref<HighLevelILFunction> ilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(il)); - HighLevelILInstruction instr = ilObj->GetExpr(exprIndex); + HighLevelILInstruction instr = ilObj->GetExpr(exprIndex, asFullAst); Ref<HighLevelILTokenEmitter> tokenObj = new HighLevelILTokenEmitter(BNNewHighLevelILTokenEmitterReference(tokens)); Ref<DisassemblySettings> settingsObj = settings ? new DisassemblySettings(BNNewDisassemblySettingsReference(settings)) : nullptr; - func->GetExprText(instr, *tokenObj, settingsObj, asFullAst, precedence, statement); + func->GetExprText(instr, *tokenObj, settingsObj, precedence, statement); } @@ -230,8 +229,8 @@ CoreLanguageRepresentationFunction::CoreLanguageRepresentationFunction(BNLanguag } -void CoreLanguageRepresentationFunction::GetExprText(const HighLevelILInstruction&, HighLevelILTokenEmitter&, - DisassemblySettings*, bool, BNOperatorPrecedence, bool statement) +void CoreLanguageRepresentationFunction::GetExprText( + const HighLevelILInstruction&, HighLevelILTokenEmitter&, DisassemblySettings*, BNOperatorPrecedence, bool) { } diff --git a/python/examples/pseudo_python.py b/python/examples/pseudo_python.py index b68b4786..6a9e401b 100644 --- a/python/examples/pseudo_python.py +++ b/python/examples/pseudo_python.py @@ -42,7 +42,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): def perform_get_expr_text( self, instr: HighLevelILInstruction, tokens: HighLevelILTokenEmitter, settings: Optional[DisassemblySettings], - as_full_ast: bool = True, precedence: OperatorPrecedence = OperatorPrecedence.TopLevelOperatorPrecedence, + precedence: OperatorPrecedence = OperatorPrecedence.TopLevelOperatorPrecedence, statement: bool = False ): with tokens.expr(instr): @@ -52,7 +52,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): # Emit the lines for each statement in the body for (idx, i) in enumerate(body): # Don't display trailing return statements that don't have values - if (as_full_ast and idx + 1 == len(body) and i.operation == HighLevelILOperation.HLIL_RET and + if (instr.as_ast and idx + 1 == len(body) and i.operation == HighLevelILOperation.HLIL_RET and len(i.src) == 0 and instr.expr_index == self.hlil.root.expr_index): continue @@ -66,18 +66,17 @@ class PseudoPythonFunction(LanguageRepresentationFunction): need_separator = has_blocks # Emit the lines for the statement itself - self.perform_get_expr_text(i, tokens, settings, as_full_ast, - OperatorPrecedence.TopLevelOperatorPrecedence, True) + self.perform_get_expr_text(i, tokens, settings, OperatorPrecedence.TopLevelOperatorPrecedence, True) tokens.new_line() elif instr.operation == HighLevelILOperation.HLIL_IF: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "if ")) self.perform_get_expr_text(instr.condition, tokens, settings) tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, ":\n")) - if as_full_ast: + if instr.as_ast: # Only display the if body when printing the full AST. When printing basic blocks in graph view, # the body of the if and the else part are rendered as other nodes in the graph. tokens.begin_scope(ScopeType.BlockScopeType) - self.perform_get_expr_text(instr.true, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.true, tokens, settings, OperatorPrecedence.TopLevelOperatorPrecedence, True) tokens.end_scope(ScopeType.BlockScopeType) @@ -88,10 +87,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): HighLevelILOperation.HLIL_UNREACHABLE]: if if_chain.operation == HighLevelILOperation.HLIL_IF: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "elif ")) - self.perform_get_expr_text(if_chain.condition, tokens, settings, as_full_ast) + self.perform_get_expr_text(if_chain.condition, tokens, settings) tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken, ":")) tokens.begin_scope(ScopeType.BlockScopeType) - self.perform_get_expr_text(if_chain.true, tokens, settings, as_full_ast, + self.perform_get_expr_text(if_chain.true, tokens, settings, OperatorPrecedence.TopLevelOperatorPrecedence, True) tokens.end_scope(ScopeType.BlockScopeType) if_chain = if_chain.false @@ -99,7 +98,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "else")) tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken, ":")) tokens.begin_scope(ScopeType.BlockScopeType) - self.perform_get_expr_text(if_chain, tokens, settings, as_full_ast, + self.perform_get_expr_text(if_chain, tokens, settings, OperatorPrecedence.TopLevelOperatorPrecedence, True) tokens.end_scope(ScopeType.BlockScopeType) break @@ -153,49 +152,57 @@ class PseudoPythonFunction(LanguageRepresentationFunction): if instr.update.operation != HighLevelILOperation.HLIL_NOP: self.perform_get_expr_text(instr.update, tokens, settings) tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, ":")) - tokens.begin_scope(ScopeType.BlockScopeType) - self.perform_get_expr_text(instr.body, tokens, settings, as_full_ast, - OperatorPrecedence.TopLevelOperatorPrecedence, True) - tokens.end_scope(ScopeType.BlockScopeType) + if instr.as_ast: + tokens.begin_scope(ScopeType.BlockScopeType) + self.perform_get_expr_text(instr.body, tokens, settings, + OperatorPrecedence.TopLevelOperatorPrecedence, True) + tokens.end_scope(ScopeType.BlockScopeType) elif instr.operation == HighLevelILOperation.HLIL_WHILE: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "while ")) - self.perform_get_expr_text(instr.condition, tokens, settings, as_full_ast) + self.perform_get_expr_text(instr.condition, tokens, settings) tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, ":")) - tokens.begin_scope(ScopeType.BlockScopeType) - self.perform_get_expr_text(instr.body, tokens, settings, as_full_ast, - OperatorPrecedence.TopLevelOperatorPrecedence, True) - tokens.end_scope(ScopeType.BlockScopeType) + if instr.as_ast: + tokens.begin_scope(ScopeType.BlockScopeType) + self.perform_get_expr_text(instr.body, tokens, settings, + OperatorPrecedence.TopLevelOperatorPrecedence, True) + tokens.end_scope(ScopeType.BlockScopeType) elif instr.operation == HighLevelILOperation.HLIL_DO_WHILE: - tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "do")) - tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, ":")) - tokens.begin_scope(ScopeType.BlockScopeType) - self.perform_get_expr_text(instr.body, tokens, settings, as_full_ast, - OperatorPrecedence.TopLevelOperatorPrecedence, True) - tokens.end_scope(ScopeType.BlockScopeType) - tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "while ")) - self.perform_get_expr_text(instr.condition, tokens, settings, as_full_ast) + if instr.as_ast: + tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "do")) + tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, ":")) + tokens.begin_scope(ScopeType.BlockScopeType) + self.perform_get_expr_text(instr.body, tokens, settings, + OperatorPrecedence.TopLevelOperatorPrecedence, True) + tokens.end_scope(ScopeType.BlockScopeType) + tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "while ")) + self.perform_get_expr_text(instr.condition, tokens, settings) + else: + tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "do ")) + tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "while ")) + self.perform_get_expr_text(instr.condition, tokens, settings) elif instr.operation == HighLevelILOperation.HLIL_SWITCH: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "match ")) self.perform_get_expr_text(instr.condition, tokens, settings) tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, ":")) tokens.begin_scope(ScopeType.SwitchScopeType) - # Output each case - for case in instr.cases: - self.perform_get_expr_text(case, tokens, settings, as_full_ast, - OperatorPrecedence.TopLevelOperatorPrecedence, True) - tokens.new_line() + if instr.as_ast: + # Output each case + for case in instr.cases: + self.perform_get_expr_text(case, tokens, settings, + OperatorPrecedence.TopLevelOperatorPrecedence, True) + tokens.new_line() - # Check for default case - if instr.default is not None and instr.default.operation not in [HighLevelILOperation.HLIL_NOP, - HighLevelILOperation.HLIL_UNREACHABLE]: - tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "default")) - tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken, ":")) - tokens.begin_scope(ScopeType.CaseScopeType) - self.perform_get_expr_text(instr.default, tokens, settings, as_full_ast, - OperatorPrecedence.TopLevelOperatorPrecedence, True) - tokens.end_scope(ScopeType.CaseScopeType) - tokens.end_scope(ScopeType.SwitchScopeType) + # Check for default case + if instr.default is not None and instr.default.operation not in [HighLevelILOperation.HLIL_NOP, + HighLevelILOperation.HLIL_UNREACHABLE]: + tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "default")) + tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken, ":")) + tokens.begin_scope(ScopeType.CaseScopeType) + self.perform_get_expr_text(instr.default, tokens, settings, + OperatorPrecedence.TopLevelOperatorPrecedence, True) + tokens.end_scope(ScopeType.CaseScopeType) + tokens.end_scope(ScopeType.SwitchScopeType) elif instr.operation == HighLevelILOperation.HLIL_CASE: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "case ")) for (i, value) in enumerate(instr.values): @@ -204,7 +211,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): self.perform_get_expr_text(value, tokens, settings) tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken, ":")) tokens.begin_scope(ScopeType.CaseScopeType) - self.perform_get_expr_text(instr.body, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.body, tokens, settings, OperatorPrecedence.TopLevelOperatorPrecedence, True) tokens.end_scope(ScopeType.CaseScopeType) elif instr.operation == HighLevelILOperation.HLIL_BREAK: @@ -212,7 +219,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): elif instr.operation == HighLevelILOperation.HLIL_CONTINUE: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "continue")) elif instr.operation == HighLevelILOperation.HLIL_CALL: - self.perform_get_expr_text(instr.dest, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.dest, tokens, settings, OperatorPrecedence.MemberAndFunctionOperatorPrecedence) tokens.append_open_paren() for (i, param) in enumerate(instr.params): @@ -222,7 +229,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): tokens.append_close_paren() elif instr.operation == HighLevelILOperation.HLIL_TAILCALL: tokens.append(InstructionTextToken(InstructionTextTokenType.KeywordToken, "return ")) - self.perform_get_expr_text(instr.dest, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.dest, tokens, settings, OperatorPrecedence.MemberAndFunctionOperatorPrecedence) tokens.append_open_paren() for (i, param) in enumerate(instr.params): @@ -278,7 +285,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): tokens.append_pointer_text_token(instr, instr.constant, settings, SymbolDisplayType.DereferenceNonDataSymbols, precedence) elif instr.operation == HighLevelILOperation.HLIL_ARRAY_INDEX: - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.MemberAndFunctionOperatorPrecedence) tokens.append_open_bracket() self.perform_get_expr_text(instr.index, tokens, settings) @@ -301,7 +308,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): # For the right side of the assignment, only use zero confidence if the instruction does # not have any side effects with tokens.force_zero_confidence(appears_dead and not instr.src.has_side_effects): - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.AssignmentOperatorPrecedence) elif instr.operation == HighLevelILOperation.HLIL_VAR_DECLARE: tokens.append_var_text_token(instr.var, instr, instr.size) @@ -402,14 +409,14 @@ class PseudoPythonFunction(LanguageRepresentationFunction): # If the variable does not appear live, show the assignment as zero confidence (grayed out) with tokens.force_zero_confidence(appears_dead): - self.perform_get_expr_text(instr.dest, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.dest, tokens, settings, OperatorPrecedence.AssignmentOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " = ")) # For the right side of the assignment, only use zero confidence if the instruction does # not have any side effects with tokens.force_zero_confidence(appears_dead and not instr.src.has_side_effects): - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.AssignmentOperatorPrecedence) elif instr.operation == HighLevelILOperation.HLIL_ASSIGN_UNPACK: tokens.append_open_paren() @@ -419,13 +426,13 @@ class PseudoPythonFunction(LanguageRepresentationFunction): self.perform_get_expr_text(dest, tokens, settings) tokens.append_close_paren() tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " = ")) - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.AssignmentOperatorPrecedence) elif instr.operation == HighLevelILOperation.HLIL_STRUCT_FIELD: parens = precedence > OperatorPrecedence.MemberAndFunctionOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.MemberAndFunctionOperatorPrecedence) self.append_field_text_tokens(instr.src, instr.offset, instr.member_index, instr.size, tokens) if parens: @@ -439,7 +446,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): SymbolDisplayType.DisplaySymbolOnly, OperatorPrecedence.MemberAndFunctionOperatorPrecedence) else: - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.MemberAndFunctionOperatorPrecedence) self.append_field_text_tokens(instr.src, instr.offset, instr.member_index, instr.size, tokens) if parens: @@ -463,7 +470,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): if parens: tokens.append_open_paren() tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, "*")) - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.UnaryOperatorPrecedence) if parens: tokens.append_close_paren() @@ -472,7 +479,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): if parens: tokens.append_open_paren() tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, "&")) - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.UnaryOperatorPrecedence) if parens: tokens.append_close_paren() @@ -480,10 +487,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.EqualityOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.left, tokens, settings, OperatorPrecedence.EqualityOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " == ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.right, tokens, settings, OperatorPrecedence.EqualityOperatorPrecedence) if parens: tokens.append_close_paren() @@ -491,10 +498,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.EqualityOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.left, tokens, settings, OperatorPrecedence.EqualityOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " != ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.right, tokens, settings, OperatorPrecedence.EqualityOperatorPrecedence) if parens: tokens.append_close_paren() @@ -503,10 +510,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.CompareOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.left, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " < ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.right, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) if parens: tokens.append_close_paren() @@ -515,10 +522,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.CompareOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.left, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " <= ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.right, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) if parens: tokens.append_close_paren() @@ -527,10 +534,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.CompareOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.left, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " >= ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.right, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) if parens: tokens.append_close_paren() @@ -539,10 +546,10 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.CompareOperatorPrecedence if parens: tokens.append_open_paren() - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.left, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, " > ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.right, tokens, settings, OperatorPrecedence.CompareOperatorPrecedence) if parens: tokens.append_close_paren() @@ -561,7 +568,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): precedence = OperatorPrecedence.BitwiseAndOperatorPrecedence if parens: tokens.append_open_paren() - self.append_two_operand_tokens(operation, instr, tokens, settings, as_full_ast, precedence) + self.append_two_operand_tokens(operation, instr, tokens, settings, precedence) if parens: tokens.append_close_paren() elif instr.operation == HighLevelILOperation.HLIL_OR: @@ -579,7 +586,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): precedence = OperatorPrecedence.BitwiseOrOperatorPrecedence if parens: tokens.append_open_paren() - self.append_two_operand_tokens(operation, instr, tokens, settings, as_full_ast, precedence) + self.append_two_operand_tokens(operation, instr, tokens, settings, precedence) if parens: tokens.append_close_paren() elif instr.operation == HighLevelILOperation.HLIL_XOR: @@ -588,7 +595,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseOrOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("^", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("^", instr, tokens, settings, OperatorPrecedence.BitwiseXorOperatorPrecedence) if parens: tokens.append_close_paren() @@ -640,7 +647,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseXorOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("+", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("+", instr, tokens, settings, OperatorPrecedence.AddOperatorPrecedence) if parens: tokens.append_close_paren() @@ -652,7 +659,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseXorOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("-", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("-", instr, tokens, settings, OperatorPrecedence.SubOperatorPrecedence) if parens: tokens.append_close_paren() @@ -665,7 +672,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseXorOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("*", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("*", instr, tokens, settings, OperatorPrecedence.MultiplyOperatorPrecedence) if parens: tokens.append_close_paren() @@ -678,7 +685,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseXorOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("//", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("//", instr, tokens, settings, OperatorPrecedence.DivideOperatorPrecedence) if parens: tokens.append_close_paren() @@ -691,7 +698,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseXorOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("%", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("%", instr, tokens, settings, OperatorPrecedence.DivideOperatorPrecedence) if parens: tokens.append_close_paren() @@ -703,7 +710,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): OperatorPrecedence.BitwiseXorOperatorPrecedence]) if parens: tokens.append_open_paren() - self.append_two_operand_tokens("/", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("/", instr, tokens, settings, OperatorPrecedence.DivideOperatorPrecedence) if parens: tokens.append_close_paren() @@ -711,7 +718,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.ShiftOperatorPrecedence if parens: tokens.append_open_paren() - self.append_two_operand_tokens("<<", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens("<<", instr, tokens, settings, OperatorPrecedence.ShiftOperatorPrecedence) if parens: tokens.append_close_paren() @@ -719,7 +726,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): parens = precedence > OperatorPrecedence.ShiftOperatorPrecedence if parens: tokens.append_open_paren() - self.append_two_operand_tokens(">>", instr, tokens, settings, as_full_ast, + self.append_two_operand_tokens(">>", instr, tokens, settings, OperatorPrecedence.ShiftOperatorPrecedence) if parens: tokens.append_close_paren() @@ -879,7 +886,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, "not ")) else: tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, "~")) - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.UnaryOperatorPrecedence) if parens: tokens.append_close_paren() @@ -888,7 +895,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): if parens: tokens.append_open_paren() tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, "-")) - self.perform_get_expr_text(instr.src, tokens, settings, as_full_ast, + self.perform_get_expr_text(instr.src, tokens, settings, OperatorPrecedence.UnaryOperatorPrecedence) if parens: tokens.append_close_paren() @@ -985,8 +992,7 @@ class PseudoPythonFunction(LanguageRepresentationFunction): f"# unimplemented {instr.operation.name}")) def append_two_operand_tokens(self, operation: str, instr: HighLevelILInstruction, tokens: HighLevelILTokenEmitter, - settings: Optional[DisassemblySettings], as_full_ast: bool, - precedence: OperatorPrecedence): + settings: Optional[DisassemblySettings], precedence: OperatorPrecedence): if precedence == OperatorPrecedence.SubOperatorPrecedence: # Treat left side of subtraction as same level as addition. This lets # (a - b) - c be represented as a - b - c, but a - (b - c) does not @@ -1000,9 +1006,9 @@ class PseudoPythonFunction(LanguageRepresentationFunction): else: left_precedence = precedence - self.perform_get_expr_text(instr.left, tokens, settings, as_full_ast, left_precedence) + self.perform_get_expr_text(instr.left, tokens, settings, left_precedence) tokens.append(InstructionTextToken(InstructionTextTokenType.OperationToken, f" {operation} ")) - self.perform_get_expr_text(instr.right, tokens, settings, as_full_ast, precedence) + self.perform_get_expr_text(instr.right, tokens, settings, precedence) def append_field_text_tokens(self, var: HighLevelILInstruction, offset: int, member_index: int, size: int, tokens: HighLevelILTokenEmitter): diff --git a/python/highlevelil.py b/python/highlevelil.py index 21359685..8a400c1e 100644 --- a/python/highlevelil.py +++ b/python/highlevelil.py @@ -696,7 +696,10 @@ class HighLevelILInstruction(BaseILInstruction): return variable.ConstantData(value, 0, state, core.max_confidence, self.core_instr.size, self.function.source_function) def get_expr(self, operand_index: int) -> 'HighLevelILInstruction': - return HighLevelILInstruction.create(self.function, ExpressionIndex(self.core_instr.operands[operand_index])) + return HighLevelILInstruction.create( + self.function, ExpressionIndex(self.core_instr.operands[operand_index]), + self.as_ast + ) def get_intrinsic(self, operand_index: int) -> 'lowlevelil.ILIntrinsic': if self.function.arch is None: @@ -2801,7 +2804,7 @@ class HighLevelILFunction: """ return core.BNGetHighLevelILExprCount(self.handle) - def get_expr(self, index: ExpressionIndex) -> Optional[HighLevelILInstruction]: + def get_expr(self, index: ExpressionIndex, as_ast: bool = True) -> Optional[HighLevelILInstruction]: """ ``get_expr`` retrieves the IL expression at a given expression index in the function. @@ -2809,12 +2812,13 @@ class HighLevelILFunction: they might not be used by the function and might not contain properly structured data. :param index: Index of desired expression in function + :param as_ast: Whether to return the expression as a full AST or a single instruction (defaults to AST) :return: A HighLevelILInstruction object for the expression, if it exists. Otherwise, None """ if index >= self.get_expr_count(): return None - return HighLevelILInstruction.create(self, index) + return HighLevelILInstruction.create(self, index, as_ast) def copy_expr(self, original: HighLevelILInstruction) -> ExpressionIndex: """ diff --git a/python/languagerepresentation.py b/python/languagerepresentation.py index 49a300c1..ac34c860 100644 --- a/python/languagerepresentation.py +++ b/python/languagerepresentation.py @@ -395,11 +395,11 @@ class LanguageRepresentationFunction: ): try: hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil)) - instr = hlil.get_expr(highlevelil.ExpressionIndex(expr_index)) + instr = hlil.get_expr(highlevelil.ExpressionIndex(expr_index), as_full_ast) tokens = HighLevelILTokenEmitter(core.BNNewHighLevelILTokenEmitterReference(tokens)) if settings is not None: settings = function.DisassemblySettings(core.BNNewDisassemblySettingsReference(settings)) - self.perform_get_expr_text(instr, tokens, settings, as_full_ast, precedence, statement) + self.perform_get_expr_text(instr, tokens, settings, precedence, statement) except: log_error(traceback.format_exc()) @@ -461,7 +461,7 @@ class LanguageRepresentationFunction: def perform_get_expr_text( self, instr: 'highlevelil.HighLevelILInstruction', tokens: HighLevelILTokenEmitter, - settings: Optional['function.DisassemblySettings'], as_full_ast: bool = True, + settings: Optional['function.DisassemblySettings'], precedence: OperatorPrecedence = OperatorPrecedence.TopLevelOperatorPrecedence, statement: bool = False ): """ @@ -481,15 +481,17 @@ class LanguageRepresentationFunction: def get_expr_text( self, instr: 'highlevelil.HighLevelILInstruction', settings: Optional['function.DisassemblySettings'], - as_full_ast: bool = True, precedence: OperatorPrecedence = OperatorPrecedence.TopLevelOperatorPrecedence, + precedence: OperatorPrecedence = OperatorPrecedence.TopLevelOperatorPrecedence, statement: bool = False ) -> List['function.DisassemblyTextLine']: """Gets the lines of tokens for a given High Level IL instruction.""" count = ctypes.c_ulonglong() if settings is not None: settings = settings.handle - lines = core.BNGetLanguageRepresentationFunctionExprText(self.handle, instr.function.handle, instr.expr_index, - settings, as_full_ast, precedence, statement, count) + lines = core.BNGetLanguageRepresentationFunctionExprText( + self.handle, instr.function.handle, instr.expr_index, + settings, instr.as_ast, precedence, statement, count + ) result = [] if lines is not None: result = [] @@ -507,8 +509,7 @@ class LanguageRepresentationFunction: def get_linear_lines( self, instr: 'highlevelil.HighLevelILInstruction', - settings: Optional['function.DisassemblySettings'] = None, - as_full_ast: bool = True + settings: Optional['function.DisassemblySettings'] = None ) -> List['function.DisassemblyTextLine']: """ Generates lines for the given High Level IL instruction in the style of the linear view. To get the lines @@ -517,8 +518,10 @@ class LanguageRepresentationFunction: count = ctypes.c_ulonglong() if settings is not None: settings = settings.handle - lines = core.BNGetLanguageRepresentationFunctionLinearLines(self.handle, instr.function.handle, instr.expr_index, - settings, as_full_ast, count) + lines = core.BNGetLanguageRepresentationFunctionLinearLines( + self.handle, instr.function.handle, instr.expr_index, + settings, instr.as_ast, count + ) result = [] if lines is not None: result = [] |
