mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-25 13:56:35 +00:00
Merge pull request #815 from nspcc-dev/fix/stacksize
compiler: calculate stack size more precisely
This commit is contained in:
commit
6b4c66a807
2 changed files with 21 additions and 1 deletions
|
@ -3,6 +3,8 @@ package compiler_test
|
||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var assignTestCases = []testCase{
|
var assignTestCases = []testCase{
|
||||||
|
@ -133,3 +135,18 @@ var assignTestCases = []testCase{
|
||||||
func TestAssignments(t *testing.T) {
|
func TestAssignments(t *testing.T) {
|
||||||
runTestCases(t, assignTestCases)
|
runTestCases(t, assignTestCases)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManyAssignments(t *testing.T) {
|
||||||
|
src1 := `package foo
|
||||||
|
func Main() int {
|
||||||
|
a := 0
|
||||||
|
`
|
||||||
|
src2 := `return a
|
||||||
|
}`
|
||||||
|
|
||||||
|
for i := 0; i < vm.MaxArraySize; i++ {
|
||||||
|
src1 += "a += 1\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
eval(t, src1+src2, big.NewInt(vm.MaxArraySize))
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A funcScope represents the scope within the function context.
|
// A funcScope represents the scope within the function context.
|
||||||
|
@ -76,7 +77,9 @@ func (c *funcScope) stackSize() int64 {
|
||||||
ast.Inspect(c.decl, func(n ast.Node) bool {
|
ast.Inspect(c.decl, func(n ast.Node) bool {
|
||||||
switch n := n.(type) {
|
switch n := n.(type) {
|
||||||
case *ast.AssignStmt:
|
case *ast.AssignStmt:
|
||||||
size += len(n.Rhs)
|
if n.Tok == token.DEFINE {
|
||||||
|
size += len(n.Rhs)
|
||||||
|
}
|
||||||
case *ast.ReturnStmt, *ast.IfStmt:
|
case *ast.ReturnStmt, *ast.IfStmt:
|
||||||
size++
|
size++
|
||||||
// This handles the inline GenDecl like "var x = 2"
|
// This handles the inline GenDecl like "var x = 2"
|
||||||
|
|
Loading…
Reference in a new issue