2018-02-15 15:35:49 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-19 09:24:28 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2018-02-15 15:35:49 +00:00
|
|
|
"go/ast"
|
2018-02-24 09:06:48 +00:00
|
|
|
"go/build"
|
2018-02-15 15:35:49 +00:00
|
|
|
"go/parser"
|
|
|
|
"go/types"
|
|
|
|
"io"
|
2018-02-19 09:24:28 +00:00
|
|
|
"io/ioutil"
|
2018-02-15 15:35:49 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2018-02-19 09:24:28 +00:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
2018-02-15 15:35:49 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
2018-02-25 12:26:56 +00:00
|
|
|
"golang.org/x/tools/go/loader"
|
2018-02-15 15:35:49 +00:00
|
|
|
)
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
const fileExt = "avm"
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// Options contains all the parameters that affect the behaviour of the compiler.
|
|
|
|
type Options struct {
|
|
|
|
// The extension of the output file default set to .avm
|
|
|
|
Ext string
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// The name of the output file.
|
|
|
|
Outfile string
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// Debug will output an hex encoded string of the generated bytecode.
|
|
|
|
Debug bool
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
type buildInfo struct {
|
|
|
|
initialPackage string
|
|
|
|
program *loader.Program
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// Compile compiles a Go program into bytecode that can run on the NEO virtual machine.
|
2018-02-25 12:26:56 +00:00
|
|
|
func Compile(r io.Reader, o *Options) ([]byte, error) {
|
|
|
|
conf := loader.Config{ParserMode: parser.ParseComments}
|
|
|
|
f, err := conf.ParseFile("", r)
|
2018-02-15 15:35:49 +00:00
|
|
|
if err != nil {
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil, err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-25 12:26:56 +00:00
|
|
|
conf.CreateFromFiles("", f)
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
prog, err := conf.Load()
|
2018-02-15 15:35:49 +00:00
|
|
|
if err != nil {
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil, err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
ctx := &buildInfo{
|
|
|
|
initialPackage: f.Name.Name,
|
|
|
|
program: prog,
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
buf, err := CodeGen(ctx)
|
2018-02-19 09:24:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
return buf.Bytes(), nil
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
type archive struct {
|
|
|
|
f *ast.File
|
|
|
|
typeInfo *types.Info
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// CompileAndSave will compile and save the file to disk.
|
|
|
|
func CompileAndSave(src string, o *Options) error {
|
2018-02-24 09:10:45 +00:00
|
|
|
if !strings.HasSuffix(src, ".go") {
|
|
|
|
return fmt.Errorf("%s is not a Go file", src)
|
|
|
|
}
|
2018-03-29 06:24:45 +00:00
|
|
|
o.Outfile = strings.TrimSuffix(o.Outfile, fmt.Sprintf(".%s", fileExt))
|
2018-02-19 09:24:28 +00:00
|
|
|
if len(o.Outfile) == 0 {
|
|
|
|
o.Outfile = strings.TrimSuffix(src, ".go")
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
if len(o.Ext) == 0 {
|
|
|
|
o.Ext = fileExt
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
b, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
b, err = Compile(bytes.NewReader(b), o)
|
|
|
|
if err != nil {
|
2018-02-24 09:10:45 +00:00
|
|
|
return fmt.Errorf("Error while trying to compile smart contract file: %v", err)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
if o.Debug {
|
|
|
|
log.Println(hex.EncodeToString(b))
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
|
|
|
|
return ioutil.WriteFile(out, b, os.ModePerm)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// DumpOpcode compiles the program and dumps the opcode in a user friendly format.
|
|
|
|
func DumpOpcode(src string) error {
|
|
|
|
b, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
b, err = Compile(bytes.NewReader(b), &Options{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
|
|
|
|
fmt.Fprintln(w, "INDEX\tOPCODE\tDESC\t")
|
|
|
|
for i := 0; i < len(b); i++ {
|
2019-08-14 12:40:31 +00:00
|
|
|
fmt.Fprintf(w, "%d\t0x%2x\t%s\t\n", i, b[i], vm.Instruction(b[i]))
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
w.Flush()
|
|
|
|
return nil
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
func gopath() string {
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if len(gopath) == 0 {
|
|
|
|
gopath = build.Default.GOPATH
|
|
|
|
}
|
|
|
|
return gopath
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(0)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|