Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
RType,
RVec,
is_bool_or_bit_rprimitive,
is_int8_rprimitive,
is_int16_rprimitive,
is_int32_rprimitive,
is_int64_rprimitive,
is_int_rprimitive,
Expand Down Expand Up @@ -937,6 +939,15 @@ def visit_int_op(self, op: IntOp) -> None:
dest = self.reg(op)
lhs = self.reg(op.lhs)
rhs = self.reg(op.rhs)
if (
op.op in (IntOp.LEFT_SHIFT, IntOp.RIGHT_SHIFT)
and isinstance(op.lhs, Integer)
and isinstance(op.type, RPrimitive)
and op.type.is_native_int
):
# The type of a C shift is the (promoted) type of the left operand,
# so cast an integer literal to get e.g. a 64-bit shift.
lhs = f"({self.ctype(op.type)}){lhs}"
if op.op == IntOp.RIGHT_SHIFT:
# Signed right shift
lhs = self.emit_signed_int_cast(op.lhs.type) + lhs
Expand Down Expand Up @@ -1191,6 +1202,10 @@ def emit_unsigned_int_cast(self, type: RType) -> str:
return "(uint32_t)"
elif is_int64_rprimitive(type):
return "(uint64_t)"
elif is_int16_rprimitive(type):
return "(uint16_t)"
elif is_int8_rprimitive(type):
return "(uint8_t)"
else:
return ""

