Skip to content

Commit

Permalink
fix: handle single item tuples correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx authored and achidlow committed Jun 25, 2024
1 parent d876b25 commit 2026815
Show file tree
Hide file tree
Showing 20 changed files with 669 additions and 488 deletions.
2 changes: 1 addition & 1 deletion examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
too_many_permutations 112 106 6 106 0
transaction/Transaction 996 932 64 932 0
tuple_support/TupleComparisons 136 75 61 75 0
tuple_support/TupleSupport 632 406 226 406 0
tuple_support/TupleSupport 643 413 230 413 0
typed_abi_call/Greeter 2861 2446 415 2446 0
typed_abi_call/Logger 896 762 134 762 0
unary/Unary 136 96 40 96 0
Expand Down
7 changes: 7 additions & 0 deletions src/puya/ir/builder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ def visit_checked_maybe(self, expr: awst_nodes.CheckedMaybe) -> TExpression:
return value

def visit_var_expression(self, expr: awst_nodes.VarExpression) -> TExpression:
# TODO: remove this once wtypes are available at IR layer and assign can deal with
# single item tuples correctly
if isinstance(expr.wtype, wtypes.WTuple) and len(expr.wtype.types) == 1:
ir_type = wtype_to_ir_type(expr.wtype.types[0])
return self.context.ssa.read_variable(
expr.name, ir_type, self.context.block_builder.active_block
)
if isinstance(expr.wtype, wtypes.WTuple):
return ValueTuple(
source_location=expr.source_location,
Expand Down
261 changes: 135 additions & 126 deletions test_cases/tuple_support/out/TupleSupport.approval.mir

Large diffs are not rendered by default.

84 changes: 48 additions & 36 deletions test_cases/tuple_support/out/TupleSupport.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,22 @@ main_entrypoint@2:
byte "not empty2"
log
// tuple_support/tuple_support.py:59
// single_tuple()
callsub single_tuple
// tuple_support/tuple_support.py:60
// return a + b
int 3
return


// test_cases.tuple_support.tuple_support.add_three_values(values.0: uint64, values.1: uint64, values.2: uint64) -> uint64:
add_three_values:
// tuple_support/tuple_support.py:121-122
// tuple_support/tuple_support.py:128-129
// @subroutine
// def add_three_values(values: tuple[UInt64, UInt64, UInt64]) -> UInt64:
proto 3 1
byte ""
// tuple_support/tuple_support.py:123
// tuple_support/tuple_support.py:130
// total = UInt64(0)
int 0
frame_dig -3
Expand All @@ -164,7 +167,7 @@ add_three_values_for_header_2@3:
frame_dig 0

add_three_values_for_body@4:
// tuple_support/tuple_support.py:125
// tuple_support/tuple_support.py:132
// total += value
frame_dig 1
uncover 2
Expand All @@ -176,62 +179,62 @@ add_three_values_for_body@4:
int 1
+
frame_bury 0
// tuple_support/tuple_support.py:124
// tuple_support/tuple_support.py:131
// for value in values:
switch add_three_values_for_header_1@2 add_three_values_for_header_2@3
// tuple_support/tuple_support.py:127
// tuple_support/tuple_support.py:134
// return total
frame_bury 0
retsub


// test_cases.tuple_support.tuple_support.bytes_combine(arg.0: bytes, arg.1: bytes) -> bytes:
bytes_combine:
// tuple_support/tuple_support.py:105-106
// tuple_support/tuple_support.py:112-113
// @subroutine
// def bytes_combine(arg: tuple[Bytes, Bytes]) -> Bytes:
proto 2 1
// tuple_support/tuple_support.py:108
// tuple_support/tuple_support.py:115
// result = a + b
frame_dig -2
frame_dig -1
concat
// tuple_support/tuple_support.py:109
// tuple_support/tuple_support.py:116
// return result
retsub


// test_cases.tuple_support.tuple_support.addw2(a.0: uint64, a.1: uint64, b.0: uint64, b.1: uint64) -> uint64, uint64, uint64:
addw2:
// tuple_support/tuple_support.py:130-131
// tuple_support/tuple_support.py:137-138
// @subroutine
// def addw2(a: tuple[UInt64, UInt64], b: tuple[UInt64, UInt64]) -> tuple[UInt64, UInt64, UInt64]:
proto 4 3
// tuple_support/tuple_support.py:134
// tuple_support/tuple_support.py:141
// lo_carry, c_lo = op.addw(a_lo, b_lo)
frame_dig -3
frame_dig -1
addw
swap
// tuple_support/tuple_support.py:135
// tuple_support/tuple_support.py:142
// hi_carry1, c_mid = op.addw(a_hi, b_hi)
frame_dig -4
frame_dig -2
addw
// tuple_support/tuple_support.py:136
// tuple_support/tuple_support.py:143
// hi_carry2, c_mid = op.addw(c_mid, lo_carry)
uncover 2
addw
cover 2
// tuple_support/tuple_support.py:137
// tuple_support/tuple_support.py:144
// did_overflow, c_hi = op.addw(hi_carry1, hi_carry2)
addw
swap
// tuple_support/tuple_support.py:138
// tuple_support/tuple_support.py:145
// assert not did_overflow, "is such a thing even possible? 👽"
!
assert // is such a thing even possible? 👽
// tuple_support/tuple_support.py:139
// tuple_support/tuple_support.py:146
// return c_hi, c_mid, c_lo
swap
uncover 2
Expand All @@ -240,31 +243,31 @@ addw2:

// test_cases.tuple_support.tuple_support.bytes_multiply(arg.0: bytes, arg.1: uint64) -> bytes:
bytes_multiply:
// tuple_support/tuple_support.py:112-113
// tuple_support/tuple_support.py:119-120
// @subroutine
// def bytes_multiply(arg: tuple[Bytes, UInt64]) -> Bytes:
proto 2 1
// tuple_support/tuple_support.py:115
// tuple_support/tuple_support.py:122
// result = Bytes()
byte 0x
// tuple_support/tuple_support.py:116
// tuple_support/tuple_support.py:123
// for _i in urange(count):
int 0

bytes_multiply_for_header@1:
// tuple_support/tuple_support.py:116
// tuple_support/tuple_support.py:123
// for _i in urange(count):
frame_dig 1
frame_dig -1
<
bz bytes_multiply_after_for@5
// tuple_support/tuple_support.py:117
// tuple_support/tuple_support.py:124
// result += b
frame_dig 0
frame_dig -2
concat
frame_bury 0
// tuple_support/tuple_support.py:116
// tuple_support/tuple_support.py:123
// for _i in urange(count):
frame_dig 1
int 1
Expand All @@ -273,33 +276,33 @@ bytes_multiply_for_header@1:
b bytes_multiply_for_header@1

bytes_multiply_after_for@5:
// tuple_support/tuple_support.py:118
// tuple_support/tuple_support.py:125
// return result
retsub


// test_cases.tuple_support.tuple_support.test_tuple_swap(zero: uint64) -> void:
test_tuple_swap:
// tuple_support/tuple_support.py:142-143
// tuple_support/tuple_support.py:149-150
// @subroutine
// def test_tuple_swap(zero: UInt64) -> None:
proto 1 0
// tuple_support/tuple_support.py:144
// tuple_support/tuple_support.py:151
// a = zero + 1
frame_dig -1
int 1
+
// tuple_support/tuple_support.py:145
// tuple_support/tuple_support.py:152
// b = zero + 2
frame_dig -1
int 2
+
// tuple_support/tuple_support.py:147
// tuple_support/tuple_support.py:154
// assert a == 2, "a should be two"
int 2
==
assert // a should be two
// tuple_support/tuple_support.py:148
// tuple_support/tuple_support.py:155
// assert b == 1, "b should be one"
int 1
==
Expand All @@ -309,11 +312,11 @@ test_tuple_swap:

// test_cases.tuple_support.tuple_support.slicing(values.0: uint64, values.1: uint64, values.2: uint64, values.3: uint64, values.4: uint64, values.5: uint64, values.6: uint64, values.7: uint64) -> void:
slicing:
// tuple_support/tuple_support.py:151-152
// tuple_support/tuple_support.py:158-159
// @subroutine
// def slicing(values: tuple[UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64]) -> None:
proto 8 0
// tuple_support/tuple_support.py:154
// tuple_support/tuple_support.py:161
// assert add_three_values(one_to_three) == values[0] + values[1] + values[2]
frame_dig -8
frame_dig -7
Expand All @@ -331,7 +334,7 @@ slicing:

// test_cases.tuple_support.tuple_support.bin_ops() -> void:
bin_ops:
// tuple_support/tuple_support.py:77-78
// tuple_support/tuple_support.py:84-85
// @subroutine
// def bin_ops() -> None:
proto 0 0
Expand All @@ -340,15 +343,15 @@ bin_ops:

// test_cases.tuple_support.tuple_support.non_empty_tuple() -> uint64, uint64:
non_empty_tuple:
// tuple_support/tuple_support.py:71-72
// tuple_support/tuple_support.py:78-79
// @subroutine
// def non_empty_tuple() -> tuple[UInt64, UInt64]:
proto 0 2
// tuple_support/tuple_support.py:73
// tuple_support/tuple_support.py:80
// log("non_empty_tuple called")
byte "non_empty_tuple called"
log
// tuple_support/tuple_support.py:74
// tuple_support/tuple_support.py:81
// return UInt64(4), UInt64(2)
int 4
int 2
Expand All @@ -357,20 +360,29 @@ non_empty_tuple:

// test_cases.tuple_support.tuple_support.get_uint_with_side_effect() -> uint64:
get_uint_with_side_effect:
// tuple_support/tuple_support.py:65-66
// tuple_support/tuple_support.py:72-73
// @subroutine
// def get_uint_with_side_effect() -> UInt64:
proto 0 1
// tuple_support/tuple_support.py:67
// tuple_support/tuple_support.py:74
// log("get_uint_with_side_effect called")
byte "get_uint_with_side_effect called"
log
// tuple_support/tuple_support.py:68
// tuple_support/tuple_support.py:75
// return UInt64(4)
int 4
retsub


// test_cases.tuple_support.tuple_support.single_tuple() -> void:
single_tuple:
// tuple_support/tuple_support.py:66-67
// @subroutine
// def single_tuple() -> None:
proto 0 0
retsub


// test_cases.tuple_support.tuple_support.TupleSupport.__init__() -> void:
__init__:
// tuple_support/tuple_support.py:5
Expand Down
4 changes: 2 additions & 2 deletions test_cases/tuple_support/out/TupleSupport.clear.mir
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

// test_cases.tuple_support.tuple_support.TupleSupport.clear_state_program() -> uint64:
main_block@0:
int 0 // 0 UInt64(0) tuple_support/tuple_support.py:62
return // return UInt64(0) tuple_support/tuple_support.py:62
int 0 // 0 UInt64(0) tuple_support/tuple_support.py:63
return // return UInt64(0) tuple_support/tuple_support.py:63

2 changes: 1 addition & 1 deletion test_cases/tuple_support/out/TupleSupport.clear.teal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma version 10

test_cases.tuple_support.tuple_support.TupleSupport.clear_state_program:
// tuple_support/tuple_support.py:62
// tuple_support/tuple_support.py:63
// return UInt64(0)
int 0
return
Loading

0 comments on commit 2026815

Please sign in to comment.