Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shift operators #3019

Merged
merged 19 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
23 changes: 2 additions & 21 deletions tests/functions/folding/test_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@pytest.mark.fuzzing
@settings(max_examples=50, deadline=1000)
@given(a=st_uint256, b=st_uint256)
@pytest.mark.parametrize("op", ["&", "|", "^"])
def test_bitwise_and_or(get_contract, a, b, op):
@pytest.mark.parametrize("op", ["&", "|", "^", "<<", ">>"])
def test_bitwise_ops(get_contract, a, b, op):

source = f"""
@external
Expand Down Expand Up @@ -45,22 +45,3 @@ def foo(a: uint256) -> uint256:
new_node = vy_fn.BitwiseNot().evaluate(old_node)

assert contract.foo(value) == new_node.value


@pytest.mark.fuzzing
@settings(max_examples=50, deadline=1000)
@given(value=st_uint256, steps=st.integers(min_value=-256, max_value=256))
def test_shift(get_contract, value, steps):

source = """
@external
def foo(a: uint256, b: int128) -> uint256:
return shift(a, b)
"""
contract = get_contract(source)

vyper_ast = vy_ast.parse_to_ast(f"shift({value}, {steps})")
old_node = vyper_ast.body[0].value
new_node = vy_fn.Shift().evaluate(old_node)

assert contract.foo(value, steps) == new_node.value
45 changes: 19 additions & 26 deletions tests/parser/functions/test_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from vyper.compiler import compile_code
from vyper.evm.opcodes import EVM_VERSIONS
from vyper.exceptions import TypeMismatch
from vyper.utils import unsigned_to_signed

code = """
@external
Expand All @@ -22,12 +23,12 @@ def _bitwise_not(x: uint256) -> uint256:
return ~x

@external
def _shift(x: uint256, y: int128) -> uint256:
return shift(x, y)
def _shl(x: uint256, y: uint256) -> uint256:
return x << y

@external
def _negatedShift(x: uint256, y: int128) -> uint256:
return shift(x, -y)
def _shr(x: uint256, y: uint256) -> uint256:
return x >> y
"""


Expand All @@ -51,22 +52,11 @@ def test_test_bitwise(get_contract_with_gas_estimation, evm_version):
assert c._bitwise_or(x, y) == (x | y)
assert c._bitwise_xor(x, y) == (x ^ y)
assert c._bitwise_not(x) == 2 ** 256 - 1 - x
assert c._shift(x, 3) == x * 8
assert c._shift(x, 255) == 0
assert c._shift(y, 255) == 2 ** 255
assert c._shift(x, 256) == 0
assert c._shift(x, 0) == x
assert c._shift(x, -1) == x // 2
assert c._shift(x, -3) == x // 8
assert c._shift(x, -256) == 0
assert c._negatedShift(x, -3) == x * 8
assert c._negatedShift(x, -255) == 0
assert c._negatedShift(y, -255) == 2 ** 255
assert c._negatedShift(x, -256) == 0
assert c._negatedShift(x, -0) == x
assert c._negatedShift(x, 1) == x // 2
assert c._negatedShift(x, 3) == x // 8
assert c._negatedShift(x, 256) == 0

for t in (x, y):
for s in (0, 1, 3, 255, 256):
assert c._shr(t, s) == t >> s
assert c._shl(t, s) == (t << s) % (2 ** 256)


POST_BYZANTIUM = [k for (k, v) in EVM_VERSIONS.items() if v > 0]
Expand All @@ -76,19 +66,22 @@ def test_test_bitwise(get_contract_with_gas_estimation, evm_version):
def test_signed_shift(get_contract_with_gas_estimation, evm_version):
code = """
@external
def _signedShift(x: int256, y: int128) -> int256:
return shift(x, y)
def _sar(x: int256, y: int256) -> int256:
return x >> y

