Skip to content

Conversation

@tzaffi
Copy link
Contributor

@tzaffi tzaffi commented Feb 2, 2022

Example PyTeal, with resulting TEAL and explanation:

PyTEAL

@Subroutine(TealType.uint64)
def xyz(x: ScratchVar, y: ScratchVar, z):
    return Seq(
        x.store(x.load() + Int(1)),
        y.store(y.load() + Int(2)),
        x.load() + y.load() + z,
    )


def approval_xyz():
    a = ScratchVar(TealType.uint64, 142)
    b = ScratchVar(TealType.uint64, 143)
    return Seq(
        [
            a.store(Int(100)),
            b.store(Int(200)),
            Pop(a.load() + b.load()),
            Approve(),
        ]
    )

TEAL + Explanation

#pragma version 6
int 100         
store 142       // a@142: 100
int 200
store 143       // b@143: 200
...
int 142
int 143
int 177
callsub xyz_0       
pop
load 142
load 143
+
pop
int 1
return

// previously stored
            // a@142: 100
            // b@143: 200

// xyz
xyz_0:      // >>> 142, 143, 177    >>> @x, @y, z
store 2     // lz@2: 177            2: z
store 1     // ly@1: b@143          1: @y
store 0     // lx@0: a@142          0: @x
load 0      // >>> a@142            >>> @x
load 0      // >>> a@142, a@142     >>> @x, @x
loads       // >>> a@142, 100       >>> @x, x
int 1       // >>> a@142, 100, 1    >>> @x, x, 1
+           // >>> a@142, 101       >>> @x, x+1
stores      // a@142: 101           @x: x+1
load 1      // >>> b@143            >>> @y
load 1      // >>> b@143, b@143     >>> @y, @y
loads       // >>> b@143, 200       >>> @y, y
int 2       // >>> b@143, 200, 2    >>> @y, y, 2
+           // >>> b@143, 202       >>> @y, y+2
stores      // b@143: 202           @y: y+2
load 0      // >>> a@142            >>> @x
loads       // >>> 101              >>> x+1
load 1      // >>> 101, b@143       >>> x+1, @y 
loads       // >>> 101, 202         >>> x+1, y+2
+           // >>> 303              >>> (x+1)+(y+2)
load 2      // >>> 303, 177         >>> (x+1)+(y+2), z
+           // >>> 480              >>> (x+1)+(y+2)+z
retsub

// SUMMARY
// @x: x+1
// @y: y+2
// returns (x+1)+(y+2)+z

