compiler: split Compile info sub-functions

Also add tests for basic debug info.
This commit is contained in:
Evgenii Stratonikov 2020-03-31 16:16:32 +03:00
parent 00c40b58aa
commit 24fef35ead
3 changed files with 94 additions and 19 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"golang.org/x/tools/go/loader"
) )
// The identifier of the entry function. Default set to Main. // The identifier of the entry function. Default set to Main.
@ -1240,23 +1241,12 @@ func (c *codegen) newFunc(decl *ast.FuncDecl) *funcScope {
return f return f
} }
// CodeGen compiles the program to bytecode. func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error {
func CodeGen(info *buildInfo) ([]byte, error) {
pkg := info.program.Package(info.initialPackage)
c := &codegen{
buildInfo: info,
prog: io.NewBufBinWriter(),
l: []int{},
funcs: map[string]*funcScope{},
labels: map[labelWithType]uint16{},
typeInfo: &pkg.Info,
}
// Resolve the entrypoint of the program. // Resolve the entrypoint of the program.
main, mainFile := resolveEntryPoint(mainIdent, pkg) main, mainFile := resolveEntryPoint(mainIdent, pkg)
if main == nil { if main == nil {
c.prog.Err = fmt.Errorf("could not find func main. Did you forget to declare it? ") c.prog.Err = fmt.Errorf("could not find func main. Did you forget to declare it? ")
return []byte{}, c.prog.Err return c.prog.Err
} }
funUsage := analyzeFuncUsage(info.program.AllPackages) funUsage := analyzeFuncUsage(info.program.AllPackages)
@ -1297,9 +1287,29 @@ func CodeGen(info *buildInfo) ([]byte, error) {
} }
} }
if c.prog.Err != nil { return c.prog.Err
return nil, c.prog.Err }
func newCodegen(info *buildInfo, pkg *loader.PackageInfo) *codegen {
return &codegen{
buildInfo: info,
prog: io.NewBufBinWriter(),
l: []int{},
funcs: map[string]*funcScope{},
labels: map[labelWithType]uint16{},
typeInfo: &pkg.Info,
} }
}
// CodeGen compiles the program to bytecode.
func CodeGen(info *buildInfo) ([]byte, error) {
pkg := info.program.Package(info.initialPackage)
c := newCodegen(info, pkg)
if err := c.compile(info, pkg); err != nil {
return nil, err
}
buf := c.prog.Bytes() buf := c.prog.Bytes()
if err := c.writeJumps(buf); err != nil { if err := c.writeJumps(buf); err != nil {
return nil, err return nil, err

View file

@ -31,10 +31,9 @@ type buildInfo struct {
program *loader.Program program *loader.Program
} }
// Compile compiles a Go program into bytecode that can run on the NEO virtual machine. func getBuildInfo(src interface{}) (*buildInfo, error) {
func Compile(r io.Reader) ([]byte, error) {
conf := loader.Config{ParserMode: parser.ParseComments} conf := loader.Config{ParserMode: parser.ParseComments}
f, err := conf.ParseFile("", r) f, err := conf.ParseFile("", src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -45,9 +44,17 @@ func Compile(r io.Reader) ([]byte, error) {
return nil, err return nil, err
} }
ctx := &buildInfo{ return &buildInfo{
initialPackage: f.Name.Name, initialPackage: f.Name.Name,
program: prog, program: prog,
}, nil
}
// Compile compiles a Go program into bytecode that can run on the NEO virtual machine.
func Compile(r io.Reader) ([]byte, error) {
ctx, err := getBuildInfo(r)
if err != nil {
return nil, err
} }
buf, err := CodeGen(ctx) buf, err := CodeGen(ctx)

View file

@ -4,8 +4,66 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestCodeGen_DebugInfo(t *testing.T) {
src := `package foo
func Main(op string) bool {
res := methodInt(op)
_ = methodString()
_ = methodByteArray()
_ = methodArray()
_ = methodStruct()
return res == 42
}
func methodInt(a string) int {
if a == "get42" {
return 42
}
return 3
}
func methodString() string { return "" }
func methodByteArray() []byte { return nil }
func methodArray() []bool { return nil }
func methodStruct() struct{} { return struct{}{} }
`
info, err := getBuildInfo(src)
require.NoError(t, err)
pkg := info.program.Package(info.initialPackage)
c := newCodegen(info, pkg)
require.NoError(t, c.compile(info, pkg))
buf := c.prog.Bytes()
d := c.emitDebugInfo()
require.NotNil(t, d)
t.Run("return types", func(t *testing.T) {
returnTypes := map[string]string{
"methodInt": "Integer",
"methodString": "String", "methodByteArray": "ByteArray",
"methodArray": "Array", "methodStruct": "Struct",
"Main": "Boolean",
}
for i := range d.Methods {
name := d.Methods[i].Name.Name
assert.Equal(t, returnTypes[name], d.Methods[i].ReturnType)
}
})
// basic check that last instruction of every method is indeed RET
for i := range d.Methods {
index := d.Methods[i].Range.End
require.True(t, int(index) < len(buf))
require.EqualValues(t, opcode.RET, buf[index])
}
}
func TestDebugInfo_MarshalJSON(t *testing.T) { func TestDebugInfo_MarshalJSON(t *testing.T) {
d := &DebugInfo{ d := &DebugInfo{
EntryPoint: "main", EntryPoint: "main",