go.tools/go/ssa: use bytes.Buffer instead of io.Writer.

This is (a) more efficient and (b) avoids the need for
constant error handling, since buffer writes can't fail.

Also:
- added WriteFunction and WritePackage functions,
  similar to types.WriteFoo.
- *Function and *Package now implement io.WriterTo.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/57930044
This commit is contained in:
Alan Donovan 2014-01-28 17:48:10 -05:00
parent 83cdd9ba10
commit 744d7e68b1
11 changed files with 130 additions and 112 deletions

View File

@ -1141,9 +1141,9 @@ func (a *analysis) genFunc(cgn *cgnode) {
fn2 := *cgn.fn // copy fn2 := *cgn.fn // copy
fn2.Locals = nil fn2.Locals = nil
fn2.Blocks = nil fn2.Blocks = nil
fn2.DumpTo(a.log) fn2.WriteTo(a.log)
} else { } else {
cgn.fn.DumpTo(a.log) cgn.fn.WriteTo(a.log)
} }
} }

View File

@ -152,7 +152,7 @@ func optimizeBlocks(f *Function) {
changed = false changed = false
if debugBlockOpt { if debugBlockOpt {
f.DumpTo(os.Stderr) f.WriteTo(os.Stderr)
mustSanityCheck(f, nil) mustSanityCheck(f, nil)
} }

View File

@ -232,7 +232,7 @@ func (prog *Program) CreatePackage(info *loader.PackageInfo) *Package {
} }
if prog.mode&LogPackages != 0 { if prog.mode&LogPackages != 0 {
p.DumpTo(os.Stderr) p.WriteTo(os.Stderr)
} }
if info.Importable { if info.Importable {

View File

@ -18,8 +18,8 @@ package ssa
// to avoid the need for buckets of size > 1. // to avoid the need for buckets of size > 1.
import ( import (
"bytes"
"fmt" "fmt"
"io"
"math/big" "math/big"
"os" "os"
"sort" "sort"
@ -310,32 +310,32 @@ func sanityCheckDomTree(f *Function) {
// Printing functions ---------------------------------------- // Printing functions ----------------------------------------
// printDomTree prints the dominator tree as text, using indentation. // printDomTree prints the dominator tree as text, using indentation.
func printDomTreeText(w io.Writer, v *BasicBlock, indent int) { func printDomTreeText(buf *bytes.Buffer, v *BasicBlock, indent int) {
fmt.Fprintf(w, "%*s%s\n", 4*indent, "", v) fmt.Fprintf(buf, "%*s%s\n", 4*indent, "", v)
for _, child := range v.dom.children { for _, child := range v.dom.children {
printDomTreeText(w, child, indent+1) printDomTreeText(buf, child, indent+1)
} }
} }
// printDomTreeDot prints the dominator tree of f in AT&T GraphViz // printDomTreeDot prints the dominator tree of f in AT&T GraphViz
// (.dot) format. // (.dot) format.
func printDomTreeDot(w io.Writer, f *Function) { func printDomTreeDot(buf *bytes.Buffer, f *Function) {
fmt.Fprintln(w, "//", f) fmt.Fprintln(buf, "//", f)
fmt.Fprintln(w, "digraph domtree {") fmt.Fprintln(buf, "digraph domtree {")
for i, b := range f.Blocks { for i, b := range f.Blocks {
v := b.dom v := b.dom
fmt.Fprintf(w, "\tn%d [label=\"%s (%d, %d)\",shape=\"rectangle\"];\n", v.pre, b, v.pre, v.post) fmt.Fprintf(buf, "\tn%d [label=\"%s (%d, %d)\",shape=\"rectangle\"];\n", v.pre, b, v.pre, v.post)
// TODO(adonovan): improve appearance of edges // TODO(adonovan): improve appearance of edges
// belonging to both dominator tree and CFG. // belonging to both dominator tree and CFG.
// Dominator tree edge. // Dominator tree edge.
if i != 0 { if i != 0 {
fmt.Fprintf(w, "\tn%d -> n%d [style=\"solid\",weight=100];\n", v.idom.dom.pre, v.pre) fmt.Fprintf(buf, "\tn%d -> n%d [style=\"solid\",weight=100];\n", v.idom.dom.pre, v.pre)
} }
// CFG edges. // CFG edges.
for _, pred := range b.Preds { for _, pred := range b.Preds {
fmt.Fprintf(w, "\tn%d -> n%d [style=\"dotted\",weight=0];\n", pred.dom.pre, v.pre) fmt.Fprintf(buf, "\tn%d -> n%d [style=\"dotted\",weight=0];\n", pred.dom.pre, v.pre)
} }
} }
fmt.Fprintln(w, "}") fmt.Fprintln(buf, "}")
} }

View File

@ -64,14 +64,14 @@ func main() {
mainPkg := prog.Package(iprog.Created[0].Pkg) mainPkg := prog.Package(iprog.Created[0].Pkg)
// Print out the package. // Print out the package.
mainPkg.DumpTo(os.Stdout) mainPkg.WriteTo(os.Stdout)
// Build SSA code for bodies of functions in mainPkg. // Build SSA code for bodies of functions in mainPkg.
mainPkg.Build() mainPkg.Build()
// Print out the package-level functions. // Print out the package-level functions.
mainPkg.Func("init").DumpTo(os.Stdout) mainPkg.Func("init").WriteTo(os.Stdout)
mainPkg.Func("main").DumpTo(os.Stdout) mainPkg.Func("main").WriteTo(os.Stdout)
// Output: // Output:
// //

View File

@ -330,7 +330,7 @@ func (f *Function) finishBody() {
if f.Prog.mode&NaiveForm == 0 { if f.Prog.mode&NaiveForm == 0 {
// For debugging pre-state of lifting pass: // For debugging pre-state of lifting pass:
// numberRegisters(f) // numberRegisters(f)
// f.DumpTo(os.Stderr) // f.WriteTo(os.Stderr)
lift(f) lift(f)
} }
@ -339,7 +339,7 @@ func (f *Function) finishBody() {
numberRegisters(f) numberRegisters(f)
if f.Prog.mode&LogFunctions != 0 { if f.Prog.mode&LogFunctions != 0 {
f.DumpTo(os.Stderr) f.WriteTo(os.Stderr)
} }
if f.Prog.mode&SanityCheckFunctions != 0 { if f.Prog.mode&SanityCheckFunctions != 0 {
@ -498,22 +498,20 @@ func (f *Function) RelString(from *types.Package) string {
return f.name return f.name
} }
// writeSignature writes to w the signature sig in declaration syntax. // writeSignature writes to buf the signature sig in declaration syntax.
func writeSignature(w io.Writer, pkg *types.Package, name string, sig *types.Signature, params []*Parameter) { func writeSignature(buf *bytes.Buffer, pkg *types.Package, name string, sig *types.Signature, params []*Parameter) {
io.WriteString(w, "func ") buf.WriteString("func ")
if recv := sig.Recv(); recv != nil { if recv := sig.Recv(); recv != nil {
io.WriteString(w, "(") buf.WriteString("(")
if n := params[0].Name(); n != "" { if n := params[0].Name(); n != "" {
io.WriteString(w, n) buf.WriteString(n)
io.WriteString(w, " ") buf.WriteString(" ")
} }
io.WriteString(w, relType(params[0].Type(), pkg)) buf.WriteString(relType(params[0].Type(), pkg))
io.WriteString(w, ") ") buf.WriteString(") ")
} }
io.WriteString(w, name) buf.WriteString(name)
var sigbuf bytes.Buffer types.WriteSignature(buf, pkg, sig)
types.WriteSignature(&sigbuf, pkg, sig)
w.Write(sigbuf.Bytes())
} }
func (f *Function) pkgobj() *types.Package { func (f *Function) pkgobj() *types.Package {
@ -523,49 +521,56 @@ func (f *Function) pkgobj() *types.Package {
return nil return nil
} }
// DumpTo prints to w a human readable "disassembly" of the SSA code of var _ io.WriterTo = (*Function)(nil) // *Function implements io.Writer
// all basic blocks of function f.
// func (f *Function) WriteTo(w io.Writer) (int64, error) {
func (f *Function) DumpTo(w io.Writer) { var buf bytes.Buffer
fmt.Fprintf(w, "# Name: %s\n", f.String()) WriteFunction(&buf, f)
n, err := w.Write(buf.Bytes())
return int64(n), err
}
// WriteFunction writes to buf a human-readable "disassembly" of f.
func WriteFunction(buf *bytes.Buffer, f *Function) {
fmt.Fprintf(buf, "# Name: %s\n", f.String())
if f.Pkg != nil { if f.Pkg != nil {
fmt.Fprintf(w, "# Package: %s\n", f.Pkg.Object.Path()) fmt.Fprintf(buf, "# Package: %s\n", f.Pkg.Object.Path())
} }
if syn := f.Synthetic; syn != "" { if syn := f.Synthetic; syn != "" {
fmt.Fprintln(w, "# Synthetic:", syn) fmt.Fprintln(buf, "# Synthetic:", syn)
} }
if pos := f.Pos(); pos.IsValid() { if pos := f.Pos(); pos.IsValid() {
fmt.Fprintf(w, "# Location: %s\n", f.Prog.Fset.Position(pos)) fmt.Fprintf(buf, "# Location: %s\n", f.Prog.Fset.Position(pos))
} }
if f.Enclosing != nil { if f.Enclosing != nil {
fmt.Fprintf(w, "# Parent: %s\n", f.Enclosing.Name()) fmt.Fprintf(buf, "# Parent: %s\n", f.Enclosing.Name())
} }
if f.Recover != nil { if f.Recover != nil {
fmt.Fprintf(w, "# Recover: %s\n", f.Recover) fmt.Fprintf(buf, "# Recover: %s\n", f.Recover)
} }
pkgobj := f.pkgobj() pkgobj := f.pkgobj()
if f.FreeVars != nil { if f.FreeVars != nil {
io.WriteString(w, "# Free variables:\n") buf.WriteString("# Free variables:\n")
for i, fv := range f.FreeVars { for i, fv := range f.FreeVars {
fmt.Fprintf(w, "# % 3d:\t%s %s\n", i, fv.Name(), relType(fv.Type(), pkgobj)) fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, fv.Name(), relType(fv.Type(), pkgobj))
} }
} }
if len(f.Locals) > 0 { if len(f.Locals) > 0 {
io.WriteString(w, "# Locals:\n") buf.WriteString("# Locals:\n")
for i, l := range f.Locals { for i, l := range f.Locals {
fmt.Fprintf(w, "# % 3d:\t%s %s\n", i, l.Name(), relType(deref(l.Type()), pkgobj)) fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, l.Name(), relType(deref(l.Type()), pkgobj))
} }
} }
writeSignature(w, pkgobj, f.Name(), f.Signature, f.Params) writeSignature(buf, pkgobj, f.Name(), f.Signature, f.Params)
io.WriteString(w, ":\n") buf.WriteString(":\n")
if f.Blocks == nil { if f.Blocks == nil {
io.WriteString(w, "\t(external)\n") buf.WriteString("\t(external)\n")
} }
// NB. column calculations are confused by non-ASCII characters. // NB. column calculations are confused by non-ASCII characters.
@ -573,42 +578,42 @@ func (f *Function) DumpTo(w io.Writer) {
for _, b := range f.Blocks { for _, b := range f.Blocks {
if b == nil { if b == nil {
// Corrupt CFG. // Corrupt CFG.
fmt.Fprintf(w, ".nil:\n") fmt.Fprintf(buf, ".nil:\n")
continue continue
} }
n, _ := fmt.Fprintf(w, ".%s:", b) n, _ := fmt.Fprintf(buf, ".%s:", b)
fmt.Fprintf(w, "%*sP:%d S:%d\n", punchcard-1-n-len("P:n S:n"), "", len(b.Preds), len(b.Succs)) fmt.Fprintf(buf, "%*sP:%d S:%d\n", punchcard-1-n-len("P:n S:n"), "", len(b.Preds), len(b.Succs))
if false { // CFG debugging if false { // CFG debugging
fmt.Fprintf(w, "\t# CFG: %s --> %s --> %s\n", b.Preds, b, b.Succs) fmt.Fprintf(buf, "\t# CFG: %s --> %s --> %s\n", b.Preds, b, b.Succs)
} }
for _, instr := range b.Instrs { for _, instr := range b.Instrs {
io.WriteString(w, "\t") buf.WriteString("\t")
switch v := instr.(type) { switch v := instr.(type) {
case Value: case Value:
l := punchcard l := punchcard
// Left-align the instruction. // Left-align the instruction.
if name := v.Name(); name != "" { if name := v.Name(); name != "" {
n, _ := fmt.Fprintf(w, "%s = ", name) n, _ := fmt.Fprintf(buf, "%s = ", name)
l -= n l -= n
} }
// TODO(adonovan): append instructions directly to w. // TODO(adonovan): append instructions directly to w.
n, _ := io.WriteString(w, instr.String()) n, _ := buf.WriteString(instr.String())
l -= n l -= n
// Right-align the type. // Right-align the type.
if t := v.Type(); t != nil { if t := v.Type(); t != nil {
fmt.Fprintf(w, " %*s", l-10, relType(t, pkgobj)) fmt.Fprintf(buf, " %*s", l-10, relType(t, pkgobj))
} }
case nil: case nil:
// Be robust against bad transforms. // Be robust against bad transforms.
io.WriteString(w, "<deleted>") buf.WriteString("<deleted>")
default: default:
io.WriteString(w, instr.String()) buf.WriteString(instr.String())
} }
io.WriteString(w, "\n") buf.WriteString("\n")
} }
} }
fmt.Fprintf(w, "\n") fmt.Fprintf(buf, "\n")
} }
// newBasicBlock adds to f a new basic block and returns it. It does // newBasicBlock adds to f a new basic block and returns it. It does

View File

@ -352,109 +352,109 @@ func copyVal(v value) value {
// Prints in the style of built-in println. // Prints in the style of built-in println.
// (More or less; in gc println is actually a compiler intrinsic and // (More or less; in gc println is actually a compiler intrinsic and
// can distinguish println(1) from println(interface{}(1)).) // can distinguish println(1) from println(interface{}(1)).)
func toWriter(w io.Writer, v value) { func writeValue(buf *bytes.Buffer, v value) {
switch v := v.(type) { switch v := v.(type) {
case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, string: case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, string:
fmt.Fprintf(w, "%v", v) fmt.Fprintf(buf, "%v", v)
case map[value]value: case map[value]value:
io.WriteString(w, "map[") buf.WriteString("map[")
sep := "" sep := ""
for k, e := range v { for k, e := range v {
io.WriteString(w, sep) buf.WriteString(sep)
sep = " " sep = " "
toWriter(w, k) writeValue(buf, k)
io.WriteString(w, ":") buf.WriteString(":")
toWriter(w, e) writeValue(buf, e)
} }
io.WriteString(w, "]") buf.WriteString("]")
case *hashmap: case *hashmap:
io.WriteString(w, "map[") buf.WriteString("map[")
sep := " " sep := " "
for _, e := range v.table { for _, e := range v.table {
for e != nil { for e != nil {
io.WriteString(w, sep) buf.WriteString(sep)
sep = " " sep = " "
toWriter(w, e.key) writeValue(buf, e.key)
io.WriteString(w, ":") buf.WriteString(":")
toWriter(w, e.value) writeValue(buf, e.value)
e = e.next e = e.next
} }
} }
io.WriteString(w, "]") buf.WriteString("]")
case chan value: case chan value:
fmt.Fprintf(w, "%v", v) // (an address) fmt.Fprintf(buf, "%v", v) // (an address)
case *value: case *value:
if v == nil { if v == nil {
io.WriteString(w, "<nil>") buf.WriteString("<nil>")
} else { } else {
fmt.Fprintf(w, "%p", v) fmt.Fprintf(buf, "%p", v)
} }
case iface: case iface:
fmt.Fprintf(w, "(%s, ", v.t) fmt.Fprintf(buf, "(%s, ", v.t)
toWriter(w, v.v) writeValue(buf, v.v)
io.WriteString(w, ")") buf.WriteString(")")
case structure: case structure:
io.WriteString(w, "{") buf.WriteString("{")
for i, e := range v { for i, e := range v {
if i > 0 { if i > 0 {
io.WriteString(w, " ") buf.WriteString(" ")
} }
toWriter(w, e) writeValue(buf, e)
} }
io.WriteString(w, "}") buf.WriteString("}")
case array: case array:
io.WriteString(w, "[") buf.WriteString("[")
for i, e := range v { for i, e := range v {
if i > 0 { if i > 0 {
io.WriteString(w, " ") buf.WriteString(" ")
} }
toWriter(w, e) writeValue(buf, e)
} }
io.WriteString(w, "]") buf.WriteString("]")
case []value: case []value:
io.WriteString(w, "[") buf.WriteString("[")
for i, e := range v { for i, e := range v {
if i > 0 { if i > 0 {
io.WriteString(w, " ") buf.WriteString(" ")
} }
toWriter(w, e) writeValue(buf, e)
} }
io.WriteString(w, "]") buf.WriteString("]")
case *ssa.Function, *ssa.Builtin, *closure: case *ssa.Function, *ssa.Builtin, *closure:
fmt.Fprintf(w, "%p", v) // (an address) fmt.Fprintf(buf, "%p", v) // (an address)
case rtype: case rtype:
io.WriteString(w, v.t.String()) buf.WriteString(v.t.String())
case tuple: case tuple:
// Unreachable in well-formed Go programs // Unreachable in well-formed Go programs
io.WriteString(w, "(") buf.WriteString("(")
for i, e := range v { for i, e := range v {
if i > 0 { if i > 0 {
io.WriteString(w, ", ") buf.WriteString(", ")
} }
toWriter(w, e) writeValue(buf, e)
} }
io.WriteString(w, ")") buf.WriteString(")")
default: default:
fmt.Fprintf(w, "<%T>", v) fmt.Fprintf(buf, "<%T>", v)
} }
} }
// Implements printing of Go values in the style of built-in println. // Implements printing of Go values in the style of built-in println.
func toString(v value) string { func toString(v value) string {
var b bytes.Buffer var b bytes.Buffer
toWriter(&b, v) writeValue(&b, v)
return b.String() return b.String()
} }

View File

@ -7,6 +7,9 @@ package ssa
// This file implements the String() methods for all Value and // This file implements the String() methods for all Value and
// Instruction types. // Instruction types.
// TODO(adonovan): define WriteValue(*bytes.Buffer) and avoid creation
// of garbage.
import ( import (
"bytes" "bytes"
"fmt" "fmt"
@ -364,8 +367,18 @@ func (p *Package) String() string {
return "package " + p.Object.Path() return "package " + p.Object.Path()
} }
func (p *Package) DumpTo(w io.Writer) { var _ io.WriterTo = (*Package)(nil) // *Package implements io.Writer
fmt.Fprintf(w, "%s:\n", p)
func (p *Package) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
WritePackage(&buf, p)
n, err := w.Write(buf.Bytes())
return int64(n), err
}
// WritePackage writes to buf a human-readable summary of p.
func WritePackage(buf *bytes.Buffer, p *Package) {
fmt.Fprintf(buf, "%s:\n", p)
var names []string var names []string
maxname := 0 maxname := 0
@ -380,27 +393,27 @@ func (p *Package) DumpTo(w io.Writer) {
for _, name := range names { for _, name := range names {
switch mem := p.Members[name].(type) { switch mem := p.Members[name].(type) {
case *NamedConst: case *NamedConst:
fmt.Fprintf(w, " const %-*s %s = %s\n", fmt.Fprintf(buf, " const %-*s %s = %s\n",
maxname, name, mem.Name(), mem.Value.RelString(p.Object)) maxname, name, mem.Name(), mem.Value.RelString(p.Object))
case *Function: case *Function:
fmt.Fprintf(w, " func %-*s %s\n", fmt.Fprintf(buf, " func %-*s %s\n",
maxname, name, types.TypeString(p.Object, mem.Type())) maxname, name, types.TypeString(p.Object, mem.Type()))
case *Type: case *Type:
fmt.Fprintf(w, " type %-*s %s\n", fmt.Fprintf(buf, " type %-*s %s\n",
maxname, name, types.TypeString(p.Object, mem.Type().Underlying())) maxname, name, types.TypeString(p.Object, mem.Type().Underlying()))
for _, meth := range IntuitiveMethodSet(mem.Type()) { for _, meth := range IntuitiveMethodSet(mem.Type()) {
fmt.Fprintf(w, " %s\n", types.SelectionString(p.Object, meth)) fmt.Fprintf(buf, " %s\n", types.SelectionString(p.Object, meth))
} }
case *Global: case *Global:
fmt.Fprintf(w, " var %-*s %s\n", fmt.Fprintf(buf, " var %-*s %s\n",
maxname, name, types.TypeString(p.Object, mem.Type().(*types.Pointer).Elem())) maxname, name, types.TypeString(p.Object, mem.Type().(*types.Pointer).Elem()))
} }
} }
fmt.Fprintf(w, "\n") fmt.Fprintf(buf, "\n")
} }
// IntuitiveMethodSet returns the intuitive method set of a type, T. // IntuitiveMethodSet returns the intuitive method set of a type, T.

View File

@ -43,7 +43,7 @@ func sanityCheck(fn *Function, reporter io.Writer) bool {
// //
func mustSanityCheck(fn *Function, reporter io.Writer) { func mustSanityCheck(fn *Function, reporter io.Writer) {
if !sanityCheck(fn, reporter) { if !sanityCheck(fn, reporter) {
fn.DumpTo(os.Stderr) fn.WriteTo(os.Stderr)
panic("SanityCheck failed") panic("SanityCheck failed")
} }
} }

View File

@ -213,7 +213,7 @@ func TestValueForExpr(t *testing.T) {
// debugging // debugging
for _, mem := range mainPkg.Members { for _, mem := range mainPkg.Members {
if fn, ok := mem.(*ssa.Function); ok { if fn, ok := mem.(*ssa.Function); ok {
fn.DumpTo(os.Stderr) fn.WriteTo(os.Stderr)
} }
} }
} }

View File

@ -128,7 +128,7 @@ func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package {
testmain.Members["main"] = main testmain.Members["main"] = main
if prog.mode&LogPackages != 0 { if prog.mode&LogPackages != 0 {
testmain.DumpTo(os.Stderr) testmain.WriteTo(os.Stderr)
} }
if prog.mode&SanityCheckFunctions != 0 { if prog.mode&SanityCheckFunctions != 0 {