def _handle_UnaryOp(self, expr_idx: int, expr: UnaryOp, stmt_idx: int, stmt: Statement, block: Optional[Block]): new_operand = self._handle_expr(0, expr.operand, stmt_idx, stmt, block) if new_operand is not None and new_operand is not expr.operand: new_expr = expr.copy() new_expr.operand = new_operand return new_expr return None
def _handle_UnaryOp(self, expr_idx: int, expr: BinaryOp, stmt_idx: int, stmt: Statement, block: Block): new_operand = self._handle_expr(0, expr.operands[0], stmt_idx, stmt, block) if new_operand is not None: return UnaryOp(expr.idx, expr.op, new_operand, expr.signed, variable=expr.variable, variable_offset=expr.variable_offset, **expr.tags ) return None
def _peephole_optimize_Expr(self, expr: Expression): # Convert(N->1, (Convert(1->N, t_x) ^ 0x1<N>) ==> Not(t_x) if isinstance(expr, Convert) and \ isinstance(expr.operand, BinaryOp) and \ expr.operand.op == "Xor" and \ isinstance(expr.operand.operands[0], Convert) and \ isinstance(expr.operand.operands[1], Const) and \ expr.operand.operands[1].value == 1: new_expr = UnaryOp(None, "Not", expr.operand.operands[0].operand) return self._peephole_optimize_Expr(new_expr) return expr
def optimize(self, expr: BinaryOp): # Sub(1, Conv(1->N, some bool expression)) ==> Conv(1->N, Not(some bool expression)) if expr.op == "Sub" and isinstance(expr.operands[0], Const) and expr.operands[0].value == 1 \ and isinstance(expr.operands[1], Convert) and expr.operands[1].from_bits == 1: conv_expr = expr.operands[1] if self.is_bool_expr(conv_expr.operand): new_expr = Convert(None, 1, conv_expr.to_bits, conv_expr.is_signed, UnaryOp(None, 'Not', conv_expr.operand, **conv_expr.operand.tags), **conv_expr.tags, ) return new_expr return None
def optimize(self, expr: Convert): # Convert(N->1, (Convert(1->N, t_x) ^ 0x1<N>) ==> Not(t_x) if expr.to_bits == 1: xor_expr = expr.operand if isinstance(xor_expr, BinaryOp) and xor_expr.op == "Xor": if isinstance(xor_expr.operands[0], Convert) and \ isinstance(xor_expr.operands[1], Const) and \ xor_expr.operands[1].value == 1 and \ xor_expr.operands[0].from_bits == 1: new_expr = UnaryOp(None, "Not", expr.operand.operands[0].operand) return new_expr return expr
def optimize(self, expr: BinaryOp): # Conv(1->N, some_bool_expr) ^ 1 ==> Conv(1->N, Not(some_bool_expr)) if expr.op == "Xor" and isinstance( expr.operands[1], Const) and expr.operands[1].value == 1: arg0 = expr.operands[0] if isinstance(arg0, Convert) and arg0.from_bits == 1 \ and self.is_bool_expr(arg0.operand): new_expr = Convert( None, 1, arg0.to_bits, arg0.is_signed, UnaryOp(None, 'Not', arg0.operands[0], **expr.tags), **arg0.tags) return new_expr return None