From c514753ee24ad11b67853f81a0cd108a08ab2741 Mon Sep 17 00:00:00 2001 From: Adam Chidlow Date: Fri, 22 Mar 2024 12:28:11 +0800 Subject: [PATCH] feat: empty constructor for arc4 numeric types, defaults to zero --- examples/sizes.txt | 2 +- src/puya/awst_build/eb/arc4/numeric.py | 33 +++++--- src/puyapy-stubs/arc4.pyi | 6 +- test_cases/arc4_types/numeric.py | 8 ++ .../out/Arc4NumericTypesContract.approval.mir | 4 +- .../Arc4NumericTypesContract.approval.teal | 2 +- .../out/Arc4NumericTypesContract.clear.mir | 4 +- .../out/Arc4NumericTypesContract.clear.teal | 2 +- .../Arc4NumericTypesContract.destructured.ir | 4 +- .../out/Arc4NumericTypesContract.ssa.ir | 14 +++- ...Arc4NumericTypesContract.ssa.opt_pass_1.ir | 14 +++- ...Arc4NumericTypesContract.ssa.opt_pass_2.ir | 4 +- ...Arc4NumericTypesContract.ssa.opt_pass_3.ir | 4 +- ...Arc4NumericTypesContract.ssa.opt_pass_4.ir | 4 +- ...Arc4NumericTypesContract.ssa.opt_pass_5.ir | 4 +- ...Arc4NumericTypesContract.ssa.opt_pass_6.ir | 4 +- ...Arc4NumericTypesContract.ssa.opt_pass_7.ir | 4 +- test_cases/arc4_types/out/numeric.awst | 5 ++ .../Arc4NumericTypesContract.destructured.ir | 4 +- .../Arc4NumericTypesContract.approval.teal | 78 +++++++++---------- .../Arc4NumericTypesContract.clear.teal | 32 +++++++- .../Arc4NumericTypesContract.destructured.ir | 14 +++- test_cases/arc4_types/puya.log | 18 ++++- 23 files changed, 182 insertions(+), 86 deletions(-) diff --git a/examples/sizes.txt b/examples/sizes.txt index 38dc99d729..1b9596e627 100644 --- a/examples/sizes.txt +++ b/examples/sizes.txt @@ -11,7 +11,7 @@ arc4_types/Arc4DynamicStringArray 230 112 118 112 0 arc4_types/Arc4MutableParams 362 222 140 220 2 arc4_types/Arc4Mutation 2803 1448 1355 1447 1 - arc4_types/Arc4NumericTypes 472 8 464 8 0 + arc4_types/Arc4NumericTypes 571 8 563 8 0 arc4_types/Arc4RefTypes 47 39 8 39 0 arc4_types/Arc4StringTypes 304 8 296 8 0 arc4_types/Arc4StructsFromAnotherModule 67 12 55 12 0 diff --git a/src/puya/awst_build/eb/arc4/numeric.py b/src/puya/awst_build/eb/arc4/numeric.py index 469ae85c91..1cb7acd848 100644 --- a/src/puya/awst_build/eb/arc4/numeric.py +++ b/src/puya/awst_build/eb/arc4/numeric.py @@ -8,6 +8,7 @@ from puya.awst import wtypes from puya.awst.nodes import ( ARC4Encode, + ConstantValue, Expression, Literal, NumericComparison, @@ -36,12 +37,12 @@ logger: structlog.types.FilteringBoundLogger = structlog.get_logger(__name__) -class NumericARC4ClassExpressionBuilder(ARC4ClassExpressionBuilder): +class NumericARC4ClassExpressionBuilder(ARC4ClassExpressionBuilder, abc.ABC): def __init__(self, location: SourceLocation): super().__init__(location) self.wtype: wtypes.ARC4UIntN | wtypes.ARC4UFixedNxM | None = None - def produces(self) -> wtypes.WType: + def produces(self) -> wtypes.ARC4Type: if self.wtype is None: raise InternalError( "Cannot resolve wtype of generic EB until the index method is called with the " @@ -56,14 +57,13 @@ def call( arg_names: list[str | None], location: SourceLocation, ) -> ExpressionBuilder: - if not self.wtype: - raise InternalError( - "Cannot resolve wtype of generic EB until the index method is called with" - " the generic type parameter." - ) + wtype = self.produces() match args: + case []: + zero_literal = Literal(value=self.zero_literal(), source_location=location) + return var_expression(convert_arc4_literal(zero_literal, wtype, location)) case [Literal() as lit]: - return var_expression(convert_arc4_literal(lit, self.wtype, location)) + return var_expression(convert_arc4_literal(lit, wtype, location)) case [ExpressionBuilder(value_type=wtypes.WType() as value_type) as eb]: value = eb.rvalue() if value_type not in ( @@ -72,11 +72,11 @@ def call( wtypes.biguint_wtype, ): raise CodeError( - f"{self.wtype} constructor expects an int literal or a " + f"{wtype} constructor expects an int literal or a " "uint64 expression or a biguint expression" ) return var_expression( - ARC4Encode(value=value, source_location=location, wtype=self.wtype) + ARC4Encode(value=value, source_location=location, wtype=wtype) ) case _: raise CodeError( @@ -84,12 +84,19 @@ def call( location, ) + @abc.abstractmethod + def zero_literal(self) -> ConstantValue: + ... + class ByteClassExpressionBuilder(NumericARC4ClassExpressionBuilder): def __init__(self, location: SourceLocation): super().__init__(location) self.wtype = wtypes.arc4_byte_type + def zero_literal(self) -> ConstantValue: + return 0 + class _UIntNClassExpressionBuilder(NumericARC4ClassExpressionBuilder, abc.ABC): def index( @@ -104,6 +111,9 @@ def index( def check_bitsize(self, n: int, location: SourceLocation) -> None: ... + def zero_literal(self) -> ConstantValue: + return 0 + class UIntNClassExpressionBuilder(_UIntNClassExpressionBuilder): def check_bitsize(self, n: int, location: SourceLocation) -> None: @@ -144,6 +154,9 @@ def index_multiple( def check_bitsize(self, n: int, location: SourceLocation) -> None: ... + def zero_literal(self) -> ConstantValue: + return "0.0" + class UFixedNxMClassExpressionBuilder(_UFixedNxMClassExpressionBuilder): def check_bitsize(self, n: int, location: SourceLocation) -> None: diff --git a/src/puyapy-stubs/arc4.pyi b/src/puyapy-stubs/arc4.pyi index 1529bf1c78..44d014beb5 100644 --- a/src/puyapy-stubs/arc4.pyi +++ b/src/puyapy-stubs/arc4.pyi @@ -86,7 +86,7 @@ class String(_ABIEncoded): _TBitSize = typing.TypeVar("_TBitSize", bound=int) class _UIntN(_ABIEncoded, typing.Protocol): - def __init__(self, value: puyapy.BigUInt | puyapy.UInt64 | int, /) -> None: ... + def __init__(self, value: puyapy.BigUInt | puyapy.UInt64 | int = 0, /) -> None: ... # ~~~ https://docs.python.org/3/reference/datamodel.html#basic-customization ~~~ # TODO: mypy suggests due to Liskov below should be other: object @@ -143,7 +143,7 @@ class UFixedNxM(_ABIEncoded, typing.Generic[_TBitSize, _TDecimalPlaces]): Max size: 64 bits""" - def __init__(self, value: str, /): + def __init__(self, value: str = "0.0", /): """ Construct an instance of UFixedNxM where value (v) is determined from the original decimal value (d) by the formula v = round(d * (10^M)) @@ -156,7 +156,7 @@ class BigUFixedNxM(_ABIEncoded, typing.Generic[_TBitSize, _TDecimalPlaces]): Max size: 512 bits""" - def __init__(self, value: str, /): + def __init__(self, value: str = "0.0", /): """ Construct an instance of UFixedNxM where value (v) is determined from the original decimal value (d) by the formula v = round(d * (10^M)) diff --git a/test_cases/arc4_types/numeric.py b/test_cases/arc4_types/numeric.py index 9b1e4549b1..3642cbb248 100644 --- a/test_cases/arc4_types/numeric.py +++ b/test_cases/arc4_types/numeric.py @@ -5,11 +5,13 @@ from puyapy.arc4 import ( BigUFixedNxM, BigUIntN, + Byte, UFixedNxM, UInt8, UInt16, UInt32, UInt64 as ARC4UInt64, + UInt512, UIntN, ) @@ -96,4 +98,10 @@ def approval_program(self) -> bool: return True def clear_state_program(self) -> bool: + assert BigUInt.from_bytes(Decimal().bytes) == 0 + assert BigUInt.from_bytes(BigUFixedNxM[t.Literal[512], t.Literal[5]]().bytes) == 0 + assert Byte() == 0 + assert ARC4UInt64() == 0 + assert UInt512() == 0 + return True diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.mir b/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.mir index 0a3181aae6..18a7945616 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.mir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.mir @@ -4,6 +4,6 @@ // test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: main_block@0: - int 1 // 1 True arc4_types/numeric.py:96 - return // return True arc4_types/numeric.py:96 + int 1 // 1 True arc4_types/numeric.py:98 + return // return True arc4_types/numeric.py:98 diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.teal b/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.teal index e763e695fd..5c8c2324f2 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.teal +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.approval.teal @@ -1,7 +1,7 @@ #pragma version 10 test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: - // arc4_types/numeric.py:96 + // arc4_types/numeric.py:98 // return True int 1 return diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.mir b/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.mir index a89996254f..460336a75e 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.mir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.mir @@ -4,6 +4,6 @@ // test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: main_block@0: - int 1 // 1 True arc4_types/numeric.py:99 - return // return True arc4_types/numeric.py:99 + int 1 // 1 True arc4_types/numeric.py:107 + return // return True arc4_types/numeric.py:107 diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.teal b/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.teal index d2bc2fe32c..2139117f92 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.teal +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.clear.teal @@ -1,7 +1,7 @@ #pragma version 10 test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program: - // arc4_types/numeric.py:99 + // arc4_types/numeric.py:107 // return True int 1 return diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.destructured.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.destructured.ir index b674536b7e..0eb163ee35 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.destructured.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.destructured.ir @@ -1,10 +1,10 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 return 1u program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.ir index 75417fff1f..e5f743ff65 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let uint8#0: uint64 = 255u let val_as_bytes%0#0: bytes = (itob uint8#0) let int8_encoded#0: bytes = ((extract 7 1) val_as_bytes%0#0) @@ -117,5 +117,15 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 + let tmp%0#0: uint64 = (b== 0x0000000000000000 0b) + (assert tmp%0#0) + let tmp%1#0: uint64 = (b== 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0b) + (assert tmp%1#0) + let tmp%2#0: uint64 = (b== 0x00 0x00) + (assert tmp%2#0) + let tmp%3#0: uint64 = (b== 0x0000000000000000 0x0000000000000000) + (assert tmp%3#0) + let tmp%4#0: uint64 = (b== 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) + (assert tmp%4#0) return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_1.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_1.ir index 0e530df712..596cbf7c58 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_1.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_1.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let int8_encoded#0: bytes = 0xff let int8_decoded#0: uint64 = (btoi int8_encoded#0) let tmp%1#0: uint64 = (== 255u int8_decoded#0) @@ -87,5 +87,15 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 + let tmp%0#0: uint64 = 1u + (assert tmp%0#0) + let tmp%1#0: uint64 = 1u + (assert tmp%1#0) + let tmp%2#0: uint64 = 1u + (assert tmp%2#0) + let tmp%3#0: uint64 = 1u + (assert tmp%3#0) + let tmp%4#0: uint64 = 1u + (assert tmp%4#0) return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_2.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_2.ir index 32c107f992..a8a18b03db 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_2.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_2.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let int8_decoded#0: uint64 = 255u let tmp%1#0: uint64 = (== 255u int8_decoded#0) (assert tmp%1#0) @@ -50,5 +50,5 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_3.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_3.ir index 6878559257..fb75cd392e 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_3.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_3.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let tmp%1#0: uint64 = 1u (assert tmp%1#0) let tmp%6#0: uint64 = 1u @@ -38,5 +38,5 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_4.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_4.ir index 119284633a..5cd79a5d6a 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_4.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_4.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let tmp%7#0: bytes = 0x7f let tmp%8#0: uint64 = (btoi tmp%7#0) let tmp%9#0: uint64 = (== tmp%8#0 127u) @@ -28,5 +28,5 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_5.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_5.ir index 81abc03188..cf756ac66a 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_5.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_5.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let tmp%8#0: uint64 = 127u let tmp%9#0: uint64 = (== tmp%8#0 127u) (assert tmp%9#0) @@ -21,5 +21,5 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_6.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_6.ir index e0ac8e096b..736391ceb2 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_6.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_6.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let tmp%9#0: uint64 = 1u (assert tmp%9#0) let tmp%17#0: uint64 = 1u @@ -16,5 +16,5 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_7.ir b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_7.ir index b674536b7e..0eb163ee35 100644 --- a/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_7.ir +++ b/test_cases/arc4_types/out/Arc4NumericTypesContract.ssa.opt_pass_7.ir @@ -1,10 +1,10 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 return 1u program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out/numeric.awst b/test_cases/arc4_types/out/numeric.awst index 675538908b..3b258a5f9b 100644 --- a/test_cases/arc4_types/out/numeric.awst +++ b/test_cases/arc4_types/out/numeric.awst @@ -47,6 +47,11 @@ contract Arc4NumericTypesContract clear_state_program(): bool { + assert(reinterpret_cast(reinterpret_cast(0E-10arc4u64x10)) == 0n) + assert(reinterpret_cast(reinterpret_cast(0.00000arc4n512x5)) == 0n) + assert(reinterpret_cast(0arc4u8) == reinterpret_cast(0arc4u8)) + assert(reinterpret_cast(0arc4u64) == reinterpret_cast(0arc4u64)) + assert(reinterpret_cast(0arc4n512) == reinterpret_cast(0arc4n512)) return true } } \ No newline at end of file diff --git a/test_cases/arc4_types/out_O2/Arc4NumericTypesContract.destructured.ir b/test_cases/arc4_types/out_O2/Arc4NumericTypesContract.destructured.ir index b674536b7e..0eb163ee35 100644 --- a/test_cases/arc4_types/out_O2/Arc4NumericTypesContract.destructured.ir +++ b/test_cases/arc4_types/out_O2/Arc4NumericTypesContract.destructured.ir @@ -1,10 +1,10 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 return 1u program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.approval.teal b/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.approval.teal index fba8b8942e..55789b3cfb 100644 --- a/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.approval.teal +++ b/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.approval.teal @@ -1,25 +1,25 @@ #pragma version 10 test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: - // arc4_types/numeric.py:26 + // arc4_types/numeric.py:28 // uint8 = UInt64(255) int 255 - // arc4_types/numeric.py:28 + // arc4_types/numeric.py:30 // int8_encoded = UInt8(uint8) dup itob extract 7 1 - // arc4_types/numeric.py:30 + // arc4_types/numeric.py:32 // int8_decoded = int8_encoded.native btoi - // arc4_types/numeric.py:32 + // arc4_types/numeric.py:34 // assert uint8 == int8_decoded == assert - // arc4_types/numeric.py:34 + // arc4_types/numeric.py:36 // test_bytes = Bytes.from_hex("7FFFFFFFFFFFFFFF00") byte 0x7fffffffffffffff00 - // arc4_types/numeric.py:35 + // arc4_types/numeric.py:37 // assert UInt8.from_bytes(test_bytes[:1]).native == 2**8 - 1 - 2**7 dup dup @@ -42,7 +42,7 @@ test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: int 127 == assert - // arc4_types/numeric.py:36 + // arc4_types/numeric.py:38 // assert UIntN[typing.Literal[24]].from_bytes(test_bytes[:3]).native == 2**24 - 1 - 2**23 dup dup @@ -65,7 +65,7 @@ test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: int 8388607 == assert - // arc4_types/numeric.py:37 + // arc4_types/numeric.py:39 // assert UInt16.from_bytes(test_bytes[:2]).native == 2**16 - 1 - 2**15 dup dup @@ -88,7 +88,7 @@ test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: int 32767 == assert - // arc4_types/numeric.py:38 + // arc4_types/numeric.py:40 // assert UInt32.from_bytes(test_bytes[:4]).native == 2**32 - 1 - 2**31 dup dup @@ -111,7 +111,7 @@ test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: int 2147483647 == assert - // arc4_types/numeric.py:39 + // arc4_types/numeric.py:41 // assert ARC4UInt64.from_bytes(test_bytes[:8]).native == 2**64 - 1 - 2**63 dup len @@ -133,114 +133,114 @@ test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: int 9223372036854775807 == assert - // arc4_types/numeric.py:41 + // arc4_types/numeric.py:43 // decimals = Decimal("145.6853943940") byte 0x0000015333430684 - // arc4_types/numeric.py:43 + // arc4_types/numeric.py:45 // assert decimals.bytes == op.itob(145_6853943940) int 1456853943940 itob == assert - // arc4_types/numeric.py:45 + // arc4_types/numeric.py:47 // decimals_from_truncated_str = Decimal("145.0") byte 0x000001519abc2400 - // arc4_types/numeric.py:47 + // arc4_types/numeric.py:49 // assert decimals_from_truncated_str.bytes == op.itob(145_0000000000) int 1450000000000 itob == assert - // arc4_types/numeric.py:49 + // arc4_types/numeric.py:51 // thousand = Decimal("1e3") byte 0x000009184e72a000 - // arc4_types/numeric.py:51 + // arc4_types/numeric.py:53 // assert thousand.bytes.length == 8 dup len int 8 == assert - // arc4_types/numeric.py:52 + // arc4_types/numeric.py:54 // assert thousand.bytes == op.itob(1000_0000000000) int 10000000000000 itob == assert - // arc4_types/numeric.py:54 + // arc4_types/numeric.py:56 // one_decimal = Decimal("1.0") byte 0x00000002540be400 - // arc4_types/numeric.py:56 + // arc4_types/numeric.py:58 // assert one_decimal.bytes == op.itob(1_0000000000) int 10000000000 itob == assert - // arc4_types/numeric.py:58 + // arc4_types/numeric.py:60 // zero_decimal = Decimal("0.0") byte 0x0000000000000000 - // arc4_types/numeric.py:60 + // arc4_types/numeric.py:62 // assert zero_decimal.bytes == op.itob(0) int 0 itob == assert - // arc4_types/numeric.py:62 + // arc4_types/numeric.py:64 // small_decimal = Decimal("0.00000001") byte 0x0000000000000064 - // arc4_types/numeric.py:64 + // arc4_types/numeric.py:66 // assert small_decimal.bytes == op.itob(100) int 100 itob == assert - // arc4_types/numeric.py:66 + // arc4_types/numeric.py:68 // smaller_decimal = Decimal("1E-9") byte 0x000000000000000a - // arc4_types/numeric.py:68 + // arc4_types/numeric.py:70 // assert smaller_decimal.bytes == op.itob(10) int 10 itob == assert - // arc4_types/numeric.py:70 + // arc4_types/numeric.py:72 // smallest_decimal = Decimal("0.0000000001") byte 0x0000000000000001 - // arc4_types/numeric.py:72 + // arc4_types/numeric.py:74 // assert smallest_decimal.bytes == op.itob(1) int 1 itob == assert - // arc4_types/numeric.py:74 + // arc4_types/numeric.py:76 // sixty_four_decimal = Decimal("1844674407.3709551615") byte 0xffffffffffffffff - // arc4_types/numeric.py:76 + // arc4_types/numeric.py:78 // assert sixty_four_decimal.bytes == op.itob(1844674407_3709551615) int 18446744073709551615 itob == assert - // arc4_types/numeric.py:78 + // arc4_types/numeric.py:80 // really_big_int = BigUIntN[t.Literal[512]](sixty_four_byte_num) byte 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - // arc4_types/numeric.py:80 + // arc4_types/numeric.py:82 // assert really_big_int == BigUIntN[t.Literal[512]].from_bytes(really_big_int.bytes) dup swap b== assert - // arc4_types/numeric.py:83 + // arc4_types/numeric.py:85 // BigUInt(sixty_four_byte_num).bytes byte 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - // arc4_types/numeric.py:86 + // arc4_types/numeric.py:88 // biguint = BigUInt(1) byte 0x01 - // arc4_types/numeric.py:87 + // arc4_types/numeric.py:89 // arc4_biguint_const = ARC4BigUInt(1) byte 0x00000000000000000000000000000001 swap - // arc4_types/numeric.py:88 + // arc4_types/numeric.py:90 // arc4_biguint_dynamic = ARC4BigUInt(biguint + 1) dup byte 0x01 @@ -253,25 +253,25 @@ test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program: int 16 bzero b| - // arc4_types/numeric.py:90 + // arc4_types/numeric.py:92 // assert biguint == arc4_biguint_const.native swap uncover 2 b== assert - // arc4_types/numeric.py:92 + // arc4_types/numeric.py:94 // assert arc4_biguint_dynamic.bytes.length == (128 // 8) len int 16 == assert - // arc4_types/numeric.py:94 + // arc4_types/numeric.py:96 // assert really_big_decimal.bytes.length == 64 len int 64 == assert - // arc4_types/numeric.py:96 + // arc4_types/numeric.py:98 // return True int 1 return diff --git a/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.clear.teal b/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.clear.teal index d2bc2fe32c..62cb9ce94c 100644 --- a/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.clear.teal +++ b/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.clear.teal @@ -1,7 +1,37 @@ #pragma version 10 test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program: - // arc4_types/numeric.py:99 + // arc4_types/numeric.py:101 + // assert BigUInt.from_bytes(Decimal().bytes) == 0 + byte 0x0000000000000000 + byte 0x + b== + assert + // arc4_types/numeric.py:102 + // assert BigUInt.from_bytes(BigUFixedNxM[t.Literal[512], t.Literal[5]]().bytes) == 0 + byte 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + byte 0x + b== + assert + // arc4_types/numeric.py:103 + // assert Byte() == 0 + byte 0x00 + byte 0x00 + b== + assert + // arc4_types/numeric.py:104 + // assert ARC4UInt64() == 0 + byte 0x0000000000000000 + byte 0x0000000000000000 + b== + assert + // arc4_types/numeric.py:105 + // assert UInt512() == 0 + byte 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + byte 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + b== + assert + // arc4_types/numeric.py:107 // return True int 1 return diff --git a/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.destructured.ir b/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.destructured.ir index 75417fff1f..e5f743ff65 100644 --- a/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.destructured.ir +++ b/test_cases/arc4_types/out_unoptimized/Arc4NumericTypesContract.destructured.ir @@ -1,7 +1,7 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program approval: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.approval_program() -> uint64: - block@0: // L25 + block@0: // L27 let uint8#0: uint64 = 255u let val_as_bytes%0#0: bytes = (itob uint8#0) let int8_encoded#0: bytes = ((extract 7 1) val_as_bytes%0#0) @@ -117,5 +117,15 @@ contract test_cases.arc4_types.numeric.Arc4NumericTypesContract: program clear-state: subroutine test_cases.arc4_types.numeric.Arc4NumericTypesContract.clear_state_program() -> uint64: - block@0: // L98 + block@0: // L100 + let tmp%0#0: uint64 = (b== 0x0000000000000000 0b) + (assert tmp%0#0) + let tmp%1#0: uint64 = (b== 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0b) + (assert tmp%1#0) + let tmp%2#0: uint64 = (b== 0x00 0x00) + (assert tmp%2#0) + let tmp%3#0: uint64 = (b== 0x0000000000000000 0x0000000000000000) + (assert tmp%3#0) + let tmp%4#0: uint64 = (b== 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) + (assert tmp%4#0) return 1u \ No newline at end of file diff --git a/test_cases/arc4_types/puya.log b/test_cases/arc4_types/puya.log index 5c73eabb36..aa906f94fb 100644 --- a/test_cases/arc4_types/puya.log +++ b/test_cases/arc4_types/puya.log @@ -554,10 +554,10 @@ debug: Sealing block@0: // L34 debug: Terminated block@0: // L34 debug: Sealing block@0: // L45 debug: Terminated block@0: // L45 -debug: Sealing block@0: // L25 -debug: Terminated block@0: // L25 -debug: Sealing block@0: // L98 -debug: Terminated block@0: // L98 +debug: Sealing block@0: // L27 +debug: Terminated block@0: // L27 +debug: Sealing block@0: // L100 +debug: Terminated block@0: // L100 debug: Sealing block@0: // L5 debug: Terminated block@0: // L5 debug: Sealing block@None: // and_contd_L15 @@ -10185,6 +10185,11 @@ debug: Splitting parallel copies prior to optimization debug: Optimizer: Constant Replacer debug: Optimizer: Copy Propagation debug: Optimizer: Intrinsic Simplifier +debug: Simplified (b== 0x0000000000000000 0b) to 1u +debug: Simplified (b== 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0b) to 1u +debug: Simplified (b== 0x00 0x00) to 1u +debug: Simplified (b== 0x0000000000000000 0x0000000000000000) to 1u +debug: Simplified (b== 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) to 1u debug: Optimizer: Remove Unused Variables debug: Optimizer: Simplify Control Ops debug: Optimizer: Remove Linear Jump @@ -10244,6 +10249,11 @@ debug: Optimizer: Constant Replacer debug: Optimizer: Copy Propagation debug: Optimizer: Intrinsic Simplifier debug: Optimizer: Remove Unused Variables +debug: Removing unused variable tmp%0#0 +debug: Removing unused variable tmp%1#0 +debug: Removing unused variable tmp%2#0 +debug: Removing unused variable tmp%3#0 +debug: Removing unused variable tmp%4#0 debug: Optimizer: Simplify Control Ops debug: Optimizer: Remove Linear Jump debug: Optimizer: Remove Empty Blocks