Skip to content

Commit b9c7f1e

Browse files
committed
[mypyc] Refactor IR build for unary "~"
1 parent 1942ccd commit b9c7f1e

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

mypyc/irbuild/ll_builder.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,32 +1690,28 @@ def unary_plus(self, value: Value, line: int) -> Value:
16901690
return value
16911691
return self._non_specialized_unary_op(value, "+", line)
16921692

1693+
def unary_invert(self, value: Value, line: int) -> Value:
1694+
typ = value.type
1695+
if is_fixed_width_rtype(typ):
1696+
if typ.is_signed:
1697+
# Translate to 'x ^ -1'
1698+
return self.int_op(typ, value, Integer(-1, typ), IntOp.XOR, line)
1699+
else:
1700+
# Translate to 'x ^ 0xff...'
1701+
mask = (1 << (typ.size * 8)) - 1
1702+
return self.int_op(typ, value, Integer(mask, typ), IntOp.XOR, line)
1703+
return self._non_specialized_unary_op(value, "~", line)
1704+
16931705
def unary_op(self, value: Value, expr_op: str, line: int) -> Value:
16941706
if expr_op == "not":
16951707
return self.unary_not(value, line)
16961708
elif expr_op == "-":
16971709
return self.unary_minus(value, line)
16981710
elif expr_op == "+":
16991711
return self.unary_plus(value, line)
1700-
typ = value.type
1701-
if is_fixed_width_rtype(typ):
1702-
if expr_op == "~":
1703-
if typ.is_signed:
1704-
# Translate to 'x ^ -1'
1705-
return self.int_op(typ, value, Integer(-1, typ), IntOp.XOR, line)
1706-
else:
1707-
# Translate to 'x ^ 0xff...'
1708-
mask = (1 << (typ.size * 8)) - 1
1709-
return self.int_op(typ, value, Integer(mask, typ), IntOp.XOR, line)
1710-
1711-
if isinstance(typ, RInstance):
1712-
result = self.dunder_op(value, None, expr_op, line)
1713-
if result is not None:
1714-
return result
1715-
primitive_ops_candidates = unary_ops.get(expr_op, [])
1716-
target = self.matching_primitive_op(primitive_ops_candidates, [value], line)
1717-
assert target, "Unsupported unary operation: %s" % expr_op
1718-
return target
1712+
elif expr_op == "~":
1713+
return self.unary_invert(value, line)
1714+
raise RuntimeError("Unsupported unary operation: %s" % expr_op)
17191715

17201716
def make_dict(self, key_value_pairs: Sequence[DictEntry], line: int) -> Value:
17211717
result: Value | None = None

0 commit comments

Comments
 (0)