2013-05-17 14:25:48 -06:00
|
|
|
package ssa
|
|
|
|
|
|
|
|
// This file implements the String() methods for all Value and
|
|
|
|
// Instruction types.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// relName returns the name of v relative to i.
|
|
|
|
// In most cases, this is identical to v.Name(), but for references to
|
|
|
|
// Functions (including methods) and Globals, the FullName is used
|
|
|
|
// instead, explicitly package-qualified for cross-package references.
|
|
|
|
//
|
|
|
|
func relName(v Value, i Instruction) string {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case *Global:
|
2013-06-13 12:43:35 -06:00
|
|
|
if i != nil && v.Pkg == i.Parent().Pkg {
|
2013-05-17 14:25:48 -06:00
|
|
|
return v.Name()
|
|
|
|
}
|
|
|
|
return v.FullName()
|
|
|
|
case *Function:
|
|
|
|
var pkg *Package
|
|
|
|
if i != nil {
|
2013-06-13 12:43:35 -06:00
|
|
|
pkg = i.Parent().Pkg
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
return v.fullName(pkg)
|
|
|
|
}
|
|
|
|
return v.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value.String()
|
|
|
|
//
|
|
|
|
// This method is provided only for debugging.
|
|
|
|
// It never appears in disassembly, which uses Value.Name().
|
|
|
|
|
2013-07-16 11:50:08 -06:00
|
|
|
func (v *Const) String() string {
|
2013-05-22 15:56:18 -06:00
|
|
|
return v.Name()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Parameter) String() string {
|
|
|
|
return fmt.Sprintf("parameter %s : %s", v.Name(), v.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Capture) String() string {
|
|
|
|
return fmt.Sprintf("capture %s : %s", v.Name(), v.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Global) String() string {
|
2013-05-22 15:56:18 -06:00
|
|
|
return v.FullName()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Builtin) String() string {
|
2013-05-22 15:56:18 -06:00
|
|
|
return fmt.Sprintf("builtin %s", v.Name())
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Function) String() string {
|
2013-06-26 10:38:08 -06:00
|
|
|
return v.fullName(nil)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullName returns g's package-qualified name.
|
|
|
|
func (g *Global) FullName() string {
|
2013-07-01 13:24:50 -06:00
|
|
|
return fmt.Sprintf("%s.%s", g.Pkg.Object.Path(), g.name)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instruction.String()
|
|
|
|
|
|
|
|
func (v *Alloc) String() string {
|
|
|
|
op := "local"
|
|
|
|
if v.Heap {
|
|
|
|
op = "new"
|
|
|
|
}
|
2013-07-12 22:09:33 -06:00
|
|
|
return fmt.Sprintf("%s %s", op, deref(v.Type()))
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Phi) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("phi [")
|
|
|
|
for i, edge := range v.Edges {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
// Be robust against malformed CFG.
|
|
|
|
blockname := "?"
|
2013-05-30 07:59:17 -06:00
|
|
|
if v.block != nil && i < len(v.block.Preds) {
|
|
|
|
blockname = v.block.Preds[i].String()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
b.WriteString(blockname)
|
|
|
|
b.WriteString(": ")
|
|
|
|
edgeVal := "<nil>" // be robust
|
|
|
|
if edge != nil {
|
|
|
|
edgeVal = relName(edge, v)
|
|
|
|
}
|
|
|
|
b.WriteString(edgeVal)
|
|
|
|
}
|
|
|
|
b.WriteString("]")
|
|
|
|
if v.Comment != "" {
|
|
|
|
b.WriteString(" #")
|
|
|
|
b.WriteString(v.Comment)
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func printCall(v *CallCommon, prefix string, instr Instruction) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString(prefix)
|
|
|
|
if !v.IsInvoke() {
|
2013-07-26 12:06:26 -06:00
|
|
|
b.WriteString(relName(v.Value, instr))
|
2013-05-17 14:25:48 -06:00
|
|
|
} else {
|
2013-07-26 12:06:26 -06:00
|
|
|
fmt.Fprintf(&b, "invoke %s.%s", relName(v.Value, instr), v.Method.Name())
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
b.WriteString("(")
|
|
|
|
for i, arg := range v.Args {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
b.WriteString(relName(arg, instr))
|
|
|
|
}
|
|
|
|
if v.HasEllipsis {
|
|
|
|
b.WriteString("...")
|
|
|
|
}
|
|
|
|
b.WriteString(")")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CallCommon) String() string {
|
|
|
|
return printCall(c, "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Call) String() string {
|
|
|
|
return printCall(&v.Call, "", v)
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
func (v *ChangeType) String() string {
|
|
|
|
return fmt.Sprintf("changetype %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
func (v *BinOp) String() string {
|
|
|
|
return fmt.Sprintf("%s %s %s", relName(v.X, v), v.Op.String(), relName(v.Y, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UnOp) String() string {
|
|
|
|
return fmt.Sprintf("%s%s%s", v.Op, relName(v.X, v), commaOk(v.CommaOk))
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
func (v *Convert) String() string {
|
2013-05-17 14:25:48 -06:00
|
|
|
return fmt.Sprintf("convert %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ChangeInterface) String() string {
|
|
|
|
return fmt.Sprintf("change interface %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeInterface) String() string {
|
2013-06-13 12:43:35 -06:00
|
|
|
return fmt.Sprintf("make %s <- %s (%s)", v.Type(), v.X.Type(), relName(v.X, v))
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeClosure) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
fmt.Fprintf(&b, "make closure %s", relName(v.Fn, v))
|
|
|
|
if v.Bindings != nil {
|
|
|
|
b.WriteString(" [")
|
|
|
|
for i, c := range v.Bindings {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
b.WriteString(relName(c, v))
|
|
|
|
}
|
|
|
|
b.WriteString("]")
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeSlice) String() string {
|
|
|
|
var b bytes.Buffer
|
2013-06-13 12:43:35 -06:00
|
|
|
b.WriteString("make ")
|
2013-05-17 14:25:48 -06:00
|
|
|
b.WriteString(v.Type().String())
|
|
|
|
b.WriteString(" ")
|
|
|
|
b.WriteString(relName(v.Len, v))
|
|
|
|
b.WriteString(" ")
|
|
|
|
b.WriteString(relName(v.Cap, v))
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Slice) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("slice ")
|
|
|
|
b.WriteString(relName(v.X, v))
|
|
|
|
b.WriteString("[")
|
|
|
|
if v.Low != nil {
|
|
|
|
b.WriteString(relName(v.Low, v))
|
|
|
|
}
|
|
|
|
b.WriteString(":")
|
|
|
|
if v.High != nil {
|
|
|
|
b.WriteString(relName(v.High, v))
|
|
|
|
}
|
|
|
|
b.WriteString("]")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeMap) String() string {
|
|
|
|
res := ""
|
|
|
|
if v.Reserve != nil {
|
|
|
|
res = relName(v.Reserve, v)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("make %s %s", v.Type(), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeChan) String() string {
|
|
|
|
return fmt.Sprintf("make %s %s", v.Type(), relName(v.Size, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FieldAddr) String() string {
|
2013-07-12 22:09:33 -06:00
|
|
|
st := deref(v.X.Type()).Underlying().(*types.Struct)
|
2013-05-17 14:25:48 -06:00
|
|
|
// Be robust against a bad index.
|
|
|
|
name := "?"
|
2013-05-17 15:02:47 -06:00
|
|
|
if 0 <= v.Field && v.Field < st.NumFields() {
|
2013-06-04 13:15:41 -06:00
|
|
|
name = st.Field(v.Field).Name()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("&%s.%s [#%d]", relName(v.X, v), name, v.Field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Field) String() string {
|
2013-05-17 15:02:47 -06:00
|
|
|
st := v.X.Type().Underlying().(*types.Struct)
|
2013-05-17 14:25:48 -06:00
|
|
|
// Be robust against a bad index.
|
|
|
|
name := "?"
|
2013-05-17 15:02:47 -06:00
|
|
|
if 0 <= v.Field && v.Field < st.NumFields() {
|
2013-06-04 13:15:41 -06:00
|
|
|
name = st.Field(v.Field).Name()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s [#%d]", relName(v.X, v), name, v.Field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *IndexAddr) String() string {
|
|
|
|
return fmt.Sprintf("&%s[%s]", relName(v.X, v), relName(v.Index, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Index) String() string {
|
|
|
|
return fmt.Sprintf("%s[%s]", relName(v.X, v), relName(v.Index, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Lookup) String() string {
|
|
|
|
return fmt.Sprintf("%s[%s]%s", relName(v.X, v), relName(v.Index, v), commaOk(v.CommaOk))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Range) String() string {
|
|
|
|
return "range " + relName(v.X, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Next) String() string {
|
|
|
|
return "next " + relName(v.Iter, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TypeAssert) String() string {
|
|
|
|
return fmt.Sprintf("typeassert%s %s.(%s)", commaOk(v.CommaOk), relName(v.X, v), v.AssertedType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Extract) String() string {
|
|
|
|
return fmt.Sprintf("extract %s #%d", relName(v.Tuple, v), v.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Jump) String() string {
|
|
|
|
// Be robust against malformed CFG.
|
|
|
|
blockname := "?"
|
2013-05-30 07:59:17 -06:00
|
|
|
if s.block != nil && len(s.block.Succs) == 1 {
|
|
|
|
blockname = s.block.Succs[0].String()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("jump %s", blockname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *If) String() string {
|
|
|
|
// Be robust against malformed CFG.
|
|
|
|
tblockname, fblockname := "?", "?"
|
2013-05-30 07:59:17 -06:00
|
|
|
if s.block != nil && len(s.block.Succs) == 2 {
|
|
|
|
tblockname = s.block.Succs[0].String()
|
|
|
|
fblockname = s.block.Succs[1].String()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("if %s goto %s else %s", relName(s.Cond, s), tblockname, fblockname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Go) String() string {
|
|
|
|
return printCall(&s.Call, "go ", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Panic) String() string {
|
|
|
|
return "panic " + relName(s.X, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Ret) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("ret")
|
|
|
|
for i, r := range s.Results {
|
|
|
|
if i == 0 {
|
|
|
|
b.WriteString(" ")
|
|
|
|
} else {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
b.WriteString(relName(r, s))
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*RunDefers) String() string {
|
|
|
|
return "rundefers"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Send) String() string {
|
|
|
|
return fmt.Sprintf("send %s <- %s", relName(s.Chan, s), relName(s.X, s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Defer) String() string {
|
|
|
|
return printCall(&s.Call, "defer ", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Select) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
for i, st := range s.States {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
if st.Dir == ast.RECV {
|
|
|
|
b.WriteString("<-")
|
|
|
|
b.WriteString(relName(st.Chan, s))
|
|
|
|
} else {
|
|
|
|
b.WriteString(relName(st.Chan, s))
|
|
|
|
b.WriteString("<-")
|
|
|
|
b.WriteString(relName(st.Send, s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
non := ""
|
|
|
|
if !s.Blocking {
|
|
|
|
non = "non"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("select %sblocking [%s]", non, b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) String() string {
|
|
|
|
return fmt.Sprintf("*%s = %s", relName(s.Addr, s), relName(s.Val, s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MapUpdate) String() string {
|
|
|
|
return fmt.Sprintf("%s[%s] = %s", relName(s.Map, s), relName(s.Key, s), relName(s.Value, s))
|
|
|
|
}
|
|
|
|
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
func (s *DebugRef) String() string {
|
|
|
|
p := s.Parent().Prog.Fset.Position(s.pos)
|
|
|
|
return fmt.Sprintf("; %s is %s @ %d:%d", s.X.Name(), s.object, p.Line, p.Column)
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
func (p *Package) String() string {
|
2013-07-01 13:24:50 -06:00
|
|
|
return "package " + p.Object.Path()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) DumpTo(w io.Writer) {
|
2013-05-31 14:14:13 -06:00
|
|
|
fmt.Fprintf(w, "%s:\n", p)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
var names []string
|
|
|
|
maxname := 0
|
|
|
|
for name := range p.Members {
|
|
|
|
if l := len(name); l > maxname {
|
|
|
|
maxname = l
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(names)
|
|
|
|
for _, name := range names {
|
|
|
|
switch mem := p.Members[name].(type) {
|
2013-07-16 11:50:08 -06:00
|
|
|
case *NamedConst:
|
2013-05-17 14:25:48 -06:00
|
|
|
fmt.Fprintf(w, " const %-*s %s = %s\n", maxname, name, mem.Name(), mem.Value.Name())
|
|
|
|
|
|
|
|
case *Function:
|
|
|
|
fmt.Fprintf(w, " func %-*s %s\n", maxname, name, mem.Type())
|
|
|
|
|
|
|
|
case *Type:
|
2013-06-13 12:43:35 -06:00
|
|
|
fmt.Fprintf(w, " type %-*s %s\n", maxname, name, mem.Type().Underlying())
|
2013-07-30 12:28:14 -06:00
|
|
|
// Iterate over the keys of mset(*T) since they
|
|
|
|
// are a superset of mset(T)'s keys.
|
|
|
|
// The keys of a types.MethodSet are sorted (by Id).
|
|
|
|
mset := methodSetOf(mem.Type())
|
|
|
|
pmset := methodSetOf(types.NewPointer(mem.Type()))
|
|
|
|
for i, n := 0, pmset.Len(); i < n; i++ {
|
|
|
|
meth := pmset.At(i)
|
|
|
|
// If the method also exists in mset(T), show that instead.
|
|
|
|
if m := mset.Lookup(meth.Obj().Pkg(), meth.Obj().Name()); m != nil {
|
|
|
|
meth = m
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, " %s\n", meth)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case *Global:
|
|
|
|
fmt.Fprintf(w, " var %-*s %s\n", maxname, name, mem.Type())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func commaOk(x bool) string {
|
|
|
|
if x {
|
|
|
|
return ",ok"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|