Expand Down
52 changes: 52 additions & 0 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2864,8 +2864,60 @@ def fixed_width_int_op(
else:
assert False, type
return self.call_c(prim, [lhs, rhs], line)
if op in (IntOp.LEFT_SHIFT, IntOp.RIGHT_SHIFT):
return self.fixed_width_int_shift(type, lhs, rhs, op, line)
return self.int_op(type, lhs, rhs, op, line)

def fixed_width_int_shift(
self, type: RPrimitive, lhs: Value, rhs: Value, op: int, line: int
) -> Value:
"""Generate a native int shift (<< or >>) using Python semantics.

A C shift by a negative count or by at least the bit width is undefined
behavior, so only use a plain shift when the count is in range. A negative
count raises ValueError. A larger count produces 0 (or -1 for a right shift
of a negative signed value).
"""
bits = type.size * 8
if isinstance(rhs, Integer):
count = rhs.numeric_value()
if 0 <= count < bits:
return self.int_op(type, lhs, rhs, op, line)
if count < 0:
self.check_for_negative_shift_count(rhs, type, line)
return self.large_shift_result(type, lhs, op, line)
res = Register(type)
in_range, out_of_range, done = BasicBlock(), BasicBlock(), BasicBlock()
# Unsigned comparison also catches negative counts
ok = self.add(ComparisonOp(rhs, Integer(bits, type), ComparisonOp.ULT, line))
self.add(Branch(ok, in_range, out_of_range, Branch.BOOL))
self.activate_block(in_range)
self.add(Assign(res, self.int_op(type, lhs, rhs, op, line)))
self.add(Goto(done))
self.activate_block(out_of_range)
if type.is_signed:
self.check_for_negative_shift_count(rhs, type, line)
self.add(Assign(res, self.large_shift_result(type, lhs, op, line)))
self.add(Goto(done))
self.activate_block(done)
return res

def large_shift_result(self, type: RPrimitive, lhs: Value, op: int, line: int) -> Value:
"""Return the result of lhs << n or lhs >> n, when n >= bit width."""
if op == IntOp.RIGHT_SHIFT and type.is_signed:
# Either 0 or -1, depending on the sign of lhs
return self.int_op(type, lhs, Integer(type.size * 8 - 1, type), op, line)
return Integer(0, type)

def check_for_negative_shift_count(self, rhs: Value, type: RType, line: int) -> None:
err, ok = BasicBlock(), BasicBlock()
is_negative = self.add(ComparisonOp(rhs, Integer(0, type), ComparisonOp.SLT, line))
self.add(Branch(is_negative, err, ok, Branch.BOOL))
self.activate_block(err)
self.add(RaiseStandardError(RaiseStandardError.VALUE_ERROR, "negative shift count", line))
self.add(Unreachable())
self.activate_block(ok)

def check_for_zero_division(self, rhs: Value, type: RType, line: int) -> None:
err, ok = BasicBlock(), BasicBlock()
is_zero = self.binary_op(rhs, Integer(0, type), "==", line)
Expand Down
128 changes: 118 additions & 10 deletions mypyc/test-data/irbuild-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,16 @@ L0:
a = r1
return a
def others(a, b):
a, b, r0, r1, r2, r3, r4, r5, r6 :: i64
a, b, r0, r1, r2, r3, r4 :: i64
r5 :: bit
r6, r7 :: i64
r8 :: bit
r9 :: bool
r10 :: bit
r11, r12 :: i64
r13 :: bit
r14 :: bool
r15 :: i64
L0:
r0 = a - b
a = r0
Expand All @@ -278,12 +287,73 @@ L0:
a = r3
r4 = a ^ b
a = r4
r5 = a << b
a = r5
r6 = a >> b
a = r6
r5 = b < 64 :: unsigned
if r5 goto L1 else goto L2 :: bool
L1:
r6 = a << b
r7 = r6
goto L5
L2:
r8 = b < 0 :: signed
if r8 goto L3 else goto L4 :: bool
L3:
r9 = raise ValueError('negative shift count')
unreachable
L4:
r7 = 0
L5:
a = r7
r10 = b < 64 :: unsigned
if r10 goto L6 else goto L7 :: bool
L6:
r11 = a >> b
r12 = r11
goto L10
L7:
r13 = b < 0 :: signed
if r13 goto L8 else goto L9 :: bool
L8:
r14 = raise ValueError('negative shift count')
unreachable
L9:
r15 = a >> 63
r12 = r15
L10:
a = r12
return a

[case testI64ShiftByConstant]
from mypy_extensions import i64

def shift(x: i64) -> i64:
a = x << 63
b = x << 64
c = x >> 63
d = x >> 64
return x >> -1
[out]
def shift(x):
x, r0, a, b, r1, c, r2, d :: i64
r3 :: bit
r4 :: bool
r5 :: i64
L0:
r0 = x << 63
a = r0
b = 0
r1 = x >> 63
c = r1
r2 = x >> 63
d = r2
r3 = -1 < 0 :: signed
if r3 goto L1 else goto L2 :: bool
L1:
r4 = raise ValueError('negative shift count')
unreachable
L2:
r5 = x >> 63
return r5

[case testI64BitwiseOps]
from mypy_extensions import i64

Expand Down Expand Up @@ -321,18 +391,56 @@ L0:
b = r4
return b
def reverse(a, b):
a, b, r0, r1, r2, r3, r4 :: i64
a, b, r0, r1, r2 :: i64
r3 :: bit
r4, r5 :: i64
r6 :: bit
r7 :: bool
r8 :: bit
r9, r10 :: i64
r11 :: bit
r12 :: bool
r13 :: i64
L0:
r0 = 1 & a
b = r0
r1 = 2 | b
a = r1
r2 = 3 ^ a
b = r2
r3 = 4 << b
a = r3
r4 = 5 >> a
b = r4
r3 = b < 64 :: unsigned
if r3 goto L1 else goto L2 :: bool
L1:
r4 = 4 << b
r5 = r4
goto L5
L2:
r6 = b < 0 :: signed
if r6 goto L3 else goto L4 :: bool
L3:
r7 = raise ValueError('negative shift count')
unreachable
L4:
r5 = 0
L5:
a = r5
r8 = a < 64 :: unsigned
if r8 goto L6 else goto L7 :: bool
L6:
r9 = 5 >> a
r10 = r9
goto L10
L7:
r11 = a < 0 :: signed
if r11 goto L8 else goto L9 :: bool
L8:
r12 = raise ValueError('negative shift count')
unreachable
L9:
r13 = 5 >> 63
r10 = r13
L10:
b = r10
return b
def unary(a):
a, r0 :: i64
Expand Down
54 changes: 54 additions & 0 deletions mypyc/test-data/irbuild-i8.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,60 @@ L0:
r2 = r0 + r1
return r2

[case testI8ShiftByVariable]
from mypy_extensions import i8

def shift(x: i8, y: i8) -> i8:
a = x << y
return a >> y
[out]
def shift(x, y):
x, y :: i8
r0 :: bit
r1, r2 :: i8
r3 :: bit
r4 :: bool
a :: i8
r5 :: bit
r6, r7 :: i8
r8 :: bit
r9 :: bool
r10 :: i8
L0:
r0 = y < 8 :: unsigned
if r0 goto L1 else goto L2 :: bool
L1:
r1 = x << y
r2 = r1
goto L5
L2:
r3 = y < 0 :: signed
if r3 goto L3 else goto L4 :: bool
L3:
r4 = raise ValueError('negative shift count')
unreachable
L4:
r2 = 0
L5:
a = r2
r5 = y < 8 :: unsigned
if r5 goto L6 else goto L7 :: bool
L6:
r6 = a >> y
r7 = r6
goto L10
L7:
r8 = y < 0 :: signed
if r8 goto L8 else goto L9 :: bool
L8:
r9 = raise ValueError('negative shift count')
unreachable
L9:
r10 = a >> 7
r7 = r10
L10:
return r7

[case testI8BoxAndUnbox]
from typing import Any
from mypy_extensions import i8
Expand Down
19 changes: 19 additions & 0 deletions mypyc/test-data/irbuild-u64.test
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ L0:
f = r5
return 1

[case testU64ShiftByConstant]
from mypy_extensions import u64

def shift(x: u64) -> u64:
a = x << 63
b = x << 64
c = x >> 63
return c >> 100
[out]
def shift(x):
x, r0, a, b, r1, c :: u64
L0:
r0 = x << 63
a = r0
b = 0
r1 = x >> 63
c = r1
return 0

[case testU64DetectMoreOutOfRangeLiterals]
from mypy_extensions import u64

Expand Down
Loading
Loading