2021-02-04 12:41:00 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2021-06-02 08:46:54 +00:00
|
|
|
"fmt"
|
2021-02-04 12:41:00 +00:00
|
|
|
"go/ast"
|
2021-06-02 08:46:08 +00:00
|
|
|
"go/constant"
|
2021-02-04 12:41:00 +00:00
|
|
|
"go/types"
|
|
|
|
|
2021-06-02 08:46:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
2021-02-04 12:41:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// inlineCall inlines call of n for function represented by f.
|
|
|
|
// Call `f(a,b)` for definition `func f(x,y int)` is translated to block:
|
2021-05-12 20:17:03 +00:00
|
|
|
// {
|
|
|
|
// x := a
|
|
|
|
// y := b
|
|
|
|
// <inline body of f directly>
|
|
|
|
// }
|
2021-02-04 12:41:00 +00:00
|
|
|
func (c *codegen) inlineCall(f *funcScope, n *ast.CallExpr) {
|
2021-02-26 14:58:08 +00:00
|
|
|
labelSz := len(c.labelList)
|
|
|
|
offSz := len(c.inlineLabelOffsets)
|
|
|
|
c.inlineLabelOffsets = append(c.inlineLabelOffsets, labelSz)
|
|
|
|
defer func() {
|
|
|
|
c.inlineLabelOffsets = c.inlineLabelOffsets[:offSz]
|
|
|
|
c.labelList = c.labelList[:labelSz]
|
|
|
|
}()
|
|
|
|
|
2021-02-04 12:41:00 +00:00
|
|
|
pkg := c.buildInfo.program.Package(f.pkg.Path())
|
|
|
|
sig := c.typeOf(n.Fun).(*types.Signature)
|
|
|
|
|
2021-06-02 08:46:08 +00:00
|
|
|
c.processNotify(f, n.Args)
|
|
|
|
|
2021-02-25 12:12:16 +00:00
|
|
|
// When inlined call is used during global initialization
|
|
|
|
// there is no func scope, thus this if.
|
|
|
|
if c.scope == nil {
|
|
|
|
c.scope = &funcScope{}
|
|
|
|
c.scope.vars.newScope()
|
2021-04-28 13:37:31 +00:00
|
|
|
defer func() {
|
|
|
|
if cnt := c.scope.vars.localsCnt; cnt > c.globalInlineCount {
|
|
|
|
c.globalInlineCount = cnt
|
|
|
|
}
|
|
|
|
c.scope = nil
|
|
|
|
}()
|
2021-02-25 12:12:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 12:41:00 +00:00
|
|
|
// Arguments need to be walked with the current scope,
|
|
|
|
// while stored in the new.
|
|
|
|
oldScope := c.scope.vars.locals
|
|
|
|
c.scope.vars.newScope()
|
2021-02-26 15:02:54 +00:00
|
|
|
newScope := make([]map[string]varInfo, len(c.scope.vars.locals))
|
|
|
|
copy(newScope, c.scope.vars.locals)
|
2021-02-04 12:41:00 +00:00
|
|
|
defer c.scope.vars.dropScope()
|
2021-02-08 10:51:25 +00:00
|
|
|
|
|
|
|
hasVarArgs := !n.Ellipsis.IsValid()
|
|
|
|
needPack := sig.Variadic() && hasVarArgs
|
2021-02-04 12:41:00 +00:00
|
|
|
for i := range n.Args {
|
|
|
|
c.scope.vars.locals = oldScope
|
2021-02-08 10:51:25 +00:00
|
|
|
// true if normal arg or var arg is `slice...`
|
|
|
|
needStore := i < sig.Params().Len()-1 || !sig.Variadic() || !hasVarArgs
|
|
|
|
if !needStore {
|
|
|
|
break
|
|
|
|
}
|
2021-02-04 13:26:33 +00:00
|
|
|
name := sig.Params().At(i).Name()
|
2021-05-25 13:02:28 +00:00
|
|
|
if !c.hasCalls(n.Args[i]) {
|
|
|
|
// If argument contains no calls, we save context and traverse the expression
|
|
|
|
// when argument is emitted.
|
2021-02-05 13:15:26 +00:00
|
|
|
c.scope.vars.locals = newScope
|
2021-05-25 13:02:28 +00:00
|
|
|
c.scope.vars.addAlias(name, -1, unspecifiedVarIndex, &varContext{
|
|
|
|
importMap: c.importMap,
|
|
|
|
expr: n.Args[i],
|
|
|
|
scope: oldScope,
|
|
|
|
})
|
2021-02-05 13:15:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-25 13:02:28 +00:00
|
|
|
|
2021-02-04 12:41:00 +00:00
|
|
|
ast.Walk(c, n.Args[i])
|
|
|
|
c.scope.vars.locals = newScope
|
|
|
|
c.scope.newLocal(name)
|
|
|
|
c.emitStoreVar("", name)
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:51:25 +00:00
|
|
|
if needPack {
|
|
|
|
// traverse variadic args and pack them
|
|
|
|
// if they are provided directly i.e. without `...`
|
|
|
|
c.scope.vars.locals = oldScope
|
|
|
|
for i := sig.Params().Len() - 1; i < len(n.Args); i++ {
|
|
|
|
ast.Walk(c, n.Args[i])
|
|
|
|
}
|
|
|
|
c.scope.vars.locals = newScope
|
|
|
|
c.packVarArgs(n, sig)
|
|
|
|
name := sig.Params().At(sig.Params().Len() - 1).Name()
|
|
|
|
c.scope.newLocal(name)
|
|
|
|
c.emitStoreVar("", name)
|
|
|
|
}
|
|
|
|
|
2021-02-04 12:41:00 +00:00
|
|
|
c.pkgInfoInline = append(c.pkgInfoInline, pkg)
|
|
|
|
oldMap := c.importMap
|
|
|
|
c.fillImportMap(f.file, pkg.Pkg)
|
|
|
|
ast.Inspect(f.decl, c.scope.analyzeVoidCalls)
|
|
|
|
ast.Walk(c, f.decl.Body)
|
|
|
|
if c.scope.voidCalls[n] {
|
|
|
|
for i := 0; i < f.decl.Type.Results.NumFields(); i++ {
|
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.DROP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.importMap = oldMap
|
|
|
|
c.pkgInfoInline = c.pkgInfoInline[:len(c.pkgInfoInline)-1]
|
|
|
|
}
|
2021-06-02 08:46:08 +00:00
|
|
|
|
|
|
|
func (c *codegen) processNotify(f *funcScope, args []ast.Expr) {
|
|
|
|
if f != nil && f.pkg.Path() == interopPrefix+"/runtime" && f.name == "Notify" {
|
|
|
|
// Sometimes event name is stored in a var.
|
|
|
|
// Skip in this case.
|
|
|
|
tv := c.typeAndValueOf(args[0])
|
|
|
|
if tv.Value == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make([]string, 0, len(args[1:]))
|
|
|
|
for _, p := range args[1:] {
|
|
|
|
st, _ := c.scAndVMTypeFromExpr(p)
|
|
|
|
params = append(params, st.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
name := constant.StringVal(tv.Value)
|
2021-06-02 08:46:54 +00:00
|
|
|
if len(name) > runtime.MaxEventNameLen {
|
|
|
|
c.prog.Err = fmt.Errorf("event name '%s' should be less than %d",
|
|
|
|
name, runtime.MaxEventNameLen)
|
|
|
|
return
|
|
|
|
}
|
2021-06-02 08:46:08 +00:00
|
|
|
c.emittedEvents[name] = append(c.emittedEvents[name], params)
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 13:02:28 +00:00
|
|
|
|
|
|
|
// hasCalls returns true if expression contains any calls.
|
|
|
|
// We uses this as a rough heuristic to determine if expression calculation
|
|
|
|
// has any side-effects.
|
|
|
|
func (c *codegen) hasCalls(expr ast.Expr) bool {
|
|
|
|
var has bool
|
|
|
|
ast.Inspect(expr, func(n ast.Node) bool {
|
|
|
|
_, ok := n.(*ast.CallExpr)
|
|
|
|
if ok {
|
|
|
|
has = true
|
|
|
|
}
|
|
|
|
return !has
|
|
|
|
})
|
|
|
|
return has
|
|
|
|
}
|