-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MultiValue expression implemented to support opcodes that return mult…
…iple 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
- Loading branch information
1 parent
20b2346
commit bf4047f
Showing
6 changed files
with
298 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from typing import Callable, List, Union, TYPE_CHECKING | ||
|
||
from ..types import TealType | ||
from ..ir import TealOp, Op, TealBlock | ||
from .expr import Expr | ||
from .leafexpr import LeafExpr | ||
from .scratch import ScratchSlot | ||
from .seq import Seq | ||
|
||
if TYPE_CHECKING: | ||
from ..compiler import CompileOptions | ||
|
||
|
||
class MultiValue(LeafExpr): | ||
"""Represents an operation that returns more than one value""" | ||
|
||
def __init__( | ||
self, | ||
op: Op, | ||
types: List[TealType], | ||
*, | ||
immediate_args: List[Union[int, str]] = None, | ||
args: List[Expr] = None | ||
): | ||
"""Create a new MultiValue. | ||
Args: | ||
op: The operation that returns values. | ||
types: The types of the returned values. | ||
immediate_args (optional): Immediate arguments for the op. Defaults to None. | ||
args (optional): Stack arguments for the op. Defaults to None. | ||
""" | ||
super().__init__() | ||
self.op = op | ||
self.types = types | ||
self.immediate_args = immediate_args if immediate_args is not None else [] | ||
self.args = args if args is not None else [] | ||
|
||
self.output_slots = [ScratchSlot() for _ in self.types] | ||
|
||
def outputReducer(self, reducer: Callable[..., Expr]) -> Expr: | ||
input = [slot.load(self.types[i]) for i, slot in enumerate(self.output_slots)] | ||
return Seq(self, reducer(*input)) | ||
|
||
def __str__(self): | ||
ret_str = "(({}".format(self.op) | ||
for a in self.immediate_args: | ||
ret_str += " " + a.__str__() | ||
|
||
for a in self.args: | ||
ret_str += " " + a.__str__() | ||
ret_str += ") " | ||
|
||
ret_str += " ".join([slot.store().__str__() for slot in self.output_slots]) | ||
ret_str += ")" | ||
|
||
return ret_str | ||
|
||
def __teal__(self, options: "CompileOptions"): | ||
tealOp = TealOp(self, self.op, *self.immediate_args) | ||
callStart, callEnd = TealBlock.FromOp(options, tealOp, *self.args) | ||
|
||
curEnd = callEnd | ||
# the list is reversed in order to preserve the ordering of the opcode's returned | ||
# values. ie the output to stack [A, B, C] should correspond to C->output_slots[2] | ||
# B->output_slots[1], and A->output_slots[0]. | ||
for slot in reversed(self.output_slots): | ||
store = slot.store() | ||
storeStart, storeEnd = store.__teal__(options) | ||
curEnd.setNextBlock(storeStart) | ||
curEnd = storeEnd | ||
|
||
return callStart, curEnd | ||
|
||
def type_of(self): | ||
return TealType.none | ||
|
||
|
||
MultiValue.__module__ = "pyteal" |
Oops, something went wrong.