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

fix: re-create blocks on for loop iterations #1768

Closed
wants to merge 3 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
10 changes: 10 additions & 0 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,16 @@ func (m *Machine) ForcePopStmt() (s Stmt) {
return
}

func (m *Machine) ForceSwapStmt(target Stmt) {
lastStmt := len(m.Stmts) - 1
if debug {
s := m.Stmts[lastStmt]
m.Printf("-s %v\n", s)
m.Printf("+s %v\n", target)
}
m.Stmts[lastStmt] = target
}

// Offset starts at 1.
func (m *Machine) PeekExpr(offset int) Expr {
return m.Exprs[len(m.Exprs)-offset]
Expand Down
50 changes: 50 additions & 0 deletions gnovm/pkg/gnolang/op_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ func (m *Machine) doOpExec(op Op) {
s = next
goto EXEC_SWITCH
} else if bs.NextBodyIndex == bs.BodyLen {
// Create new block. This allows any closures generated within to
// reference the correct block, without having values changed.
cur := m.PopBlock()
newBlock := m.Alloc.NewBlock(cur.Source, m.LastBlock())
newBlock.bodyStmt = *bs
bs = &newBlock.bodyStmt
m.PushBlock(newBlock)
m.ForceSwapStmt(bs)
// TODO: instead of relying on source, maybe use .Op of bodystmt
sb := cur.Source.(*ForStmt)
if as, ok := sb.Init.(*AssignStmt); ok && as.Op == DEFINE {
// There is an init statement and it defines values.
// Copy over the values to the new block.
for i := 0; i < len(as.Lhs); i++ {
// Get name and value of i'th term.
nx := as.Lhs[i].(*NameExpr)
// Define new name using value of old.
curPtr := cur.GetPointerTo(m.Store, nx.Path)
ptr := newBlock.GetPointerTo(m.Store, nx.Path)
ptr.Assign2(m.Alloc, m.Store, m.Realm, *curPtr.TV, true)
}
}

// (queue to) go back.
if bs.Cond != nil {
m.PushExpr(bs.Cond)
Expand Down Expand Up @@ -207,6 +230,15 @@ func (m *Machine) doOpExec(op Op) {
goto EXEC_SWITCH
} else if bs.NextBodyIndex == bs.BodyLen {
if bs.ListIndex < bs.ListLen-1 {
// Create new block. This allows any closures generated within to
// reference the correct block, without having values changed.
cur := m.PopBlock()
newBlock := m.Alloc.NewBlock(cur.Source, m.LastBlock())
newBlock.bodyStmt = *bs
bs = &newBlock.bodyStmt
m.PushBlock(newBlock)
m.ForceSwapStmt(bs)

// set up next assign if needed.
switch bs.Op {
case ASSIGN:
Expand Down Expand Up @@ -301,6 +333,15 @@ func (m *Machine) doOpExec(op Op) {
goto EXEC_SWITCH
} else if bs.NextBodyIndex == bs.BodyLen {
if bs.StrIndex < bs.StrLen {
// Create new block. This allows any closures generated within to
// reference the correct block, without having values changed.
cur := m.PopBlock()
newBlock := m.Alloc.NewBlock(cur.Source, m.LastBlock())
newBlock.bodyStmt = *bs
bs = &newBlock.bodyStmt
m.PushBlock(newBlock)
m.ForceSwapStmt(bs)

// set up next assign if needed.
switch bs.Op {
case ASSIGN:
Expand Down Expand Up @@ -399,6 +440,15 @@ func (m *Machine) doOpExec(op Op) {
m.PopFrameAndReset()
return
} else {
// Create new block. This allows any closures generated within to
// reference the correct block, without having values changed.
cur := m.PopBlock()
newBlock := m.Alloc.NewBlock(cur.Source, m.LastBlock())
newBlock.bodyStmt = *bs
bs = &newBlock.bodyStmt
m.PushBlock(newBlock)
m.ForceSwapStmt(bs)

// set up next assign if needed.
switch bs.Op {
case ASSIGN:
Expand Down
26 changes: 26 additions & 0 deletions gnovm/tests/debug/3_break.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

// this is a fix, intuitive, and same with Go
func main() {
var fns []func() int

for i := 0; i < 5; i++ {
x := i
f := func() int {
return x
}
fns = append(fns, f)
x += 1
if i == 2 {
break
}
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 1
// 2
// 3
26 changes: 26 additions & 0 deletions gnovm/tests/debug/3_continue.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

// this is a fix, intuitive, and same with Go
func main() {
var fns []func() int

for i := 0; i < 5; i++ {
x := i
if i <= 2 {
continue
}

f := func() int {
return x
}
fns = append(fns, f)
x += 1
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 4
// 5
27 changes: 27 additions & 0 deletions gnovm/tests/debug/3_goto.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

// this is a fix, intuitive, and same with Go
func main() {
var fns []func() int

for i := 0; i < 5; i++ {
x := i
f := func() int {
return x
}
fns = append(fns, f)
x += 1
if i == 2 {
goto END
}
}
END:
for _, fn := range fns {
println(fn())
}
}

// Output:
// 1
// 2
// 3
26 changes: 26 additions & 0 deletions gnovm/tests/debug2/1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

func main() {
var y, counter int
var f []func()
defer func() {
for _, ff := range f {
ff()
}
}()

// this is actually a implicit for loop
LABEL_1:
if counter == 2 {
return
}
x := y
f = append(f, func() { println(x) })
y++
counter++
goto LABEL_1 // this is the edge condition, break? continue?
}

// Output:
// 0
// 1
23 changes: 23 additions & 0 deletions gnovm/tests/debug2/1a.gnoa
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

func main() {
var y int
var f []func()

defer func() {
for _, ff := range f {
ff()
}
}()

for i := 0; i < 3; i++ {
x := y
f = append(f, func() { println(x) })
y++
}
}

// Output:
// 0
// 1
// 2
25 changes: 25 additions & 0 deletions gnovm/tests/debug2/1a_goto.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

func main() {
var y, counter int
var f []func()
defer func() {
for _, ff := range f {
ff()
}
}()

// this is actually a implicit for loop
LABEL_1:
if counter == 2 {
return
}
f = append(f, func() { println(y) })
y++
counter++
goto LABEL_1 // this is the edge condition, break? continue?
}

// Output:
// 1
// 2
34 changes: 34 additions & 0 deletions gnovm/tests/debug2/1b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

// invalid goto
func main() {
var y, counter int
var f []func()
defer func() {
for _, ff := range f {
ff()
}
}()

// this is actually a implicit for loop
LABEL_1:
if counter == 5 {
goto LABEL_2
}
x := y
f = append(f, func() { println(x) })
y++
counter++
goto LABEL_1 // this is the edge condition, break? continue?

LABEL_2:
println("end")
}

// Output:
// end
// 0
// 1
// 2
// 3
// 4
36 changes: 36 additions & 0 deletions gnovm/tests/debug2/1b_goto.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

func main() {
var y, counter int

println(&y)

var f []func() *int

defer func() {
for _, ff := range f {
println(ff())
println(*ff())
}
}()

// this is actually a implicit for loop
LABEL_1:
if counter == 2 {
return
}
f = append(f, func() *int {
//x := &y
return &y
})
y++
counter++
goto LABEL_1 // this is the edge condition, break? continue?
}

// Output:
// &0x14000283f80.(*int)
// &0x14000283f80.(*int)
// 1
// &0x14000283f80.(*int)
// 2
34 changes: 34 additions & 0 deletions gnovm/tests/debug2/2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

// this is a fix, intuitive, and same with Go
func main() {
var fns []func() int
//defer func() {
// for _, fn := range fns {
// println(fn())
// }
//}()

for i := 0; i < 5; i++ {
//defer func() {
// for _, fn := range fns {
// println(fn())
// }
//}()
x := i
f := func() int {
return x
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 0
// 1
// 2
// 3
// 4
25 changes: 25 additions & 0 deletions gnovm/tests/debug2/3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

// this is a fix, intuitive, and same with Go
func main() {
var fns []func() int

for i := 0; i < 5; i++ {
x := i
f := func() int {
return x
}
fns = append(fns, f)
x += 1
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 1
// 2
// 3
// 4
// 5
Loading
Loading