Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify branches with true or false conditions #379

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Set option to true
  • Loading branch information
fnhartmann committed Jan 29, 2024
commit d1ab68af0ae5ea9626968ec3fc7df172dcbc5272
2 changes: 1 addition & 1 deletion decompiler/backend/codevisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, task: DecompilerTask):
self._int_repr_scope: int = task.options.getint("code-generator.int_representation_scope", fallback=256)
self._neg_hex_as_twos_complement: bool = task.options.getboolean("code-generator.negative_hex_as_twos_complement", fallback=True)
self._aggressive_array_detection: bool = task.options.getboolean("code-generator.aggressive_array_detection", fallback=False)
self._simplify_branches: bool = task.options.getboolean("code-generator.simplify_branches", fallback=False)
self._simplify_branches: bool = task.options.getboolean("code-generator.simplify_branches", fallback=True)
self.task = task

def visit_seq_node(self, node: ast_nodes.SeqNode) -> str:
Expand Down
2 changes: 1 addition & 1 deletion decompiler/util/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@
},
{
"dest": "code-generator.simplify_branches",
"default": false,
"default": true,
"title": "Simplify branches with true or false conditions",
"type": "boolean",
"description": "Removes branches in the output that wont be reached because of a 'true' or 'false' condition",
Expand Down
12 changes: 6 additions & 6 deletions tests/backend/test_codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _generate_options(
twos_complement: bool = True,
array_detection: bool = False,
var_declarations_per_line: int = 1,
simplify_branches: bool = False,
simplify_branches: bool = True,
):
options = Options()
options.set("code-generator.max_complexity", max_complx)
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_function_with_true_condition(self):
ast._add_edges_from(((root, condition_node), (seq_node, code_node)))
assert self._regex_matches(
r"^%int +test_function\(%int +a%,%int +b%\)%{%int%c;%if%\(%true%\)%{%c%=%5%;%return%c%;%}%}%$".replace("%", "\\s*"),
self._task(ast, params=[var_a.copy(), var_b.copy()]),
self._task(ast, params=[var_a.copy(), var_b.copy()], options=_generate_options(simplify_branches=False)),
)

def test_function_with_simplified_true_condition(self):
Expand All @@ -264,7 +264,7 @@ def test_function_with_simplified_true_condition(self):
ast._add_edges_from(((root, condition_node), (seq_node, code_node)))
assert self._regex_matches(
r"^%int +test_function\(%int +a%,%int +b%\)%{%int%c;%c%=%5%;%return%c%;%}%$".replace("%", "\\s*"),
self._task(ast, params=[var_a.copy(), var_b.copy()], options=_generate_options(simplify_branches=True)),
self._task(ast, params=[var_a.copy(), var_b.copy()]),
)

def test_function_with_simplified_false_condition(self):
Expand All @@ -291,7 +291,7 @@ def test_function_with_simplified_false_condition(self):
ast._add_edges_from(((root, condition_node), (true_seq_node, true_code_node), (false_seq_node, false_code_node)))
assert self._regex_matches(
r"^%int +test_function\(%int +a%,%int +b%\)%{%int%c;%return%0%;%}%$".replace("%", "\\s*"),
self._task(ast, params=[var_a.copy(), var_b.copy()], options=_generate_options(simplify_branches=True)),
self._task(ast, params=[var_a.copy(), var_b.copy()]),
)

def test_function_with_simplified_false_condition_in_true_branch(self):
Expand All @@ -314,7 +314,7 @@ def test_function_with_simplified_false_condition_in_true_branch(self):
ast._add_edges_from(((root, condition_node), (seq_node, code_node)))
assert self._regex_matches(
r"^%int +test_function\(%int +a%,%int +b%\)%{%int%c;%}%$".replace("%", "\\s*"),
self._task(ast, params=[var_a.copy(), var_b.copy()], options=_generate_options(simplify_branches=True)),
self._task(ast, params=[var_a.copy(), var_b.copy()]),
)

def test_function_with_simplified_false_condition_in_false_branch(self):
Expand Down Expand Up @@ -344,7 +344,7 @@ def test_function_with_simplified_false_condition_in_false_branch(self):
ast._add_edges_from(((root, condition_node), (seq_node, false_condition_code_node)))
assert self._regex_matches(
r"^%int +test_function\(%int +a%,%int +b%\)%{%int%c;%if%\(%c%<%5%\)%{%return%0%;%}%}%$".replace("%", "\\s*"),
self._task(ast, params=[var_a.copy(), var_b.copy()], options=_generate_options(simplify_branches=True)),
self._task(ast, params=[var_a.copy(), var_b.copy()]),
)

def test_function_with_ifelse(self):
Expand Down