Commit 4cc027fb authored by Yury Smolsky's avatar Yury Smolsky Committed by Keith Randall

cmd/compile: display AST IR in ssa.html

This change adds a new column, AST IR. That column contains
nodes for a function specified in $GOSSAFUNC.

Also this CL enables horizontal scrolling of sources and AST columns.

Fixes #26662

Change-Id: I3fba39fd998bb05e9c93038e8ec2384c69613b24
Reviewed-on: https://go-review.googlesource.com/126858
Run-TryBot: Yury Smolsky <yury@smolsky.by>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 97f15352
...@@ -7,6 +7,7 @@ package gc ...@@ -7,6 +7,7 @@ package gc
import ( import (
"cmd/compile/internal/types" "cmd/compile/internal/types"
"fmt" "fmt"
"io"
"strconv" "strconv"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
...@@ -1836,6 +1837,10 @@ func dumplist(s string, l Nodes) { ...@@ -1836,6 +1837,10 @@ func dumplist(s string, l Nodes) {
fmt.Printf("%s%+v\n", s, l) fmt.Printf("%s%+v\n", s, l)
} }
func fdumplist(w io.Writer, s string, l Nodes) {
fmt.Fprintf(w, "%s%+v\n", s, l)
}
func Dump(s string, n *Node) { func Dump(s string, n *Node) {
fmt.Printf("%s [%p]%+v\n", s, n, n) fmt.Printf("%s [%p]%+v\n", s, n, n)
} }
......
...@@ -111,11 +111,16 @@ func initssaconfig() { ...@@ -111,11 +111,16 @@ func initssaconfig() {
func buildssa(fn *Node, worker int) *ssa.Func { func buildssa(fn *Node, worker int) *ssa.Func {
name := fn.funcname() name := fn.funcname()
printssa := name == ssaDump printssa := name == ssaDump
var astBuf *bytes.Buffer
if printssa { if printssa {
fmt.Println("generating SSA for", name) astBuf = &bytes.Buffer{}
dumplist("buildssa-enter", fn.Func.Enter) fdumplist(astBuf, "buildssa-enter", fn.Func.Enter)
dumplist("buildssa-body", fn.Nbody) fdumplist(astBuf, "buildssa-body", fn.Nbody)
dumplist("buildssa-exit", fn.Func.Exit) fdumplist(astBuf, "buildssa-exit", fn.Func.Exit)
if ssaDumpStdout {
fmt.Println("generating SSA for", name)
fmt.Print(astBuf.String())
}
} }
var s state var s state
...@@ -151,6 +156,7 @@ func buildssa(fn *Node, worker int) *ssa.Func { ...@@ -151,6 +156,7 @@ func buildssa(fn *Node, worker int) *ssa.Func {
s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDumpFile, s.f.Frontend(), name) s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDumpFile, s.f.Frontend(), name)
// TODO: generate and print a mapping from nodes to values and blocks // TODO: generate and print a mapping from nodes to values and blocks
dumpSourcesColumn(s.f.HTMLWriter, fn) dumpSourcesColumn(s.f.HTMLWriter, fn)
s.f.HTMLWriter.WriteAST("AST", astBuf)
} }
// Allocate starting block // Allocate starting block
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
...@@ -103,11 +104,15 @@ td.collapsed div { ...@@ -103,11 +104,15 @@ td.collapsed div {
text-align: right; text-align: right;
} }
code, pre, .lines { code, pre, .lines, .ast {
font-family: Menlo, monospace; font-family: Menlo, monospace;
font-size: 12px; font-size: 12px;
} }
.allow-x-scroll {
overflow-x: scroll;
}
.lines { .lines {
float: left; float: left;
overflow: hidden; overflow: hidden;
...@@ -123,6 +128,10 @@ div.line-number { ...@@ -123,6 +128,10 @@ div.line-number {
font-size: 12px; font-size: 12px;
} }
.ast {
white-space: nowrap;
}
td.ssa-prog { td.ssa-prog {
width: 600px; width: 600px;
word-wrap: break-word; word-wrap: break-word;
...@@ -521,7 +530,49 @@ func (w *HTMLWriter) WriteSources(phase string, all []*FuncLines) { ...@@ -521,7 +530,49 @@ func (w *HTMLWriter) WriteSources(phase string, all []*FuncLines) {
} }
} }
fmt.Fprint(&buf, "</pre></div>") fmt.Fprint(&buf, "</pre></div>")
w.WriteColumn(phase, phase, "", buf.String()) w.WriteColumn(phase, phase, "allow-x-scroll", buf.String())
}
func (w *HTMLWriter) WriteAST(phase string, buf *bytes.Buffer) {
if w == nil {
return // avoid generating HTML just to discard it
}
lines := strings.Split(buf.String(), "\n")
var out bytes.Buffer
fmt.Fprint(&out, "<div>")
for _, l := range lines {
l = strings.TrimSpace(l)
var escaped string
var lineNo string
if l == "" {
escaped = "&nbsp;"
} else {
if strings.HasPrefix(l, "buildssa") {
escaped = fmt.Sprintf("<b>%v</b>", l)
} else {
// Parse the line number from the format l(123).
idx := strings.Index(l, " l(")
if idx != -1 {
subl := l[idx+3:]
idxEnd := strings.Index(subl, ")")
if idxEnd != -1 {
if _, err := strconv.Atoi(subl[:idxEnd]); err == nil {
lineNo = subl[:idxEnd]
}
}
}
escaped = html.EscapeString(l)
}
}
if lineNo != "" {
fmt.Fprintf(&out, "<div class=\"l%v line-number ast\">%v</div>", lineNo, escaped)
} else {
fmt.Fprintf(&out, "<div class=\"ast\">%v</div>", escaped)
}
}
fmt.Fprint(&out, "</div>")
w.WriteColumn(phase, phase, "allow-x-scroll", out.String())
} }
// WriteColumn writes raw HTML in a column headed by title. // WriteColumn writes raw HTML in a column headed by title.
......
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