Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: inject closure-calls for loop externs in closures #1780

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,8 @@ type BlockNode interface {
GetNumNames() uint16
GetParentNode(Store) BlockNode
GetPathForName(Store, Name) ValuePath
GetExternPathForName(Store, Name) ValuePath
GetBlockNodeForPath(Store, ValuePath) BlockNode
GetIsConst(Store, Name) bool
GetLocalIndex(Name) (uint16, bool)
GetValueRef(Store, Name) *TypedValue
Expand Down Expand Up @@ -1557,6 +1559,7 @@ func (sb *StaticBlock) GetBlockNames() (ns []Name) {
}

// Implements BlockNode.
// NOTE: Extern names may also be local, if declared later.
func (sb *StaticBlock) GetExternNames() (ns []Name) {
return sb.Externs // copy?
}
Expand Down Expand Up @@ -1598,6 +1601,8 @@ func (sb *StaticBlock) GetPathForName(store Store, n Name) ValuePath {
}
// Register as extern.
// NOTE: uverse names are externs too.
// NOTE: if a name is later declared in this block later, it is both an
// extern name with depth > 1, as well as local name with depth == 1.
if !isFile(sb.GetSource(store)) {
sb.GetStaticBlock().addExternName(n)
}
Expand Down Expand Up @@ -1626,6 +1631,33 @@ func (sb *StaticBlock) GetPathForName(store Store, n Name) ValuePath {
panic(fmt.Sprintf("name %s not declared", n))
}

// Like GetPathForName, but always returns the path of the extern name.
// This is relevant for when a name is declared later in the block.
func (sb *StaticBlock) GetExternPathForName(store Store, n Name) ValuePath {
if n == "_" {
return NewValuePathBlock(0, 0, "_")
}
parent := sb.GetParentNode(store)
path := parent.GetPathForName(store, n)
path.Depth += 1
return path
}

// Get the containing block node for node with path relative to this containing block.
func (sb *StaticBlock) GetBlockNodeForPath(store Store, path ValuePath) BlockNode {
if path.Type != VPBlock {
panic("expected block type value path but got something else")
}

// NOTE: path.Depth == 1 means it's in bn.
var bn BlockNode = sb.GetSource(store)
for i := 1; i < int(path.Depth); i++ {
bn = bn.GetParentNode(store)
}

return bn
}

// Returns whether a name defined here in in ancestry is a const.
// This is not the same as whether a name's static type is
// untyped -- as in c := a == b, a name may be an untyped non-const.
Expand Down
231 changes: 221 additions & 10 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,16 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
// elide composite lit element (nested) composite types.
elideCompositeElements(clx, clt)
}
switch n.(type) {
// TRANS_LEAVE (deferred)---------
// NOTE: DO NOT USE TRANS_SKIP WITHIN BLOCK
// NODES, AS TRANS_LEAVE WILL BE SKIPPED; OR
// POP BLOCK YOURSELF.
case BlockNode:
// Pop block.
stack = stack[:len(stack)-1]
last = stack[len(stack)-1]
}
}()

// The main TRANS_LEAVE switch.
Expand Down Expand Up @@ -737,6 +747,88 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
cx := evalConst(store, last, n)
return cx, TRANS_CONTINUE

// TRANS_LEAVE -----------------------
case *FuncLitExpr:
// We need to closure-call if any variables
// used were declared in a for-loop, and this
// is the outer-most function within the loop.
// NOTE: if the variable is defined as a
// function parameter of the closure, then it
// is shadowed and not relevant whether or not
// the closure is immediately called.

// Step 1: check if this func lit is in a loop,
// not embedded within another func lit.
loopDepth, inLoop := isBlockNodeInLoop(store, n)
if !inLoop {
return n, TRANS_CONTINUE
}

// Step 2: gather all extern names declared in for-loops.
leNames := []Name{}
leTypes := []Type{}
lePaths := []ValuePath{}
for _, name := range n.GetExternNames() {
path := n.GetExternPathForName(store, name)
if path.Type != VPBlock {
continue // not a for-loop block path
}
if int(path.Depth) <= loopDepth+1 {
// it is within the loop, carry on...
} else {
// it is outside the closest loop ancestor,
// check to see if it is declared in outer loop.
container := n.GetBlockNodeForPath(store, path)
if _, ok := isBlockNodeInLoop(store, container); !ok {
continue // not declared in any loop.
}
}
typ := n.GetStaticTypeOfAt(store, path)
// Append loop extern name and related.
leNames = append(leNames, name)
lePaths = append(lePaths, path)
leTypes = append(leTypes, typ)
}
if len(leNames) == 0 {
// nothing to transform
return n, TRANS_CONTINUE
}
panic("!!! ITS HAPPENING (insert ron paul jazz hands) !!!")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: I haven't actually tested the running of this logic, yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"TestSearchEfficiency" is triggered by this but the tests have been passing because the closure is called synchronously. good sign that it is picked up by this panic, and probably a good one to focus on first.

Copy link
Contributor

Choose a reason for hiding this comment

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

ROFL! Epic panic message.

