Skip to content

Commit

Permalink
Use 'neg' operation in SubToAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
rihi committed Sep 20, 2023
1 parent 97ed807 commit b62a423
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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]]:
Expand All @@ -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
)
)]
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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])])],
),
],
)
Expand Down

0 comments on commit b62a423

Please sign in to comment.