Skip to content

Commit

Permalink
Use subroutines to handle inner_txns for better group handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fergalwalsh committed Dec 22, 2023
1 parent cd01f20 commit 5cc6c6e
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions tealish/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,44 @@ def consume(cls, compiler: "TealishCompiler", parent: Optional[Node]) -> "Progra
def process(self) -> None:
for n in self.nodes:
n.process()
if self.has_child_node(InnerTxn):
scope = self.get_current_scope()
var = scope.declare_scratch_var(
"inner_group_flag", IntType, self.compiler.max_slot + 1
)
self.compiler.max_slot = max(self.compiler.max_slot, var.scratch_slot)

def write_teal(self, writer: "TealWriter") -> None:
for n in self.child_nodes:
n.write_teal(writer)

if self.has_child_node(InnerTxn):
var = self.get_var("inner_group_flag")
teal = f"""
_itxn_group_begin:
load {var.scratch_slot}; !; assert // ensure no group active
int 1; store {var.scratch_slot}; retsub // set group flag
_itxn_begin:
load {var.scratch_slot}
switch _itxn_begin__0 _itxn_begin__1 _itxn_begin__2
err
_itxn_begin__0: itxn_begin; retsub // no group
_itxn_begin__1: itxn_begin; int 2; store {var.scratch_slot}; retsub // start first txn of group
_itxn_begin__2: itxn_next; retsub // start next txn of group
_itxn_submit:
load {var.scratch_slot}
bz _itxn_submit__0
retsub // in a group, don't submit
_itxn_submit__0: itxn_submit; retsub // no group, submit
_itxn_group_submit:
itxn_submit
int 0; store {var.scratch_slot}; retsub // set group flag to 0
"""
for line in teal.splitlines():
writer.write(self, line.strip())

def _tealish(self) -> str:
s = ""
for n in self.child_nodes:
Expand Down Expand Up @@ -874,16 +907,12 @@ def process(self) -> None:

def write_teal(self, writer: "TealWriter") -> None:
writer.write(self, f"// {self.line}")
if not self.group:
writer.write(self, "itxn_begin")
elif self.group_index > 0:
writer.write(self, "itxn_next")
writer.write(self, "callsub _itxn_begin")
writer.level += 1
for node in self.child_nodes:
writer.write(self, node)
writer.level -= 1
if not self.group:
writer.write(self, "itxn_submit")
writer.write(self, "callsub _itxn_submit")
writer.write(self, "// end inner_txn")

def _tealish(self) -> str:
Expand Down Expand Up @@ -917,11 +946,11 @@ def process(self) -> None:

def write_teal(self, writer: "TealWriter") -> None:
writer.write(self, f"// {self.line}")
writer.write(self, "callsub _itxn_group_begin")
writer.level += 1
writer.write(self, "itxn_begin")
for i, node in enumerate(self.child_nodes):
writer.write(self, node)
writer.write(self, "itxn_submit")
writer.write(self, "callsub _itxn_group_submit")
writer.level -= 1
writer.write(self, "// end inner_group")

Expand Down

0 comments on commit 5cc6c6e

Please sign in to comment.