Commit 35fb5145 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

[dev.ssa] cmd/compile: add HTML SSA printer

This is an initial implementation.
There are many rough edges and TODOs,
which will hopefully be polished out
with use.

Fixes #12071.

Change-Id: I1d6fd5a343063b5200623bceef2c2cfcc885794e
Reviewed-on: https://go-review.googlesource.com/13472Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 3e7904b6
......@@ -5,7 +5,9 @@
package gc
import (
"bytes"
"fmt"
"html"
"os"
"strings"
......@@ -40,6 +42,18 @@ func buildssa(fn *Node) (ssafn *ssa.Func, usessa bool) {
s.f = s.config.NewFunc()
s.f.Name = name
if name == os.Getenv("GOSSAFUNC") {
// TODO: tempfile? it is handy to have the location
// of this file be stable, so you can just reload in the browser.
s.config.HTML = ssa.NewHTMLWriter("ssa.html", &s, name)
// TODO: generate and print a mapping from nodes to values and blocks
}
defer func() {
if !usessa {
s.config.HTML.Close()
}
}()
// If SSA support for the function is incomplete,
// assume that any panics are due to violated
// invariants. Swallow them silently.
......@@ -1811,6 +1825,30 @@ func genssa(f *ssa.Func, ptxt *obj.Prog, gcargs, gclocals *Sym) {
}
f.Logf("%s\t%s\n", s, p)
}
if f.Config.HTML != nil {
saved := ptxt.Ctxt.LineHist.PrintFilenameOnly
ptxt.Ctxt.LineHist.PrintFilenameOnly = true
var buf bytes.Buffer
buf.WriteString("<code>")
buf.WriteString("<dl class=\"ssa-gen\">")
for p := ptxt; p != nil; p = p.Link {
buf.WriteString("<dt class=\"ssa-prog-src\">")
if v, ok := valueProgs[p]; ok {
buf.WriteString(v.HTML())
} else if b, ok := blockProgs[p]; ok {
buf.WriteString(b.HTML())
}
buf.WriteString("</dt>")
buf.WriteString("<dd class=\"ssa-prog\">")
buf.WriteString(html.EscapeString(p.String()))
buf.WriteString("</dd>")
buf.WriteString("</li>")
}
buf.WriteString("</dl>")
buf.WriteString("</code>")
f.Config.HTML.WriteColumn("genssa", buf.String())
ptxt.Ctxt.LineHist.PrintFilenameOnly = saved
}
}
// Emit static data
......@@ -1834,6 +1872,8 @@ func genssa(f *ssa.Func, ptxt *obj.Prog, gcargs, gclocals *Sym) {
ggloblsym(gcargs, 4, obj.RODATA|obj.DUPOK)
duint32(gclocals, 0, 0)
ggloblsym(gclocals, 4, obj.RODATA|obj.DUPOK)
f.Config.HTML.Close()
}
func genValue(v *ssa.Value) {
......
......@@ -34,13 +34,16 @@ func Compile(f *Func) {
// Run all the passes
printFunc(f)
f.Config.HTML.WriteFunc("start", f)
checkFunc(f)
for _, p := range passes {
phaseName = p.name
f.Logf(" pass %s begin\n", p.name)
// TODO: capture logging during this pass, add it to the HTML
p.fn(f)
f.Logf(" pass %s end\n", p.name)
printFunc(f)
f.Config.HTML.WriteFunc("after "+phaseName, f)
checkFunc(f)
}
......
......@@ -11,6 +11,7 @@ type Config struct {
lowerBlock func(*Block) bool // lowering function
lowerValue func(*Value, *Config) bool // lowering function
fe Frontend // callbacks into compiler frontend
HTML *HTMLWriter // html writer, for debugging
// TODO: more stuff. Compiler flags of interest, ...
}
......@@ -31,12 +32,7 @@ type TypeSource interface {
TypeBytePtr() Type // TODO: use unsafe.Pointer instead?
}
type Frontend interface {
TypeSource
// StringData returns a symbol pointing to the given string's contents.
StringData(string) interface{} // returns *gc.Sym
type Logger interface {
// Log logs a message from the compiler.
Logf(string, ...interface{})
......@@ -48,6 +44,14 @@ type Frontend interface {
Unimplementedf(msg string, args ...interface{})
}
type Frontend interface {
TypeSource
Logger
// StringData returns a symbol pointing to the given string's contents.
StringData(string) interface{} // returns *gc.Sym
}
// NewConfig returns a new configuration object for the given architecture.
func NewConfig(arch string, fe Frontend) *Config {
c := &Config{arch: arch, fe: fe}
......
......@@ -4,10 +4,10 @@
package ssa
// deadcode removes dead code from f.
func deadcode(f *Func) {
// findlive returns the reachable blocks and live values in f.
func findlive(f *Func) (reachable []bool, live []bool) {
// Find all reachable basic blocks.
reachable := make([]bool, f.NumBlocks())
reachable = make([]bool, f.NumBlocks())
reachable[f.Entry.ID] = true
p := []*Block{f.Entry} // stack-like worklist
for len(p) > 0 {
......@@ -24,8 +24,8 @@ func deadcode(f *Func) {
}
// Find all live values
live := make([]bool, f.NumValues()) // flag to set for each live value
var q []*Value // stack-like worklist of unscanned values
live = make([]bool, f.NumValues()) // flag to set for each live value
var q []*Value // stack-like worklist of unscanned values
// Starting set: all control values of reachable blocks are live.
for _, b := range f.Blocks {
......@@ -54,6 +54,13 @@ func deadcode(f *Func) {
}
}
return reachable, live
}
// deadcode removes dead code from f.
func deadcode(f *Func) {
reachable, live := findlive(f)
// Remove dead values from blocks' value list. Return dead
// value ids to the allocator.
for _, b := range f.Blocks {
......
This diff is collapsed.
......@@ -16,33 +16,77 @@ func printFunc(f *Func) {
func (f *Func) String() string {
var buf bytes.Buffer
fprintFunc(&buf, f)
p := stringFuncPrinter{w: &buf}
fprintFunc(p, f)
return buf.String()
}
func fprintFunc(w io.Writer, f *Func) {
fmt.Fprint(w, f.Name)
fmt.Fprint(w, " ")
fmt.Fprintln(w, f.Type)
type funcPrinter interface {
header(f *Func)
startBlock(b *Block, reachable bool)
endBlock(b *Block)
value(v *Value, live bool)
startDepCycle()
endDepCycle()
}
type stringFuncPrinter struct {
w io.Writer
}
func (p stringFuncPrinter) header(f *Func) {
fmt.Fprint(p.w, f.Name)
fmt.Fprint(p.w, " ")
fmt.Fprintln(p.w, f.Type)
}
func (p stringFuncPrinter) startBlock(b *Block, reachable bool) {
fmt.Fprintf(p.w, " b%d:", b.ID)
if len(b.Preds) > 0 {
io.WriteString(p.w, " <-")
for _, pred := range b.Preds {
fmt.Fprintf(p.w, " b%d", pred.ID)
}
}
if !reachable {
fmt.Fprint(p.w, " DEAD")
}
io.WriteString(p.w, "\n")
}
func (p stringFuncPrinter) endBlock(b *Block) {
fmt.Fprintln(p.w, " "+b.LongString())
}
func (p stringFuncPrinter) value(v *Value, live bool) {
fmt.Fprint(p.w, " ")
fmt.Fprint(p.w, v.LongString())
if !live {
fmt.Fprint(p.w, " DEAD")
}
fmt.Fprintln(p.w)
}
func (p stringFuncPrinter) startDepCycle() {
fmt.Fprintln(p.w, "dependency cycle!")
}
func (p stringFuncPrinter) endDepCycle() {}
func fprintFunc(p funcPrinter, f *Func) {
reachable, live := findlive(f)
p.header(f)
printed := make([]bool, f.NumValues())
for _, b := range f.Blocks {
fmt.Fprintf(w, " b%d:", b.ID)
if len(b.Preds) > 0 {
io.WriteString(w, " <-")
for _, pred := range b.Preds {
fmt.Fprintf(w, " b%d", pred.ID)
}
}
io.WriteString(w, "\n")
p.startBlock(b, reachable[b.ID])
if f.scheduled {
// Order of Values has been decided - print in that order.
for _, v := range b.Values {
fmt.Fprint(w, " ")
fmt.Fprintln(w, v.LongString())
p.value(v, live[v.ID])
printed[v.ID] = true
}
fmt.Fprintln(w, " "+b.LongString())
p.endBlock(b)
continue
}
......@@ -52,8 +96,7 @@ func fprintFunc(w io.Writer, f *Func) {
if v.Op != OpPhi {
continue
}
fmt.Fprint(w, " ")
fmt.Fprintln(w, v.LongString())
p.value(v, live[v.ID])
printed[v.ID] = true
n++
}
......@@ -73,25 +116,24 @@ func fprintFunc(w io.Writer, f *Func) {
continue outer
}
}
fmt.Fprint(w, " ")
fmt.Fprintln(w, v.LongString())
p.value(v, live[v.ID])
printed[v.ID] = true
n++
}
if m == n {
fmt.Fprintln(w, "dependency cycle!")
p.startDepCycle()
for _, v := range b.Values {
if printed[v.ID] {
continue
}
fmt.Fprint(w, " ")
fmt.Fprintln(w, v.LongString())
p.value(v, live[v.ID])
printed[v.ID] = true
n++
}
p.endDepCycle()
}
}
fmt.Fprintln(w, " "+b.LongString())
p.endBlock(b)
}
}
......@@ -25,12 +25,13 @@ import (
// together, so that given (only) calls Push(10, "x.go", 1) and Pop(15),
// virtual line 12 corresponds to x.go line 3.
type LineHist struct {
Top *LineStack // current top of stack
Ranges []LineRange // ranges for lookup
Dir string // directory to qualify relative paths
TrimPathPrefix string // remove leading TrimPath from recorded file names
GOROOT string // current GOROOT
GOROOT_FINAL string // target GOROOT
Top *LineStack // current top of stack
Ranges []LineRange // ranges for lookup
Dir string // directory to qualify relative paths
TrimPathPrefix string // remove leading TrimPath from recorded file names
PrintFilenameOnly bool // ignore path when pretty-printing a line; internal use only
GOROOT string // current GOROOT
GOROOT_FINAL string // target GOROOT
}
// A LineStack is an entry in the recorded line history.
......@@ -221,20 +222,24 @@ func (h *LineHist) LineString(lineno int) string {
return "<unknown line number>"
}
text := fmt.Sprintf("%s:%d", stk.File, stk.fileLineAt(lineno))
filename := stk.File
if h.PrintFilenameOnly {
filename = filepath.Base(filename)
}
text := fmt.Sprintf("%s:%d", filename, stk.fileLineAt(lineno))
if stk.Directive && stk.Parent != nil {
stk = stk.Parent
text += fmt.Sprintf("[%s:%d]", stk.File, stk.fileLineAt(lineno))
text += fmt.Sprintf("[%s:%d]", filename, stk.fileLineAt(lineno))
}
const showFullStack = false // was used by old C compilers
if showFullStack {
for stk.Parent != nil {
lineno = stk.Lineno - 1
stk = stk.Parent
text += fmt.Sprintf(" %s:%d", stk.File, stk.fileLineAt(lineno))
text += fmt.Sprintf(" %s:%d", filename, stk.fileLineAt(lineno))
if stk.Directive && stk.Parent != nil {
stk = stk.Parent
text += fmt.Sprintf("[%s:%d]", stk.File, stk.fileLineAt(lineno))
text += fmt.Sprintf("[%s:%d]", filename, stk.fileLineAt(lineno))
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment