Commit 0e930015 authored by Keith Randall's avatar Keith Randall

cmd/compile: log rules to a file for rule coverage tool

When rules are generated with -log, log rule application to a file.

The file is opened in append mode so multiple calls to the compiler
union their logs.

Change-Id: Ib35c7c85bf58e5909ea9231043f8cbaa6bf278b7
Reviewed-on: https://go-review.googlesource.com/23406Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
parent 13a5b1fa
......@@ -150,9 +150,6 @@ func genRules(arch arch) {
fmt.Fprintln(w, "// generated with: cd gen; go run *.go")
fmt.Fprintln(w)
fmt.Fprintln(w, "package ssa")
if *genLog {
fmt.Fprintln(w, "import \"fmt\"")
}
fmt.Fprintln(w, "import \"math\"")
fmt.Fprintln(w, "var _ = math.MinInt8 // in case not otherwise used")
......@@ -196,7 +193,7 @@ func genRules(arch arch) {
genResult(w, arch, result, rule.loc)
if *genLog {
fmt.Fprintf(w, "fmt.Println(\"rewrite %s\")\n", rule.loc)
fmt.Fprintf(w, "logRule(\"%s\")\n", rule.loc)
}
fmt.Fprintf(w, "return true\n")
......@@ -300,7 +297,7 @@ func genRules(arch arch) {
}
if *genLog {
fmt.Fprintf(w, "fmt.Println(\"rewrite %s\")\n", rule.loc)
fmt.Fprintf(w, "logRule(\"%s\")\n", rule.loc)
}
fmt.Fprintf(w, "return true\n")
......
......@@ -7,6 +7,8 @@ package ssa
import (
"fmt"
"math"
"os"
"path/filepath"
)
func applyRewrite(f *Func, rb func(*Block) bool, rv func(*Value, *Config) bool) {
......@@ -357,3 +359,28 @@ func clobber(v *Value) bool {
// Note: leave v.Block intact. The Block field is used after clobber.
return true
}
// logRule logs the use of the rule s. This will only be enabled if
// rewrite rules were generated with the -log option, see gen/rulegen.go.
func logRule(s string) {
if ruleFile == nil {
// Open a log file to write log to. We open in append
// mode because all.bash runs the compiler lots of times,
// and we want the concatenation of all of those logs.
// This means, of course, that users need to rm the old log
// to get fresh data.
// TODO: all.bash runs compilers in parallel. Need to synchronize logging somehow?
w, err := os.OpenFile(filepath.Join(os.Getenv("GOROOT"), "src", "rulelog"),
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
ruleFile = w
}
_, err := fmt.Fprintf(ruleFile, "rewrite %s\n", s)
if err != nil {
panic(err)
}
}
var ruleFile *os.File
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