Merge pull request #1488 from nspcc-dev/fix/compiler

compiler: fix manifest method offset
This commit is contained in:
Roman Khimov 2020-10-14 15:12:57 +03:00 committed by GitHub
commit e9a8e957f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View file

@ -1951,18 +1951,21 @@ func (c *codegen) writeJumps(b []byte) ([]byte, error) {
// Correct function ip range. // Correct function ip range.
// Note: indices are sorted in increasing order. // Note: indices are sorted in increasing order.
for _, f := range c.funcs { for _, f := range c.funcs {
start, end := f.rng.Start, f.rng.End
loop: loop:
for _, ind := range offsets { for _, ind := range offsets {
switch { switch {
case ind > int(f.rng.End): case ind > int(f.rng.End):
break loop break loop
case ind < int(f.rng.Start): case ind < int(f.rng.Start):
f.rng.Start -= longToShortRemoveCount start -= longToShortRemoveCount
f.rng.End -= longToShortRemoveCount end -= longToShortRemoveCount
case ind >= int(f.rng.Start): case ind >= int(f.rng.Start):
f.rng.End -= longToShortRemoveCount end -= longToShortRemoveCount
} }
} }
f.rng.Start = start
f.rng.End = end
} }
return shortenJumps(b, offsets), nil return shortenJumps(b, offsets), nil
} }

View file

@ -3,7 +3,12 @@ package compiler_test
import ( import (
"fmt" "fmt"
"math/big" "math/big"
"strings"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/require"
) )
func TestSimpleFunctionCall(t *testing.T) { func TestSimpleFunctionCall(t *testing.T) {
@ -269,3 +274,19 @@ func TestVariadicMethod(t *testing.T) {
}` }`
eval(t, src, big.NewInt(42)) eval(t, src, big.NewInt(42))
} }
func TestJumpOptimize(t *testing.T) {
src := `package foo
func Get1() int { return 1 }
func Get2() int { Get1(); Get1(); Get1(); Get1(); return Get1() }
func Get3() int { return Get2() }
func Main() int {
return Get3()
}`
b, di, err := compiler.CompileWithDebugInfo("", strings.NewReader(src))
require.NoError(t, err)
for _, mi := range di.Methods {
require.Equal(t, b[mi.Range.Start], byte(opcode.INITSLOT))
require.Equal(t, b[mi.Range.End], byte(opcode.RET))
}
}