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"
|
2020-02-21 08:50:03 +00:00
|
|
|
"math"
|
2019-02-15 13:38:16 +00:00
|
|
|
"sort"
|
2018-07-02 13:02:00 +00:00
|
|
|
"strings"
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-10 11:08:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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/opcode"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-31 13:16:32 +00:00
|
|
|
"golang.org/x/tools/go/loader"
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-05-06 15:10:20 +00:00
|
|
|
// A mapping of lambda functions into their scope.
|
|
|
|
lambda 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
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
globals map[string]int
|
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
// A mapping from label's names to their ids.
|
2020-02-21 08:50:03 +00:00
|
|
|
labels map[labelWithType]uint16
|
2020-03-06 12:11:14 +00:00
|
|
|
// A list of nested label names together with evaluation stack depth.
|
|
|
|
labelList []labelWithStackSize
|
2020-02-17 13:48:41 +00:00
|
|
|
|
|
|
|
// A label for the for-loop being currently visited.
|
|
|
|
currentFor string
|
2020-02-18 14:07:38 +00:00
|
|
|
// A label for the switch statement being visited.
|
|
|
|
currentSwitch string
|
2020-02-17 13:48:41 +00:00
|
|
|
// A label to be used in the next statement.
|
|
|
|
nextLabel string
|
|
|
|
|
2020-03-31 13:57:35 +00:00
|
|
|
// sequencePoints is mapping from method name to a slice
|
|
|
|
// containing info about mapping from opcode's offset
|
|
|
|
// to a text span in the source file.
|
|
|
|
sequencePoints map[string][]DebugSeqPoint
|
|
|
|
|
2020-07-24 10:40:54 +00:00
|
|
|
// initEndOffset specifies the end of the initialization method.
|
|
|
|
initEndOffset int
|
2020-10-02 10:16:02 +00:00
|
|
|
// deployEndOffset specifies the end of the deployment method.
|
|
|
|
deployEndOffset int
|
2020-07-24 10:40:54 +00:00
|
|
|
|
2020-07-28 16:35:41 +00:00
|
|
|
// importMap contains mapping from package aliases to full package names for the current file.
|
|
|
|
importMap map[string]string
|
|
|
|
|
2020-07-28 16:50:44 +00:00
|
|
|
// constMap contains constants from foreign packages.
|
|
|
|
constMap map[string]types.TypeAndValue
|
|
|
|
|
2020-07-29 14:20:00 +00:00
|
|
|
// currPkg is current package being processed.
|
|
|
|
currPkg *types.Package
|
|
|
|
|
2020-07-24 10:40:54 +00:00
|
|
|
// mainPkg is a main package metadata.
|
|
|
|
mainPkg *loader.PackageInfo
|
|
|
|
|
2020-08-05 07:56:36 +00:00
|
|
|
// packages contains packages in the order they were loaded.
|
|
|
|
packages []string
|
|
|
|
|
2020-08-21 12:37:46 +00:00
|
|
|
// exceptionIndex is the index of static slot where exception is stored.
|
|
|
|
exceptionIndex int
|
|
|
|
|
2020-08-10 10:10:35 +00:00
|
|
|
// documents contains paths to all files used by the program.
|
|
|
|
documents []string
|
|
|
|
// docIndex maps file path to an index in documents array.
|
|
|
|
docIndex map[string]int
|
|
|
|
|
2020-11-24 13:36:35 +00:00
|
|
|
// emittedEvents contains all events emitted by contract.
|
|
|
|
emittedEvents map[string][][]string
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// Label table for recording jump destinations.
|
|
|
|
l []int
|
|
|
|
}
|
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
type labelOffsetType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
labelStart labelOffsetType = iota // labelStart is a default label type
|
|
|
|
labelEnd // labelEnd is a type for labels that are targets for break
|
2020-02-17 14:19:54 +00:00
|
|
|
labelPost // labelPost is a type for labels that are targets for continue
|
2020-02-17 13:48:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type labelWithType struct {
|
|
|
|
name string
|
|
|
|
typ labelOffsetType
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:11:14 +00:00
|
|
|
type labelWithStackSize struct {
|
|
|
|
name string
|
|
|
|
sz int
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
type varType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
varGlobal varType = iota
|
|
|
|
varLocal
|
|
|
|
varArgument
|
|
|
|
)
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
// newLabel creates a new label to jump to
|
2020-02-21 08:50:03 +00:00
|
|
|
func (c *codegen) newLabel() (l uint16) {
|
|
|
|
li := len(c.l)
|
|
|
|
if li > math.MaxUint16 {
|
|
|
|
c.prog.Err = errors.New("label number is too big")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l = uint16(li)
|
2018-02-19 09:24:28 +00:00
|
|
|
c.l = append(c.l, -1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
// newNamedLabel creates a new label with a specified name.
|
2020-02-21 08:50:03 +00:00
|
|
|
func (c *codegen) newNamedLabel(typ labelOffsetType, name string) (l uint16) {
|
2020-02-17 13:48:41 +00:00
|
|
|
l = c.newLabel()
|
|
|
|
lt := labelWithType{name: name, typ: typ}
|
|
|
|
c.labels[lt] = l
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:50:03 +00:00
|
|
|
func (c *codegen) setLabel(l uint16) {
|
2018-02-19 09:24:28 +00:00
|
|
|
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
|
|
|
|
}
|
2020-05-13 15:09:55 +00:00
|
|
|
|
|
|
|
typ, ok := t.Type.Underlying().(*types.Basic)
|
|
|
|
if !ok {
|
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
|
|
|
switch typ.Kind() {
|
2020-04-28 13:42:29 +00:00
|
|
|
case types.Int, types.UntypedInt, types.Uint,
|
2020-05-13 07:53:11 +00:00
|
|
|
types.Int8, types.Uint8,
|
2020-04-28 13:42:29 +00:00
|
|
|
types.Int16, types.Uint16,
|
|
|
|
types.Int32, types.Uint32, types.Int64, types.Uint64:
|
2020-01-23 14:28:35 +00:00
|
|
|
val, _ := constant.Int64Val(t.Value)
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.Int(c.prog.BinWriter, val)
|
2020-01-23 14:28:35 +00:00
|
|
|
case types.String, types.UntypedString:
|
|
|
|
val := constant.StringVal(t.Value)
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.String(c.prog.BinWriter, val)
|
2020-01-23 14:28:35 +00:00
|
|
|
case types.Bool, types.UntypedBool:
|
|
|
|
val := constant.BoolVal(t.Value)
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.Bool(c.prog.BinWriter, val)
|
2020-01-23 14:28:35 +00:00
|
|
|
default:
|
|
|
|
c.prog.Err = fmt.Errorf("compiler doesn't know how to convert this basic type: %v", t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
func (c *codegen) emitLoadField(i int) {
|
|
|
|
emit.Int(c.prog.BinWriter, int64(i))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PICKITEM)
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
func (c *codegen) emitStoreStructField(i int) {
|
|
|
|
emit.Int(c.prog.BinWriter, int64(i))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.ROT, opcode.SETITEM)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
// getVarIndex returns variable type and position in corresponding slot,
|
|
|
|
// according to current scope.
|
2020-07-28 16:35:41 +00:00
|
|
|
func (c *codegen) getVarIndex(pkg string, name string) (varType, int) {
|
|
|
|
if pkg == "" {
|
|
|
|
if c.scope != nil {
|
|
|
|
vt, val := c.scope.vars.getVarIndex(name)
|
|
|
|
if val >= 0 {
|
|
|
|
return vt, val
|
|
|
|
}
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
if i, ok := c.globals[c.getIdentName(pkg, name)]; ok {
|
2020-05-07 08:54:35 +00:00
|
|
|
return varGlobal, i
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
return varLocal, c.scope.newVariable(varLocal, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBaseOpcode(t varType) (opcode.Opcode, opcode.Opcode) {
|
|
|
|
switch t {
|
|
|
|
case varGlobal:
|
|
|
|
return opcode.LDSFLD0, opcode.STSFLD0
|
|
|
|
case varLocal:
|
|
|
|
return opcode.LDLOC0, opcode.STLOC0
|
|
|
|
case varArgument:
|
|
|
|
return opcode.LDARG0, opcode.STARG0
|
|
|
|
default:
|
|
|
|
panic("invalid type")
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
// emitLoadVar loads specified variable to the evaluation stack.
|
2020-07-28 16:35:41 +00:00
|
|
|
func (c *codegen) emitLoadVar(pkg string, name string) {
|
|
|
|
t, i := c.getVarIndex(pkg, name)
|
2020-08-21 12:37:46 +00:00
|
|
|
c.emitLoadByIndex(t, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitLoadByIndex loads specified variable type with index i.
|
|
|
|
func (c *codegen) emitLoadByIndex(t varType, i int) {
|
2020-05-07 08:54:35 +00:00
|
|
|
base, _ := getBaseOpcode(t)
|
|
|
|
if i < 7 {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, base+opcode.Opcode(i))
|
2020-05-07 08:54:35 +00:00
|
|
|
} else {
|
|
|
|
emit.Instruction(c.prog.BinWriter, base+7, []byte{byte(i)})
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
// emitStoreVar stores top value from the evaluation stack in the specified variable.
|
2020-07-28 16:35:41 +00:00
|
|
|
func (c *codegen) emitStoreVar(pkg string, name string) {
|
2020-05-19 15:15:03 +00:00
|
|
|
if name == "_" {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP)
|
2020-05-19 15:15:03 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
t, i := c.getVarIndex(pkg, name)
|
2020-08-21 12:37:46 +00:00
|
|
|
c.emitStoreByIndex(t, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emitLoadByIndex stores top value in the specified variable type with index i.
|
|
|
|
func (c *codegen) emitStoreByIndex(t varType, i int) {
|
2020-05-07 08:54:35 +00:00
|
|
|
_, base := getBaseOpcode(t)
|
|
|
|
if i < 7 {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, base+opcode.Opcode(i))
|
2020-05-07 08:54:35 +00:00
|
|
|
} else {
|
|
|
|
emit.Instruction(c.prog.BinWriter, base+7, []byte{byte(i)})
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:57:42 +00:00
|
|
|
func (c *codegen) emitDefault(t types.Type) {
|
|
|
|
switch t := t.Underlying().(type) {
|
|
|
|
case *types.Basic:
|
2020-05-07 08:54:35 +00:00
|
|
|
info := t.Info()
|
|
|
|
switch {
|
|
|
|
case info&types.IsInteger != 0:
|
|
|
|
emit.Int(c.prog.BinWriter, 0)
|
|
|
|
case info&types.IsString != 0:
|
|
|
|
emit.Bytes(c.prog.BinWriter, []byte{})
|
|
|
|
case info&types.IsBoolean != 0:
|
|
|
|
emit.Bool(c.prog.BinWriter, false)
|
|
|
|
default:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PUSHNULL)
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2020-05-19 13:57:42 +00:00
|
|
|
case *types.Struct:
|
2020-05-20 08:32:52 +00:00
|
|
|
num := t.NumFields()
|
|
|
|
emit.Int(c.prog.BinWriter, int64(num))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWSTRUCT)
|
2020-05-20 08:32:52 +00:00
|
|
|
for i := 0; i < num; i++ {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-05-20 08:32:52 +00:00
|
|
|
emit.Int(c.prog.BinWriter, int64(i))
|
|
|
|
c.emitDefault(t.Field(i).Type())
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SETITEM)
|
2020-05-20 08:32:52 +00:00
|
|
|
}
|
2020-05-19 13:57:42 +00:00
|
|
|
default:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PUSHNULL)
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
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.
|
2020-07-28 15:40:41 +00:00
|
|
|
func (c *codegen) convertGlobals(f *ast.File, _ *types.Package) {
|
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:
|
2020-07-28 16:50:44 +00:00
|
|
|
ast.Walk(c, n)
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-04 08:06:23 +00:00
|
|
|
func isInitFunc(decl *ast.FuncDecl) bool {
|
|
|
|
return decl.Name.Name == "init" && decl.Recv == nil &&
|
|
|
|
decl.Type.Params.NumFields() == 0 &&
|
|
|
|
decl.Type.Results.NumFields() == 0
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:25:40 +00:00
|
|
|
func (c *codegen) clearSlots(n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PUSHNULL)
|
|
|
|
c.emitStoreByIndex(varLocal, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) convertInitFuncs(f *ast.File, pkg *types.Package, seenBefore bool) bool {
|
2020-08-04 08:06:23 +00:00
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if isInitFunc(n) {
|
2020-10-06 15:25:40 +00:00
|
|
|
if seenBefore {
|
|
|
|
cnt, _ := countLocals(n)
|
|
|
|
c.clearSlots(cnt)
|
|
|
|
seenBefore = true
|
|
|
|
}
|
2020-08-04 08:06:23 +00:00
|
|
|
c.convertFuncDecl(f, n, pkg)
|
|
|
|
}
|
|
|
|
case *ast.GenDecl:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2020-10-06 15:25:40 +00:00
|
|
|
return seenBefore
|
2020-08-04 08:06:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 10:16:02 +00:00
|
|
|
func isDeployFunc(decl *ast.FuncDecl) bool {
|
|
|
|
if decl.Name.Name != "_deploy" || decl.Recv != nil ||
|
|
|
|
decl.Type.Params.NumFields() != 1 ||
|
|
|
|
decl.Type.Results.NumFields() != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
typ, ok := decl.Type.Params.List[0].Type.(*ast.Ident)
|
|
|
|
return ok && typ.Name == "bool"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) convertDeployFuncs() {
|
|
|
|
seenBefore := false
|
|
|
|
c.ForEachFile(func(f *ast.File, pkg *types.Package) {
|
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if isDeployFunc(n) {
|
|
|
|
if seenBefore {
|
|
|
|
cnt, _ := countLocals(n)
|
|
|
|
c.clearSlots(cnt)
|
|
|
|
}
|
|
|
|
c.convertFuncDecl(f, n, pkg)
|
|
|
|
seenBefore = true
|
|
|
|
}
|
|
|
|
case *ast.GenDecl:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:26:44 +00:00
|
|
|
func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl, pkg *types.Package) {
|
2018-02-19 09:24:28 +00:00
|
|
|
var (
|
2020-05-06 15:10:20 +00:00
|
|
|
f *funcScope
|
|
|
|
ok, isLambda bool
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
2020-08-04 08:06:23 +00:00
|
|
|
isInit := isInitFunc(decl)
|
2020-10-02 10:16:02 +00:00
|
|
|
isDeploy := isDeployFunc(decl)
|
|
|
|
if isInit || isDeploy {
|
2020-08-04 08:06:23 +00:00
|
|
|
f = c.newFuncScope(decl, c.newLabel())
|
2018-02-19 09:24:28 +00:00
|
|
|
} else {
|
2020-08-04 08:06:23 +00:00
|
|
|
f, ok = c.funcs[c.getFuncNameFromDecl("", decl)]
|
|
|
|
if ok {
|
|
|
|
// If this function is a syscall or builtin we will not convert it to bytecode.
|
|
|
|
if isSyscall(f) || isCustomBuiltin(f) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.setLabel(f.label)
|
|
|
|
} else if f, ok = c.lambda[c.getIdentName("", decl.Name.Name)]; ok {
|
|
|
|
isLambda = ok
|
|
|
|
c.setLabel(f.label)
|
|
|
|
} else {
|
|
|
|
f = c.newFunc(decl)
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
|
2020-03-31 12:56:10 +00:00
|
|
|
f.rng.Start = uint16(c.prog.Len())
|
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.
|
2020-10-02 10:16:02 +00:00
|
|
|
if !isInit && !isDeploy {
|
2020-10-06 15:25:40 +00:00
|
|
|
sizeLoc := f.countLocals()
|
|
|
|
if sizeLoc > 255 {
|
|
|
|
c.prog.Err = errors.New("maximum of 255 local variables is allowed")
|
|
|
|
}
|
|
|
|
sizeArg := f.countArgs()
|
|
|
|
if sizeArg > 255 {
|
|
|
|
c.prog.Err = errors.New("maximum of 255 local variables is allowed")
|
|
|
|
}
|
|
|
|
if sizeLoc != 0 || sizeArg != 0 {
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.INITSLOT, []byte{byte(sizeLoc), byte(sizeArg)})
|
|
|
|
}
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-06-29 13:45:27 +00:00
|
|
|
f.vars.newScope()
|
|
|
|
defer f.vars.dropScope()
|
|
|
|
|
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 {
|
2020-05-07 08:54:35 +00:00
|
|
|
// only create an argument here, it will be stored via INITSLOT
|
2020-05-19 13:04:41 +00:00
|
|
|
c.scope.newVariable(varArgument, arg.Names[0].Name)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the arguments in scope.
|
|
|
|
for _, arg := range decl.Type.Params.List {
|
2020-05-06 15:20:12 +00:00
|
|
|
for _, id := range arg.Names {
|
2020-05-07 08:54:35 +00:00
|
|
|
// only create an argument here, it will be stored via INITSLOT
|
|
|
|
c.scope.newVariable(varArgument, id.Name)
|
2020-05-06 15:20:12 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-04-10 09:45:31 +00:00
|
|
|
|
|
|
|
ast.Walk(c, decl.Body)
|
|
|
|
|
2020-05-06 12:39:25 +00:00
|
|
|
// If we have reached the end of the function without encountering `return` statement,
|
|
|
|
// we should clean alt.stack manually.
|
|
|
|
// This can be the case with void and named-return functions.
|
2020-10-02 10:16:02 +00:00
|
|
|
if !isInit && !isDeploy && !lastStmtIsReturn(decl) {
|
2020-03-31 13:57:35 +00:00
|
|
|
c.saveSequencePoint(decl.Body)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.RET)
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
2020-03-31 12:56:10 +00:00
|
|
|
|
|
|
|
f.rng.End = uint16(c.prog.Len() - 1)
|
2020-05-06 15:10:20 +00:00
|
|
|
|
|
|
|
if !isLambda {
|
|
|
|
for _, f := range c.lambda {
|
2020-07-07 10:26:44 +00:00
|
|
|
c.convertFuncDecl(file, f.decl, pkg)
|
2020-05-06 15:10:20 +00:00
|
|
|
}
|
|
|
|
c.lambda = make(map[string]*funcScope)
|
|
|
|
}
|
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:
|
2020-08-10 14:43:26 +00:00
|
|
|
if n.Tok == token.VAR || n.Tok == token.CONST {
|
|
|
|
c.saveSequencePoint(n)
|
|
|
|
}
|
2020-07-28 16:50:44 +00:00
|
|
|
if n.Tok == token.CONST {
|
|
|
|
for _, spec := range n.Specs {
|
|
|
|
vs := spec.(*ast.ValueSpec)
|
|
|
|
for i := range vs.Names {
|
2020-08-25 07:22:58 +00:00
|
|
|
info := c.buildInfo.program.Package(c.currPkg.Path())
|
|
|
|
obj := info.Defs[vs.Names[i]]
|
|
|
|
c.constMap[c.getIdentName("", vs.Names[i].Name)] = types.TypeAndValue{
|
|
|
|
Type: obj.Type(),
|
|
|
|
Value: obj.(*types.Const).Val(),
|
|
|
|
}
|
2020-07-28 16:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-25 12:26:56 +00:00
|
|
|
for _, spec := range n.Specs {
|
|
|
|
switch t := spec.(type) {
|
|
|
|
case *ast.ValueSpec:
|
2020-04-02 13:36:11 +00:00
|
|
|
for _, id := range t.Names {
|
2020-09-06 12:26:03 +00:00
|
|
|
if id.Name != "_" {
|
|
|
|
if c.scope == nil {
|
|
|
|
// it is a global declaration
|
|
|
|
c.newGlobal("", id.Name)
|
|
|
|
} else {
|
|
|
|
c.scope.newLocal(id.Name)
|
|
|
|
}
|
|
|
|
c.registerDebugVariable(id.Name, t.Type)
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2020-04-02 13:36:11 +00:00
|
|
|
}
|
2020-05-19 13:50:20 +00:00
|
|
|
for i := range t.Names {
|
|
|
|
if len(t.Values) != 0 {
|
|
|
|
ast.Walk(c, t.Values[i])
|
2020-05-13 15:09:55 +00:00
|
|
|
} else {
|
2020-05-19 13:57:42 +00:00
|
|
|
c.emitDefault(c.typeOf(t.Type))
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitStoreVar("", t.Names[i].Name)
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
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)
|
2020-03-31 13:57:35 +00:00
|
|
|
c.saveSequencePoint(n)
|
2020-05-18 08:45:20 +00:00
|
|
|
// Assign operations are grouped https://github.com/golang/go/blob/master/src/go/types/stmt.go#L160
|
|
|
|
isAssignOp := token.ADD_ASSIGN <= n.Tok && n.Tok <= token.AND_NOT_ASSIGN
|
|
|
|
if isAssignOp {
|
|
|
|
// RHS can contain exactly one expression, thus there is no need to iterate.
|
|
|
|
ast.Walk(c, n.Lhs[0])
|
|
|
|
ast.Walk(c, n.Rhs[0])
|
2020-08-23 09:39:58 +00:00
|
|
|
c.emitToken(n.Tok, c.typeOf(n.Rhs[0]))
|
2020-05-18 08:45:20 +00:00
|
|
|
}
|
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:
|
2020-05-18 08:45:20 +00:00
|
|
|
if n.Tok == token.DEFINE {
|
2020-04-02 13:36:11 +00:00
|
|
|
if !multiRet {
|
|
|
|
c.registerDebugVariable(t.Name, n.Rhs[i])
|
|
|
|
}
|
2020-05-08 13:10:33 +00:00
|
|
|
if t.Name != "_" {
|
|
|
|
c.scope.newLocal(t.Name)
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
2020-05-18 08:45:20 +00:00
|
|
|
if !isAssignOp && (i == 0 || !multiRet) {
|
|
|
|
ast.Walk(c, n.Rhs[i])
|
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitStoreVar("", t.Name)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
case *ast.SelectorExpr:
|
2020-05-20 08:08:59 +00:00
|
|
|
if !isAssignOp {
|
|
|
|
ast.Walk(c, n.Rhs[i])
|
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
typ := c.typeOf(t.X)
|
|
|
|
if typ == nil {
|
|
|
|
// Store to other package global variable.
|
|
|
|
c.emitStoreVar(t.X.(*ast.Ident).Name, t.Sel.Name)
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-02 11:42:29 +00:00
|
|
|
strct, ok := c.getStruct(typ)
|
2020-05-20 08:08:59 +00:00
|
|
|
if !ok {
|
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
|
|
|
}
|
2020-05-20 08:08:59 +00:00
|
|
|
ast.Walk(c, t.X) // load the struct
|
|
|
|
i := indexOfStruct(strct, t.Sel.Name) // get the index of the field
|
|
|
|
c.emitStoreStructField(i) // store the field
|
2018-10-23 08:23:03 +00:00
|
|
|
|
|
|
|
// Assignments to index expressions.
|
|
|
|
// slice[0] = 10
|
|
|
|
case *ast.IndexExpr:
|
2020-05-18 08:45:20 +00:00
|
|
|
if !isAssignOp {
|
|
|
|
ast.Walk(c, n.Rhs[i])
|
|
|
|
}
|
2020-05-20 14:53:01 +00:00
|
|
|
ast.Walk(c, t.X)
|
2020-05-19 12:35:57 +00:00
|
|
|
ast.Walk(c, t.Index)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.ROT, opcode.SETITEM)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2020-02-10 15:14:34 +00:00
|
|
|
case *ast.SliceExpr:
|
2020-09-15 06:52:56 +00:00
|
|
|
if isCompoundSlice(c.typeOf(n.X.(*ast.Ident)).Underlying()) {
|
|
|
|
c.prog.Err = errors.New("subslices are supported only for []byte")
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-10 15:14:34 +00:00
|
|
|
name := n.X.(*ast.Ident).Name
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitLoadVar("", name)
|
2020-02-10 15:14:34 +00:00
|
|
|
|
|
|
|
if n.Low != nil {
|
|
|
|
ast.Walk(c, n.Low)
|
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PUSH0)
|
2020-02-10 15:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.High != nil {
|
|
|
|
ast.Walk(c, n.High)
|
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.OVER, opcode.SIZE)
|
2020-02-10 15:14:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.OVER, opcode.SUB, opcode.SUBSTR)
|
2020-02-10 15:14:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
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
|
|
|
|
2020-03-06 12:11:14 +00:00
|
|
|
cnt := 0
|
|
|
|
for i := range c.labelList {
|
|
|
|
cnt += c.labelList[i].sz
|
|
|
|
}
|
|
|
|
c.dropItems(cnt)
|
|
|
|
|
2020-05-06 14:24:32 +00:00
|
|
|
if len(n.Results) == 0 {
|
|
|
|
results := c.scope.decl.Type.Results
|
|
|
|
if results.NumFields() != 0 {
|
|
|
|
// function with named returns
|
|
|
|
for i := len(results.List) - 1; i >= 0; i-- {
|
|
|
|
names := results.List[i].Names
|
|
|
|
for j := len(names) - 1; j >= 0; j-- {
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitLoadVar("", names[j].Name)
|
2020-05-06 14:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-08-20 05:34:14 +00:00
|
|
|
c.processDefers()
|
|
|
|
|
2020-03-31 13:57:35 +00:00
|
|
|
c.saveSequencePoint(n)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.RET)
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.IfStmt:
|
2020-06-29 13:57:38 +00:00
|
|
|
c.scope.vars.newScope()
|
|
|
|
defer c.scope.vars.dropScope()
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
lIf := c.newLabel()
|
|
|
|
lElse := c.newLabel()
|
2018-10-20 05:11:00 +00:00
|
|
|
lElseEnd := c.newLabel()
|
|
|
|
|
2020-08-21 08:41:10 +00:00
|
|
|
if n.Init != nil {
|
|
|
|
ast.Walk(c, n.Init)
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
if n.Cond != nil {
|
2020-08-23 11:52:37 +00:00
|
|
|
c.emitBoolExpr(n.Cond, true, false, lElse)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.setLabel(lIf)
|
|
|
|
ast.Walk(c, n.Body)
|
|
|
|
if n.Else != nil {
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, 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
|
|
|
|
|
2020-01-28 12:47:56 +00:00
|
|
|
case *ast.SwitchStmt:
|
2020-08-24 15:55:30 +00:00
|
|
|
eqOpcode := opcode.EQUAL
|
|
|
|
if n.Tag != nil {
|
|
|
|
ast.Walk(c, n.Tag)
|
|
|
|
eqOpcode, _ = convertToken(token.EQL, c.typeOf(n.Tag))
|
|
|
|
} else {
|
|
|
|
emit.Bool(c.prog.BinWriter, true)
|
|
|
|
}
|
2020-02-18 14:07:38 +00:00
|
|
|
switchEnd, label := c.generateLabel(labelEnd)
|
|
|
|
|
|
|
|
lastSwitch := c.currentSwitch
|
|
|
|
c.currentSwitch = label
|
2020-03-06 12:11:14 +00:00
|
|
|
c.pushStackLabel(label, 1)
|
2020-01-28 12:47:56 +00:00
|
|
|
|
2020-03-10 09:34:05 +00:00
|
|
|
startLabels := make([]uint16, len(n.Body.List))
|
|
|
|
for i := range startLabels {
|
|
|
|
startLabels[i] = c.newLabel()
|
|
|
|
}
|
2020-01-28 12:47:56 +00:00
|
|
|
for i := range n.Body.List {
|
|
|
|
lEnd := c.newLabel()
|
2020-03-10 09:34:05 +00:00
|
|
|
lStart := startLabels[i]
|
2020-01-28 12:47:56 +00:00
|
|
|
cc := n.Body.List[i].(*ast.CaseClause)
|
|
|
|
|
|
|
|
if l := len(cc.List); l != 0 { // if not `default`
|
|
|
|
for j := range cc.List {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-01-28 12:47:56 +00:00
|
|
|
ast.Walk(c, cc.List[j])
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, eqOpcode)
|
2020-01-28 12:47:56 +00:00
|
|
|
if j == l-1 {
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPIFNOTL, lEnd)
|
2020-01-28 12:47:56 +00:00
|
|
|
} else {
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPIFL, lStart)
|
2020-01-28 12:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:57:38 +00:00
|
|
|
c.scope.vars.newScope()
|
|
|
|
|
2020-01-28 12:47:56 +00:00
|
|
|
c.setLabel(lStart)
|
2020-03-10 09:34:05 +00:00
|
|
|
last := len(cc.Body) - 1
|
|
|
|
for j, stmt := range cc.Body {
|
|
|
|
if j == last && isFallthroughStmt(stmt) {
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, startLabels[i+1])
|
2020-03-10 09:34:05 +00:00
|
|
|
break
|
|
|
|
}
|
2020-01-28 12:47:56 +00:00
|
|
|
ast.Walk(c, stmt)
|
|
|
|
}
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, switchEnd)
|
2020-01-28 12:47:56 +00:00
|
|
|
c.setLabel(lEnd)
|
2020-06-29 13:57:38 +00:00
|
|
|
|
|
|
|
c.scope.vars.dropScope()
|
2020-01-28 12:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.setLabel(switchEnd)
|
2020-03-06 12:11:14 +00:00
|
|
|
c.dropStackLabel()
|
2020-01-28 12:47:56 +00:00
|
|
|
|
2020-02-18 14:07:38 +00:00
|
|
|
c.currentSwitch = lastSwitch
|
|
|
|
|
2020-01-28 12:47:56 +00:00
|
|
|
return nil
|
|
|
|
|
2020-05-06 15:10:20 +00:00
|
|
|
case *ast.FuncLit:
|
|
|
|
l := c.newLabel()
|
|
|
|
c.newLambda(l, n)
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint16(buf, l)
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.PUSHA, buf)
|
|
|
|
return nil
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
case *ast.BasicLit:
|
2020-05-13 15:09:55 +00:00
|
|
|
c.emitLoadConst(c.typeAndValueOf(n))
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
2020-08-02 11:56:47 +00:00
|
|
|
case *ast.StarExpr:
|
|
|
|
_, ok := c.getStruct(c.typeOf(n.X))
|
|
|
|
if !ok {
|
|
|
|
c.prog.Err = errors.New("dereferencing is only supported on structs")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
c.emitConvert(stackitem.StructT)
|
|
|
|
return nil
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
case *ast.Ident:
|
2020-05-19 12:20:10 +00:00
|
|
|
if tv := c.typeAndValueOf(n); tv.Value != nil {
|
2020-01-27 10:37:33 +00:00
|
|
|
c.emitLoadConst(tv)
|
2020-06-17 08:21:37 +00:00
|
|
|
} else if n.Name == "nil" {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PUSHNULL)
|
2018-02-19 09:24:28 +00:00
|
|
|
} else {
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitLoadVar("", n.Name)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.CompositeLit:
|
2020-12-10 11:08:42 +00:00
|
|
|
t := c.typeOf(n)
|
|
|
|
switch typ := t.Underlying().(type) {
|
2020-06-24 16:25:08 +00:00
|
|
|
case *types.Struct:
|
2020-08-02 11:42:29 +00:00
|
|
|
c.convertStruct(n, false)
|
2020-06-24 16:25:08 +00:00
|
|
|
case *types.Map:
|
|
|
|
c.convertMap(n)
|
2018-02-19 09:24:28 +00:00
|
|
|
default:
|
2020-12-10 11:08:42 +00:00
|
|
|
if tn, ok := t.(*types.Named); ok && isInteropPath(tn.String()) {
|
|
|
|
st, _ := scAndVMInteropTypeFromExpr(tn)
|
|
|
|
expectedLen := -1
|
|
|
|
switch st {
|
|
|
|
case smartcontract.Hash160Type:
|
|
|
|
expectedLen = 20
|
|
|
|
case smartcontract.Hash256Type:
|
|
|
|
expectedLen = 32
|
|
|
|
}
|
|
|
|
if expectedLen != -1 && expectedLen != len(n.Elts) {
|
|
|
|
c.prog.Err = fmt.Errorf("%s type must have size %d", tn.Obj().Name(), expectedLen)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
ln := len(n.Elts)
|
2019-10-22 14:56:03 +00:00
|
|
|
// ByteArrays needs a different approach than normal arrays.
|
2020-05-13 15:09:55 +00:00
|
|
|
if isByteSlice(typ) {
|
2018-02-27 09:04:24 +00:00
|
|
|
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
|
|
|
}
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.Int(c.prog.BinWriter, int64(ln))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PACK)
|
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:
|
2020-08-23 11:52:37 +00:00
|
|
|
c.emitBinaryExpr(n, false, false, 0)
|
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
|
|
|
|
case *ast.CallExpr:
|
|
|
|
var (
|
2018-04-04 19:41:19 +00:00
|
|
|
f *funcScope
|
|
|
|
ok bool
|
2020-05-06 15:10:20 +00:00
|
|
|
name string
|
2018-04-04 19:41:19 +00:00
|
|
|
numArgs = len(n.Args)
|
2020-06-08 11:16:41 +00:00
|
|
|
isBuiltin bool
|
2020-06-24 15:36:21 +00:00
|
|
|
isFunc bool
|
2020-08-19 14:31:02 +00:00
|
|
|
isLiteral bool
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
switch fun := n.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
2020-07-29 14:20:00 +00:00
|
|
|
f, ok = c.funcs[c.getIdentName("", fun.Name)]
|
2020-06-08 11:16:41 +00:00
|
|
|
isBuiltin = isGoBuiltin(fun.Name)
|
2018-04-04 19:41:19 +00:00
|
|
|
if !ok && !isBuiltin {
|
2020-05-06 15:10:20 +00:00
|
|
|
name = fun.Name
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2020-06-24 15:36:21 +00:00
|
|
|
// distinguish lambda invocations from type conversions
|
|
|
|
if fun.Obj != nil && fun.Obj.Kind == ast.Var {
|
|
|
|
isFunc = true
|
|
|
|
}
|
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.
|
2020-07-29 14:20:00 +00:00
|
|
|
name, isMethod := c.getFuncNameFromSelector(fun)
|
|
|
|
if isMethod {
|
2018-02-24 09:06:48 +00:00
|
|
|
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
|
|
|
|
2020-07-29 14:20:00 +00:00
|
|
|
f, ok = c.funcs[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
|
|
|
}
|
2020-06-08 11:16:41 +00:00
|
|
|
isBuiltin = isCustomBuiltin(f)
|
2018-05-06 06:03:26 +00:00
|
|
|
case *ast.ArrayType:
|
2020-01-27 10:37:33 +00:00
|
|
|
// For now we will assume that there are only byte slice conversions.
|
|
|
|
// E.g. []byte("foobar") or []byte(scriptHash).
|
|
|
|
ast.Walk(c, n.Args[0])
|
2020-06-03 12:55:06 +00:00
|
|
|
c.emitConvert(stackitem.BufferT)
|
2018-05-06 06:03:26 +00:00
|
|
|
return nil
|
2020-08-19 14:31:02 +00:00
|
|
|
case *ast.FuncLit:
|
|
|
|
isLiteral = true
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:57:35 +00:00
|
|
|
c.saveSequencePoint(n)
|
|
|
|
|
2020-01-29 07:08:24 +00:00
|
|
|
args := transformArgs(n.Fun, n.Args)
|
2020-01-27 07:59:57 +00:00
|
|
|
|
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)
|
2020-08-05 06:33:18 +00:00
|
|
|
typ := c.typeOf(arg)
|
|
|
|
_, ok := typ.Underlying().(*types.Struct)
|
|
|
|
if ok && !isInteropPath(typ.String()) {
|
|
|
|
// To clone struct fields we create a new array and append struct to it.
|
|
|
|
// This way even non-pointer struct fields will be copied.
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWARRAY0,
|
|
|
|
opcode.DUP, opcode.ROT, opcode.APPEND,
|
|
|
|
opcode.PUSH0, opcode.PICKITEM)
|
2020-08-05 06:33:18 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-04-04 19:41:19 +00:00
|
|
|
// Do not swap for builtin functions.
|
|
|
|
if !isBuiltin {
|
2020-06-27 07:39:30 +00:00
|
|
|
typ, ok := c.typeOf(n.Fun).(*types.Signature)
|
|
|
|
if ok && typ.Variadic() && !n.Ellipsis.IsValid() {
|
|
|
|
// pack variadic args into an array only if last argument is not of form `...`
|
2020-06-26 10:30:48 +00:00
|
|
|
varSize := len(n.Args) - typ.Params().Len() + 1
|
|
|
|
c.emitReverse(varSize)
|
|
|
|
emit.Int(c.prog.BinWriter, int64(varSize))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PACK)
|
2020-06-26 10:30:48 +00:00
|
|
|
numArgs -= varSize - 1
|
|
|
|
}
|
2020-12-08 12:37:03 +00:00
|
|
|
// CallFlag in CallEx interop should be the last argument
|
|
|
|
// but this can't be reflected in signature due to varargs.
|
|
|
|
// It is first in compiler interop though, thus we just need to reverse 1 values less.
|
|
|
|
if f != nil && isSyscall(f) && f.pkg.Name() == "contract" && f.name == "CallEx" {
|
|
|
|
c.emitReverse(numArgs - 1)
|
|
|
|
} else {
|
|
|
|
c.emitReverse(numArgs)
|
|
|
|
}
|
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-05-06 15:10:20 +00:00
|
|
|
case name != "":
|
2020-06-24 15:36:21 +00:00
|
|
|
// Function was not found thus is can be only an invocation of func-typed variable or type conversion.
|
2020-06-24 15:43:29 +00:00
|
|
|
// We care only about string conversions because all others are effectively no-op in NeoVM.
|
|
|
|
// E.g. one cannot write `bool(int(a))`, only `int32(int(a))`.
|
|
|
|
if isString(c.typeOf(n.Fun)) {
|
|
|
|
c.emitConvert(stackitem.ByteArrayT)
|
|
|
|
} else if isFunc {
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitLoadVar("", name)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.CALLA)
|
2020-06-24 15:36:21 +00:00
|
|
|
}
|
2020-08-19 14:31:02 +00:00
|
|
|
case isLiteral:
|
|
|
|
ast.Walk(c, n.Fun)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.CALLA)
|
2018-08-22 07:51:35 +00:00
|
|
|
case isSyscall(f):
|
2020-11-24 13:36:35 +00:00
|
|
|
if f.pkg.Name() == "runtime" && f.name == "Notify" {
|
|
|
|
tv := c.typeAndValueOf(n.Args[0])
|
|
|
|
params := make([]string, 0, len(n.Args[1:]))
|
|
|
|
for _, p := range n.Args[1:] {
|
2020-12-09 10:14:21 +00:00
|
|
|
st, _ := c.scAndVMTypeFromExpr(p)
|
|
|
|
params = append(params, st.String())
|
2020-11-24 13:36:35 +00:00
|
|
|
}
|
2020-12-03 11:57:45 +00:00
|
|
|
// Sometimes event name is stored in a var.
|
|
|
|
// Skip in this case.
|
|
|
|
if tv.Value != nil {
|
|
|
|
name := constant.StringVal(tv.Value)
|
|
|
|
c.emittedEvents[name] = append(c.emittedEvents[name], params)
|
|
|
|
}
|
2020-11-24 13:36:35 +00:00
|
|
|
}
|
2020-08-19 07:10:40 +00:00
|
|
|
c.convertSyscall(n, f.pkg.Name(), f.name)
|
2019-02-09 15:53:58 +00:00
|
|
|
default:
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Call(c.prog.BinWriter, opcode.CALLL, f.label)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 15:37:39 +00:00
|
|
|
if c.scope != nil && c.scope.voidCalls[n] {
|
|
|
|
var sz int
|
|
|
|
if f != nil {
|
|
|
|
sz = f.decl.Type.Results.NumFields()
|
|
|
|
} else if !isBuiltin {
|
|
|
|
// lambda invocation
|
|
|
|
f := c.typeOf(n.Fun).Underlying().(*types.Signature)
|
|
|
|
sz = f.Results().Len()
|
|
|
|
}
|
|
|
|
for i := 0; i < sz; i++ {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP)
|
2020-08-24 15:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
|
|
|
|
2020-08-20 05:34:14 +00:00
|
|
|
case *ast.DeferStmt:
|
2020-08-21 12:37:46 +00:00
|
|
|
catch := c.newLabel()
|
2020-08-20 05:34:14 +00:00
|
|
|
finally := c.newLabel()
|
|
|
|
param := make([]byte, 8)
|
2020-08-21 12:37:46 +00:00
|
|
|
binary.LittleEndian.PutUint16(param[0:], catch)
|
2020-08-20 05:34:14 +00:00
|
|
|
binary.LittleEndian.PutUint16(param[4:], finally)
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.TRYL, param)
|
|
|
|
c.scope.deferStack = append(c.scope.deferStack, deferInfo{
|
2020-08-21 12:37:46 +00:00
|
|
|
catchLabel: catch,
|
2020-08-20 05:34:14 +00:00
|
|
|
finallyLabel: finally,
|
|
|
|
expr: n.Call,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
case *ast.SelectorExpr:
|
2020-07-28 16:35:41 +00:00
|
|
|
typ := c.typeOf(n.X)
|
|
|
|
if typ == nil {
|
|
|
|
// This is a global variable from a package.
|
|
|
|
pkgAlias := n.X.(*ast.Ident).Name
|
2020-07-28 16:50:44 +00:00
|
|
|
name := c.getIdentName(pkgAlias, n.Sel.Name)
|
|
|
|
if tv, ok := c.constMap[name]; ok {
|
|
|
|
c.emitLoadConst(tv)
|
|
|
|
} else {
|
|
|
|
c.emitLoadVar(pkgAlias, n.Sel.Name)
|
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-02 11:42:29 +00:00
|
|
|
strct, ok := c.getStruct(typ)
|
2020-05-20 08:05:01 +00:00
|
|
|
if !ok {
|
|
|
|
c.prog.Err = fmt.Errorf("selectors are supported only on structs")
|
2019-11-22 14:16:52 +00:00
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2020-05-20 08:05:01 +00:00
|
|
|
ast.Walk(c, n.X) // load the struct
|
|
|
|
i := indexOfStruct(strct, n.Sel.Name)
|
|
|
|
c.emitLoadField(i) // load the field
|
2018-02-19 09:24:28 +00:00
|
|
|
return nil
|
2018-02-27 09:04:24 +00:00
|
|
|
|
|
|
|
case *ast.UnaryExpr:
|
2020-08-02 11:42:29 +00:00
|
|
|
if n.Op == token.AND {
|
|
|
|
// We support only taking address from struct literals.
|
|
|
|
// For identifiers we can't support "taking address" in a general way
|
|
|
|
// because both struct and array are reference types.
|
|
|
|
lit, ok := n.X.(*ast.CompositeLit)
|
|
|
|
if ok {
|
|
|
|
c.convertStruct(lit, true)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.prog.Err = fmt.Errorf("'&' can be used only with struct literals")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEGATE)
|
2019-02-19 13:50:34 +00:00
|
|
|
case token.NOT:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NOT)
|
2019-02-19 13:50:34 +00:00
|
|
|
case token.XOR:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(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)
|
2020-08-23 09:39:58 +00:00
|
|
|
c.emitToken(n.Tok, c.typeOf(n.X))
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
// 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 {
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitStoreVar("", ident.Name)
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
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)
|
2020-05-19 12:35:57 +00:00
|
|
|
ast.Walk(c, n.Index)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PICKITEM) // just pickitem here
|
2020-01-23 14:28:35 +00:00
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
return nil
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
case *ast.BranchStmt:
|
2020-02-18 14:07:38 +00:00
|
|
|
var label string
|
2020-02-17 13:48:41 +00:00
|
|
|
if n.Label != nil {
|
|
|
|
label = n.Label.Name
|
2020-02-18 14:07:38 +00:00
|
|
|
} else if n.Tok == token.BREAK {
|
|
|
|
label = c.currentSwitch
|
|
|
|
} else if n.Tok == token.CONTINUE {
|
|
|
|
label = c.currentFor
|
2020-02-17 13:48:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 12:11:14 +00:00
|
|
|
cnt := 0
|
|
|
|
for i := len(c.labelList) - 1; i >= 0 && c.labelList[i].name != label; i-- {
|
|
|
|
cnt += c.labelList[i].sz
|
|
|
|
}
|
|
|
|
c.dropItems(cnt)
|
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
switch n.Tok {
|
|
|
|
case token.BREAK:
|
|
|
|
end := c.getLabelOffset(labelEnd, label)
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, end)
|
2020-02-17 13:48:41 +00:00
|
|
|
case token.CONTINUE:
|
2020-02-17 14:19:54 +00:00
|
|
|
post := c.getLabelOffset(labelPost, label)
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, post)
|
2020-02-17 13:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ast.LabeledStmt:
|
|
|
|
c.nextLabel = n.Label.Name
|
|
|
|
|
|
|
|
ast.Walk(c, n.Stmt)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2020-06-29 13:59:13 +00:00
|
|
|
case *ast.BlockStmt:
|
|
|
|
c.scope.vars.newScope()
|
|
|
|
defer c.scope.vars.dropScope()
|
|
|
|
|
|
|
|
for i := range n.List {
|
|
|
|
ast.Walk(c, n.List[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
case *ast.ForStmt:
|
2020-06-29 13:57:38 +00:00
|
|
|
c.scope.vars.newScope()
|
|
|
|
defer c.scope.vars.dropScope()
|
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
fstart, label := c.generateLabel(labelStart)
|
|
|
|
fend := c.newNamedLabel(labelEnd, label)
|
2020-02-17 14:19:54 +00:00
|
|
|
fpost := c.newNamedLabel(labelPost, label)
|
2020-02-17 13:48:41 +00:00
|
|
|
|
|
|
|
lastLabel := c.currentFor
|
2020-02-18 14:07:38 +00:00
|
|
|
lastSwitch := c.currentSwitch
|
2020-02-17 13:48:41 +00:00
|
|
|
c.currentFor = label
|
2020-02-18 14:07:38 +00:00
|
|
|
c.currentSwitch = label
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
// 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.
|
2020-03-06 12:11:14 +00:00
|
|
|
c.pushStackLabel(label, 0)
|
2018-04-02 15:04:42 +00:00
|
|
|
c.setLabel(fstart)
|
2020-03-26 11:57:57 +00:00
|
|
|
if n.Cond != nil {
|
|
|
|
ast.Walk(c, n.Cond)
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2020-03-26 11:57:57 +00:00
|
|
|
// Jump if the condition is false
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPIFNOTL, fend)
|
2020-03-26 11:57:57 +00:00
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
// Walk body followed by the iterator (post stmt).
|
|
|
|
ast.Walk(c, n.Body)
|
2020-02-17 14:19:54 +00:00
|
|
|
c.setLabel(fpost)
|
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.
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, fstart)
|
2018-04-02 15:04:42 +00:00
|
|
|
c.setLabel(fend)
|
2020-03-06 12:11:14 +00:00
|
|
|
c.dropStackLabel()
|
2018-04-02 15:04:42 +00:00
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
c.currentFor = lastLabel
|
2020-02-18 14:07:38 +00:00
|
|
|
c.currentSwitch = lastSwitch
|
2020-02-17 13:48:41 +00:00
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
return nil
|
2018-04-10 09:45:31 +00:00
|
|
|
|
2020-02-11 12:26:11 +00:00
|
|
|
case *ast.RangeStmt:
|
2020-06-29 13:57:38 +00:00
|
|
|
c.scope.vars.newScope()
|
|
|
|
defer c.scope.vars.dropScope()
|
|
|
|
|
2020-02-19 08:24:31 +00:00
|
|
|
start, label := c.generateLabel(labelStart)
|
|
|
|
end := c.newNamedLabel(labelEnd, label)
|
|
|
|
post := c.newNamedLabel(labelPost, label)
|
|
|
|
|
|
|
|
lastFor := c.currentFor
|
|
|
|
lastSwitch := c.currentSwitch
|
|
|
|
c.currentFor = label
|
|
|
|
c.currentSwitch = label
|
2020-02-11 12:26:11 +00:00
|
|
|
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
|
2020-06-19 14:14:00 +00:00
|
|
|
// Implementation is a bit different for slices and maps:
|
|
|
|
// For slices we iterate index from 0 to len-1, storing array, len and index on stack.
|
|
|
|
// For maps we iterate index from 0 to len-1, storing map, keyarray, size and index on stack.
|
|
|
|
_, isMap := c.typeOf(n.X).Underlying().(*types.Map)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-06-19 14:14:00 +00:00
|
|
|
if isMap {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.KEYS, opcode.DUP)
|
2020-06-19 14:14:00 +00:00
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SIZE, opcode.PUSH0)
|
2020-06-19 14:14:00 +00:00
|
|
|
|
|
|
|
stackSize := 3 // slice, len(slice), index
|
|
|
|
if isMap {
|
|
|
|
stackSize++ // map, keys, len(keys), index in keys
|
|
|
|
}
|
|
|
|
c.pushStackLabel(label, stackSize)
|
2020-02-11 12:26:11 +00:00
|
|
|
c.setLabel(start)
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.OVER, opcode.OVER)
|
2020-06-19 14:14:00 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPLEL, end)
|
2020-02-11 12:26:11 +00:00
|
|
|
|
2020-06-19 14:41:06 +00:00
|
|
|
var keyLoaded bool
|
|
|
|
needValue := n.Value != nil && n.Value.(*ast.Ident).Name != "_"
|
2020-06-19 14:37:07 +00:00
|
|
|
if n.Key != nil && n.Key.(*ast.Ident).Name != "_" {
|
2020-06-19 14:14:00 +00:00
|
|
|
if isMap {
|
|
|
|
c.rangeLoadKey()
|
2020-06-19 14:41:06 +00:00
|
|
|
if needValue {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-06-19 14:41:06 +00:00
|
|
|
keyLoaded = true
|
|
|
|
}
|
2020-06-19 14:14:00 +00:00
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-06-19 14:14:00 +00:00
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitStoreVar("", n.Key.(*ast.Ident).Name)
|
2020-02-11 12:26:11 +00:00
|
|
|
}
|
2020-06-19 14:41:06 +00:00
|
|
|
if needValue {
|
|
|
|
if !isMap || !keyLoaded {
|
|
|
|
c.rangeLoadKey()
|
|
|
|
}
|
2020-06-19 14:14:00 +00:00
|
|
|
if isMap {
|
|
|
|
// we have loaded only key from key array, now load value
|
|
|
|
emit.Int(c.prog.BinWriter, 4)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter,
|
|
|
|
opcode.PICK, // load map itself (+1 because key was pushed)
|
|
|
|
opcode.SWAP, // key should be on top
|
|
|
|
opcode.PICKITEM)
|
2020-06-19 14:14:00 +00:00
|
|
|
}
|
2020-07-28 16:35:41 +00:00
|
|
|
c.emitStoreVar("", n.Value.(*ast.Ident).Name)
|
2020-05-19 15:07:29 +00:00
|
|
|
}
|
2020-02-11 12:26:11 +00:00
|
|
|
|
|
|
|
ast.Walk(c, n.Body)
|
|
|
|
|
2020-02-19 08:24:31 +00:00
|
|
|
c.setLabel(post)
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.INC)
|
2020-04-23 08:56:36 +00:00
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, start)
|
2020-02-11 12:26:11 +00:00
|
|
|
|
|
|
|
c.setLabel(end)
|
2020-03-06 12:11:14 +00:00
|
|
|
c.dropStackLabel()
|
2020-02-11 12:26:11 +00:00
|
|
|
|
2020-02-19 08:24:31 +00:00
|
|
|
c.currentFor = lastFor
|
|
|
|
c.currentSwitch = lastSwitch
|
|
|
|
|
2020-02-11 12:26:11 +00:00
|
|
|
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)
|
2020-06-24 14:58:15 +00:00
|
|
|
typ := toNeoType(c.typeOf(n.Type))
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)})
|
2018-04-10 09:45:31 +00:00
|
|
|
return nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-08-20 05:34:14 +00:00
|
|
|
// processDefers emits code for `defer` statements.
|
2020-08-21 12:37:46 +00:00
|
|
|
// TRY-related opcodes handle exception as follows:
|
|
|
|
// 1. CATCH block is executed only if exception has occured.
|
|
|
|
// 2. FINALLY block is always executed, but after catch block.
|
|
|
|
// Go `defer` statements are a bit different:
|
|
|
|
// 1. `defer` is always executed irregardless of whether an exception has occured.
|
|
|
|
// 2. `recover` can or can not handle a possible exception.
|
|
|
|
// Thus we use the following approach:
|
|
|
|
// 1. Throwed exception is saved in a static field X, static fields Y and is set to true.
|
|
|
|
// 2. CATCH and FINALLY blocks are the same, and both contain the same CALLs.
|
|
|
|
// 3. In CATCH block we set Y to true and emit default return values if it is the last defer.
|
|
|
|
// 4. Execute FINALLY block only if Y is false.
|
2020-08-20 05:34:14 +00:00
|
|
|
func (c *codegen) processDefers() {
|
|
|
|
for i := len(c.scope.deferStack) - 1; i >= 0; i-- {
|
|
|
|
stmt := c.scope.deferStack[i]
|
|
|
|
after := c.newLabel()
|
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.ENDTRYL, after)
|
2020-08-21 12:37:46 +00:00
|
|
|
|
|
|
|
c.setLabel(stmt.catchLabel)
|
|
|
|
c.emitStoreByIndex(varGlobal, c.exceptionIndex)
|
|
|
|
emit.Int(c.prog.BinWriter, 1)
|
|
|
|
c.emitStoreByIndex(varLocal, c.scope.finallyProcessedIndex)
|
|
|
|
ast.Walk(c, stmt.expr)
|
|
|
|
if i == 0 {
|
|
|
|
// After panic, default values must be returns, except for named returns,
|
|
|
|
// which we don't support here for now.
|
|
|
|
for i := len(c.scope.decl.Type.Results.List) - 1; i >= 0; i-- {
|
|
|
|
c.emitDefault(c.typeOf(c.scope.decl.Type.Results.List[i].Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.ENDTRYL, after)
|
|
|
|
|
2020-08-20 05:34:14 +00:00
|
|
|
c.setLabel(stmt.finallyLabel)
|
2020-08-21 12:37:46 +00:00
|
|
|
before := c.newLabel()
|
|
|
|
c.emitLoadByIndex(varLocal, c.scope.finallyProcessedIndex)
|
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPIFL, before)
|
2020-08-20 05:34:14 +00:00
|
|
|
ast.Walk(c, stmt.expr)
|
2020-08-21 12:37:46 +00:00
|
|
|
c.setLabel(before)
|
|
|
|
emit.Int(c.prog.BinWriter, 0)
|
|
|
|
c.emitStoreByIndex(varLocal, c.scope.finallyProcessedIndex)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.ENDFINALLY)
|
2020-08-20 05:34:14 +00:00
|
|
|
c.setLabel(after)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 14:14:00 +00:00
|
|
|
func (c *codegen) rangeLoadKey() {
|
|
|
|
emit.Int(c.prog.BinWriter, 2)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter,
|
|
|
|
opcode.PICK, // load keys
|
|
|
|
opcode.OVER, // load index in key array
|
|
|
|
opcode.PICKITEM)
|
2020-06-19 14:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 09:34:05 +00:00
|
|
|
func isFallthroughStmt(c ast.Node) bool {
|
|
|
|
s, ok := c.(*ast.BranchStmt)
|
|
|
|
return ok && s.Tok == token.FALLTHROUGH
|
|
|
|
}
|
|
|
|
|
2020-08-23 09:33:21 +00:00
|
|
|
func (c *codegen) getCompareWithNilArg(n *ast.BinaryExpr) ast.Expr {
|
|
|
|
if isExprNil(n.X) {
|
|
|
|
return n.Y
|
|
|
|
} else if isExprNil(n.Y) {
|
|
|
|
return n.X
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-23 11:52:37 +00:00
|
|
|
func (c *codegen) emitJumpOnCondition(cond bool, jmpLabel uint16) {
|
|
|
|
if cond {
|
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPIFL, jmpLabel)
|
|
|
|
} else {
|
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPIFNOTL, jmpLabel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:00:23 +00:00
|
|
|
// emitBoolExpr emits boolean expression. If needJump is true and expression evaluates to `cond`,
|
|
|
|
// jump to jmpLabel is performed and no item is left on stack.
|
2020-08-23 11:52:37 +00:00
|
|
|
func (c *codegen) emitBoolExpr(n ast.Expr, needJump bool, cond bool, jmpLabel uint16) {
|
|
|
|
if be, ok := n.(*ast.BinaryExpr); ok {
|
|
|
|
c.emitBinaryExpr(be, needJump, cond, jmpLabel)
|
|
|
|
} else {
|
|
|
|
ast.Walk(c, n)
|
|
|
|
if needJump {
|
|
|
|
c.emitJumpOnCondition(cond, jmpLabel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:00:23 +00:00
|
|
|
// emitBinaryExpr emits binary expression. If needJump is true and expression evaluates to `cond`,
|
|
|
|
// jump to jmpLabel is performed and no item is left on stack.
|
2020-08-23 11:52:37 +00:00
|
|
|
func (c *codegen) emitBinaryExpr(n *ast.BinaryExpr, needJump bool, cond bool, jmpLabel uint16) {
|
|
|
|
// 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.
|
|
|
|
// NOTE: Constants will also be automatically resolved be the AST parser.
|
|
|
|
// example:
|
|
|
|
// const x = 10
|
|
|
|
// x + 2 will results into 12
|
|
|
|
tinfo := c.typeAndValueOf(n)
|
|
|
|
if tinfo.Value != nil {
|
|
|
|
c.emitLoadConst(tinfo)
|
|
|
|
if needJump && isBool(tinfo.Type) {
|
|
|
|
c.emitJumpOnCondition(cond, jmpLabel)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if arg := c.getCompareWithNilArg(n); arg != nil {
|
|
|
|
ast.Walk(c, arg)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.ISNULL)
|
2020-08-23 11:52:37 +00:00
|
|
|
if needJump {
|
|
|
|
c.emitJumpOnCondition(cond == (n.Op == token.EQL), jmpLabel)
|
|
|
|
} else if n.Op == token.NEQ {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NOT)
|
2020-08-23 11:52:37 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n.Op {
|
|
|
|
case token.LAND, token.LOR:
|
|
|
|
end := c.newLabel()
|
|
|
|
|
|
|
|
// true || .. == true, false && .. == false
|
|
|
|
condShort := n.Op == token.LOR
|
|
|
|
if needJump {
|
|
|
|
l := end
|
|
|
|
if cond == condShort {
|
|
|
|
l = jmpLabel
|
|
|
|
}
|
|
|
|
c.emitBoolExpr(n.X, true, condShort, l)
|
|
|
|
c.emitBoolExpr(n.Y, true, cond, jmpLabel)
|
|
|
|
} else {
|
|
|
|
push := c.newLabel()
|
|
|
|
c.emitBoolExpr(n.X, true, condShort, push)
|
|
|
|
c.emitBoolExpr(n.Y, false, false, 0)
|
|
|
|
emit.Jmp(c.prog.BinWriter, opcode.JMPL, end)
|
|
|
|
c.setLabel(push)
|
|
|
|
emit.Bool(c.prog.BinWriter, condShort)
|
|
|
|
}
|
|
|
|
c.setLabel(end)
|
|
|
|
|
|
|
|
default:
|
|
|
|
ast.Walk(c, n.X)
|
|
|
|
ast.Walk(c, n.Y)
|
2020-08-23 15:00:23 +00:00
|
|
|
typ := c.typeOf(n.X)
|
|
|
|
if !needJump {
|
|
|
|
c.emitToken(n.Op, typ)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op, ok := getJumpForToken(n.Op, typ)
|
|
|
|
if !ok {
|
|
|
|
c.emitToken(n.Op, typ)
|
2020-08-23 11:52:37 +00:00
|
|
|
c.emitJumpOnCondition(cond, jmpLabel)
|
2020-08-23 15:00:23 +00:00
|
|
|
return
|
2020-08-23 11:52:37 +00:00
|
|
|
}
|
2020-08-23 15:00:23 +00:00
|
|
|
if !cond {
|
|
|
|
op = negateJmp(op)
|
|
|
|
}
|
|
|
|
emit.Jmp(c.prog.BinWriter, op, jmpLabel)
|
2020-08-23 11:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:11:14 +00:00
|
|
|
func (c *codegen) pushStackLabel(name string, size int) {
|
|
|
|
c.labelList = append(c.labelList, labelWithStackSize{
|
|
|
|
name: name,
|
|
|
|
sz: size,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) dropStackLabel() {
|
|
|
|
last := len(c.labelList) - 1
|
|
|
|
c.dropItems(c.labelList[last].sz)
|
|
|
|
c.labelList = c.labelList[:last]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) dropItems(n int) {
|
|
|
|
if n < 4 {
|
|
|
|
for i := 0; i < n; i++ {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP)
|
2020-03-06 12:11:14 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
emit.Int(c.prog.BinWriter, int64(n))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PACK, opcode.DROP)
|
2020-03-06 12:11:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:43:29 +00:00
|
|
|
// emitReverse reverses top num items of the stack.
|
|
|
|
func (c *codegen) emitReverse(num int) {
|
|
|
|
switch num {
|
2020-06-19 14:28:07 +00:00
|
|
|
case 0, 1:
|
2020-02-10 07:43:29 +00:00
|
|
|
case 2:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SWAP)
|
2020-02-10 07:43:29 +00:00
|
|
|
case 3:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REVERSE3)
|
2020-05-06 09:12:29 +00:00
|
|
|
case 4:
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REVERSE4)
|
2020-02-10 07:43:29 +00:00
|
|
|
default:
|
2020-05-06 09:12:29 +00:00
|
|
|
emit.Int(c.prog.BinWriter, int64(num))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REVERSEN)
|
2020-02-10 07:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 13:48:41 +00:00
|
|
|
// generateLabel returns a new label.
|
2020-02-21 08:50:03 +00:00
|
|
|
func (c *codegen) generateLabel(typ labelOffsetType) (uint16, string) {
|
2020-02-17 13:48:41 +00:00
|
|
|
name := c.nextLabel
|
|
|
|
if name == "" {
|
|
|
|
name = fmt.Sprintf("@%d", len(c.l))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.nextLabel = ""
|
|
|
|
return c.newNamedLabel(typ, name), name
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:50:03 +00:00
|
|
|
func (c *codegen) getLabelOffset(typ labelOffsetType, name string) uint16 {
|
2020-02-17 13:48:41 +00:00
|
|
|
return c.labels[labelWithType{name: name, typ: typ}]
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:00:23 +00:00
|
|
|
// For `&&` and `||` it return an opcode which jumps only if result is known:
|
|
|
|
// false && .. == false, true || .. = true
|
|
|
|
func getJumpForToken(tok token.Token, typ types.Type) (opcode.Opcode, bool) {
|
|
|
|
switch tok {
|
|
|
|
case token.GTR:
|
|
|
|
return opcode.JMPGTL, true
|
|
|
|
case token.GEQ:
|
|
|
|
return opcode.JMPGEL, true
|
|
|
|
case token.LSS:
|
|
|
|
return opcode.JMPLTL, true
|
|
|
|
case token.LEQ:
|
|
|
|
return opcode.JMPLEL, true
|
|
|
|
case token.EQL, token.NEQ:
|
|
|
|
if isNumber(typ) {
|
|
|
|
if tok == token.EQL {
|
|
|
|
return opcode.JMPEQL, true
|
|
|
|
}
|
|
|
|
return opcode.JMPNEL, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
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:
|
2020-05-13 15:09:55 +00:00
|
|
|
if !isByteSlice(c.typeOf(t.Type)) {
|
2020-01-27 07:59:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(t.Elts))
|
|
|
|
for i := 0; i < len(t.Elts); i++ {
|
2020-05-13 15:09:55 +00:00
|
|
|
t := c.typeAndValueOf(t.Elts[i])
|
2020-01-27 07:59:57 +00:00
|
|
|
val, _ := constant.Int64Val(t.Value)
|
|
|
|
buf[i] = byte(val)
|
|
|
|
}
|
|
|
|
return buf
|
2020-01-27 12:30:36 +00:00
|
|
|
case *ast.CallExpr:
|
2020-05-13 15:09:55 +00:00
|
|
|
if tv := c.typeAndValueOf(t.Args[0]); tv.Value != nil {
|
2020-01-27 12:30:36 +00:00
|
|
|
val := constant.StringVal(tv.Value)
|
|
|
|
return []byte(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-01-27 07:59:57 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:05:40 +00:00
|
|
|
func (c *codegen) convertSyscall(expr *ast.CallExpr, api, name string) {
|
2020-07-16 09:39:54 +00:00
|
|
|
syscall, ok := syscalls[api][name]
|
2018-02-24 09:06:48 +00:00
|
|
|
if !ok {
|
2020-07-16 09:39:54 +00:00
|
|
|
c.prog.Err = fmt.Errorf("unknown VM syscall api: %s.%s", api, name)
|
2019-11-22 14:16:52 +00:00
|
|
|
return
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
2020-09-08 10:41:47 +00:00
|
|
|
emit.Syscall(c.prog.BinWriter, syscall)
|
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.
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NOP)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 09:54:01 +00:00
|
|
|
// emitSliceHelper emits 3 items on stack: slice, its first index, and its size.
|
|
|
|
func (c *codegen) emitSliceHelper(e ast.Expr) {
|
|
|
|
if !isByteSlice(c.typeOf(e)) {
|
|
|
|
c.prog.Err = fmt.Errorf("copy is supported only for byte-slices")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var hasLowIndex bool
|
|
|
|
switch src := e.(type) {
|
|
|
|
case *ast.SliceExpr:
|
|
|
|
ast.Walk(c, src.X)
|
|
|
|
if src.High != nil {
|
|
|
|
ast.Walk(c, src.High)
|
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP, opcode.SIZE)
|
2020-08-24 09:54:01 +00:00
|
|
|
}
|
|
|
|
if src.Low != nil {
|
|
|
|
ast.Walk(c, src.Low)
|
|
|
|
hasLowIndex = true
|
|
|
|
} else {
|
|
|
|
emit.Int(c.prog.BinWriter, 0)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ast.Walk(c, src)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP, opcode.SIZE)
|
2020-08-24 09:54:01 +00:00
|
|
|
emit.Int(c.prog.BinWriter, 0)
|
|
|
|
}
|
|
|
|
if !hasLowIndex {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SWAP)
|
2020-08-24 09:54:01 +00:00
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP, opcode.ROT, opcode.SWAP, opcode.SUB)
|
2020-08-24 09:54:01 +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 {
|
2020-08-24 09:54:01 +00:00
|
|
|
case "copy":
|
|
|
|
// stack for MEMCPY is: dst, dst_index, src, src_index, count
|
|
|
|
c.emitSliceHelper(expr.Args[0])
|
|
|
|
c.emitSliceHelper(expr.Args[1])
|
|
|
|
emit.Int(c.prog.BinWriter, 3)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.ROLL, opcode.MIN)
|
2020-09-02 12:29:59 +00:00
|
|
|
if !c.scope.voidCalls[expr] {
|
|
|
|
// insert top item to the bottom of MEMCPY args, so that it is left on stack
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-09-02 12:29:59 +00:00
|
|
|
emit.Int(c.prog.BinWriter, 6)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REVERSEN)
|
2020-09-02 12:29:59 +00:00
|
|
|
emit.Int(c.prog.BinWriter, 5)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REVERSEN)
|
2020-09-02 12:29:59 +00:00
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.MEMCPY)
|
2020-08-24 08:58:29 +00:00
|
|
|
case "make":
|
|
|
|
typ := c.typeOf(expr.Args[0])
|
|
|
|
switch {
|
|
|
|
case isMap(typ):
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWMAP)
|
2020-08-24 08:58:29 +00:00
|
|
|
default:
|
|
|
|
if len(expr.Args) == 3 {
|
|
|
|
c.prog.Err = fmt.Errorf("`make()` with a capacity argument is not supported")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ast.Walk(c, expr.Args[1])
|
|
|
|
if isByteSlice(typ) {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWBUFFER)
|
2020-08-24 08:58:29 +00:00
|
|
|
} else {
|
|
|
|
neoT := toNeoType(typ.(*types.Slice).Elem())
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.NEWARRAYT, []byte{byte(neoT)})
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
case "len":
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP, opcode.ISNULL)
|
2020-07-07 18:49:21 +00:00
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.JMPIF, []byte{2 + 1 + 2})
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SIZE)
|
2020-07-07 18:49:21 +00:00
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.JMP, []byte{2 + 1 + 1})
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP, opcode.PUSH0)
|
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
|
2020-07-09 10:51:49 +00:00
|
|
|
c.emitReverse(len(expr.Args))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP, opcode.ISNULL)
|
2020-07-09 10:51:49 +00:00
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.JMPIFNOT, []byte{2 + 3})
|
2020-05-13 15:09:55 +00:00
|
|
|
if isByteSlice(typ) {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP, opcode.PUSH0, opcode.NEWBUFFER)
|
2019-09-12 14:39:23 +00:00
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP, opcode.NEWARRAY0, opcode.NOP)
|
2020-07-09 10:51:49 +00:00
|
|
|
}
|
|
|
|
// Jump target.
|
|
|
|
for range expr.Args[1:] {
|
|
|
|
if isByteSlice(typ) {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SWAP, opcode.CAT)
|
2020-07-09 10:51:49 +00:00
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP, opcode.ROT, opcode.APPEND)
|
2020-07-09 10:51:49 +00:00
|
|
|
}
|
2019-09-12 14:39:23 +00:00
|
|
|
}
|
2020-01-28 14:18:38 +00:00
|
|
|
case "panic":
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.THROW)
|
2020-08-21 12:37:46 +00:00
|
|
|
case "recover":
|
2020-09-02 12:20:11 +00:00
|
|
|
if !c.scope.voidCalls[expr] {
|
|
|
|
c.emitLoadByIndex(varGlobal, c.exceptionIndex)
|
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.PUSHNULL)
|
2020-08-21 12:37:46 +00:00
|
|
|
c.emitStoreByIndex(varGlobal, c.exceptionIndex)
|
2020-09-06 12:49:41 +00:00
|
|
|
case "delete":
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REMOVE)
|
2020-04-28 12:46:03 +00:00
|
|
|
case "ToInteger", "ToByteArray", "ToBool":
|
2020-06-03 12:55:06 +00:00
|
|
|
typ := stackitem.IntegerT
|
2020-04-28 12:46:03 +00:00
|
|
|
switch name {
|
|
|
|
case "ToByteArray":
|
2020-06-03 12:55:06 +00:00
|
|
|
typ = stackitem.ByteArrayT
|
2020-04-28 12:46:03 +00:00
|
|
|
case "ToBool":
|
2020-06-03 12:55:06 +00:00
|
|
|
typ = stackitem.BooleanT
|
2020-04-28 12:46:03 +00:00
|
|
|
}
|
2020-05-13 08:06:12 +00:00
|
|
|
c.emitConvert(typ)
|
2020-09-15 07:05:41 +00:00
|
|
|
case "Remove":
|
|
|
|
if !isCompoundSlice(c.typeOf(expr.Args[0])) {
|
|
|
|
c.prog.Err = errors.New("`Remove` supports only non-byte slices")
|
|
|
|
return
|
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.REMOVE)
|
2018-08-23 17:44:17 +00:00
|
|
|
case "Equals":
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(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()
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.Bytes(c.prog.BinWriter, bytes)
|
2020-06-03 12:55:06 +00:00
|
|
|
c.emitConvert(stackitem.BufferT)
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 07:08:24 +00:00
|
|
|
// transformArgs returns a list of function arguments
|
|
|
|
// which should be put on stack.
|
|
|
|
// There are special cases for builtins:
|
2020-05-07 11:38:19 +00:00
|
|
|
// 1. With FromAddress, parameter conversion is happening at compile-time
|
2020-01-29 07:08:24 +00:00
|
|
|
// so there is no need to push parameters on stack and perform an actual call
|
2020-05-07 11:38:19 +00:00
|
|
|
// 2. With panic, generated code depends on if argument was nil or a string so
|
2020-01-29 07:08:24 +00:00
|
|
|
// it should be handled accordingly.
|
|
|
|
func transformArgs(fun ast.Expr, args []ast.Expr) []ast.Expr {
|
|
|
|
switch f := fun.(type) {
|
|
|
|
case *ast.SelectorExpr:
|
2020-05-07 11:38:19 +00:00
|
|
|
if f.Sel.Name == "FromAddress" {
|
2020-01-29 07:08:24 +00:00
|
|
|
return args[1:]
|
|
|
|
}
|
|
|
|
case *ast.Ident:
|
2020-08-24 09:54:01 +00:00
|
|
|
switch f.Name {
|
|
|
|
case "make", "copy":
|
2020-08-24 08:58:29 +00:00
|
|
|
return nil
|
2020-01-29 07:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2020-05-13 08:06:12 +00:00
|
|
|
// emitConvert converts top stack item to the specified type.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (c *codegen) emitConvert(typ stackitem.Type) {
|
2020-05-13 08:06:12 +00:00
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)})
|
|
|
|
}
|
|
|
|
|
2018-02-27 09:04:24 +00:00
|
|
|
func (c *codegen) convertByteArray(lit *ast.CompositeLit) {
|
|
|
|
buf := make([]byte, len(lit.Elts))
|
2020-09-24 17:20:34 +00:00
|
|
|
varIndices := []int{}
|
2018-02-27 09:04:24 +00:00
|
|
|
for i := 0; i < len(lit.Elts); i++ {
|
2020-05-13 15:09:55 +00:00
|
|
|
t := c.typeAndValueOf(lit.Elts[i])
|
2020-09-24 17:20:34 +00:00
|
|
|
if t.Value != nil {
|
|
|
|
val, _ := constant.Int64Val(t.Value)
|
|
|
|
buf[i] = byte(val)
|
|
|
|
} else {
|
|
|
|
varIndices = append(varIndices, i)
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.Bytes(c.prog.BinWriter, buf)
|
2020-06-03 12:55:06 +00:00
|
|
|
c.emitConvert(stackitem.BufferT)
|
2020-09-24 17:20:34 +00:00
|
|
|
for _, i := range varIndices {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-09-24 17:20:34 +00:00
|
|
|
emit.Int(c.prog.BinWriter, int64(i))
|
|
|
|
ast.Walk(c, lit.Elts[i])
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SETITEM)
|
2020-09-24 17:20:34 +00:00
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:06:15 +00:00
|
|
|
func (c *codegen) convertMap(lit *ast.CompositeLit) {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWMAP)
|
2020-01-23 14:06:15 +00:00
|
|
|
for i := range lit.Elts {
|
|
|
|
elem := lit.Elts[i].(*ast.KeyValueExpr)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-01-23 14:06:15 +00:00
|
|
|
ast.Walk(c, elem.Key)
|
|
|
|
ast.Walk(c, elem.Value)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SETITEM)
|
2020-01-23 14:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 11:42:29 +00:00
|
|
|
func (c *codegen) getStruct(typ types.Type) (*types.Struct, bool) {
|
|
|
|
switch t := typ.Underlying().(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
return t, true
|
|
|
|
case *types.Pointer:
|
|
|
|
strct, ok := t.Elem().Underlying().(*types.Struct)
|
|
|
|
return strct, ok
|
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) convertStruct(lit *ast.CompositeLit, ptr bool) {
|
2018-02-24 09:06:48 +00:00
|
|
|
// Create a new structScope to initialize and store
|
|
|
|
// the positions of its variables.
|
2020-05-13 15:09:55 +00:00
|
|
|
strct, ok := c.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
|
|
|
}
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NOP)
|
2020-02-03 14:58:21 +00:00
|
|
|
emit.Int(c.prog.BinWriter, int64(strct.NumFields()))
|
2020-08-02 11:42:29 +00:00
|
|
|
if ptr {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWARRAY)
|
2020-08-02 11:42:29 +00:00
|
|
|
} else {
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.NEWSTRUCT)
|
2020-08-02 11:42:29 +00:00
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-06-24 16:33:58 +00:00
|
|
|
keyedLit := len(lit.Elts) > 0
|
|
|
|
if keyedLit {
|
|
|
|
_, ok := lit.Elts[0].(*ast.KeyValueExpr)
|
|
|
|
keyedLit = keyedLit && ok
|
|
|
|
}
|
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)
|
2020-07-09 09:26:16 +00:00
|
|
|
var initialized bool
|
2018-02-24 09:06:48 +00:00
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
2020-07-09 09:26:16 +00:00
|
|
|
emit.Int(c.prog.BinWriter, int64(i))
|
2020-02-11 08:10:51 +00:00
|
|
|
|
2020-07-09 09:26:16 +00:00
|
|
|
if !keyedLit {
|
|
|
|
if len(lit.Elts) > i {
|
|
|
|
ast.Walk(c, lit.Elts[i])
|
|
|
|
initialized = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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)
|
|
|
|
initialized = true
|
|
|
|
break
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 09:26:16 +00:00
|
|
|
if !initialized {
|
|
|
|
c.emitDefault(sField.Type())
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.SETITEM)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 09:39:58 +00:00
|
|
|
func (c *codegen) emitToken(tok token.Token, typ types.Type) {
|
|
|
|
op, err := convertToken(tok, typ)
|
|
|
|
if err != nil {
|
|
|
|
c.prog.Err = err
|
|
|
|
return
|
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, op)
|
2020-08-23 09:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func convertToken(tok token.Token, typ types.Type) (opcode.Opcode, error) {
|
2018-02-24 09:06:48 +00:00
|
|
|
switch tok {
|
2020-08-23 10:12:10 +00:00
|
|
|
case token.ADD_ASSIGN, token.ADD:
|
|
|
|
// VM has separate opcodes for number and string concatenation
|
|
|
|
if isString(typ) {
|
|
|
|
return opcode.CAT, nil
|
|
|
|
}
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.ADD, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.SUB_ASSIGN:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.SUB, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.MUL_ASSIGN:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.MUL, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.QUO_ASSIGN:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.DIV, nil
|
2019-12-19 10:11:05 +00:00
|
|
|
case token.REM_ASSIGN:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.MOD, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.SUB:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.SUB, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.MUL:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.MUL, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.QUO:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.DIV, nil
|
2019-12-19 10:11:05 +00:00
|
|
|
case token.REM:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.MOD, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.LSS:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.LT, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.LEQ:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.LTE, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.GTR:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.GT, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
case token.GEQ:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.GTE, nil
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.EQL:
|
2020-08-23 09:39:58 +00:00
|
|
|
// VM has separate opcodes for number and string equality
|
|
|
|
if isNumber(typ) {
|
|
|
|
return opcode.NUMEQUAL, nil
|
|
|
|
}
|
|
|
|
return opcode.EQUAL, nil
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.NEQ:
|
2020-08-23 09:39:58 +00:00
|
|
|
// VM has separate opcodes for number and string equality
|
|
|
|
if isNumber(typ) {
|
|
|
|
return opcode.NUMNOTEQUAL, nil
|
|
|
|
}
|
|
|
|
return opcode.NOTEQUAL, nil
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.DEC:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.DEC, nil
|
2018-04-02 15:04:42 +00:00
|
|
|
case token.INC:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.INC, nil
|
2018-04-04 19:41:19 +00:00
|
|
|
case token.NOT:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.NOT, nil
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.AND:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.AND, nil
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.OR:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.OR, nil
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.SHL:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.SHL, nil
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.SHR:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.SHR, nil
|
2018-10-26 14:02:32 +00:00
|
|
|
case token.XOR:
|
2020-08-23 09:39:58 +00:00
|
|
|
return opcode.XOR, nil
|
2018-02-24 09:06:48 +00:00
|
|
|
default:
|
2020-08-23 09:39:58 +00:00
|
|
|
return 0, fmt.Errorf("compiler could not convert token: %s", tok)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
func (c *codegen) newFunc(decl *ast.FuncDecl) *funcScope {
|
2020-07-29 14:20:00 +00:00
|
|
|
f := c.newFuncScope(decl, c.newLabel())
|
|
|
|
c.funcs[c.getFuncNameFromDecl("", decl)] = f
|
2018-02-19 09:24:28 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-07-29 14:20:00 +00:00
|
|
|
// getFuncNameFromSelector returns fully-qualified function name from the selector expression.
|
|
|
|
// Second return value is true iff this was a method call, not foreign package call.
|
|
|
|
func (c *codegen) getFuncNameFromSelector(e *ast.SelectorExpr) (string, bool) {
|
|
|
|
ident := e.X.(*ast.Ident)
|
|
|
|
if c.typeInfo.Selections[e] != nil {
|
|
|
|
typ := c.typeInfo.Types[ident].Type.String()
|
|
|
|
return c.getIdentName(typ, e.Sel.Name), true
|
|
|
|
}
|
|
|
|
return c.getIdentName(ident.Name, e.Sel.Name), false
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:10:20 +00:00
|
|
|
func (c *codegen) newLambda(u uint16, lit *ast.FuncLit) {
|
|
|
|
name := fmt.Sprintf("lambda@%d", u)
|
2020-07-29 14:20:00 +00:00
|
|
|
f := c.newFuncScope(&ast.FuncDecl{
|
2020-05-06 15:10:20 +00:00
|
|
|
Name: ast.NewIdent(name),
|
|
|
|
Type: lit.Type,
|
|
|
|
Body: lit.Body,
|
|
|
|
}, u)
|
2020-07-29 14:20:00 +00:00
|
|
|
c.lambda[c.getFuncNameFromDecl("", f.decl)] = f
|
2020-05-06 15:10:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:16:32 +00:00
|
|
|
func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error {
|
2020-07-28 15:40:41 +00:00
|
|
|
c.mainPkg = pkg
|
2020-08-05 07:56:36 +00:00
|
|
|
c.analyzePkgOrder()
|
2020-08-10 10:10:35 +00:00
|
|
|
c.fillDocumentInfo()
|
2020-07-28 15:40:41 +00:00
|
|
|
funUsage := c.analyzeFuncUsage()
|
2018-02-27 09:04:24 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Bring all imported functions into scope.
|
2020-07-28 15:40:41 +00:00
|
|
|
c.ForEachFile(c.resolveFuncDecls)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-10-02 10:16:02 +00:00
|
|
|
n, initLocals, deployLocals := c.traverseGlobals()
|
2020-10-06 15:25:40 +00:00
|
|
|
hasInit := initLocals > -1
|
2020-08-05 09:59:50 +00:00
|
|
|
if n > 0 || hasInit {
|
2020-07-24 10:40:54 +00:00
|
|
|
c.initEndOffset = c.prog.Len()
|
2020-10-02 10:16:02 +00:00
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.RET)
|
|
|
|
}
|
|
|
|
|
|
|
|
hasDeploy := deployLocals > -1
|
|
|
|
if hasDeploy {
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.INITSLOT, []byte{byte(deployLocals), 1})
|
|
|
|
c.convertDeployFuncs()
|
|
|
|
c.deployEndOffset = c.prog.Len()
|
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.RET)
|
2020-07-24 10:40:54 +00:00
|
|
|
}
|
2018-02-25 12:26:56 +00:00
|
|
|
|
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.
|
2020-07-28 15:40:41 +00:00
|
|
|
c.ForEachFile(func(f *ast.File, pkg *types.Package) {
|
|
|
|
for _, decl := range f.Decls {
|
|
|
|
switch n := decl.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
// Don't convert the function if it's not used. This will save a lot
|
|
|
|
// of bytecode space.
|
2020-07-29 14:20:00 +00:00
|
|
|
name := c.getFuncNameFromDecl(pkg.Path(), n)
|
2020-10-02 10:16:02 +00:00
|
|
|
if !isInitFunc(n) && !isDeployFunc(n) && funUsage.funcUsed(name) && !isInteropPath(pkg.Path()) {
|
2020-07-28 15:40:41 +00:00
|
|
|
c.convertFuncDecl(f, n, pkg)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 15:40:41 +00:00
|
|
|
})
|
2018-02-24 09:06:48 +00:00
|
|
|
|
2020-03-31 13:16:32 +00:00
|
|
|
return c.prog.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCodegen(info *buildInfo, pkg *loader.PackageInfo) *codegen {
|
|
|
|
return &codegen{
|
|
|
|
buildInfo: info,
|
|
|
|
prog: io.NewBufBinWriter(),
|
|
|
|
l: []int{},
|
|
|
|
funcs: map[string]*funcScope{},
|
2020-05-06 15:10:20 +00:00
|
|
|
lambda: map[string]*funcScope{},
|
2020-05-07 08:54:35 +00:00
|
|
|
globals: map[string]int{},
|
2020-03-31 13:16:32 +00:00
|
|
|
labels: map[labelWithType]uint16{},
|
|
|
|
typeInfo: &pkg.Info,
|
2020-07-28 16:50:44 +00:00
|
|
|
constMap: map[string]types.TypeAndValue{},
|
2020-08-10 10:10:35 +00:00
|
|
|
docIndex: map[string]int{},
|
2020-03-31 13:57:35 +00:00
|
|
|
|
2020-10-02 10:16:02 +00:00
|
|
|
initEndOffset: -1,
|
|
|
|
deployEndOffset: -1,
|
|
|
|
|
2020-11-24 13:36:35 +00:00
|
|
|
emittedEvents: make(map[string][][]string),
|
2020-03-31 13:57:35 +00:00
|
|
|
sequencePoints: make(map[string][]DebugSeqPoint),
|
2020-03-31 13:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CodeGen compiles the program to bytecode.
|
2020-04-02 12:38:53 +00:00
|
|
|
func CodeGen(info *buildInfo) ([]byte, *DebugInfo, error) {
|
2020-03-31 13:16:32 +00:00
|
|
|
pkg := info.program.Package(info.initialPackage)
|
|
|
|
c := newCodegen(info, pkg)
|
|
|
|
|
|
|
|
if err := c.compile(info, pkg); err != nil {
|
2020-04-02 12:38:53 +00:00
|
|
|
return nil, nil, err
|
2019-11-22 10:06:32 +00:00
|
|
|
}
|
2020-03-31 13:16:32 +00:00
|
|
|
|
2020-08-19 09:36:17 +00:00
|
|
|
buf, err := c.writeJumps(c.prog.Bytes())
|
|
|
|
if err != nil {
|
2020-04-02 12:38:53 +00:00
|
|
|
return nil, nil, err
|
2020-02-21 08:57:24 +00:00
|
|
|
}
|
2020-06-24 04:22:33 +00:00
|
|
|
return buf, c.emitDebugInfo(buf), nil
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 11:16:41 +00:00
|
|
|
func (c *codegen) resolveFuncDecls(f *ast.File, pkg *types.Package) {
|
2018-02-19 09:24:28 +00:00
|
|
|
for _, decl := range f.Decls {
|
|
|
|
switch n := decl.(type) {
|
|
|
|
case *ast.FuncDecl:
|
2020-07-24 10:40:54 +00:00
|
|
|
c.newFunc(n)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:36:17 +00:00
|
|
|
func (c *codegen) writeJumps(b []byte) ([]byte, error) {
|
2020-02-03 09:10:07 +00:00
|
|
|
ctx := vm.NewContext(b)
|
2020-08-19 09:36:17 +00:00
|
|
|
var offsets []int
|
2020-08-19 10:37:24 +00:00
|
|
|
for op, _, err := ctx.Next(); err == nil && ctx.IP() < len(b); op, _, err = ctx.Next() {
|
2020-02-03 09:10:07 +00:00
|
|
|
switch op {
|
2020-04-23 08:56:36 +00:00
|
|
|
case opcode.JMP, opcode.JMPIFNOT, opcode.JMPIF, opcode.CALL,
|
|
|
|
opcode.JMPEQ, opcode.JMPNE,
|
|
|
|
opcode.JMPGT, opcode.JMPGE, opcode.JMPLE, opcode.JMPLT:
|
2020-08-20 05:34:14 +00:00
|
|
|
case opcode.TRYL:
|
|
|
|
nextIP := ctx.NextIP()
|
2020-08-21 12:37:46 +00:00
|
|
|
catchArg := b[nextIP-8:]
|
|
|
|
_, err := c.replaceLabelWithOffset(ctx.IP(), catchArg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-20 05:34:14 +00:00
|
|
|
finallyArg := b[nextIP-4:]
|
2020-08-21 12:37:46 +00:00
|
|
|
_, err = c.replaceLabelWithOffset(ctx.IP(), finallyArg)
|
2020-08-20 05:34:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-23 08:56:36 +00:00
|
|
|
case opcode.JMPL, opcode.JMPIFL, opcode.JMPIFNOTL,
|
|
|
|
opcode.JMPEQL, opcode.JMPNEL,
|
|
|
|
opcode.JMPGTL, opcode.JMPGEL, opcode.JMPLEL, opcode.JMPLTL,
|
2020-08-20 05:34:14 +00:00
|
|
|
opcode.CALLL, opcode.PUSHA, opcode.ENDTRYL:
|
2020-02-03 09:10:07 +00:00
|
|
|
// we can't use arg returned by ctx.Next() because it is copied
|
2020-02-21 08:57:24 +00:00
|
|
|
nextIP := ctx.NextIP()
|
2020-04-23 08:56:36 +00:00
|
|
|
arg := b[nextIP-4:]
|
2020-08-20 05:34:14 +00:00
|
|
|
offset, err := c.replaceLabelWithOffset(ctx.IP(), arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2020-08-19 09:36:17 +00:00
|
|
|
if op != opcode.PUSHA && math.MinInt8 <= offset && offset <= math.MaxInt8 {
|
|
|
|
offsets = append(offsets, ctx.IP())
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-19 09:36:17 +00:00
|
|
|
// Correct function ip range.
|
|
|
|
// Note: indices are sorted in increasing order.
|
|
|
|
for _, f := range c.funcs {
|
2020-10-14 11:34:53 +00:00
|
|
|
start, end := f.rng.Start, f.rng.End
|
2020-08-19 09:36:17 +00:00
|
|
|
loop:
|
|
|
|
for _, ind := range offsets {
|
|
|
|
switch {
|
|
|
|
case ind > int(f.rng.End):
|
|
|
|
break loop
|
|
|
|
case ind < int(f.rng.Start):
|
2020-10-14 11:34:53 +00:00
|
|
|
start -= longToShortRemoveCount
|
|
|
|
end -= longToShortRemoveCount
|
2020-08-19 09:36:17 +00:00
|
|
|
case ind >= int(f.rng.Start):
|
2020-10-14 11:34:53 +00:00
|
|
|
end -= longToShortRemoveCount
|
2020-08-19 09:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-14 11:34:53 +00:00
|
|
|
f.rng.Start = start
|
|
|
|
f.rng.End = end
|
2020-08-19 09:36:17 +00:00
|
|
|
}
|
|
|
|
return shortenJumps(b, offsets), nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 05:34:14 +00:00
|
|
|
func (c *codegen) replaceLabelWithOffset(ip int, arg []byte) (int, error) {
|
|
|
|
index := binary.LittleEndian.Uint16(arg)
|
|
|
|
if int(index) > len(c.l) {
|
|
|
|
return 0, fmt.Errorf("unexpected label number: %d (max %d)", index, len(c.l))
|
|
|
|
}
|
|
|
|
offset := c.l[index] - ip
|
|
|
|
if offset > math.MaxInt32 || offset < math.MinInt32 {
|
|
|
|
return 0, fmt.Errorf("label offset is too big at the instruction %d: %d (max %d, min %d)",
|
|
|
|
ip, offset, math.MaxInt32, math.MinInt32)
|
|
|
|
}
|
|
|
|
binary.LittleEndian.PutUint32(arg, uint32(offset))
|
|
|
|
return offset, nil
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:36:17 +00:00
|
|
|
// longToShortRemoveCount is a difference between short and long instruction sizes in bytes.
|
|
|
|
const longToShortRemoveCount = 3
|
|
|
|
|
|
|
|
// shortenJumps returns converts b to a program where all long JMP*/CALL* specified by absolute offsets,
|
|
|
|
// are replaced with their corresponding short counterparts. It panics if either b or offsets are invalid.
|
|
|
|
// This is done in 2 passes:
|
|
|
|
// 1. Alter jump offsets taking into account parts to be removed.
|
|
|
|
// 2. Perform actual removal of jump targets.
|
|
|
|
// Note: after jump offsets altering, there can appear new candidates for conversion.
|
|
|
|
// These are ignored for now.
|
|
|
|
func shortenJumps(b []byte, offsets []int) []byte {
|
|
|
|
if len(offsets) == 0 {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Alter existing jump offsets.
|
|
|
|
ctx := vm.NewContext(b)
|
|
|
|
for op, _, err := ctx.Next(); err == nil && ctx.IP() < len(b); op, _, err = ctx.Next() {
|
|
|
|
// we can't use arg returned by ctx.Next() because it is copied
|
|
|
|
nextIP := ctx.NextIP()
|
|
|
|
ip := ctx.IP()
|
|
|
|
switch op {
|
|
|
|
case opcode.JMP, opcode.JMPIFNOT, opcode.JMPIF, opcode.CALL,
|
|
|
|
opcode.JMPEQ, opcode.JMPNE,
|
2020-08-20 05:34:14 +00:00
|
|
|
opcode.JMPGT, opcode.JMPGE, opcode.JMPLE, opcode.JMPLT, opcode.ENDTRY:
|
2020-08-19 09:36:17 +00:00
|
|
|
offset := int(int8(b[nextIP-1]))
|
|
|
|
offset += calcOffsetCorrection(ip, ip+offset, offsets)
|
|
|
|
b[nextIP-1] = byte(offset)
|
2020-08-20 05:34:14 +00:00
|
|
|
case opcode.TRY:
|
2020-08-21 12:37:46 +00:00
|
|
|
catchOffset := int(int8(b[nextIP-2]))
|
|
|
|
catchOffset += calcOffsetCorrection(ip, ip+catchOffset, offsets)
|
|
|
|
b[nextIP-1] = byte(catchOffset)
|
2020-08-20 05:34:14 +00:00
|
|
|
finallyOffset := int(int8(b[nextIP-1]))
|
|
|
|
finallyOffset += calcOffsetCorrection(ip, ip+finallyOffset, offsets)
|
|
|
|
b[nextIP-1] = byte(finallyOffset)
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPL, opcode.JMPIFL, opcode.JMPIFNOTL,
|
|
|
|
opcode.JMPEQL, opcode.JMPNEL,
|
|
|
|
opcode.JMPGTL, opcode.JMPGEL, opcode.JMPLEL, opcode.JMPLTL,
|
2020-08-20 05:34:14 +00:00
|
|
|
opcode.CALLL, opcode.PUSHA, opcode.ENDTRYL:
|
2020-08-19 09:36:17 +00:00
|
|
|
arg := b[nextIP-4:]
|
|
|
|
offset := int(int32(binary.LittleEndian.Uint32(arg)))
|
|
|
|
offset += calcOffsetCorrection(ip, ip+offset, offsets)
|
|
|
|
binary.LittleEndian.PutUint32(arg, uint32(offset))
|
2020-08-20 05:34:14 +00:00
|
|
|
case opcode.TRYL:
|
2020-08-21 12:37:46 +00:00
|
|
|
arg := b[nextIP-8:]
|
|
|
|
catchOffset := int(int32(binary.LittleEndian.Uint32(arg)))
|
|
|
|
catchOffset += calcOffsetCorrection(ip, ip+catchOffset, offsets)
|
|
|
|
binary.LittleEndian.PutUint32(arg, uint32(catchOffset))
|
|
|
|
arg = b[nextIP-4:]
|
2020-08-20 05:34:14 +00:00
|
|
|
finallyOffset := int(int32(binary.LittleEndian.Uint32(arg)))
|
|
|
|
finallyOffset += calcOffsetCorrection(ip, ip+finallyOffset, offsets)
|
|
|
|
binary.LittleEndian.PutUint32(arg, uint32(finallyOffset))
|
2020-08-19 09:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Convert instructions.
|
|
|
|
copyOffset := 0
|
|
|
|
l := len(offsets)
|
2020-08-23 10:56:55 +00:00
|
|
|
b[offsets[0]] = byte(toShortForm(opcode.Opcode(b[offsets[0]])))
|
2020-08-19 09:36:17 +00:00
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
start := offsets[i] + 2
|
|
|
|
end := len(b)
|
|
|
|
if i != l-1 {
|
|
|
|
end = offsets[i+1] + 2
|
2020-08-23 10:56:55 +00:00
|
|
|
b[offsets[i+1]] = byte(toShortForm(opcode.Opcode(b[offsets[i+1]])))
|
2020-08-19 09:36:17 +00:00
|
|
|
}
|
|
|
|
copy(b[start-copyOffset:], b[start+3:end])
|
|
|
|
copyOffset += longToShortRemoveCount
|
|
|
|
}
|
|
|
|
return b[:len(b)-copyOffset]
|
|
|
|
}
|
|
|
|
|
|
|
|
func calcOffsetCorrection(ip, target int, offsets []int) int {
|
|
|
|
cnt := 0
|
|
|
|
start := sort.Search(len(offsets), func(i int) bool {
|
|
|
|
return offsets[i] >= ip || offsets[i] >= target
|
|
|
|
})
|
|
|
|
for i := start; i < len(offsets) && (offsets[i] < target || offsets[i] <= ip); i++ {
|
|
|
|
ind := offsets[i]
|
|
|
|
if ip <= ind && ind < target ||
|
|
|
|
ind != ip && target <= ind && ind <= ip {
|
|
|
|
cnt += longToShortRemoveCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ip < target {
|
|
|
|
return -cnt
|
|
|
|
}
|
|
|
|
return cnt
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:00:23 +00:00
|
|
|
func negateJmp(op opcode.Opcode) opcode.Opcode {
|
|
|
|
switch op {
|
|
|
|
case opcode.JMPIFL:
|
|
|
|
return opcode.JMPIFNOTL
|
|
|
|
case opcode.JMPIFNOTL:
|
|
|
|
return opcode.JMPIFL
|
|
|
|
case opcode.JMPEQL:
|
|
|
|
return opcode.JMPNEL
|
|
|
|
case opcode.JMPNEL:
|
|
|
|
return opcode.JMPEQL
|
|
|
|
case opcode.JMPGTL:
|
|
|
|
return opcode.JMPLEL
|
|
|
|
case opcode.JMPGEL:
|
|
|
|
return opcode.JMPLTL
|
|
|
|
case opcode.JMPLEL:
|
|
|
|
return opcode.JMPGTL
|
|
|
|
case opcode.JMPLTL:
|
|
|
|
return opcode.JMPGEL
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("invalid opcode in negateJmp: %s", op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 10:56:55 +00:00
|
|
|
func toShortForm(op opcode.Opcode) opcode.Opcode {
|
|
|
|
switch op {
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMP
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPIFL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPIF
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPIFNOTL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPIFNOT
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPEQL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPEQ
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPNEL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPNE
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPGTL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPGT
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPGEL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPGE
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPLEL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPLE
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.JMPLTL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.JMPLT
|
2020-08-19 09:36:17 +00:00
|
|
|
case opcode.CALLL:
|
2020-08-23 10:56:55 +00:00
|
|
|
return opcode.CALL
|
2020-08-20 05:34:14 +00:00
|
|
|
case opcode.ENDTRYL:
|
|
|
|
return opcode.ENDTRY
|
2020-08-19 09:36:17 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("invalid opcode: %s", op))
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|