2018-02-19 09:24:28 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2020-01-27 07:59:57 +00:00
|
|
|
"errors"
|
2019-11-22 14:16:52 +00:00
|
|
|
"fmt"
|
2018-02-19 09:24:28 +00:00
|
|
|
"go/ast"
|
|
|
|
"go/constant"
|
|
|
|
"go/token"
|
|
|
|
"go/types"
|
2019-02-15 13:38:16 +00:00
|
|
|
"sort"
|
2018-10-23 08:23:03 +00:00
|
|
|
"strconv"
|
2018-07-02 13:02:00 +00:00
|
|
|
"strings"
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2019-12-25 12:22:02 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/encoding/address"
|
2019-11-22 10:06:32 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-12-03 14:05:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
|
|
|
|
2018-07-02 13:02:00 +00:00
|
|
|
// The identifier of the entry function. Default set to Main.
|
2018-02-19 09:24:28 +00:00
|
|
|
const mainIdent = "Main"
|
|
|
|
|
|
|
|
type codegen struct {
|
2018-02-25 12:26:56 +00:00
|
|
|
// Information about the program with all its dependencies.
|
|
|
|
buildInfo *buildInfo
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// prog holds the output buffer.
|
2019-11-22 10:06:32 +00:00
|
|
|
prog *io.BufBinWriter
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Type information.
|
2018-02-19 09:24:28 +00:00
|
|
|
typeInfo *types.Info
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// A mapping of func identifiers with their scope.
|
2018-02-19 09:24:28 +00:00
|
|
|
funcs map[string]*funcScope
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// Current funcScope being converted.
|
|
|
|
scope *funcScope
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
// Label table for recording jump destinations.
|
|
|
|
l []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLabel creates a new label to jump to
|
|
|
|
func (c *codegen) newLabel() (l int) {
|
|
|
|
l = len(c.l)
|
|
|
|
c.l = append(c.l, -1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) setLabel(l int) {
|
|
|
|
c.l[l] = c.pc() + 1
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// pc returns the program offset off the last instruction.
|
2018-02-19 09:24:28 +00:00
|
|
|
func (c *codegen) pc() int {
|
|
|
|
return c.prog.Len() - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) emitLoadConst(t types.TypeAndValue) {
|
2019-11-22 10:06:32 +00:00
|
|
|
if c.prog.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
switch typ := t.Type.Underlying().(type) {
|
2018-02-19 09:24:28 +00:00
|
|
|
case *types.Basic:
|
2020-01-23 14:28:35 +00:00
|
|
|
c.convertBasicType(t, typ)
|
2018-02-19 09:24:28 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("compiler doesn't know how to convert this constant: %v", t)
|
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:28:35 +00:00
|
|
|
func (c *codegen) convertBasicType(t types.TypeAndValue, typ *types.Basic) {
|
|
|
|
switch typ.Kind() {
|
|
|
|
case types.Int, types.UntypedInt, types.Uint:
|
|
|
|
val, _ := constant.Int64Val(t.Value)
|
|
|
|
emitInt(c.prog.BinWriter, val)
|
|
|
|
case types.String, types.UntypedString:
|
|
|
|
val := constant.StringVal(t.Value)
|
|
|
|
emitString(c.prog.BinWriter, val)
|
|
|
|
case types.Bool, types.UntypedBool:
|
|
|
|
val := constant.BoolVal(t.Value)
|
|
|
|
emitBool(c.prog.BinWriter, val)
|
|
|
|
case types.Byte:
|
|
|
|
val, _ := constant.Int64Val(t.Value)
|
|
|
|
b := byte(val)
|
|
|
|
emitBytes(c.prog.BinWriter, []byte{b})
|
|
|
|
default:
|
|
|
|
c.prog.Err = fmt.Errorf("compiler doesn't know how to convert this basic type: %v", t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
func (c *codegen) emitLoadLocal(name string) {
|
2018-02-25 12:26:56 +00:00
|
|
|
pos := c.scope.loadLocal(name)
|
2018-02-19 09:24:28 +00:00
|
|
|
if pos < 0 {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("cannot load local variable with position: %d", pos)
|
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
c.emitLoadLocalPos(pos)
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
func (c *codegen) emitLoadLocalPos(pos int) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DUPFROMALTSTACK)
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(pos))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.PICKITEM)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) emitStoreLocal(pos int) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DUPFROMALTSTACK)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
if pos < 0 {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("invalid position to store local: %d", pos)
|
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(pos))
|
|
|
|
emitInt(c.prog.BinWriter, 2)
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ROLL)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SETITEM)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
func (c *codegen) emitLoadField(i int) {
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(i))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.PICKITEM)
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) emitStoreStructField(i int) {
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(i))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ROT)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SETITEM)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// convertGlobals traverses the AST and only converts global declarations.
|
2018-02-25 12:26:56 +00:00
|
|
|
// If we call this in convertFuncDecl then it will load all global variables
|
|
|
|
// into the scope of the function.
|
2019-02-09 15:53:58 +00:00
|
|
|
func (c *codegen) convertGlobals(f ast.Node) {
|
2018-02-25 12:26:56 +00:00
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
return false
|
|
|
|
case *ast.GenDecl:
|
|
|
|
ast.Walk(c, n)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
|
2018-02-19 09:24:28 +00:00
|
|
|
var (
|
|
|
|
f *funcScope
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
f, ok = c.funcs[decl.Name.Name]
|
|
|
|
if ok {
|
2018-10-17 17:19:01 +00:00
|
|
|
// If this function is a syscall we will not convert it to bytecode.
|
|
|
|
if isSyscall(f) {
|
|
|
|
return
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
c.setLabel(f.label)
|
|
|
|
} else {
|
|
|
|
f = c.newFunc(decl)
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
c.scope = f
|
2018-04-10 09:45:31 +00:00
|
|
|
ast.Inspect(decl, c.scope.analyzeVoidCalls) // @OPTIMIZE
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// All globals copied into the scope of the function need to be added
|
|
|
|
// to the stack size of the function.
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, f.stackSize()+countGlobals(file))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NEWARRAY)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.TOALTSTACK)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
// We need to handle methods, which in Go, is just syntactic sugar.
|
|
|
|
// The method receiver will be passed in as first argument.
|
|
|
|
// We check if this declaration has a receiver and load it into scope.
|
|
|
|
//
|
2019-02-13 18:01:10 +00:00
|
|
|
// FIXME: For now we will hard cast this to a struct. We can later fine tune this
|
2018-02-19 09:24:28 +00:00
|
|
|
// to support other types.
|
|
|
|
if decl.Recv != nil {
|
|
|
|
for _, arg := range decl.Recv.List {
|
|
|
|
ident := arg.Names[0]
|
2018-02-27 09:04:24 +00:00
|
|
|
// Currently only method receives for struct types is supported.
|
|
|
|
_, ok := c.typeInfo.Defs[ident].Type().Underlying().(*types.Struct)
|
2018-02-24 09:06:48 +00:00
|
|
|
if !ok {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("method receives for non-struct types is not yet supported")
|
|
|
|
return
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
2018-02-25 12:26:56 +00:00
|
|
|
l := c.scope.newLocal(ident.Name)
|
2018-02-19 09:24:28 +00:00
|
|
|
c.emitStoreLocal(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the arguments in scope.
|
|
|
|
for _, arg := range decl.Type.Params.List {
|
|
|
|
name := arg.Names[0].Name // for now.
|
2018-02-25 12:26:56 +00:00
|
|
|
l := c.scope.newLocal(name)
|
2018-02-19 09:24:28 +00:00
|
|
|
c.emitStoreLocal(l)
|
|
|
|
}
|
2018-04-10 09:45:31 +00:00
|
|
|
// Load in all the global variables in to the scope of the function.
|
|
|
|
// This is not necessary for syscalls.
|
2018-08-22 07:51:35 +00:00
|
|
|
if !isSyscall(f) {
|
2018-02-25 12:26:56 +00:00
|
|
|
c.convertGlobals(file)
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ast.Walk(c, decl.Body)
|
|
|
|
|
2018-07-02 13:02:00 +00:00
|
|
|
// If this function returns the void (no return stmt) we will cleanup its junk on the stack.
|
2018-04-10 09:45:31 +00:00
|
|
|
if !hasReturnStmt(decl) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.FROMALTSTACK)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DROP)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.RET)
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
2019-11-22 10:06:32 +00:00
|
|
|
if c.prog.Err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
switch n := node.(type) {
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
// General declarations.
|
2018-02-25 12:26:56 +00:00
|
|
|
// var (
|
|
|
|
// x = 2
|
|
|
|
// )
|
2018-02-24 09:06:48 +00:00
|
|
|
case *ast.GenDecl:
|
2018-02-25 12:26:56 +00:00
|
|
|
for _, spec := range n.Specs {
|
|
|
|
switch t := spec.(type) {
|
|
|
|
case *ast.ValueSpec:
|
|
|
|
for i, val := range t.Values {
|
|
|
|
ast.Walk(c, val)
|
|
|
|
l := c.scope.newLocal(t.Names[i].Name)
|
|
|
|
c.emitStoreLocal(l)
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
case *ast.AssignStmt:
|
2019-12-24 13:46:43 +00:00
|
|
|
multiRet := len(n.Rhs) != len(n.Lhs)
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
for i := 0; i < len(n.Lhs); i++ {
|
|
|
|
switch t := n.Lhs[i].(type) {
|
|
|
|
case *ast.Ident:
|
2018-02-24 09:06:48 +00:00
|
|
|
switch n.Tok {
|
2019-12-19 10:11:05 +00:00
|
|
|
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN:
|
2018-02-24 09:06:48 +00:00
|
|
|
c.emitLoadLocal(t.Name)
|
2018-02-27 09:04:24 +00:00
|
|
|
ast.Walk(c, n.Rhs[0]) // can only add assign to 1 expr on the RHS
|
2018-02-24 09:06:48 +00:00
|
|
|
c.convertToken(n.Tok)
|
2018-02-25 12:26:56 +00:00
|
|
|
l := c.scope.loadLocal(t.Name)
|
2018-02-24 09:06:48 +00:00
|
|
|
c.emitStoreLocal(l)
|
|
|
|
default:
|
2019-12-24 13:46:43 +00:00
|
|
|
if i == 0 || !multiRet {
|
|
|
|
ast.Walk(c, n.Rhs[i])
|
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
l := c.scope.loadLocal(t.Name)
|
2018-02-24 09:06:48 +00:00
|
|
|
c.emitStoreLocal(l)
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
case *ast.SelectorExpr:
|
2018-02-24 09:06:48 +00:00
|
|
|
switch expr := t.X.(type) {
|
2018-02-19 09:24:28 +00:00
|
|
|
case *ast.Ident:
|
2018-02-24 09:06:48 +00:00
|
|
|
ast.Walk(c, n.Rhs[i])
|
2018-02-27 09:04:24 +00:00
|
|
|
typ := c.typeInfo.ObjectOf(expr).Type().Underlying()
|
|
|
|
if strct, ok := typ.(*types.Struct); ok {
|
|
|
|
c.emitLoadLocal(expr.Name) // load the struct
|
|
|
|
i := indexOfStruct(strct, t.Sel.Name) // get the index of the field
|
|
|
|
c.emitStoreStructField(i) // store the field
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("nested selector assigns not supported yet")
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-10-23 08:23:03 +00:00
|
|
|
|
|
|
|
// Assignments to index expressions.
|
|
|
|
// slice[0] = 10
|
|
|
|
case *ast.IndexExpr:
|
|
|
|
ast.Walk(c, n.Rhs[i])
|
|
|
|
name := t.X.(*ast.Ident).Name
|
|
|
|
c.emitLoadLocal(name)
|
2019-12-19 07:48:06 +00:00
|
|
|
switch ind := t.Index.(type) {
|
|
|
|
case *ast.BasicLit:
|
|
|
|
indexStr := ind.Value
|
|
|
|
index, err := strconv.Atoi(indexStr)
|
|
|
|
if err != nil {
|
|
|
|
c.prog.Err = fmt.Errorf("failed to convert slice index to integer")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.emitStoreStructField(index)
|
|
|
|
case *ast.Ident:
|
|
|
|
c.emitLoadLocal(ind.Name)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ROT)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SETITEM)
|
|
|
|
default:
|
|
|
|
c.prog.Err = fmt.Errorf("unsupported index expression")
|
2019-11-22 14:16:52 +00:00
|
|
|
return nil
|
2018-10-23 08:23:03 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.ReturnStmt:
|
2018-04-02 15:04:42 +00:00
|
|
|
l := c.newLabel()
|
|
|
|
c.setLabel(l)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2019-12-24 13:46:43 +00:00
|
|
|
// first result should be on top of the stack
|
|
|
|
for i := len(n.Results) - 1; i >= 0; i-- {
|
|
|
|
ast.Walk(c, n.Results[i])
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.FROMALTSTACK)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DROP) // Cleanup the stack.
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.RET)
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.IfStmt:
|
|
|
|
lIf := c.newLabel()
|
|
|
|
lElse := c.newLabel()
|
2018-10-20 05:11:00 +00:00
|
|
|
lElseEnd := c.newLabel()
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
if n.Cond != nil {
|
|
|
|
ast.Walk(c, n.Cond)
|
2019-12-03 14:05:06 +00:00
|
|
|
emitJmp(c.prog.BinWriter, opcode.JMPIFNOT, int16(lElse))
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.setLabel(lIf)
|
|
|
|
ast.Walk(c, n.Body)
|
|
|
|
if n.Else != nil {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitJmp(c.prog.BinWriter, opcode.JMP, int16(lElseEnd))
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-10-20 05:11:00 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
c.setLabel(lElse)
|
|
|
|
if n.Else != nil {
|
|
|
|
ast.Walk(c, n.Else)
|
|
|
|
}
|
2018-10-20 05:11:00 +00:00
|
|
|
c.setLabel(lElseEnd)
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.BasicLit:
|
2018-02-25 12:26:56 +00:00
|
|
|
c.emitLoadConst(c.typeInfo.Types[n])
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.Ident:
|
|
|
|
if isIdentBool(n) {
|
2019-11-22 14:16:52 +00:00
|
|
|
value, err := makeBoolFromIdent(n, c.typeInfo)
|
|
|
|
if err != nil {
|
|
|
|
c.prog.Err = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.emitLoadConst(value)
|
2018-02-19 09:24:28 +00:00
|
|
|
} else {
|
|
|
|
c.emitLoadLocal(n.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.CompositeLit:
|
2018-02-24 09:06:48 +00:00
|
|
|
var typ types.Type
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
switch t := n.Type.(type) {
|
|
|
|
case *ast.Ident:
|
2018-02-24 09:06:48 +00:00
|
|
|
typ = c.typeInfo.ObjectOf(t).Type().Underlying()
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
typ = c.typeInfo.ObjectOf(t.Sel).Type().Underlying()
|
2020-01-23 14:06:15 +00:00
|
|
|
case *ast.MapType:
|
|
|
|
typ = c.typeInfo.TypeOf(t)
|
2018-02-19 09:24:28 +00:00
|
|
|
default:
|
|
|
|
ln := len(n.Elts)
|
2019-10-22 14:56:03 +00:00
|
|
|
// ByteArrays needs a different approach than normal arrays.
|
2018-02-27 09:04:24 +00:00
|
|
|
if isByteArray(n, c.typeInfo) {
|
|
|
|
c.convertByteArray(n)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
for i := ln - 1; i >= 0; i-- {
|
2020-01-23 12:42:50 +00:00
|
|
|
ast.Walk(c, n.Elts[i])
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(ln))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.PACK)
|
2018-02-24 09:06:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
c.convertStruct(n)
|
2020-01-23 14:06:15 +00:00
|
|
|
case *types.Map:
|
|
|
|
c.convertMap(n)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.BinaryExpr:
|
|
|
|
switch n.Op {
|
|
|
|
case token.LAND:
|
|
|
|
ast.Walk(c, n.X)
|
2019-12-03 14:05:06 +00:00
|
|
|
emitJmp(c.prog.BinWriter, opcode.JMPIFNOT, int16(len(c.l)-1))
|
2018-02-19 09:24:28 +00:00
|
|
|
ast.Walk(c, n.Y)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case token.LOR:
|
|
|
|
ast.Walk(c, n.X)
|
2019-12-03 14:05:06 +00:00
|
|
|
emitJmp(c.prog.BinWriter, opcode.JMPIF, int16(len(c.l)-3))
|
2018-02-19 09:24:28 +00:00
|
|
|
ast.Walk(c, n.Y)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
// The AST package will try to resolve all basic literals for us.
|
|
|
|
// If the typeinfo.Value is not nil we know that the expr is resolved
|
|
|
|
// and needs no further action. e.g. x := 2 + 2 + 2 will be resolved to 6.
|
2019-02-13 18:01:10 +00:00
|
|
|
// NOTE: Constants will also be automatically resolved be the AST parser.
|
2018-02-25 12:26:56 +00:00
|
|
|
// example:
|
|
|
|
// const x = 10
|
|
|
|
// x + 2 will results into 12
|
2018-10-29 15:36:13 +00:00
|
|
|
tinfo := c.typeInfo.Types[n]
|
|
|
|
if tinfo.Value != nil {
|
2018-02-19 09:24:28 +00:00
|
|
|
c.emitLoadConst(tinfo)
|
2018-02-25 12:26:56 +00:00
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
ast.Walk(c, n.Y)
|
2018-10-29 15:36:13 +00:00
|
|
|
|
2019-08-16 10:05:07 +00:00
|
|
|
switch {
|
|
|
|
case n.Op == token.ADD:
|
|
|
|
// VM has separate opcodes for number and string concatenation
|
|
|
|
if isStringType(tinfo.Type) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.CAT)
|
2018-10-29 15:36:13 +00:00
|
|
|
} else {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ADD)
|
2018-10-29 15:36:13 +00:00
|
|
|
}
|
2019-08-16 10:05:07 +00:00
|
|
|
case n.Op == token.EQL:
|
|
|
|
// VM has separate opcodes for number and string equality
|
2019-08-19 16:18:17 +00:00
|
|
|
if isStringType(c.typeInfo.Types[n.X].Type) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.EQUAL)
|
2019-08-16 10:05:07 +00:00
|
|
|
} else {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NUMEQUAL)
|
2019-08-16 10:05:07 +00:00
|
|
|
}
|
2019-08-19 16:18:17 +00:00
|
|
|
case n.Op == token.NEQ:
|
|
|
|
// VM has separate opcodes for number and string equality
|
|
|
|
if isStringType(c.typeInfo.Types[n.X].Type) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.EQUAL)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NOT)
|
2019-08-19 16:18:17 +00:00
|
|
|
} else {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NUMNOTEQUAL)
|
2019-08-19 16:18:17 +00:00
|
|
|
}
|
2019-08-16 10:05:07 +00:00
|
|
|
default:
|
2018-10-29 15:36:13 +00:00
|
|
|
c.convertToken(n.Op)
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case *ast.CallExpr:
|
|
|
|
var (
|
2018-04-04 19:41:19 +00:00
|
|
|
f *funcScope
|
|
|
|
ok bool
|
|
|
|
numArgs = len(n.Args)
|
|
|
|
isBuiltin = isBuiltin(n.Fun)
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
switch fun := n.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
f, ok = c.funcs[fun.Name]
|
2018-04-04 19:41:19 +00:00
|
|
|
if !ok && !isBuiltin {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("could not resolve function %s", fun.Name)
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
case *ast.SelectorExpr:
|
2018-02-24 09:06:48 +00:00
|
|
|
// If this is a method call we need to walk the AST to load the struct locally.
|
|
|
|
// Otherwise this is a function call from a imported package and we can call it
|
|
|
|
// directly.
|
|
|
|
if c.typeInfo.Selections[fun] != nil {
|
|
|
|
ast.Walk(c, fun.X)
|
|
|
|
// Dont forget to add 1 extra argument when its a method.
|
|
|
|
numArgs++
|
|
|
|
}
|
2018-08-22 07:51:35 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
f, ok = c.funcs[fun.Sel.Name]
|
2018-08-22 07:51:35 +00:00
|
|
|
// @FIXME this could cause runtime errors.
|
|
|
|
f.selector = fun.X.(*ast.Ident)
|
2018-02-19 09:24:28 +00:00
|
|
|
if !ok {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("could not resolve function %s", fun.Sel.Name)
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-05-06 06:03:26 +00:00
|
|
|
case *ast.ArrayType:
|
|
|
|
// For now we will assume that there is only 1 argument passed which
|
|
|
|
// will be a basic literal (string kind). This only to handle string
|
|
|
|
// to byte slice conversions. E.G. []byte("foobar")
|
|
|
|
arg := n.Args[0].(*ast.BasicLit)
|
|
|
|
c.emitLoadConst(c.typeInfo.Types[arg])
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 07:59:57 +00:00
|
|
|
args := n.Args
|
|
|
|
isAppCall := isAppCall(n.Fun)
|
2020-01-27 08:53:47 +00:00
|
|
|
isFromAddress := isFromAddress(n.Fun)
|
|
|
|
// There are 2 special cases:
|
|
|
|
// 1. When using APPCALL, script hash is a part of the instruction so
|
|
|
|
// script hash should be emitted after APPCALL.
|
|
|
|
// 2. With FromAddress, parameter conversion is happening at compile-time
|
|
|
|
// so there is no need to push parameters on stack and perform an actual call
|
|
|
|
if isAppCall || isFromAddress {
|
2020-01-27 07:59:57 +00:00
|
|
|
args = n.Args[1:]
|
|
|
|
}
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
// Handle the arguments
|
2020-01-27 07:59:57 +00:00
|
|
|
for _, arg := range args {
|
2018-02-19 09:24:28 +00:00
|
|
|
ast.Walk(c, arg)
|
|
|
|
}
|
2018-04-04 19:41:19 +00:00
|
|
|
// Do not swap for builtin functions.
|
|
|
|
if !isBuiltin {
|
|
|
|
if numArgs == 2 {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SWAP)
|
2018-10-26 19:16:06 +00:00
|
|
|
} else if numArgs == 3 {
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, 2)
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.XSWAP)
|
2018-10-26 19:16:06 +00:00
|
|
|
} else {
|
2018-10-29 15:37:08 +00:00
|
|
|
for i := 1; i < numArgs; i++ {
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(i))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ROLL)
|
2018-10-29 15:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
// Check builtin first to avoid nil pointer on funcScope!
|
2019-02-09 15:53:58 +00:00
|
|
|
switch {
|
|
|
|
case isBuiltin:
|
2018-03-25 16:21:00 +00:00
|
|
|
// Use the ident to check, builtins are not in func scopes.
|
|
|
|
// We can be sure builtins are of type *ast.Ident.
|
2018-04-22 18:11:37 +00:00
|
|
|
c.convertBuiltin(n)
|
2020-01-27 07:59:57 +00:00
|
|
|
|
|
|
|
if isAppCall {
|
|
|
|
buf := c.getByteArray(n.Args[0])
|
|
|
|
if len(buf) != 20 {
|
|
|
|
c.prog.Err = errors.New("invalid script hash")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.prog.WriteBytes(buf)
|
|
|
|
}
|
2018-08-22 07:51:35 +00:00
|
|
|
case isSyscall(f):
|
|
|
|
c.convertSyscall(f.selector.Name, f.name)
|
2019-02-09 15:53:58 +00:00
|
|
|
default:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitCall(c.prog.BinWriter, opcode.CALL, int16(f.label))
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
switch t := n.X.(type) {
|
|
|
|
case *ast.Ident:
|
2018-02-25 12:26:56 +00:00
|
|
|
typ := c.typeInfo.ObjectOf(t).Type().Underlying()
|
2018-02-27 09:04:24 +00:00
|
|
|
if strct, ok := typ.(*types.Struct); ok {
|
|
|
|
c.emitLoadLocal(t.Name) // load the struct
|
|
|
|
i := indexOfStruct(strct, n.Sel.Name)
|
2018-03-25 16:21:00 +00:00
|
|
|
c.emitLoadField(i) // load the field
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("nested selectors not supported yet")
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-02-27 09:04:24 +00:00
|
|
|
|
|
|
|
case *ast.UnaryExpr:
|
2018-04-04 19:41:19 +00:00
|
|
|
ast.Walk(c, n.X)
|
2019-02-19 13:50:34 +00:00
|
|
|
// From https://golang.org/ref/spec#Operators
|
|
|
|
// there can be only following unary operators
|
|
|
|
// "+" | "-" | "!" | "^" | "*" | "&" | "<-" .
|
|
|
|
// of which last three are not used in SC
|
|
|
|
switch n.Op {
|
|
|
|
case token.ADD:
|
|
|
|
// +10 == 10, no need to do anything in this case
|
|
|
|
case token.SUB:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NEGATE)
|
2019-02-19 13:50:34 +00:00
|
|
|
case token.NOT:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NOT)
|
2019-02-19 13:50:34 +00:00
|
|
|
case token.XOR:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.INVERT)
|
2019-02-19 13:50:34 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("invalid unary operator: %s", n.Op)
|
|
|
|
return nil
|
2019-02-19 13:50:34 +00:00
|
|
|
}
|
2018-04-04 19:41:19 +00:00
|
|
|
return nil
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
case *ast.IncDecStmt:
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
c.convertToken(n.Tok)
|
|
|
|
|
|
|
|
// For now only identifiers are supported for (post) for stmts.
|
|
|
|
// for i := 0; i < 10; i++ {}
|
|
|
|
// Where the post stmt is ( i++ )
|
|
|
|
if ident, ok := n.X.(*ast.Ident); ok {
|
|
|
|
pos := c.scope.loadLocal(ident.Name)
|
|
|
|
c.emitStoreLocal(pos)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
case *ast.IndexExpr:
|
|
|
|
// Walk the expression, this could be either an Ident or SelectorExpr.
|
|
|
|
// This will load local whatever X is.
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
|
|
|
|
switch n.Index.(type) {
|
|
|
|
case *ast.BasicLit:
|
|
|
|
t := c.typeInfo.Types[n.Index]
|
2020-01-23 14:28:35 +00:00
|
|
|
switch typ := t.Type.Underlying().(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
c.convertBasicType(t, typ)
|
|
|
|
default:
|
|
|
|
c.prog.Err = fmt.Errorf("compiler can't use following type as an index: %T", typ)
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
default:
|
|
|
|
ast.Walk(c, n.Index)
|
|
|
|
}
|
2020-01-23 14:28:35 +00:00
|
|
|
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.PICKITEM) // just pickitem here
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
return nil
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
case *ast.ForStmt:
|
|
|
|
var (
|
|
|
|
fstart = c.newLabel()
|
|
|
|
fend = c.newLabel()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Walk the initializer and condition.
|
2020-01-23 08:48:41 +00:00
|
|
|
if n.Init != nil {
|
|
|
|
ast.Walk(c, n.Init)
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
// Set label and walk the condition.
|
|
|
|
c.setLabel(fstart)
|
|
|
|
ast.Walk(c, n.Cond)
|
|
|
|
|
|
|
|
// Jump if the condition is false
|
2019-12-03 14:05:06 +00:00
|
|
|
emitJmp(c.prog.BinWriter, opcode.JMPIFNOT, int16(fend))
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
// Walk body followed by the iterator (post stmt).
|
|
|
|
ast.Walk(c, n.Body)
|
2020-01-23 08:48:41 +00:00
|
|
|
if n.Post != nil {
|
|
|
|
ast.Walk(c, n.Post)
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
// Jump back to condition.
|
2019-12-03 14:05:06 +00:00
|
|
|
emitJmp(c.prog.BinWriter, opcode.JMP, int16(fstart))
|
2018-04-02 15:04:42 +00:00
|
|
|
c.setLabel(fend)
|
|
|
|
|
|
|
|
return nil
|
2018-04-10 09:45:31 +00:00
|
|
|
|
|
|
|
// We dont really care about assertions for the core logic.
|
|
|
|
// The only thing we need is to please the compiler type checking.
|
|
|
|
// For this to work properly, we only need to walk the expression
|
|
|
|
// not the assertion type.
|
|
|
|
case *ast.TypeAssertExpr:
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-27 07:59:57 +00:00
|
|
|
// getByteArray returns byte array value from constant expr.
|
|
|
|
// Only literals are supported.
|
|
|
|
func (c *codegen) getByteArray(expr ast.Expr) []byte {
|
|
|
|
switch t := expr.(type) {
|
|
|
|
case *ast.CompositeLit:
|
|
|
|
if !isByteArray(t, c.typeInfo) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(t.Elts))
|
|
|
|
for i := 0; i < len(t.Elts); i++ {
|
|
|
|
t := c.typeInfo.Types[t.Elts[i]]
|
|
|
|
val, _ := constant.Int64Val(t.Value)
|
|
|
|
buf[i] = byte(val)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:51:35 +00:00
|
|
|
func (c *codegen) convertSyscall(api, name string) {
|
|
|
|
api, ok := syscalls[api][name]
|
2018-02-24 09:06:48 +00:00
|
|
|
if !ok {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("unknown VM syscall api: %s", name)
|
|
|
|
return
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
2019-12-02 10:20:16 +00:00
|
|
|
emitSyscall(c.prog.BinWriter, api)
|
2018-10-17 17:19:01 +00:00
|
|
|
|
|
|
|
// This NOP instruction is basically not needed, but if we do, we have a
|
|
|
|
// one to one matching avm file with neo-python which is very nice for debugging.
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NOP)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 18:11:37 +00:00
|
|
|
func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
|
|
|
|
var name string
|
|
|
|
switch t := expr.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
name = t.Name
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
name = t.Sel.Name
|
|
|
|
}
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
switch name {
|
|
|
|
case "len":
|
2018-04-04 19:41:19 +00:00
|
|
|
arg := expr.Args[0]
|
|
|
|
typ := c.typeInfo.Types[arg].Type
|
|
|
|
if isStringType(typ) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SIZE)
|
2018-04-04 19:41:19 +00:00
|
|
|
} else {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ARRAYSIZE)
|
2018-04-04 19:41:19 +00:00
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
case "append":
|
2019-09-12 14:39:23 +00:00
|
|
|
arg := expr.Args[0]
|
|
|
|
typ := c.typeInfo.Types[arg].Type
|
|
|
|
if isByteArrayType(typ) {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.CAT)
|
2019-09-12 14:39:23 +00:00
|
|
|
} else {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SWAP)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DUP)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.PUSH2)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.XSWAP)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.APPEND)
|
2019-09-12 14:39:23 +00:00
|
|
|
}
|
2018-04-22 18:11:37 +00:00
|
|
|
case "SHA256":
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SHA256)
|
2018-04-22 18:11:37 +00:00
|
|
|
case "SHA1":
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SHA1)
|
2018-04-22 18:11:37 +00:00
|
|
|
case "Hash256":
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.HASH256)
|
2018-04-22 18:11:37 +00:00
|
|
|
case "Hash160":
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.HASH160)
|
2020-01-22 15:24:58 +00:00
|
|
|
case "VerifySignature":
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.VERIFY)
|
2020-01-27 07:59:57 +00:00
|
|
|
case "AppCall":
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.APPCALL)
|
2018-08-23 17:44:17 +00:00
|
|
|
case "Equals":
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.EQUAL)
|
2018-07-02 13:02:00 +00:00
|
|
|
case "FromAddress":
|
|
|
|
// We can be sure that this is a ast.BasicLit just containing a simple
|
2019-02-13 18:01:10 +00:00
|
|
|
// address string. Note that the string returned from calling Value will
|
|
|
|
// contain double quotes that need to be stripped.
|
2018-07-02 13:02:00 +00:00
|
|
|
addressStr := expr.Args[0].(*ast.BasicLit).Value
|
|
|
|
addressStr = strings.Replace(addressStr, "\"", "", 2)
|
2019-12-25 14:34:18 +00:00
|
|
|
uint160, err := address.StringToUint160(addressStr)
|
2018-07-02 13:02:00 +00:00
|
|
|
if err != nil {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = err
|
|
|
|
return
|
2018-07-02 13:02:00 +00:00
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
bytes := uint160.BytesBE()
|
2019-12-02 10:20:16 +00:00
|
|
|
emitBytes(c.prog.BinWriter, bytes)
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 09:04:24 +00:00
|
|
|
func (c *codegen) convertByteArray(lit *ast.CompositeLit) {
|
|
|
|
buf := make([]byte, len(lit.Elts))
|
|
|
|
for i := 0; i < len(lit.Elts); i++ {
|
|
|
|
t := c.typeInfo.Types[lit.Elts[i]]
|
|
|
|
val, _ := constant.Int64Val(t.Value)
|
|
|
|
buf[i] = byte(val)
|
|
|
|
}
|
2019-12-02 10:20:16 +00:00
|
|
|
emitBytes(c.prog.BinWriter, buf)
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:06:15 +00:00
|
|
|
func (c *codegen) convertMap(lit *ast.CompositeLit) {
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NEWMAP)
|
|
|
|
for i := range lit.Elts {
|
|
|
|
elem := lit.Elts[i].(*ast.KeyValueExpr)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DUP)
|
|
|
|
ast.Walk(c, elem.Key)
|
|
|
|
ast.Walk(c, elem.Value)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SETITEM)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
func (c *codegen) convertStruct(lit *ast.CompositeLit) {
|
2018-02-24 09:06:48 +00:00
|
|
|
// Create a new structScope to initialize and store
|
|
|
|
// the positions of its variables.
|
2018-02-27 09:04:24 +00:00
|
|
|
strct, ok := c.typeInfo.TypeOf(lit).Underlying().(*types.Struct)
|
2018-02-24 09:06:48 +00:00
|
|
|
if !ok {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("the given literal is not of type struct: %v", lit)
|
|
|
|
return
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NOP)
|
2019-12-02 10:20:16 +00:00
|
|
|
emitInt(c.prog.BinWriter, int64(strct.NumFields()))
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NEWSTRUCT)
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.TOALTSTACK)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
// We need to locally store all the fields, even if they are not initialized.
|
|
|
|
// We will initialize all fields to their "zero" value.
|
2018-02-27 09:04:24 +00:00
|
|
|
for i := 0; i < strct.NumFields(); i++ {
|
|
|
|
sField := strct.Field(i)
|
2018-02-24 09:06:48 +00:00
|
|
|
fieldAdded := false
|
|
|
|
|
|
|
|
// Fields initialized by the program.
|
|
|
|
for _, field := range lit.Elts {
|
|
|
|
f := field.(*ast.KeyValueExpr)
|
|
|
|
fieldName := f.Key.(*ast.Ident).Name
|
|
|
|
|
|
|
|
if sField.Name() == fieldName {
|
|
|
|
ast.Walk(c, f.Value)
|
2018-02-27 09:04:24 +00:00
|
|
|
pos := indexOfStruct(strct, fieldName)
|
2018-02-24 09:06:48 +00:00
|
|
|
c.emitStoreLocal(pos)
|
|
|
|
fieldAdded = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fieldAdded {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
|
2019-11-22 14:16:52 +00:00
|
|
|
typeAndVal, err := typeAndValueForField(sField)
|
|
|
|
if err != nil {
|
|
|
|
c.prog.Err = err
|
|
|
|
return
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
c.emitLoadConst(typeAndVal)
|
2018-02-24 09:06:48 +00:00
|
|
|
c.emitStoreLocal(i)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.FROMALTSTACK)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
func (c *codegen) convertToken(tok token.Token) {
|
|
|
|
switch tok {
|
|
|
|
case token.ADD_ASSIGN:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ADD)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.SUB_ASSIGN:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SUB)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.MUL_ASSIGN:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.MUL)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.QUO_ASSIGN:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DIV)
|
2019-12-19 10:11:05 +00:00
|
|
|
case token.REM_ASSIGN:
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.MOD)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.ADD:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.ADD)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.SUB:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SUB)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.MUL:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.MUL)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.QUO:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DIV)
|
2019-12-19 10:11:05 +00:00
|
|
|
case token.REM:
|
|
|
|
emitOpcode(c.prog.BinWriter, opcode.MOD)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.LSS:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.LT)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.LEQ:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.LTE)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.GTR:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.GT)
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.GEQ:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.GTE)
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.EQL:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NUMEQUAL)
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.NEQ:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NUMNOTEQUAL)
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.DEC:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.DEC)
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.INC:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.INC)
|
2018-04-04 19:41:19 +00:00
|
|
|
case token.NOT:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.NOT)
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.AND:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.AND)
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.OR:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.OR)
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.SHL:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SHL)
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.SHR:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.SHR)
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.XOR:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(c.prog.BinWriter, opcode.XOR)
|
2018-02-24 09:06:48 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("compiler could not convert token: %s", tok)
|
|
|
|
return
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
func (c *codegen) newFunc(decl *ast.FuncDecl) *funcScope {
|
|
|
|
f := newFuncScope(decl, c.newLabel())
|
|
|
|
c.funcs[f.name] = f
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// CodeGen compiles the program to bytecode.
|
2019-11-22 10:06:32 +00:00
|
|
|
func CodeGen(info *buildInfo) ([]byte, error) {
|
2018-02-25 12:26:56 +00:00
|
|
|
pkg := info.program.Package(info.initialPackage)
|
2018-02-19 09:24:28 +00:00
|
|
|
c := &codegen{
|
2018-02-25 12:26:56 +00:00
|
|
|
buildInfo: info,
|
2019-11-22 10:06:32 +00:00
|
|
|
prog: io.NewBufBinWriter(),
|
2018-02-25 12:26:56 +00:00
|
|
|
l: []int{},
|
|
|
|
funcs: map[string]*funcScope{},
|
|
|
|
typeInfo: &pkg.Info,
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Resolve the entrypoint of the program.
|
2018-02-25 12:26:56 +00:00
|
|
|
main, mainFile := resolveEntryPoint(mainIdent, pkg)
|
2018-02-19 09:24:28 +00:00
|
|
|
if main == nil {
|
2019-11-22 14:16:52 +00:00
|
|
|
c.prog.Err = fmt.Errorf("could not find func main. Did you forget to declare it? ")
|
|
|
|
return []byte{}, c.prog.Err
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 09:04:24 +00:00
|
|
|
funUsage := analyzeFuncUsage(info.program.AllPackages)
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Bring all imported functions into scope.
|
2018-02-25 12:26:56 +00:00
|
|
|
for _, pkg := range info.program.AllPackages {
|
|
|
|
for _, f := range pkg.Files {
|
|
|
|
c.resolveFuncDecls(f)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// convert the entry point first.
|
2018-02-25 12:26:56 +00:00
|
|
|
c.convertFuncDecl(mainFile, main)
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// sort map keys to generate code deterministically.
|
2019-02-15 13:38:16 +00:00
|
|
|
keys := make([]*types.Package, 0, len(info.program.AllPackages))
|
|
|
|
for p := range info.program.AllPackages {
|
|
|
|
keys = append(keys, p)
|
|
|
|
}
|
|
|
|
sort.Slice(keys, func(i, j int) bool { return keys[i].Path() < keys[j].Path() })
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Generate the code for the program.
|
2019-02-15 13:38:16 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
pkg := info.program.AllPackages[k]
|
2018-02-25 12:26:56 +00:00
|
|
|
c.typeInfo = &pkg.Info
|
2018-02-27 09:04:24 +00:00
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
for _, f := range pkg.Files {
|
|
|
|
for _, decl := range f.Decls {
|
|
|
|
switch n := decl.(type) {
|
|
|
|
case *ast.FuncDecl:
|
2019-10-22 14:56:03 +00:00
|
|
|
// Don't convert the function if it's not used. This will save a lot
|
2018-02-27 09:04:24 +00:00
|
|
|
// of bytecode space.
|
|
|
|
if n.Name.Name != mainIdent && funUsage.funcUsed(n.Name.Name) {
|
2018-02-25 12:26:56 +00:00
|
|
|
c.convertFuncDecl(f, n)
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
if c.prog.Err != nil {
|
|
|
|
return nil, c.prog.Err
|
|
|
|
}
|
|
|
|
buf := c.prog.Bytes()
|
|
|
|
c.writeJumps(buf)
|
|
|
|
return buf, nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) resolveFuncDecls(f *ast.File) {
|
|
|
|
for _, decl := range f.Decls {
|
|
|
|
switch n := decl.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if n.Name.Name != mainIdent {
|
|
|
|
c.newFunc(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
func (c *codegen) writeJumps(b []byte) {
|
2018-02-19 09:24:28 +00:00
|
|
|
for i, op := range b {
|
|
|
|
j := i + 1
|
2019-12-03 14:05:06 +00:00
|
|
|
switch opcode.Opcode(op) {
|
|
|
|
case opcode.JMP, opcode.JMPIFNOT, opcode.JMPIF, opcode.CALL:
|
2018-04-02 15:04:42 +00:00
|
|
|
index := int16(binary.LittleEndian.Uint16(b[j : j+2]))
|
2018-04-04 19:41:19 +00:00
|
|
|
if int(index) > len(c.l) || int(index) < 0 {
|
2018-02-19 09:24:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
offset := uint16(c.l[index] - i)
|
|
|
|
binary.LittleEndian.PutUint16(b[j:j+2], offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|