import (
"cmd/compile/internal/types"
"fmt"
+ "io"
"strconv"
"strings"
"unicode/utf8"
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) {
fmt.Printf("%s [%p]%+v\n", s, n, n)
}
func buildssa(fn *Node, worker int) *ssa.Func {
name := fn.funcname()
printssa := name == ssaDump
+ var astBuf *bytes.Buffer
if printssa {
- fmt.Println("generating SSA for", name)
- dumplist("buildssa-enter", fn.Func.Enter)
- dumplist("buildssa-body", fn.Nbody)
- dumplist("buildssa-exit", fn.Func.Exit)
+ astBuf = &bytes.Buffer{}
+ fdumplist(astBuf, "buildssa-enter", fn.Func.Enter)
+ fdumplist(astBuf, "buildssa-body", fn.Nbody)
+ fdumplist(astBuf, "buildssa-exit", fn.Func.Exit)
+ if ssaDumpStdout {
+ fmt.Println("generating SSA for", name)
+ fmt.Print(astBuf.String())
+ }
}
var s state
s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDumpFile, s.f.Frontend(), name)
// TODO: generate and print a mapping from nodes to values and blocks
dumpSourcesColumn(s.f.HTMLWriter, fn)
+ s.f.HTMLWriter.WriteAST("AST", astBuf)
}
// Allocate starting block
"io"
"os"
"path/filepath"
+ "strconv"
"strings"
)
text-align: right;
}
-code, pre, .lines {
+code, pre, .lines, .ast {
font-family: Menlo, monospace;
font-size: 12px;
}
+.allow-x-scroll {
+ overflow-x: scroll;
+}
+
.lines {
float: left;
overflow: hidden;
font-size: 12px;
}
+.ast {
+ white-space: nowrap;
+}
+
td.ssa-prog {
width: 600px;
word-wrap: break-word;
}
}
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 = " "
+ } 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.