Skip to content

Commit

Permalink
Refactoring switch node handler (#423)
Browse files Browse the repository at this point in the history
Co-authored-by: mari-mari <11994049+mari-mari@users.noreply.github.com>
Co-authored-by: Steffen Enders <steffen.enders@fkie.fraunhofer.de>
  • Loading branch information
3 people authored Jun 26, 2024
1 parent c7098e4 commit 6613dd2
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from decompiler.pipeline.controlflowanalysis.restructuring_options import LoopBreakOptions, RestructuringOptions
from decompiler.structures.ast.ast_nodes import AbstractSyntaxTreeNode, CaseNode, FalseNode, SwitchNode, TrueNode
from decompiler.structures.ast.condition_symbol import ConditionHandler
from decompiler.structures.ast.switch_node_handler import ExpressionUsages
from decompiler.structures.ast.condition_symbol import ConditionHandler, ExpressionUsages
from decompiler.structures.ast.syntaxforest import AbstractSyntaxForest
from decompiler.structures.logic.logic_condition import LogicCondition, PseudoLogicCondition
from decompiler.structures.pseudo import Condition, Constant, Expression, OperationType
Expand Down Expand Up @@ -110,14 +109,14 @@ def _get_expression_compared_with_constant(self, reaching_condition: LogicCondit
Check whether the given reaching condition, which is a literal, i.e., a z3-symbol or its negation is of the form `expr == const`.
If this is the case, then we return the expression `expr`.
"""
return self.asforest.switch_node_handler.get_potential_switch_expression(reaching_condition)
return self.asforest.condition_handler.get_potential_switch_expression_of(reaching_condition)

def _get_constant_compared_with_expression(self, reaching_condition: LogicCondition) -> Optional[Constant]:
"""
Check whether the given reaching condition, which is a literal, i.e., a z3-symbol or its negation is of the form `expr == const`.
If this is the case, then we return the constant `const`.
"""
return self.asforest.switch_node_handler.get_potential_switch_constant(reaching_condition)
return self.asforest.condition_handler.get_potential_switch_constant_of(reaching_condition)

def _convert_to_z3_condition(self, condition: LogicCondition) -> PseudoLogicCondition:
return PseudoLogicCondition.initialize_from_formula(condition, self.condition_handler.get_z3_condition_map())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
)
from decompiler.pipeline.controlflowanalysis.restructuring_options import RestructuringOptions
from decompiler.structures.ast.ast_nodes import AbstractSyntaxTreeNode, CaseNode, CodeNode, ConditionNode, SeqNode, SwitchNode, TrueNode
from decompiler.structures.ast.condition_symbol import ExpressionUsages
from decompiler.structures.ast.reachability_graph import CaseDependencyGraph, LinearOrderDependency, SiblingReachability
from decompiler.structures.ast.switch_node_handler import ExpressionUsages
from decompiler.structures.ast.syntaxforest import AbstractSyntaxForest
from decompiler.structures.logic.logic_condition import LogicCondition
from decompiler.structures.pseudo import Constant, Expression
Expand Down Expand Up @@ -90,8 +90,8 @@ def _clean_up_reachability(self):
"""
for candidate_1, candidate_2 in permutations(self.switch_candidate.cases, 2):
if self.sibling_reachability.reaches(candidate_1.node, candidate_2.node) and not (
set(self.asforest.switch_node_handler.get_constants_for(candidate_1.condition))
& set(self.asforest.switch_node_handler.get_constants_for(candidate_2.condition))
set(self.asforest.condition_handler.get_constants_of(candidate_1.condition))
& set(self.asforest.condition_handler.get_constants_of(candidate_2.condition))
):
self.asforest._code_node_reachability_graph.remove_reachability_between([candidate_1.node, candidate_2.node])
self.sibling_reachability.remove_reachability_between([candidate_1.node, candidate_2.node])
Expand Down Expand Up @@ -521,7 +521,7 @@ def _add_constants_to_cases_for(
case_node.constant = Constant("add_to_previous_case")
else:
considered_conditions.update(
(c, l) for l, c in self.asforest.switch_node_handler.get_literal_and_constant_for(case_node.reaching_condition)
(c, l) for l, c in self.asforest.condition_handler.get_literal_and_constant_of(case_node.reaching_condition)
)

def _update_reaching_condition_of(self, case_node: CaseNode, considered_conditions: Dict[Constant, LogicCondition]) -> None:
Expand All @@ -537,8 +537,7 @@ def _update_reaching_condition_of(self, case_node: CaseNode, considered_conditio
:param considered_conditions: The conditions (literals) that are already fulfilled when we reach the given case node.
"""
constant_of_case_node_literal = {
const: literal
for literal, const in self.asforest.switch_node_handler.get_literal_and_constant_for(case_node.reaching_condition)
const: literal for literal, const in self.asforest.condition_handler.get_literal_and_constant_of(case_node.reaching_condition)
}
exception_condition: LogicCondition = self.condition_handler.get_true_value()

Expand Down Expand Up @@ -578,7 +577,7 @@ def prepend_empty_cases_to_case_with_or_condition(self, case: CaseNode) -> List[
the list of new case nodes.
"""
condition_for_constant: Dict[Constant, LogicCondition] = dict()
for l, c in self.asforest.switch_node_handler.get_literal_and_constant_for(case.reaching_condition):
for l, c in self.asforest.condition_handler.get_literal_and_constant_of(case.reaching_condition):
if c is None:
raise ValueError(
f"The case node should have a reaching-condition that is a disjunction of literals, but it has the clause {l}."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
)
from decompiler.pipeline.controlflowanalysis.restructuring_options import RestructuringOptions
from decompiler.structures.ast.ast_nodes import AbstractSyntaxTreeNode, ConditionNode, FalseNode, SeqNode, SwitchNode, TrueNode
from decompiler.structures.ast.condition_symbol import ExpressionUsages
from decompiler.structures.ast.reachability_graph import SiblingReachabilityGraph
from decompiler.structures.ast.switch_node_handler import ExpressionUsages
from decompiler.structures.ast.syntaxforest import AbstractSyntaxForest
from decompiler.structures.logic.logic_condition import LogicCondition, PseudoLogicCondition
from decompiler.structures.pseudo import Condition, Constant, OperationType
Expand Down
Loading

0 comments on commit 6613dd2

Please sign in to comment.