1
0
mirror of https://github.com/golang/go synced 2024-09-30 22:48:32 -06: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
// # Synthetic: package initializer
// func init():
// .0.entry: P:0 S:2
// 0: entry P:0 S:2
// t0 = *init$guard bool
// if t0 goto 2.init.done else 1.init.start
// .1.init.start: P:1 S:1
// if t0 goto 2 else 1
// 1: init.start P:1 S:1
// *init$guard = true:bool
// t1 = fmt.init() ()
// jump 2.init.done
// .2.init.done: P:2 S:0
// jump 2
// 2: init.done P:2 S:0
// return
//
// # Name: main.main
// # Package: main
// # Location: hello.go:8:6
// func main():
// .0.entry: P:0 S:0
// 0: entry P:0 S:0
// t0 = new [1]interface{} (varargs) *[1]interface{}
// t1 = &t0[0:int] *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.
//
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.
@ -575,16 +575,19 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
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 tabwidth = 8
for _, b := range f.Blocks {
if b == nil {
// Corrupt CFG.
fmt.Fprintf(buf, ".nil:\n")
continue
}
n, _ := fmt.Fprintf(buf, ".%s:", b)
fmt.Fprintf(buf, "%*sP:%d S:%d\n", punchcard-1-n-len("P:n S:n"), "", len(b.Preds), len(b.Succs))
n, _ := fmt.Fprintf(buf, "%d:", b.Index)
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
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")
switch v := instr.(type) {
case Value:
l := punchcard
l := punchcard - tabwidth
// Left-align the instruction.
if name := v.Name(); name != "" {
n, _ := fmt.Fprintf(buf, "%s = ", name)
l -= n
}
// TODO(adonovan): append instructions directly to w.
n, _ := buf.WriteString(instr.String())
l -= n
// Right-align the type.
// Right-align the type if there's space.
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:
// Be robust against bad transforms.

View File

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