// Finally, do the transform.
// The inner closure loop externs will be
// modified to have a path depth of 2 (or more)
// (1+1 to reach the outer closure ), while all
// other inner non-loop-externs names will have
// path depths +1, while non-extern names will
// not be affected.
// The outer closure call’s passed arguments
// will be modified by the final preprocess
// call on the call expression (variable depth).
adjustLoopExternPaths(n, leNames, lePaths)
// Inject a closure-call and preprocess it.
paramFields := []interface{}{}
for i := 0; i < len(leNames); i++ {
paramFields = append(paramFields,
leNames[i],
constType(nil, leTypes[i]),
)
}
params := Flds(paramFields...)
fnType := evalStaticTypeOf(store, last, n)
results := Flds("", constType(nil, fnType))
returnFn := Return(n)
closure := Fn(params, results, []Stmt{returnFn})
leNameExprs := []interface{}{}
for _, leName := range leNames {
leNameExprs = append(leNameExprs, Nx(leName))
}
call := Call(closure, leNameExprs...)
// this preprocess will also set the path for the
// outer closure's params and the call's
// argument value paths.
call = Preprocess(store, last, call).(*CallExpr)
return call, TRANS_CONTINUE

// TRANS_LEAVE -----------------------
case *BinaryExpr:
lt := evalStaticTypeOf(store, last, n.Left)
Expand Down Expand Up @@ -1951,17 +2043,10 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
n.Type = constType(n.Type, dst)
}
// end type switch statement
// END TRANS_LEAVE -----------------------

// TRANS_LEAVE -----------------------
// finalization.
if _, ok := n.(BlockNode); ok {
// Pop block.
stack = stack[:len(stack)-1]
last = stack[len(stack)-1]
return n, TRANS_CONTINUE
} else {
return n, TRANS_CONTINUE
}
// Convenience return in case not already returned.
return n, TRANS_CONTINUE
}

panic(fmt.Sprintf(
Expand Down Expand Up @@ -3559,6 +3644,132 @@ func findDependentNames(n Node, dst map[Name]struct{}) {
}
}

// return the depth offset of loop and true if block node is in a loop,
// but not embedded within another func lit intermediary.
func isBlockNodeInLoop(store Store, bn BlockNode) (int, bool) {
depthOffset := 0
for bn != nil {
depthOffset += 1
bn = bn.GetParentNode(store)
switch bn.(type) {
case *FuncLitExpr:
return 0, false
case *ForStmt:
return depthOffset, true
default:
continue
}
}
return 0, false
}

func adjustLoopExternPaths(bn BlockNode, leNames []Name, lePaths []ValuePath) {
var depthOffset uint8 = 0
isLeName := func(name Name) (ValuePath, bool) {
for i, leName := range leNames {
if name == leName {
return lePaths[i], true
}
}
return ValuePath{}, false
}

Transcribe(bn, func(ns []Node, ftype TransField, index int, n Node, stage TransStage) (Node, TransCtrl) {
// expect only preprocessed nodes.
if n.GetAttribute(ATTR_PREPROCESSED) != true {
panic("expected only preprocessed nodes for adjustLoopExrternPaths")
}

switch stage {
case TRANS_ENTER:
switch cn := n.(type) {
case *NameExpr:
if cn.Path.Type != VPBlock {
panic("unexpected value path type for name")
}
lePath, isLeName := isLeName(cn.Name)
if isLeName {
// sanity check
if cn.Path.Depth >
lePath.Depth+depthOffset {
panic("should not happen")
}
// If the depth is shallow,
// it does not apply, was redeclared.
if cn.Path.Depth <
lePath.Depth+depthOffset {
return cn, TRANS_CONTINUE
}
// e.g.
// for {
// i := 0
// for {
// func() { // offset 0
// func() { // offset 1
// i // depth 4
// }
// }
// }
// }
// becomes
// for {
// i := 0
// for {
// func(i <int>) {
// func() { // offset 0
// func() { // offset 1
// i // depth of 1+1+1
// // 1 for base inner
// // 1 for offset
// // 1 for outer closure
// }
// }
// }(i)
// }
// }
cn.Path.Depth = 1 + depthOffset + 1
} else {
// If the depth is shallow,
// it is not affected.
if cn.Path.Depth <=
1+depthOffset {
return cn, TRANS_CONTINUE
} else {
// Non-loop extern names are
// bumped by 1.
cn.Path.Depth += 1
}
}
return cn, TRANS_CONTINUE
/*
case *FuncLitExpr:
// if func lit already has all args
// declared, we could skip recursion
// here, but tracking that would be
// cumbersome so just ignore and
// continue.
continue // "continue" to track offset.
*/
case BlockNode:
depthOffset += 1 // keep track of depth
return cn, TRANS_CONTINUE
default:
return cn, TRANS_SKIP
}
case TRANS_LEAVE:
switch cn := n.(type) {
case BlockNode:
depthOffset -= 1 // keep track of depth
return cn, TRANS_CONTINUE
default:
return cn, TRANS_SKIP
}
default:
return n, TRANS_CONTINUE
}
})
}

// ----------------------------------------
// SetNodeLocations

Expand Down
Loading