From b62a4232692262917d1857a2f246f54f8100a372 Mon Sep 17 00:00:00 2001 From: rihi <19492038+rihi@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:42:10 +0200 Subject: [PATCH] Use 'neg' operation in SubToAdd --- .../expression_simplification/rules/sub_to_add.py | 8 ++++---- .../expression_simplification/rules/test_sub_to_add.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/decompiler/pipeline/controlflowanalysis/expression_simplification/rules/sub_to_add.py b/decompiler/pipeline/controlflowanalysis/expression_simplification/rules/sub_to_add.py index 7ba105fba..9943d365d 100644 --- a/decompiler/pipeline/controlflowanalysis/expression_simplification/rules/sub_to_add.py +++ b/decompiler/pipeline/controlflowanalysis/expression_simplification/rules/sub_to_add.py @@ -1,12 +1,12 @@ from decompiler.pipeline.controlflowanalysis.expression_simplification.rules.rule import SimplificationRule -from decompiler.structures.pseudo import BinaryOperation, Constant, Expression, Operation, OperationType +from decompiler.structures.pseudo import BinaryOperation, Expression, Operation, OperationType, UnaryOperation class SubToAdd(SimplificationRule): """ Replace subtractions with additions. - `e0 - e1 -> e0 + (e1 * -1)` + `e0 - e1 -> e0 + (-e1)` """ def apply(self, operation: Operation) -> list[tuple[Expression, Expression]]: @@ -15,13 +15,13 @@ def apply(self, operation: Operation) -> list[tuple[Expression, Expression]]: if not isinstance(operation, BinaryOperation): raise TypeError(f"Expected BinaryOperation, got {type(operation)}") - mul_op = BinaryOperation(OperationType.multiply, [operation.right, Constant(-1, operation.type)]) + neg_op = UnaryOperation(OperationType.negate, [operation.right]) return [( operation, BinaryOperation( OperationType.plus, - [operation.left, mul_op], + [operation.left, neg_op], operation.type ) )] diff --git a/tests/pipeline/controlflowanalysis/expression_simplification/rules/test_sub_to_add.py b/tests/pipeline/controlflowanalysis/expression_simplification/rules/test_sub_to_add.py index 565b9067e..d171aeb93 100644 --- a/tests/pipeline/controlflowanalysis/expression_simplification/rules/test_sub_to_add.py +++ b/tests/pipeline/controlflowanalysis/expression_simplification/rules/test_sub_to_add.py @@ -1,6 +1,6 @@ import pytest from decompiler.pipeline.controlflowanalysis.expression_simplification.rules.sub_to_add import SubToAdd -from decompiler.structures.pseudo import BinaryOperation, Constant, Expression, Integer, Operation, OperationType, Variable +from decompiler.structures.pseudo import BinaryOperation, Constant, Expression, Integer, Operation, OperationType, UnaryOperation, Variable var_x = Variable("x", Integer.int32_t()) var_y = Variable("y", Integer.int32_t()) @@ -12,7 +12,7 @@ [ ( BinaryOperation(OperationType.minus, [var_x, var_y]), - [BinaryOperation(OperationType.plus, [var_x, BinaryOperation(OperationType.multiply, [var_y, con_neg1])])], + [BinaryOperation(OperationType.plus, [var_x, UnaryOperation(OperationType.negate, [var_y])])], ), ], )