Skip to content

Conversation

@tzaffi
Copy link
Contributor

@tzaffi tzaffi commented Feb 14, 2022

Utilize loads and stores to Allow Specifying a Slot's Index with an Expr

Summary of Changes

  • Various __init__*'s
    • To bubble up the visibility of the new DynamicSlot and make mypy happy
    • Also, sorted some of the imports alphabetically
  • pyteal/ast/scratch.py
    • New DynamicSlot class a sibling to ScratchSlot with new abstract parent Slot
  • pyteal/ast/scratchvar.py - ability for ScratchVar to handle dynamic index by delegating to DynamicSlot when given a slotId of type Expr
  • pyteal/compiler/* and pyteal/ir/* - changes to make mypy happy, with some type assertions to make sure that we haven't contaminated the compilation with DynamicSlot's
  • New end-to-end test tests/dynamic_scratch_tests.py
    • Various expected outputs of compilation tests/teal/*_expected.py

TODO

  1. Some type assertions in the initializers of ScratchVar and *Slot's
  2. UNIT TESTS!!!!

Motivating Example

A "roaming" scratch variable:

def wilt_the_stilt():
    player_index = ScratchVar(TealType.uint64)
    player_score = ScratchVar(TealType.uint64, player_index.load())
    return Seq(
        player_index.store(Int(129)),  # Wilt Chamberlain
        player_score.store(Int(100)),
        player_index.store(Int(130)),  # Kobe Bryant
        player_score.store(Int(81)),
        player_index.store(Int(131)),  # David Thompson
        player_score.store(Int(73)),
        Assert(player_score.load() == Int(73)),
        Assert(player_score.index() == Int(131)),
        player_index.store(player_index.load() - Int(2)),  # back to Wilt:
        Assert(player_score.load() == Int(100)),
        Assert(player_score.index() == Int(129)),
        Int(100),
    )

with generated TEAL:

#pragma version 6
int 129
store 0         // set the roaming index variable to 129, aka Wilt
load 0
int 100         // give Wilt 100 points
stores          // Done with Wilt
int 130         
store 0         // set the roaming index variable to 130, aka Kobe
load 0
int 81
stores          // Done with Kobe
int 131         
store 0         // set the roaming index variable to 131, David Thompson, aka D.T.
load 0
int 73
stores          // Done with D.T.
load 0
loads
int 73
==
assert
load 0
int 131
==
assert          // asserting that player_score.Index() == Int(131)
load 0
int 2
-
store 0         // now the player index should be 129 aka Wilt's score
load 0          
loads           // Wilt's score
int 100         
==
assert
load 0
int 129
==
assert
int 100
return

from .config import MAX_GROUP_SIZE, NUM_SLOTS

__all__ = [
"Expr",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabetize and add DynamicSlot

from .for_ import For
from .break_ import Break
from .continue_ import Continue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabetize and add DynamicSlot

@tzaffi tzaffi requested a review from barnjamin February 15, 2022 01:54
@tzaffi tzaffi marked this pull request as ready for review February 15, 2022 01:54
)


def wilt_the_stilt():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roaming scratch variable example

@tzaffi tzaffi changed the title Fresh Start: Dynamic Scratch Variables Dynamic Scratch Variables Feb 15, 2022
from .compile_asserts import assert_new_v_old, compile_and_save


def dynamic_scratch():
Copy link
Contributor Author

@tzaffi tzaffi Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I continued developing against this test I realized that there is flakiness here mostly due to reordering of subroutines. So I will probably rename the *_test() methods so that they don't get triggered on Cricle.

@tzaffi
Copy link
Contributor Author

tzaffi commented Feb 17, 2022

another abandoned PR. In favor of #198

@tzaffi tzaffi closed this Feb 17, 2022
@tzaffi tzaffi deleted the dynamic-scratch branch February 18, 2022 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants