2015-03-03 13:38:14 -08:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package ssa
|
|
|
|
|
|
2015-05-30 13:17:12 -04:00
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
)
|
2015-03-03 13:38:14 -08:00
|
|
|
|
|
|
|
|
func printFunc(f *Func) {
|
2015-06-24 14:03:39 -07:00
|
|
|
f.Logf("%s", f.String())
|
2015-05-30 13:17:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2015-03-03 13:38:14 -08:00
|
|
|
printed := make([]bool, f.NumValues())
|
|
|
|
|
for _, b := range f.Blocks {
|
2015-05-30 13:17:12 -04:00
|
|
|
fmt.Fprintf(w, " b%d:\n", b.ID)
|
2015-03-03 13:38:14 -08:00
|
|
|
n := 0
|
|
|
|
|
|
|
|
|
|
// print phis first since all value cycles contain a phi
|
|
|
|
|
for _, v := range b.Values {
|
|
|
|
|
if v.Op != OpPhi {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-05-30 13:17:12 -04:00
|
|
|
fmt.Fprint(w, " ")
|
|
|
|
|
fmt.Fprintln(w, v.LongString())
|
2015-03-03 13:38:14 -08:00
|
|
|
printed[v.ID] = true
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// print rest of values in dependency order
|
|
|
|
|
for n < len(b.Values) {
|
|
|
|
|
m := n
|
|
|
|
|
outer:
|
|
|
|
|
for _, v := range b.Values {
|
|
|
|
|
if printed[v.ID] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, w := range v.Args {
|
|
|
|
|
if w.Block == b && !printed[w.ID] {
|
|
|
|
|
continue outer
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-30 13:17:12 -04:00
|
|
|
fmt.Fprint(w, " ")
|
|
|
|
|
fmt.Fprintln(w, v.LongString())
|
2015-03-03 13:38:14 -08:00
|
|
|
printed[v.ID] = true
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
if m == n {
|
2015-05-30 13:17:12 -04:00
|
|
|
fmt.Fprintln(w, "dependency cycle!")
|
2015-03-03 13:38:14 -08:00
|
|
|
for _, v := range b.Values {
|
|
|
|
|
if printed[v.ID] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-05-30 13:17:12 -04:00
|
|
|
fmt.Fprint(w, " ")
|
|
|
|
|
fmt.Fprintln(w, v.LongString())
|
2015-03-03 13:38:14 -08:00
|
|
|
printed[v.ID] = true
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-30 13:17:12 -04:00
|
|
|
fmt.Fprintln(w, " "+b.LongString())
|
2015-03-03 13:38:14 -08:00
|
|
|
}
|
|
|
|
|
}
|