@external
def _shl(x: int256, y: int256) -> int256:
return x << y
"""
c = get_contract_with_gas_estimation(code, evm_version=evm_version)
x = 126416208461208640982146408124
y = 7128468721412412459
cases = [x, y, -x, -y]

for t in cases:
assert c._signedShift(t, 0) == t >> 0
assert c._signedShift(t, -1) == t >> 1
assert c._signedShift(t, -3) == t >> 3
assert c._signedShift(t, -256) == t >> 256
for s in (0, 1, 3, 255, 256):
assert c._sar(t, s) == t >> s
assert c._shl(t, s) == unsigned_to_signed((t << s) % (2 ** 256), 256)


def test_precedence(get_contract):
Expand Down
14 changes: 14 additions & 0 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,20 @@ class BitXor(VyperNode):
_op = operator.xor


class LShift(VyperNode):
__slots__ = ()
_description = "bitwise left shift"
_pretty = "<<"
_op = operator.lshift


class RShift(VyperNode):
__slots__ = ()
_description = "bitwise right shift"
_pretty = ">>"
_op = operator.rshift


class BoolOp(VyperNode):
__slots__ = ("op", "values")

Expand Down
2 changes: 2 additions & 0 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class Mult(VyperNode): ...
class Div(VyperNode): ...
class Mod(VyperNode): ...
class Pow(VyperNode): ...
class LShift(VyperNode): ...
class RShift(VyperNode): ...
class BoolOp(VyperNode): ...
class And(VyperNode): ...
class Or(VyperNode): ...
Expand Down
5 changes: 5 additions & 0 deletions vyper/builtin_functions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,8 +1441,13 @@ class Shift(BuiltinFunction):
("x", (Uint256Definition(), Int256Definition())),
("shift_bits", SignedIntegerAbstractType()),
]
_warned = False

def evaluate(self, node):
if not self.__class__._warned:
vyper_warn("`shift()` is deprecated! Please use the << or >> operator instead.")
self.__class__._warned = True

validate_call_args(node, 2)
if [i for i in node.args if not isinstance(i, vy_ast.Num)]:
raise UnfoldableNode
Expand Down
18 changes: 18 additions & 0 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
getpos,
make_setter,
pop_dyn_array,
sar,
shl,
shr,
unwrap_location,
)
from vyper.codegen.ir_node import IRnode
Expand Down Expand Up @@ -368,6 +371,21 @@ def parse_BinOp(self):
new_typ = left.typ
return IRnode.from_list(["xor", left, right], typ=new_typ)

if isinstance(self.expr.op, vy_ast.LShift):
new_typ = left.typ
if new_typ._int_info.bits != 256:
# TODO implement me. ["and", 2**bits - 1, shl(right, left)]
return
return IRnode.from_list(shl(right, left), typ=new_typ)
if isinstance(self.expr.op, vy_ast.RShift):
new_typ = left.typ
if new_typ._int_info.bits != 256:
# TODO implement me. promote_signed_int(op(right, left), bits)
return
op = shr if not left.typ._int_info.is_signed else sar
# note: sar NotImplementedError for pre-constantinople
return IRnode.from_list(op(right, left), typ=new_typ)

out_typ = BaseType(ltyp)

with left.cache_when_complex("x") as (b1, x), right.cache_when_complex("y") as (b2, y):
Expand Down
5 changes: 5 additions & 0 deletions vyper/semantics/types/value/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def validate_numeric_op(
if self._invalid_op and isinstance(node.op, self._invalid_op):
raise InvalidOperation(f"Cannot perform {node.op.description} on {self}", node)

if isinstance(node.op, (vy_ast.LShift, vy_ast.RShift)) and self._bits != 256:
raise InvalidOperation(
f"Cannot perform {node.op.description} on non-int256/uint256 type!", node
)

if isinstance(node.op, vy_ast.Pow):
if isinstance(node, vy_ast.BinOp):
left, right = node.left, node.right
Expand Down