@tzaffi tzaffi marked this pull request as draft February 2, 2022 05:06
tzaffi added a commit that referenced this pull request Feb 2, 2022
This PR requires that any type annotation of a Subroutine parameter or return value be of type `Expr`. Missing annotations are assumed to be `Expr`'s as well. In a follow up PR #183 this restriction will be loosened.
Base automatically changed from subroutine-annotation-expr-if-present to master February 2, 2022 18:52
@tzaffi tzaffi changed the title handle ScratchVar and Expr subtypes for the return WIP: stores + loads Feb 2, 2022
@tzaffi tzaffi linked an issue Feb 2, 2022 that may be closed by this pull request
"""
return ScratchLoad(self, type)

# TODO: Can I get this to work?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't leave as is

def dynamic(self) -> bool:
return True

# TODO: Can I get this to work?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't leave as is

slotIdExpr, Expr
), "slotIdExpr must be an Expr but was provided {}".format(type(slotIdExpr))

self.id: Expr = slotIdExpr
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code smell: id property can either be an int or Expr based on Slot subclass

for stmt in teal:
for subroutine in stmt.getSubroutines():
referencedSubroutines.add(subroutine)
assert isinstance(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new assertion would break any previous code that has a misbehaving getSubroutines() method

if self.declaration is None:
# lazy evaluate subroutine
self.declaration = evaluateSubroutine(self)
self.declaration = evaluateSubroutine(self, callback=callback)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code smell: lazy evaluation is no longer valid because a parameter is being introduced. In this case, I believe we're ok (when you follow the branches), but it's preferred to either:

  • get rid of the laziness here and take a performance hit
  • or split this into a new method that expects the parameter and isn't lazy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

JUST PLAIN WRONG ...gonna fix

@tzaffi tzaffi changed the title Opcodes stores + loads _and_ Pass-By-Reference Semantics Opcodes stores + loads and Pass-By-Reference Semantics Feb 9, 2022
@tzaffi
Copy link
Contributor Author

tzaffi commented Feb 14, 2022

Closing for now - too much detritus. Need fresh start with less open ended approach.

@tzaffi tzaffi closed this Feb 14, 2022
@tzaffi
Copy link
Contributor Author

tzaffi commented Feb 14, 2022

Might re-base this off of #195

algoidurovic pushed a commit to algoidurovic/pyteal that referenced this pull request Mar 23, 2022
This PR requires that any type annotation of a Subroutine parameter or return value be of type `Expr`. Missing annotations are assumed to be `Expr`'s as well. In a follow up PR algorand#183 this restriction will be loosened.
algoidurovic added a commit that referenced this pull request Mar 31, 2022
* Optimization added for repeated int constants under 2**7 w/ tests

* fixed type problem and formatted

* Expanded test and added comment for clarification

* implement optimization utility with simple slot store/load canceling

* minor refactor

* reformat code

* Update pyteal/compiler/optimizer/optimizer.py

Co-authored-by: Michael Diamant <michaeldiamant@users.noreply.github.com>

* Update pyteal/compiler/optimizer/optimizer.py

Co-authored-by: Michael Diamant <michaeldiamant@users.noreply.github.com>

* Adding exponentiation to arithmatic ops docs (#134)

Add missing exponentiation operation in document

* updating to use new syntax for seq (#135)

* updating to use new syntax for seq

* rewording

* Make pylance recognize wildcard imports (#133)

* adding exports directly to top level __all__

* apply black formatter

* adding initial generate script

* fmt

* rm all from all

* adding check to travis

* reading in original __init__ and using its imports, dont write to filesystem if --check is passed

* make messages more profesh

* fix flags after black formatted them

* y

* flippin black formatter

* help text fix

* asdfasdf

* Include pyi files in build (#137)

* Revert "Optimization for constant assembly (#128)"

This reverts commit 5636ccd.

* Revert "String optimization and addition of Suffix() (#126)"

This reverts commit 7cb7b9a.

* Update to v0.9.1 (#138)

* Revert "Revert "String optimization and addition of Suffix() (#126)""

This reverts commit 564e602.

* Revert "Revert "Optimization for constant assembly (#128)""

This reverts commit cc405a5.

* Update examples.rst (#140)

* Fix type for App.globalGetEx in docs (#142)

* up max teal version (#146)

* up max teal version

* make test fail if its greater than version defined as MAX_TEAL_VERSION

* Fmt

* hardcode to 7

* Add version 6 test

* Formatting subroutines with name and newline (#148)

* using the subroutine name for the label

* adding newline before label declaration, fix tests to account for newline

* remove commented name, fix test

* only add newline for subroutines with comment

* naming with suffix

* adding test for invalid name

* Call type_of() in require_type() for better exception messages (#151)

* call type_of in require_type to catch exceptions

* fix formatting for types.py and types_test.py

* `method` pseudo-op support for ABI methods (#153)

- Add support for `method` pseudo-opcode in PyTeal.
- Add `name` field in `subroutine` to override __name__ from function implementation, for readability in generated code.

* Print diff of `__init__.pyi` (#166)

* Print diff of __init__.pyi

* Format

* Undo travis change

* C2C Feature Support (#149)

- `itxn_next` implementation / test
- `itxn_field` support for array field setting
- `gitxn / gitxna` implementation / test
- `gloadss` implementation / test

* Add BytesSqrt (#163)

* Add BytesSqrt

* Update pyteal/ast/unaryexpr_test.py

Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>

Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>

* adding new globals from teal6 (#168)

* adding new globals from teal6

* fmt

* Acct params get (#165)

* Adding account param getter

* Add to init

* fix op names and type

* adding tests

* allow bytes to be passed

* tweak docs, add require check for any

* Change Subroutine Wrapped Callable to a class with call method (#171)

Allows for more information (name, return type, has return) about the subroutine extractable from wrapped fnImpl by subroutine

* Subroutine Type Annotations (#182)

This PR requires that any type annotation of a Subroutine parameter or return value be of type `Expr`. Missing annotations are assumed to be `Expr`'s as well. In a follow up PR #183 this restriction will be loosened.

* fix docs referencing what apps should eval to (#191)

* Move from Travis to Github Actions (#190)

* MultiValue expression implemented to support opcodes that return multiple values (#196)

* Optimization added for repeated int constants under 2**7 w/ tests

* fixed type problem and formatted

* Expanded test and added comment for clarification

* add multivalue expr and change maybevalue to derive from multivalue

* updated tests and formatting

* reorder output slots to reflect stack ordering

* add additional assertion in MaybeValue test to enforce slot ordering

* Support TEAL 6 txn fields LastLog, StateProofPK and opcodes divw, itxnas, gitxnas (#174)

* adding new teal6 ops, no pyteal expressions defined for them yet

* Add opcode support for divw

* Add opcode support for divw (#192)

* Add opcode support for itxnas and gitxnas (#193)

* Add opcode support for itxnas and gitxnas

* Update stale reference to inner transaction limit

* Fix allowed types for GitxnaExpr txnIndex

* Remove obsolete logic for handling GitxnaExpr.teal construction

* Remove unnecessary cast and fix gitxna runtime type checking

* Move type validation to constructors for gtxn and gitxn variants

* Add missed tests from prior commit

* Fix duplicate test case

* Move index validation from subclasses to TxnaExpr

* Inline validation functions per PR feedback

* Remove unused imports

* Refactor to isinstance tupled check

* Remove TEAL v1 min version test per PR feedback

* Fix constructor type checking for GtxnExpr

* Refactor to remove duplicate type check function

* Update last_log docstring

Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>

* Expose state_proof_pk txn field

* Update transaction field docs to reflect TEAL v6

* Update transaction field docs to reflect TEAL v6

Co-authored-by: michaeldiamant <michaeldiamant@users.noreply.github.com>
Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>

* Fixed typo (#202)

* Add Github action to generate docset (#201)

* Add build docset step

* non-slim container

* Update docs to group transaction field tables like go-algorand (#204)

* Update accessing_transaction_field.rst to fix typo (#207)

* Add docs README to explain docs/ testing procedure (#205)

* v0.10.0 (#206)

* Update to v0.10.0

* Add latest commits to changelog

* fixing github actions to run on tags (#208)

* Update build.yml

* Update build.yml

* Fix typos in docstrings and error messages (#211)

* Test on Python 3.10 (#212)

* Update versions.rst (#210)

* Update versions.rst

content of [https://github.com/algorand/pyteal/releases] is not shown in [https://pyteal.readthedocs.io/en/latest/versions.html]

* Update docs/versions.rst

Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>

Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>

* Pass-by-Ref / Dynamic Scratch Variables via the `loads` and `stores` opcodes (#198)

* Pass-by-Reference Semantics
* Use a Dynamic ScratchVar to "iterate" over other ScratchVar's
* Another approach for E2E testing

* Fix build script invocation (#223)

* Bring #225 to master (#227)

* Ignore tests generating TEAL file outputs used for expected comparisons (#228)

* Fix typo in CONTRIBUTING.md (#229)

* Fix subroutine mutual recursion with different argument counts bug (#234)

* Fix mutual recursion bug

* Remove usage of set.pop

* Revert "Pass-by-Ref / Dynamic Scratch Variables via the `loads` and `stores` opcodes (#198)"

This reverts commit cf95165.

* v0.10.1 (#237)

* Revert "Revert "Pass-by-Ref / Dynamic Scratch Variables via the `loads` and `stores` opcodes (#198)""

This reverts commit 51ec8c9.

* Update user guide docs to reflect addition of DynamicScratchVar (#226)

* Update CONTRIBUTING.md on PEP 8 naming conventions policy (#241)

* implement optimization utility with simple slot store/load canceling

* minor refactor

* reformat code

* correct import format to match convention

* slot optimization awareness of reserved ids added

* fix typo

* remove dataclass usage

* slight reorg of compiler process in order to perform optimization on cfg

* clean up imports

* updated documentation and reformatted with new version of black

* remove unused imports and comments

* reformatting

* add additional optimizer unit tests

* improve testing and slight refactoring

* more renaming

* documentation and import changes

* fixed typos in docs

Co-authored-by: Michael Diamant <michaeldiamant@users.noreply.github.com>
Co-authored-by: Ben Guidarelli <ben.guidarelli@gmail.com>
Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>
Co-authored-by: Edward D Gaudio <edwardgaudio@gmail.com>
Co-authored-by: Joe Polny <50534337+joe-p@users.noreply.github.com>
Co-authored-by: Hang Su <87964331+ahangsu@users.noreply.github.com>
Co-authored-by: Łukasz Ptak <StylishTriangles@users.noreply.github.com>
Co-authored-by: Zeph Grunschlag <tzaffi@users.noreply.github.com>
Co-authored-by: Jack <87339414+algojack@users.noreply.github.com>
Co-authored-by: Glory Agatevure <agatevureglory@gmail.com>
Co-authored-by: Adriano Di Luzio <aldur@users.noreply.github.com>
Co-authored-by: PabloLION <36828324+PabloLION@users.noreply.github.com>
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.

4 participants