1
0
mirror of https://github.com/golang/go synced 2024-10-05 20:21:21 -06:00

[dev.ssa] cmd/compile/internal/ssa: add a String() method to Func

The string method has the same output as printFunc.

Change-Id: Iab2ebc17a3d6418edfeb7b585e4f251e7a11f399
Reviewed-on: https://go-review.googlesource.com/10552
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Michael Matloob 2015-05-30 13:17:12 -04:00 committed by Keith Randall
parent a9cec30fdc
commit bd95412d23
2 changed files with 34 additions and 23 deletions

View File

@ -324,11 +324,9 @@ func TestEquiv(t *testing.T) {
} }
for _, c := range equivalentCases { for _, c := range equivalentCases {
if !Equiv(c.f.f, c.g.f) { if !Equiv(c.f.f, c.g.f) {
t.Errorf("expected equivalence. Func definitions:") t.Error("expected equivalence. Func definitions:")
// TODO(matloob): Rewrite PrintFunc to output to a string or writer, t.Error(c.f.f)
// so the functions can be written to the error log. t.Error(c.g.f)
PrintFunc(c.f.f)
PrintFunc(c.g.f)
} }
} }
@ -394,11 +392,9 @@ func TestEquiv(t *testing.T) {
} }
for _, c := range differentCases { for _, c := range differentCases {
if Equiv(c.f.f, c.g.f) { if Equiv(c.f.f, c.g.f) {
t.Errorf("expected difference. Func definitions:") t.Error("expected difference. Func definitions:")
// TODO(matloob): Rewrite PrintFunc to output to a string or writer, t.Error(c.f.f)
// so the functions can be written to the error log. t.Error(c.g.f)
PrintFunc(c.f.f)
PrintFunc(c.g.f)
} }
} }
} }

View File

@ -4,15 +4,30 @@
package ssa package ssa
import "fmt" import (
"bytes"
"fmt"
"io"
"os"
)
func printFunc(f *Func) { func printFunc(f *Func) {
fmt.Print(f.Name) fprintFunc(os.Stdout, f)
fmt.Print(" ") }
fmt.Println(f.Type)
func (f *Func) String() string {
var buf bytes.Buffer
fprintFunc(&buf, f)
return buf.String()
}
func fprintFunc(w io.Writer, f *Func) {
fmt.Fprint(w, f.Name)
fmt.Fprint(w, " ")
fmt.Fprintln(w, f.Type)
printed := make([]bool, f.NumValues()) printed := make([]bool, f.NumValues())
for _, b := range f.Blocks { for _, b := range f.Blocks {
fmt.Printf(" b%d:\n", b.ID) fmt.Fprintf(w, " b%d:\n", b.ID)
n := 0 n := 0
// print phis first since all value cycles contain a phi // print phis first since all value cycles contain a phi
@ -20,8 +35,8 @@ func printFunc(f *Func) {
if v.Op != OpPhi { if v.Op != OpPhi {
continue continue
} }
fmt.Print(" ") fmt.Fprint(w, " ")
fmt.Println(v.LongString()) fmt.Fprintln(w, v.LongString())
printed[v.ID] = true printed[v.ID] = true
n++ n++
} }
@ -39,25 +54,25 @@ func printFunc(f *Func) {
continue outer continue outer
} }
} }
fmt.Print(" ") fmt.Fprint(w, " ")
fmt.Println(v.LongString()) fmt.Fprintln(w, v.LongString())
printed[v.ID] = true printed[v.ID] = true
n++ n++
} }
if m == n { if m == n {
fmt.Println("dependency cycle!") fmt.Fprintln(w, "dependency cycle!")
for _, v := range b.Values { for _, v := range b.Values {
if printed[v.ID] { if printed[v.ID] {
continue continue
} }
fmt.Print(" ") fmt.Fprint(w, " ")
fmt.Println(v.LongString()) fmt.Fprintln(w, v.LongString())
printed[v.ID] = true printed[v.ID] = true
n++ n++
} }
} }
} }
fmt.Println(" " + b.LongString()) fmt.Fprintln(w, " "+b.LongString())
} }
} }