algorand/pyteal

Improve subroutine evaluation code (post frame pointers)

jasonpaulos opened this issue · 0 comments

This issue deals with code implemented in #584 and #562.

In those PRs, to probe info for has_return and type_of of subroutine declaration, subroutines are evaluated twice: once with ScratchVar arguments, and once with frame pointer arguments. Then during compilation the appropriate version of subroutine evaulator is chosen and used in code generation.

Generally speaking it would be nice to avoid having to evaluate every subroutine twice. Once way of doing that could be with something like a ProxyExpr:

class ProxyExpr(Expr):
    
    def __init__(self, type_of: TealType):
        super().__init__()
        self.representing: Expr | None = None
    
    def resolve_to(self, expr: Expr):
        self.representing = expr
    
    def __teal__(self, options: "CompileOptions"):
        if self.representing is None:
            raise TealInternalError("Not assigned....")
        return self.representing.__teal__(options)

The idea would be to use this as a placeholder before compilation to evaluate the subroutine, then assign the ProxyExpr to its actual value once that's known at compile time.