Skip to content

Commit

Permalink
Remove loaded limits for int128 and address (#2506)
Browse files Browse the repository at this point in the history
* remove old ADDRSIZE comparison

* remove some loaded limits, reduce reserved memory

* fix int128 clamp in convert.py

* fix int128 clamp again

* update variable location in a test
  • Loading branch information
charles-cooper authored Nov 4, 2021
1 parent f692635 commit 98eff0a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 84 deletions.
2 changes: 1 addition & 1 deletion tests/compiler/test_sha3_32.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

def test_sha3_32():
lll = ["sha3_32", 0]
evm = ["PUSH1", 0, "PUSH1", 192, "MSTORE", "PUSH1", 32, "PUSH1", 192, "SHA3"]
evm = ["PUSH1", 0, "PUSH1", 128, "MSTORE", "PUSH1", 32, "PUSH1", 128, "SHA3"]
assert compile_lll.compile_to_assembly(LLLnode.from_list(lll)) == evm
assert compile_lll.compile_to_assembly(optimizer.optimize(LLLnode.from_list(lll))) == evm
2 changes: 1 addition & 1 deletion tests/parser/types/numbers/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test() -> uint256:
"""

lll = compile_code(code, ["ir"])["ir"]
assert search_for_sublist(lll, ["mstore", [320], [2 ** 12 * some_prime]])
assert search_for_sublist(lll, ["mstore", [224], [2 ** 12 * some_prime]])


def test_constant_lists(get_contract):
Expand Down
14 changes: 8 additions & 6 deletions vyper/builtin_functions/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
clamp_basetype,
get_bytearray_length,
getpos,
int_clamp,
load_op,
shr,
)
Expand Down Expand Up @@ -175,12 +176,13 @@ def to_int128(expr, args, kwargs, context):
else:
return LLLnode.from_list(in_arg, typ=BaseType("int128"), pos=getpos(expr))

else:
return LLLnode.from_list(
["uclample", in_arg, ["mload", MemoryPositions.MAX_INT128]],
typ=BaseType("int128"),
pos=getpos(expr),
)
# !! do not use clamp_basetype. check that 0 <= input <= MAX_INT128.
res = int_clamp(in_arg, 127, signed=False)
return LLLnode.from_list(
res,
typ="int128",
pos=getpos(expr),
)

elif input_type == "decimal":
# cast to int128 so clamp_basetype works
Expand Down
23 changes: 7 additions & 16 deletions vyper/builtin_functions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
ZeroDivisionException,
)
from vyper.old_codegen.abi import ABI_Tuple, abi_encode, abi_type_of, abi_type_of2
from vyper.old_codegen.arg_clamps import int128_clamp
from vyper.old_codegen.expr import Expr
from vyper.old_codegen.keccak256_helper import keccak256_helper
from vyper.old_codegen.parser_utils import (
LLLnode,
clamp_basetype,
get_bytearray_length,
get_element_ptr,
getpos,
Expand Down Expand Up @@ -939,22 +939,13 @@ def build_LLL(self, expr, args, kwargs, context):
],
typ=BaseType(ret_type),
pos=getpos(expr),
annotation="extracting 32 bytes",
)
if ret_type == "int128":
return LLLnode.from_list(
int128_clamp(o),
typ=BaseType("int128"),
pos=getpos(expr),
)
elif ret_type == "address":
return LLLnode.from_list(
["uclamplt", o, ["mload", MemoryPositions.ADDRSIZE]],
typ=BaseType(ret_type),
pos=getpos(expr),
annotation="extract32",
)
else:
return o
return LLLnode.from_list(
clamp_basetype(o),
typ=ret_type,
pos=getpos(expr),
)


class AsWeiValue:
Expand Down
41 changes: 0 additions & 41 deletions vyper/old_codegen/arg_clamps.py

This file was deleted.

10 changes: 5 additions & 5 deletions vyper/old_codegen/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,9 @@ def clamp_basetype(lll_node):
lll_node = unwrap_location(lll_node)

if t.typ in ("int128"):
return _int_clamp(lll_node, 128, signed=True)
return int_clamp(lll_node, 128, signed=True)
if t.typ == "uint8":
return _int_clamp(lll_node, 8)
return int_clamp(lll_node, 8)
if t.typ in ("decimal"):
return [
"clamp",
Expand All @@ -731,16 +731,16 @@ def clamp_basetype(lll_node):
]

if t.typ in ("address",):
return _int_clamp(lll_node, 160)
return int_clamp(lll_node, 160)
if t.typ in ("bool",):
return _int_clamp(lll_node, 1)
return int_clamp(lll_node, 1)
if t.typ in ("int256", "uint256", "bytes32"):
return lll_node # special case, no clamp.

return # raises


def _int_clamp(lll_node, bits, signed=False):
def int_clamp(lll_node, bits, signed=False):
"""Generalized clamper for integer types. Takes the number of bits,
whether it's signed, and returns an LLL node which checks it is
in bounds. (Consumers should use clamp_basetype instead which uses
Expand Down
21 changes: 7 additions & 14 deletions vyper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,14 @@ def calc_mem_gas(memsize):
DECIMAL_DIVISOR = 10 ** MAX_DECIMAL_PLACES


# Number of bytes in memory used for system purposes, not for variables
# memory used for system purposes, not for variables
class MemoryPositions:
ADDRSIZE = 32
MAX_INT128 = 64
MIN_INT128 = 96
MAXDECIMAL = 128
MINDECIMAL = 160
FREE_VAR_SPACE = 192
FREE_VAR_SPACE2 = 224
BLANK_SPACE = 256
FREE_LOOP_INDEX = 288
RESERVED_MEMORY = 320
MAXDECIMAL = 32
MINDECIMAL = 64
FREE_VAR_SPACE = 128
FREE_VAR_SPACE2 = 160
FREE_LOOP_INDEX = 192
RESERVED_MEMORY = 224


# Sizes of different data types. Used to clamp types.
Expand Down Expand Up @@ -152,9 +148,6 @@ def in_bounds(cls, type_str, value):
# Map representing all limits loaded into a contract as part of the initializer
# code.
LOADED_LIMITS: Dict[int, int] = {
MemoryPositions.ADDRSIZE: SizeLimits.ADDRSIZE,
MemoryPositions.MAX_INT128: SizeLimits.MAX_INT128,
MemoryPositions.MIN_INT128: SizeLimits.MIN_INT128,
MemoryPositions.MAXDECIMAL: SizeLimits.MAXDECIMAL,
MemoryPositions.MINDECIMAL: SizeLimits.MINDECIMAL,
}
Expand Down

0 comments on commit 98eff0a

Please sign in to comment.