1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:14:43 -07:00

go.tools/go/ssa: fix incorrect indentation in SSA printout.

Very long instructions caused the printf width spec to go
negative, which causes right-padding, often several lines'
worth.

Also: print the basic block comment once on the RHS. It's too
verbose to print it each time we mention the block.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/97490046
This commit is contained in:
Alan Donovan 2014-05-16 12:37:17 -04:00
parent b752e9ffdf
commit 18c694293a
3 changed files with 32 additions and 25 deletions

View File

@ -85,21 +85,21 @@ func main() {
// # Package: main // # Package: main
// # Synthetic: package initializer // # Synthetic: package initializer
// func init(): // func init():
// .0.entry: P:0 S:2 // 0: entry P:0 S:2
// t0 = *init$guard bool // t0 = *init$guard bool
// if t0 goto 2.init.done else 1.init.start // if t0 goto 2 else 1
// .1.init.start: P:1 S:1 // 1: init.start P:1 S:1
// *init$guard = true:bool // *init$guard = true:bool
// t1 = fmt.init() () // t1 = fmt.init() ()
// jump 2.init.done // jump 2
// .2.init.done: P:2 S:0 // 2: init.done P:2 S:0
// return // return
// //
// # Name: main.main // # Name: main.main
// # Package: main // # Package: main
// # Location: hello.go:8:6 // # Location: hello.go:8:6
// func main(): // func main():
// .0.entry: P:0 S:0 // 0: entry P:0 S:0
// t0 = new [1]interface{} (varargs) *[1]interface{} // t0 = new [1]interface{} (varargs) *[1]interface{}
// t1 = &t0[0:int] *interface{} // t1 = &t0[0:int] *interface{}
// t2 = make interface{} <- string ("Hello, World!":string) interface{} // t2 = make interface{} <- string ("Hello, World!":string) interface{}

View File

@ -31,7 +31,7 @@ func (b *BasicBlock) Parent() *Function { return b.parent }
// It is not guaranteed unique within the function. // It is not guaranteed unique within the function.
// //
func (b *BasicBlock) String() string { func (b *BasicBlock) String() string {
return fmt.Sprintf("%d.%s", b.Index, b.Comment) return fmt.Sprintf("%d", b.Index)
} }
// emit appends an instruction to the current basic block. // emit appends an instruction to the current basic block.
@ -575,16 +575,19 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
buf.WriteString("\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 and assume 8-space tabs.
const punchcard = 80 // for old time's sake. const punchcard = 80 // for old time's sake.
const tabwidth = 8
for _, b := range f.Blocks { for _, b := range f.Blocks {
if b == nil { if b == nil {
// Corrupt CFG. // Corrupt CFG.
fmt.Fprintf(buf, ".nil:\n") fmt.Fprintf(buf, ".nil:\n")
continue continue
} }
n, _ := fmt.Fprintf(buf, ".%s:", b) n, _ := fmt.Fprintf(buf, "%d:", b.Index)
fmt.Fprintf(buf, "%*sP:%d S:%d\n", punchcard-1-n-len("P:n S:n"), "", len(b.Preds), len(b.Succs)) bmsg := fmt.Sprintf("%s P:%d S:%d", b.Comment, len(b.Preds), len(b.Succs))
fmt.Fprintf(buf, "%*s%s\n", punchcard-1-n-len(bmsg), "", bmsg)
if false { // CFG debugging if false { // CFG debugging
fmt.Fprintf(buf, "\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)
@ -593,18 +596,23 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
buf.WriteString("\t") buf.WriteString("\t")
switch v := instr.(type) { switch v := instr.(type) {
case Value: case Value:
l := punchcard l := punchcard - tabwidth
// Left-align the instruction. // Left-align the instruction.
if name := v.Name(); name != "" { if name := v.Name(); name != "" {
n, _ := fmt.Fprintf(buf, "%s = ", name) n, _ := fmt.Fprintf(buf, "%s = ", name)
l -= n l -= n
} }
// TODO(adonovan): append instructions directly to w.
n, _ := buf.WriteString(instr.String()) n, _ := buf.WriteString(instr.String())
l -= n l -= n
// Right-align the type. // Right-align the type if there's space.
if t := v.Type(); t != nil { if t := v.Type(); t != nil {
fmt.Fprintf(buf, " %*s", l-10, relType(t, pkgobj)) buf.WriteByte(' ')
ts := relType(t, pkgobj)
l -= len(ts) + len(" ") // (spaces before and after type)
if l > 0 {
fmt.Fprintf(buf, "%*s", l, "")
}
buf.WriteString(ts)
} }
case nil: case nil:
// Be robust against bad transforms. // Be robust against bad transforms.

View File

@ -89,12 +89,11 @@ func (v *Phi) String() string {
b.WriteString(", ") b.WriteString(", ")
} }
// Be robust against malformed CFG. // Be robust against malformed CFG.
blockname := "?" block := -1
if v.block != nil && i < len(v.block.Preds) { if v.block != nil && i < len(v.block.Preds) {
blockname = v.block.Preds[i].String() block = v.block.Preds[i].Index
} }
b.WriteString(blockname) fmt.Fprintf(&b, "%d: ", block)
b.WriteString(": ")
edgeVal := "<nil>" // be robust edgeVal := "<nil>" // be robust
if edge != nil { if edge != nil {
edgeVal = relName(edge, v) edgeVal = relName(edge, v)
@ -272,21 +271,21 @@ func (v *Extract) String() string {
func (s *Jump) String() string { func (s *Jump) String() string {
// Be robust against malformed CFG. // Be robust against malformed CFG.
blockname := "?" block := -1
if s.block != nil && len(s.block.Succs) == 1 { if s.block != nil && len(s.block.Succs) == 1 {
blockname = s.block.Succs[0].String() block = s.block.Succs[0].Index
} }
return fmt.Sprintf("jump %s", blockname) return fmt.Sprintf("jump %d", block)
} }
func (s *If) String() string { func (s *If) String() string {
// Be robust against malformed CFG. // Be robust against malformed CFG.
tblockname, fblockname := "?", "?" tblock, fblock := -1, -1
if s.block != nil && len(s.block.Succs) == 2 { if s.block != nil && len(s.block.Succs) == 2 {
tblockname = s.block.Succs[0].String() tblock = s.block.Succs[0].Index
fblockname = s.block.Succs[1].String() fblock = s.block.Succs[1].Index
} }
return fmt.Sprintf("if %s goto %s else %s", relName(s.Cond, s), tblockname, fblockname) return fmt.Sprintf("if %s goto %d else %d", relName(s.Cond, s), tblock, fblock)
} }
func (s *Go) String() string { func (s *Go) String() string {