2018-02-19 09:24:28 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
2020-04-01 14:25:08 +00:00
|
|
|
"go/token"
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// A funcScope represents the scope within the function context.
|
2018-02-19 09:24:28 +00:00
|
|
|
// It holds al the local variables along with the initialized struct positions.
|
|
|
|
type funcScope struct {
|
2019-10-22 14:56:03 +00:00
|
|
|
// Identifier of the function.
|
2018-02-19 09:24:28 +00:00
|
|
|
name string
|
|
|
|
|
2018-08-22 07:51:35 +00:00
|
|
|
// Selector of the function if there is any. Only functions imported
|
|
|
|
// from other packages should have a selector.
|
|
|
|
selector *ast.Ident
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// The declaration of the function in the AST. Nil if this scope is not a function.
|
2018-02-19 09:24:28 +00:00
|
|
|
decl *ast.FuncDecl
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// Program label of the scope
|
2020-02-21 08:50:03 +00:00
|
|
|
label uint16
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2020-03-31 12:56:10 +00:00
|
|
|
// Range of opcodes corresponding to the function.
|
|
|
|
rng DebugRange
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// Local variables
|
|
|
|
locals map[string]int
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
// voidCalls are basically functions that return their value
|
|
|
|
// into nothing. The stack has their return value but there
|
|
|
|
// is nothing that consumes it. We need to keep track of
|
|
|
|
// these functions so we can cleanup (drop) the returned
|
|
|
|
// value from the stack. We also need to add every voidCall
|
|
|
|
// return value to the stack size.
|
|
|
|
voidCalls map[*ast.CallExpr]bool
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Local variable counter.
|
2018-02-19 09:24:28 +00:00
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:50:03 +00:00
|
|
|
func newFuncScope(decl *ast.FuncDecl, label uint16) *funcScope {
|
2018-02-19 09:24:28 +00:00
|
|
|
return &funcScope{
|
2018-02-24 09:06:48 +00:00
|
|
|
name: decl.Name.Name,
|
|
|
|
decl: decl,
|
|
|
|
label: label,
|
2018-02-25 12:26:56 +00:00
|
|
|
locals: map[string]int{},
|
2018-02-24 09:06:48 +00:00
|
|
|
voidCalls: map[*ast.CallExpr]bool{},
|
|
|
|
i: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// analyzeVoidCalls checks for functions that are not assigned
|
2018-02-24 09:06:48 +00:00
|
|
|
// and therefore we need to cleanup the return value from the stack.
|
|
|
|
func (c *funcScope) analyzeVoidCalls(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
for i := 0; i < len(n.Rhs); i++ {
|
|
|
|
switch n.Rhs[i].(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *ast.ReturnStmt:
|
|
|
|
switch n.Results[0].(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
return false
|
|
|
|
}
|
2018-02-25 12:26:56 +00:00
|
|
|
case *ast.BinaryExpr:
|
|
|
|
return false
|
2018-02-24 09:06:48 +00:00
|
|
|
case *ast.CallExpr:
|
|
|
|
c.voidCalls[n] = true
|
2018-02-25 12:26:56 +00:00
|
|
|
return false
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
return true
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *funcScope) stackSize() int64 {
|
|
|
|
size := 0
|
|
|
|
ast.Inspect(c.decl, func(n ast.Node) bool {
|
2018-02-24 09:06:48 +00:00
|
|
|
switch n := n.(type) {
|
2018-02-27 09:04:24 +00:00
|
|
|
case *ast.AssignStmt:
|
2020-04-01 14:25:08 +00:00
|
|
|
if n.Tok == token.DEFINE {
|
|
|
|
size += len(n.Rhs)
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
case *ast.ReturnStmt, *ast.IfStmt:
|
2018-02-19 09:24:28 +00:00
|
|
|
size++
|
2018-02-24 09:06:48 +00:00
|
|
|
// This handles the inline GenDecl like "var x = 2"
|
|
|
|
case *ast.GenDecl:
|
|
|
|
switch t := n.Specs[0].(type) {
|
|
|
|
case *ast.ValueSpec:
|
|
|
|
if len(t.Values) > 0 {
|
|
|
|
size++
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
numArgs := len(c.decl.Type.Params.List)
|
|
|
|
// Also take care of struct methods recv: e.g. (t Token).Foo().
|
|
|
|
if c.decl.Recv != nil {
|
|
|
|
numArgs += len(c.decl.Recv.List)
|
|
|
|
}
|
2018-02-24 09:06:48 +00:00
|
|
|
return int64(size + numArgs + len(c.voidCalls))
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newLocal creates a new local variable into the scope of the function.
|
|
|
|
func (c *funcScope) newLocal(name string) int {
|
|
|
|
c.i++
|
2018-02-25 12:26:56 +00:00
|
|
|
c.locals[name] = c.i
|
2018-02-19 09:24:28 +00:00
|
|
|
return c.i
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadLocal loads the position of a local variable inside the scope of the function.
|
|
|
|
func (c *funcScope) loadLocal(name string) int {
|
2018-02-25 12:26:56 +00:00
|
|
|
i, ok := c.locals[name]
|
2018-02-19 09:24:28 +00:00
|
|
|
if !ok {
|
|
|
|
// should emit a compiler warning.
|
|
|
|
return c.newLocal(name)
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|