diff options
author | Russ Cox <rsc@golang.org> | 2015-05-21 13:28:10 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2015-05-21 17:31:51 +0000 |
commit | 17eba6e6b72b9dbf24d73a84be22edd65c229631 (patch) | |
tree | c0adc98c7629331795787f4facd558fa3b4e29dd /src/cmd/compile/internal/gc/util.go | |
parent | 2a141dedc4bb7b33f5caee6e7b185dfbd20c92bc (diff) | |
download | go-17eba6e6b72b9dbf24d73a84be22edd65c229631.tar.gz go-17eba6e6b72b9dbf24d73a84be22edd65c229631.zip |
cmd/compile, cmd/link: create from 5g, 5l, etc
Trivial merging of 5g, 6g, ... into go tool compile,
and similarlly 5l, 6l, ... into go tool link.
The files compile/main.go and link/main.go are new.
Everything else in those directories is a move followed by
change of imports and package name.
This CL breaks the build. Manual fixups are in the next CL.
See golang-dev thread titled "go tool compile, etc" for background.
Change-Id: Id35ff5a5859ad9037c61275d637b1bd51df6828b
Reviewed-on: https://go-review.googlesource.com/10287
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/gc/util.go')
-rw-r--r-- | src/cmd/compile/internal/gc/util.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/util.go b/src/cmd/compile/internal/gc/util.go new file mode 100644 index 0000000000..c59af0665b --- /dev/null +++ b/src/cmd/compile/internal/gc/util.go @@ -0,0 +1,103 @@ +package gc + +import ( + "os" + "runtime" + "runtime/pprof" + "strconv" + "strings" +) + +func (n *Node) Line() string { + return Ctxt.LineHist.LineString(int(n.Lineno)) +} + +func atoi(s string) int { + // NOTE: Not strconv.Atoi, accepts hex and octal prefixes. + n, _ := strconv.ParseInt(s, 0, 0) + return int(n) +} + +func isalnum(c int) bool { + return isalpha(c) || isdigit(c) +} + +func isalpha(c int) bool { + return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' +} + +func isdigit(c int) bool { + return '0' <= c && c <= '9' +} + +func plan9quote(s string) string { + if s == "" { + return "'" + strings.Replace(s, "'", "''", -1) + "'" + } + for i := 0; i < len(s); i++ { + if s[i] <= ' ' || s[i] == '\'' { + return "'" + strings.Replace(s, "'", "''", -1) + "'" + } + } + return s +} + +// strings.Compare, introduced in Go 1.5. +func stringsCompare(a, b string) int { + if a == b { + return 0 + } + if a < b { + return -1 + } + return +1 +} + +var atExitFuncs []func() + +func AtExit(f func()) { + atExitFuncs = append(atExitFuncs, f) +} + +func Exit(code int) { + for i := len(atExitFuncs) - 1; i >= 0; i-- { + f := atExitFuncs[i] + atExitFuncs = atExitFuncs[:i] + f() + } + os.Exit(code) +} + +var ( + cpuprofile string + memprofile string + memprofilerate int64 +) + +func startProfile() { + if cpuprofile != "" { + f, err := os.Create(cpuprofile) + if err != nil { + Fatal("%v", err) + } + if err := pprof.StartCPUProfile(f); err != nil { + Fatal("%v", err) + } + AtExit(pprof.StopCPUProfile) + } + if memprofile != "" { + if memprofilerate != 0 { + runtime.MemProfileRate = int(memprofilerate) + } + f, err := os.Create(memprofile) + if err != nil { + Fatal("%v", err) + } + AtExit(func() { + runtime.GC() // profile all outstanding allocations + if err := pprof.WriteHeapProfile(f); err != nil { + Fatal("%v", err) + } + }) + } +} |